4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
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"
48 static inline int cifs_convert_flags(unsigned int flags)
50 if ((flags & O_ACCMODE) == O_RDONLY)
52 else if ((flags & O_ACCMODE) == O_WRONLY)
54 else if ((flags & O_ACCMODE) == O_RDWR) {
55 /* GENERIC_ALL is too much permission to request
56 can cause unnecessary access denied on create */
57 /* return GENERIC_ALL; */
58 return (GENERIC_READ | GENERIC_WRITE);
61 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
62 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
66 static u32 cifs_posix_convert_flags(unsigned int flags)
70 if ((flags & O_ACCMODE) == O_RDONLY)
71 posix_flags = SMB_O_RDONLY;
72 else if ((flags & O_ACCMODE) == O_WRONLY)
73 posix_flags = SMB_O_WRONLY;
74 else if ((flags & O_ACCMODE) == O_RDWR)
75 posix_flags = SMB_O_RDWR;
77 if (flags & O_CREAT) {
78 posix_flags |= SMB_O_CREAT;
80 posix_flags |= SMB_O_EXCL;
81 } else if (flags & O_EXCL)
82 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
83 current->comm, current->tgid);
86 posix_flags |= SMB_O_TRUNC;
87 /* be safe and imply O_SYNC for O_DSYNC */
89 posix_flags |= SMB_O_SYNC;
90 if (flags & O_DIRECTORY)
91 posix_flags |= SMB_O_DIRECTORY;
92 if (flags & O_NOFOLLOW)
93 posix_flags |= SMB_O_NOFOLLOW;
95 posix_flags |= SMB_O_DIRECT;
100 static inline int cifs_get_disposition(unsigned int flags)
102 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
104 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
105 return FILE_OVERWRITE_IF;
106 else if ((flags & O_CREAT) == O_CREAT)
108 else if ((flags & O_TRUNC) == O_TRUNC)
109 return FILE_OVERWRITE;
114 int cifs_posix_open(char *full_path, struct inode **pinode,
115 struct super_block *sb, int mode, unsigned int f_flags,
116 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
119 FILE_UNIX_BASIC_INFO *presp_data;
120 __u32 posix_flags = 0;
121 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
122 struct cifs_fattr fattr;
123 struct tcon_link *tlink;
124 struct cifs_tcon *tcon;
126 cifs_dbg(FYI, "posix open %s\n", full_path);
128 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
129 if (presp_data == NULL)
132 tlink = cifs_sb_tlink(cifs_sb);
138 tcon = tlink_tcon(tlink);
139 mode &= ~current_umask();
141 posix_flags = cifs_posix_convert_flags(f_flags);
142 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
143 poplock, full_path, cifs_sb->local_nls,
144 cifs_remap(cifs_sb));
145 cifs_put_tlink(tlink);
150 if (presp_data->Type == cpu_to_le32(-1))
151 goto posix_open_ret; /* open ok, caller does qpathinfo */
154 goto posix_open_ret; /* caller does not need info */
156 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
158 /* get new inode and set it up */
159 if (*pinode == NULL) {
160 cifs_fill_uniqueid(sb, &fattr);
161 *pinode = cifs_iget(sb, &fattr);
167 cifs_fattr_to_inode(*pinode, &fattr);
176 cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
177 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
178 struct cifs_fid *fid, unsigned int xid)
183 int create_options = CREATE_NOT_DIR;
185 struct TCP_Server_Info *server = tcon->ses->server;
186 struct cifs_open_parms oparms;
188 if (!server->ops->open)
191 desired_access = cifs_convert_flags(f_flags);
193 /*********************************************************************
194 * open flag mapping table:
196 * POSIX Flag CIFS Disposition
197 * ---------- ----------------
198 * O_CREAT FILE_OPEN_IF
199 * O_CREAT | O_EXCL FILE_CREATE
200 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
201 * O_TRUNC FILE_OVERWRITE
202 * none of the above FILE_OPEN
204 * Note that there is not a direct match between disposition
205 * FILE_SUPERSEDE (ie create whether or not file exists although
206 * O_CREAT | O_TRUNC is similar but truncates the existing
207 * file rather than creating a new file as FILE_SUPERSEDE does
208 * (which uses the attributes / metadata passed in on open call)
210 *? O_SYNC is a reasonable match to CIFS writethrough flag
211 *? and the read write flags match reasonably. O_LARGEFILE
212 *? is irrelevant because largefile support is always used
213 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
214 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
215 *********************************************************************/
217 disposition = cifs_get_disposition(f_flags);
219 /* BB pass O_SYNC flag through on file attributes .. BB */
221 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
225 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
226 if (f_flags & O_SYNC)
227 create_options |= CREATE_WRITE_THROUGH;
229 if (f_flags & O_DIRECT)
230 create_options |= CREATE_NO_BUFFER;
233 oparms.cifs_sb = cifs_sb;
234 oparms.desired_access = desired_access;
235 oparms.create_options = cifs_create_options(cifs_sb, create_options);
236 oparms.disposition = disposition;
237 oparms.path = full_path;
239 oparms.reconnect = false;
241 rc = server->ops->open(xid, &oparms, oplock, buf);
246 /* TODO: Add support for calling posix query info but with passing in fid */
248 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
251 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
255 server->ops->close(xid, tcon, fid);
266 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
268 struct cifs_fid_locks *cur;
269 bool has_locks = false;
271 down_read(&cinode->lock_sem);
272 list_for_each_entry(cur, &cinode->llist, llist) {
273 if (!list_empty(&cur->locks)) {
278 up_read(&cinode->lock_sem);
283 cifs_down_write(struct rw_semaphore *sem)
285 while (!down_write_trylock(sem))
289 static void cifsFileInfo_put_work(struct work_struct *work);
291 struct cifsFileInfo *
292 cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
293 struct tcon_link *tlink, __u32 oplock)
295 struct dentry *dentry = file_dentry(file);
296 struct inode *inode = d_inode(dentry);
297 struct cifsInodeInfo *cinode = CIFS_I(inode);
298 struct cifsFileInfo *cfile;
299 struct cifs_fid_locks *fdlocks;
300 struct cifs_tcon *tcon = tlink_tcon(tlink);
301 struct TCP_Server_Info *server = tcon->ses->server;
303 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
307 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
313 INIT_LIST_HEAD(&fdlocks->locks);
314 fdlocks->cfile = cfile;
315 cfile->llist = fdlocks;
318 cfile->pid = current->tgid;
319 cfile->uid = current_fsuid();
320 cfile->dentry = dget(dentry);
321 cfile->f_flags = file->f_flags;
322 cfile->invalidHandle = false;
323 cfile->tlink = cifs_get_tlink(tlink);
324 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
325 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
326 mutex_init(&cfile->fh_mutex);
327 spin_lock_init(&cfile->file_info_lock);
329 cifs_sb_active(inode->i_sb);
332 * If the server returned a read oplock and we have mandatory brlocks,
333 * set oplock level to None.
335 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
336 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
340 cifs_down_write(&cinode->lock_sem);
341 list_add(&fdlocks->llist, &cinode->llist);
342 up_write(&cinode->lock_sem);
344 spin_lock(&tcon->open_file_lock);
345 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
346 oplock = fid->pending_open->oplock;
347 list_del(&fid->pending_open->olist);
349 fid->purge_cache = false;
350 server->ops->set_fid(cfile, fid, oplock);
352 list_add(&cfile->tlist, &tcon->openFileList);
353 atomic_inc(&tcon->num_local_opens);
355 /* if readable file instance put first in list*/
356 spin_lock(&cinode->open_file_lock);
357 if (file->f_mode & FMODE_READ)
358 list_add(&cfile->flist, &cinode->openFileList);
360 list_add_tail(&cfile->flist, &cinode->openFileList);
361 spin_unlock(&cinode->open_file_lock);
362 spin_unlock(&tcon->open_file_lock);
364 if (fid->purge_cache)
365 cifs_zap_mapping(inode);
367 file->private_data = cfile;
371 struct cifsFileInfo *
372 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
374 spin_lock(&cifs_file->file_info_lock);
375 cifsFileInfo_get_locked(cifs_file);
376 spin_unlock(&cifs_file->file_info_lock);
380 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
382 struct inode *inode = d_inode(cifs_file->dentry);
383 struct cifsInodeInfo *cifsi = CIFS_I(inode);
384 struct cifsLockInfo *li, *tmp;
385 struct super_block *sb = inode->i_sb;
388 * Delete any outstanding lock records. We'll lose them when the file
391 cifs_down_write(&cifsi->lock_sem);
392 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
393 list_del(&li->llist);
394 cifs_del_lock_waiters(li);
397 list_del(&cifs_file->llist->llist);
398 kfree(cifs_file->llist);
399 up_write(&cifsi->lock_sem);
401 cifs_put_tlink(cifs_file->tlink);
402 dput(cifs_file->dentry);
403 cifs_sb_deactive(sb);
407 static void cifsFileInfo_put_work(struct work_struct *work)
409 struct cifsFileInfo *cifs_file = container_of(work,
410 struct cifsFileInfo, put);
412 cifsFileInfo_put_final(cifs_file);
416 * cifsFileInfo_put - release a reference of file priv data
418 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
420 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
422 _cifsFileInfo_put(cifs_file, true, true);
426 * _cifsFileInfo_put - release a reference of file priv data
428 * This may involve closing the filehandle @cifs_file out on the
429 * server. Must be called without holding tcon->open_file_lock,
430 * cinode->open_file_lock and cifs_file->file_info_lock.
432 * If @wait_for_oplock_handler is true and we are releasing the last
433 * reference, wait for any running oplock break handler of the file
434 * and cancel any pending one. If calling this function from the
435 * oplock break handler, you need to pass false.
438 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
439 bool wait_oplock_handler, bool offload)
441 struct inode *inode = d_inode(cifs_file->dentry);
442 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
443 struct TCP_Server_Info *server = tcon->ses->server;
444 struct cifsInodeInfo *cifsi = CIFS_I(inode);
445 struct super_block *sb = inode->i_sb;
446 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
448 struct cifs_pending_open open;
449 bool oplock_break_cancelled;
451 spin_lock(&tcon->open_file_lock);
452 spin_lock(&cifsi->open_file_lock);
453 spin_lock(&cifs_file->file_info_lock);
454 if (--cifs_file->count > 0) {
455 spin_unlock(&cifs_file->file_info_lock);
456 spin_unlock(&cifsi->open_file_lock);
457 spin_unlock(&tcon->open_file_lock);
460 spin_unlock(&cifs_file->file_info_lock);
462 if (server->ops->get_lease_key)
463 server->ops->get_lease_key(inode, &fid);
465 /* store open in pending opens to make sure we don't miss lease break */
466 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
468 /* remove it from the lists */
469 list_del(&cifs_file->flist);
470 list_del(&cifs_file->tlist);
471 atomic_dec(&tcon->num_local_opens);
473 if (list_empty(&cifsi->openFileList)) {
474 cifs_dbg(FYI, "closing last open instance for inode %p\n",
475 d_inode(cifs_file->dentry));
477 * In strict cache mode we need invalidate mapping on the last
478 * close because it may cause a error when we open this file
479 * again and get at least level II oplock.
481 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
482 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
483 cifs_set_oplock_level(cifsi, 0);
486 spin_unlock(&cifsi->open_file_lock);
487 spin_unlock(&tcon->open_file_lock);
489 oplock_break_cancelled = wait_oplock_handler ?
490 cancel_work_sync(&cifs_file->oplock_break) : false;
492 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
493 struct TCP_Server_Info *server = tcon->ses->server;
497 if (server->ops->close_getattr)
498 server->ops->close_getattr(xid, tcon, cifs_file);
499 else if (server->ops->close)
500 server->ops->close(xid, tcon, &cifs_file->fid);
504 if (oplock_break_cancelled)
505 cifs_done_oplock_break(cifsi);
507 cifs_del_pending_open(&open);
510 queue_work(fileinfo_put_wq, &cifs_file->put);
512 cifsFileInfo_put_final(cifs_file);
515 int cifs_open(struct inode *inode, struct file *file)
521 struct cifs_sb_info *cifs_sb;
522 struct TCP_Server_Info *server;
523 struct cifs_tcon *tcon;
524 struct tcon_link *tlink;
525 struct cifsFileInfo *cfile = NULL;
526 char *full_path = NULL;
527 bool posix_open_ok = false;
529 struct cifs_pending_open open;
533 cifs_sb = CIFS_SB(inode->i_sb);
534 tlink = cifs_sb_tlink(cifs_sb);
537 return PTR_ERR(tlink);
539 tcon = tlink_tcon(tlink);
540 server = tcon->ses->server;
542 full_path = build_path_from_dentry(file_dentry(file));
543 if (full_path == NULL) {
548 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
549 inode, file->f_flags, full_path);
551 if (file->f_flags & O_DIRECT &&
552 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
553 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
554 file->f_op = &cifs_file_direct_nobrl_ops;
556 file->f_op = &cifs_file_direct_ops;
564 if (!tcon->broken_posix_open && tcon->unix_ext &&
565 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
566 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
567 /* can not refresh inode info since size could be stale */
568 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
569 cifs_sb->mnt_file_mode /* ignored */,
570 file->f_flags, &oplock, &fid.netfid, xid);
572 cifs_dbg(FYI, "posix open succeeded\n");
573 posix_open_ok = true;
574 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
575 if (tcon->ses->serverNOS)
576 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",
577 tcon->ses->serverName,
578 tcon->ses->serverNOS);
579 tcon->broken_posix_open = true;
580 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
581 (rc != -EOPNOTSUPP)) /* path not found or net err */
584 * Else fallthrough to retry open the old way on network i/o
589 if (server->ops->get_lease_key)
590 server->ops->get_lease_key(inode, &fid);
592 cifs_add_pending_open(&fid, tlink, &open);
594 if (!posix_open_ok) {
595 if (server->ops->get_lease_key)
596 server->ops->get_lease_key(inode, &fid);
598 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
599 file->f_flags, &oplock, &fid, xid);
601 cifs_del_pending_open(&open);
606 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
608 if (server->ops->close)
609 server->ops->close(xid, tcon, &fid);
610 cifs_del_pending_open(&open);
615 cifs_fscache_set_inode_cookie(inode, file);
617 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
619 * Time to set mode which we can not set earlier due to
620 * problems creating new read-only files.
622 struct cifs_unix_set_info_args args = {
623 .mode = inode->i_mode,
624 .uid = INVALID_UID, /* no change */
625 .gid = INVALID_GID, /* no change */
626 .ctime = NO_CHANGE_64,
627 .atime = NO_CHANGE_64,
628 .mtime = NO_CHANGE_64,
631 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
638 cifs_put_tlink(tlink);
642 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
645 * Try to reacquire byte range locks that were released when session
646 * to server was lost.
649 cifs_relock_file(struct cifsFileInfo *cfile)
651 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
652 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
653 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
656 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
657 if (cinode->can_cache_brlcks) {
658 /* can cache locks - no need to relock */
659 up_read(&cinode->lock_sem);
663 if (cap_unix(tcon->ses) &&
664 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
665 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
666 rc = cifs_push_posix_locks(cfile);
668 rc = tcon->ses->server->ops->push_mand_locks(cfile);
670 up_read(&cinode->lock_sem);
675 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
680 struct cifs_sb_info *cifs_sb;
681 struct cifs_tcon *tcon;
682 struct TCP_Server_Info *server;
683 struct cifsInodeInfo *cinode;
685 char *full_path = NULL;
687 int disposition = FILE_OPEN;
688 int create_options = CREATE_NOT_DIR;
689 struct cifs_open_parms oparms;
692 mutex_lock(&cfile->fh_mutex);
693 if (!cfile->invalidHandle) {
694 mutex_unlock(&cfile->fh_mutex);
700 inode = d_inode(cfile->dentry);
701 cifs_sb = CIFS_SB(inode->i_sb);
702 tcon = tlink_tcon(cfile->tlink);
703 server = tcon->ses->server;
706 * Can not grab rename sem here because various ops, including those
707 * that already have the rename sem can end up causing writepage to get
708 * called and if the server was down that means we end up here, and we
709 * can never tell if the caller already has the rename_sem.
711 full_path = build_path_from_dentry(cfile->dentry);
712 if (full_path == NULL) {
714 mutex_unlock(&cfile->fh_mutex);
719 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
720 inode, cfile->f_flags, full_path);
722 if (tcon->ses->server->oplocks)
727 if (tcon->unix_ext && cap_unix(tcon->ses) &&
728 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
729 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
731 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
732 * original open. Must mask them off for a reopen.
734 unsigned int oflags = cfile->f_flags &
735 ~(O_CREAT | O_EXCL | O_TRUNC);
737 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
738 cifs_sb->mnt_file_mode /* ignored */,
739 oflags, &oplock, &cfile->fid.netfid, xid);
741 cifs_dbg(FYI, "posix reopen succeeded\n");
742 oparms.reconnect = true;
746 * fallthrough to retry open the old way on errors, especially
747 * in the reconnect path it is important to retry hard
751 desired_access = cifs_convert_flags(cfile->f_flags);
753 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
754 if (cfile->f_flags & O_SYNC)
755 create_options |= CREATE_WRITE_THROUGH;
757 if (cfile->f_flags & O_DIRECT)
758 create_options |= CREATE_NO_BUFFER;
760 if (server->ops->get_lease_key)
761 server->ops->get_lease_key(inode, &cfile->fid);
764 oparms.cifs_sb = cifs_sb;
765 oparms.desired_access = desired_access;
766 oparms.create_options = cifs_create_options(cifs_sb, create_options);
767 oparms.disposition = disposition;
768 oparms.path = full_path;
769 oparms.fid = &cfile->fid;
770 oparms.reconnect = true;
773 * Can not refresh inode by passing in file_info buf to be returned by
774 * ops->open and then calling get_inode_info with returned buf since
775 * file might have write behind data that needs to be flushed and server
776 * version of file size can be stale. If we knew for sure that inode was
777 * not dirty locally we could do this.
779 rc = server->ops->open(xid, &oparms, &oplock, NULL);
780 if (rc == -ENOENT && oparms.reconnect == false) {
781 /* durable handle timeout is expired - open the file again */
782 rc = server->ops->open(xid, &oparms, &oplock, NULL);
783 /* indicate that we need to relock the file */
784 oparms.reconnect = true;
788 mutex_unlock(&cfile->fh_mutex);
789 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
790 cifs_dbg(FYI, "oplock: %d\n", oplock);
791 goto reopen_error_exit;
795 cfile->invalidHandle = false;
796 mutex_unlock(&cfile->fh_mutex);
797 cinode = CIFS_I(inode);
800 rc = filemap_write_and_wait(inode->i_mapping);
801 if (!is_interrupt_error(rc))
802 mapping_set_error(inode->i_mapping, rc);
804 if (tcon->posix_extensions)
805 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
806 else if (tcon->unix_ext)
807 rc = cifs_get_inode_info_unix(&inode, full_path,
810 rc = cifs_get_inode_info(&inode, full_path, NULL,
811 inode->i_sb, xid, NULL);
814 * Else we are writing out data to server already and could deadlock if
815 * we tried to flush data, and since we do not know if we have data that
816 * would invalidate the current end of file on the server we can not go
817 * to the server to get the new inode info.
821 * If the server returned a read oplock and we have mandatory brlocks,
822 * set oplock level to None.
824 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
825 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
829 server->ops->set_fid(cfile, &cfile->fid, oplock);
830 if (oparms.reconnect)
831 cifs_relock_file(cfile);
839 int cifs_close(struct inode *inode, struct file *file)
841 if (file->private_data != NULL) {
842 _cifsFileInfo_put(file->private_data, true, false);
843 file->private_data = NULL;
846 /* return code from the ->release op is always ignored */
851 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
853 struct cifsFileInfo *open_file;
854 struct list_head *tmp;
855 struct list_head *tmp1;
856 struct list_head tmp_list;
858 if (!tcon->use_persistent || !tcon->need_reopen_files)
861 tcon->need_reopen_files = false;
863 cifs_dbg(FYI, "Reopen persistent handles\n");
864 INIT_LIST_HEAD(&tmp_list);
866 /* list all files open on tree connection, reopen resilient handles */
867 spin_lock(&tcon->open_file_lock);
868 list_for_each(tmp, &tcon->openFileList) {
869 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
870 if (!open_file->invalidHandle)
872 cifsFileInfo_get(open_file);
873 list_add_tail(&open_file->rlist, &tmp_list);
875 spin_unlock(&tcon->open_file_lock);
877 list_for_each_safe(tmp, tmp1, &tmp_list) {
878 open_file = list_entry(tmp, struct cifsFileInfo, rlist);
879 if (cifs_reopen_file(open_file, false /* do not flush */))
880 tcon->need_reopen_files = true;
881 list_del_init(&open_file->rlist);
882 cifsFileInfo_put(open_file);
886 int cifs_closedir(struct inode *inode, struct file *file)
890 struct cifsFileInfo *cfile = file->private_data;
891 struct cifs_tcon *tcon;
892 struct TCP_Server_Info *server;
895 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
901 tcon = tlink_tcon(cfile->tlink);
902 server = tcon->ses->server;
904 cifs_dbg(FYI, "Freeing private data in close dir\n");
905 spin_lock(&cfile->file_info_lock);
906 if (server->ops->dir_needs_close(cfile)) {
907 cfile->invalidHandle = true;
908 spin_unlock(&cfile->file_info_lock);
909 if (server->ops->close_dir)
910 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
913 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
914 /* not much we can do if it fails anyway, ignore rc */
917 spin_unlock(&cfile->file_info_lock);
919 buf = cfile->srch_inf.ntwrk_buf_start;
921 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
922 cfile->srch_inf.ntwrk_buf_start = NULL;
923 if (cfile->srch_inf.smallBuf)
924 cifs_small_buf_release(buf);
926 cifs_buf_release(buf);
929 cifs_put_tlink(cfile->tlink);
930 kfree(file->private_data);
931 file->private_data = NULL;
932 /* BB can we lock the filestruct while this is going on? */
937 static struct cifsLockInfo *
938 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
940 struct cifsLockInfo *lock =
941 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
944 lock->offset = offset;
945 lock->length = length;
947 lock->pid = current->tgid;
949 INIT_LIST_HEAD(&lock->blist);
950 init_waitqueue_head(&lock->block_q);
955 cifs_del_lock_waiters(struct cifsLockInfo *lock)
957 struct cifsLockInfo *li, *tmp;
958 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
959 list_del_init(&li->blist);
960 wake_up(&li->block_q);
964 #define CIFS_LOCK_OP 0
965 #define CIFS_READ_OP 1
966 #define CIFS_WRITE_OP 2
968 /* @rw_check : 0 - no op, 1 - read, 2 - write */
970 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
971 __u64 length, __u8 type, __u16 flags,
972 struct cifsFileInfo *cfile,
973 struct cifsLockInfo **conf_lock, int rw_check)
975 struct cifsLockInfo *li;
976 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
977 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
979 list_for_each_entry(li, &fdlocks->locks, llist) {
980 if (offset + length <= li->offset ||
981 offset >= li->offset + li->length)
983 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
984 server->ops->compare_fids(cfile, cur_cfile)) {
985 /* shared lock prevents write op through the same fid */
986 if (!(li->type & server->vals->shared_lock_type) ||
987 rw_check != CIFS_WRITE_OP)
990 if ((type & server->vals->shared_lock_type) &&
991 ((server->ops->compare_fids(cfile, cur_cfile) &&
992 current->tgid == li->pid) || type == li->type))
994 if (rw_check == CIFS_LOCK_OP &&
995 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
996 server->ops->compare_fids(cfile, cur_cfile))
1006 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1007 __u8 type, __u16 flags,
1008 struct cifsLockInfo **conf_lock, int rw_check)
1011 struct cifs_fid_locks *cur;
1012 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1014 list_for_each_entry(cur, &cinode->llist, llist) {
1015 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1016 flags, cfile, conf_lock,
1026 * Check if there is another lock that prevents us to set the lock (mandatory
1027 * style). If such a lock exists, update the flock structure with its
1028 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1029 * or leave it the same if we can't. Returns 0 if we don't need to request to
1030 * the server or 1 otherwise.
1033 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1034 __u8 type, struct file_lock *flock)
1037 struct cifsLockInfo *conf_lock;
1038 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1039 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1042 down_read(&cinode->lock_sem);
1044 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1045 flock->fl_flags, &conf_lock,
1048 flock->fl_start = conf_lock->offset;
1049 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1050 flock->fl_pid = conf_lock->pid;
1051 if (conf_lock->type & server->vals->shared_lock_type)
1052 flock->fl_type = F_RDLCK;
1054 flock->fl_type = F_WRLCK;
1055 } else if (!cinode->can_cache_brlcks)
1058 flock->fl_type = F_UNLCK;
1060 up_read(&cinode->lock_sem);
1065 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1067 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1068 cifs_down_write(&cinode->lock_sem);
1069 list_add_tail(&lock->llist, &cfile->llist->locks);
1070 up_write(&cinode->lock_sem);
1074 * Set the byte-range lock (mandatory style). Returns:
1075 * 1) 0, if we set the lock and don't need to request to the server;
1076 * 2) 1, if no locks prevent us but we need to request to the server;
1077 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1080 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1083 struct cifsLockInfo *conf_lock;
1084 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1090 cifs_down_write(&cinode->lock_sem);
1092 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1093 lock->type, lock->flags, &conf_lock,
1095 if (!exist && cinode->can_cache_brlcks) {
1096 list_add_tail(&lock->llist, &cfile->llist->locks);
1097 up_write(&cinode->lock_sem);
1106 list_add_tail(&lock->blist, &conf_lock->blist);
1107 up_write(&cinode->lock_sem);
1108 rc = wait_event_interruptible(lock->block_q,
1109 (lock->blist.prev == &lock->blist) &&
1110 (lock->blist.next == &lock->blist));
1113 cifs_down_write(&cinode->lock_sem);
1114 list_del_init(&lock->blist);
1117 up_write(&cinode->lock_sem);
1122 * Check if there is another lock that prevents us to set the lock (posix
1123 * style). If such a lock exists, update the flock structure with its
1124 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1125 * or leave it the same if we can't. Returns 0 if we don't need to request to
1126 * the server or 1 otherwise.
1129 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1132 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1133 unsigned char saved_type = flock->fl_type;
1135 if ((flock->fl_flags & FL_POSIX) == 0)
1138 down_read(&cinode->lock_sem);
1139 posix_test_lock(file, flock);
1141 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1142 flock->fl_type = saved_type;
1146 up_read(&cinode->lock_sem);
1151 * Set the byte-range lock (posix style). Returns:
1152 * 1) 0, if we set the lock and don't need to request to the server;
1153 * 2) 1, if we need to request to the server;
1154 * 3) <0, if the error occurs while setting the lock.
1157 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1159 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1162 if ((flock->fl_flags & FL_POSIX) == 0)
1166 cifs_down_write(&cinode->lock_sem);
1167 if (!cinode->can_cache_brlcks) {
1168 up_write(&cinode->lock_sem);
1172 rc = posix_lock_file(file, flock, NULL);
1173 up_write(&cinode->lock_sem);
1174 if (rc == FILE_LOCK_DEFERRED) {
1175 rc = wait_event_interruptible(flock->fl_wait,
1176 list_empty(&flock->fl_blocked_member));
1179 locks_delete_block(flock);
1185 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1188 int rc = 0, stored_rc;
1189 struct cifsLockInfo *li, *tmp;
1190 struct cifs_tcon *tcon;
1191 unsigned int num, max_num, max_buf;
1192 LOCKING_ANDX_RANGE *buf, *cur;
1193 static const int types[] = {
1194 LOCKING_ANDX_LARGE_FILES,
1195 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1200 tcon = tlink_tcon(cfile->tlink);
1203 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1204 * and check it before using.
1206 max_buf = tcon->ses->server->maxBuf;
1207 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1212 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1214 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1216 max_num = (max_buf - sizeof(struct smb_hdr)) /
1217 sizeof(LOCKING_ANDX_RANGE);
1218 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1224 for (i = 0; i < 2; i++) {
1227 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1228 if (li->type != types[i])
1230 cur->Pid = cpu_to_le16(li->pid);
1231 cur->LengthLow = cpu_to_le32((u32)li->length);
1232 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1233 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1234 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1235 if (++num == max_num) {
1236 stored_rc = cifs_lockv(xid, tcon,
1238 (__u8)li->type, 0, num,
1249 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1250 (__u8)types[i], 0, num, buf);
1262 hash_lockowner(fl_owner_t owner)
1264 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1267 struct lock_to_push {
1268 struct list_head llist;
1277 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1279 struct inode *inode = d_inode(cfile->dentry);
1280 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1281 struct file_lock *flock;
1282 struct file_lock_context *flctx = inode->i_flctx;
1283 unsigned int count = 0, i;
1284 int rc = 0, xid, type;
1285 struct list_head locks_to_send, *el;
1286 struct lock_to_push *lck, *tmp;
1294 spin_lock(&flctx->flc_lock);
1295 list_for_each(el, &flctx->flc_posix) {
1298 spin_unlock(&flctx->flc_lock);
1300 INIT_LIST_HEAD(&locks_to_send);
1303 * Allocating count locks is enough because no FL_POSIX locks can be
1304 * added to the list while we are holding cinode->lock_sem that
1305 * protects locking operations of this inode.
1307 for (i = 0; i < count; i++) {
1308 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1313 list_add_tail(&lck->llist, &locks_to_send);
1316 el = locks_to_send.next;
1317 spin_lock(&flctx->flc_lock);
1318 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1319 if (el == &locks_to_send) {
1321 * The list ended. We don't have enough allocated
1322 * structures - something is really wrong.
1324 cifs_dbg(VFS, "Can't push all brlocks!\n");
1327 length = 1 + flock->fl_end - flock->fl_start;
1328 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1332 lck = list_entry(el, struct lock_to_push, llist);
1333 lck->pid = hash_lockowner(flock->fl_owner);
1334 lck->netfid = cfile->fid.netfid;
1335 lck->length = length;
1337 lck->offset = flock->fl_start;
1339 spin_unlock(&flctx->flc_lock);
1341 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1344 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1345 lck->offset, lck->length, NULL,
1349 list_del(&lck->llist);
1357 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1358 list_del(&lck->llist);
1365 cifs_push_locks(struct cifsFileInfo *cfile)
1367 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1368 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1369 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1372 /* we are going to update can_cache_brlcks here - need a write access */
1373 cifs_down_write(&cinode->lock_sem);
1374 if (!cinode->can_cache_brlcks) {
1375 up_write(&cinode->lock_sem);
1379 if (cap_unix(tcon->ses) &&
1380 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1381 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1382 rc = cifs_push_posix_locks(cfile);
1384 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1386 cinode->can_cache_brlcks = false;
1387 up_write(&cinode->lock_sem);
1392 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1393 bool *wait_flag, struct TCP_Server_Info *server)
1395 if (flock->fl_flags & FL_POSIX)
1396 cifs_dbg(FYI, "Posix\n");
1397 if (flock->fl_flags & FL_FLOCK)
1398 cifs_dbg(FYI, "Flock\n");
1399 if (flock->fl_flags & FL_SLEEP) {
1400 cifs_dbg(FYI, "Blocking lock\n");
1403 if (flock->fl_flags & FL_ACCESS)
1404 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1405 if (flock->fl_flags & FL_LEASE)
1406 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1407 if (flock->fl_flags &
1408 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1409 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1410 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1412 *type = server->vals->large_lock_type;
1413 if (flock->fl_type == F_WRLCK) {
1414 cifs_dbg(FYI, "F_WRLCK\n");
1415 *type |= server->vals->exclusive_lock_type;
1417 } else if (flock->fl_type == F_UNLCK) {
1418 cifs_dbg(FYI, "F_UNLCK\n");
1419 *type |= server->vals->unlock_lock_type;
1421 /* Check if unlock includes more than one lock range */
1422 } else if (flock->fl_type == F_RDLCK) {
1423 cifs_dbg(FYI, "F_RDLCK\n");
1424 *type |= server->vals->shared_lock_type;
1426 } else if (flock->fl_type == F_EXLCK) {
1427 cifs_dbg(FYI, "F_EXLCK\n");
1428 *type |= server->vals->exclusive_lock_type;
1430 } else if (flock->fl_type == F_SHLCK) {
1431 cifs_dbg(FYI, "F_SHLCK\n");
1432 *type |= server->vals->shared_lock_type;
1435 cifs_dbg(FYI, "Unknown type of lock\n");
1439 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1440 bool wait_flag, bool posix_lck, unsigned int xid)
1443 __u64 length = 1 + flock->fl_end - flock->fl_start;
1444 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1445 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1446 struct TCP_Server_Info *server = tcon->ses->server;
1447 __u16 netfid = cfile->fid.netfid;
1450 int posix_lock_type;
1452 rc = cifs_posix_lock_test(file, flock);
1456 if (type & server->vals->shared_lock_type)
1457 posix_lock_type = CIFS_RDLCK;
1459 posix_lock_type = CIFS_WRLCK;
1460 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1461 hash_lockowner(flock->fl_owner),
1462 flock->fl_start, length, flock,
1463 posix_lock_type, wait_flag);
1467 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1471 /* BB we could chain these into one lock request BB */
1472 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1475 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1477 flock->fl_type = F_UNLCK;
1479 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1484 if (type & server->vals->shared_lock_type) {
1485 flock->fl_type = F_WRLCK;
1489 type &= ~server->vals->exclusive_lock_type;
1491 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1492 type | server->vals->shared_lock_type,
1495 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1496 type | server->vals->shared_lock_type, 0, 1, false);
1497 flock->fl_type = F_RDLCK;
1499 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1502 flock->fl_type = F_WRLCK;
1508 cifs_move_llist(struct list_head *source, struct list_head *dest)
1510 struct list_head *li, *tmp;
1511 list_for_each_safe(li, tmp, source)
1512 list_move(li, dest);
1516 cifs_free_llist(struct list_head *llist)
1518 struct cifsLockInfo *li, *tmp;
1519 list_for_each_entry_safe(li, tmp, llist, llist) {
1520 cifs_del_lock_waiters(li);
1521 list_del(&li->llist);
1527 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1530 int rc = 0, stored_rc;
1531 static const int types[] = {
1532 LOCKING_ANDX_LARGE_FILES,
1533 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1536 unsigned int max_num, num, max_buf;
1537 LOCKING_ANDX_RANGE *buf, *cur;
1538 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1539 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1540 struct cifsLockInfo *li, *tmp;
1541 __u64 length = 1 + flock->fl_end - flock->fl_start;
1542 struct list_head tmp_llist;
1544 INIT_LIST_HEAD(&tmp_llist);
1547 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1548 * and check it before using.
1550 max_buf = tcon->ses->server->maxBuf;
1551 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1554 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1556 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1558 max_num = (max_buf - sizeof(struct smb_hdr)) /
1559 sizeof(LOCKING_ANDX_RANGE);
1560 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1564 cifs_down_write(&cinode->lock_sem);
1565 for (i = 0; i < 2; i++) {
1568 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1569 if (flock->fl_start > li->offset ||
1570 (flock->fl_start + length) <
1571 (li->offset + li->length))
1573 if (current->tgid != li->pid)
1575 if (types[i] != li->type)
1577 if (cinode->can_cache_brlcks) {
1579 * We can cache brlock requests - simply remove
1580 * a lock from the file's list.
1582 list_del(&li->llist);
1583 cifs_del_lock_waiters(li);
1587 cur->Pid = cpu_to_le16(li->pid);
1588 cur->LengthLow = cpu_to_le32((u32)li->length);
1589 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1590 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1591 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1593 * We need to save a lock here to let us add it again to
1594 * the file's list if the unlock range request fails on
1597 list_move(&li->llist, &tmp_llist);
1598 if (++num == max_num) {
1599 stored_rc = cifs_lockv(xid, tcon,
1601 li->type, num, 0, buf);
1604 * We failed on the unlock range
1605 * request - add all locks from the tmp
1606 * list to the head of the file's list.
1608 cifs_move_llist(&tmp_llist,
1609 &cfile->llist->locks);
1613 * The unlock range request succeed -
1614 * free the tmp list.
1616 cifs_free_llist(&tmp_llist);
1623 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1624 types[i], num, 0, buf);
1626 cifs_move_llist(&tmp_llist,
1627 &cfile->llist->locks);
1630 cifs_free_llist(&tmp_llist);
1634 up_write(&cinode->lock_sem);
1640 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1641 bool wait_flag, bool posix_lck, int lock, int unlock,
1645 __u64 length = 1 + flock->fl_end - flock->fl_start;
1646 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1647 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1648 struct TCP_Server_Info *server = tcon->ses->server;
1649 struct inode *inode = d_inode(cfile->dentry);
1652 int posix_lock_type;
1654 rc = cifs_posix_lock_set(file, flock);
1658 if (type & server->vals->shared_lock_type)
1659 posix_lock_type = CIFS_RDLCK;
1661 posix_lock_type = CIFS_WRLCK;
1664 posix_lock_type = CIFS_UNLCK;
1666 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1667 hash_lockowner(flock->fl_owner),
1668 flock->fl_start, length,
1669 NULL, posix_lock_type, wait_flag);
1674 struct cifsLockInfo *lock;
1676 lock = cifs_lock_init(flock->fl_start, length, type,
1681 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1690 * Windows 7 server can delay breaking lease from read to None
1691 * if we set a byte-range lock on a file - break it explicitly
1692 * before sending the lock to the server to be sure the next
1693 * read won't conflict with non-overlapted locks due to
1696 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1697 CIFS_CACHE_READ(CIFS_I(inode))) {
1698 cifs_zap_mapping(inode);
1699 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1701 CIFS_I(inode)->oplock = 0;
1704 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1705 type, 1, 0, wait_flag);
1711 cifs_lock_add(cfile, lock);
1713 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1716 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1718 * If this is a request to remove all locks because we
1719 * are closing the file, it doesn't matter if the
1720 * unlocking failed as both cifs.ko and the SMB server
1721 * remove the lock on file close
1724 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1725 if (!(flock->fl_flags & FL_CLOSE))
1728 rc = locks_lock_file_wait(file, flock);
1733 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1736 int lock = 0, unlock = 0;
1737 bool wait_flag = false;
1738 bool posix_lck = false;
1739 struct cifs_sb_info *cifs_sb;
1740 struct cifs_tcon *tcon;
1741 struct cifsFileInfo *cfile;
1747 if (!(fl->fl_flags & FL_FLOCK))
1750 cfile = (struct cifsFileInfo *)file->private_data;
1751 tcon = tlink_tcon(cfile->tlink);
1753 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
1755 cifs_sb = CIFS_FILE_SB(file);
1757 if (cap_unix(tcon->ses) &&
1758 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1759 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1762 if (!lock && !unlock) {
1764 * if no lock or unlock then nothing to do since we do not
1771 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1779 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1782 int lock = 0, unlock = 0;
1783 bool wait_flag = false;
1784 bool posix_lck = false;
1785 struct cifs_sb_info *cifs_sb;
1786 struct cifs_tcon *tcon;
1787 struct cifsFileInfo *cfile;
1793 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1794 cmd, flock->fl_flags, flock->fl_type,
1795 flock->fl_start, flock->fl_end);
1797 cfile = (struct cifsFileInfo *)file->private_data;
1798 tcon = tlink_tcon(cfile->tlink);
1800 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1802 cifs_sb = CIFS_FILE_SB(file);
1804 if (cap_unix(tcon->ses) &&
1805 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1806 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1809 * BB add code here to normalize offset and length to account for
1810 * negative length which we can not accept over the wire.
1812 if (IS_GETLK(cmd)) {
1813 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1818 if (!lock && !unlock) {
1820 * if no lock or unlock then nothing to do since we do not
1827 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1834 * update the file size (if needed) after a write. Should be called with
1835 * the inode->i_lock held
1838 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1839 unsigned int bytes_written)
1841 loff_t end_of_write = offset + bytes_written;
1843 if (end_of_write > cifsi->server_eof)
1844 cifsi->server_eof = end_of_write;
1848 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1849 size_t write_size, loff_t *offset)
1852 unsigned int bytes_written = 0;
1853 unsigned int total_written;
1854 struct cifs_tcon *tcon;
1855 struct TCP_Server_Info *server;
1857 struct dentry *dentry = open_file->dentry;
1858 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
1859 struct cifs_io_parms io_parms = {0};
1861 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1862 write_size, *offset, dentry);
1864 tcon = tlink_tcon(open_file->tlink);
1865 server = tcon->ses->server;
1867 if (!server->ops->sync_write)
1872 for (total_written = 0; write_size > total_written;
1873 total_written += bytes_written) {
1875 while (rc == -EAGAIN) {
1879 if (open_file->invalidHandle) {
1880 /* we could deadlock if we called
1881 filemap_fdatawait from here so tell
1882 reopen_file not to flush data to
1884 rc = cifs_reopen_file(open_file, false);
1889 len = min(server->ops->wp_retry_size(d_inode(dentry)),
1890 (unsigned int)write_size - total_written);
1891 /* iov[0] is reserved for smb header */
1892 iov[1].iov_base = (char *)write_data + total_written;
1893 iov[1].iov_len = len;
1895 io_parms.tcon = tcon;
1896 io_parms.offset = *offset;
1897 io_parms.length = len;
1898 rc = server->ops->sync_write(xid, &open_file->fid,
1899 &io_parms, &bytes_written, iov, 1);
1901 if (rc || (bytes_written == 0)) {
1909 spin_lock(&d_inode(dentry)->i_lock);
1910 cifs_update_eof(cifsi, *offset, bytes_written);
1911 spin_unlock(&d_inode(dentry)->i_lock);
1912 *offset += bytes_written;
1916 cifs_stats_bytes_written(tcon, total_written);
1918 if (total_written > 0) {
1919 spin_lock(&d_inode(dentry)->i_lock);
1920 if (*offset > d_inode(dentry)->i_size)
1921 i_size_write(d_inode(dentry), *offset);
1922 spin_unlock(&d_inode(dentry)->i_lock);
1924 mark_inode_dirty_sync(d_inode(dentry));
1926 return total_written;
1929 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1932 struct cifsFileInfo *open_file = NULL;
1933 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1935 /* only filter by fsuid on multiuser mounts */
1936 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1939 spin_lock(&cifs_inode->open_file_lock);
1940 /* we could simply get the first_list_entry since write-only entries
1941 are always at the end of the list but since the first entry might
1942 have a close pending, we go through the whole list */
1943 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1944 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1946 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
1947 if (!open_file->invalidHandle) {
1948 /* found a good file */
1949 /* lock it so it will not be closed on us */
1950 cifsFileInfo_get(open_file);
1951 spin_unlock(&cifs_inode->open_file_lock);
1953 } /* else might as well continue, and look for
1954 another, or simply have the caller reopen it
1955 again rather than trying to fix this handle */
1956 } else /* write only file */
1957 break; /* write only files are last so must be done */
1959 spin_unlock(&cifs_inode->open_file_lock);
1963 /* Return -EBADF if no handle is found and general rc otherwise */
1965 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
1966 struct cifsFileInfo **ret_file)
1968 struct cifsFileInfo *open_file, *inv_file = NULL;
1969 struct cifs_sb_info *cifs_sb;
1970 bool any_available = false;
1972 unsigned int refind = 0;
1973 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
1974 bool with_delete = flags & FIND_WR_WITH_DELETE;
1978 * Having a null inode here (because mapping->host was set to zero by
1979 * the VFS or MM) should not happen but we had reports of on oops (due
1980 * to it being zero) during stress testcases so we need to check for it
1983 if (cifs_inode == NULL) {
1984 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
1989 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1991 /* only filter by fsuid on multiuser mounts */
1992 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1995 spin_lock(&cifs_inode->open_file_lock);
1997 if (refind > MAX_REOPEN_ATT) {
1998 spin_unlock(&cifs_inode->open_file_lock);
2001 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2002 if (!any_available && open_file->pid != current->tgid)
2004 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2006 if (with_delete && !(open_file->fid.access & DELETE))
2008 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2009 if (!open_file->invalidHandle) {
2010 /* found a good writable file */
2011 cifsFileInfo_get(open_file);
2012 spin_unlock(&cifs_inode->open_file_lock);
2013 *ret_file = open_file;
2017 inv_file = open_file;
2021 /* couldn't find useable FH with same pid, try any available */
2022 if (!any_available) {
2023 any_available = true;
2024 goto refind_writable;
2028 any_available = false;
2029 cifsFileInfo_get(inv_file);
2032 spin_unlock(&cifs_inode->open_file_lock);
2035 rc = cifs_reopen_file(inv_file, false);
2037 *ret_file = inv_file;
2041 spin_lock(&cifs_inode->open_file_lock);
2042 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2043 spin_unlock(&cifs_inode->open_file_lock);
2044 cifsFileInfo_put(inv_file);
2047 spin_lock(&cifs_inode->open_file_lock);
2048 goto refind_writable;
2054 struct cifsFileInfo *
2055 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2057 struct cifsFileInfo *cfile;
2060 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2062 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2068 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2070 struct cifsFileInfo **ret_file)
2072 struct list_head *tmp;
2073 struct cifsFileInfo *cfile;
2074 struct cifsInodeInfo *cinode;
2079 spin_lock(&tcon->open_file_lock);
2080 list_for_each(tmp, &tcon->openFileList) {
2081 cfile = list_entry(tmp, struct cifsFileInfo,
2083 full_path = build_path_from_dentry(cfile->dentry);
2084 if (full_path == NULL) {
2085 spin_unlock(&tcon->open_file_lock);
2088 if (strcmp(full_path, name)) {
2094 cinode = CIFS_I(d_inode(cfile->dentry));
2095 spin_unlock(&tcon->open_file_lock);
2096 return cifs_get_writable_file(cinode, flags, ret_file);
2099 spin_unlock(&tcon->open_file_lock);
2104 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2105 struct cifsFileInfo **ret_file)
2107 struct list_head *tmp;
2108 struct cifsFileInfo *cfile;
2109 struct cifsInodeInfo *cinode;
2114 spin_lock(&tcon->open_file_lock);
2115 list_for_each(tmp, &tcon->openFileList) {
2116 cfile = list_entry(tmp, struct cifsFileInfo,
2118 full_path = build_path_from_dentry(cfile->dentry);
2119 if (full_path == NULL) {
2120 spin_unlock(&tcon->open_file_lock);
2123 if (strcmp(full_path, name)) {
2129 cinode = CIFS_I(d_inode(cfile->dentry));
2130 spin_unlock(&tcon->open_file_lock);
2131 *ret_file = find_readable_file(cinode, 0);
2132 return *ret_file ? 0 : -ENOENT;
2135 spin_unlock(&tcon->open_file_lock);
2139 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2141 struct address_space *mapping = page->mapping;
2142 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2145 int bytes_written = 0;
2146 struct inode *inode;
2147 struct cifsFileInfo *open_file;
2149 if (!mapping || !mapping->host)
2152 inode = page->mapping->host;
2154 offset += (loff_t)from;
2155 write_data = kmap(page);
2158 if ((to > PAGE_SIZE) || (from > to)) {
2163 /* racing with truncate? */
2164 if (offset > mapping->host->i_size) {
2166 return 0; /* don't care */
2169 /* check to make sure that we are not extending the file */
2170 if (mapping->host->i_size - offset < (loff_t)to)
2171 to = (unsigned)(mapping->host->i_size - offset);
2173 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2176 bytes_written = cifs_write(open_file, open_file->pid,
2177 write_data, to - from, &offset);
2178 cifsFileInfo_put(open_file);
2179 /* Does mm or vfs already set times? */
2180 inode->i_atime = inode->i_mtime = current_time(inode);
2181 if ((bytes_written > 0) && (offset))
2183 else if (bytes_written < 0)
2188 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2189 if (!is_retryable_error(rc))
2197 static struct cifs_writedata *
2198 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2199 pgoff_t end, pgoff_t *index,
2200 unsigned int *found_pages)
2202 struct cifs_writedata *wdata;
2204 wdata = cifs_writedata_alloc((unsigned int)tofind,
2205 cifs_writev_complete);
2209 *found_pages = find_get_pages_range_tag(mapping, index, end,
2210 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2215 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2216 struct address_space *mapping,
2217 struct writeback_control *wbc,
2218 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2220 unsigned int nr_pages = 0, i;
2223 for (i = 0; i < found_pages; i++) {
2224 page = wdata->pages[i];
2226 * At this point we hold neither the i_pages lock nor the
2227 * page lock: the page may be truncated or invalidated
2228 * (changing page->mapping to NULL), or even swizzled
2229 * back from swapper_space to tmpfs file mapping
2234 else if (!trylock_page(page))
2237 if (unlikely(page->mapping != mapping)) {
2242 if (!wbc->range_cyclic && page->index > end) {
2248 if (*next && (page->index != *next)) {
2249 /* Not next consecutive page */
2254 if (wbc->sync_mode != WB_SYNC_NONE)
2255 wait_on_page_writeback(page);
2257 if (PageWriteback(page) ||
2258 !clear_page_dirty_for_io(page)) {
2264 * This actually clears the dirty bit in the radix tree.
2265 * See cifs_writepage() for more commentary.
2267 set_page_writeback(page);
2268 if (page_offset(page) >= i_size_read(mapping->host)) {
2271 end_page_writeback(page);
2275 wdata->pages[i] = page;
2276 *next = page->index + 1;
2280 /* reset index to refind any pages skipped */
2282 *index = wdata->pages[0]->index + 1;
2284 /* put any pages we aren't going to use */
2285 for (i = nr_pages; i < found_pages; i++) {
2286 put_page(wdata->pages[i]);
2287 wdata->pages[i] = NULL;
2294 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2295 struct address_space *mapping, struct writeback_control *wbc)
2299 wdata->sync_mode = wbc->sync_mode;
2300 wdata->nr_pages = nr_pages;
2301 wdata->offset = page_offset(wdata->pages[0]);
2302 wdata->pagesz = PAGE_SIZE;
2303 wdata->tailsz = min(i_size_read(mapping->host) -
2304 page_offset(wdata->pages[nr_pages - 1]),
2306 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2307 wdata->pid = wdata->cfile->pid;
2309 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2313 if (wdata->cfile->invalidHandle)
2316 rc = wdata->server->ops->async_writev(wdata,
2317 cifs_writedata_release);
2322 static int cifs_writepages(struct address_space *mapping,
2323 struct writeback_control *wbc)
2325 struct inode *inode = mapping->host;
2326 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2327 struct TCP_Server_Info *server;
2328 bool done = false, scanned = false, range_whole = false;
2330 struct cifs_writedata *wdata;
2331 struct cifsFileInfo *cfile = NULL;
2337 * If wsize is smaller than the page cache size, default to writing
2338 * one page at a time via cifs_writepage
2340 if (cifs_sb->wsize < PAGE_SIZE)
2341 return generic_writepages(mapping, wbc);
2344 if (wbc->range_cyclic) {
2345 index = mapping->writeback_index; /* Start from prev offset */
2348 index = wbc->range_start >> PAGE_SHIFT;
2349 end = wbc->range_end >> PAGE_SHIFT;
2350 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2354 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2357 while (!done && index <= end) {
2358 unsigned int i, nr_pages, found_pages, wsize;
2359 pgoff_t next = 0, tofind, saved_index = index;
2360 struct cifs_credits credits_on_stack;
2361 struct cifs_credits *credits = &credits_on_stack;
2362 int get_file_rc = 0;
2365 cifsFileInfo_put(cfile);
2367 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2369 /* in case of an error store it to return later */
2373 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2380 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2382 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2387 add_credits_and_wake_if(server, credits, 0);
2391 if (found_pages == 0) {
2392 kref_put(&wdata->refcount, cifs_writedata_release);
2393 add_credits_and_wake_if(server, credits, 0);
2397 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2398 end, &index, &next, &done);
2400 /* nothing to write? */
2401 if (nr_pages == 0) {
2402 kref_put(&wdata->refcount, cifs_writedata_release);
2403 add_credits_and_wake_if(server, credits, 0);
2407 wdata->credits = credits_on_stack;
2408 wdata->cfile = cfile;
2409 wdata->server = server;
2412 if (!wdata->cfile) {
2413 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2415 if (is_retryable_error(get_file_rc))
2420 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2422 for (i = 0; i < nr_pages; ++i)
2423 unlock_page(wdata->pages[i]);
2425 /* send failure -- clean up the mess */
2427 add_credits_and_wake_if(server, &wdata->credits, 0);
2428 for (i = 0; i < nr_pages; ++i) {
2429 if (is_retryable_error(rc))
2430 redirty_page_for_writepage(wbc,
2433 SetPageError(wdata->pages[i]);
2434 end_page_writeback(wdata->pages[i]);
2435 put_page(wdata->pages[i]);
2437 if (!is_retryable_error(rc))
2438 mapping_set_error(mapping, rc);
2440 kref_put(&wdata->refcount, cifs_writedata_release);
2442 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2443 index = saved_index;
2447 /* Return immediately if we received a signal during writing */
2448 if (is_interrupt_error(rc)) {
2453 if (rc != 0 && saved_rc == 0)
2456 wbc->nr_to_write -= nr_pages;
2457 if (wbc->nr_to_write <= 0)
2463 if (!scanned && !done) {
2465 * We hit the last page and there is more work to be done: wrap
2466 * back to the start of the file
2476 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2477 mapping->writeback_index = index;
2480 cifsFileInfo_put(cfile);
2486 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2492 /* BB add check for wbc flags */
2494 if (!PageUptodate(page))
2495 cifs_dbg(FYI, "ppw - page not up to date\n");
2498 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2500 * A writepage() implementation always needs to do either this,
2501 * or re-dirty the page with "redirty_page_for_writepage()" in
2502 * the case of a failure.
2504 * Just unlocking the page will cause the radix tree tag-bits
2505 * to fail to update with the state of the page correctly.
2507 set_page_writeback(page);
2509 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2510 if (is_retryable_error(rc)) {
2511 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2513 redirty_page_for_writepage(wbc, page);
2514 } else if (rc != 0) {
2516 mapping_set_error(page->mapping, rc);
2518 SetPageUptodate(page);
2520 end_page_writeback(page);
2526 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2528 int rc = cifs_writepage_locked(page, wbc);
2533 static int cifs_write_end(struct file *file, struct address_space *mapping,
2534 loff_t pos, unsigned len, unsigned copied,
2535 struct page *page, void *fsdata)
2538 struct inode *inode = mapping->host;
2539 struct cifsFileInfo *cfile = file->private_data;
2540 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2543 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2546 pid = current->tgid;
2548 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2551 if (PageChecked(page)) {
2553 SetPageUptodate(page);
2554 ClearPageChecked(page);
2555 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2556 SetPageUptodate(page);
2558 if (!PageUptodate(page)) {
2560 unsigned offset = pos & (PAGE_SIZE - 1);
2564 /* this is probably better than directly calling
2565 partialpage_write since in this function the file handle is
2566 known which we might as well leverage */
2567 /* BB check if anything else missing out of ppw
2568 such as updating last write time */
2569 page_data = kmap(page);
2570 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2571 /* if (rc < 0) should we set writebehind rc? */
2578 set_page_dirty(page);
2582 spin_lock(&inode->i_lock);
2583 if (pos > inode->i_size)
2584 i_size_write(inode, pos);
2585 spin_unlock(&inode->i_lock);
2594 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2599 struct cifs_tcon *tcon;
2600 struct TCP_Server_Info *server;
2601 struct cifsFileInfo *smbfile = file->private_data;
2602 struct inode *inode = file_inode(file);
2603 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2605 rc = file_write_and_wait_range(file, start, end);
2607 trace_cifs_fsync_err(inode->i_ino, rc);
2613 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2616 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2617 rc = cifs_zap_mapping(inode);
2619 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2620 rc = 0; /* don't care about it in fsync */
2624 tcon = tlink_tcon(smbfile->tlink);
2625 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2626 server = tcon->ses->server;
2627 if (server->ops->flush)
2628 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2637 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2641 struct cifs_tcon *tcon;
2642 struct TCP_Server_Info *server;
2643 struct cifsFileInfo *smbfile = file->private_data;
2644 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2646 rc = file_write_and_wait_range(file, start, end);
2648 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
2654 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2657 tcon = tlink_tcon(smbfile->tlink);
2658 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2659 server = tcon->ses->server;
2660 if (server->ops->flush)
2661 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2671 * As file closes, flush all cached write data for this inode checking
2672 * for write behind errors.
2674 int cifs_flush(struct file *file, fl_owner_t id)
2676 struct inode *inode = file_inode(file);
2679 if (file->f_mode & FMODE_WRITE)
2680 rc = filemap_write_and_wait(inode->i_mapping);
2682 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2684 trace_cifs_flush_err(inode->i_ino, rc);
2689 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2694 for (i = 0; i < num_pages; i++) {
2695 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2698 * save number of pages we have already allocated and
2699 * return with ENOMEM error
2708 for (i = 0; i < num_pages; i++)
2715 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2720 clen = min_t(const size_t, len, wsize);
2721 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
2730 cifs_uncached_writedata_release(struct kref *refcount)
2733 struct cifs_writedata *wdata = container_of(refcount,
2734 struct cifs_writedata, refcount);
2736 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
2737 for (i = 0; i < wdata->nr_pages; i++)
2738 put_page(wdata->pages[i]);
2739 cifs_writedata_release(refcount);
2742 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2745 cifs_uncached_writev_complete(struct work_struct *work)
2747 struct cifs_writedata *wdata = container_of(work,
2748 struct cifs_writedata, work);
2749 struct inode *inode = d_inode(wdata->cfile->dentry);
2750 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2752 spin_lock(&inode->i_lock);
2753 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2754 if (cifsi->server_eof > inode->i_size)
2755 i_size_write(inode, cifsi->server_eof);
2756 spin_unlock(&inode->i_lock);
2758 complete(&wdata->done);
2759 collect_uncached_write_data(wdata->ctx);
2760 /* the below call can possibly free the last ref to aio ctx */
2761 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2765 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2766 size_t *len, unsigned long *num_pages)
2768 size_t save_len, copied, bytes, cur_len = *len;
2769 unsigned long i, nr_pages = *num_pages;
2772 for (i = 0; i < nr_pages; i++) {
2773 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2774 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2777 * If we didn't copy as much as we expected, then that
2778 * may mean we trod into an unmapped area. Stop copying
2779 * at that point. On the next pass through the big
2780 * loop, we'll likely end up getting a zero-length
2781 * write and bailing out of it.
2786 cur_len = save_len - cur_len;
2790 * If we have no data to send, then that probably means that
2791 * the copy above failed altogether. That's most likely because
2792 * the address in the iovec was bogus. Return -EFAULT and let
2793 * the caller free anything we allocated and bail out.
2799 * i + 1 now represents the number of pages we actually used in
2800 * the copy phase above.
2807 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
2808 struct cifs_aio_ctx *ctx)
2811 struct cifs_credits credits;
2813 struct TCP_Server_Info *server = wdata->server;
2816 if (wdata->cfile->invalidHandle) {
2817 rc = cifs_reopen_file(wdata->cfile, false);
2826 * Wait for credits to resend this wdata.
2827 * Note: we are attempting to resend the whole wdata not in
2831 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
2836 if (wsize < wdata->bytes) {
2837 add_credits_and_wake_if(server, &credits, 0);
2840 } while (wsize < wdata->bytes);
2841 wdata->credits = credits;
2843 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2846 if (wdata->cfile->invalidHandle)
2849 #ifdef CONFIG_CIFS_SMB_DIRECT
2851 wdata->mr->need_invalidate = true;
2852 smbd_deregister_mr(wdata->mr);
2856 rc = server->ops->async_writev(wdata,
2857 cifs_uncached_writedata_release);
2861 /* If the write was successfully sent, we are done */
2863 list_add_tail(&wdata->list, wdata_list);
2867 /* Roll back credits and retry if needed */
2868 add_credits_and_wake_if(server, &wdata->credits, 0);
2869 } while (rc == -EAGAIN);
2872 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2877 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2878 struct cifsFileInfo *open_file,
2879 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2880 struct cifs_aio_ctx *ctx)
2884 unsigned long nr_pages, num_pages, i;
2885 struct cifs_writedata *wdata;
2886 struct iov_iter saved_from = *from;
2887 loff_t saved_offset = offset;
2889 struct TCP_Server_Info *server;
2890 struct page **pagevec;
2894 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2895 pid = open_file->pid;
2897 pid = current->tgid;
2899 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
2904 struct cifs_credits credits_on_stack;
2905 struct cifs_credits *credits = &credits_on_stack;
2907 if (open_file->invalidHandle) {
2908 rc = cifs_reopen_file(open_file, false);
2915 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2920 cur_len = min_t(const size_t, len, wsize);
2922 if (ctx->direct_io) {
2925 result = iov_iter_get_pages_alloc(
2926 from, &pagevec, cur_len, &start);
2929 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
2930 result, iov_iter_type(from),
2931 from->iov_offset, from->count);
2935 add_credits_and_wake_if(server, credits, 0);
2938 cur_len = (size_t)result;
2939 iov_iter_advance(from, cur_len);
2942 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
2944 wdata = cifs_writedata_direct_alloc(pagevec,
2945 cifs_uncached_writev_complete);
2948 add_credits_and_wake_if(server, credits, 0);
2953 wdata->page_offset = start;
2956 cur_len - (PAGE_SIZE - start) -
2957 (nr_pages - 2) * PAGE_SIZE :
2960 nr_pages = get_numpages(wsize, len, &cur_len);
2961 wdata = cifs_writedata_alloc(nr_pages,
2962 cifs_uncached_writev_complete);
2965 add_credits_and_wake_if(server, credits, 0);
2969 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2971 kvfree(wdata->pages);
2973 add_credits_and_wake_if(server, credits, 0);
2977 num_pages = nr_pages;
2978 rc = wdata_fill_from_iovec(
2979 wdata, from, &cur_len, &num_pages);
2981 for (i = 0; i < nr_pages; i++)
2982 put_page(wdata->pages[i]);
2983 kvfree(wdata->pages);
2985 add_credits_and_wake_if(server, credits, 0);
2990 * Bring nr_pages down to the number of pages we
2991 * actually used, and free any pages that we didn't use.
2993 for ( ; nr_pages > num_pages; nr_pages--)
2994 put_page(wdata->pages[nr_pages - 1]);
2996 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
2999 wdata->sync_mode = WB_SYNC_ALL;
3000 wdata->nr_pages = nr_pages;
3001 wdata->offset = (__u64)offset;
3002 wdata->cfile = cifsFileInfo_get(open_file);
3003 wdata->server = server;
3005 wdata->bytes = cur_len;
3006 wdata->pagesz = PAGE_SIZE;
3007 wdata->credits = credits_on_stack;
3009 kref_get(&ctx->refcount);
3011 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3014 if (wdata->cfile->invalidHandle)
3017 rc = server->ops->async_writev(wdata,
3018 cifs_uncached_writedata_release);
3022 add_credits_and_wake_if(server, &wdata->credits, 0);
3023 kref_put(&wdata->refcount,
3024 cifs_uncached_writedata_release);
3025 if (rc == -EAGAIN) {
3027 iov_iter_advance(from, offset - saved_offset);
3033 list_add_tail(&wdata->list, wdata_list);
3042 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3044 struct cifs_writedata *wdata, *tmp;
3045 struct cifs_tcon *tcon;
3046 struct cifs_sb_info *cifs_sb;
3047 struct dentry *dentry = ctx->cfile->dentry;
3050 tcon = tlink_tcon(ctx->cfile->tlink);
3051 cifs_sb = CIFS_SB(dentry->d_sb);
3053 mutex_lock(&ctx->aio_mutex);
3055 if (list_empty(&ctx->list)) {
3056 mutex_unlock(&ctx->aio_mutex);
3062 * Wait for and collect replies for any successful sends in order of
3063 * increasing offset. Once an error is hit, then return without waiting
3064 * for any more replies.
3067 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3069 if (!try_wait_for_completion(&wdata->done)) {
3070 mutex_unlock(&ctx->aio_mutex);
3077 ctx->total_len += wdata->bytes;
3079 /* resend call if it's a retryable error */
3080 if (rc == -EAGAIN) {
3081 struct list_head tmp_list;
3082 struct iov_iter tmp_from = ctx->iter;
3084 INIT_LIST_HEAD(&tmp_list);
3085 list_del_init(&wdata->list);
3088 rc = cifs_resend_wdata(
3089 wdata, &tmp_list, ctx);
3091 iov_iter_advance(&tmp_from,
3092 wdata->offset - ctx->pos);
3094 rc = cifs_write_from_iter(wdata->offset,
3095 wdata->bytes, &tmp_from,
3096 ctx->cfile, cifs_sb, &tmp_list,
3099 kref_put(&wdata->refcount,
3100 cifs_uncached_writedata_release);
3103 list_splice(&tmp_list, &ctx->list);
3107 list_del_init(&wdata->list);
3108 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3111 cifs_stats_bytes_written(tcon, ctx->total_len);
3112 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3114 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3116 mutex_unlock(&ctx->aio_mutex);
3118 if (ctx->iocb && ctx->iocb->ki_complete)
3119 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3121 complete(&ctx->done);
3124 static ssize_t __cifs_writev(
3125 struct kiocb *iocb, struct iov_iter *from, bool direct)
3127 struct file *file = iocb->ki_filp;
3128 ssize_t total_written = 0;
3129 struct cifsFileInfo *cfile;
3130 struct cifs_tcon *tcon;
3131 struct cifs_sb_info *cifs_sb;
3132 struct cifs_aio_ctx *ctx;
3133 struct iov_iter saved_from = *from;
3134 size_t len = iov_iter_count(from);
3138 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3139 * In this case, fall back to non-direct write function.
3140 * this could be improved by getting pages directly in ITER_KVEC
3142 if (direct && iov_iter_is_kvec(from)) {
3143 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3147 rc = generic_write_checks(iocb, from);
3151 cifs_sb = CIFS_FILE_SB(file);
3152 cfile = file->private_data;
3153 tcon = tlink_tcon(cfile->tlink);
3155 if (!tcon->ses->server->ops->async_writev)
3158 ctx = cifs_aio_ctx_alloc();
3162 ctx->cfile = cifsFileInfo_get(cfile);
3164 if (!is_sync_kiocb(iocb))
3167 ctx->pos = iocb->ki_pos;
3170 ctx->direct_io = true;
3174 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3176 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3181 /* grab a lock here due to read response handlers can access ctx */
3182 mutex_lock(&ctx->aio_mutex);
3184 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3185 cfile, cifs_sb, &ctx->list, ctx);
3188 * If at least one write was successfully sent, then discard any rc
3189 * value from the later writes. If the other write succeeds, then
3190 * we'll end up returning whatever was written. If it fails, then
3191 * we'll get a new rc value from that.
3193 if (!list_empty(&ctx->list))
3196 mutex_unlock(&ctx->aio_mutex);
3199 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3203 if (!is_sync_kiocb(iocb)) {
3204 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3205 return -EIOCBQUEUED;
3208 rc = wait_for_completion_killable(&ctx->done);
3210 mutex_lock(&ctx->aio_mutex);
3211 ctx->rc = rc = -EINTR;
3212 total_written = ctx->total_len;
3213 mutex_unlock(&ctx->aio_mutex);
3216 total_written = ctx->total_len;
3219 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3221 if (unlikely(!total_written))
3224 iocb->ki_pos += total_written;
3225 return total_written;
3228 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3230 return __cifs_writev(iocb, from, true);
3233 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3235 return __cifs_writev(iocb, from, false);
3239 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3241 struct file *file = iocb->ki_filp;
3242 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3243 struct inode *inode = file->f_mapping->host;
3244 struct cifsInodeInfo *cinode = CIFS_I(inode);
3245 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3250 * We need to hold the sem to be sure nobody modifies lock list
3251 * with a brlock that prevents writing.
3253 down_read(&cinode->lock_sem);
3255 rc = generic_write_checks(iocb, from);
3259 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3260 server->vals->exclusive_lock_type, 0,
3261 NULL, CIFS_WRITE_OP))
3262 rc = __generic_file_write_iter(iocb, from);
3266 up_read(&cinode->lock_sem);
3267 inode_unlock(inode);
3270 rc = generic_write_sync(iocb, rc);
3275 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3277 struct inode *inode = file_inode(iocb->ki_filp);
3278 struct cifsInodeInfo *cinode = CIFS_I(inode);
3279 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3280 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3281 iocb->ki_filp->private_data;
3282 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3285 written = cifs_get_writer(cinode);
3289 if (CIFS_CACHE_WRITE(cinode)) {
3290 if (cap_unix(tcon->ses) &&
3291 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3292 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3293 written = generic_file_write_iter(iocb, from);
3296 written = cifs_writev(iocb, from);
3300 * For non-oplocked files in strict cache mode we need to write the data
3301 * to the server exactly from the pos to pos+len-1 rather than flush all
3302 * affected pages because it may cause a error with mandatory locks on
3303 * these pages but not on the region from pos to ppos+len-1.
3305 written = cifs_user_writev(iocb, from);
3306 if (CIFS_CACHE_READ(cinode)) {
3308 * We have read level caching and we have just sent a write
3309 * request to the server thus making data in the cache stale.
3310 * Zap the cache and set oplock/lease level to NONE to avoid
3311 * reading stale data from the cache. All subsequent read
3312 * operations will read new data from the server.
3314 cifs_zap_mapping(inode);
3315 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3320 cifs_put_writer(cinode);
3324 static struct cifs_readdata *
3325 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3327 struct cifs_readdata *rdata;
3329 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3330 if (rdata != NULL) {
3331 rdata->pages = pages;
3332 kref_init(&rdata->refcount);
3333 INIT_LIST_HEAD(&rdata->list);
3334 init_completion(&rdata->done);
3335 INIT_WORK(&rdata->work, complete);
3341 static struct cifs_readdata *
3342 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3344 struct page **pages =
3345 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3346 struct cifs_readdata *ret = NULL;
3349 ret = cifs_readdata_direct_alloc(pages, complete);
3358 cifs_readdata_release(struct kref *refcount)
3360 struct cifs_readdata *rdata = container_of(refcount,
3361 struct cifs_readdata, refcount);
3362 #ifdef CONFIG_CIFS_SMB_DIRECT
3364 smbd_deregister_mr(rdata->mr);
3369 cifsFileInfo_put(rdata->cfile);
3371 kvfree(rdata->pages);
3376 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3382 for (i = 0; i < nr_pages; i++) {
3383 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3388 rdata->pages[i] = page;
3392 unsigned int nr_page_failed = i;
3394 for (i = 0; i < nr_page_failed; i++) {
3395 put_page(rdata->pages[i]);
3396 rdata->pages[i] = NULL;
3403 cifs_uncached_readdata_release(struct kref *refcount)
3405 struct cifs_readdata *rdata = container_of(refcount,
3406 struct cifs_readdata, refcount);
3409 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3410 for (i = 0; i < rdata->nr_pages; i++) {
3411 put_page(rdata->pages[i]);
3413 cifs_readdata_release(refcount);
3417 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3418 * @rdata: the readdata response with list of pages holding data
3419 * @iter: destination for our data
3421 * This function copies data from a list of pages in a readdata response into
3422 * an array of iovecs. It will first calculate where the data should go
3423 * based on the info in the readdata and then copy the data into that spot.
3426 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3428 size_t remaining = rdata->got_bytes;
3431 for (i = 0; i < rdata->nr_pages; i++) {
3432 struct page *page = rdata->pages[i];
3433 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3436 if (unlikely(iov_iter_is_pipe(iter))) {
3437 void *addr = kmap_atomic(page);
3439 written = copy_to_iter(addr, copy, iter);
3440 kunmap_atomic(addr);
3442 written = copy_page_to_iter(page, 0, copy, iter);
3443 remaining -= written;
3444 if (written < copy && iov_iter_count(iter) > 0)
3447 return remaining ? -EFAULT : 0;
3450 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3453 cifs_uncached_readv_complete(struct work_struct *work)
3455 struct cifs_readdata *rdata = container_of(work,
3456 struct cifs_readdata, work);
3458 complete(&rdata->done);
3459 collect_uncached_read_data(rdata->ctx);
3460 /* the below call can possibly free the last ref to aio ctx */
3461 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3465 uncached_fill_pages(struct TCP_Server_Info *server,
3466 struct cifs_readdata *rdata, struct iov_iter *iter,
3471 unsigned int nr_pages = rdata->nr_pages;
3472 unsigned int page_offset = rdata->page_offset;
3474 rdata->got_bytes = 0;
3475 rdata->tailsz = PAGE_SIZE;
3476 for (i = 0; i < nr_pages; i++) {
3477 struct page *page = rdata->pages[i];
3479 unsigned int segment_size = rdata->pagesz;
3482 segment_size -= page_offset;
3488 /* no need to hold page hostage */
3489 rdata->pages[i] = NULL;
3496 if (len >= segment_size)
3497 /* enough data to fill the page */
3500 rdata->tailsz = len;
3504 result = copy_page_from_iter(
3505 page, page_offset, n, iter);
3506 #ifdef CONFIG_CIFS_SMB_DIRECT
3511 result = cifs_read_page_from_socket(
3512 server, page, page_offset, n);
3516 rdata->got_bytes += result;
3519 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3520 rdata->got_bytes : result;
3524 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3525 struct cifs_readdata *rdata, unsigned int len)
3527 return uncached_fill_pages(server, rdata, NULL, len);
3531 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3532 struct cifs_readdata *rdata,
3533 struct iov_iter *iter)
3535 return uncached_fill_pages(server, rdata, iter, iter->count);
3538 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3539 struct list_head *rdata_list,
3540 struct cifs_aio_ctx *ctx)
3543 struct cifs_credits credits;
3545 struct TCP_Server_Info *server;
3547 /* XXX: should we pick a new channel here? */
3548 server = rdata->server;
3551 if (rdata->cfile->invalidHandle) {
3552 rc = cifs_reopen_file(rdata->cfile, true);
3560 * Wait for credits to resend this rdata.
3561 * Note: we are attempting to resend the whole rdata not in
3565 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3571 if (rsize < rdata->bytes) {
3572 add_credits_and_wake_if(server, &credits, 0);
3575 } while (rsize < rdata->bytes);
3576 rdata->credits = credits;
3578 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3580 if (rdata->cfile->invalidHandle)
3583 #ifdef CONFIG_CIFS_SMB_DIRECT
3585 rdata->mr->need_invalidate = true;
3586 smbd_deregister_mr(rdata->mr);
3590 rc = server->ops->async_readv(rdata);
3594 /* If the read was successfully sent, we are done */
3596 /* Add to aio pending list */
3597 list_add_tail(&rdata->list, rdata_list);
3601 /* Roll back credits and retry if needed */
3602 add_credits_and_wake_if(server, &rdata->credits, 0);
3603 } while (rc == -EAGAIN);
3606 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3611 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3612 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3613 struct cifs_aio_ctx *ctx)
3615 struct cifs_readdata *rdata;
3616 unsigned int npages, rsize;
3617 struct cifs_credits credits_on_stack;
3618 struct cifs_credits *credits = &credits_on_stack;
3622 struct TCP_Server_Info *server;
3623 struct page **pagevec;
3625 struct iov_iter direct_iov = ctx->iter;
3627 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3629 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3630 pid = open_file->pid;
3632 pid = current->tgid;
3635 iov_iter_advance(&direct_iov, offset - ctx->pos);
3638 if (open_file->invalidHandle) {
3639 rc = cifs_reopen_file(open_file, true);
3646 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
3651 cur_len = min_t(const size_t, len, rsize);
3653 if (ctx->direct_io) {
3656 result = iov_iter_get_pages_alloc(
3657 &direct_iov, &pagevec,
3661 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3662 result, iov_iter_type(&direct_iov),
3663 direct_iov.iov_offset,
3668 add_credits_and_wake_if(server, credits, 0);
3671 cur_len = (size_t)result;
3672 iov_iter_advance(&direct_iov, cur_len);
3674 rdata = cifs_readdata_direct_alloc(
3675 pagevec, cifs_uncached_readv_complete);
3677 add_credits_and_wake_if(server, credits, 0);
3682 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
3683 rdata->page_offset = start;
3684 rdata->tailsz = npages > 1 ?
3685 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
3690 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
3691 /* allocate a readdata struct */
3692 rdata = cifs_readdata_alloc(npages,
3693 cifs_uncached_readv_complete);
3695 add_credits_and_wake_if(server, credits, 0);
3700 rc = cifs_read_allocate_pages(rdata, npages);
3702 kvfree(rdata->pages);
3704 add_credits_and_wake_if(server, credits, 0);
3708 rdata->tailsz = PAGE_SIZE;
3711 rdata->server = server;
3712 rdata->cfile = cifsFileInfo_get(open_file);
3713 rdata->nr_pages = npages;
3714 rdata->offset = offset;
3715 rdata->bytes = cur_len;
3717 rdata->pagesz = PAGE_SIZE;
3718 rdata->read_into_pages = cifs_uncached_read_into_pages;
3719 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
3720 rdata->credits = credits_on_stack;
3722 kref_get(&ctx->refcount);
3724 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3727 if (rdata->cfile->invalidHandle)
3730 rc = server->ops->async_readv(rdata);
3734 add_credits_and_wake_if(server, &rdata->credits, 0);
3735 kref_put(&rdata->refcount,
3736 cifs_uncached_readdata_release);
3737 if (rc == -EAGAIN) {
3738 iov_iter_revert(&direct_iov, cur_len);
3744 list_add_tail(&rdata->list, rdata_list);
3753 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3755 struct cifs_readdata *rdata, *tmp;
3756 struct iov_iter *to = &ctx->iter;
3757 struct cifs_sb_info *cifs_sb;
3760 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
3762 mutex_lock(&ctx->aio_mutex);
3764 if (list_empty(&ctx->list)) {
3765 mutex_unlock(&ctx->aio_mutex);
3770 /* the loop below should proceed in the order of increasing offsets */
3772 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
3774 if (!try_wait_for_completion(&rdata->done)) {
3775 mutex_unlock(&ctx->aio_mutex);
3779 if (rdata->result == -EAGAIN) {
3780 /* resend call if it's a retryable error */
3781 struct list_head tmp_list;
3782 unsigned int got_bytes = rdata->got_bytes;
3784 list_del_init(&rdata->list);
3785 INIT_LIST_HEAD(&tmp_list);
3788 * Got a part of data and then reconnect has
3789 * happened -- fill the buffer and continue
3792 if (got_bytes && got_bytes < rdata->bytes) {
3794 if (!ctx->direct_io)
3795 rc = cifs_readdata_to_iov(rdata, to);
3797 kref_put(&rdata->refcount,
3798 cifs_uncached_readdata_release);
3803 if (ctx->direct_io) {
3805 * Re-use rdata as this is a
3808 rc = cifs_resend_rdata(
3812 rc = cifs_send_async_read(
3813 rdata->offset + got_bytes,
3814 rdata->bytes - got_bytes,
3815 rdata->cfile, cifs_sb,
3818 kref_put(&rdata->refcount,
3819 cifs_uncached_readdata_release);
3822 list_splice(&tmp_list, &ctx->list);
3825 } else if (rdata->result)
3827 else if (!ctx->direct_io)
3828 rc = cifs_readdata_to_iov(rdata, to);
3830 /* if there was a short read -- discard anything left */
3831 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3834 ctx->total_len += rdata->got_bytes;
3836 list_del_init(&rdata->list);
3837 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3840 if (!ctx->direct_io)
3841 ctx->total_len = ctx->len - iov_iter_count(to);
3843 /* mask nodata case */
3847 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
3849 mutex_unlock(&ctx->aio_mutex);
3851 if (ctx->iocb && ctx->iocb->ki_complete)
3852 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3854 complete(&ctx->done);
3857 static ssize_t __cifs_readv(
3858 struct kiocb *iocb, struct iov_iter *to, bool direct)
3861 struct file *file = iocb->ki_filp;
3862 struct cifs_sb_info *cifs_sb;
3863 struct cifsFileInfo *cfile;
3864 struct cifs_tcon *tcon;
3865 ssize_t rc, total_read = 0;
3866 loff_t offset = iocb->ki_pos;
3867 struct cifs_aio_ctx *ctx;
3870 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
3871 * fall back to data copy read path
3872 * this could be improved by getting pages directly in ITER_KVEC
3874 if (direct && iov_iter_is_kvec(to)) {
3875 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
3879 len = iov_iter_count(to);
3883 cifs_sb = CIFS_FILE_SB(file);
3884 cfile = file->private_data;
3885 tcon = tlink_tcon(cfile->tlink);
3887 if (!tcon->ses->server->ops->async_readv)
3890 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3891 cifs_dbg(FYI, "attempting read on write only file instance\n");
3893 ctx = cifs_aio_ctx_alloc();
3897 ctx->cfile = cifsFileInfo_get(cfile);
3899 if (!is_sync_kiocb(iocb))
3902 if (iter_is_iovec(to))
3903 ctx->should_dirty = true;
3907 ctx->direct_io = true;
3911 rc = setup_aio_ctx_iter(ctx, to, READ);
3913 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3919 /* grab a lock here due to read response handlers can access ctx */
3920 mutex_lock(&ctx->aio_mutex);
3922 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
3924 /* if at least one read request send succeeded, then reset rc */
3925 if (!list_empty(&ctx->list))
3928 mutex_unlock(&ctx->aio_mutex);
3931 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3935 if (!is_sync_kiocb(iocb)) {
3936 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3937 return -EIOCBQUEUED;
3940 rc = wait_for_completion_killable(&ctx->done);
3942 mutex_lock(&ctx->aio_mutex);
3943 ctx->rc = rc = -EINTR;
3944 total_read = ctx->total_len;
3945 mutex_unlock(&ctx->aio_mutex);
3948 total_read = ctx->total_len;
3951 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3954 iocb->ki_pos += total_read;
3960 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
3962 return __cifs_readv(iocb, to, true);
3965 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
3967 return __cifs_readv(iocb, to, false);
3971 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
3973 struct inode *inode = file_inode(iocb->ki_filp);
3974 struct cifsInodeInfo *cinode = CIFS_I(inode);
3975 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3976 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3977 iocb->ki_filp->private_data;
3978 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3982 * In strict cache mode we need to read from the server all the time
3983 * if we don't have level II oplock because the server can delay mtime
3984 * change - so we can't make a decision about inode invalidating.
3985 * And we can also fail with pagereading if there are mandatory locks
3986 * on pages affected by this read but not on the region from pos to
3989 if (!CIFS_CACHE_READ(cinode))
3990 return cifs_user_readv(iocb, to);
3992 if (cap_unix(tcon->ses) &&
3993 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3994 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
3995 return generic_file_read_iter(iocb, to);
3998 * We need to hold the sem to be sure nobody modifies lock list
3999 * with a brlock that prevents reading.
4001 down_read(&cinode->lock_sem);
4002 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4003 tcon->ses->server->vals->shared_lock_type,
4004 0, NULL, CIFS_READ_OP))
4005 rc = generic_file_read_iter(iocb, to);
4006 up_read(&cinode->lock_sem);
4011 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4014 unsigned int bytes_read = 0;
4015 unsigned int total_read;
4016 unsigned int current_read_size;
4018 struct cifs_sb_info *cifs_sb;
4019 struct cifs_tcon *tcon;
4020 struct TCP_Server_Info *server;
4023 struct cifsFileInfo *open_file;
4024 struct cifs_io_parms io_parms = {0};
4025 int buf_type = CIFS_NO_BUFFER;
4029 cifs_sb = CIFS_FILE_SB(file);
4031 /* FIXME: set up handlers for larger reads and/or convert to async */
4032 rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
4034 if (file->private_data == NULL) {
4039 open_file = file->private_data;
4040 tcon = tlink_tcon(open_file->tlink);
4041 server = cifs_pick_channel(tcon->ses);
4043 if (!server->ops->sync_read) {
4048 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4049 pid = open_file->pid;
4051 pid = current->tgid;
4053 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4054 cifs_dbg(FYI, "attempting read on write only file instance\n");
4056 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4057 total_read += bytes_read, cur_offset += bytes_read) {
4059 current_read_size = min_t(uint, read_size - total_read,
4062 * For windows me and 9x we do not want to request more
4063 * than it negotiated since it will refuse the read
4066 if (!(tcon->ses->capabilities &
4067 tcon->ses->server->vals->cap_large_files)) {
4068 current_read_size = min_t(uint,
4069 current_read_size, CIFSMaxBufSize);
4071 if (open_file->invalidHandle) {
4072 rc = cifs_reopen_file(open_file, true);
4077 io_parms.tcon = tcon;
4078 io_parms.offset = *offset;
4079 io_parms.length = current_read_size;
4080 io_parms.server = server;
4081 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4082 &bytes_read, &cur_offset,
4084 } while (rc == -EAGAIN);
4086 if (rc || (bytes_read == 0)) {
4094 cifs_stats_bytes_read(tcon, total_read);
4095 *offset += bytes_read;
4103 * If the page is mmap'ed into a process' page tables, then we need to make
4104 * sure that it doesn't change while being written back.
4107 cifs_page_mkwrite(struct vm_fault *vmf)
4109 struct page *page = vmf->page;
4112 return VM_FAULT_LOCKED;
4115 static const struct vm_operations_struct cifs_file_vm_ops = {
4116 .fault = filemap_fault,
4117 .map_pages = filemap_map_pages,
4118 .page_mkwrite = cifs_page_mkwrite,
4121 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4124 struct inode *inode = file_inode(file);
4128 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4129 rc = cifs_zap_mapping(inode);
4131 rc = generic_file_mmap(file, vma);
4133 vma->vm_ops = &cifs_file_vm_ops;
4139 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4145 rc = cifs_revalidate_file(file);
4147 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4150 rc = generic_file_mmap(file, vma);
4152 vma->vm_ops = &cifs_file_vm_ops;
4159 cifs_readv_complete(struct work_struct *work)
4161 unsigned int i, got_bytes;
4162 struct cifs_readdata *rdata = container_of(work,
4163 struct cifs_readdata, work);
4165 got_bytes = rdata->got_bytes;
4166 for (i = 0; i < rdata->nr_pages; i++) {
4167 struct page *page = rdata->pages[i];
4169 lru_cache_add(page);
4171 if (rdata->result == 0 ||
4172 (rdata->result == -EAGAIN && got_bytes)) {
4173 flush_dcache_page(page);
4174 SetPageUptodate(page);
4179 if (rdata->result == 0 ||
4180 (rdata->result == -EAGAIN && got_bytes))
4181 cifs_readpage_to_fscache(rdata->mapping->host, page);
4183 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4186 rdata->pages[i] = NULL;
4188 kref_put(&rdata->refcount, cifs_readdata_release);
4192 readpages_fill_pages(struct TCP_Server_Info *server,
4193 struct cifs_readdata *rdata, struct iov_iter *iter,
4200 unsigned int nr_pages = rdata->nr_pages;
4201 unsigned int page_offset = rdata->page_offset;
4203 /* determine the eof that the server (probably) has */
4204 eof = CIFS_I(rdata->mapping->host)->server_eof;
4205 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4206 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4208 rdata->got_bytes = 0;
4209 rdata->tailsz = PAGE_SIZE;
4210 for (i = 0; i < nr_pages; i++) {
4211 struct page *page = rdata->pages[i];
4212 unsigned int to_read = rdata->pagesz;
4216 to_read -= page_offset;
4222 if (len >= to_read) {
4224 } else if (len > 0) {
4225 /* enough for partial page, fill and zero the rest */
4226 zero_user(page, len + page_offset, to_read - len);
4227 n = rdata->tailsz = len;
4229 } else if (page->index > eof_index) {
4231 * The VFS will not try to do readahead past the
4232 * i_size, but it's possible that we have outstanding
4233 * writes with gaps in the middle and the i_size hasn't
4234 * caught up yet. Populate those with zeroed out pages
4235 * to prevent the VFS from repeatedly attempting to
4236 * fill them until the writes are flushed.
4238 zero_user(page, 0, PAGE_SIZE);
4239 lru_cache_add(page);
4240 flush_dcache_page(page);
4241 SetPageUptodate(page);
4244 rdata->pages[i] = NULL;
4248 /* no need to hold page hostage */
4249 lru_cache_add(page);
4252 rdata->pages[i] = NULL;
4258 result = copy_page_from_iter(
4259 page, page_offset, n, iter);
4260 #ifdef CONFIG_CIFS_SMB_DIRECT
4265 result = cifs_read_page_from_socket(
4266 server, page, page_offset, n);
4270 rdata->got_bytes += result;
4273 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4274 rdata->got_bytes : result;
4278 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4279 struct cifs_readdata *rdata, unsigned int len)
4281 return readpages_fill_pages(server, rdata, NULL, len);
4285 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4286 struct cifs_readdata *rdata,
4287 struct iov_iter *iter)
4289 return readpages_fill_pages(server, rdata, iter, iter->count);
4293 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4294 unsigned int rsize, struct list_head *tmplist,
4295 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4297 struct page *page, *tpage;
4298 unsigned int expected_index;
4300 gfp_t gfp = readahead_gfp_mask(mapping);
4302 INIT_LIST_HEAD(tmplist);
4304 page = lru_to_page(page_list);
4307 * Lock the page and put it in the cache. Since no one else
4308 * should have access to this page, we're safe to simply set
4309 * PG_locked without checking it first.
4311 __SetPageLocked(page);
4312 rc = add_to_page_cache_locked(page, mapping,
4315 /* give up if we can't stick it in the cache */
4317 __ClearPageLocked(page);
4321 /* move first page to the tmplist */
4322 *offset = (loff_t)page->index << PAGE_SHIFT;
4325 list_move_tail(&page->lru, tmplist);
4327 /* now try and add more pages onto the request */
4328 expected_index = page->index + 1;
4329 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4330 /* discontinuity ? */
4331 if (page->index != expected_index)
4334 /* would this page push the read over the rsize? */
4335 if (*bytes + PAGE_SIZE > rsize)
4338 __SetPageLocked(page);
4339 if (add_to_page_cache_locked(page, mapping, page->index, gfp)) {
4340 __ClearPageLocked(page);
4343 list_move_tail(&page->lru, tmplist);
4344 (*bytes) += PAGE_SIZE;
4351 static int cifs_readpages(struct file *file, struct address_space *mapping,
4352 struct list_head *page_list, unsigned num_pages)
4355 struct list_head tmplist;
4356 struct cifsFileInfo *open_file = file->private_data;
4357 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4358 struct TCP_Server_Info *server;
4364 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4365 * immediately if the cookie is negative
4367 * After this point, every page in the list might have PG_fscache set,
4368 * so we will need to clean that up off of every page we don't use.
4370 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4377 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4378 pid = open_file->pid;
4380 pid = current->tgid;
4383 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4385 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4386 __func__, file, mapping, num_pages);
4389 * Start with the page at end of list and move it to private
4390 * list. Do the same with any following pages until we hit
4391 * the rsize limit, hit an index discontinuity, or run out of
4392 * pages. Issue the async read and then start the loop again
4393 * until the list is empty.
4395 * Note that list order is important. The page_list is in
4396 * the order of declining indexes. When we put the pages in
4397 * the rdata->pages, then we want them in increasing order.
4399 while (!list_empty(page_list)) {
4400 unsigned int i, nr_pages, bytes, rsize;
4402 struct page *page, *tpage;
4403 struct cifs_readdata *rdata;
4404 struct cifs_credits credits_on_stack;
4405 struct cifs_credits *credits = &credits_on_stack;
4407 if (open_file->invalidHandle) {
4408 rc = cifs_reopen_file(open_file, true);
4415 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
4421 * Give up immediately if rsize is too small to read an entire
4422 * page. The VFS will fall back to readpage. We should never
4423 * reach this point however since we set ra_pages to 0 when the
4424 * rsize is smaller than a cache page.
4426 if (unlikely(rsize < PAGE_SIZE)) {
4427 add_credits_and_wake_if(server, credits, 0);
4432 rc = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4433 &nr_pages, &offset, &bytes);
4435 add_credits_and_wake_if(server, credits, 0);
4439 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4441 /* best to give up if we're out of mem */
4442 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4443 list_del(&page->lru);
4444 lru_cache_add(page);
4449 add_credits_and_wake_if(server, credits, 0);
4453 rdata->cfile = cifsFileInfo_get(open_file);
4454 rdata->server = server;
4455 rdata->mapping = mapping;
4456 rdata->offset = offset;
4457 rdata->bytes = bytes;
4459 rdata->pagesz = PAGE_SIZE;
4460 rdata->tailsz = PAGE_SIZE;
4461 rdata->read_into_pages = cifs_readpages_read_into_pages;
4462 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4463 rdata->credits = credits_on_stack;
4465 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4466 list_del(&page->lru);
4467 rdata->pages[rdata->nr_pages++] = page;
4470 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4473 if (rdata->cfile->invalidHandle)
4476 rc = server->ops->async_readv(rdata);
4480 add_credits_and_wake_if(server, &rdata->credits, 0);
4481 for (i = 0; i < rdata->nr_pages; i++) {
4482 page = rdata->pages[i];
4483 lru_cache_add(page);
4487 /* Fallback to the readpage in error/reconnect cases */
4488 kref_put(&rdata->refcount, cifs_readdata_release);
4492 kref_put(&rdata->refcount, cifs_readdata_release);
4495 /* Any pages that have been shown to fscache but didn't get added to
4496 * the pagecache must be uncached before they get returned to the
4499 cifs_fscache_readpages_cancel(mapping->host, page_list);
4505 * cifs_readpage_worker must be called with the page pinned
4507 static int cifs_readpage_worker(struct file *file, struct page *page,
4513 /* Is the page cached? */
4514 rc = cifs_readpage_from_fscache(file_inode(file), page);
4518 read_data = kmap(page);
4519 /* for reads over a certain size could initiate async read ahead */
4521 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4526 cifs_dbg(FYI, "Bytes read %d\n", rc);
4528 /* we do not want atime to be less than mtime, it broke some apps */
4529 file_inode(file)->i_atime = current_time(file_inode(file));
4530 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4531 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4533 file_inode(file)->i_atime = current_time(file_inode(file));
4536 memset(read_data + rc, 0, PAGE_SIZE - rc);
4538 flush_dcache_page(page);
4539 SetPageUptodate(page);
4541 /* send this page to the cache */
4542 cifs_readpage_to_fscache(file_inode(file), page);
4554 static int cifs_readpage(struct file *file, struct page *page)
4556 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
4562 if (file->private_data == NULL) {
4568 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4569 page, (int)offset, (int)offset);
4571 rc = cifs_readpage_worker(file, page, &offset);
4577 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4579 struct cifsFileInfo *open_file;
4581 spin_lock(&cifs_inode->open_file_lock);
4582 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4583 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4584 spin_unlock(&cifs_inode->open_file_lock);
4588 spin_unlock(&cifs_inode->open_file_lock);
4592 /* We do not want to update the file size from server for inodes
4593 open for write - to avoid races with writepage extending
4594 the file - in the future we could consider allowing
4595 refreshing the inode only on increases in the file size
4596 but this is tricky to do without racing with writebehind
4597 page caching in the current Linux kernel design */
4598 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4603 if (is_inode_writable(cifsInode)) {
4604 /* This inode is open for write at least once */
4605 struct cifs_sb_info *cifs_sb;
4607 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4608 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4609 /* since no page cache to corrupt on directio
4610 we can change size safely */
4614 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4622 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4623 loff_t pos, unsigned len, unsigned flags,
4624 struct page **pagep, void **fsdata)
4627 pgoff_t index = pos >> PAGE_SHIFT;
4628 loff_t offset = pos & (PAGE_SIZE - 1);
4629 loff_t page_start = pos & PAGE_MASK;
4634 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4637 page = grab_cache_page_write_begin(mapping, index, flags);
4643 if (PageUptodate(page))
4647 * If we write a full page it will be up to date, no need to read from
4648 * the server. If the write is short, we'll end up doing a sync write
4651 if (len == PAGE_SIZE)
4655 * optimize away the read when we have an oplock, and we're not
4656 * expecting to use any of the data we'd be reading in. That
4657 * is, when the page lies beyond the EOF, or straddles the EOF
4658 * and the write will cover all of the existing data.
4660 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4661 i_size = i_size_read(mapping->host);
4662 if (page_start >= i_size ||
4663 (offset == 0 && (pos + len) >= i_size)) {
4664 zero_user_segments(page, 0, offset,
4668 * PageChecked means that the parts of the page
4669 * to which we're not writing are considered up
4670 * to date. Once the data is copied to the
4671 * page, it can be set uptodate.
4673 SetPageChecked(page);
4678 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4680 * might as well read a page, it is fast enough. If we get
4681 * an error, we don't need to return it. cifs_write_end will
4682 * do a sync write instead since PG_uptodate isn't set.
4684 cifs_readpage_worker(file, page, &page_start);
4689 /* we could try using another file handle if there is one -
4690 but how would we lock it to prevent close of that handle
4691 racing with this read? In any case
4692 this will be written out by write_end so is fine */
4699 static int cifs_release_page(struct page *page, gfp_t gfp)
4701 if (PagePrivate(page))
4704 return cifs_fscache_release_page(page, gfp);
4707 static void cifs_invalidate_page(struct page *page, unsigned int offset,
4708 unsigned int length)
4710 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4712 if (offset == 0 && length == PAGE_SIZE)
4713 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4716 static int cifs_launder_page(struct page *page)
4719 loff_t range_start = page_offset(page);
4720 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
4721 struct writeback_control wbc = {
4722 .sync_mode = WB_SYNC_ALL,
4724 .range_start = range_start,
4725 .range_end = range_end,
4728 cifs_dbg(FYI, "Launder page: %p\n", page);
4730 if (clear_page_dirty_for_io(page))
4731 rc = cifs_writepage_locked(page, &wbc);
4733 cifs_fscache_invalidate_page(page, page->mapping->host);
4737 void cifs_oplock_break(struct work_struct *work)
4739 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4741 struct inode *inode = d_inode(cfile->dentry);
4742 struct cifsInodeInfo *cinode = CIFS_I(inode);
4743 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4744 struct TCP_Server_Info *server = tcon->ses->server;
4746 bool purge_cache = false;
4748 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4749 TASK_UNINTERRUPTIBLE);
4751 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4752 cfile->oplock_epoch, &purge_cache);
4754 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4755 cifs_has_mand_locks(cinode)) {
4756 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4761 if (inode && S_ISREG(inode->i_mode)) {
4762 if (CIFS_CACHE_READ(cinode))
4763 break_lease(inode, O_RDONLY);
4765 break_lease(inode, O_WRONLY);
4766 rc = filemap_fdatawrite(inode->i_mapping);
4767 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4768 rc = filemap_fdatawait(inode->i_mapping);
4769 mapping_set_error(inode->i_mapping, rc);
4770 cifs_zap_mapping(inode);
4772 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4773 if (CIFS_CACHE_WRITE(cinode))
4774 goto oplock_break_ack;
4777 rc = cifs_push_locks(cfile);
4779 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4783 * releasing stale oplock after recent reconnect of smb session using
4784 * a now incorrect file handle is not a data integrity issue but do
4785 * not bother sending an oplock release if session to server still is
4786 * disconnected since oplock already released by the server
4788 if (!cfile->oplock_break_cancelled) {
4789 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4791 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4793 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4794 cifs_done_oplock_break(cinode);
4798 * The presence of cifs_direct_io() in the address space ops vector
4799 * allowes open() O_DIRECT flags which would have failed otherwise.
4801 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4802 * so this method should never be called.
4804 * Direct IO is not yet supported in the cached mode.
4807 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4811 * Eventually need to support direct IO for non forcedirectio mounts
4816 static int cifs_swap_activate(struct swap_info_struct *sis,
4817 struct file *swap_file, sector_t *span)
4819 struct cifsFileInfo *cfile = swap_file->private_data;
4820 struct inode *inode = swap_file->f_mapping->host;
4821 unsigned long blocks;
4824 cifs_dbg(FYI, "swap activate\n");
4826 spin_lock(&inode->i_lock);
4827 blocks = inode->i_blocks;
4828 isize = inode->i_size;
4829 spin_unlock(&inode->i_lock);
4830 if (blocks*512 < isize) {
4831 pr_warn("swap activate: swapfile has holes\n");
4836 pr_warn_once("Swap support over SMB3 is experimental\n");
4839 * TODO: consider adding ACL (or documenting how) to prevent other
4840 * users (on this or other systems) from reading it
4844 /* TODO: add sk_set_memalloc(inet) or similar */
4847 cfile->swapfile = true;
4849 * TODO: Since file already open, we can't open with DENY_ALL here
4850 * but we could add call to grab a byte range lock to prevent others
4851 * from reading or writing the file
4857 static void cifs_swap_deactivate(struct file *file)
4859 struct cifsFileInfo *cfile = file->private_data;
4861 cifs_dbg(FYI, "swap deactivate\n");
4863 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
4866 cfile->swapfile = false;
4868 /* do we need to unpin (or unlock) the file */
4871 const struct address_space_operations cifs_addr_ops = {
4872 .readpage = cifs_readpage,
4873 .readpages = cifs_readpages,
4874 .writepage = cifs_writepage,
4875 .writepages = cifs_writepages,
4876 .write_begin = cifs_write_begin,
4877 .write_end = cifs_write_end,
4878 .set_page_dirty = __set_page_dirty_nobuffers,
4879 .releasepage = cifs_release_page,
4880 .direct_IO = cifs_direct_io,
4881 .invalidatepage = cifs_invalidate_page,
4882 .launder_page = cifs_launder_page,
4884 * TODO: investigate and if useful we could add an cifs_migratePage
4885 * helper (under an CONFIG_MIGRATION) in the future, and also
4886 * investigate and add an is_dirty_writeback helper if needed
4888 .swap_activate = cifs_swap_activate,
4889 .swap_deactivate = cifs_swap_deactivate,
4893 * cifs_readpages requires the server to support a buffer large enough to
4894 * contain the header plus one complete page of data. Otherwise, we need
4895 * to leave cifs_readpages out of the address space operations.
4897 const struct address_space_operations cifs_addr_ops_smallbuf = {
4898 .readpage = cifs_readpage,
4899 .writepage = cifs_writepage,
4900 .writepages = cifs_writepages,
4901 .write_begin = cifs_write_begin,
4902 .write_end = cifs_write_end,
4903 .set_page_dirty = __set_page_dirty_nobuffers,
4904 .releasepage = cifs_release_page,
4905 .invalidatepage = cifs_invalidate_page,
4906 .launder_page = cifs_launder_page,