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"
39 * Mark as invalid, all open files on tree connections since they
40 * were closed when session to server was lost.
43 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
45 struct cifsFileInfo *open_file = NULL;
46 struct list_head *tmp;
47 struct list_head *tmp1;
49 /* only send once per connect */
50 spin_lock(&tcon->ses->ses_lock);
51 if ((tcon->ses->ses_status != SES_GOOD) || (tcon->status != TID_NEED_RECON)) {
52 spin_unlock(&tcon->ses->ses_lock);
55 tcon->status = TID_IN_FILES_INVALIDATE;
56 spin_unlock(&tcon->ses->ses_lock);
58 /* list all files open on tree connection and mark them invalid */
59 spin_lock(&tcon->open_file_lock);
60 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
61 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
62 open_file->invalidHandle = true;
63 open_file->oplock_break_cancelled = true;
65 spin_unlock(&tcon->open_file_lock);
67 mutex_lock(&tcon->crfid.fid_mutex);
68 tcon->crfid.is_valid = false;
69 /* cached handle is not valid, so SMB2_CLOSE won't be sent below */
70 close_cached_dir_lease_locked(&tcon->crfid);
71 memset(tcon->crfid.fid, 0, sizeof(struct cifs_fid));
72 mutex_unlock(&tcon->crfid.fid_mutex);
74 spin_lock(&tcon->tc_lock);
75 if (tcon->status == TID_IN_FILES_INVALIDATE)
76 tcon->status = TID_NEED_TCON;
77 spin_unlock(&tcon->tc_lock);
80 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
85 static inline int cifs_convert_flags(unsigned int flags)
87 if ((flags & O_ACCMODE) == O_RDONLY)
89 else if ((flags & O_ACCMODE) == O_WRONLY)
91 else if ((flags & O_ACCMODE) == O_RDWR) {
92 /* GENERIC_ALL is too much permission to request
93 can cause unnecessary access denied on create */
94 /* return GENERIC_ALL; */
95 return (GENERIC_READ | GENERIC_WRITE);
98 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
99 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
103 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
104 static u32 cifs_posix_convert_flags(unsigned int flags)
108 if ((flags & O_ACCMODE) == O_RDONLY)
109 posix_flags = SMB_O_RDONLY;
110 else if ((flags & O_ACCMODE) == O_WRONLY)
111 posix_flags = SMB_O_WRONLY;
112 else if ((flags & O_ACCMODE) == O_RDWR)
113 posix_flags = SMB_O_RDWR;
115 if (flags & O_CREAT) {
116 posix_flags |= SMB_O_CREAT;
118 posix_flags |= SMB_O_EXCL;
119 } else if (flags & O_EXCL)
120 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
121 current->comm, current->tgid);
124 posix_flags |= SMB_O_TRUNC;
125 /* be safe and imply O_SYNC for O_DSYNC */
127 posix_flags |= SMB_O_SYNC;
128 if (flags & O_DIRECTORY)
129 posix_flags |= SMB_O_DIRECTORY;
130 if (flags & O_NOFOLLOW)
131 posix_flags |= SMB_O_NOFOLLOW;
132 if (flags & O_DIRECT)
133 posix_flags |= SMB_O_DIRECT;
137 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
139 static inline int cifs_get_disposition(unsigned int flags)
141 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
143 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
144 return FILE_OVERWRITE_IF;
145 else if ((flags & O_CREAT) == O_CREAT)
147 else if ((flags & O_TRUNC) == O_TRUNC)
148 return FILE_OVERWRITE;
153 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
154 int cifs_posix_open(const char *full_path, struct inode **pinode,
155 struct super_block *sb, int mode, unsigned int f_flags,
156 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
159 FILE_UNIX_BASIC_INFO *presp_data;
160 __u32 posix_flags = 0;
161 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
162 struct cifs_fattr fattr;
163 struct tcon_link *tlink;
164 struct cifs_tcon *tcon;
166 cifs_dbg(FYI, "posix open %s\n", full_path);
168 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
169 if (presp_data == NULL)
172 tlink = cifs_sb_tlink(cifs_sb);
178 tcon = tlink_tcon(tlink);
179 mode &= ~current_umask();
181 posix_flags = cifs_posix_convert_flags(f_flags);
182 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
183 poplock, full_path, cifs_sb->local_nls,
184 cifs_remap(cifs_sb));
185 cifs_put_tlink(tlink);
190 if (presp_data->Type == cpu_to_le32(-1))
191 goto posix_open_ret; /* open ok, caller does qpathinfo */
194 goto posix_open_ret; /* caller does not need info */
196 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
198 /* get new inode and set it up */
199 if (*pinode == NULL) {
200 cifs_fill_uniqueid(sb, &fattr);
201 *pinode = cifs_iget(sb, &fattr);
207 cifs_revalidate_mapping(*pinode);
208 rc = cifs_fattr_to_inode(*pinode, &fattr);
215 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
218 cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
219 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
220 struct cifs_fid *fid, unsigned int xid)
225 int create_options = CREATE_NOT_DIR;
227 struct TCP_Server_Info *server = tcon->ses->server;
228 struct cifs_open_parms oparms;
230 if (!server->ops->open)
233 desired_access = cifs_convert_flags(f_flags);
235 /*********************************************************************
236 * open flag mapping table:
238 * POSIX Flag CIFS Disposition
239 * ---------- ----------------
240 * O_CREAT FILE_OPEN_IF
241 * O_CREAT | O_EXCL FILE_CREATE
242 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
243 * O_TRUNC FILE_OVERWRITE
244 * none of the above FILE_OPEN
246 * Note that there is not a direct match between disposition
247 * FILE_SUPERSEDE (ie create whether or not file exists although
248 * O_CREAT | O_TRUNC is similar but truncates the existing
249 * file rather than creating a new file as FILE_SUPERSEDE does
250 * (which uses the attributes / metadata passed in on open call)
252 *? O_SYNC is a reasonable match to CIFS writethrough flag
253 *? and the read write flags match reasonably. O_LARGEFILE
254 *? is irrelevant because largefile support is always used
255 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
256 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
257 *********************************************************************/
259 disposition = cifs_get_disposition(f_flags);
261 /* BB pass O_SYNC flag through on file attributes .. BB */
263 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
267 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
268 if (f_flags & O_SYNC)
269 create_options |= CREATE_WRITE_THROUGH;
271 if (f_flags & O_DIRECT)
272 create_options |= CREATE_NO_BUFFER;
275 oparms.cifs_sb = cifs_sb;
276 oparms.desired_access = desired_access;
277 oparms.create_options = cifs_create_options(cifs_sb, create_options);
278 oparms.disposition = disposition;
279 oparms.path = full_path;
281 oparms.reconnect = false;
283 rc = server->ops->open(xid, &oparms, oplock, buf);
288 /* TODO: Add support for calling posix query info but with passing in fid */
290 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
293 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
297 server->ops->close(xid, tcon, fid);
308 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
310 struct cifs_fid_locks *cur;
311 bool has_locks = false;
313 down_read(&cinode->lock_sem);
314 list_for_each_entry(cur, &cinode->llist, llist) {
315 if (!list_empty(&cur->locks)) {
320 up_read(&cinode->lock_sem);
325 cifs_down_write(struct rw_semaphore *sem)
327 while (!down_write_trylock(sem))
331 static void cifsFileInfo_put_work(struct work_struct *work);
333 struct cifsFileInfo *
334 cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
335 struct tcon_link *tlink, __u32 oplock)
337 struct dentry *dentry = file_dentry(file);
338 struct inode *inode = d_inode(dentry);
339 struct cifsInodeInfo *cinode = CIFS_I(inode);
340 struct cifsFileInfo *cfile;
341 struct cifs_fid_locks *fdlocks;
342 struct cifs_tcon *tcon = tlink_tcon(tlink);
343 struct TCP_Server_Info *server = tcon->ses->server;
345 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
349 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
355 INIT_LIST_HEAD(&fdlocks->locks);
356 fdlocks->cfile = cfile;
357 cfile->llist = fdlocks;
360 cfile->pid = current->tgid;
361 cfile->uid = current_fsuid();
362 cfile->dentry = dget(dentry);
363 cfile->f_flags = file->f_flags;
364 cfile->invalidHandle = false;
365 cfile->deferred_close_scheduled = false;
366 cfile->tlink = cifs_get_tlink(tlink);
367 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
368 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
369 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
370 mutex_init(&cfile->fh_mutex);
371 spin_lock_init(&cfile->file_info_lock);
373 cifs_sb_active(inode->i_sb);
376 * If the server returned a read oplock and we have mandatory brlocks,
377 * set oplock level to None.
379 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
380 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
384 cifs_down_write(&cinode->lock_sem);
385 list_add(&fdlocks->llist, &cinode->llist);
386 up_write(&cinode->lock_sem);
388 spin_lock(&tcon->open_file_lock);
389 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
390 oplock = fid->pending_open->oplock;
391 list_del(&fid->pending_open->olist);
393 fid->purge_cache = false;
394 server->ops->set_fid(cfile, fid, oplock);
396 list_add(&cfile->tlist, &tcon->openFileList);
397 atomic_inc(&tcon->num_local_opens);
399 /* if readable file instance put first in list*/
400 spin_lock(&cinode->open_file_lock);
401 if (file->f_mode & FMODE_READ)
402 list_add(&cfile->flist, &cinode->openFileList);
404 list_add_tail(&cfile->flist, &cinode->openFileList);
405 spin_unlock(&cinode->open_file_lock);
406 spin_unlock(&tcon->open_file_lock);
408 if (fid->purge_cache)
409 cifs_zap_mapping(inode);
411 file->private_data = cfile;
415 struct cifsFileInfo *
416 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
418 spin_lock(&cifs_file->file_info_lock);
419 cifsFileInfo_get_locked(cifs_file);
420 spin_unlock(&cifs_file->file_info_lock);
424 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
426 struct inode *inode = d_inode(cifs_file->dentry);
427 struct cifsInodeInfo *cifsi = CIFS_I(inode);
428 struct cifsLockInfo *li, *tmp;
429 struct super_block *sb = inode->i_sb;
432 * Delete any outstanding lock records. We'll lose them when the file
435 cifs_down_write(&cifsi->lock_sem);
436 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
437 list_del(&li->llist);
438 cifs_del_lock_waiters(li);
441 list_del(&cifs_file->llist->llist);
442 kfree(cifs_file->llist);
443 up_write(&cifsi->lock_sem);
445 cifs_put_tlink(cifs_file->tlink);
446 dput(cifs_file->dentry);
447 cifs_sb_deactive(sb);
451 static void cifsFileInfo_put_work(struct work_struct *work)
453 struct cifsFileInfo *cifs_file = container_of(work,
454 struct cifsFileInfo, put);
456 cifsFileInfo_put_final(cifs_file);
460 * cifsFileInfo_put - release a reference of file priv data
462 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
464 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
466 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
468 _cifsFileInfo_put(cifs_file, true, true);
472 * _cifsFileInfo_put - release a reference of file priv data
474 * This may involve closing the filehandle @cifs_file out on the
475 * server. Must be called without holding tcon->open_file_lock,
476 * cinode->open_file_lock and cifs_file->file_info_lock.
478 * If @wait_for_oplock_handler is true and we are releasing the last
479 * reference, wait for any running oplock break handler of the file
480 * and cancel any pending one.
482 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
483 * @wait_oplock_handler: must be false if called from oplock_break_handler
484 * @offload: not offloaded on close and oplock breaks
487 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
488 bool wait_oplock_handler, bool offload)
490 struct inode *inode = d_inode(cifs_file->dentry);
491 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
492 struct TCP_Server_Info *server = tcon->ses->server;
493 struct cifsInodeInfo *cifsi = CIFS_I(inode);
494 struct super_block *sb = inode->i_sb;
495 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
497 struct cifs_pending_open open;
498 bool oplock_break_cancelled;
500 spin_lock(&tcon->open_file_lock);
501 spin_lock(&cifsi->open_file_lock);
502 spin_lock(&cifs_file->file_info_lock);
503 if (--cifs_file->count > 0) {
504 spin_unlock(&cifs_file->file_info_lock);
505 spin_unlock(&cifsi->open_file_lock);
506 spin_unlock(&tcon->open_file_lock);
509 spin_unlock(&cifs_file->file_info_lock);
511 if (server->ops->get_lease_key)
512 server->ops->get_lease_key(inode, &fid);
514 /* store open in pending opens to make sure we don't miss lease break */
515 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
517 /* remove it from the lists */
518 list_del(&cifs_file->flist);
519 list_del(&cifs_file->tlist);
520 atomic_dec(&tcon->num_local_opens);
522 if (list_empty(&cifsi->openFileList)) {
523 cifs_dbg(FYI, "closing last open instance for inode %p\n",
524 d_inode(cifs_file->dentry));
526 * In strict cache mode we need invalidate mapping on the last
527 * close because it may cause a error when we open this file
528 * again and get at least level II oplock.
530 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
531 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
532 cifs_set_oplock_level(cifsi, 0);
535 spin_unlock(&cifsi->open_file_lock);
536 spin_unlock(&tcon->open_file_lock);
538 oplock_break_cancelled = wait_oplock_handler ?
539 cancel_work_sync(&cifs_file->oplock_break) : false;
541 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
542 struct TCP_Server_Info *server = tcon->ses->server;
546 if (server->ops->close_getattr)
547 server->ops->close_getattr(xid, tcon, cifs_file);
548 else if (server->ops->close)
549 server->ops->close(xid, tcon, &cifs_file->fid);
553 if (oplock_break_cancelled)
554 cifs_done_oplock_break(cifsi);
556 cifs_del_pending_open(&open);
559 queue_work(fileinfo_put_wq, &cifs_file->put);
561 cifsFileInfo_put_final(cifs_file);
564 int cifs_open(struct inode *inode, struct file *file)
570 struct cifs_sb_info *cifs_sb;
571 struct TCP_Server_Info *server;
572 struct cifs_tcon *tcon;
573 struct tcon_link *tlink;
574 struct cifsFileInfo *cfile = NULL;
576 const char *full_path;
577 bool posix_open_ok = false;
579 struct cifs_pending_open open;
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,
671 file->f_flags, &oplock, &fid, xid);
673 cifs_del_pending_open(&open);
678 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
680 if (server->ops->close)
681 server->ops->close(xid, tcon, &fid);
682 cifs_del_pending_open(&open);
687 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
688 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
690 * Time to set mode which we can not set earlier due to
691 * problems creating new read-only files.
693 struct cifs_unix_set_info_args args = {
694 .mode = inode->i_mode,
695 .uid = INVALID_UID, /* no change */
696 .gid = INVALID_GID, /* no change */
697 .ctime = NO_CHANGE_64,
698 .atime = NO_CHANGE_64,
699 .mtime = NO_CHANGE_64,
702 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
705 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
708 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
709 file->f_mode & FMODE_WRITE);
710 if (file->f_flags & O_DIRECT &&
711 (!((file->f_flags & O_ACCMODE) != O_RDONLY) ||
712 file->f_flags & O_APPEND))
713 cifs_invalidate_cache(file_inode(file),
714 FSCACHE_INVAL_DIO_WRITE);
717 free_dentry_path(page);
719 cifs_put_tlink(tlink);
723 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
724 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
725 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
728 * Try to reacquire byte range locks that were released when session
729 * to server was lost.
732 cifs_relock_file(struct cifsFileInfo *cfile)
734 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
735 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
737 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
738 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
739 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
741 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
742 if (cinode->can_cache_brlcks) {
743 /* can cache locks - no need to relock */
744 up_read(&cinode->lock_sem);
748 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
749 if (cap_unix(tcon->ses) &&
750 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
751 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
752 rc = cifs_push_posix_locks(cfile);
754 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
755 rc = tcon->ses->server->ops->push_mand_locks(cfile);
757 up_read(&cinode->lock_sem);
762 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
767 struct cifs_sb_info *cifs_sb;
768 struct cifs_tcon *tcon;
769 struct TCP_Server_Info *server;
770 struct cifsInodeInfo *cinode;
773 const char *full_path;
775 int disposition = FILE_OPEN;
776 int create_options = CREATE_NOT_DIR;
777 struct cifs_open_parms oparms;
780 mutex_lock(&cfile->fh_mutex);
781 if (!cfile->invalidHandle) {
782 mutex_unlock(&cfile->fh_mutex);
787 inode = d_inode(cfile->dentry);
788 cifs_sb = CIFS_SB(inode->i_sb);
789 tcon = tlink_tcon(cfile->tlink);
790 server = tcon->ses->server;
793 * Can not grab rename sem here because various ops, including those
794 * that already have the rename sem can end up causing writepage to get
795 * called and if the server was down that means we end up here, and we
796 * can never tell if the caller already has the rename_sem.
798 page = alloc_dentry_path();
799 full_path = build_path_from_dentry(cfile->dentry, page);
800 if (IS_ERR(full_path)) {
801 mutex_unlock(&cfile->fh_mutex);
802 free_dentry_path(page);
804 return PTR_ERR(full_path);
807 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
808 inode, cfile->f_flags, full_path);
810 if (tcon->ses->server->oplocks)
815 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
816 if (tcon->unix_ext && cap_unix(tcon->ses) &&
817 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
818 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
820 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
821 * original open. Must mask them off for a reopen.
823 unsigned int oflags = cfile->f_flags &
824 ~(O_CREAT | O_EXCL | O_TRUNC);
826 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
827 cifs_sb->ctx->file_mode /* ignored */,
828 oflags, &oplock, &cfile->fid.netfid, xid);
830 cifs_dbg(FYI, "posix reopen succeeded\n");
831 oparms.reconnect = true;
835 * fallthrough to retry open the old way on errors, especially
836 * in the reconnect path it is important to retry hard
839 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
841 desired_access = cifs_convert_flags(cfile->f_flags);
843 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
844 if (cfile->f_flags & O_SYNC)
845 create_options |= CREATE_WRITE_THROUGH;
847 if (cfile->f_flags & O_DIRECT)
848 create_options |= CREATE_NO_BUFFER;
850 if (server->ops->get_lease_key)
851 server->ops->get_lease_key(inode, &cfile->fid);
854 oparms.cifs_sb = cifs_sb;
855 oparms.desired_access = desired_access;
856 oparms.create_options = cifs_create_options(cifs_sb, create_options);
857 oparms.disposition = disposition;
858 oparms.path = full_path;
859 oparms.fid = &cfile->fid;
860 oparms.reconnect = true;
863 * Can not refresh inode by passing in file_info buf to be returned by
864 * ops->open and then calling get_inode_info with returned buf since
865 * file might have write behind data that needs to be flushed and server
866 * version of file size can be stale. If we knew for sure that inode was
867 * not dirty locally we could do this.
869 rc = server->ops->open(xid, &oparms, &oplock, NULL);
870 if (rc == -ENOENT && oparms.reconnect == false) {
871 /* durable handle timeout is expired - open the file again */
872 rc = server->ops->open(xid, &oparms, &oplock, NULL);
873 /* indicate that we need to relock the file */
874 oparms.reconnect = true;
878 mutex_unlock(&cfile->fh_mutex);
879 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
880 cifs_dbg(FYI, "oplock: %d\n", oplock);
881 goto reopen_error_exit;
884 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
886 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
887 cfile->invalidHandle = false;
888 mutex_unlock(&cfile->fh_mutex);
889 cinode = CIFS_I(inode);
892 rc = filemap_write_and_wait(inode->i_mapping);
893 if (!is_interrupt_error(rc))
894 mapping_set_error(inode->i_mapping, rc);
896 if (tcon->posix_extensions)
897 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
898 else if (tcon->unix_ext)
899 rc = cifs_get_inode_info_unix(&inode, full_path,
902 rc = cifs_get_inode_info(&inode, full_path, NULL,
903 inode->i_sb, xid, NULL);
906 * Else we are writing out data to server already and could deadlock if
907 * we tried to flush data, and since we do not know if we have data that
908 * would invalidate the current end of file on the server we can not go
909 * to the server to get the new inode info.
913 * If the server returned a read oplock and we have mandatory brlocks,
914 * set oplock level to None.
916 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
917 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
921 server->ops->set_fid(cfile, &cfile->fid, oplock);
922 if (oparms.reconnect)
923 cifs_relock_file(cfile);
926 free_dentry_path(page);
931 void smb2_deferred_work_close(struct work_struct *work)
933 struct cifsFileInfo *cfile = container_of(work,
934 struct cifsFileInfo, deferred.work);
936 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
937 cifs_del_deferred_close(cfile);
938 cfile->deferred_close_scheduled = false;
939 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
940 _cifsFileInfo_put(cfile, true, false);
943 int cifs_close(struct inode *inode, struct file *file)
945 struct cifsFileInfo *cfile;
946 struct cifsInodeInfo *cinode = CIFS_I(inode);
947 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
948 struct cifs_deferred_close *dclose;
950 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
952 if (file->private_data != NULL) {
953 cfile = file->private_data;
954 file->private_data = NULL;
955 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
956 if ((cinode->oplock == CIFS_CACHE_RHW_FLG) &&
957 cinode->lease_granted &&
958 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) &&
960 if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) {
961 inode->i_ctime = inode->i_mtime = current_time(inode);
963 spin_lock(&cinode->deferred_lock);
964 cifs_add_deferred_close(cfile, dclose);
965 if (cfile->deferred_close_scheduled &&
966 delayed_work_pending(&cfile->deferred)) {
968 * If there is no pending work, mod_delayed_work queues new work.
969 * So, Increase the ref count to avoid use-after-free.
971 if (!mod_delayed_work(deferredclose_wq,
972 &cfile->deferred, cifs_sb->ctx->acregmax))
973 cifsFileInfo_get(cfile);
975 /* Deferred close for files */
976 queue_delayed_work(deferredclose_wq,
977 &cfile->deferred, cifs_sb->ctx->acregmax);
978 cfile->deferred_close_scheduled = true;
979 spin_unlock(&cinode->deferred_lock);
982 spin_unlock(&cinode->deferred_lock);
983 _cifsFileInfo_put(cfile, true, false);
985 _cifsFileInfo_put(cfile, true, false);
990 /* return code from the ->release op is always ignored */
995 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
997 struct cifsFileInfo *open_file, *tmp;
998 struct list_head tmp_list;
1000 if (!tcon->use_persistent || !tcon->need_reopen_files)
1003 tcon->need_reopen_files = false;
1005 cifs_dbg(FYI, "Reopen persistent handles\n");
1006 INIT_LIST_HEAD(&tmp_list);
1008 /* list all files open on tree connection, reopen resilient handles */
1009 spin_lock(&tcon->open_file_lock);
1010 list_for_each_entry(open_file, &tcon->openFileList, tlist) {
1011 if (!open_file->invalidHandle)
1013 cifsFileInfo_get(open_file);
1014 list_add_tail(&open_file->rlist, &tmp_list);
1016 spin_unlock(&tcon->open_file_lock);
1018 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
1019 if (cifs_reopen_file(open_file, false /* do not flush */))
1020 tcon->need_reopen_files = true;
1021 list_del_init(&open_file->rlist);
1022 cifsFileInfo_put(open_file);
1026 int cifs_closedir(struct inode *inode, struct file *file)
1030 struct cifsFileInfo *cfile = file->private_data;
1031 struct cifs_tcon *tcon;
1032 struct TCP_Server_Info *server;
1035 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1041 tcon = tlink_tcon(cfile->tlink);
1042 server = tcon->ses->server;
1044 cifs_dbg(FYI, "Freeing private data in close dir\n");
1045 spin_lock(&cfile->file_info_lock);
1046 if (server->ops->dir_needs_close(cfile)) {
1047 cfile->invalidHandle = true;
1048 spin_unlock(&cfile->file_info_lock);
1049 if (server->ops->close_dir)
1050 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
1053 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
1054 /* not much we can do if it fails anyway, ignore rc */
1057 spin_unlock(&cfile->file_info_lock);
1059 buf = cfile->srch_inf.ntwrk_buf_start;
1061 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1062 cfile->srch_inf.ntwrk_buf_start = NULL;
1063 if (cfile->srch_inf.smallBuf)
1064 cifs_small_buf_release(buf);
1066 cifs_buf_release(buf);
1069 cifs_put_tlink(cfile->tlink);
1070 kfree(file->private_data);
1071 file->private_data = NULL;
1072 /* BB can we lock the filestruct while this is going on? */
1077 static struct cifsLockInfo *
1078 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1080 struct cifsLockInfo *lock =
1081 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
1084 lock->offset = offset;
1085 lock->length = length;
1087 lock->pid = current->tgid;
1088 lock->flags = flags;
1089 INIT_LIST_HEAD(&lock->blist);
1090 init_waitqueue_head(&lock->block_q);
1095 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1097 struct cifsLockInfo *li, *tmp;
1098 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1099 list_del_init(&li->blist);
1100 wake_up(&li->block_q);
1104 #define CIFS_LOCK_OP 0
1105 #define CIFS_READ_OP 1
1106 #define CIFS_WRITE_OP 2
1108 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1110 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1111 __u64 length, __u8 type, __u16 flags,
1112 struct cifsFileInfo *cfile,
1113 struct cifsLockInfo **conf_lock, int rw_check)
1115 struct cifsLockInfo *li;
1116 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1117 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1119 list_for_each_entry(li, &fdlocks->locks, llist) {
1120 if (offset + length <= li->offset ||
1121 offset >= li->offset + li->length)
1123 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1124 server->ops->compare_fids(cfile, cur_cfile)) {
1125 /* shared lock prevents write op through the same fid */
1126 if (!(li->type & server->vals->shared_lock_type) ||
1127 rw_check != CIFS_WRITE_OP)
1130 if ((type & server->vals->shared_lock_type) &&
1131 ((server->ops->compare_fids(cfile, cur_cfile) &&
1132 current->tgid == li->pid) || type == li->type))
1134 if (rw_check == CIFS_LOCK_OP &&
1135 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1136 server->ops->compare_fids(cfile, cur_cfile))
1146 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1147 __u8 type, __u16 flags,
1148 struct cifsLockInfo **conf_lock, int rw_check)
1151 struct cifs_fid_locks *cur;
1152 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1154 list_for_each_entry(cur, &cinode->llist, llist) {
1155 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1156 flags, cfile, conf_lock,
1166 * Check if there is another lock that prevents us to set the lock (mandatory
1167 * style). If such a lock exists, update the flock structure with its
1168 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1169 * or leave it the same if we can't. Returns 0 if we don't need to request to
1170 * the server or 1 otherwise.
1173 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1174 __u8 type, struct file_lock *flock)
1177 struct cifsLockInfo *conf_lock;
1178 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1179 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1182 down_read(&cinode->lock_sem);
1184 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1185 flock->fl_flags, &conf_lock,
1188 flock->fl_start = conf_lock->offset;
1189 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1190 flock->fl_pid = conf_lock->pid;
1191 if (conf_lock->type & server->vals->shared_lock_type)
1192 flock->fl_type = F_RDLCK;
1194 flock->fl_type = F_WRLCK;
1195 } else if (!cinode->can_cache_brlcks)
1198 flock->fl_type = F_UNLCK;
1200 up_read(&cinode->lock_sem);
1205 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1207 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1208 cifs_down_write(&cinode->lock_sem);
1209 list_add_tail(&lock->llist, &cfile->llist->locks);
1210 up_write(&cinode->lock_sem);
1214 * Set the byte-range lock (mandatory style). Returns:
1215 * 1) 0, if we set the lock and don't need to request to the server;
1216 * 2) 1, if no locks prevent us but we need to request to the server;
1217 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1220 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1223 struct cifsLockInfo *conf_lock;
1224 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1230 cifs_down_write(&cinode->lock_sem);
1232 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1233 lock->type, lock->flags, &conf_lock,
1235 if (!exist && cinode->can_cache_brlcks) {
1236 list_add_tail(&lock->llist, &cfile->llist->locks);
1237 up_write(&cinode->lock_sem);
1246 list_add_tail(&lock->blist, &conf_lock->blist);
1247 up_write(&cinode->lock_sem);
1248 rc = wait_event_interruptible(lock->block_q,
1249 (lock->blist.prev == &lock->blist) &&
1250 (lock->blist.next == &lock->blist));
1253 cifs_down_write(&cinode->lock_sem);
1254 list_del_init(&lock->blist);
1257 up_write(&cinode->lock_sem);
1261 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1263 * Check if there is another lock that prevents us to set the lock (posix
1264 * style). If such a lock exists, update the flock structure with its
1265 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1266 * or leave it the same if we can't. Returns 0 if we don't need to request to
1267 * the server or 1 otherwise.
1270 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1273 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1274 unsigned char saved_type = flock->fl_type;
1276 if ((flock->fl_flags & FL_POSIX) == 0)
1279 down_read(&cinode->lock_sem);
1280 posix_test_lock(file, flock);
1282 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1283 flock->fl_type = saved_type;
1287 up_read(&cinode->lock_sem);
1292 * Set the byte-range lock (posix style). Returns:
1293 * 1) <0, if the error occurs while setting the lock;
1294 * 2) 0, if we set the lock and don't need to request to the server;
1295 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1296 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1299 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1301 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1302 int rc = FILE_LOCK_DEFERRED + 1;
1304 if ((flock->fl_flags & FL_POSIX) == 0)
1307 cifs_down_write(&cinode->lock_sem);
1308 if (!cinode->can_cache_brlcks) {
1309 up_write(&cinode->lock_sem);
1313 rc = posix_lock_file(file, flock, NULL);
1314 up_write(&cinode->lock_sem);
1319 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1322 int rc = 0, stored_rc;
1323 struct cifsLockInfo *li, *tmp;
1324 struct cifs_tcon *tcon;
1325 unsigned int num, max_num, max_buf;
1326 LOCKING_ANDX_RANGE *buf, *cur;
1327 static const int types[] = {
1328 LOCKING_ANDX_LARGE_FILES,
1329 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1334 tcon = tlink_tcon(cfile->tlink);
1337 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1338 * and check it before using.
1340 max_buf = tcon->ses->server->maxBuf;
1341 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1346 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1348 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1350 max_num = (max_buf - sizeof(struct smb_hdr)) /
1351 sizeof(LOCKING_ANDX_RANGE);
1352 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1358 for (i = 0; i < 2; i++) {
1361 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1362 if (li->type != types[i])
1364 cur->Pid = cpu_to_le16(li->pid);
1365 cur->LengthLow = cpu_to_le32((u32)li->length);
1366 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1367 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1368 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1369 if (++num == max_num) {
1370 stored_rc = cifs_lockv(xid, tcon,
1372 (__u8)li->type, 0, num,
1383 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1384 (__u8)types[i], 0, num, buf);
1396 hash_lockowner(fl_owner_t owner)
1398 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1400 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1402 struct lock_to_push {
1403 struct list_head llist;
1411 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1413 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1415 struct inode *inode = d_inode(cfile->dentry);
1416 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1417 struct file_lock *flock;
1418 struct file_lock_context *flctx = inode->i_flctx;
1419 unsigned int count = 0, i;
1420 int rc = 0, xid, type;
1421 struct list_head locks_to_send, *el;
1422 struct lock_to_push *lck, *tmp;
1430 spin_lock(&flctx->flc_lock);
1431 list_for_each(el, &flctx->flc_posix) {
1434 spin_unlock(&flctx->flc_lock);
1436 INIT_LIST_HEAD(&locks_to_send);
1439 * Allocating count locks is enough because no FL_POSIX locks can be
1440 * added to the list while we are holding cinode->lock_sem that
1441 * protects locking operations of this inode.
1443 for (i = 0; i < count; i++) {
1444 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1449 list_add_tail(&lck->llist, &locks_to_send);
1452 el = locks_to_send.next;
1453 spin_lock(&flctx->flc_lock);
1454 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1455 if (el == &locks_to_send) {
1457 * The list ended. We don't have enough allocated
1458 * structures - something is really wrong.
1460 cifs_dbg(VFS, "Can't push all brlocks!\n");
1463 length = cifs_flock_len(flock);
1464 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1468 lck = list_entry(el, struct lock_to_push, llist);
1469 lck->pid = hash_lockowner(flock->fl_owner);
1470 lck->netfid = cfile->fid.netfid;
1471 lck->length = length;
1473 lck->offset = flock->fl_start;
1475 spin_unlock(&flctx->flc_lock);
1477 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1480 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1481 lck->offset, lck->length, NULL,
1485 list_del(&lck->llist);
1493 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1494 list_del(&lck->llist);
1499 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1502 cifs_push_locks(struct cifsFileInfo *cfile)
1504 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1505 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1507 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1508 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1509 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1511 /* we are going to update can_cache_brlcks here - need a write access */
1512 cifs_down_write(&cinode->lock_sem);
1513 if (!cinode->can_cache_brlcks) {
1514 up_write(&cinode->lock_sem);
1518 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1519 if (cap_unix(tcon->ses) &&
1520 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1521 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1522 rc = cifs_push_posix_locks(cfile);
1524 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1525 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1527 cinode->can_cache_brlcks = false;
1528 up_write(&cinode->lock_sem);
1533 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1534 bool *wait_flag, struct TCP_Server_Info *server)
1536 if (flock->fl_flags & FL_POSIX)
1537 cifs_dbg(FYI, "Posix\n");
1538 if (flock->fl_flags & FL_FLOCK)
1539 cifs_dbg(FYI, "Flock\n");
1540 if (flock->fl_flags & FL_SLEEP) {
1541 cifs_dbg(FYI, "Blocking lock\n");
1544 if (flock->fl_flags & FL_ACCESS)
1545 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1546 if (flock->fl_flags & FL_LEASE)
1547 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1548 if (flock->fl_flags &
1549 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1550 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1551 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1553 *type = server->vals->large_lock_type;
1554 if (flock->fl_type == F_WRLCK) {
1555 cifs_dbg(FYI, "F_WRLCK\n");
1556 *type |= server->vals->exclusive_lock_type;
1558 } else if (flock->fl_type == F_UNLCK) {
1559 cifs_dbg(FYI, "F_UNLCK\n");
1560 *type |= server->vals->unlock_lock_type;
1562 /* Check if unlock includes more than one lock range */
1563 } else if (flock->fl_type == F_RDLCK) {
1564 cifs_dbg(FYI, "F_RDLCK\n");
1565 *type |= server->vals->shared_lock_type;
1567 } else if (flock->fl_type == F_EXLCK) {
1568 cifs_dbg(FYI, "F_EXLCK\n");
1569 *type |= server->vals->exclusive_lock_type;
1571 } else if (flock->fl_type == F_SHLCK) {
1572 cifs_dbg(FYI, "F_SHLCK\n");
1573 *type |= server->vals->shared_lock_type;
1576 cifs_dbg(FYI, "Unknown type of lock\n");
1580 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1581 bool wait_flag, bool posix_lck, unsigned int xid)
1584 __u64 length = cifs_flock_len(flock);
1585 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1586 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1587 struct TCP_Server_Info *server = tcon->ses->server;
1588 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1589 __u16 netfid = cfile->fid.netfid;
1592 int posix_lock_type;
1594 rc = cifs_posix_lock_test(file, flock);
1598 if (type & server->vals->shared_lock_type)
1599 posix_lock_type = CIFS_RDLCK;
1601 posix_lock_type = CIFS_WRLCK;
1602 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1603 hash_lockowner(flock->fl_owner),
1604 flock->fl_start, length, flock,
1605 posix_lock_type, wait_flag);
1608 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1610 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1614 /* BB we could chain these into one lock request BB */
1615 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1618 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1620 flock->fl_type = F_UNLCK;
1622 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1627 if (type & server->vals->shared_lock_type) {
1628 flock->fl_type = F_WRLCK;
1632 type &= ~server->vals->exclusive_lock_type;
1634 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1635 type | server->vals->shared_lock_type,
1638 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1639 type | server->vals->shared_lock_type, 0, 1, false);
1640 flock->fl_type = F_RDLCK;
1642 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1645 flock->fl_type = F_WRLCK;
1651 cifs_move_llist(struct list_head *source, struct list_head *dest)
1653 struct list_head *li, *tmp;
1654 list_for_each_safe(li, tmp, source)
1655 list_move(li, dest);
1659 cifs_free_llist(struct list_head *llist)
1661 struct cifsLockInfo *li, *tmp;
1662 list_for_each_entry_safe(li, tmp, llist, llist) {
1663 cifs_del_lock_waiters(li);
1664 list_del(&li->llist);
1669 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1671 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1674 int rc = 0, stored_rc;
1675 static const int types[] = {
1676 LOCKING_ANDX_LARGE_FILES,
1677 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1680 unsigned int max_num, num, max_buf;
1681 LOCKING_ANDX_RANGE *buf, *cur;
1682 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1683 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1684 struct cifsLockInfo *li, *tmp;
1685 __u64 length = cifs_flock_len(flock);
1686 struct list_head tmp_llist;
1688 INIT_LIST_HEAD(&tmp_llist);
1691 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1692 * and check it before using.
1694 max_buf = tcon->ses->server->maxBuf;
1695 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1698 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1700 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1702 max_num = (max_buf - sizeof(struct smb_hdr)) /
1703 sizeof(LOCKING_ANDX_RANGE);
1704 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1708 cifs_down_write(&cinode->lock_sem);
1709 for (i = 0; i < 2; i++) {
1712 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1713 if (flock->fl_start > li->offset ||
1714 (flock->fl_start + length) <
1715 (li->offset + li->length))
1717 if (current->tgid != li->pid)
1719 if (types[i] != li->type)
1721 if (cinode->can_cache_brlcks) {
1723 * We can cache brlock requests - simply remove
1724 * a lock from the file's list.
1726 list_del(&li->llist);
1727 cifs_del_lock_waiters(li);
1731 cur->Pid = cpu_to_le16(li->pid);
1732 cur->LengthLow = cpu_to_le32((u32)li->length);
1733 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1734 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1735 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1737 * We need to save a lock here to let us add it again to
1738 * the file's list if the unlock range request fails on
1741 list_move(&li->llist, &tmp_llist);
1742 if (++num == max_num) {
1743 stored_rc = cifs_lockv(xid, tcon,
1745 li->type, num, 0, buf);
1748 * We failed on the unlock range
1749 * request - add all locks from the tmp
1750 * list to the head of the file's list.
1752 cifs_move_llist(&tmp_llist,
1753 &cfile->llist->locks);
1757 * The unlock range request succeed -
1758 * free the tmp list.
1760 cifs_free_llist(&tmp_llist);
1767 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1768 types[i], num, 0, buf);
1770 cifs_move_llist(&tmp_llist,
1771 &cfile->llist->locks);
1774 cifs_free_llist(&tmp_llist);
1778 up_write(&cinode->lock_sem);
1782 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1785 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1786 bool wait_flag, bool posix_lck, int lock, int unlock,
1790 __u64 length = cifs_flock_len(flock);
1791 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1792 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1793 struct TCP_Server_Info *server = tcon->ses->server;
1794 struct inode *inode = d_inode(cfile->dentry);
1796 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1798 int posix_lock_type;
1800 rc = cifs_posix_lock_set(file, flock);
1801 if (rc <= FILE_LOCK_DEFERRED)
1804 if (type & server->vals->shared_lock_type)
1805 posix_lock_type = CIFS_RDLCK;
1807 posix_lock_type = CIFS_WRLCK;
1810 posix_lock_type = CIFS_UNLCK;
1812 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1813 hash_lockowner(flock->fl_owner),
1814 flock->fl_start, length,
1815 NULL, posix_lock_type, wait_flag);
1818 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1820 struct cifsLockInfo *lock;
1822 lock = cifs_lock_init(flock->fl_start, length, type,
1827 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1836 * Windows 7 server can delay breaking lease from read to None
1837 * if we set a byte-range lock on a file - break it explicitly
1838 * before sending the lock to the server to be sure the next
1839 * read won't conflict with non-overlapted locks due to
1842 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1843 CIFS_CACHE_READ(CIFS_I(inode))) {
1844 cifs_zap_mapping(inode);
1845 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1847 CIFS_I(inode)->oplock = 0;
1850 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1851 type, 1, 0, wait_flag);
1857 cifs_lock_add(cfile, lock);
1859 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1862 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1864 * If this is a request to remove all locks because we
1865 * are closing the file, it doesn't matter if the
1866 * unlocking failed as both cifs.ko and the SMB server
1867 * remove the lock on file close
1870 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1871 if (!(flock->fl_flags & FL_CLOSE))
1874 rc = locks_lock_file_wait(file, flock);
1879 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1882 int lock = 0, unlock = 0;
1883 bool wait_flag = false;
1884 bool posix_lck = false;
1885 struct cifs_sb_info *cifs_sb;
1886 struct cifs_tcon *tcon;
1887 struct cifsFileInfo *cfile;
1893 if (!(fl->fl_flags & FL_FLOCK))
1896 cfile = (struct cifsFileInfo *)file->private_data;
1897 tcon = tlink_tcon(cfile->tlink);
1899 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
1901 cifs_sb = CIFS_FILE_SB(file);
1903 if (cap_unix(tcon->ses) &&
1904 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1905 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1908 if (!lock && !unlock) {
1910 * if no lock or unlock then nothing to do since we do not
1917 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1925 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1928 int lock = 0, unlock = 0;
1929 bool wait_flag = false;
1930 bool posix_lck = false;
1931 struct cifs_sb_info *cifs_sb;
1932 struct cifs_tcon *tcon;
1933 struct cifsFileInfo *cfile;
1939 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1940 cmd, flock->fl_flags, flock->fl_type,
1941 flock->fl_start, flock->fl_end);
1943 cfile = (struct cifsFileInfo *)file->private_data;
1944 tcon = tlink_tcon(cfile->tlink);
1946 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1948 cifs_sb = CIFS_FILE_SB(file);
1949 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
1951 if (cap_unix(tcon->ses) &&
1952 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1953 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1956 * BB add code here to normalize offset and length to account for
1957 * negative length which we can not accept over the wire.
1959 if (IS_GETLK(cmd)) {
1960 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1965 if (!lock && !unlock) {
1967 * if no lock or unlock then nothing to do since we do not
1974 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1981 * update the file size (if needed) after a write. Should be called with
1982 * the inode->i_lock held
1985 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1986 unsigned int bytes_written)
1988 loff_t end_of_write = offset + bytes_written;
1990 if (end_of_write > cifsi->server_eof)
1991 cifsi->server_eof = end_of_write;
1995 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1996 size_t write_size, loff_t *offset)
1999 unsigned int bytes_written = 0;
2000 unsigned int total_written;
2001 struct cifs_tcon *tcon;
2002 struct TCP_Server_Info *server;
2004 struct dentry *dentry = open_file->dentry;
2005 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
2006 struct cifs_io_parms io_parms = {0};
2008 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
2009 write_size, *offset, dentry);
2011 tcon = tlink_tcon(open_file->tlink);
2012 server = tcon->ses->server;
2014 if (!server->ops->sync_write)
2019 for (total_written = 0; write_size > total_written;
2020 total_written += bytes_written) {
2022 while (rc == -EAGAIN) {
2026 if (open_file->invalidHandle) {
2027 /* we could deadlock if we called
2028 filemap_fdatawait from here so tell
2029 reopen_file not to flush data to
2031 rc = cifs_reopen_file(open_file, false);
2036 len = min(server->ops->wp_retry_size(d_inode(dentry)),
2037 (unsigned int)write_size - total_written);
2038 /* iov[0] is reserved for smb header */
2039 iov[1].iov_base = (char *)write_data + total_written;
2040 iov[1].iov_len = len;
2042 io_parms.tcon = tcon;
2043 io_parms.offset = *offset;
2044 io_parms.length = len;
2045 rc = server->ops->sync_write(xid, &open_file->fid,
2046 &io_parms, &bytes_written, iov, 1);
2048 if (rc || (bytes_written == 0)) {
2056 spin_lock(&d_inode(dentry)->i_lock);
2057 cifs_update_eof(cifsi, *offset, bytes_written);
2058 spin_unlock(&d_inode(dentry)->i_lock);
2059 *offset += bytes_written;
2063 cifs_stats_bytes_written(tcon, total_written);
2065 if (total_written > 0) {
2066 spin_lock(&d_inode(dentry)->i_lock);
2067 if (*offset > d_inode(dentry)->i_size) {
2068 i_size_write(d_inode(dentry), *offset);
2069 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
2071 spin_unlock(&d_inode(dentry)->i_lock);
2073 mark_inode_dirty_sync(d_inode(dentry));
2075 return total_written;
2078 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
2081 struct cifsFileInfo *open_file = NULL;
2082 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2084 /* only filter by fsuid on multiuser mounts */
2085 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2088 spin_lock(&cifs_inode->open_file_lock);
2089 /* we could simply get the first_list_entry since write-only entries
2090 are always at the end of the list but since the first entry might
2091 have a close pending, we go through the whole list */
2092 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2093 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2095 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2096 if ((!open_file->invalidHandle)) {
2097 /* found a good file */
2098 /* lock it so it will not be closed on us */
2099 cifsFileInfo_get(open_file);
2100 spin_unlock(&cifs_inode->open_file_lock);
2102 } /* else might as well continue, and look for
2103 another, or simply have the caller reopen it
2104 again rather than trying to fix this handle */
2105 } else /* write only file */
2106 break; /* write only files are last so must be done */
2108 spin_unlock(&cifs_inode->open_file_lock);
2112 /* Return -EBADF if no handle is found and general rc otherwise */
2114 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
2115 struct cifsFileInfo **ret_file)
2117 struct cifsFileInfo *open_file, *inv_file = NULL;
2118 struct cifs_sb_info *cifs_sb;
2119 bool any_available = false;
2121 unsigned int refind = 0;
2122 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
2123 bool with_delete = flags & FIND_WR_WITH_DELETE;
2127 * Having a null inode here (because mapping->host was set to zero by
2128 * the VFS or MM) should not happen but we had reports of on oops (due
2129 * to it being zero) during stress testcases so we need to check for it
2132 if (cifs_inode == NULL) {
2133 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2138 cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2140 /* only filter by fsuid on multiuser mounts */
2141 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2144 spin_lock(&cifs_inode->open_file_lock);
2146 if (refind > MAX_REOPEN_ATT) {
2147 spin_unlock(&cifs_inode->open_file_lock);
2150 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2151 if (!any_available && open_file->pid != current->tgid)
2153 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2155 if (with_delete && !(open_file->fid.access & DELETE))
2157 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2158 if (!open_file->invalidHandle) {
2159 /* found a good writable file */
2160 cifsFileInfo_get(open_file);
2161 spin_unlock(&cifs_inode->open_file_lock);
2162 *ret_file = open_file;
2166 inv_file = open_file;
2170 /* couldn't find useable FH with same pid, try any available */
2171 if (!any_available) {
2172 any_available = true;
2173 goto refind_writable;
2177 any_available = false;
2178 cifsFileInfo_get(inv_file);
2181 spin_unlock(&cifs_inode->open_file_lock);
2184 rc = cifs_reopen_file(inv_file, false);
2186 *ret_file = inv_file;
2190 spin_lock(&cifs_inode->open_file_lock);
2191 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2192 spin_unlock(&cifs_inode->open_file_lock);
2193 cifsFileInfo_put(inv_file);
2196 spin_lock(&cifs_inode->open_file_lock);
2197 goto refind_writable;
2203 struct cifsFileInfo *
2204 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2206 struct cifsFileInfo *cfile;
2209 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2211 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2217 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2219 struct cifsFileInfo **ret_file)
2221 struct cifsFileInfo *cfile;
2222 void *page = alloc_dentry_path();
2226 spin_lock(&tcon->open_file_lock);
2227 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2228 struct cifsInodeInfo *cinode;
2229 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2230 if (IS_ERR(full_path)) {
2231 spin_unlock(&tcon->open_file_lock);
2232 free_dentry_path(page);
2233 return PTR_ERR(full_path);
2235 if (strcmp(full_path, name))
2238 cinode = CIFS_I(d_inode(cfile->dentry));
2239 spin_unlock(&tcon->open_file_lock);
2240 free_dentry_path(page);
2241 return cifs_get_writable_file(cinode, flags, ret_file);
2244 spin_unlock(&tcon->open_file_lock);
2245 free_dentry_path(page);
2250 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2251 struct cifsFileInfo **ret_file)
2253 struct cifsFileInfo *cfile;
2254 void *page = alloc_dentry_path();
2258 spin_lock(&tcon->open_file_lock);
2259 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2260 struct cifsInodeInfo *cinode;
2261 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2262 if (IS_ERR(full_path)) {
2263 spin_unlock(&tcon->open_file_lock);
2264 free_dentry_path(page);
2265 return PTR_ERR(full_path);
2267 if (strcmp(full_path, name))
2270 cinode = CIFS_I(d_inode(cfile->dentry));
2271 spin_unlock(&tcon->open_file_lock);
2272 free_dentry_path(page);
2273 *ret_file = find_readable_file(cinode, 0);
2274 return *ret_file ? 0 : -ENOENT;
2277 spin_unlock(&tcon->open_file_lock);
2278 free_dentry_path(page);
2283 cifs_writedata_release(struct kref *refcount)
2285 struct cifs_writedata *wdata = container_of(refcount,
2286 struct cifs_writedata, refcount);
2287 #ifdef CONFIG_CIFS_SMB_DIRECT
2289 smbd_deregister_mr(wdata->mr);
2295 cifsFileInfo_put(wdata->cfile);
2297 kvfree(wdata->pages);
2302 * Write failed with a retryable error. Resend the write request. It's also
2303 * possible that the page was redirtied so re-clean the page.
2306 cifs_writev_requeue(struct cifs_writedata *wdata)
2309 struct inode *inode = d_inode(wdata->cfile->dentry);
2310 struct TCP_Server_Info *server;
2311 unsigned int rest_len;
2313 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2315 rest_len = wdata->bytes;
2317 struct cifs_writedata *wdata2;
2318 unsigned int j, nr_pages, wsize, tailsz, cur_len;
2320 wsize = server->ops->wp_retry_size(inode);
2321 if (wsize < rest_len) {
2322 nr_pages = wsize / PAGE_SIZE;
2327 cur_len = nr_pages * PAGE_SIZE;
2330 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
2332 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
2335 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2341 for (j = 0; j < nr_pages; j++) {
2342 wdata2->pages[j] = wdata->pages[i + j];
2343 lock_page(wdata2->pages[j]);
2344 clear_page_dirty_for_io(wdata2->pages[j]);
2347 wdata2->sync_mode = wdata->sync_mode;
2348 wdata2->nr_pages = nr_pages;
2349 wdata2->offset = page_offset(wdata2->pages[0]);
2350 wdata2->pagesz = PAGE_SIZE;
2351 wdata2->tailsz = tailsz;
2352 wdata2->bytes = cur_len;
2354 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
2356 if (!wdata2->cfile) {
2357 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2359 if (!is_retryable_error(rc))
2362 wdata2->pid = wdata2->cfile->pid;
2363 rc = server->ops->async_writev(wdata2,
2364 cifs_writedata_release);
2367 for (j = 0; j < nr_pages; j++) {
2368 unlock_page(wdata2->pages[j]);
2369 if (rc != 0 && !is_retryable_error(rc)) {
2370 SetPageError(wdata2->pages[j]);
2371 end_page_writeback(wdata2->pages[j]);
2372 put_page(wdata2->pages[j]);
2376 kref_put(&wdata2->refcount, cifs_writedata_release);
2378 if (is_retryable_error(rc))
2384 rest_len -= cur_len;
2386 } while (i < wdata->nr_pages);
2388 /* cleanup remaining pages from the original wdata */
2389 for (; i < wdata->nr_pages; i++) {
2390 SetPageError(wdata->pages[i]);
2391 end_page_writeback(wdata->pages[i]);
2392 put_page(wdata->pages[i]);
2395 if (rc != 0 && !is_retryable_error(rc))
2396 mapping_set_error(inode->i_mapping, rc);
2397 kref_put(&wdata->refcount, cifs_writedata_release);
2401 cifs_writev_complete(struct work_struct *work)
2403 struct cifs_writedata *wdata = container_of(work,
2404 struct cifs_writedata, work);
2405 struct inode *inode = d_inode(wdata->cfile->dentry);
2408 if (wdata->result == 0) {
2409 spin_lock(&inode->i_lock);
2410 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2411 spin_unlock(&inode->i_lock);
2412 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2414 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2415 return cifs_writev_requeue(wdata);
2417 for (i = 0; i < wdata->nr_pages; i++) {
2418 struct page *page = wdata->pages[i];
2420 if (wdata->result == -EAGAIN)
2421 __set_page_dirty_nobuffers(page);
2422 else if (wdata->result < 0)
2424 end_page_writeback(page);
2425 cifs_readpage_to_fscache(inode, page);
2428 if (wdata->result != -EAGAIN)
2429 mapping_set_error(inode->i_mapping, wdata->result);
2430 kref_put(&wdata->refcount, cifs_writedata_release);
2433 struct cifs_writedata *
2434 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
2436 struct page **pages =
2437 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
2439 return cifs_writedata_direct_alloc(pages, complete);
2444 struct cifs_writedata *
2445 cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2447 struct cifs_writedata *wdata;
2449 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
2450 if (wdata != NULL) {
2451 wdata->pages = pages;
2452 kref_init(&wdata->refcount);
2453 INIT_LIST_HEAD(&wdata->list);
2454 init_completion(&wdata->done);
2455 INIT_WORK(&wdata->work, complete);
2461 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2463 struct address_space *mapping = page->mapping;
2464 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2467 int bytes_written = 0;
2468 struct inode *inode;
2469 struct cifsFileInfo *open_file;
2471 if (!mapping || !mapping->host)
2474 inode = page->mapping->host;
2476 offset += (loff_t)from;
2477 write_data = kmap(page);
2480 if ((to > PAGE_SIZE) || (from > to)) {
2485 /* racing with truncate? */
2486 if (offset > mapping->host->i_size) {
2488 return 0; /* don't care */
2491 /* check to make sure that we are not extending the file */
2492 if (mapping->host->i_size - offset < (loff_t)to)
2493 to = (unsigned)(mapping->host->i_size - offset);
2495 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2498 bytes_written = cifs_write(open_file, open_file->pid,
2499 write_data, to - from, &offset);
2500 cifsFileInfo_put(open_file);
2501 /* Does mm or vfs already set times? */
2502 inode->i_atime = inode->i_mtime = current_time(inode);
2503 if ((bytes_written > 0) && (offset))
2505 else if (bytes_written < 0)
2510 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2511 if (!is_retryable_error(rc))
2519 static struct cifs_writedata *
2520 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2521 pgoff_t end, pgoff_t *index,
2522 unsigned int *found_pages)
2524 struct cifs_writedata *wdata;
2526 wdata = cifs_writedata_alloc((unsigned int)tofind,
2527 cifs_writev_complete);
2531 *found_pages = find_get_pages_range_tag(mapping, index, end,
2532 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2537 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2538 struct address_space *mapping,
2539 struct writeback_control *wbc,
2540 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2542 unsigned int nr_pages = 0, i;
2545 for (i = 0; i < found_pages; i++) {
2546 page = wdata->pages[i];
2548 * At this point we hold neither the i_pages lock nor the
2549 * page lock: the page may be truncated or invalidated
2550 * (changing page->mapping to NULL), or even swizzled
2551 * back from swapper_space to tmpfs file mapping
2556 else if (!trylock_page(page))
2559 if (unlikely(page->mapping != mapping)) {
2564 if (!wbc->range_cyclic && page->index > end) {
2570 if (*next && (page->index != *next)) {
2571 /* Not next consecutive page */
2576 if (wbc->sync_mode != WB_SYNC_NONE)
2577 wait_on_page_writeback(page);
2579 if (PageWriteback(page) ||
2580 !clear_page_dirty_for_io(page)) {
2586 * This actually clears the dirty bit in the radix tree.
2587 * See cifs_writepage() for more commentary.
2589 set_page_writeback(page);
2590 if (page_offset(page) >= i_size_read(mapping->host)) {
2593 end_page_writeback(page);
2597 wdata->pages[i] = page;
2598 *next = page->index + 1;
2602 /* reset index to refind any pages skipped */
2604 *index = wdata->pages[0]->index + 1;
2606 /* put any pages we aren't going to use */
2607 for (i = nr_pages; i < found_pages; i++) {
2608 put_page(wdata->pages[i]);
2609 wdata->pages[i] = NULL;
2616 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2617 struct address_space *mapping, struct writeback_control *wbc)
2621 wdata->sync_mode = wbc->sync_mode;
2622 wdata->nr_pages = nr_pages;
2623 wdata->offset = page_offset(wdata->pages[0]);
2624 wdata->pagesz = PAGE_SIZE;
2625 wdata->tailsz = min(i_size_read(mapping->host) -
2626 page_offset(wdata->pages[nr_pages - 1]),
2628 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2629 wdata->pid = wdata->cfile->pid;
2631 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2635 if (wdata->cfile->invalidHandle)
2638 rc = wdata->server->ops->async_writev(wdata,
2639 cifs_writedata_release);
2644 static int cifs_writepages(struct address_space *mapping,
2645 struct writeback_control *wbc)
2647 struct inode *inode = mapping->host;
2648 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2649 struct TCP_Server_Info *server;
2650 bool done = false, scanned = false, range_whole = false;
2652 struct cifs_writedata *wdata;
2653 struct cifsFileInfo *cfile = NULL;
2659 * If wsize is smaller than the page cache size, default to writing
2660 * one page at a time via cifs_writepage
2662 if (cifs_sb->ctx->wsize < PAGE_SIZE)
2663 return generic_writepages(mapping, wbc);
2666 if (wbc->range_cyclic) {
2667 index = mapping->writeback_index; /* Start from prev offset */
2670 index = wbc->range_start >> PAGE_SHIFT;
2671 end = wbc->range_end >> PAGE_SHIFT;
2672 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2676 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2679 while (!done && index <= end) {
2680 unsigned int i, nr_pages, found_pages, wsize;
2681 pgoff_t next = 0, tofind, saved_index = index;
2682 struct cifs_credits credits_on_stack;
2683 struct cifs_credits *credits = &credits_on_stack;
2684 int get_file_rc = 0;
2687 cifsFileInfo_put(cfile);
2689 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2691 /* in case of an error store it to return later */
2695 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2702 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2704 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2709 add_credits_and_wake_if(server, credits, 0);
2713 if (found_pages == 0) {
2714 kref_put(&wdata->refcount, cifs_writedata_release);
2715 add_credits_and_wake_if(server, credits, 0);
2719 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2720 end, &index, &next, &done);
2722 /* nothing to write? */
2723 if (nr_pages == 0) {
2724 kref_put(&wdata->refcount, cifs_writedata_release);
2725 add_credits_and_wake_if(server, credits, 0);
2729 wdata->credits = credits_on_stack;
2730 wdata->cfile = cfile;
2731 wdata->server = server;
2734 if (!wdata->cfile) {
2735 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2737 if (is_retryable_error(get_file_rc))
2742 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2744 for (i = 0; i < nr_pages; ++i)
2745 unlock_page(wdata->pages[i]);
2747 /* send failure -- clean up the mess */
2749 add_credits_and_wake_if(server, &wdata->credits, 0);
2750 for (i = 0; i < nr_pages; ++i) {
2751 if (is_retryable_error(rc))
2752 redirty_page_for_writepage(wbc,
2755 SetPageError(wdata->pages[i]);
2756 end_page_writeback(wdata->pages[i]);
2757 put_page(wdata->pages[i]);
2759 if (!is_retryable_error(rc))
2760 mapping_set_error(mapping, rc);
2762 kref_put(&wdata->refcount, cifs_writedata_release);
2764 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2765 index = saved_index;
2769 /* Return immediately if we received a signal during writing */
2770 if (is_interrupt_error(rc)) {
2775 if (rc != 0 && saved_rc == 0)
2778 wbc->nr_to_write -= nr_pages;
2779 if (wbc->nr_to_write <= 0)
2785 if (!scanned && !done) {
2787 * We hit the last page and there is more work to be done: wrap
2788 * back to the start of the file
2798 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2799 mapping->writeback_index = index;
2802 cifsFileInfo_put(cfile);
2804 /* Indication to update ctime and mtime as close is deferred */
2805 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2810 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2816 /* BB add check for wbc flags */
2818 if (!PageUptodate(page))
2819 cifs_dbg(FYI, "ppw - page not up to date\n");
2822 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2824 * A writepage() implementation always needs to do either this,
2825 * or re-dirty the page with "redirty_page_for_writepage()" in
2826 * the case of a failure.
2828 * Just unlocking the page will cause the radix tree tag-bits
2829 * to fail to update with the state of the page correctly.
2831 set_page_writeback(page);
2833 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2834 if (is_retryable_error(rc)) {
2835 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2837 redirty_page_for_writepage(wbc, page);
2838 } else if (rc != 0) {
2840 mapping_set_error(page->mapping, rc);
2842 SetPageUptodate(page);
2844 end_page_writeback(page);
2850 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2852 int rc = cifs_writepage_locked(page, wbc);
2857 static int cifs_write_end(struct file *file, struct address_space *mapping,
2858 loff_t pos, unsigned len, unsigned copied,
2859 struct page *page, void *fsdata)
2862 struct inode *inode = mapping->host;
2863 struct cifsFileInfo *cfile = file->private_data;
2864 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2867 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2870 pid = current->tgid;
2872 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2875 if (PageChecked(page)) {
2877 SetPageUptodate(page);
2878 ClearPageChecked(page);
2879 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2880 SetPageUptodate(page);
2882 if (!PageUptodate(page)) {
2884 unsigned offset = pos & (PAGE_SIZE - 1);
2888 /* this is probably better than directly calling
2889 partialpage_write since in this function the file handle is
2890 known which we might as well leverage */
2891 /* BB check if anything else missing out of ppw
2892 such as updating last write time */
2893 page_data = kmap(page);
2894 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2895 /* if (rc < 0) should we set writebehind rc? */
2902 set_page_dirty(page);
2906 spin_lock(&inode->i_lock);
2907 if (pos > inode->i_size) {
2908 i_size_write(inode, pos);
2909 inode->i_blocks = (512 - 1 + pos) >> 9;
2911 spin_unlock(&inode->i_lock);
2916 /* Indication to update ctime and mtime as close is deferred */
2917 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2922 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2927 struct cifs_tcon *tcon;
2928 struct TCP_Server_Info *server;
2929 struct cifsFileInfo *smbfile = file->private_data;
2930 struct inode *inode = file_inode(file);
2931 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2933 rc = file_write_and_wait_range(file, start, end);
2935 trace_cifs_fsync_err(inode->i_ino, rc);
2941 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2944 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2945 rc = cifs_zap_mapping(inode);
2947 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2948 rc = 0; /* don't care about it in fsync */
2952 tcon = tlink_tcon(smbfile->tlink);
2953 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2954 server = tcon->ses->server;
2955 if (server->ops->flush == NULL) {
2957 goto strict_fsync_exit;
2960 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2961 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
2963 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2964 cifsFileInfo_put(smbfile);
2966 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2968 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2976 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2980 struct cifs_tcon *tcon;
2981 struct TCP_Server_Info *server;
2982 struct cifsFileInfo *smbfile = file->private_data;
2983 struct inode *inode = file_inode(file);
2984 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2986 rc = file_write_and_wait_range(file, start, end);
2988 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
2994 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2997 tcon = tlink_tcon(smbfile->tlink);
2998 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2999 server = tcon->ses->server;
3000 if (server->ops->flush == NULL) {
3005 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3006 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3008 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3009 cifsFileInfo_put(smbfile);
3011 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3013 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3022 * As file closes, flush all cached write data for this inode checking
3023 * for write behind errors.
3025 int cifs_flush(struct file *file, fl_owner_t id)
3027 struct inode *inode = file_inode(file);
3030 if (file->f_mode & FMODE_WRITE)
3031 rc = filemap_write_and_wait(inode->i_mapping);
3033 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
3035 /* get more nuanced writeback errors */
3036 rc = filemap_check_wb_err(file->f_mapping, 0);
3037 trace_cifs_flush_err(inode->i_ino, rc);
3043 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
3048 for (i = 0; i < num_pages; i++) {
3049 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3052 * save number of pages we have already allocated and
3053 * return with ENOMEM error
3062 for (i = 0; i < num_pages; i++)
3069 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
3074 clen = min_t(const size_t, len, wsize);
3075 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
3084 cifs_uncached_writedata_release(struct kref *refcount)
3087 struct cifs_writedata *wdata = container_of(refcount,
3088 struct cifs_writedata, refcount);
3090 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
3091 for (i = 0; i < wdata->nr_pages; i++)
3092 put_page(wdata->pages[i]);
3093 cifs_writedata_release(refcount);
3096 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
3099 cifs_uncached_writev_complete(struct work_struct *work)
3101 struct cifs_writedata *wdata = container_of(work,
3102 struct cifs_writedata, work);
3103 struct inode *inode = d_inode(wdata->cfile->dentry);
3104 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3106 spin_lock(&inode->i_lock);
3107 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
3108 if (cifsi->server_eof > inode->i_size)
3109 i_size_write(inode, cifsi->server_eof);
3110 spin_unlock(&inode->i_lock);
3112 complete(&wdata->done);
3113 collect_uncached_write_data(wdata->ctx);
3114 /* the below call can possibly free the last ref to aio ctx */
3115 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3119 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
3120 size_t *len, unsigned long *num_pages)
3122 size_t save_len, copied, bytes, cur_len = *len;
3123 unsigned long i, nr_pages = *num_pages;
3126 for (i = 0; i < nr_pages; i++) {
3127 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
3128 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
3131 * If we didn't copy as much as we expected, then that
3132 * may mean we trod into an unmapped area. Stop copying
3133 * at that point. On the next pass through the big
3134 * loop, we'll likely end up getting a zero-length
3135 * write and bailing out of it.
3140 cur_len = save_len - cur_len;
3144 * If we have no data to send, then that probably means that
3145 * the copy above failed altogether. That's most likely because
3146 * the address in the iovec was bogus. Return -EFAULT and let
3147 * the caller free anything we allocated and bail out.
3153 * i + 1 now represents the number of pages we actually used in
3154 * the copy phase above.
3161 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
3162 struct cifs_aio_ctx *ctx)
3165 struct cifs_credits credits;
3167 struct TCP_Server_Info *server = wdata->server;
3170 if (wdata->cfile->invalidHandle) {
3171 rc = cifs_reopen_file(wdata->cfile, false);
3180 * Wait for credits to resend this wdata.
3181 * Note: we are attempting to resend the whole wdata not in
3185 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
3190 if (wsize < wdata->bytes) {
3191 add_credits_and_wake_if(server, &credits, 0);
3194 } while (wsize < wdata->bytes);
3195 wdata->credits = credits;
3197 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3200 if (wdata->cfile->invalidHandle)
3203 #ifdef CONFIG_CIFS_SMB_DIRECT
3205 wdata->mr->need_invalidate = true;
3206 smbd_deregister_mr(wdata->mr);
3210 rc = server->ops->async_writev(wdata,
3211 cifs_uncached_writedata_release);
3215 /* If the write was successfully sent, we are done */
3217 list_add_tail(&wdata->list, wdata_list);
3221 /* Roll back credits and retry if needed */
3222 add_credits_and_wake_if(server, &wdata->credits, 0);
3223 } while (rc == -EAGAIN);
3226 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3231 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
3232 struct cifsFileInfo *open_file,
3233 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
3234 struct cifs_aio_ctx *ctx)
3238 unsigned long nr_pages, num_pages, i;
3239 struct cifs_writedata *wdata;
3240 struct iov_iter saved_from = *from;
3241 loff_t saved_offset = offset;
3243 struct TCP_Server_Info *server;
3244 struct page **pagevec;
3248 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3249 pid = open_file->pid;
3251 pid = current->tgid;
3253 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3258 struct cifs_credits credits_on_stack;
3259 struct cifs_credits *credits = &credits_on_stack;
3261 if (open_file->invalidHandle) {
3262 rc = cifs_reopen_file(open_file, false);
3269 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
3274 cur_len = min_t(const size_t, len, wsize);
3276 if (ctx->direct_io) {
3279 result = iov_iter_get_pages_alloc2(
3280 from, &pagevec, cur_len, &start);
3283 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3284 result, iov_iter_type(from),
3285 from->iov_offset, from->count);
3289 add_credits_and_wake_if(server, credits, 0);
3292 cur_len = (size_t)result;
3295 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
3297 wdata = cifs_writedata_direct_alloc(pagevec,
3298 cifs_uncached_writev_complete);
3301 add_credits_and_wake_if(server, credits, 0);
3306 wdata->page_offset = start;
3309 cur_len - (PAGE_SIZE - start) -
3310 (nr_pages - 2) * PAGE_SIZE :
3313 nr_pages = get_numpages(wsize, len, &cur_len);
3314 wdata = cifs_writedata_alloc(nr_pages,
3315 cifs_uncached_writev_complete);
3318 add_credits_and_wake_if(server, credits, 0);
3322 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
3324 kvfree(wdata->pages);
3326 add_credits_and_wake_if(server, credits, 0);
3330 num_pages = nr_pages;
3331 rc = wdata_fill_from_iovec(
3332 wdata, from, &cur_len, &num_pages);
3334 for (i = 0; i < nr_pages; i++)
3335 put_page(wdata->pages[i]);
3336 kvfree(wdata->pages);
3338 add_credits_and_wake_if(server, credits, 0);
3343 * Bring nr_pages down to the number of pages we
3344 * actually used, and free any pages that we didn't use.
3346 for ( ; nr_pages > num_pages; nr_pages--)
3347 put_page(wdata->pages[nr_pages - 1]);
3349 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
3352 wdata->sync_mode = WB_SYNC_ALL;
3353 wdata->nr_pages = nr_pages;
3354 wdata->offset = (__u64)offset;
3355 wdata->cfile = cifsFileInfo_get(open_file);
3356 wdata->server = server;
3358 wdata->bytes = cur_len;
3359 wdata->pagesz = PAGE_SIZE;
3360 wdata->credits = credits_on_stack;
3362 kref_get(&ctx->refcount);
3364 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3367 if (wdata->cfile->invalidHandle)
3370 rc = server->ops->async_writev(wdata,
3371 cifs_uncached_writedata_release);
3375 add_credits_and_wake_if(server, &wdata->credits, 0);
3376 kref_put(&wdata->refcount,
3377 cifs_uncached_writedata_release);
3378 if (rc == -EAGAIN) {
3380 iov_iter_advance(from, offset - saved_offset);
3386 list_add_tail(&wdata->list, wdata_list);
3395 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3397 struct cifs_writedata *wdata, *tmp;
3398 struct cifs_tcon *tcon;
3399 struct cifs_sb_info *cifs_sb;
3400 struct dentry *dentry = ctx->cfile->dentry;
3403 tcon = tlink_tcon(ctx->cfile->tlink);
3404 cifs_sb = CIFS_SB(dentry->d_sb);
3406 mutex_lock(&ctx->aio_mutex);
3408 if (list_empty(&ctx->list)) {
3409 mutex_unlock(&ctx->aio_mutex);
3415 * Wait for and collect replies for any successful sends in order of
3416 * increasing offset. Once an error is hit, then return without waiting
3417 * for any more replies.
3420 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3422 if (!try_wait_for_completion(&wdata->done)) {
3423 mutex_unlock(&ctx->aio_mutex);
3430 ctx->total_len += wdata->bytes;
3432 /* resend call if it's a retryable error */
3433 if (rc == -EAGAIN) {
3434 struct list_head tmp_list;
3435 struct iov_iter tmp_from = ctx->iter;
3437 INIT_LIST_HEAD(&tmp_list);
3438 list_del_init(&wdata->list);
3441 rc = cifs_resend_wdata(
3442 wdata, &tmp_list, ctx);
3444 iov_iter_advance(&tmp_from,
3445 wdata->offset - ctx->pos);
3447 rc = cifs_write_from_iter(wdata->offset,
3448 wdata->bytes, &tmp_from,
3449 ctx->cfile, cifs_sb, &tmp_list,
3452 kref_put(&wdata->refcount,
3453 cifs_uncached_writedata_release);
3456 list_splice(&tmp_list, &ctx->list);
3460 list_del_init(&wdata->list);
3461 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3464 cifs_stats_bytes_written(tcon, ctx->total_len);
3465 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3467 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3469 mutex_unlock(&ctx->aio_mutex);
3471 if (ctx->iocb && ctx->iocb->ki_complete)
3472 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
3474 complete(&ctx->done);
3477 static ssize_t __cifs_writev(
3478 struct kiocb *iocb, struct iov_iter *from, bool direct)
3480 struct file *file = iocb->ki_filp;
3481 ssize_t total_written = 0;
3482 struct cifsFileInfo *cfile;
3483 struct cifs_tcon *tcon;
3484 struct cifs_sb_info *cifs_sb;
3485 struct cifs_aio_ctx *ctx;
3486 struct iov_iter saved_from = *from;
3487 size_t len = iov_iter_count(from);
3491 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3492 * In this case, fall back to non-direct write function.
3493 * this could be improved by getting pages directly in ITER_KVEC
3495 if (direct && iov_iter_is_kvec(from)) {
3496 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3500 rc = generic_write_checks(iocb, from);
3504 cifs_sb = CIFS_FILE_SB(file);
3505 cfile = file->private_data;
3506 tcon = tlink_tcon(cfile->tlink);
3508 if (!tcon->ses->server->ops->async_writev)
3511 ctx = cifs_aio_ctx_alloc();
3515 ctx->cfile = cifsFileInfo_get(cfile);
3517 if (!is_sync_kiocb(iocb))
3520 ctx->pos = iocb->ki_pos;
3523 ctx->direct_io = true;
3527 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3529 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3534 /* grab a lock here due to read response handlers can access ctx */
3535 mutex_lock(&ctx->aio_mutex);
3537 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3538 cfile, cifs_sb, &ctx->list, ctx);
3541 * If at least one write was successfully sent, then discard any rc
3542 * value from the later writes. If the other write succeeds, then
3543 * we'll end up returning whatever was written. If it fails, then
3544 * we'll get a new rc value from that.
3546 if (!list_empty(&ctx->list))
3549 mutex_unlock(&ctx->aio_mutex);
3552 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3556 if (!is_sync_kiocb(iocb)) {
3557 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3558 return -EIOCBQUEUED;
3561 rc = wait_for_completion_killable(&ctx->done);
3563 mutex_lock(&ctx->aio_mutex);
3564 ctx->rc = rc = -EINTR;
3565 total_written = ctx->total_len;
3566 mutex_unlock(&ctx->aio_mutex);
3569 total_written = ctx->total_len;
3572 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3574 if (unlikely(!total_written))
3577 iocb->ki_pos += total_written;
3578 return total_written;
3581 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3583 return __cifs_writev(iocb, from, true);
3586 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3588 return __cifs_writev(iocb, from, false);
3592 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3594 struct file *file = iocb->ki_filp;
3595 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3596 struct inode *inode = file->f_mapping->host;
3597 struct cifsInodeInfo *cinode = CIFS_I(inode);
3598 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3603 * We need to hold the sem to be sure nobody modifies lock list
3604 * with a brlock that prevents writing.
3606 down_read(&cinode->lock_sem);
3608 rc = generic_write_checks(iocb, from);
3612 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3613 server->vals->exclusive_lock_type, 0,
3614 NULL, CIFS_WRITE_OP))
3615 rc = __generic_file_write_iter(iocb, from);
3619 up_read(&cinode->lock_sem);
3620 inode_unlock(inode);
3623 rc = generic_write_sync(iocb, rc);
3628 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3630 struct inode *inode = file_inode(iocb->ki_filp);
3631 struct cifsInodeInfo *cinode = CIFS_I(inode);
3632 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3633 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3634 iocb->ki_filp->private_data;
3635 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3638 written = cifs_get_writer(cinode);
3642 if (CIFS_CACHE_WRITE(cinode)) {
3643 if (cap_unix(tcon->ses) &&
3644 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3645 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3646 written = generic_file_write_iter(iocb, from);
3649 written = cifs_writev(iocb, from);
3653 * For non-oplocked files in strict cache mode we need to write the data
3654 * to the server exactly from the pos to pos+len-1 rather than flush all
3655 * affected pages because it may cause a error with mandatory locks on
3656 * these pages but not on the region from pos to ppos+len-1.
3658 written = cifs_user_writev(iocb, from);
3659 if (CIFS_CACHE_READ(cinode)) {
3661 * We have read level caching and we have just sent a write
3662 * request to the server thus making data in the cache stale.
3663 * Zap the cache and set oplock/lease level to NONE to avoid
3664 * reading stale data from the cache. All subsequent read
3665 * operations will read new data from the server.
3667 cifs_zap_mapping(inode);
3668 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3673 cifs_put_writer(cinode);
3677 static struct cifs_readdata *
3678 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3680 struct cifs_readdata *rdata;
3682 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3683 if (rdata != NULL) {
3684 rdata->pages = pages;
3685 kref_init(&rdata->refcount);
3686 INIT_LIST_HEAD(&rdata->list);
3687 init_completion(&rdata->done);
3688 INIT_WORK(&rdata->work, complete);
3694 static struct cifs_readdata *
3695 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3697 struct page **pages =
3698 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3699 struct cifs_readdata *ret = NULL;
3702 ret = cifs_readdata_direct_alloc(pages, complete);
3711 cifs_readdata_release(struct kref *refcount)
3713 struct cifs_readdata *rdata = container_of(refcount,
3714 struct cifs_readdata, refcount);
3715 #ifdef CONFIG_CIFS_SMB_DIRECT
3717 smbd_deregister_mr(rdata->mr);
3722 cifsFileInfo_put(rdata->cfile);
3724 kvfree(rdata->pages);
3729 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3735 for (i = 0; i < nr_pages; i++) {
3736 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3741 rdata->pages[i] = page;
3745 unsigned int nr_page_failed = i;
3747 for (i = 0; i < nr_page_failed; i++) {
3748 put_page(rdata->pages[i]);
3749 rdata->pages[i] = NULL;
3756 cifs_uncached_readdata_release(struct kref *refcount)
3758 struct cifs_readdata *rdata = container_of(refcount,
3759 struct cifs_readdata, refcount);
3762 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3763 for (i = 0; i < rdata->nr_pages; i++) {
3764 put_page(rdata->pages[i]);
3766 cifs_readdata_release(refcount);
3770 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3771 * @rdata: the readdata response with list of pages holding data
3772 * @iter: destination for our data
3774 * This function copies data from a list of pages in a readdata response into
3775 * an array of iovecs. It will first calculate where the data should go
3776 * based on the info in the readdata and then copy the data into that spot.
3779 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3781 size_t remaining = rdata->got_bytes;
3784 for (i = 0; i < rdata->nr_pages; i++) {
3785 struct page *page = rdata->pages[i];
3786 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3789 if (unlikely(iov_iter_is_pipe(iter))) {
3790 void *addr = kmap_atomic(page);
3792 written = copy_to_iter(addr, copy, iter);
3793 kunmap_atomic(addr);
3795 written = copy_page_to_iter(page, 0, copy, iter);
3796 remaining -= written;
3797 if (written < copy && iov_iter_count(iter) > 0)
3800 return remaining ? -EFAULT : 0;
3803 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3806 cifs_uncached_readv_complete(struct work_struct *work)
3808 struct cifs_readdata *rdata = container_of(work,
3809 struct cifs_readdata, work);
3811 complete(&rdata->done);
3812 collect_uncached_read_data(rdata->ctx);
3813 /* the below call can possibly free the last ref to aio ctx */
3814 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3818 uncached_fill_pages(struct TCP_Server_Info *server,
3819 struct cifs_readdata *rdata, struct iov_iter *iter,
3824 unsigned int nr_pages = rdata->nr_pages;
3825 unsigned int page_offset = rdata->page_offset;
3827 rdata->got_bytes = 0;
3828 rdata->tailsz = PAGE_SIZE;
3829 for (i = 0; i < nr_pages; i++) {
3830 struct page *page = rdata->pages[i];
3832 unsigned int segment_size = rdata->pagesz;
3835 segment_size -= page_offset;
3841 /* no need to hold page hostage */
3842 rdata->pages[i] = NULL;
3849 if (len >= segment_size)
3850 /* enough data to fill the page */
3853 rdata->tailsz = len;
3857 result = copy_page_from_iter(
3858 page, page_offset, n, iter);
3859 #ifdef CONFIG_CIFS_SMB_DIRECT
3864 result = cifs_read_page_from_socket(
3865 server, page, page_offset, n);
3869 rdata->got_bytes += result;
3872 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3873 rdata->got_bytes : result;
3877 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3878 struct cifs_readdata *rdata, unsigned int len)
3880 return uncached_fill_pages(server, rdata, NULL, len);
3884 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3885 struct cifs_readdata *rdata,
3886 struct iov_iter *iter)
3888 return uncached_fill_pages(server, rdata, iter, iter->count);
3891 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3892 struct list_head *rdata_list,
3893 struct cifs_aio_ctx *ctx)
3896 struct cifs_credits credits;
3898 struct TCP_Server_Info *server;
3900 /* XXX: should we pick a new channel here? */
3901 server = rdata->server;
3904 if (rdata->cfile->invalidHandle) {
3905 rc = cifs_reopen_file(rdata->cfile, true);
3913 * Wait for credits to resend this rdata.
3914 * Note: we are attempting to resend the whole rdata not in
3918 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3924 if (rsize < rdata->bytes) {
3925 add_credits_and_wake_if(server, &credits, 0);
3928 } while (rsize < rdata->bytes);
3929 rdata->credits = credits;
3931 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3933 if (rdata->cfile->invalidHandle)
3936 #ifdef CONFIG_CIFS_SMB_DIRECT
3938 rdata->mr->need_invalidate = true;
3939 smbd_deregister_mr(rdata->mr);
3943 rc = server->ops->async_readv(rdata);
3947 /* If the read was successfully sent, we are done */
3949 /* Add to aio pending list */
3950 list_add_tail(&rdata->list, rdata_list);
3954 /* Roll back credits and retry if needed */
3955 add_credits_and_wake_if(server, &rdata->credits, 0);
3956 } while (rc == -EAGAIN);
3959 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3964 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3965 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3966 struct cifs_aio_ctx *ctx)
3968 struct cifs_readdata *rdata;
3969 unsigned int npages, rsize;
3970 struct cifs_credits credits_on_stack;
3971 struct cifs_credits *credits = &credits_on_stack;
3975 struct TCP_Server_Info *server;
3976 struct page **pagevec;
3978 struct iov_iter direct_iov = ctx->iter;
3980 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3982 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3983 pid = open_file->pid;
3985 pid = current->tgid;
3988 iov_iter_advance(&direct_iov, offset - ctx->pos);
3991 if (open_file->invalidHandle) {
3992 rc = cifs_reopen_file(open_file, true);
3999 if (cifs_sb->ctx->rsize == 0)
4000 cifs_sb->ctx->rsize =
4001 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4004 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4009 cur_len = min_t(const size_t, len, rsize);
4011 if (ctx->direct_io) {
4014 result = iov_iter_get_pages_alloc2(
4015 &direct_iov, &pagevec,
4019 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
4020 result, iov_iter_type(&direct_iov),
4021 direct_iov.iov_offset,
4026 add_credits_and_wake_if(server, credits, 0);
4029 cur_len = (size_t)result;
4031 rdata = cifs_readdata_direct_alloc(
4032 pagevec, cifs_uncached_readv_complete);
4034 add_credits_and_wake_if(server, credits, 0);
4039 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
4040 rdata->page_offset = start;
4041 rdata->tailsz = npages > 1 ?
4042 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
4047 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
4048 /* allocate a readdata struct */
4049 rdata = cifs_readdata_alloc(npages,
4050 cifs_uncached_readv_complete);
4052 add_credits_and_wake_if(server, credits, 0);
4057 rc = cifs_read_allocate_pages(rdata, npages);
4059 kvfree(rdata->pages);
4061 add_credits_and_wake_if(server, credits, 0);
4065 rdata->tailsz = PAGE_SIZE;
4068 rdata->server = server;
4069 rdata->cfile = cifsFileInfo_get(open_file);
4070 rdata->nr_pages = npages;
4071 rdata->offset = offset;
4072 rdata->bytes = cur_len;
4074 rdata->pagesz = PAGE_SIZE;
4075 rdata->read_into_pages = cifs_uncached_read_into_pages;
4076 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
4077 rdata->credits = credits_on_stack;
4079 kref_get(&ctx->refcount);
4081 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4084 if (rdata->cfile->invalidHandle)
4087 rc = server->ops->async_readv(rdata);
4091 add_credits_and_wake_if(server, &rdata->credits, 0);
4092 kref_put(&rdata->refcount,
4093 cifs_uncached_readdata_release);
4094 if (rc == -EAGAIN) {
4095 iov_iter_revert(&direct_iov, cur_len);
4101 list_add_tail(&rdata->list, rdata_list);
4110 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
4112 struct cifs_readdata *rdata, *tmp;
4113 struct iov_iter *to = &ctx->iter;
4114 struct cifs_sb_info *cifs_sb;
4117 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
4119 mutex_lock(&ctx->aio_mutex);
4121 if (list_empty(&ctx->list)) {
4122 mutex_unlock(&ctx->aio_mutex);
4127 /* the loop below should proceed in the order of increasing offsets */
4129 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
4131 if (!try_wait_for_completion(&rdata->done)) {
4132 mutex_unlock(&ctx->aio_mutex);
4136 if (rdata->result == -EAGAIN) {
4137 /* resend call if it's a retryable error */
4138 struct list_head tmp_list;
4139 unsigned int got_bytes = rdata->got_bytes;
4141 list_del_init(&rdata->list);
4142 INIT_LIST_HEAD(&tmp_list);
4145 * Got a part of data and then reconnect has
4146 * happened -- fill the buffer and continue
4149 if (got_bytes && got_bytes < rdata->bytes) {
4151 if (!ctx->direct_io)
4152 rc = cifs_readdata_to_iov(rdata, to);
4154 kref_put(&rdata->refcount,
4155 cifs_uncached_readdata_release);
4160 if (ctx->direct_io) {
4162 * Re-use rdata as this is a
4165 rc = cifs_resend_rdata(
4169 rc = cifs_send_async_read(
4170 rdata->offset + got_bytes,
4171 rdata->bytes - got_bytes,
4172 rdata->cfile, cifs_sb,
4175 kref_put(&rdata->refcount,
4176 cifs_uncached_readdata_release);
4179 list_splice(&tmp_list, &ctx->list);
4182 } else if (rdata->result)
4184 else if (!ctx->direct_io)
4185 rc = cifs_readdata_to_iov(rdata, to);
4187 /* if there was a short read -- discard anything left */
4188 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
4191 ctx->total_len += rdata->got_bytes;
4193 list_del_init(&rdata->list);
4194 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
4197 if (!ctx->direct_io)
4198 ctx->total_len = ctx->len - iov_iter_count(to);
4200 /* mask nodata case */
4204 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
4206 mutex_unlock(&ctx->aio_mutex);
4208 if (ctx->iocb && ctx->iocb->ki_complete)
4209 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
4211 complete(&ctx->done);
4214 static ssize_t __cifs_readv(
4215 struct kiocb *iocb, struct iov_iter *to, bool direct)
4218 struct file *file = iocb->ki_filp;
4219 struct cifs_sb_info *cifs_sb;
4220 struct cifsFileInfo *cfile;
4221 struct cifs_tcon *tcon;
4222 ssize_t rc, total_read = 0;
4223 loff_t offset = iocb->ki_pos;
4224 struct cifs_aio_ctx *ctx;
4227 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
4228 * fall back to data copy read path
4229 * this could be improved by getting pages directly in ITER_KVEC
4231 if (direct && iov_iter_is_kvec(to)) {
4232 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
4236 len = iov_iter_count(to);
4240 cifs_sb = CIFS_FILE_SB(file);
4241 cfile = file->private_data;
4242 tcon = tlink_tcon(cfile->tlink);
4244 if (!tcon->ses->server->ops->async_readv)
4247 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4248 cifs_dbg(FYI, "attempting read on write only file instance\n");
4250 ctx = cifs_aio_ctx_alloc();
4254 ctx->cfile = cifsFileInfo_get(cfile);
4256 if (!is_sync_kiocb(iocb))
4259 if (user_backed_iter(to))
4260 ctx->should_dirty = true;
4264 ctx->direct_io = true;
4268 rc = setup_aio_ctx_iter(ctx, to, READ);
4270 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4276 /* grab a lock here due to read response handlers can access ctx */
4277 mutex_lock(&ctx->aio_mutex);
4279 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
4281 /* if at least one read request send succeeded, then reset rc */
4282 if (!list_empty(&ctx->list))
4285 mutex_unlock(&ctx->aio_mutex);
4288 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4292 if (!is_sync_kiocb(iocb)) {
4293 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4294 return -EIOCBQUEUED;
4297 rc = wait_for_completion_killable(&ctx->done);
4299 mutex_lock(&ctx->aio_mutex);
4300 ctx->rc = rc = -EINTR;
4301 total_read = ctx->total_len;
4302 mutex_unlock(&ctx->aio_mutex);
4305 total_read = ctx->total_len;
4308 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4311 iocb->ki_pos += total_read;
4317 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
4319 return __cifs_readv(iocb, to, true);
4322 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
4324 return __cifs_readv(iocb, to, false);
4328 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
4330 struct inode *inode = file_inode(iocb->ki_filp);
4331 struct cifsInodeInfo *cinode = CIFS_I(inode);
4332 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4333 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
4334 iocb->ki_filp->private_data;
4335 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4339 * In strict cache mode we need to read from the server all the time
4340 * if we don't have level II oplock because the server can delay mtime
4341 * change - so we can't make a decision about inode invalidating.
4342 * And we can also fail with pagereading if there are mandatory locks
4343 * on pages affected by this read but not on the region from pos to
4346 if (!CIFS_CACHE_READ(cinode))
4347 return cifs_user_readv(iocb, to);
4349 if (cap_unix(tcon->ses) &&
4350 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
4351 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
4352 return generic_file_read_iter(iocb, to);
4355 * We need to hold the sem to be sure nobody modifies lock list
4356 * with a brlock that prevents reading.
4358 down_read(&cinode->lock_sem);
4359 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4360 tcon->ses->server->vals->shared_lock_type,
4361 0, NULL, CIFS_READ_OP))
4362 rc = generic_file_read_iter(iocb, to);
4363 up_read(&cinode->lock_sem);
4368 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4371 unsigned int bytes_read = 0;
4372 unsigned int total_read;
4373 unsigned int current_read_size;
4375 struct cifs_sb_info *cifs_sb;
4376 struct cifs_tcon *tcon;
4377 struct TCP_Server_Info *server;
4380 struct cifsFileInfo *open_file;
4381 struct cifs_io_parms io_parms = {0};
4382 int buf_type = CIFS_NO_BUFFER;
4386 cifs_sb = CIFS_FILE_SB(file);
4388 /* FIXME: set up handlers for larger reads and/or convert to async */
4389 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4391 if (file->private_data == NULL) {
4396 open_file = file->private_data;
4397 tcon = tlink_tcon(open_file->tlink);
4398 server = cifs_pick_channel(tcon->ses);
4400 if (!server->ops->sync_read) {
4405 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4406 pid = open_file->pid;
4408 pid = current->tgid;
4410 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4411 cifs_dbg(FYI, "attempting read on write only file instance\n");
4413 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4414 total_read += bytes_read, cur_offset += bytes_read) {
4416 current_read_size = min_t(uint, read_size - total_read,
4419 * For windows me and 9x we do not want to request more
4420 * than it negotiated since it will refuse the read
4423 if (!(tcon->ses->capabilities &
4424 tcon->ses->server->vals->cap_large_files)) {
4425 current_read_size = min_t(uint,
4426 current_read_size, CIFSMaxBufSize);
4428 if (open_file->invalidHandle) {
4429 rc = cifs_reopen_file(open_file, true);
4434 io_parms.tcon = tcon;
4435 io_parms.offset = *offset;
4436 io_parms.length = current_read_size;
4437 io_parms.server = server;
4438 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4439 &bytes_read, &cur_offset,
4441 } while (rc == -EAGAIN);
4443 if (rc || (bytes_read == 0)) {
4451 cifs_stats_bytes_read(tcon, total_read);
4452 *offset += bytes_read;
4460 * If the page is mmap'ed into a process' page tables, then we need to make
4461 * sure that it doesn't change while being written back.
4464 cifs_page_mkwrite(struct vm_fault *vmf)
4466 struct page *page = vmf->page;
4468 /* Wait for the page to be written to the cache before we allow it to
4469 * be modified. We then assume the entire page will need writing back.
4471 #ifdef CONFIG_CIFS_FSCACHE
4472 if (PageFsCache(page) &&
4473 wait_on_page_fscache_killable(page) < 0)
4474 return VM_FAULT_RETRY;
4477 wait_on_page_writeback(page);
4479 if (lock_page_killable(page) < 0)
4480 return VM_FAULT_RETRY;
4481 return VM_FAULT_LOCKED;
4484 static const struct vm_operations_struct cifs_file_vm_ops = {
4485 .fault = filemap_fault,
4486 .map_pages = filemap_map_pages,
4487 .page_mkwrite = cifs_page_mkwrite,
4490 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4493 struct inode *inode = file_inode(file);
4497 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4498 rc = cifs_zap_mapping(inode);
4500 rc = generic_file_mmap(file, vma);
4502 vma->vm_ops = &cifs_file_vm_ops;
4508 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4514 rc = cifs_revalidate_file(file);
4516 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4519 rc = generic_file_mmap(file, vma);
4521 vma->vm_ops = &cifs_file_vm_ops;
4528 cifs_readv_complete(struct work_struct *work)
4530 unsigned int i, got_bytes;
4531 struct cifs_readdata *rdata = container_of(work,
4532 struct cifs_readdata, work);
4534 got_bytes = rdata->got_bytes;
4535 for (i = 0; i < rdata->nr_pages; i++) {
4536 struct page *page = rdata->pages[i];
4538 if (rdata->result == 0 ||
4539 (rdata->result == -EAGAIN && got_bytes)) {
4540 flush_dcache_page(page);
4541 SetPageUptodate(page);
4545 if (rdata->result == 0 ||
4546 (rdata->result == -EAGAIN && got_bytes))
4547 cifs_readpage_to_fscache(rdata->mapping->host, page);
4551 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4554 rdata->pages[i] = NULL;
4556 kref_put(&rdata->refcount, cifs_readdata_release);
4560 readpages_fill_pages(struct TCP_Server_Info *server,
4561 struct cifs_readdata *rdata, struct iov_iter *iter,
4568 unsigned int nr_pages = rdata->nr_pages;
4569 unsigned int page_offset = rdata->page_offset;
4571 /* determine the eof that the server (probably) has */
4572 eof = CIFS_I(rdata->mapping->host)->server_eof;
4573 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4574 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4576 rdata->got_bytes = 0;
4577 rdata->tailsz = PAGE_SIZE;
4578 for (i = 0; i < nr_pages; i++) {
4579 struct page *page = rdata->pages[i];
4580 unsigned int to_read = rdata->pagesz;
4584 to_read -= page_offset;
4590 if (len >= to_read) {
4592 } else if (len > 0) {
4593 /* enough for partial page, fill and zero the rest */
4594 zero_user(page, len + page_offset, to_read - len);
4595 n = rdata->tailsz = len;
4597 } else if (page->index > eof_index) {
4599 * The VFS will not try to do readahead past the
4600 * i_size, but it's possible that we have outstanding
4601 * writes with gaps in the middle and the i_size hasn't
4602 * caught up yet. Populate those with zeroed out pages
4603 * to prevent the VFS from repeatedly attempting to
4604 * fill them until the writes are flushed.
4606 zero_user(page, 0, PAGE_SIZE);
4607 flush_dcache_page(page);
4608 SetPageUptodate(page);
4611 rdata->pages[i] = NULL;
4615 /* no need to hold page hostage */
4618 rdata->pages[i] = NULL;
4624 result = copy_page_from_iter(
4625 page, page_offset, n, iter);
4626 #ifdef CONFIG_CIFS_SMB_DIRECT
4631 result = cifs_read_page_from_socket(
4632 server, page, page_offset, n);
4636 rdata->got_bytes += result;
4639 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4640 rdata->got_bytes : result;
4644 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4645 struct cifs_readdata *rdata, unsigned int len)
4647 return readpages_fill_pages(server, rdata, NULL, len);
4651 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4652 struct cifs_readdata *rdata,
4653 struct iov_iter *iter)
4655 return readpages_fill_pages(server, rdata, iter, iter->count);
4658 static void cifs_readahead(struct readahead_control *ractl)
4661 struct cifsFileInfo *open_file = ractl->file->private_data;
4662 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file);
4663 struct TCP_Server_Info *server;
4665 unsigned int xid, nr_pages, last_batch_size = 0, cache_nr_pages = 0;
4666 pgoff_t next_cached = ULONG_MAX;
4667 bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) &&
4668 cifs_inode_cookie(ractl->mapping->host)->cache_priv;
4669 bool check_cache = caching;
4673 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4674 pid = open_file->pid;
4676 pid = current->tgid;
4679 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4681 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4682 __func__, ractl->file, ractl->mapping, readahead_count(ractl));
4685 * Chop the readahead request up into rsize-sized read requests.
4687 while ((nr_pages = readahead_count(ractl) - last_batch_size)) {
4688 unsigned int i, got, rsize;
4690 struct cifs_readdata *rdata;
4691 struct cifs_credits credits_on_stack;
4692 struct cifs_credits *credits = &credits_on_stack;
4693 pgoff_t index = readahead_index(ractl) + last_batch_size;
4696 * Find out if we have anything cached in the range of
4697 * interest, and if so, where the next chunk of cached data is.
4701 rc = cifs_fscache_query_occupancy(
4702 ractl->mapping->host, index, nr_pages,
4703 &next_cached, &cache_nr_pages);
4706 check_cache = false;
4709 if (index == next_cached) {
4711 * TODO: Send a whole batch of pages to be read
4714 struct folio *folio = readahead_folio(ractl);
4716 last_batch_size = folio_nr_pages(folio);
4717 if (cifs_readpage_from_fscache(ractl->mapping->host,
4718 &folio->page) < 0) {
4720 * TODO: Deal with cache read failure
4721 * here, but for the moment, delegate
4726 folio_unlock(folio);
4729 if (cache_nr_pages == 0)
4735 if (open_file->invalidHandle) {
4736 rc = cifs_reopen_file(open_file, true);
4744 if (cifs_sb->ctx->rsize == 0)
4745 cifs_sb->ctx->rsize =
4746 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4749 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4753 nr_pages = min_t(size_t, rsize / PAGE_SIZE, readahead_count(ractl));
4754 nr_pages = min_t(size_t, nr_pages, next_cached - index);
4757 * Give up immediately if rsize is too small to read an entire
4758 * page. The VFS will fall back to readpage. We should never
4759 * reach this point however since we set ra_pages to 0 when the
4760 * rsize is smaller than a cache page.
4762 if (unlikely(!nr_pages)) {
4763 add_credits_and_wake_if(server, credits, 0);
4767 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4769 /* best to give up if we're out of mem */
4770 add_credits_and_wake_if(server, credits, 0);
4774 got = __readahead_batch(ractl, rdata->pages, nr_pages);
4775 if (got != nr_pages) {
4776 pr_warn("__readahead_batch() returned %u/%u\n",
4781 rdata->nr_pages = nr_pages;
4782 rdata->bytes = readahead_batch_length(ractl);
4783 rdata->cfile = cifsFileInfo_get(open_file);
4784 rdata->server = server;
4785 rdata->mapping = ractl->mapping;
4786 rdata->offset = readahead_pos(ractl);
4788 rdata->pagesz = PAGE_SIZE;
4789 rdata->tailsz = PAGE_SIZE;
4790 rdata->read_into_pages = cifs_readpages_read_into_pages;
4791 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4792 rdata->credits = credits_on_stack;
4794 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4796 if (rdata->cfile->invalidHandle)
4799 rc = server->ops->async_readv(rdata);
4803 add_credits_and_wake_if(server, &rdata->credits, 0);
4804 for (i = 0; i < rdata->nr_pages; i++) {
4805 page = rdata->pages[i];
4809 /* Fallback to the readpage in error/reconnect cases */
4810 kref_put(&rdata->refcount, cifs_readdata_release);
4814 kref_put(&rdata->refcount, cifs_readdata_release);
4815 last_batch_size = nr_pages;
4822 * cifs_readpage_worker must be called with the page pinned
4824 static int cifs_readpage_worker(struct file *file, struct page *page,
4830 /* Is the page cached? */
4831 rc = cifs_readpage_from_fscache(file_inode(file), page);
4835 read_data = kmap(page);
4836 /* for reads over a certain size could initiate async read ahead */
4838 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4843 cifs_dbg(FYI, "Bytes read %d\n", rc);
4845 /* we do not want atime to be less than mtime, it broke some apps */
4846 file_inode(file)->i_atime = current_time(file_inode(file));
4847 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4848 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4850 file_inode(file)->i_atime = current_time(file_inode(file));
4853 memset(read_data + rc, 0, PAGE_SIZE - rc);
4855 flush_dcache_page(page);
4856 SetPageUptodate(page);
4858 /* send this page to the cache */
4859 cifs_readpage_to_fscache(file_inode(file), page);
4871 static int cifs_read_folio(struct file *file, struct folio *folio)
4873 struct page *page = &folio->page;
4874 loff_t offset = page_file_offset(page);
4880 if (file->private_data == NULL) {
4886 cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n",
4887 page, (int)offset, (int)offset);
4889 rc = cifs_readpage_worker(file, page, &offset);
4895 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4897 struct cifsFileInfo *open_file;
4899 spin_lock(&cifs_inode->open_file_lock);
4900 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4901 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4902 spin_unlock(&cifs_inode->open_file_lock);
4906 spin_unlock(&cifs_inode->open_file_lock);
4910 /* We do not want to update the file size from server for inodes
4911 open for write - to avoid races with writepage extending
4912 the file - in the future we could consider allowing
4913 refreshing the inode only on increases in the file size
4914 but this is tricky to do without racing with writebehind
4915 page caching in the current Linux kernel design */
4916 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4921 if (is_inode_writable(cifsInode)) {
4922 /* This inode is open for write at least once */
4923 struct cifs_sb_info *cifs_sb;
4925 cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
4926 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4927 /* since no page cache to corrupt on directio
4928 we can change size safely */
4932 if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
4940 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4941 loff_t pos, unsigned len,
4942 struct page **pagep, void **fsdata)
4945 pgoff_t index = pos >> PAGE_SHIFT;
4946 loff_t offset = pos & (PAGE_SIZE - 1);
4947 loff_t page_start = pos & PAGE_MASK;
4952 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4955 page = grab_cache_page_write_begin(mapping, index);
4961 if (PageUptodate(page))
4965 * If we write a full page it will be up to date, no need to read from
4966 * the server. If the write is short, we'll end up doing a sync write
4969 if (len == PAGE_SIZE)
4973 * optimize away the read when we have an oplock, and we're not
4974 * expecting to use any of the data we'd be reading in. That
4975 * is, when the page lies beyond the EOF, or straddles the EOF
4976 * and the write will cover all of the existing data.
4978 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4979 i_size = i_size_read(mapping->host);
4980 if (page_start >= i_size ||
4981 (offset == 0 && (pos + len) >= i_size)) {
4982 zero_user_segments(page, 0, offset,
4986 * PageChecked means that the parts of the page
4987 * to which we're not writing are considered up
4988 * to date. Once the data is copied to the
4989 * page, it can be set uptodate.
4991 SetPageChecked(page);
4996 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4998 * might as well read a page, it is fast enough. If we get
4999 * an error, we don't need to return it. cifs_write_end will
5000 * do a sync write instead since PG_uptodate isn't set.
5002 cifs_readpage_worker(file, page, &page_start);
5007 /* we could try using another file handle if there is one -
5008 but how would we lock it to prevent close of that handle
5009 racing with this read? In any case
5010 this will be written out by write_end so is fine */
5017 static bool cifs_release_folio(struct folio *folio, gfp_t gfp)
5019 if (folio_test_private(folio))
5021 if (folio_test_fscache(folio)) {
5022 if (current_is_kswapd() || !(gfp & __GFP_FS))
5024 folio_wait_fscache(folio);
5026 fscache_note_page_release(cifs_inode_cookie(folio->mapping->host));
5030 static void cifs_invalidate_folio(struct folio *folio, size_t offset,
5033 folio_wait_fscache(folio);
5036 static int cifs_launder_folio(struct folio *folio)
5039 loff_t range_start = folio_pos(folio);
5040 loff_t range_end = range_start + folio_size(folio);
5041 struct writeback_control wbc = {
5042 .sync_mode = WB_SYNC_ALL,
5044 .range_start = range_start,
5045 .range_end = range_end,
5048 cifs_dbg(FYI, "Launder page: %lu\n", folio->index);
5050 if (folio_clear_dirty_for_io(folio))
5051 rc = cifs_writepage_locked(&folio->page, &wbc);
5053 folio_wait_fscache(folio);
5057 void cifs_oplock_break(struct work_struct *work)
5059 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
5061 struct inode *inode = d_inode(cfile->dentry);
5062 struct cifsInodeInfo *cinode = CIFS_I(inode);
5063 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
5064 struct TCP_Server_Info *server = tcon->ses->server;
5066 bool purge_cache = false;
5067 bool is_deferred = false;
5068 struct cifs_deferred_close *dclose;
5070 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
5071 TASK_UNINTERRUPTIBLE);
5073 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
5074 cfile->oplock_epoch, &purge_cache);
5076 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
5077 cifs_has_mand_locks(cinode)) {
5078 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
5083 if (inode && S_ISREG(inode->i_mode)) {
5084 if (CIFS_CACHE_READ(cinode))
5085 break_lease(inode, O_RDONLY);
5087 break_lease(inode, O_WRONLY);
5088 rc = filemap_fdatawrite(inode->i_mapping);
5089 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
5090 rc = filemap_fdatawait(inode->i_mapping);
5091 mapping_set_error(inode->i_mapping, rc);
5092 cifs_zap_mapping(inode);
5094 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
5095 if (CIFS_CACHE_WRITE(cinode))
5096 goto oplock_break_ack;
5099 rc = cifs_push_locks(cfile);
5101 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
5105 * When oplock break is received and there are no active
5106 * file handles but cached, then schedule deferred close immediately.
5107 * So, new open will not use cached handle.
5109 spin_lock(&CIFS_I(inode)->deferred_lock);
5110 is_deferred = cifs_is_deferred_close(cfile, &dclose);
5111 spin_unlock(&CIFS_I(inode)->deferred_lock);
5113 cfile->deferred_close_scheduled &&
5114 delayed_work_pending(&cfile->deferred)) {
5115 if (cancel_delayed_work(&cfile->deferred)) {
5116 _cifsFileInfo_put(cfile, false, false);
5117 goto oplock_break_done;
5121 * releasing stale oplock after recent reconnect of smb session using
5122 * a now incorrect file handle is not a data integrity issue but do
5123 * not bother sending an oplock release if session to server still is
5124 * disconnected since oplock already released by the server
5126 if (!cfile->oplock_break_cancelled) {
5127 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
5129 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
5132 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
5133 cifs_done_oplock_break(cinode);
5137 * The presence of cifs_direct_io() in the address space ops vector
5138 * allowes open() O_DIRECT flags which would have failed otherwise.
5140 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
5141 * so this method should never be called.
5143 * Direct IO is not yet supported in the cached mode.
5146 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
5150 * Eventually need to support direct IO for non forcedirectio mounts
5155 static int cifs_swap_activate(struct swap_info_struct *sis,
5156 struct file *swap_file, sector_t *span)
5158 struct cifsFileInfo *cfile = swap_file->private_data;
5159 struct inode *inode = swap_file->f_mapping->host;
5160 unsigned long blocks;
5163 cifs_dbg(FYI, "swap activate\n");
5165 if (!swap_file->f_mapping->a_ops->swap_rw)
5166 /* Cannot support swap */
5169 spin_lock(&inode->i_lock);
5170 blocks = inode->i_blocks;
5171 isize = inode->i_size;
5172 spin_unlock(&inode->i_lock);
5173 if (blocks*512 < isize) {
5174 pr_warn("swap activate: swapfile has holes\n");
5179 pr_warn_once("Swap support over SMB3 is experimental\n");
5182 * TODO: consider adding ACL (or documenting how) to prevent other
5183 * users (on this or other systems) from reading it
5187 /* TODO: add sk_set_memalloc(inet) or similar */
5190 cfile->swapfile = true;
5192 * TODO: Since file already open, we can't open with DENY_ALL here
5193 * but we could add call to grab a byte range lock to prevent others
5194 * from reading or writing the file
5197 sis->flags |= SWP_FS_OPS;
5198 return add_swap_extent(sis, 0, sis->max, 0);
5201 static void cifs_swap_deactivate(struct file *file)
5203 struct cifsFileInfo *cfile = file->private_data;
5205 cifs_dbg(FYI, "swap deactivate\n");
5207 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
5210 cfile->swapfile = false;
5212 /* do we need to unpin (or unlock) the file */
5216 * Mark a page as having been made dirty and thus needing writeback. We also
5217 * need to pin the cache object to write back to.
5219 #ifdef CONFIG_CIFS_FSCACHE
5220 static bool cifs_dirty_folio(struct address_space *mapping, struct folio *folio)
5222 return fscache_dirty_folio(mapping, folio,
5223 cifs_inode_cookie(mapping->host));
5226 #define cifs_dirty_folio filemap_dirty_folio
5229 const struct address_space_operations cifs_addr_ops = {
5230 .read_folio = cifs_read_folio,
5231 .readahead = cifs_readahead,
5232 .writepage = cifs_writepage,
5233 .writepages = cifs_writepages,
5234 .write_begin = cifs_write_begin,
5235 .write_end = cifs_write_end,
5236 .dirty_folio = cifs_dirty_folio,
5237 .release_folio = cifs_release_folio,
5238 .direct_IO = cifs_direct_io,
5239 .invalidate_folio = cifs_invalidate_folio,
5240 .launder_folio = cifs_launder_folio,
5242 * TODO: investigate and if useful we could add an cifs_migratePage
5243 * helper (under an CONFIG_MIGRATION) in the future, and also
5244 * investigate and add an is_dirty_writeback helper if needed
5246 .swap_activate = cifs_swap_activate,
5247 .swap_deactivate = cifs_swap_deactivate,
5251 * cifs_readahead requires the server to support a buffer large enough to
5252 * contain the header plus one complete page of data. Otherwise, we need
5253 * to leave cifs_readahead out of the address space operations.
5255 const struct address_space_operations cifs_addr_ops_smallbuf = {
5256 .read_folio = cifs_read_folio,
5257 .writepage = cifs_writepage,
5258 .writepages = cifs_writepages,
5259 .write_begin = cifs_write_begin,
5260 .write_end = cifs_write_end,
5261 .dirty_folio = cifs_dirty_folio,
5262 .release_folio = cifs_release_folio,
5263 .invalidate_folio = cifs_invalidate_folio,
5264 .launder_folio = cifs_launder_folio,