4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/backing-dev.h>
26 #include <linux/stat.h>
27 #include <linux/fcntl.h>
28 #include <linux/pagemap.h>
29 #include <linux/pagevec.h>
30 #include <linux/writeback.h>
31 #include <linux/task_io_accounting_ops.h>
32 #include <linux/delay.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
37 #include <asm/div64.h>
41 #include "cifsproto.h"
42 #include "cifs_unicode.h"
43 #include "cifs_debug.h"
44 #include "cifs_fs_sb.h"
46 #include "smbdirect.h"
47 #include "fs_context.h"
48 #include "cifs_ioctl.h"
50 static inline int cifs_convert_flags(unsigned int flags)
52 if ((flags & O_ACCMODE) == O_RDONLY)
54 else if ((flags & O_ACCMODE) == O_WRONLY)
56 else if ((flags & O_ACCMODE) == O_RDWR) {
57 /* GENERIC_ALL is too much permission to request
58 can cause unnecessary access denied on create */
59 /* return GENERIC_ALL; */
60 return (GENERIC_READ | GENERIC_WRITE);
63 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
64 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
68 static u32 cifs_posix_convert_flags(unsigned int flags)
72 if ((flags & O_ACCMODE) == O_RDONLY)
73 posix_flags = SMB_O_RDONLY;
74 else if ((flags & O_ACCMODE) == O_WRONLY)
75 posix_flags = SMB_O_WRONLY;
76 else if ((flags & O_ACCMODE) == O_RDWR)
77 posix_flags = SMB_O_RDWR;
79 if (flags & O_CREAT) {
80 posix_flags |= SMB_O_CREAT;
82 posix_flags |= SMB_O_EXCL;
83 } else if (flags & O_EXCL)
84 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
85 current->comm, current->tgid);
88 posix_flags |= SMB_O_TRUNC;
89 /* be safe and imply O_SYNC for O_DSYNC */
91 posix_flags |= SMB_O_SYNC;
92 if (flags & O_DIRECTORY)
93 posix_flags |= SMB_O_DIRECTORY;
94 if (flags & O_NOFOLLOW)
95 posix_flags |= SMB_O_NOFOLLOW;
97 posix_flags |= SMB_O_DIRECT;
102 static inline int cifs_get_disposition(unsigned int flags)
104 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
106 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
107 return FILE_OVERWRITE_IF;
108 else if ((flags & O_CREAT) == O_CREAT)
110 else if ((flags & O_TRUNC) == O_TRUNC)
111 return FILE_OVERWRITE;
116 int cifs_posix_open(const char *full_path, struct inode **pinode,
117 struct super_block *sb, int mode, unsigned int f_flags,
118 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
121 FILE_UNIX_BASIC_INFO *presp_data;
122 __u32 posix_flags = 0;
123 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
124 struct cifs_fattr fattr;
125 struct tcon_link *tlink;
126 struct cifs_tcon *tcon;
128 cifs_dbg(FYI, "posix open %s\n", full_path);
130 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
131 if (presp_data == NULL)
134 tlink = cifs_sb_tlink(cifs_sb);
140 tcon = tlink_tcon(tlink);
141 mode &= ~current_umask();
143 posix_flags = cifs_posix_convert_flags(f_flags);
144 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
145 poplock, full_path, cifs_sb->local_nls,
146 cifs_remap(cifs_sb));
147 cifs_put_tlink(tlink);
152 if (presp_data->Type == cpu_to_le32(-1))
153 goto posix_open_ret; /* open ok, caller does qpathinfo */
156 goto posix_open_ret; /* caller does not need info */
158 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
160 /* get new inode and set it up */
161 if (*pinode == NULL) {
162 cifs_fill_uniqueid(sb, &fattr);
163 *pinode = cifs_iget(sb, &fattr);
169 cifs_revalidate_mapping(*pinode);
170 rc = cifs_fattr_to_inode(*pinode, &fattr);
179 cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
180 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
181 struct cifs_fid *fid, unsigned int xid)
186 int create_options = CREATE_NOT_DIR;
188 struct TCP_Server_Info *server = tcon->ses->server;
189 struct cifs_open_parms oparms;
191 if (!server->ops->open)
194 desired_access = cifs_convert_flags(f_flags);
196 /*********************************************************************
197 * open flag mapping table:
199 * POSIX Flag CIFS Disposition
200 * ---------- ----------------
201 * O_CREAT FILE_OPEN_IF
202 * O_CREAT | O_EXCL FILE_CREATE
203 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
204 * O_TRUNC FILE_OVERWRITE
205 * none of the above FILE_OPEN
207 * Note that there is not a direct match between disposition
208 * FILE_SUPERSEDE (ie create whether or not file exists although
209 * O_CREAT | O_TRUNC is similar but truncates the existing
210 * file rather than creating a new file as FILE_SUPERSEDE does
211 * (which uses the attributes / metadata passed in on open call)
213 *? O_SYNC is a reasonable match to CIFS writethrough flag
214 *? and the read write flags match reasonably. O_LARGEFILE
215 *? is irrelevant because largefile support is always used
216 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
217 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
218 *********************************************************************/
220 disposition = cifs_get_disposition(f_flags);
222 /* BB pass O_SYNC flag through on file attributes .. BB */
224 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
228 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
229 if (f_flags & O_SYNC)
230 create_options |= CREATE_WRITE_THROUGH;
232 if (f_flags & O_DIRECT)
233 create_options |= CREATE_NO_BUFFER;
236 oparms.cifs_sb = cifs_sb;
237 oparms.desired_access = desired_access;
238 oparms.create_options = cifs_create_options(cifs_sb, create_options);
239 oparms.disposition = disposition;
240 oparms.path = full_path;
242 oparms.reconnect = false;
244 rc = server->ops->open(xid, &oparms, oplock, buf);
249 /* TODO: Add support for calling posix query info but with passing in fid */
251 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
254 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
258 server->ops->close(xid, tcon, fid);
269 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
271 struct cifs_fid_locks *cur;
272 bool has_locks = false;
274 down_read(&cinode->lock_sem);
275 list_for_each_entry(cur, &cinode->llist, llist) {
276 if (!list_empty(&cur->locks)) {
281 up_read(&cinode->lock_sem);
286 cifs_down_write(struct rw_semaphore *sem)
288 while (!down_write_trylock(sem))
292 static void cifsFileInfo_put_work(struct work_struct *work);
294 struct cifsFileInfo *
295 cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
296 struct tcon_link *tlink, __u32 oplock)
298 struct dentry *dentry = file_dentry(file);
299 struct inode *inode = d_inode(dentry);
300 struct cifsInodeInfo *cinode = CIFS_I(inode);
301 struct cifsFileInfo *cfile;
302 struct cifs_fid_locks *fdlocks;
303 struct cifs_tcon *tcon = tlink_tcon(tlink);
304 struct TCP_Server_Info *server = tcon->ses->server;
306 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
310 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
316 INIT_LIST_HEAD(&fdlocks->locks);
317 fdlocks->cfile = cfile;
318 cfile->llist = fdlocks;
321 cfile->pid = current->tgid;
322 cfile->uid = current_fsuid();
323 cfile->dentry = dget(dentry);
324 cfile->f_flags = file->f_flags;
325 cfile->invalidHandle = false;
326 cfile->deferred_close_scheduled = false;
327 cfile->tlink = cifs_get_tlink(tlink);
328 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
329 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
330 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
331 mutex_init(&cfile->fh_mutex);
332 spin_lock_init(&cfile->file_info_lock);
334 cifs_sb_active(inode->i_sb);
337 * If the server returned a read oplock and we have mandatory brlocks,
338 * set oplock level to None.
340 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
341 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
345 cifs_down_write(&cinode->lock_sem);
346 list_add(&fdlocks->llist, &cinode->llist);
347 up_write(&cinode->lock_sem);
349 spin_lock(&tcon->open_file_lock);
350 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
351 oplock = fid->pending_open->oplock;
352 list_del(&fid->pending_open->olist);
354 fid->purge_cache = false;
355 server->ops->set_fid(cfile, fid, oplock);
357 list_add(&cfile->tlist, &tcon->openFileList);
358 atomic_inc(&tcon->num_local_opens);
360 /* if readable file instance put first in list*/
361 spin_lock(&cinode->open_file_lock);
362 if (file->f_mode & FMODE_READ)
363 list_add(&cfile->flist, &cinode->openFileList);
365 list_add_tail(&cfile->flist, &cinode->openFileList);
366 spin_unlock(&cinode->open_file_lock);
367 spin_unlock(&tcon->open_file_lock);
369 if (fid->purge_cache)
370 cifs_zap_mapping(inode);
372 file->private_data = cfile;
376 struct cifsFileInfo *
377 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
379 spin_lock(&cifs_file->file_info_lock);
380 cifsFileInfo_get_locked(cifs_file);
381 spin_unlock(&cifs_file->file_info_lock);
385 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
387 struct inode *inode = d_inode(cifs_file->dentry);
388 struct cifsInodeInfo *cifsi = CIFS_I(inode);
389 struct cifsLockInfo *li, *tmp;
390 struct super_block *sb = inode->i_sb;
393 * Delete any outstanding lock records. We'll lose them when the file
396 cifs_down_write(&cifsi->lock_sem);
397 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
398 list_del(&li->llist);
399 cifs_del_lock_waiters(li);
402 list_del(&cifs_file->llist->llist);
403 kfree(cifs_file->llist);
404 up_write(&cifsi->lock_sem);
406 cifs_put_tlink(cifs_file->tlink);
407 dput(cifs_file->dentry);
408 cifs_sb_deactive(sb);
412 static void cifsFileInfo_put_work(struct work_struct *work)
414 struct cifsFileInfo *cifs_file = container_of(work,
415 struct cifsFileInfo, put);
417 cifsFileInfo_put_final(cifs_file);
421 * cifsFileInfo_put - release a reference of file priv data
423 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
425 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
427 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
429 _cifsFileInfo_put(cifs_file, true, true);
433 * _cifsFileInfo_put - release a reference of file priv data
435 * This may involve closing the filehandle @cifs_file out on the
436 * server. Must be called without holding tcon->open_file_lock,
437 * cinode->open_file_lock and cifs_file->file_info_lock.
439 * If @wait_for_oplock_handler is true and we are releasing the last
440 * reference, wait for any running oplock break handler of the file
441 * and cancel any pending one.
443 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
444 * @wait_oplock_handler: must be false if called from oplock_break_handler
445 * @offload: not offloaded on close and oplock breaks
448 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
449 bool wait_oplock_handler, bool offload)
451 struct inode *inode = d_inode(cifs_file->dentry);
452 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
453 struct TCP_Server_Info *server = tcon->ses->server;
454 struct cifsInodeInfo *cifsi = CIFS_I(inode);
455 struct super_block *sb = inode->i_sb;
456 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
458 struct cifs_pending_open open;
459 bool oplock_break_cancelled;
461 spin_lock(&tcon->open_file_lock);
462 spin_lock(&cifsi->open_file_lock);
463 spin_lock(&cifs_file->file_info_lock);
464 if (--cifs_file->count > 0) {
465 spin_unlock(&cifs_file->file_info_lock);
466 spin_unlock(&cifsi->open_file_lock);
467 spin_unlock(&tcon->open_file_lock);
470 spin_unlock(&cifs_file->file_info_lock);
472 if (server->ops->get_lease_key)
473 server->ops->get_lease_key(inode, &fid);
475 /* store open in pending opens to make sure we don't miss lease break */
476 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
478 /* remove it from the lists */
479 list_del(&cifs_file->flist);
480 list_del(&cifs_file->tlist);
481 atomic_dec(&tcon->num_local_opens);
483 if (list_empty(&cifsi->openFileList)) {
484 cifs_dbg(FYI, "closing last open instance for inode %p\n",
485 d_inode(cifs_file->dentry));
487 * In strict cache mode we need invalidate mapping on the last
488 * close because it may cause a error when we open this file
489 * again and get at least level II oplock.
491 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
492 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
493 cifs_set_oplock_level(cifsi, 0);
496 spin_unlock(&cifsi->open_file_lock);
497 spin_unlock(&tcon->open_file_lock);
499 oplock_break_cancelled = wait_oplock_handler ?
500 cancel_work_sync(&cifs_file->oplock_break) : false;
502 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
503 struct TCP_Server_Info *server = tcon->ses->server;
507 if (server->ops->close_getattr)
508 server->ops->close_getattr(xid, tcon, cifs_file);
509 else if (server->ops->close)
510 server->ops->close(xid, tcon, &cifs_file->fid);
514 if (oplock_break_cancelled)
515 cifs_done_oplock_break(cifsi);
517 cifs_del_pending_open(&open);
520 queue_work(fileinfo_put_wq, &cifs_file->put);
522 cifsFileInfo_put_final(cifs_file);
525 int cifs_open(struct inode *inode, struct file *file)
531 struct cifs_sb_info *cifs_sb;
532 struct TCP_Server_Info *server;
533 struct cifs_tcon *tcon;
534 struct tcon_link *tlink;
535 struct cifsFileInfo *cfile = NULL;
537 const char *full_path;
538 bool posix_open_ok = false;
540 struct cifs_pending_open open;
544 cifs_sb = CIFS_SB(inode->i_sb);
545 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
550 tlink = cifs_sb_tlink(cifs_sb);
553 return PTR_ERR(tlink);
555 tcon = tlink_tcon(tlink);
556 server = tcon->ses->server;
558 page = alloc_dentry_path();
559 full_path = build_path_from_dentry(file_dentry(file), page);
560 if (IS_ERR(full_path)) {
561 rc = PTR_ERR(full_path);
565 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
566 inode, file->f_flags, full_path);
568 if (file->f_flags & O_DIRECT &&
569 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
570 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
571 file->f_op = &cifs_file_direct_nobrl_ops;
573 file->f_op = &cifs_file_direct_ops;
576 /* Get the cached handle as SMB2 close is deferred */
577 rc = cifs_get_readable_path(tcon, full_path, &cfile);
579 if (file->f_flags == cfile->f_flags) {
580 file->private_data = cfile;
581 spin_lock(&CIFS_I(inode)->deferred_lock);
582 cifs_del_deferred_close(cfile);
583 spin_unlock(&CIFS_I(inode)->deferred_lock);
586 _cifsFileInfo_put(cfile, true, false);
595 if (!tcon->broken_posix_open && tcon->unix_ext &&
596 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
597 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
598 /* can not refresh inode info since size could be stale */
599 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
600 cifs_sb->ctx->file_mode /* ignored */,
601 file->f_flags, &oplock, &fid.netfid, xid);
603 cifs_dbg(FYI, "posix open succeeded\n");
604 posix_open_ok = true;
605 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
606 if (tcon->ses->serverNOS)
607 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",
609 tcon->ses->serverNOS);
610 tcon->broken_posix_open = true;
611 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
612 (rc != -EOPNOTSUPP)) /* path not found or net err */
615 * Else fallthrough to retry open the old way on network i/o
620 if (server->ops->get_lease_key)
621 server->ops->get_lease_key(inode, &fid);
623 cifs_add_pending_open(&fid, tlink, &open);
625 if (!posix_open_ok) {
626 if (server->ops->get_lease_key)
627 server->ops->get_lease_key(inode, &fid);
629 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
630 file->f_flags, &oplock, &fid, xid);
632 cifs_del_pending_open(&open);
637 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
639 if (server->ops->close)
640 server->ops->close(xid, tcon, &fid);
641 cifs_del_pending_open(&open);
646 cifs_fscache_set_inode_cookie(inode, file);
648 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
650 * Time to set mode which we can not set earlier due to
651 * problems creating new read-only files.
653 struct cifs_unix_set_info_args args = {
654 .mode = inode->i_mode,
655 .uid = INVALID_UID, /* no change */
656 .gid = INVALID_GID, /* no change */
657 .ctime = NO_CHANGE_64,
658 .atime = NO_CHANGE_64,
659 .mtime = NO_CHANGE_64,
662 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
667 free_dentry_path(page);
669 cifs_put_tlink(tlink);
673 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
676 * Try to reacquire byte range locks that were released when session
677 * to server was lost.
680 cifs_relock_file(struct cifsFileInfo *cfile)
682 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
683 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
684 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
687 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
688 if (cinode->can_cache_brlcks) {
689 /* can cache locks - no need to relock */
690 up_read(&cinode->lock_sem);
694 if (cap_unix(tcon->ses) &&
695 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
696 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
697 rc = cifs_push_posix_locks(cfile);
699 rc = tcon->ses->server->ops->push_mand_locks(cfile);
701 up_read(&cinode->lock_sem);
706 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
711 struct cifs_sb_info *cifs_sb;
712 struct cifs_tcon *tcon;
713 struct TCP_Server_Info *server;
714 struct cifsInodeInfo *cinode;
717 const char *full_path;
719 int disposition = FILE_OPEN;
720 int create_options = CREATE_NOT_DIR;
721 struct cifs_open_parms oparms;
724 mutex_lock(&cfile->fh_mutex);
725 if (!cfile->invalidHandle) {
726 mutex_unlock(&cfile->fh_mutex);
731 inode = d_inode(cfile->dentry);
732 cifs_sb = CIFS_SB(inode->i_sb);
733 tcon = tlink_tcon(cfile->tlink);
734 server = tcon->ses->server;
737 * Can not grab rename sem here because various ops, including those
738 * that already have the rename sem can end up causing writepage to get
739 * called and if the server was down that means we end up here, and we
740 * can never tell if the caller already has the rename_sem.
742 page = alloc_dentry_path();
743 full_path = build_path_from_dentry(cfile->dentry, page);
744 if (IS_ERR(full_path)) {
745 mutex_unlock(&cfile->fh_mutex);
746 free_dentry_path(page);
748 return PTR_ERR(full_path);
751 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
752 inode, cfile->f_flags, full_path);
754 if (tcon->ses->server->oplocks)
759 if (tcon->unix_ext && cap_unix(tcon->ses) &&
760 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
761 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
763 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
764 * original open. Must mask them off for a reopen.
766 unsigned int oflags = cfile->f_flags &
767 ~(O_CREAT | O_EXCL | O_TRUNC);
769 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
770 cifs_sb->ctx->file_mode /* ignored */,
771 oflags, &oplock, &cfile->fid.netfid, xid);
773 cifs_dbg(FYI, "posix reopen succeeded\n");
774 oparms.reconnect = true;
778 * fallthrough to retry open the old way on errors, especially
779 * in the reconnect path it is important to retry hard
783 desired_access = cifs_convert_flags(cfile->f_flags);
785 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
786 if (cfile->f_flags & O_SYNC)
787 create_options |= CREATE_WRITE_THROUGH;
789 if (cfile->f_flags & O_DIRECT)
790 create_options |= CREATE_NO_BUFFER;
792 if (server->ops->get_lease_key)
793 server->ops->get_lease_key(inode, &cfile->fid);
796 oparms.cifs_sb = cifs_sb;
797 oparms.desired_access = desired_access;
798 oparms.create_options = cifs_create_options(cifs_sb, create_options);
799 oparms.disposition = disposition;
800 oparms.path = full_path;
801 oparms.fid = &cfile->fid;
802 oparms.reconnect = true;
805 * Can not refresh inode by passing in file_info buf to be returned by
806 * ops->open and then calling get_inode_info with returned buf since
807 * file might have write behind data that needs to be flushed and server
808 * version of file size can be stale. If we knew for sure that inode was
809 * not dirty locally we could do this.
811 rc = server->ops->open(xid, &oparms, &oplock, NULL);
812 if (rc == -ENOENT && oparms.reconnect == false) {
813 /* durable handle timeout is expired - open the file again */
814 rc = server->ops->open(xid, &oparms, &oplock, NULL);
815 /* indicate that we need to relock the file */
816 oparms.reconnect = true;
820 mutex_unlock(&cfile->fh_mutex);
821 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
822 cifs_dbg(FYI, "oplock: %d\n", oplock);
823 goto reopen_error_exit;
827 cfile->invalidHandle = false;
828 mutex_unlock(&cfile->fh_mutex);
829 cinode = CIFS_I(inode);
832 rc = filemap_write_and_wait(inode->i_mapping);
833 if (!is_interrupt_error(rc))
834 mapping_set_error(inode->i_mapping, rc);
836 if (tcon->posix_extensions)
837 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
838 else if (tcon->unix_ext)
839 rc = cifs_get_inode_info_unix(&inode, full_path,
842 rc = cifs_get_inode_info(&inode, full_path, NULL,
843 inode->i_sb, xid, NULL);
846 * Else we are writing out data to server already and could deadlock if
847 * we tried to flush data, and since we do not know if we have data that
848 * would invalidate the current end of file on the server we can not go
849 * to the server to get the new inode info.
853 * If the server returned a read oplock and we have mandatory brlocks,
854 * set oplock level to None.
856 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
857 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
861 server->ops->set_fid(cfile, &cfile->fid, oplock);
862 if (oparms.reconnect)
863 cifs_relock_file(cfile);
866 free_dentry_path(page);
871 void smb2_deferred_work_close(struct work_struct *work)
873 struct cifsFileInfo *cfile = container_of(work,
874 struct cifsFileInfo, deferred.work);
876 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
877 cifs_del_deferred_close(cfile);
878 cfile->deferred_close_scheduled = false;
879 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
880 _cifsFileInfo_put(cfile, true, false);
883 int cifs_close(struct inode *inode, struct file *file)
885 struct cifsFileInfo *cfile;
886 struct cifsInodeInfo *cinode = CIFS_I(inode);
887 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
888 struct cifs_deferred_close *dclose;
890 if (file->private_data != NULL) {
891 cfile = file->private_data;
892 file->private_data = NULL;
893 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
894 if ((cinode->oplock == CIFS_CACHE_RHW_FLG) &&
895 cinode->lease_granted &&
897 if (test_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags))
898 inode->i_ctime = inode->i_mtime = current_time(inode);
899 spin_lock(&cinode->deferred_lock);
900 cifs_add_deferred_close(cfile, dclose);
901 if (cfile->deferred_close_scheduled &&
902 delayed_work_pending(&cfile->deferred)) {
904 * If there is no pending work, mod_delayed_work queues new work.
905 * So, Increase the ref count to avoid use-after-free.
907 if (!mod_delayed_work(deferredclose_wq,
908 &cfile->deferred, cifs_sb->ctx->acregmax))
909 cifsFileInfo_get(cfile);
911 /* Deferred close for files */
912 queue_delayed_work(deferredclose_wq,
913 &cfile->deferred, cifs_sb->ctx->acregmax);
914 cfile->deferred_close_scheduled = true;
915 spin_unlock(&cinode->deferred_lock);
918 spin_unlock(&cinode->deferred_lock);
919 _cifsFileInfo_put(cfile, true, false);
921 _cifsFileInfo_put(cfile, true, false);
926 /* return code from the ->release op is always ignored */
931 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
933 struct cifsFileInfo *open_file;
934 struct list_head *tmp;
935 struct list_head *tmp1;
936 struct list_head tmp_list;
938 if (!tcon->use_persistent || !tcon->need_reopen_files)
941 tcon->need_reopen_files = false;
943 cifs_dbg(FYI, "Reopen persistent handles\n");
944 INIT_LIST_HEAD(&tmp_list);
946 /* list all files open on tree connection, reopen resilient handles */
947 spin_lock(&tcon->open_file_lock);
948 list_for_each(tmp, &tcon->openFileList) {
949 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
950 if (!open_file->invalidHandle)
952 cifsFileInfo_get(open_file);
953 list_add_tail(&open_file->rlist, &tmp_list);
955 spin_unlock(&tcon->open_file_lock);
957 list_for_each_safe(tmp, tmp1, &tmp_list) {
958 open_file = list_entry(tmp, struct cifsFileInfo, rlist);
959 if (cifs_reopen_file(open_file, false /* do not flush */))
960 tcon->need_reopen_files = true;
961 list_del_init(&open_file->rlist);
962 cifsFileInfo_put(open_file);
966 int cifs_closedir(struct inode *inode, struct file *file)
970 struct cifsFileInfo *cfile = file->private_data;
971 struct cifs_tcon *tcon;
972 struct TCP_Server_Info *server;
975 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
981 tcon = tlink_tcon(cfile->tlink);
982 server = tcon->ses->server;
984 cifs_dbg(FYI, "Freeing private data in close dir\n");
985 spin_lock(&cfile->file_info_lock);
986 if (server->ops->dir_needs_close(cfile)) {
987 cfile->invalidHandle = true;
988 spin_unlock(&cfile->file_info_lock);
989 if (server->ops->close_dir)
990 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
993 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
994 /* not much we can do if it fails anyway, ignore rc */
997 spin_unlock(&cfile->file_info_lock);
999 buf = cfile->srch_inf.ntwrk_buf_start;
1001 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1002 cfile->srch_inf.ntwrk_buf_start = NULL;
1003 if (cfile->srch_inf.smallBuf)
1004 cifs_small_buf_release(buf);
1006 cifs_buf_release(buf);
1009 cifs_put_tlink(cfile->tlink);
1010 kfree(file->private_data);
1011 file->private_data = NULL;
1012 /* BB can we lock the filestruct while this is going on? */
1017 static struct cifsLockInfo *
1018 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1020 struct cifsLockInfo *lock =
1021 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
1024 lock->offset = offset;
1025 lock->length = length;
1027 lock->pid = current->tgid;
1028 lock->flags = flags;
1029 INIT_LIST_HEAD(&lock->blist);
1030 init_waitqueue_head(&lock->block_q);
1035 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1037 struct cifsLockInfo *li, *tmp;
1038 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1039 list_del_init(&li->blist);
1040 wake_up(&li->block_q);
1044 #define CIFS_LOCK_OP 0
1045 #define CIFS_READ_OP 1
1046 #define CIFS_WRITE_OP 2
1048 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1050 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1051 __u64 length, __u8 type, __u16 flags,
1052 struct cifsFileInfo *cfile,
1053 struct cifsLockInfo **conf_lock, int rw_check)
1055 struct cifsLockInfo *li;
1056 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1057 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1059 list_for_each_entry(li, &fdlocks->locks, llist) {
1060 if (offset + length <= li->offset ||
1061 offset >= li->offset + li->length)
1063 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1064 server->ops->compare_fids(cfile, cur_cfile)) {
1065 /* shared lock prevents write op through the same fid */
1066 if (!(li->type & server->vals->shared_lock_type) ||
1067 rw_check != CIFS_WRITE_OP)
1070 if ((type & server->vals->shared_lock_type) &&
1071 ((server->ops->compare_fids(cfile, cur_cfile) &&
1072 current->tgid == li->pid) || type == li->type))
1074 if (rw_check == CIFS_LOCK_OP &&
1075 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1076 server->ops->compare_fids(cfile, cur_cfile))
1086 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1087 __u8 type, __u16 flags,
1088 struct cifsLockInfo **conf_lock, int rw_check)
1091 struct cifs_fid_locks *cur;
1092 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1094 list_for_each_entry(cur, &cinode->llist, llist) {
1095 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1096 flags, cfile, conf_lock,
1106 * Check if there is another lock that prevents us to set the lock (mandatory
1107 * style). If such a lock exists, update the flock structure with its
1108 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1109 * or leave it the same if we can't. Returns 0 if we don't need to request to
1110 * the server or 1 otherwise.
1113 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1114 __u8 type, struct file_lock *flock)
1117 struct cifsLockInfo *conf_lock;
1118 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1119 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1122 down_read(&cinode->lock_sem);
1124 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1125 flock->fl_flags, &conf_lock,
1128 flock->fl_start = conf_lock->offset;
1129 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1130 flock->fl_pid = conf_lock->pid;
1131 if (conf_lock->type & server->vals->shared_lock_type)
1132 flock->fl_type = F_RDLCK;
1134 flock->fl_type = F_WRLCK;
1135 } else if (!cinode->can_cache_brlcks)
1138 flock->fl_type = F_UNLCK;
1140 up_read(&cinode->lock_sem);
1145 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1147 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1148 cifs_down_write(&cinode->lock_sem);
1149 list_add_tail(&lock->llist, &cfile->llist->locks);
1150 up_write(&cinode->lock_sem);
1154 * Set the byte-range lock (mandatory style). Returns:
1155 * 1) 0, if we set the lock and don't need to request to the server;
1156 * 2) 1, if no locks prevent us but we need to request to the server;
1157 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1160 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1163 struct cifsLockInfo *conf_lock;
1164 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1170 cifs_down_write(&cinode->lock_sem);
1172 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1173 lock->type, lock->flags, &conf_lock,
1175 if (!exist && cinode->can_cache_brlcks) {
1176 list_add_tail(&lock->llist, &cfile->llist->locks);
1177 up_write(&cinode->lock_sem);
1186 list_add_tail(&lock->blist, &conf_lock->blist);
1187 up_write(&cinode->lock_sem);
1188 rc = wait_event_interruptible(lock->block_q,
1189 (lock->blist.prev == &lock->blist) &&
1190 (lock->blist.next == &lock->blist));
1193 cifs_down_write(&cinode->lock_sem);
1194 list_del_init(&lock->blist);
1197 up_write(&cinode->lock_sem);
1202 * Check if there is another lock that prevents us to set the lock (posix
1203 * style). If such a lock exists, update the flock structure with its
1204 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1205 * or leave it the same if we can't. Returns 0 if we don't need to request to
1206 * the server or 1 otherwise.
1209 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1212 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1213 unsigned char saved_type = flock->fl_type;
1215 if ((flock->fl_flags & FL_POSIX) == 0)
1218 down_read(&cinode->lock_sem);
1219 posix_test_lock(file, flock);
1221 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1222 flock->fl_type = saved_type;
1226 up_read(&cinode->lock_sem);
1231 * Set the byte-range lock (posix style). Returns:
1232 * 1) <0, if the error occurs while setting the lock;
1233 * 2) 0, if we set the lock and don't need to request to the server;
1234 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1235 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1238 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1240 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1241 int rc = FILE_LOCK_DEFERRED + 1;
1243 if ((flock->fl_flags & FL_POSIX) == 0)
1246 cifs_down_write(&cinode->lock_sem);
1247 if (!cinode->can_cache_brlcks) {
1248 up_write(&cinode->lock_sem);
1252 rc = posix_lock_file(file, flock, NULL);
1253 up_write(&cinode->lock_sem);
1258 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1261 int rc = 0, stored_rc;
1262 struct cifsLockInfo *li, *tmp;
1263 struct cifs_tcon *tcon;
1264 unsigned int num, max_num, max_buf;
1265 LOCKING_ANDX_RANGE *buf, *cur;
1266 static const int types[] = {
1267 LOCKING_ANDX_LARGE_FILES,
1268 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1273 tcon = tlink_tcon(cfile->tlink);
1276 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1277 * and check it before using.
1279 max_buf = tcon->ses->server->maxBuf;
1280 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1285 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1287 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1289 max_num = (max_buf - sizeof(struct smb_hdr)) /
1290 sizeof(LOCKING_ANDX_RANGE);
1291 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1297 for (i = 0; i < 2; i++) {
1300 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1301 if (li->type != types[i])
1303 cur->Pid = cpu_to_le16(li->pid);
1304 cur->LengthLow = cpu_to_le32((u32)li->length);
1305 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1306 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1307 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1308 if (++num == max_num) {
1309 stored_rc = cifs_lockv(xid, tcon,
1311 (__u8)li->type, 0, num,
1322 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1323 (__u8)types[i], 0, num, buf);
1335 hash_lockowner(fl_owner_t owner)
1337 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1340 struct lock_to_push {
1341 struct list_head llist;
1350 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1352 struct inode *inode = d_inode(cfile->dentry);
1353 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1354 struct file_lock *flock;
1355 struct file_lock_context *flctx = inode->i_flctx;
1356 unsigned int count = 0, i;
1357 int rc = 0, xid, type;
1358 struct list_head locks_to_send, *el;
1359 struct lock_to_push *lck, *tmp;
1367 spin_lock(&flctx->flc_lock);
1368 list_for_each(el, &flctx->flc_posix) {
1371 spin_unlock(&flctx->flc_lock);
1373 INIT_LIST_HEAD(&locks_to_send);
1376 * Allocating count locks is enough because no FL_POSIX locks can be
1377 * added to the list while we are holding cinode->lock_sem that
1378 * protects locking operations of this inode.
1380 for (i = 0; i < count; i++) {
1381 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1386 list_add_tail(&lck->llist, &locks_to_send);
1389 el = locks_to_send.next;
1390 spin_lock(&flctx->flc_lock);
1391 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1392 if (el == &locks_to_send) {
1394 * The list ended. We don't have enough allocated
1395 * structures - something is really wrong.
1397 cifs_dbg(VFS, "Can't push all brlocks!\n");
1400 length = 1 + flock->fl_end - flock->fl_start;
1401 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1405 lck = list_entry(el, struct lock_to_push, llist);
1406 lck->pid = hash_lockowner(flock->fl_owner);
1407 lck->netfid = cfile->fid.netfid;
1408 lck->length = length;
1410 lck->offset = flock->fl_start;
1412 spin_unlock(&flctx->flc_lock);
1414 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1417 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1418 lck->offset, lck->length, NULL,
1422 list_del(&lck->llist);
1430 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1431 list_del(&lck->llist);
1438 cifs_push_locks(struct cifsFileInfo *cfile)
1440 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1441 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1442 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1445 /* we are going to update can_cache_brlcks here - need a write access */
1446 cifs_down_write(&cinode->lock_sem);
1447 if (!cinode->can_cache_brlcks) {
1448 up_write(&cinode->lock_sem);
1452 if (cap_unix(tcon->ses) &&
1453 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1454 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1455 rc = cifs_push_posix_locks(cfile);
1457 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1459 cinode->can_cache_brlcks = false;
1460 up_write(&cinode->lock_sem);
1465 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1466 bool *wait_flag, struct TCP_Server_Info *server)
1468 if (flock->fl_flags & FL_POSIX)
1469 cifs_dbg(FYI, "Posix\n");
1470 if (flock->fl_flags & FL_FLOCK)
1471 cifs_dbg(FYI, "Flock\n");
1472 if (flock->fl_flags & FL_SLEEP) {
1473 cifs_dbg(FYI, "Blocking lock\n");
1476 if (flock->fl_flags & FL_ACCESS)
1477 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1478 if (flock->fl_flags & FL_LEASE)
1479 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1480 if (flock->fl_flags &
1481 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1482 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1483 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1485 *type = server->vals->large_lock_type;
1486 if (flock->fl_type == F_WRLCK) {
1487 cifs_dbg(FYI, "F_WRLCK\n");
1488 *type |= server->vals->exclusive_lock_type;
1490 } else if (flock->fl_type == F_UNLCK) {
1491 cifs_dbg(FYI, "F_UNLCK\n");
1492 *type |= server->vals->unlock_lock_type;
1494 /* Check if unlock includes more than one lock range */
1495 } else if (flock->fl_type == F_RDLCK) {
1496 cifs_dbg(FYI, "F_RDLCK\n");
1497 *type |= server->vals->shared_lock_type;
1499 } else if (flock->fl_type == F_EXLCK) {
1500 cifs_dbg(FYI, "F_EXLCK\n");
1501 *type |= server->vals->exclusive_lock_type;
1503 } else if (flock->fl_type == F_SHLCK) {
1504 cifs_dbg(FYI, "F_SHLCK\n");
1505 *type |= server->vals->shared_lock_type;
1508 cifs_dbg(FYI, "Unknown type of lock\n");
1512 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1513 bool wait_flag, bool posix_lck, unsigned int xid)
1516 __u64 length = 1 + flock->fl_end - flock->fl_start;
1517 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1518 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1519 struct TCP_Server_Info *server = tcon->ses->server;
1520 __u16 netfid = cfile->fid.netfid;
1523 int posix_lock_type;
1525 rc = cifs_posix_lock_test(file, flock);
1529 if (type & server->vals->shared_lock_type)
1530 posix_lock_type = CIFS_RDLCK;
1532 posix_lock_type = CIFS_WRLCK;
1533 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1534 hash_lockowner(flock->fl_owner),
1535 flock->fl_start, length, flock,
1536 posix_lock_type, wait_flag);
1540 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1544 /* BB we could chain these into one lock request BB */
1545 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1548 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1550 flock->fl_type = F_UNLCK;
1552 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1557 if (type & server->vals->shared_lock_type) {
1558 flock->fl_type = F_WRLCK;
1562 type &= ~server->vals->exclusive_lock_type;
1564 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1565 type | server->vals->shared_lock_type,
1568 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1569 type | server->vals->shared_lock_type, 0, 1, false);
1570 flock->fl_type = F_RDLCK;
1572 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1575 flock->fl_type = F_WRLCK;
1581 cifs_move_llist(struct list_head *source, struct list_head *dest)
1583 struct list_head *li, *tmp;
1584 list_for_each_safe(li, tmp, source)
1585 list_move(li, dest);
1589 cifs_free_llist(struct list_head *llist)
1591 struct cifsLockInfo *li, *tmp;
1592 list_for_each_entry_safe(li, tmp, llist, llist) {
1593 cifs_del_lock_waiters(li);
1594 list_del(&li->llist);
1600 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1603 int rc = 0, stored_rc;
1604 static const int types[] = {
1605 LOCKING_ANDX_LARGE_FILES,
1606 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1609 unsigned int max_num, num, max_buf;
1610 LOCKING_ANDX_RANGE *buf, *cur;
1611 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1612 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1613 struct cifsLockInfo *li, *tmp;
1614 __u64 length = 1 + flock->fl_end - flock->fl_start;
1615 struct list_head tmp_llist;
1617 INIT_LIST_HEAD(&tmp_llist);
1620 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1621 * and check it before using.
1623 max_buf = tcon->ses->server->maxBuf;
1624 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1627 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1629 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1631 max_num = (max_buf - sizeof(struct smb_hdr)) /
1632 sizeof(LOCKING_ANDX_RANGE);
1633 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1637 cifs_down_write(&cinode->lock_sem);
1638 for (i = 0; i < 2; i++) {
1641 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1642 if (flock->fl_start > li->offset ||
1643 (flock->fl_start + length) <
1644 (li->offset + li->length))
1646 if (current->tgid != li->pid)
1648 if (types[i] != li->type)
1650 if (cinode->can_cache_brlcks) {
1652 * We can cache brlock requests - simply remove
1653 * a lock from the file's list.
1655 list_del(&li->llist);
1656 cifs_del_lock_waiters(li);
1660 cur->Pid = cpu_to_le16(li->pid);
1661 cur->LengthLow = cpu_to_le32((u32)li->length);
1662 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1663 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1664 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1666 * We need to save a lock here to let us add it again to
1667 * the file's list if the unlock range request fails on
1670 list_move(&li->llist, &tmp_llist);
1671 if (++num == max_num) {
1672 stored_rc = cifs_lockv(xid, tcon,
1674 li->type, num, 0, buf);
1677 * We failed on the unlock range
1678 * request - add all locks from the tmp
1679 * list to the head of the file's list.
1681 cifs_move_llist(&tmp_llist,
1682 &cfile->llist->locks);
1686 * The unlock range request succeed -
1687 * free the tmp list.
1689 cifs_free_llist(&tmp_llist);
1696 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1697 types[i], num, 0, buf);
1699 cifs_move_llist(&tmp_llist,
1700 &cfile->llist->locks);
1703 cifs_free_llist(&tmp_llist);
1707 up_write(&cinode->lock_sem);
1713 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1714 bool wait_flag, bool posix_lck, int lock, int unlock,
1718 __u64 length = 1 + flock->fl_end - flock->fl_start;
1719 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1720 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1721 struct TCP_Server_Info *server = tcon->ses->server;
1722 struct inode *inode = d_inode(cfile->dentry);
1725 int posix_lock_type;
1727 rc = cifs_posix_lock_set(file, flock);
1728 if (rc <= FILE_LOCK_DEFERRED)
1731 if (type & server->vals->shared_lock_type)
1732 posix_lock_type = CIFS_RDLCK;
1734 posix_lock_type = CIFS_WRLCK;
1737 posix_lock_type = CIFS_UNLCK;
1739 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1740 hash_lockowner(flock->fl_owner),
1741 flock->fl_start, length,
1742 NULL, posix_lock_type, wait_flag);
1747 struct cifsLockInfo *lock;
1749 lock = cifs_lock_init(flock->fl_start, length, type,
1754 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1763 * Windows 7 server can delay breaking lease from read to None
1764 * if we set a byte-range lock on a file - break it explicitly
1765 * before sending the lock to the server to be sure the next
1766 * read won't conflict with non-overlapted locks due to
1769 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1770 CIFS_CACHE_READ(CIFS_I(inode))) {
1771 cifs_zap_mapping(inode);
1772 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1774 CIFS_I(inode)->oplock = 0;
1777 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1778 type, 1, 0, wait_flag);
1784 cifs_lock_add(cfile, lock);
1786 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1789 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1791 * If this is a request to remove all locks because we
1792 * are closing the file, it doesn't matter if the
1793 * unlocking failed as both cifs.ko and the SMB server
1794 * remove the lock on file close
1797 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1798 if (!(flock->fl_flags & FL_CLOSE))
1801 rc = locks_lock_file_wait(file, flock);
1806 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1809 int lock = 0, unlock = 0;
1810 bool wait_flag = false;
1811 bool posix_lck = false;
1812 struct cifs_sb_info *cifs_sb;
1813 struct cifs_tcon *tcon;
1814 struct cifsFileInfo *cfile;
1820 if (!(fl->fl_flags & FL_FLOCK))
1823 cfile = (struct cifsFileInfo *)file->private_data;
1824 tcon = tlink_tcon(cfile->tlink);
1826 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
1828 cifs_sb = CIFS_FILE_SB(file);
1830 if (cap_unix(tcon->ses) &&
1831 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1832 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1835 if (!lock && !unlock) {
1837 * if no lock or unlock then nothing to do since we do not
1844 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1852 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1855 int lock = 0, unlock = 0;
1856 bool wait_flag = false;
1857 bool posix_lck = false;
1858 struct cifs_sb_info *cifs_sb;
1859 struct cifs_tcon *tcon;
1860 struct cifsFileInfo *cfile;
1866 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1867 cmd, flock->fl_flags, flock->fl_type,
1868 flock->fl_start, flock->fl_end);
1870 cfile = (struct cifsFileInfo *)file->private_data;
1871 tcon = tlink_tcon(cfile->tlink);
1873 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1875 cifs_sb = CIFS_FILE_SB(file);
1877 if (cap_unix(tcon->ses) &&
1878 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1879 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1882 * BB add code here to normalize offset and length to account for
1883 * negative length which we can not accept over the wire.
1885 if (IS_GETLK(cmd)) {
1886 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1891 if (!lock && !unlock) {
1893 * if no lock or unlock then nothing to do since we do not
1900 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1907 * update the file size (if needed) after a write. Should be called with
1908 * the inode->i_lock held
1911 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1912 unsigned int bytes_written)
1914 loff_t end_of_write = offset + bytes_written;
1916 if (end_of_write > cifsi->server_eof)
1917 cifsi->server_eof = end_of_write;
1921 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1922 size_t write_size, loff_t *offset)
1925 unsigned int bytes_written = 0;
1926 unsigned int total_written;
1927 struct cifs_tcon *tcon;
1928 struct TCP_Server_Info *server;
1930 struct dentry *dentry = open_file->dentry;
1931 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
1932 struct cifs_io_parms io_parms = {0};
1934 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1935 write_size, *offset, dentry);
1937 tcon = tlink_tcon(open_file->tlink);
1938 server = tcon->ses->server;
1940 if (!server->ops->sync_write)
1945 for (total_written = 0; write_size > total_written;
1946 total_written += bytes_written) {
1948 while (rc == -EAGAIN) {
1952 if (open_file->invalidHandle) {
1953 /* we could deadlock if we called
1954 filemap_fdatawait from here so tell
1955 reopen_file not to flush data to
1957 rc = cifs_reopen_file(open_file, false);
1962 len = min(server->ops->wp_retry_size(d_inode(dentry)),
1963 (unsigned int)write_size - total_written);
1964 /* iov[0] is reserved for smb header */
1965 iov[1].iov_base = (char *)write_data + total_written;
1966 iov[1].iov_len = len;
1968 io_parms.tcon = tcon;
1969 io_parms.offset = *offset;
1970 io_parms.length = len;
1971 rc = server->ops->sync_write(xid, &open_file->fid,
1972 &io_parms, &bytes_written, iov, 1);
1974 if (rc || (bytes_written == 0)) {
1982 spin_lock(&d_inode(dentry)->i_lock);
1983 cifs_update_eof(cifsi, *offset, bytes_written);
1984 spin_unlock(&d_inode(dentry)->i_lock);
1985 *offset += bytes_written;
1989 cifs_stats_bytes_written(tcon, total_written);
1991 if (total_written > 0) {
1992 spin_lock(&d_inode(dentry)->i_lock);
1993 if (*offset > d_inode(dentry)->i_size) {
1994 i_size_write(d_inode(dentry), *offset);
1995 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
1997 spin_unlock(&d_inode(dentry)->i_lock);
1999 mark_inode_dirty_sync(d_inode(dentry));
2001 return total_written;
2004 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
2007 struct cifsFileInfo *open_file = NULL;
2008 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
2010 /* only filter by fsuid on multiuser mounts */
2011 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2014 spin_lock(&cifs_inode->open_file_lock);
2015 /* we could simply get the first_list_entry since write-only entries
2016 are always at the end of the list but since the first entry might
2017 have a close pending, we go through the whole list */
2018 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2019 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2021 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2022 if ((!open_file->invalidHandle)) {
2023 /* found a good file */
2024 /* lock it so it will not be closed on us */
2025 cifsFileInfo_get(open_file);
2026 spin_unlock(&cifs_inode->open_file_lock);
2028 } /* else might as well continue, and look for
2029 another, or simply have the caller reopen it
2030 again rather than trying to fix this handle */
2031 } else /* write only file */
2032 break; /* write only files are last so must be done */
2034 spin_unlock(&cifs_inode->open_file_lock);
2038 /* Return -EBADF if no handle is found and general rc otherwise */
2040 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
2041 struct cifsFileInfo **ret_file)
2043 struct cifsFileInfo *open_file, *inv_file = NULL;
2044 struct cifs_sb_info *cifs_sb;
2045 bool any_available = false;
2047 unsigned int refind = 0;
2048 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
2049 bool with_delete = flags & FIND_WR_WITH_DELETE;
2053 * Having a null inode here (because mapping->host was set to zero by
2054 * the VFS or MM) should not happen but we had reports of on oops (due
2055 * to it being zero) during stress testcases so we need to check for it
2058 if (cifs_inode == NULL) {
2059 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2064 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
2066 /* only filter by fsuid on multiuser mounts */
2067 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2070 spin_lock(&cifs_inode->open_file_lock);
2072 if (refind > MAX_REOPEN_ATT) {
2073 spin_unlock(&cifs_inode->open_file_lock);
2076 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2077 if (!any_available && open_file->pid != current->tgid)
2079 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2081 if (with_delete && !(open_file->fid.access & DELETE))
2083 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2084 if (!open_file->invalidHandle) {
2085 /* found a good writable file */
2086 cifsFileInfo_get(open_file);
2087 spin_unlock(&cifs_inode->open_file_lock);
2088 *ret_file = open_file;
2092 inv_file = open_file;
2096 /* couldn't find useable FH with same pid, try any available */
2097 if (!any_available) {
2098 any_available = true;
2099 goto refind_writable;
2103 any_available = false;
2104 cifsFileInfo_get(inv_file);
2107 spin_unlock(&cifs_inode->open_file_lock);
2110 rc = cifs_reopen_file(inv_file, false);
2112 *ret_file = inv_file;
2116 spin_lock(&cifs_inode->open_file_lock);
2117 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2118 spin_unlock(&cifs_inode->open_file_lock);
2119 cifsFileInfo_put(inv_file);
2122 spin_lock(&cifs_inode->open_file_lock);
2123 goto refind_writable;
2129 struct cifsFileInfo *
2130 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2132 struct cifsFileInfo *cfile;
2135 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2137 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2143 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2145 struct cifsFileInfo **ret_file)
2147 struct cifsFileInfo *cfile;
2148 void *page = alloc_dentry_path();
2152 spin_lock(&tcon->open_file_lock);
2153 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2154 struct cifsInodeInfo *cinode;
2155 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2156 if (IS_ERR(full_path)) {
2157 spin_unlock(&tcon->open_file_lock);
2158 free_dentry_path(page);
2159 return PTR_ERR(full_path);
2161 if (strcmp(full_path, name))
2164 cinode = CIFS_I(d_inode(cfile->dentry));
2165 spin_unlock(&tcon->open_file_lock);
2166 free_dentry_path(page);
2167 return cifs_get_writable_file(cinode, flags, ret_file);
2170 spin_unlock(&tcon->open_file_lock);
2171 free_dentry_path(page);
2176 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2177 struct cifsFileInfo **ret_file)
2179 struct cifsFileInfo *cfile;
2180 void *page = alloc_dentry_path();
2184 spin_lock(&tcon->open_file_lock);
2185 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2186 struct cifsInodeInfo *cinode;
2187 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2188 if (IS_ERR(full_path)) {
2189 spin_unlock(&tcon->open_file_lock);
2190 free_dentry_path(page);
2191 return PTR_ERR(full_path);
2193 if (strcmp(full_path, name))
2196 cinode = CIFS_I(d_inode(cfile->dentry));
2197 spin_unlock(&tcon->open_file_lock);
2198 free_dentry_path(page);
2199 *ret_file = find_readable_file(cinode, 0);
2200 return *ret_file ? 0 : -ENOENT;
2203 spin_unlock(&tcon->open_file_lock);
2204 free_dentry_path(page);
2208 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2210 struct address_space *mapping = page->mapping;
2211 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2214 int bytes_written = 0;
2215 struct inode *inode;
2216 struct cifsFileInfo *open_file;
2218 if (!mapping || !mapping->host)
2221 inode = page->mapping->host;
2223 offset += (loff_t)from;
2224 write_data = kmap(page);
2227 if ((to > PAGE_SIZE) || (from > to)) {
2232 /* racing with truncate? */
2233 if (offset > mapping->host->i_size) {
2235 return 0; /* don't care */
2238 /* check to make sure that we are not extending the file */
2239 if (mapping->host->i_size - offset < (loff_t)to)
2240 to = (unsigned)(mapping->host->i_size - offset);
2242 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2245 bytes_written = cifs_write(open_file, open_file->pid,
2246 write_data, to - from, &offset);
2247 cifsFileInfo_put(open_file);
2248 /* Does mm or vfs already set times? */
2249 inode->i_atime = inode->i_mtime = current_time(inode);
2250 if ((bytes_written > 0) && (offset))
2252 else if (bytes_written < 0)
2257 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2258 if (!is_retryable_error(rc))
2266 static struct cifs_writedata *
2267 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2268 pgoff_t end, pgoff_t *index,
2269 unsigned int *found_pages)
2271 struct cifs_writedata *wdata;
2273 wdata = cifs_writedata_alloc((unsigned int)tofind,
2274 cifs_writev_complete);
2278 *found_pages = find_get_pages_range_tag(mapping, index, end,
2279 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2284 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2285 struct address_space *mapping,
2286 struct writeback_control *wbc,
2287 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2289 unsigned int nr_pages = 0, i;
2292 for (i = 0; i < found_pages; i++) {
2293 page = wdata->pages[i];
2295 * At this point we hold neither the i_pages lock nor the
2296 * page lock: the page may be truncated or invalidated
2297 * (changing page->mapping to NULL), or even swizzled
2298 * back from swapper_space to tmpfs file mapping
2303 else if (!trylock_page(page))
2306 if (unlikely(page->mapping != mapping)) {
2311 if (!wbc->range_cyclic && page->index > end) {
2317 if (*next && (page->index != *next)) {
2318 /* Not next consecutive page */
2323 if (wbc->sync_mode != WB_SYNC_NONE)
2324 wait_on_page_writeback(page);
2326 if (PageWriteback(page) ||
2327 !clear_page_dirty_for_io(page)) {
2333 * This actually clears the dirty bit in the radix tree.
2334 * See cifs_writepage() for more commentary.
2336 set_page_writeback(page);
2337 if (page_offset(page) >= i_size_read(mapping->host)) {
2340 end_page_writeback(page);
2344 wdata->pages[i] = page;
2345 *next = page->index + 1;
2349 /* reset index to refind any pages skipped */
2351 *index = wdata->pages[0]->index + 1;
2353 /* put any pages we aren't going to use */
2354 for (i = nr_pages; i < found_pages; i++) {
2355 put_page(wdata->pages[i]);
2356 wdata->pages[i] = NULL;
2363 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2364 struct address_space *mapping, struct writeback_control *wbc)
2368 wdata->sync_mode = wbc->sync_mode;
2369 wdata->nr_pages = nr_pages;
2370 wdata->offset = page_offset(wdata->pages[0]);
2371 wdata->pagesz = PAGE_SIZE;
2372 wdata->tailsz = min(i_size_read(mapping->host) -
2373 page_offset(wdata->pages[nr_pages - 1]),
2375 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2376 wdata->pid = wdata->cfile->pid;
2378 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2382 if (wdata->cfile->invalidHandle)
2385 rc = wdata->server->ops->async_writev(wdata,
2386 cifs_writedata_release);
2391 static int cifs_writepages(struct address_space *mapping,
2392 struct writeback_control *wbc)
2394 struct inode *inode = mapping->host;
2395 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2396 struct TCP_Server_Info *server;
2397 bool done = false, scanned = false, range_whole = false;
2399 struct cifs_writedata *wdata;
2400 struct cifsFileInfo *cfile = NULL;
2406 * If wsize is smaller than the page cache size, default to writing
2407 * one page at a time via cifs_writepage
2409 if (cifs_sb->ctx->wsize < PAGE_SIZE)
2410 return generic_writepages(mapping, wbc);
2413 if (wbc->range_cyclic) {
2414 index = mapping->writeback_index; /* Start from prev offset */
2417 index = wbc->range_start >> PAGE_SHIFT;
2418 end = wbc->range_end >> PAGE_SHIFT;
2419 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2423 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2426 while (!done && index <= end) {
2427 unsigned int i, nr_pages, found_pages, wsize;
2428 pgoff_t next = 0, tofind, saved_index = index;
2429 struct cifs_credits credits_on_stack;
2430 struct cifs_credits *credits = &credits_on_stack;
2431 int get_file_rc = 0;
2434 cifsFileInfo_put(cfile);
2436 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2438 /* in case of an error store it to return later */
2442 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2449 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2451 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2456 add_credits_and_wake_if(server, credits, 0);
2460 if (found_pages == 0) {
2461 kref_put(&wdata->refcount, cifs_writedata_release);
2462 add_credits_and_wake_if(server, credits, 0);
2466 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2467 end, &index, &next, &done);
2469 /* nothing to write? */
2470 if (nr_pages == 0) {
2471 kref_put(&wdata->refcount, cifs_writedata_release);
2472 add_credits_and_wake_if(server, credits, 0);
2476 wdata->credits = credits_on_stack;
2477 wdata->cfile = cfile;
2478 wdata->server = server;
2481 if (!wdata->cfile) {
2482 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2484 if (is_retryable_error(get_file_rc))
2489 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2491 for (i = 0; i < nr_pages; ++i)
2492 unlock_page(wdata->pages[i]);
2494 /* send failure -- clean up the mess */
2496 add_credits_and_wake_if(server, &wdata->credits, 0);
2497 for (i = 0; i < nr_pages; ++i) {
2498 if (is_retryable_error(rc))
2499 redirty_page_for_writepage(wbc,
2502 SetPageError(wdata->pages[i]);
2503 end_page_writeback(wdata->pages[i]);
2504 put_page(wdata->pages[i]);
2506 if (!is_retryable_error(rc))
2507 mapping_set_error(mapping, rc);
2509 kref_put(&wdata->refcount, cifs_writedata_release);
2511 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2512 index = saved_index;
2516 /* Return immediately if we received a signal during writing */
2517 if (is_interrupt_error(rc)) {
2522 if (rc != 0 && saved_rc == 0)
2525 wbc->nr_to_write -= nr_pages;
2526 if (wbc->nr_to_write <= 0)
2532 if (!scanned && !done) {
2534 * We hit the last page and there is more work to be done: wrap
2535 * back to the start of the file
2545 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2546 mapping->writeback_index = index;
2549 cifsFileInfo_put(cfile);
2551 /* Indication to update ctime and mtime as close is deferred */
2552 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2557 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2563 /* BB add check for wbc flags */
2565 if (!PageUptodate(page))
2566 cifs_dbg(FYI, "ppw - page not up to date\n");
2569 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2571 * A writepage() implementation always needs to do either this,
2572 * or re-dirty the page with "redirty_page_for_writepage()" in
2573 * the case of a failure.
2575 * Just unlocking the page will cause the radix tree tag-bits
2576 * to fail to update with the state of the page correctly.
2578 set_page_writeback(page);
2580 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2581 if (is_retryable_error(rc)) {
2582 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2584 redirty_page_for_writepage(wbc, page);
2585 } else if (rc != 0) {
2587 mapping_set_error(page->mapping, rc);
2589 SetPageUptodate(page);
2591 end_page_writeback(page);
2597 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2599 int rc = cifs_writepage_locked(page, wbc);
2604 static int cifs_write_end(struct file *file, struct address_space *mapping,
2605 loff_t pos, unsigned len, unsigned copied,
2606 struct page *page, void *fsdata)
2609 struct inode *inode = mapping->host;
2610 struct cifsFileInfo *cfile = file->private_data;
2611 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2614 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2617 pid = current->tgid;
2619 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2622 if (PageChecked(page)) {
2624 SetPageUptodate(page);
2625 ClearPageChecked(page);
2626 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2627 SetPageUptodate(page);
2629 if (!PageUptodate(page)) {
2631 unsigned offset = pos & (PAGE_SIZE - 1);
2635 /* this is probably better than directly calling
2636 partialpage_write since in this function the file handle is
2637 known which we might as well leverage */
2638 /* BB check if anything else missing out of ppw
2639 such as updating last write time */
2640 page_data = kmap(page);
2641 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2642 /* if (rc < 0) should we set writebehind rc? */
2649 set_page_dirty(page);
2653 spin_lock(&inode->i_lock);
2654 if (pos > inode->i_size) {
2655 i_size_write(inode, pos);
2656 inode->i_blocks = (512 - 1 + pos) >> 9;
2658 spin_unlock(&inode->i_lock);
2663 /* Indication to update ctime and mtime as close is deferred */
2664 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2669 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2674 struct cifs_tcon *tcon;
2675 struct TCP_Server_Info *server;
2676 struct cifsFileInfo *smbfile = file->private_data;
2677 struct inode *inode = file_inode(file);
2678 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2680 rc = file_write_and_wait_range(file, start, end);
2682 trace_cifs_fsync_err(inode->i_ino, rc);
2688 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2691 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2692 rc = cifs_zap_mapping(inode);
2694 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2695 rc = 0; /* don't care about it in fsync */
2699 tcon = tlink_tcon(smbfile->tlink);
2700 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2701 server = tcon->ses->server;
2702 if (server->ops->flush)
2703 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2712 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2716 struct cifs_tcon *tcon;
2717 struct TCP_Server_Info *server;
2718 struct cifsFileInfo *smbfile = file->private_data;
2719 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2721 rc = file_write_and_wait_range(file, start, end);
2723 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
2729 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2732 tcon = tlink_tcon(smbfile->tlink);
2733 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2734 server = tcon->ses->server;
2735 if (server->ops->flush)
2736 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2746 * As file closes, flush all cached write data for this inode checking
2747 * for write behind errors.
2749 int cifs_flush(struct file *file, fl_owner_t id)
2751 struct inode *inode = file_inode(file);
2754 if (file->f_mode & FMODE_WRITE)
2755 rc = filemap_write_and_wait(inode->i_mapping);
2757 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2759 trace_cifs_flush_err(inode->i_ino, rc);
2764 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2769 for (i = 0; i < num_pages; i++) {
2770 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2773 * save number of pages we have already allocated and
2774 * return with ENOMEM error
2783 for (i = 0; i < num_pages; i++)
2790 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2795 clen = min_t(const size_t, len, wsize);
2796 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
2805 cifs_uncached_writedata_release(struct kref *refcount)
2808 struct cifs_writedata *wdata = container_of(refcount,
2809 struct cifs_writedata, refcount);
2811 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
2812 for (i = 0; i < wdata->nr_pages; i++)
2813 put_page(wdata->pages[i]);
2814 cifs_writedata_release(refcount);
2817 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2820 cifs_uncached_writev_complete(struct work_struct *work)
2822 struct cifs_writedata *wdata = container_of(work,
2823 struct cifs_writedata, work);
2824 struct inode *inode = d_inode(wdata->cfile->dentry);
2825 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2827 spin_lock(&inode->i_lock);
2828 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2829 if (cifsi->server_eof > inode->i_size)
2830 i_size_write(inode, cifsi->server_eof);
2831 spin_unlock(&inode->i_lock);
2833 complete(&wdata->done);
2834 collect_uncached_write_data(wdata->ctx);
2835 /* the below call can possibly free the last ref to aio ctx */
2836 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2840 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2841 size_t *len, unsigned long *num_pages)
2843 size_t save_len, copied, bytes, cur_len = *len;
2844 unsigned long i, nr_pages = *num_pages;
2847 for (i = 0; i < nr_pages; i++) {
2848 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2849 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2852 * If we didn't copy as much as we expected, then that
2853 * may mean we trod into an unmapped area. Stop copying
2854 * at that point. On the next pass through the big
2855 * loop, we'll likely end up getting a zero-length
2856 * write and bailing out of it.
2861 cur_len = save_len - cur_len;
2865 * If we have no data to send, then that probably means that
2866 * the copy above failed altogether. That's most likely because
2867 * the address in the iovec was bogus. Return -EFAULT and let
2868 * the caller free anything we allocated and bail out.
2874 * i + 1 now represents the number of pages we actually used in
2875 * the copy phase above.
2882 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
2883 struct cifs_aio_ctx *ctx)
2886 struct cifs_credits credits;
2888 struct TCP_Server_Info *server = wdata->server;
2891 if (wdata->cfile->invalidHandle) {
2892 rc = cifs_reopen_file(wdata->cfile, false);
2901 * Wait for credits to resend this wdata.
2902 * Note: we are attempting to resend the whole wdata not in
2906 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
2911 if (wsize < wdata->bytes) {
2912 add_credits_and_wake_if(server, &credits, 0);
2915 } while (wsize < wdata->bytes);
2916 wdata->credits = credits;
2918 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2921 if (wdata->cfile->invalidHandle)
2924 #ifdef CONFIG_CIFS_SMB_DIRECT
2926 wdata->mr->need_invalidate = true;
2927 smbd_deregister_mr(wdata->mr);
2931 rc = server->ops->async_writev(wdata,
2932 cifs_uncached_writedata_release);
2936 /* If the write was successfully sent, we are done */
2938 list_add_tail(&wdata->list, wdata_list);
2942 /* Roll back credits and retry if needed */
2943 add_credits_and_wake_if(server, &wdata->credits, 0);
2944 } while (rc == -EAGAIN);
2947 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2952 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2953 struct cifsFileInfo *open_file,
2954 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2955 struct cifs_aio_ctx *ctx)
2959 unsigned long nr_pages, num_pages, i;
2960 struct cifs_writedata *wdata;
2961 struct iov_iter saved_from = *from;
2962 loff_t saved_offset = offset;
2964 struct TCP_Server_Info *server;
2965 struct page **pagevec;
2969 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2970 pid = open_file->pid;
2972 pid = current->tgid;
2974 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
2979 struct cifs_credits credits_on_stack;
2980 struct cifs_credits *credits = &credits_on_stack;
2982 if (open_file->invalidHandle) {
2983 rc = cifs_reopen_file(open_file, false);
2990 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2995 cur_len = min_t(const size_t, len, wsize);
2997 if (ctx->direct_io) {
3000 result = iov_iter_get_pages_alloc(
3001 from, &pagevec, cur_len, &start);
3004 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3005 result, iov_iter_type(from),
3006 from->iov_offset, from->count);
3010 add_credits_and_wake_if(server, credits, 0);
3013 cur_len = (size_t)result;
3014 iov_iter_advance(from, cur_len);
3017 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
3019 wdata = cifs_writedata_direct_alloc(pagevec,
3020 cifs_uncached_writev_complete);
3023 add_credits_and_wake_if(server, credits, 0);
3028 wdata->page_offset = start;
3031 cur_len - (PAGE_SIZE - start) -
3032 (nr_pages - 2) * PAGE_SIZE :
3035 nr_pages = get_numpages(wsize, len, &cur_len);
3036 wdata = cifs_writedata_alloc(nr_pages,
3037 cifs_uncached_writev_complete);
3040 add_credits_and_wake_if(server, credits, 0);
3044 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
3046 kvfree(wdata->pages);
3048 add_credits_and_wake_if(server, credits, 0);
3052 num_pages = nr_pages;
3053 rc = wdata_fill_from_iovec(
3054 wdata, from, &cur_len, &num_pages);
3056 for (i = 0; i < nr_pages; i++)
3057 put_page(wdata->pages[i]);
3058 kvfree(wdata->pages);
3060 add_credits_and_wake_if(server, credits, 0);
3065 * Bring nr_pages down to the number of pages we
3066 * actually used, and free any pages that we didn't use.
3068 for ( ; nr_pages > num_pages; nr_pages--)
3069 put_page(wdata->pages[nr_pages - 1]);
3071 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
3074 wdata->sync_mode = WB_SYNC_ALL;
3075 wdata->nr_pages = nr_pages;
3076 wdata->offset = (__u64)offset;
3077 wdata->cfile = cifsFileInfo_get(open_file);
3078 wdata->server = server;
3080 wdata->bytes = cur_len;
3081 wdata->pagesz = PAGE_SIZE;
3082 wdata->credits = credits_on_stack;
3084 kref_get(&ctx->refcount);
3086 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3089 if (wdata->cfile->invalidHandle)
3092 rc = server->ops->async_writev(wdata,
3093 cifs_uncached_writedata_release);
3097 add_credits_and_wake_if(server, &wdata->credits, 0);
3098 kref_put(&wdata->refcount,
3099 cifs_uncached_writedata_release);
3100 if (rc == -EAGAIN) {
3102 iov_iter_advance(from, offset - saved_offset);
3108 list_add_tail(&wdata->list, wdata_list);
3117 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3119 struct cifs_writedata *wdata, *tmp;
3120 struct cifs_tcon *tcon;
3121 struct cifs_sb_info *cifs_sb;
3122 struct dentry *dentry = ctx->cfile->dentry;
3125 tcon = tlink_tcon(ctx->cfile->tlink);
3126 cifs_sb = CIFS_SB(dentry->d_sb);
3128 mutex_lock(&ctx->aio_mutex);
3130 if (list_empty(&ctx->list)) {
3131 mutex_unlock(&ctx->aio_mutex);
3137 * Wait for and collect replies for any successful sends in order of
3138 * increasing offset. Once an error is hit, then return without waiting
3139 * for any more replies.
3142 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3144 if (!try_wait_for_completion(&wdata->done)) {
3145 mutex_unlock(&ctx->aio_mutex);
3152 ctx->total_len += wdata->bytes;
3154 /* resend call if it's a retryable error */
3155 if (rc == -EAGAIN) {
3156 struct list_head tmp_list;
3157 struct iov_iter tmp_from = ctx->iter;
3159 INIT_LIST_HEAD(&tmp_list);
3160 list_del_init(&wdata->list);
3163 rc = cifs_resend_wdata(
3164 wdata, &tmp_list, ctx);
3166 iov_iter_advance(&tmp_from,
3167 wdata->offset - ctx->pos);
3169 rc = cifs_write_from_iter(wdata->offset,
3170 wdata->bytes, &tmp_from,
3171 ctx->cfile, cifs_sb, &tmp_list,
3174 kref_put(&wdata->refcount,
3175 cifs_uncached_writedata_release);
3178 list_splice(&tmp_list, &ctx->list);
3182 list_del_init(&wdata->list);
3183 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3186 cifs_stats_bytes_written(tcon, ctx->total_len);
3187 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3189 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3191 mutex_unlock(&ctx->aio_mutex);
3193 if (ctx->iocb && ctx->iocb->ki_complete)
3194 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3196 complete(&ctx->done);
3199 static ssize_t __cifs_writev(
3200 struct kiocb *iocb, struct iov_iter *from, bool direct)
3202 struct file *file = iocb->ki_filp;
3203 ssize_t total_written = 0;
3204 struct cifsFileInfo *cfile;
3205 struct cifs_tcon *tcon;
3206 struct cifs_sb_info *cifs_sb;
3207 struct cifs_aio_ctx *ctx;
3208 struct iov_iter saved_from = *from;
3209 size_t len = iov_iter_count(from);
3213 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3214 * In this case, fall back to non-direct write function.
3215 * this could be improved by getting pages directly in ITER_KVEC
3217 if (direct && iov_iter_is_kvec(from)) {
3218 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3222 rc = generic_write_checks(iocb, from);
3226 cifs_sb = CIFS_FILE_SB(file);
3227 cfile = file->private_data;
3228 tcon = tlink_tcon(cfile->tlink);
3230 if (!tcon->ses->server->ops->async_writev)
3233 ctx = cifs_aio_ctx_alloc();
3237 ctx->cfile = cifsFileInfo_get(cfile);
3239 if (!is_sync_kiocb(iocb))
3242 ctx->pos = iocb->ki_pos;
3245 ctx->direct_io = true;
3249 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3251 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3256 /* grab a lock here due to read response handlers can access ctx */
3257 mutex_lock(&ctx->aio_mutex);
3259 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3260 cfile, cifs_sb, &ctx->list, ctx);
3263 * If at least one write was successfully sent, then discard any rc
3264 * value from the later writes. If the other write succeeds, then
3265 * we'll end up returning whatever was written. If it fails, then
3266 * we'll get a new rc value from that.
3268 if (!list_empty(&ctx->list))
3271 mutex_unlock(&ctx->aio_mutex);
3274 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3278 if (!is_sync_kiocb(iocb)) {
3279 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3280 return -EIOCBQUEUED;
3283 rc = wait_for_completion_killable(&ctx->done);
3285 mutex_lock(&ctx->aio_mutex);
3286 ctx->rc = rc = -EINTR;
3287 total_written = ctx->total_len;
3288 mutex_unlock(&ctx->aio_mutex);
3291 total_written = ctx->total_len;
3294 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3296 if (unlikely(!total_written))
3299 iocb->ki_pos += total_written;
3300 return total_written;
3303 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3305 return __cifs_writev(iocb, from, true);
3308 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3310 return __cifs_writev(iocb, from, false);
3314 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3316 struct file *file = iocb->ki_filp;
3317 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3318 struct inode *inode = file->f_mapping->host;
3319 struct cifsInodeInfo *cinode = CIFS_I(inode);
3320 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3325 * We need to hold the sem to be sure nobody modifies lock list
3326 * with a brlock that prevents writing.
3328 down_read(&cinode->lock_sem);
3330 rc = generic_write_checks(iocb, from);
3334 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3335 server->vals->exclusive_lock_type, 0,
3336 NULL, CIFS_WRITE_OP))
3337 rc = __generic_file_write_iter(iocb, from);
3341 up_read(&cinode->lock_sem);
3342 inode_unlock(inode);
3345 rc = generic_write_sync(iocb, rc);
3350 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3352 struct inode *inode = file_inode(iocb->ki_filp);
3353 struct cifsInodeInfo *cinode = CIFS_I(inode);
3354 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3355 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3356 iocb->ki_filp->private_data;
3357 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3360 written = cifs_get_writer(cinode);
3364 if (CIFS_CACHE_WRITE(cinode)) {
3365 if (cap_unix(tcon->ses) &&
3366 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3367 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3368 written = generic_file_write_iter(iocb, from);
3371 written = cifs_writev(iocb, from);
3375 * For non-oplocked files in strict cache mode we need to write the data
3376 * to the server exactly from the pos to pos+len-1 rather than flush all
3377 * affected pages because it may cause a error with mandatory locks on
3378 * these pages but not on the region from pos to ppos+len-1.
3380 written = cifs_user_writev(iocb, from);
3381 if (CIFS_CACHE_READ(cinode)) {
3383 * We have read level caching and we have just sent a write
3384 * request to the server thus making data in the cache stale.
3385 * Zap the cache and set oplock/lease level to NONE to avoid
3386 * reading stale data from the cache. All subsequent read
3387 * operations will read new data from the server.
3389 cifs_zap_mapping(inode);
3390 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3395 cifs_put_writer(cinode);
3399 static struct cifs_readdata *
3400 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3402 struct cifs_readdata *rdata;
3404 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3405 if (rdata != NULL) {
3406 rdata->pages = pages;
3407 kref_init(&rdata->refcount);
3408 INIT_LIST_HEAD(&rdata->list);
3409 init_completion(&rdata->done);
3410 INIT_WORK(&rdata->work, complete);
3416 static struct cifs_readdata *
3417 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3419 struct page **pages =
3420 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3421 struct cifs_readdata *ret = NULL;
3424 ret = cifs_readdata_direct_alloc(pages, complete);
3433 cifs_readdata_release(struct kref *refcount)
3435 struct cifs_readdata *rdata = container_of(refcount,
3436 struct cifs_readdata, refcount);
3437 #ifdef CONFIG_CIFS_SMB_DIRECT
3439 smbd_deregister_mr(rdata->mr);
3444 cifsFileInfo_put(rdata->cfile);
3446 kvfree(rdata->pages);
3451 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3457 for (i = 0; i < nr_pages; i++) {
3458 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3463 rdata->pages[i] = page;
3467 unsigned int nr_page_failed = i;
3469 for (i = 0; i < nr_page_failed; i++) {
3470 put_page(rdata->pages[i]);
3471 rdata->pages[i] = NULL;
3478 cifs_uncached_readdata_release(struct kref *refcount)
3480 struct cifs_readdata *rdata = container_of(refcount,
3481 struct cifs_readdata, refcount);
3484 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3485 for (i = 0; i < rdata->nr_pages; i++) {
3486 put_page(rdata->pages[i]);
3488 cifs_readdata_release(refcount);
3492 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3493 * @rdata: the readdata response with list of pages holding data
3494 * @iter: destination for our data
3496 * This function copies data from a list of pages in a readdata response into
3497 * an array of iovecs. It will first calculate where the data should go
3498 * based on the info in the readdata and then copy the data into that spot.
3501 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3503 size_t remaining = rdata->got_bytes;
3506 for (i = 0; i < rdata->nr_pages; i++) {
3507 struct page *page = rdata->pages[i];
3508 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3511 if (unlikely(iov_iter_is_pipe(iter))) {
3512 void *addr = kmap_atomic(page);
3514 written = copy_to_iter(addr, copy, iter);
3515 kunmap_atomic(addr);
3517 written = copy_page_to_iter(page, 0, copy, iter);
3518 remaining -= written;
3519 if (written < copy && iov_iter_count(iter) > 0)
3522 return remaining ? -EFAULT : 0;
3525 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3528 cifs_uncached_readv_complete(struct work_struct *work)
3530 struct cifs_readdata *rdata = container_of(work,
3531 struct cifs_readdata, work);
3533 complete(&rdata->done);
3534 collect_uncached_read_data(rdata->ctx);
3535 /* the below call can possibly free the last ref to aio ctx */
3536 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3540 uncached_fill_pages(struct TCP_Server_Info *server,
3541 struct cifs_readdata *rdata, struct iov_iter *iter,
3546 unsigned int nr_pages = rdata->nr_pages;
3547 unsigned int page_offset = rdata->page_offset;
3549 rdata->got_bytes = 0;
3550 rdata->tailsz = PAGE_SIZE;
3551 for (i = 0; i < nr_pages; i++) {
3552 struct page *page = rdata->pages[i];
3554 unsigned int segment_size = rdata->pagesz;
3557 segment_size -= page_offset;
3563 /* no need to hold page hostage */
3564 rdata->pages[i] = NULL;
3571 if (len >= segment_size)
3572 /* enough data to fill the page */
3575 rdata->tailsz = len;
3579 result = copy_page_from_iter(
3580 page, page_offset, n, iter);
3581 #ifdef CONFIG_CIFS_SMB_DIRECT
3586 result = cifs_read_page_from_socket(
3587 server, page, page_offset, n);
3591 rdata->got_bytes += result;
3594 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3595 rdata->got_bytes : result;
3599 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3600 struct cifs_readdata *rdata, unsigned int len)
3602 return uncached_fill_pages(server, rdata, NULL, len);
3606 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3607 struct cifs_readdata *rdata,
3608 struct iov_iter *iter)
3610 return uncached_fill_pages(server, rdata, iter, iter->count);
3613 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3614 struct list_head *rdata_list,
3615 struct cifs_aio_ctx *ctx)
3618 struct cifs_credits credits;
3620 struct TCP_Server_Info *server;
3622 /* XXX: should we pick a new channel here? */
3623 server = rdata->server;
3626 if (rdata->cfile->invalidHandle) {
3627 rc = cifs_reopen_file(rdata->cfile, true);
3635 * Wait for credits to resend this rdata.
3636 * Note: we are attempting to resend the whole rdata not in
3640 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3646 if (rsize < rdata->bytes) {
3647 add_credits_and_wake_if(server, &credits, 0);
3650 } while (rsize < rdata->bytes);
3651 rdata->credits = credits;
3653 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3655 if (rdata->cfile->invalidHandle)
3658 #ifdef CONFIG_CIFS_SMB_DIRECT
3660 rdata->mr->need_invalidate = true;
3661 smbd_deregister_mr(rdata->mr);
3665 rc = server->ops->async_readv(rdata);
3669 /* If the read was successfully sent, we are done */
3671 /* Add to aio pending list */
3672 list_add_tail(&rdata->list, rdata_list);
3676 /* Roll back credits and retry if needed */
3677 add_credits_and_wake_if(server, &rdata->credits, 0);
3678 } while (rc == -EAGAIN);
3681 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3686 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3687 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3688 struct cifs_aio_ctx *ctx)
3690 struct cifs_readdata *rdata;
3691 unsigned int npages, rsize;
3692 struct cifs_credits credits_on_stack;
3693 struct cifs_credits *credits = &credits_on_stack;
3697 struct TCP_Server_Info *server;
3698 struct page **pagevec;
3700 struct iov_iter direct_iov = ctx->iter;
3702 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3704 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3705 pid = open_file->pid;
3707 pid = current->tgid;
3710 iov_iter_advance(&direct_iov, offset - ctx->pos);
3713 if (open_file->invalidHandle) {
3714 rc = cifs_reopen_file(open_file, true);
3721 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
3726 cur_len = min_t(const size_t, len, rsize);
3728 if (ctx->direct_io) {
3731 result = iov_iter_get_pages_alloc(
3732 &direct_iov, &pagevec,
3736 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3737 result, iov_iter_type(&direct_iov),
3738 direct_iov.iov_offset,
3743 add_credits_and_wake_if(server, credits, 0);
3746 cur_len = (size_t)result;
3747 iov_iter_advance(&direct_iov, cur_len);
3749 rdata = cifs_readdata_direct_alloc(
3750 pagevec, cifs_uncached_readv_complete);
3752 add_credits_and_wake_if(server, credits, 0);
3757 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
3758 rdata->page_offset = start;
3759 rdata->tailsz = npages > 1 ?
3760 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
3765 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
3766 /* allocate a readdata struct */
3767 rdata = cifs_readdata_alloc(npages,
3768 cifs_uncached_readv_complete);
3770 add_credits_and_wake_if(server, credits, 0);
3775 rc = cifs_read_allocate_pages(rdata, npages);
3777 kvfree(rdata->pages);
3779 add_credits_and_wake_if(server, credits, 0);
3783 rdata->tailsz = PAGE_SIZE;
3786 rdata->server = server;
3787 rdata->cfile = cifsFileInfo_get(open_file);
3788 rdata->nr_pages = npages;
3789 rdata->offset = offset;
3790 rdata->bytes = cur_len;
3792 rdata->pagesz = PAGE_SIZE;
3793 rdata->read_into_pages = cifs_uncached_read_into_pages;
3794 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
3795 rdata->credits = credits_on_stack;
3797 kref_get(&ctx->refcount);
3799 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3802 if (rdata->cfile->invalidHandle)
3805 rc = server->ops->async_readv(rdata);
3809 add_credits_and_wake_if(server, &rdata->credits, 0);
3810 kref_put(&rdata->refcount,
3811 cifs_uncached_readdata_release);
3812 if (rc == -EAGAIN) {
3813 iov_iter_revert(&direct_iov, cur_len);
3819 list_add_tail(&rdata->list, rdata_list);
3828 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3830 struct cifs_readdata *rdata, *tmp;
3831 struct iov_iter *to = &ctx->iter;
3832 struct cifs_sb_info *cifs_sb;
3835 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
3837 mutex_lock(&ctx->aio_mutex);
3839 if (list_empty(&ctx->list)) {
3840 mutex_unlock(&ctx->aio_mutex);
3845 /* the loop below should proceed in the order of increasing offsets */
3847 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
3849 if (!try_wait_for_completion(&rdata->done)) {
3850 mutex_unlock(&ctx->aio_mutex);
3854 if (rdata->result == -EAGAIN) {
3855 /* resend call if it's a retryable error */
3856 struct list_head tmp_list;
3857 unsigned int got_bytes = rdata->got_bytes;
3859 list_del_init(&rdata->list);
3860 INIT_LIST_HEAD(&tmp_list);
3863 * Got a part of data and then reconnect has
3864 * happened -- fill the buffer and continue
3867 if (got_bytes && got_bytes < rdata->bytes) {
3869 if (!ctx->direct_io)
3870 rc = cifs_readdata_to_iov(rdata, to);
3872 kref_put(&rdata->refcount,
3873 cifs_uncached_readdata_release);
3878 if (ctx->direct_io) {
3880 * Re-use rdata as this is a
3883 rc = cifs_resend_rdata(
3887 rc = cifs_send_async_read(
3888 rdata->offset + got_bytes,
3889 rdata->bytes - got_bytes,
3890 rdata->cfile, cifs_sb,
3893 kref_put(&rdata->refcount,
3894 cifs_uncached_readdata_release);
3897 list_splice(&tmp_list, &ctx->list);
3900 } else if (rdata->result)
3902 else if (!ctx->direct_io)
3903 rc = cifs_readdata_to_iov(rdata, to);
3905 /* if there was a short read -- discard anything left */
3906 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3909 ctx->total_len += rdata->got_bytes;
3911 list_del_init(&rdata->list);
3912 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3915 if (!ctx->direct_io)
3916 ctx->total_len = ctx->len - iov_iter_count(to);
3918 /* mask nodata case */
3922 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
3924 mutex_unlock(&ctx->aio_mutex);
3926 if (ctx->iocb && ctx->iocb->ki_complete)
3927 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3929 complete(&ctx->done);
3932 static ssize_t __cifs_readv(
3933 struct kiocb *iocb, struct iov_iter *to, bool direct)
3936 struct file *file = iocb->ki_filp;
3937 struct cifs_sb_info *cifs_sb;
3938 struct cifsFileInfo *cfile;
3939 struct cifs_tcon *tcon;
3940 ssize_t rc, total_read = 0;
3941 loff_t offset = iocb->ki_pos;
3942 struct cifs_aio_ctx *ctx;
3945 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
3946 * fall back to data copy read path
3947 * this could be improved by getting pages directly in ITER_KVEC
3949 if (direct && iov_iter_is_kvec(to)) {
3950 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
3954 len = iov_iter_count(to);
3958 cifs_sb = CIFS_FILE_SB(file);
3959 cfile = file->private_data;
3960 tcon = tlink_tcon(cfile->tlink);
3962 if (!tcon->ses->server->ops->async_readv)
3965 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3966 cifs_dbg(FYI, "attempting read on write only file instance\n");
3968 ctx = cifs_aio_ctx_alloc();
3972 ctx->cfile = cifsFileInfo_get(cfile);
3974 if (!is_sync_kiocb(iocb))
3977 if (iter_is_iovec(to))
3978 ctx->should_dirty = true;
3982 ctx->direct_io = true;
3986 rc = setup_aio_ctx_iter(ctx, to, READ);
3988 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3994 /* grab a lock here due to read response handlers can access ctx */
3995 mutex_lock(&ctx->aio_mutex);
3997 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
3999 /* if at least one read request send succeeded, then reset rc */
4000 if (!list_empty(&ctx->list))
4003 mutex_unlock(&ctx->aio_mutex);
4006 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4010 if (!is_sync_kiocb(iocb)) {
4011 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4012 return -EIOCBQUEUED;
4015 rc = wait_for_completion_killable(&ctx->done);
4017 mutex_lock(&ctx->aio_mutex);
4018 ctx->rc = rc = -EINTR;
4019 total_read = ctx->total_len;
4020 mutex_unlock(&ctx->aio_mutex);
4023 total_read = ctx->total_len;
4026 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4029 iocb->ki_pos += total_read;
4035 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
4037 return __cifs_readv(iocb, to, true);
4040 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
4042 return __cifs_readv(iocb, to, false);
4046 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
4048 struct inode *inode = file_inode(iocb->ki_filp);
4049 struct cifsInodeInfo *cinode = CIFS_I(inode);
4050 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4051 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
4052 iocb->ki_filp->private_data;
4053 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4057 * In strict cache mode we need to read from the server all the time
4058 * if we don't have level II oplock because the server can delay mtime
4059 * change - so we can't make a decision about inode invalidating.
4060 * And we can also fail with pagereading if there are mandatory locks
4061 * on pages affected by this read but not on the region from pos to
4064 if (!CIFS_CACHE_READ(cinode))
4065 return cifs_user_readv(iocb, to);
4067 if (cap_unix(tcon->ses) &&
4068 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
4069 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
4070 return generic_file_read_iter(iocb, to);
4073 * We need to hold the sem to be sure nobody modifies lock list
4074 * with a brlock that prevents reading.
4076 down_read(&cinode->lock_sem);
4077 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4078 tcon->ses->server->vals->shared_lock_type,
4079 0, NULL, CIFS_READ_OP))
4080 rc = generic_file_read_iter(iocb, to);
4081 up_read(&cinode->lock_sem);
4086 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4089 unsigned int bytes_read = 0;
4090 unsigned int total_read;
4091 unsigned int current_read_size;
4093 struct cifs_sb_info *cifs_sb;
4094 struct cifs_tcon *tcon;
4095 struct TCP_Server_Info *server;
4098 struct cifsFileInfo *open_file;
4099 struct cifs_io_parms io_parms = {0};
4100 int buf_type = CIFS_NO_BUFFER;
4104 cifs_sb = CIFS_FILE_SB(file);
4106 /* FIXME: set up handlers for larger reads and/or convert to async */
4107 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4109 if (file->private_data == NULL) {
4114 open_file = file->private_data;
4115 tcon = tlink_tcon(open_file->tlink);
4116 server = cifs_pick_channel(tcon->ses);
4118 if (!server->ops->sync_read) {
4123 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4124 pid = open_file->pid;
4126 pid = current->tgid;
4128 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4129 cifs_dbg(FYI, "attempting read on write only file instance\n");
4131 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4132 total_read += bytes_read, cur_offset += bytes_read) {
4134 current_read_size = min_t(uint, read_size - total_read,
4137 * For windows me and 9x we do not want to request more
4138 * than it negotiated since it will refuse the read
4141 if (!(tcon->ses->capabilities &
4142 tcon->ses->server->vals->cap_large_files)) {
4143 current_read_size = min_t(uint,
4144 current_read_size, CIFSMaxBufSize);
4146 if (open_file->invalidHandle) {
4147 rc = cifs_reopen_file(open_file, true);
4152 io_parms.tcon = tcon;
4153 io_parms.offset = *offset;
4154 io_parms.length = current_read_size;
4155 io_parms.server = server;
4156 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4157 &bytes_read, &cur_offset,
4159 } while (rc == -EAGAIN);
4161 if (rc || (bytes_read == 0)) {
4169 cifs_stats_bytes_read(tcon, total_read);
4170 *offset += bytes_read;
4178 * If the page is mmap'ed into a process' page tables, then we need to make
4179 * sure that it doesn't change while being written back.
4182 cifs_page_mkwrite(struct vm_fault *vmf)
4184 struct page *page = vmf->page;
4187 return VM_FAULT_LOCKED;
4190 static const struct vm_operations_struct cifs_file_vm_ops = {
4191 .fault = filemap_fault,
4192 .map_pages = filemap_map_pages,
4193 .page_mkwrite = cifs_page_mkwrite,
4196 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4199 struct inode *inode = file_inode(file);
4203 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4204 rc = cifs_zap_mapping(inode);
4206 rc = generic_file_mmap(file, vma);
4208 vma->vm_ops = &cifs_file_vm_ops;
4214 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4220 rc = cifs_revalidate_file(file);
4222 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4225 rc = generic_file_mmap(file, vma);
4227 vma->vm_ops = &cifs_file_vm_ops;
4234 cifs_readv_complete(struct work_struct *work)
4236 unsigned int i, got_bytes;
4237 struct cifs_readdata *rdata = container_of(work,
4238 struct cifs_readdata, work);
4240 got_bytes = rdata->got_bytes;
4241 for (i = 0; i < rdata->nr_pages; i++) {
4242 struct page *page = rdata->pages[i];
4244 lru_cache_add(page);
4246 if (rdata->result == 0 ||
4247 (rdata->result == -EAGAIN && got_bytes)) {
4248 flush_dcache_page(page);
4249 SetPageUptodate(page);
4254 if (rdata->result == 0 ||
4255 (rdata->result == -EAGAIN && got_bytes))
4256 cifs_readpage_to_fscache(rdata->mapping->host, page);
4258 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4261 rdata->pages[i] = NULL;
4263 kref_put(&rdata->refcount, cifs_readdata_release);
4267 readpages_fill_pages(struct TCP_Server_Info *server,
4268 struct cifs_readdata *rdata, struct iov_iter *iter,
4275 unsigned int nr_pages = rdata->nr_pages;
4276 unsigned int page_offset = rdata->page_offset;
4278 /* determine the eof that the server (probably) has */
4279 eof = CIFS_I(rdata->mapping->host)->server_eof;
4280 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4281 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4283 rdata->got_bytes = 0;
4284 rdata->tailsz = PAGE_SIZE;
4285 for (i = 0; i < nr_pages; i++) {
4286 struct page *page = rdata->pages[i];
4287 unsigned int to_read = rdata->pagesz;
4291 to_read -= page_offset;
4297 if (len >= to_read) {
4299 } else if (len > 0) {
4300 /* enough for partial page, fill and zero the rest */
4301 zero_user(page, len + page_offset, to_read - len);
4302 n = rdata->tailsz = len;
4304 } else if (page->index > eof_index) {
4306 * The VFS will not try to do readahead past the
4307 * i_size, but it's possible that we have outstanding
4308 * writes with gaps in the middle and the i_size hasn't
4309 * caught up yet. Populate those with zeroed out pages
4310 * to prevent the VFS from repeatedly attempting to
4311 * fill them until the writes are flushed.
4313 zero_user(page, 0, PAGE_SIZE);
4314 lru_cache_add(page);
4315 flush_dcache_page(page);
4316 SetPageUptodate(page);
4319 rdata->pages[i] = NULL;
4323 /* no need to hold page hostage */
4324 lru_cache_add(page);
4327 rdata->pages[i] = NULL;
4333 result = copy_page_from_iter(
4334 page, page_offset, n, iter);
4335 #ifdef CONFIG_CIFS_SMB_DIRECT
4340 result = cifs_read_page_from_socket(
4341 server, page, page_offset, n);
4345 rdata->got_bytes += result;
4348 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4349 rdata->got_bytes : result;
4353 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4354 struct cifs_readdata *rdata, unsigned int len)
4356 return readpages_fill_pages(server, rdata, NULL, len);
4360 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4361 struct cifs_readdata *rdata,
4362 struct iov_iter *iter)
4364 return readpages_fill_pages(server, rdata, iter, iter->count);
4368 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4369 unsigned int rsize, struct list_head *tmplist,
4370 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4372 struct page *page, *tpage;
4373 unsigned int expected_index;
4375 gfp_t gfp = readahead_gfp_mask(mapping);
4377 INIT_LIST_HEAD(tmplist);
4379 page = lru_to_page(page_list);
4382 * Lock the page and put it in the cache. Since no one else
4383 * should have access to this page, we're safe to simply set
4384 * PG_locked without checking it first.
4386 __SetPageLocked(page);
4387 rc = add_to_page_cache_locked(page, mapping,
4390 /* give up if we can't stick it in the cache */
4392 __ClearPageLocked(page);
4396 /* move first page to the tmplist */
4397 *offset = (loff_t)page->index << PAGE_SHIFT;
4400 list_move_tail(&page->lru, tmplist);
4402 /* now try and add more pages onto the request */
4403 expected_index = page->index + 1;
4404 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4405 /* discontinuity ? */
4406 if (page->index != expected_index)
4409 /* would this page push the read over the rsize? */
4410 if (*bytes + PAGE_SIZE > rsize)
4413 __SetPageLocked(page);
4414 rc = add_to_page_cache_locked(page, mapping, page->index, gfp);
4416 __ClearPageLocked(page);
4419 list_move_tail(&page->lru, tmplist);
4420 (*bytes) += PAGE_SIZE;
4427 static int cifs_readpages(struct file *file, struct address_space *mapping,
4428 struct list_head *page_list, unsigned num_pages)
4432 struct list_head tmplist;
4433 struct cifsFileInfo *open_file = file->private_data;
4434 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4435 struct TCP_Server_Info *server;
4441 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4442 * immediately if the cookie is negative
4444 * After this point, every page in the list might have PG_fscache set,
4445 * so we will need to clean that up off of every page we don't use.
4447 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4454 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4455 pid = open_file->pid;
4457 pid = current->tgid;
4460 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4462 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4463 __func__, file, mapping, num_pages);
4466 * Start with the page at end of list and move it to private
4467 * list. Do the same with any following pages until we hit
4468 * the rsize limit, hit an index discontinuity, or run out of
4469 * pages. Issue the async read and then start the loop again
4470 * until the list is empty.
4472 * Note that list order is important. The page_list is in
4473 * the order of declining indexes. When we put the pages in
4474 * the rdata->pages, then we want them in increasing order.
4476 while (!list_empty(page_list) && !err) {
4477 unsigned int i, nr_pages, bytes, rsize;
4479 struct page *page, *tpage;
4480 struct cifs_readdata *rdata;
4481 struct cifs_credits credits_on_stack;
4482 struct cifs_credits *credits = &credits_on_stack;
4484 if (open_file->invalidHandle) {
4485 rc = cifs_reopen_file(open_file, true);
4492 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4498 * Give up immediately if rsize is too small to read an entire
4499 * page. The VFS will fall back to readpage. We should never
4500 * reach this point however since we set ra_pages to 0 when the
4501 * rsize is smaller than a cache page.
4503 if (unlikely(rsize < PAGE_SIZE)) {
4504 add_credits_and_wake_if(server, credits, 0);
4510 err = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4511 &nr_pages, &offset, &bytes);
4513 add_credits_and_wake_if(server, credits, 0);
4517 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4519 /* best to give up if we're out of mem */
4520 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4521 list_del(&page->lru);
4522 lru_cache_add(page);
4527 add_credits_and_wake_if(server, credits, 0);
4531 rdata->cfile = cifsFileInfo_get(open_file);
4532 rdata->server = server;
4533 rdata->mapping = mapping;
4534 rdata->offset = offset;
4535 rdata->bytes = bytes;
4537 rdata->pagesz = PAGE_SIZE;
4538 rdata->tailsz = PAGE_SIZE;
4539 rdata->read_into_pages = cifs_readpages_read_into_pages;
4540 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4541 rdata->credits = credits_on_stack;
4543 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4544 list_del(&page->lru);
4545 rdata->pages[rdata->nr_pages++] = page;
4548 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4551 if (rdata->cfile->invalidHandle)
4554 rc = server->ops->async_readv(rdata);
4558 add_credits_and_wake_if(server, &rdata->credits, 0);
4559 for (i = 0; i < rdata->nr_pages; i++) {
4560 page = rdata->pages[i];
4561 lru_cache_add(page);
4565 /* Fallback to the readpage in error/reconnect cases */
4566 kref_put(&rdata->refcount, cifs_readdata_release);
4570 kref_put(&rdata->refcount, cifs_readdata_release);
4573 /* Any pages that have been shown to fscache but didn't get added to
4574 * the pagecache must be uncached before they get returned to the
4577 cifs_fscache_readpages_cancel(mapping->host, page_list);
4583 * cifs_readpage_worker must be called with the page pinned
4585 static int cifs_readpage_worker(struct file *file, struct page *page,
4591 /* Is the page cached? */
4592 rc = cifs_readpage_from_fscache(file_inode(file), page);
4596 read_data = kmap(page);
4597 /* for reads over a certain size could initiate async read ahead */
4599 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4604 cifs_dbg(FYI, "Bytes read %d\n", rc);
4606 /* we do not want atime to be less than mtime, it broke some apps */
4607 file_inode(file)->i_atime = current_time(file_inode(file));
4608 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4609 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4611 file_inode(file)->i_atime = current_time(file_inode(file));
4614 memset(read_data + rc, 0, PAGE_SIZE - rc);
4616 flush_dcache_page(page);
4617 SetPageUptodate(page);
4619 /* send this page to the cache */
4620 cifs_readpage_to_fscache(file_inode(file), page);
4632 static int cifs_readpage(struct file *file, struct page *page)
4634 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
4640 if (file->private_data == NULL) {
4646 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4647 page, (int)offset, (int)offset);
4649 rc = cifs_readpage_worker(file, page, &offset);
4655 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4657 struct cifsFileInfo *open_file;
4659 spin_lock(&cifs_inode->open_file_lock);
4660 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4661 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4662 spin_unlock(&cifs_inode->open_file_lock);
4666 spin_unlock(&cifs_inode->open_file_lock);
4670 /* We do not want to update the file size from server for inodes
4671 open for write - to avoid races with writepage extending
4672 the file - in the future we could consider allowing
4673 refreshing the inode only on increases in the file size
4674 but this is tricky to do without racing with writebehind
4675 page caching in the current Linux kernel design */
4676 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4681 if (is_inode_writable(cifsInode)) {
4682 /* This inode is open for write at least once */
4683 struct cifs_sb_info *cifs_sb;
4685 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4686 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4687 /* since no page cache to corrupt on directio
4688 we can change size safely */
4692 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4700 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4701 loff_t pos, unsigned len, unsigned flags,
4702 struct page **pagep, void **fsdata)
4705 pgoff_t index = pos >> PAGE_SHIFT;
4706 loff_t offset = pos & (PAGE_SIZE - 1);
4707 loff_t page_start = pos & PAGE_MASK;
4712 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4715 page = grab_cache_page_write_begin(mapping, index, flags);
4721 if (PageUptodate(page))
4725 * If we write a full page it will be up to date, no need to read from
4726 * the server. If the write is short, we'll end up doing a sync write
4729 if (len == PAGE_SIZE)
4733 * optimize away the read when we have an oplock, and we're not
4734 * expecting to use any of the data we'd be reading in. That
4735 * is, when the page lies beyond the EOF, or straddles the EOF
4736 * and the write will cover all of the existing data.
4738 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4739 i_size = i_size_read(mapping->host);
4740 if (page_start >= i_size ||
4741 (offset == 0 && (pos + len) >= i_size)) {
4742 zero_user_segments(page, 0, offset,
4746 * PageChecked means that the parts of the page
4747 * to which we're not writing are considered up
4748 * to date. Once the data is copied to the
4749 * page, it can be set uptodate.
4751 SetPageChecked(page);
4756 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4758 * might as well read a page, it is fast enough. If we get
4759 * an error, we don't need to return it. cifs_write_end will
4760 * do a sync write instead since PG_uptodate isn't set.
4762 cifs_readpage_worker(file, page, &page_start);
4767 /* we could try using another file handle if there is one -
4768 but how would we lock it to prevent close of that handle
4769 racing with this read? In any case
4770 this will be written out by write_end so is fine */
4777 static int cifs_release_page(struct page *page, gfp_t gfp)
4779 if (PagePrivate(page))
4782 return cifs_fscache_release_page(page, gfp);
4785 static void cifs_invalidate_page(struct page *page, unsigned int offset,
4786 unsigned int length)
4788 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4790 if (offset == 0 && length == PAGE_SIZE)
4791 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4794 static int cifs_launder_page(struct page *page)
4797 loff_t range_start = page_offset(page);
4798 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
4799 struct writeback_control wbc = {
4800 .sync_mode = WB_SYNC_ALL,
4802 .range_start = range_start,
4803 .range_end = range_end,
4806 cifs_dbg(FYI, "Launder page: %p\n", page);
4808 if (clear_page_dirty_for_io(page))
4809 rc = cifs_writepage_locked(page, &wbc);
4811 cifs_fscache_invalidate_page(page, page->mapping->host);
4815 void cifs_oplock_break(struct work_struct *work)
4817 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4819 struct inode *inode = d_inode(cfile->dentry);
4820 struct cifsInodeInfo *cinode = CIFS_I(inode);
4821 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4822 struct TCP_Server_Info *server = tcon->ses->server;
4824 bool purge_cache = false;
4825 bool is_deferred = false;
4826 struct cifs_deferred_close *dclose;
4828 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4829 TASK_UNINTERRUPTIBLE);
4831 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4832 cfile->oplock_epoch, &purge_cache);
4834 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4835 cifs_has_mand_locks(cinode)) {
4836 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4841 if (inode && S_ISREG(inode->i_mode)) {
4842 if (CIFS_CACHE_READ(cinode))
4843 break_lease(inode, O_RDONLY);
4845 break_lease(inode, O_WRONLY);
4846 rc = filemap_fdatawrite(inode->i_mapping);
4847 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4848 rc = filemap_fdatawait(inode->i_mapping);
4849 mapping_set_error(inode->i_mapping, rc);
4850 cifs_zap_mapping(inode);
4852 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4853 if (CIFS_CACHE_WRITE(cinode))
4854 goto oplock_break_ack;
4857 rc = cifs_push_locks(cfile);
4859 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4863 * releasing stale oplock after recent reconnect of smb session using
4864 * a now incorrect file handle is not a data integrity issue but do
4865 * not bother sending an oplock release if session to server still is
4866 * disconnected since oplock already released by the server
4868 if (!cfile->oplock_break_cancelled) {
4869 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4871 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4874 * When oplock break is received and there are no active
4875 * file handles but cached, then schedule deferred close immediately.
4876 * So, new open will not use cached handle.
4878 spin_lock(&CIFS_I(inode)->deferred_lock);
4879 is_deferred = cifs_is_deferred_close(cfile, &dclose);
4881 cfile->deferred_close_scheduled &&
4882 delayed_work_pending(&cfile->deferred)) {
4884 * If there is no pending work, mod_delayed_work queues new work.
4885 * So, Increase the ref count to avoid use-after-free.
4887 if (!mod_delayed_work(deferredclose_wq, &cfile->deferred, 0))
4888 cifsFileInfo_get(cfile);
4890 spin_unlock(&CIFS_I(inode)->deferred_lock);
4891 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4892 cifs_done_oplock_break(cinode);
4896 * The presence of cifs_direct_io() in the address space ops vector
4897 * allowes open() O_DIRECT flags which would have failed otherwise.
4899 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4900 * so this method should never be called.
4902 * Direct IO is not yet supported in the cached mode.
4905 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4909 * Eventually need to support direct IO for non forcedirectio mounts
4914 static int cifs_swap_activate(struct swap_info_struct *sis,
4915 struct file *swap_file, sector_t *span)
4917 struct cifsFileInfo *cfile = swap_file->private_data;
4918 struct inode *inode = swap_file->f_mapping->host;
4919 unsigned long blocks;
4922 cifs_dbg(FYI, "swap activate\n");
4924 spin_lock(&inode->i_lock);
4925 blocks = inode->i_blocks;
4926 isize = inode->i_size;
4927 spin_unlock(&inode->i_lock);
4928 if (blocks*512 < isize) {
4929 pr_warn("swap activate: swapfile has holes\n");
4934 pr_warn_once("Swap support over SMB3 is experimental\n");
4937 * TODO: consider adding ACL (or documenting how) to prevent other
4938 * users (on this or other systems) from reading it
4942 /* TODO: add sk_set_memalloc(inet) or similar */
4945 cfile->swapfile = true;
4947 * TODO: Since file already open, we can't open with DENY_ALL here
4948 * but we could add call to grab a byte range lock to prevent others
4949 * from reading or writing the file
4955 static void cifs_swap_deactivate(struct file *file)
4957 struct cifsFileInfo *cfile = file->private_data;
4959 cifs_dbg(FYI, "swap deactivate\n");
4961 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
4964 cfile->swapfile = false;
4966 /* do we need to unpin (or unlock) the file */
4969 const struct address_space_operations cifs_addr_ops = {
4970 .readpage = cifs_readpage,
4971 .readpages = cifs_readpages,
4972 .writepage = cifs_writepage,
4973 .writepages = cifs_writepages,
4974 .write_begin = cifs_write_begin,
4975 .write_end = cifs_write_end,
4976 .set_page_dirty = __set_page_dirty_nobuffers,
4977 .releasepage = cifs_release_page,
4978 .direct_IO = cifs_direct_io,
4979 .invalidatepage = cifs_invalidate_page,
4980 .launder_page = cifs_launder_page,
4982 * TODO: investigate and if useful we could add an cifs_migratePage
4983 * helper (under an CONFIG_MIGRATION) in the future, and also
4984 * investigate and add an is_dirty_writeback helper if needed
4986 .swap_activate = cifs_swap_activate,
4987 .swap_deactivate = cifs_swap_deactivate,
4991 * cifs_readpages requires the server to support a buffer large enough to
4992 * contain the header plus one complete page of data. Otherwise, we need
4993 * to leave cifs_readpages out of the address space operations.
4995 const struct address_space_operations cifs_addr_ops_smallbuf = {
4996 .readpage = cifs_readpage,
4997 .writepage = cifs_writepage,
4998 .writepages = cifs_writepages,
4999 .write_begin = cifs_write_begin,
5000 .write_end = cifs_write_end,
5001 .set_page_dirty = __set_page_dirty_nobuffers,
5002 .releasepage = cifs_release_page,
5003 .invalidatepage = cifs_invalidate_page,
5004 .launder_page = cifs_launder_page,