4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/backing-dev.h>
26 #include <linux/stat.h>
27 #include <linux/fcntl.h>
28 #include <linux/pagemap.h>
29 #include <linux/pagevec.h>
30 #include <linux/writeback.h>
31 #include <linux/task_io_accounting_ops.h>
32 #include <linux/delay.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
37 #include <asm/div64.h>
41 #include "cifsproto.h"
42 #include "cifs_unicode.h"
43 #include "cifs_debug.h"
44 #include "cifs_fs_sb.h"
46 #include "smbdirect.h"
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 the error occurs while setting the lock;
1153 * 2) 0, if we set the lock and don't need to request to the server;
1154 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1155 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1158 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1160 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1161 int rc = FILE_LOCK_DEFERRED + 1;
1163 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);
1178 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1181 int rc = 0, stored_rc;
1182 struct cifsLockInfo *li, *tmp;
1183 struct cifs_tcon *tcon;
1184 unsigned int num, max_num, max_buf;
1185 LOCKING_ANDX_RANGE *buf, *cur;
1186 static const int types[] = {
1187 LOCKING_ANDX_LARGE_FILES,
1188 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1193 tcon = tlink_tcon(cfile->tlink);
1196 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1197 * and check it before using.
1199 max_buf = tcon->ses->server->maxBuf;
1200 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1205 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1207 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1209 max_num = (max_buf - sizeof(struct smb_hdr)) /
1210 sizeof(LOCKING_ANDX_RANGE);
1211 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1217 for (i = 0; i < 2; i++) {
1220 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1221 if (li->type != types[i])
1223 cur->Pid = cpu_to_le16(li->pid);
1224 cur->LengthLow = cpu_to_le32((u32)li->length);
1225 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1226 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1227 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1228 if (++num == max_num) {
1229 stored_rc = cifs_lockv(xid, tcon,
1231 (__u8)li->type, 0, num,
1242 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1243 (__u8)types[i], 0, num, buf);
1255 hash_lockowner(fl_owner_t owner)
1257 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1260 struct lock_to_push {
1261 struct list_head llist;
1270 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1272 struct inode *inode = d_inode(cfile->dentry);
1273 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1274 struct file_lock *flock;
1275 struct file_lock_context *flctx = inode->i_flctx;
1276 unsigned int count = 0, i;
1277 int rc = 0, xid, type;
1278 struct list_head locks_to_send, *el;
1279 struct lock_to_push *lck, *tmp;
1287 spin_lock(&flctx->flc_lock);
1288 list_for_each(el, &flctx->flc_posix) {
1291 spin_unlock(&flctx->flc_lock);
1293 INIT_LIST_HEAD(&locks_to_send);
1296 * Allocating count locks is enough because no FL_POSIX locks can be
1297 * added to the list while we are holding cinode->lock_sem that
1298 * protects locking operations of this inode.
1300 for (i = 0; i < count; i++) {
1301 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1306 list_add_tail(&lck->llist, &locks_to_send);
1309 el = locks_to_send.next;
1310 spin_lock(&flctx->flc_lock);
1311 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1312 if (el == &locks_to_send) {
1314 * The list ended. We don't have enough allocated
1315 * structures - something is really wrong.
1317 cifs_dbg(VFS, "Can't push all brlocks!\n");
1320 length = 1 + flock->fl_end - flock->fl_start;
1321 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1325 lck = list_entry(el, struct lock_to_push, llist);
1326 lck->pid = hash_lockowner(flock->fl_owner);
1327 lck->netfid = cfile->fid.netfid;
1328 lck->length = length;
1330 lck->offset = flock->fl_start;
1332 spin_unlock(&flctx->flc_lock);
1334 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1337 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1338 lck->offset, lck->length, NULL,
1342 list_del(&lck->llist);
1350 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1351 list_del(&lck->llist);
1358 cifs_push_locks(struct cifsFileInfo *cfile)
1360 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1361 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1362 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1365 /* we are going to update can_cache_brlcks here - need a write access */
1366 cifs_down_write(&cinode->lock_sem);
1367 if (!cinode->can_cache_brlcks) {
1368 up_write(&cinode->lock_sem);
1372 if (cap_unix(tcon->ses) &&
1373 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1374 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1375 rc = cifs_push_posix_locks(cfile);
1377 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1379 cinode->can_cache_brlcks = false;
1380 up_write(&cinode->lock_sem);
1385 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1386 bool *wait_flag, struct TCP_Server_Info *server)
1388 if (flock->fl_flags & FL_POSIX)
1389 cifs_dbg(FYI, "Posix\n");
1390 if (flock->fl_flags & FL_FLOCK)
1391 cifs_dbg(FYI, "Flock\n");
1392 if (flock->fl_flags & FL_SLEEP) {
1393 cifs_dbg(FYI, "Blocking lock\n");
1396 if (flock->fl_flags & FL_ACCESS)
1397 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1398 if (flock->fl_flags & FL_LEASE)
1399 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1400 if (flock->fl_flags &
1401 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1402 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1403 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1405 *type = server->vals->large_lock_type;
1406 if (flock->fl_type == F_WRLCK) {
1407 cifs_dbg(FYI, "F_WRLCK\n");
1408 *type |= server->vals->exclusive_lock_type;
1410 } else if (flock->fl_type == F_UNLCK) {
1411 cifs_dbg(FYI, "F_UNLCK\n");
1412 *type |= server->vals->unlock_lock_type;
1414 /* Check if unlock includes more than one lock range */
1415 } else if (flock->fl_type == F_RDLCK) {
1416 cifs_dbg(FYI, "F_RDLCK\n");
1417 *type |= server->vals->shared_lock_type;
1419 } else if (flock->fl_type == F_EXLCK) {
1420 cifs_dbg(FYI, "F_EXLCK\n");
1421 *type |= server->vals->exclusive_lock_type;
1423 } else if (flock->fl_type == F_SHLCK) {
1424 cifs_dbg(FYI, "F_SHLCK\n");
1425 *type |= server->vals->shared_lock_type;
1428 cifs_dbg(FYI, "Unknown type of lock\n");
1432 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1433 bool wait_flag, bool posix_lck, unsigned int xid)
1436 __u64 length = 1 + flock->fl_end - flock->fl_start;
1437 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1438 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1439 struct TCP_Server_Info *server = tcon->ses->server;
1440 __u16 netfid = cfile->fid.netfid;
1443 int posix_lock_type;
1445 rc = cifs_posix_lock_test(file, flock);
1449 if (type & server->vals->shared_lock_type)
1450 posix_lock_type = CIFS_RDLCK;
1452 posix_lock_type = CIFS_WRLCK;
1453 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1454 hash_lockowner(flock->fl_owner),
1455 flock->fl_start, length, flock,
1456 posix_lock_type, wait_flag);
1460 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1464 /* BB we could chain these into one lock request BB */
1465 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1468 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1470 flock->fl_type = F_UNLCK;
1472 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1477 if (type & server->vals->shared_lock_type) {
1478 flock->fl_type = F_WRLCK;
1482 type &= ~server->vals->exclusive_lock_type;
1484 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1485 type | server->vals->shared_lock_type,
1488 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1489 type | server->vals->shared_lock_type, 0, 1, false);
1490 flock->fl_type = F_RDLCK;
1492 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1495 flock->fl_type = F_WRLCK;
1501 cifs_move_llist(struct list_head *source, struct list_head *dest)
1503 struct list_head *li, *tmp;
1504 list_for_each_safe(li, tmp, source)
1505 list_move(li, dest);
1509 cifs_free_llist(struct list_head *llist)
1511 struct cifsLockInfo *li, *tmp;
1512 list_for_each_entry_safe(li, tmp, llist, llist) {
1513 cifs_del_lock_waiters(li);
1514 list_del(&li->llist);
1520 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1523 int rc = 0, stored_rc;
1524 static const int types[] = {
1525 LOCKING_ANDX_LARGE_FILES,
1526 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1529 unsigned int max_num, num, max_buf;
1530 LOCKING_ANDX_RANGE *buf, *cur;
1531 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1532 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1533 struct cifsLockInfo *li, *tmp;
1534 __u64 length = 1 + flock->fl_end - flock->fl_start;
1535 struct list_head tmp_llist;
1537 INIT_LIST_HEAD(&tmp_llist);
1540 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1541 * and check it before using.
1543 max_buf = tcon->ses->server->maxBuf;
1544 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1547 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1549 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1551 max_num = (max_buf - sizeof(struct smb_hdr)) /
1552 sizeof(LOCKING_ANDX_RANGE);
1553 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1557 cifs_down_write(&cinode->lock_sem);
1558 for (i = 0; i < 2; i++) {
1561 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1562 if (flock->fl_start > li->offset ||
1563 (flock->fl_start + length) <
1564 (li->offset + li->length))
1566 if (current->tgid != li->pid)
1568 if (types[i] != li->type)
1570 if (cinode->can_cache_brlcks) {
1572 * We can cache brlock requests - simply remove
1573 * a lock from the file's list.
1575 list_del(&li->llist);
1576 cifs_del_lock_waiters(li);
1580 cur->Pid = cpu_to_le16(li->pid);
1581 cur->LengthLow = cpu_to_le32((u32)li->length);
1582 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1583 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1584 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1586 * We need to save a lock here to let us add it again to
1587 * the file's list if the unlock range request fails on
1590 list_move(&li->llist, &tmp_llist);
1591 if (++num == max_num) {
1592 stored_rc = cifs_lockv(xid, tcon,
1594 li->type, num, 0, buf);
1597 * We failed on the unlock range
1598 * request - add all locks from the tmp
1599 * list to the head of the file's list.
1601 cifs_move_llist(&tmp_llist,
1602 &cfile->llist->locks);
1606 * The unlock range request succeed -
1607 * free the tmp list.
1609 cifs_free_llist(&tmp_llist);
1616 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1617 types[i], num, 0, buf);
1619 cifs_move_llist(&tmp_llist,
1620 &cfile->llist->locks);
1623 cifs_free_llist(&tmp_llist);
1627 up_write(&cinode->lock_sem);
1633 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1634 bool wait_flag, bool posix_lck, int lock, int unlock,
1638 __u64 length = 1 + flock->fl_end - flock->fl_start;
1639 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1640 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1641 struct TCP_Server_Info *server = tcon->ses->server;
1642 struct inode *inode = d_inode(cfile->dentry);
1645 int posix_lock_type;
1647 rc = cifs_posix_lock_set(file, flock);
1648 if (rc <= FILE_LOCK_DEFERRED)
1651 if (type & server->vals->shared_lock_type)
1652 posix_lock_type = CIFS_RDLCK;
1654 posix_lock_type = CIFS_WRLCK;
1657 posix_lock_type = CIFS_UNLCK;
1659 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1660 hash_lockowner(flock->fl_owner),
1661 flock->fl_start, length,
1662 NULL, posix_lock_type, wait_flag);
1667 struct cifsLockInfo *lock;
1669 lock = cifs_lock_init(flock->fl_start, length, type,
1674 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1683 * Windows 7 server can delay breaking lease from read to None
1684 * if we set a byte-range lock on a file - break it explicitly
1685 * before sending the lock to the server to be sure the next
1686 * read won't conflict with non-overlapted locks due to
1689 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1690 CIFS_CACHE_READ(CIFS_I(inode))) {
1691 cifs_zap_mapping(inode);
1692 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1694 CIFS_I(inode)->oplock = 0;
1697 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1698 type, 1, 0, wait_flag);
1704 cifs_lock_add(cfile, lock);
1706 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1709 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1711 * If this is a request to remove all locks because we
1712 * are closing the file, it doesn't matter if the
1713 * unlocking failed as both cifs.ko and the SMB server
1714 * remove the lock on file close
1717 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1718 if (!(flock->fl_flags & FL_CLOSE))
1721 rc = locks_lock_file_wait(file, flock);
1726 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1729 int lock = 0, unlock = 0;
1730 bool wait_flag = false;
1731 bool posix_lck = false;
1732 struct cifs_sb_info *cifs_sb;
1733 struct cifs_tcon *tcon;
1734 struct cifsFileInfo *cfile;
1740 if (!(fl->fl_flags & FL_FLOCK))
1743 cfile = (struct cifsFileInfo *)file->private_data;
1744 tcon = tlink_tcon(cfile->tlink);
1746 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
1748 cifs_sb = CIFS_FILE_SB(file);
1750 if (cap_unix(tcon->ses) &&
1751 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1752 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1755 if (!lock && !unlock) {
1757 * if no lock or unlock then nothing to do since we do not
1764 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1772 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1775 int lock = 0, unlock = 0;
1776 bool wait_flag = false;
1777 bool posix_lck = false;
1778 struct cifs_sb_info *cifs_sb;
1779 struct cifs_tcon *tcon;
1780 struct cifsFileInfo *cfile;
1786 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1787 cmd, flock->fl_flags, flock->fl_type,
1788 flock->fl_start, flock->fl_end);
1790 cfile = (struct cifsFileInfo *)file->private_data;
1791 tcon = tlink_tcon(cfile->tlink);
1793 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1795 cifs_sb = CIFS_FILE_SB(file);
1797 if (cap_unix(tcon->ses) &&
1798 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1799 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1802 * BB add code here to normalize offset and length to account for
1803 * negative length which we can not accept over the wire.
1805 if (IS_GETLK(cmd)) {
1806 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1811 if (!lock && !unlock) {
1813 * if no lock or unlock then nothing to do since we do not
1820 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1827 * update the file size (if needed) after a write. Should be called with
1828 * the inode->i_lock held
1831 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1832 unsigned int bytes_written)
1834 loff_t end_of_write = offset + bytes_written;
1836 if (end_of_write > cifsi->server_eof)
1837 cifsi->server_eof = end_of_write;
1841 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1842 size_t write_size, loff_t *offset)
1845 unsigned int bytes_written = 0;
1846 unsigned int total_written;
1847 struct cifs_tcon *tcon;
1848 struct TCP_Server_Info *server;
1850 struct dentry *dentry = open_file->dentry;
1851 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
1852 struct cifs_io_parms io_parms = {0};
1854 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1855 write_size, *offset, dentry);
1857 tcon = tlink_tcon(open_file->tlink);
1858 server = tcon->ses->server;
1860 if (!server->ops->sync_write)
1865 for (total_written = 0; write_size > total_written;
1866 total_written += bytes_written) {
1868 while (rc == -EAGAIN) {
1872 if (open_file->invalidHandle) {
1873 /* we could deadlock if we called
1874 filemap_fdatawait from here so tell
1875 reopen_file not to flush data to
1877 rc = cifs_reopen_file(open_file, false);
1882 len = min(server->ops->wp_retry_size(d_inode(dentry)),
1883 (unsigned int)write_size - total_written);
1884 /* iov[0] is reserved for smb header */
1885 iov[1].iov_base = (char *)write_data + total_written;
1886 iov[1].iov_len = len;
1888 io_parms.tcon = tcon;
1889 io_parms.offset = *offset;
1890 io_parms.length = len;
1891 rc = server->ops->sync_write(xid, &open_file->fid,
1892 &io_parms, &bytes_written, iov, 1);
1894 if (rc || (bytes_written == 0)) {
1902 spin_lock(&d_inode(dentry)->i_lock);
1903 cifs_update_eof(cifsi, *offset, bytes_written);
1904 spin_unlock(&d_inode(dentry)->i_lock);
1905 *offset += bytes_written;
1909 cifs_stats_bytes_written(tcon, total_written);
1911 if (total_written > 0) {
1912 spin_lock(&d_inode(dentry)->i_lock);
1913 if (*offset > d_inode(dentry)->i_size)
1914 i_size_write(d_inode(dentry), *offset);
1915 spin_unlock(&d_inode(dentry)->i_lock);
1917 mark_inode_dirty_sync(d_inode(dentry));
1919 return total_written;
1922 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1925 struct cifsFileInfo *open_file = NULL;
1926 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1928 /* only filter by fsuid on multiuser mounts */
1929 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1932 spin_lock(&cifs_inode->open_file_lock);
1933 /* we could simply get the first_list_entry since write-only entries
1934 are always at the end of the list but since the first entry might
1935 have a close pending, we go through the whole list */
1936 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1937 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1939 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
1940 if (!open_file->invalidHandle) {
1941 /* found a good file */
1942 /* lock it so it will not be closed on us */
1943 cifsFileInfo_get(open_file);
1944 spin_unlock(&cifs_inode->open_file_lock);
1946 } /* else might as well continue, and look for
1947 another, or simply have the caller reopen it
1948 again rather than trying to fix this handle */
1949 } else /* write only file */
1950 break; /* write only files are last so must be done */
1952 spin_unlock(&cifs_inode->open_file_lock);
1956 /* Return -EBADF if no handle is found and general rc otherwise */
1958 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
1959 struct cifsFileInfo **ret_file)
1961 struct cifsFileInfo *open_file, *inv_file = NULL;
1962 struct cifs_sb_info *cifs_sb;
1963 bool any_available = false;
1965 unsigned int refind = 0;
1966 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
1967 bool with_delete = flags & FIND_WR_WITH_DELETE;
1971 * Having a null inode here (because mapping->host was set to zero by
1972 * the VFS or MM) should not happen but we had reports of on oops (due
1973 * to it being zero) during stress testcases so we need to check for it
1976 if (cifs_inode == NULL) {
1977 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
1982 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1984 /* only filter by fsuid on multiuser mounts */
1985 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1988 spin_lock(&cifs_inode->open_file_lock);
1990 if (refind > MAX_REOPEN_ATT) {
1991 spin_unlock(&cifs_inode->open_file_lock);
1994 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1995 if (!any_available && open_file->pid != current->tgid)
1997 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1999 if (with_delete && !(open_file->fid.access & DELETE))
2001 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2002 if (!open_file->invalidHandle) {
2003 /* found a good writable file */
2004 cifsFileInfo_get(open_file);
2005 spin_unlock(&cifs_inode->open_file_lock);
2006 *ret_file = open_file;
2010 inv_file = open_file;
2014 /* couldn't find useable FH with same pid, try any available */
2015 if (!any_available) {
2016 any_available = true;
2017 goto refind_writable;
2021 any_available = false;
2022 cifsFileInfo_get(inv_file);
2025 spin_unlock(&cifs_inode->open_file_lock);
2028 rc = cifs_reopen_file(inv_file, false);
2030 *ret_file = inv_file;
2034 spin_lock(&cifs_inode->open_file_lock);
2035 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2036 spin_unlock(&cifs_inode->open_file_lock);
2037 cifsFileInfo_put(inv_file);
2040 spin_lock(&cifs_inode->open_file_lock);
2041 goto refind_writable;
2047 struct cifsFileInfo *
2048 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2050 struct cifsFileInfo *cfile;
2053 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2055 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2061 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2063 struct cifsFileInfo **ret_file)
2065 struct list_head *tmp;
2066 struct cifsFileInfo *cfile;
2067 struct cifsInodeInfo *cinode;
2072 spin_lock(&tcon->open_file_lock);
2073 list_for_each(tmp, &tcon->openFileList) {
2074 cfile = list_entry(tmp, struct cifsFileInfo,
2076 full_path = build_path_from_dentry(cfile->dentry);
2077 if (full_path == NULL) {
2078 spin_unlock(&tcon->open_file_lock);
2081 if (strcmp(full_path, name)) {
2087 cinode = CIFS_I(d_inode(cfile->dentry));
2088 spin_unlock(&tcon->open_file_lock);
2089 return cifs_get_writable_file(cinode, flags, ret_file);
2092 spin_unlock(&tcon->open_file_lock);
2097 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2098 struct cifsFileInfo **ret_file)
2100 struct list_head *tmp;
2101 struct cifsFileInfo *cfile;
2102 struct cifsInodeInfo *cinode;
2107 spin_lock(&tcon->open_file_lock);
2108 list_for_each(tmp, &tcon->openFileList) {
2109 cfile = list_entry(tmp, struct cifsFileInfo,
2111 full_path = build_path_from_dentry(cfile->dentry);
2112 if (full_path == NULL) {
2113 spin_unlock(&tcon->open_file_lock);
2116 if (strcmp(full_path, name)) {
2122 cinode = CIFS_I(d_inode(cfile->dentry));
2123 spin_unlock(&tcon->open_file_lock);
2124 *ret_file = find_readable_file(cinode, 0);
2125 return *ret_file ? 0 : -ENOENT;
2128 spin_unlock(&tcon->open_file_lock);
2132 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2134 struct address_space *mapping = page->mapping;
2135 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2138 int bytes_written = 0;
2139 struct inode *inode;
2140 struct cifsFileInfo *open_file;
2142 if (!mapping || !mapping->host)
2145 inode = page->mapping->host;
2147 offset += (loff_t)from;
2148 write_data = kmap(page);
2151 if ((to > PAGE_SIZE) || (from > to)) {
2156 /* racing with truncate? */
2157 if (offset > mapping->host->i_size) {
2159 return 0; /* don't care */
2162 /* check to make sure that we are not extending the file */
2163 if (mapping->host->i_size - offset < (loff_t)to)
2164 to = (unsigned)(mapping->host->i_size - offset);
2166 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2169 bytes_written = cifs_write(open_file, open_file->pid,
2170 write_data, to - from, &offset);
2171 cifsFileInfo_put(open_file);
2172 /* Does mm or vfs already set times? */
2173 inode->i_atime = inode->i_mtime = current_time(inode);
2174 if ((bytes_written > 0) && (offset))
2176 else if (bytes_written < 0)
2181 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2182 if (!is_retryable_error(rc))
2190 static struct cifs_writedata *
2191 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2192 pgoff_t end, pgoff_t *index,
2193 unsigned int *found_pages)
2195 struct cifs_writedata *wdata;
2197 wdata = cifs_writedata_alloc((unsigned int)tofind,
2198 cifs_writev_complete);
2202 *found_pages = find_get_pages_range_tag(mapping, index, end,
2203 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2208 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2209 struct address_space *mapping,
2210 struct writeback_control *wbc,
2211 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2213 unsigned int nr_pages = 0, i;
2216 for (i = 0; i < found_pages; i++) {
2217 page = wdata->pages[i];
2219 * At this point we hold neither the i_pages lock nor the
2220 * page lock: the page may be truncated or invalidated
2221 * (changing page->mapping to NULL), or even swizzled
2222 * back from swapper_space to tmpfs file mapping
2227 else if (!trylock_page(page))
2230 if (unlikely(page->mapping != mapping)) {
2235 if (!wbc->range_cyclic && page->index > end) {
2241 if (*next && (page->index != *next)) {
2242 /* Not next consecutive page */
2247 if (wbc->sync_mode != WB_SYNC_NONE)
2248 wait_on_page_writeback(page);
2250 if (PageWriteback(page) ||
2251 !clear_page_dirty_for_io(page)) {
2257 * This actually clears the dirty bit in the radix tree.
2258 * See cifs_writepage() for more commentary.
2260 set_page_writeback(page);
2261 if (page_offset(page) >= i_size_read(mapping->host)) {
2264 end_page_writeback(page);
2268 wdata->pages[i] = page;
2269 *next = page->index + 1;
2273 /* reset index to refind any pages skipped */
2275 *index = wdata->pages[0]->index + 1;
2277 /* put any pages we aren't going to use */
2278 for (i = nr_pages; i < found_pages; i++) {
2279 put_page(wdata->pages[i]);
2280 wdata->pages[i] = NULL;
2287 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2288 struct address_space *mapping, struct writeback_control *wbc)
2292 wdata->sync_mode = wbc->sync_mode;
2293 wdata->nr_pages = nr_pages;
2294 wdata->offset = page_offset(wdata->pages[0]);
2295 wdata->pagesz = PAGE_SIZE;
2296 wdata->tailsz = min(i_size_read(mapping->host) -
2297 page_offset(wdata->pages[nr_pages - 1]),
2299 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2300 wdata->pid = wdata->cfile->pid;
2302 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2306 if (wdata->cfile->invalidHandle)
2309 rc = wdata->server->ops->async_writev(wdata,
2310 cifs_writedata_release);
2315 static int cifs_writepages(struct address_space *mapping,
2316 struct writeback_control *wbc)
2318 struct inode *inode = mapping->host;
2319 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2320 struct TCP_Server_Info *server;
2321 bool done = false, scanned = false, range_whole = false;
2323 struct cifs_writedata *wdata;
2324 struct cifsFileInfo *cfile = NULL;
2330 * If wsize is smaller than the page cache size, default to writing
2331 * one page at a time via cifs_writepage
2333 if (cifs_sb->wsize < PAGE_SIZE)
2334 return generic_writepages(mapping, wbc);
2337 if (wbc->range_cyclic) {
2338 index = mapping->writeback_index; /* Start from prev offset */
2341 index = wbc->range_start >> PAGE_SHIFT;
2342 end = wbc->range_end >> PAGE_SHIFT;
2343 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2347 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2350 while (!done && index <= end) {
2351 unsigned int i, nr_pages, found_pages, wsize;
2352 pgoff_t next = 0, tofind, saved_index = index;
2353 struct cifs_credits credits_on_stack;
2354 struct cifs_credits *credits = &credits_on_stack;
2355 int get_file_rc = 0;
2358 cifsFileInfo_put(cfile);
2360 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2362 /* in case of an error store it to return later */
2366 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2373 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2375 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2380 add_credits_and_wake_if(server, credits, 0);
2384 if (found_pages == 0) {
2385 kref_put(&wdata->refcount, cifs_writedata_release);
2386 add_credits_and_wake_if(server, credits, 0);
2390 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2391 end, &index, &next, &done);
2393 /* nothing to write? */
2394 if (nr_pages == 0) {
2395 kref_put(&wdata->refcount, cifs_writedata_release);
2396 add_credits_and_wake_if(server, credits, 0);
2400 wdata->credits = credits_on_stack;
2401 wdata->cfile = cfile;
2402 wdata->server = server;
2405 if (!wdata->cfile) {
2406 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2408 if (is_retryable_error(get_file_rc))
2413 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2415 for (i = 0; i < nr_pages; ++i)
2416 unlock_page(wdata->pages[i]);
2418 /* send failure -- clean up the mess */
2420 add_credits_and_wake_if(server, &wdata->credits, 0);
2421 for (i = 0; i < nr_pages; ++i) {
2422 if (is_retryable_error(rc))
2423 redirty_page_for_writepage(wbc,
2426 SetPageError(wdata->pages[i]);
2427 end_page_writeback(wdata->pages[i]);
2428 put_page(wdata->pages[i]);
2430 if (!is_retryable_error(rc))
2431 mapping_set_error(mapping, rc);
2433 kref_put(&wdata->refcount, cifs_writedata_release);
2435 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2436 index = saved_index;
2440 /* Return immediately if we received a signal during writing */
2441 if (is_interrupt_error(rc)) {
2446 if (rc != 0 && saved_rc == 0)
2449 wbc->nr_to_write -= nr_pages;
2450 if (wbc->nr_to_write <= 0)
2456 if (!scanned && !done) {
2458 * We hit the last page and there is more work to be done: wrap
2459 * back to the start of the file
2469 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2470 mapping->writeback_index = index;
2473 cifsFileInfo_put(cfile);
2479 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2485 /* BB add check for wbc flags */
2487 if (!PageUptodate(page))
2488 cifs_dbg(FYI, "ppw - page not up to date\n");
2491 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2493 * A writepage() implementation always needs to do either this,
2494 * or re-dirty the page with "redirty_page_for_writepage()" in
2495 * the case of a failure.
2497 * Just unlocking the page will cause the radix tree tag-bits
2498 * to fail to update with the state of the page correctly.
2500 set_page_writeback(page);
2502 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2503 if (is_retryable_error(rc)) {
2504 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2506 redirty_page_for_writepage(wbc, page);
2507 } else if (rc != 0) {
2509 mapping_set_error(page->mapping, rc);
2511 SetPageUptodate(page);
2513 end_page_writeback(page);
2519 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2521 int rc = cifs_writepage_locked(page, wbc);
2526 static int cifs_write_end(struct file *file, struct address_space *mapping,
2527 loff_t pos, unsigned len, unsigned copied,
2528 struct page *page, void *fsdata)
2531 struct inode *inode = mapping->host;
2532 struct cifsFileInfo *cfile = file->private_data;
2533 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2536 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2539 pid = current->tgid;
2541 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2544 if (PageChecked(page)) {
2546 SetPageUptodate(page);
2547 ClearPageChecked(page);
2548 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2549 SetPageUptodate(page);
2551 if (!PageUptodate(page)) {
2553 unsigned offset = pos & (PAGE_SIZE - 1);
2557 /* this is probably better than directly calling
2558 partialpage_write since in this function the file handle is
2559 known which we might as well leverage */
2560 /* BB check if anything else missing out of ppw
2561 such as updating last write time */
2562 page_data = kmap(page);
2563 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2564 /* if (rc < 0) should we set writebehind rc? */
2571 set_page_dirty(page);
2575 spin_lock(&inode->i_lock);
2576 if (pos > inode->i_size)
2577 i_size_write(inode, pos);
2578 spin_unlock(&inode->i_lock);
2587 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2592 struct cifs_tcon *tcon;
2593 struct TCP_Server_Info *server;
2594 struct cifsFileInfo *smbfile = file->private_data;
2595 struct inode *inode = file_inode(file);
2596 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2598 rc = file_write_and_wait_range(file, start, end);
2600 trace_cifs_fsync_err(inode->i_ino, rc);
2606 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2609 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2610 rc = cifs_zap_mapping(inode);
2612 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2613 rc = 0; /* don't care about it in fsync */
2617 tcon = tlink_tcon(smbfile->tlink);
2618 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2619 server = tcon->ses->server;
2620 if (server->ops->flush)
2621 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2630 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2634 struct cifs_tcon *tcon;
2635 struct TCP_Server_Info *server;
2636 struct cifsFileInfo *smbfile = file->private_data;
2637 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2639 rc = file_write_and_wait_range(file, start, end);
2641 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
2647 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2650 tcon = tlink_tcon(smbfile->tlink);
2651 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2652 server = tcon->ses->server;
2653 if (server->ops->flush)
2654 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2664 * As file closes, flush all cached write data for this inode checking
2665 * for write behind errors.
2667 int cifs_flush(struct file *file, fl_owner_t id)
2669 struct inode *inode = file_inode(file);
2672 if (file->f_mode & FMODE_WRITE)
2673 rc = filemap_write_and_wait(inode->i_mapping);
2675 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2677 trace_cifs_flush_err(inode->i_ino, rc);
2682 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2687 for (i = 0; i < num_pages; i++) {
2688 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2691 * save number of pages we have already allocated and
2692 * return with ENOMEM error
2701 for (i = 0; i < num_pages; i++)
2708 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2713 clen = min_t(const size_t, len, wsize);
2714 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
2723 cifs_uncached_writedata_release(struct kref *refcount)
2726 struct cifs_writedata *wdata = container_of(refcount,
2727 struct cifs_writedata, refcount);
2729 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
2730 for (i = 0; i < wdata->nr_pages; i++)
2731 put_page(wdata->pages[i]);
2732 cifs_writedata_release(refcount);
2735 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2738 cifs_uncached_writev_complete(struct work_struct *work)
2740 struct cifs_writedata *wdata = container_of(work,
2741 struct cifs_writedata, work);
2742 struct inode *inode = d_inode(wdata->cfile->dentry);
2743 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2745 spin_lock(&inode->i_lock);
2746 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2747 if (cifsi->server_eof > inode->i_size)
2748 i_size_write(inode, cifsi->server_eof);
2749 spin_unlock(&inode->i_lock);
2751 complete(&wdata->done);
2752 collect_uncached_write_data(wdata->ctx);
2753 /* the below call can possibly free the last ref to aio ctx */
2754 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2758 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2759 size_t *len, unsigned long *num_pages)
2761 size_t save_len, copied, bytes, cur_len = *len;
2762 unsigned long i, nr_pages = *num_pages;
2765 for (i = 0; i < nr_pages; i++) {
2766 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2767 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2770 * If we didn't copy as much as we expected, then that
2771 * may mean we trod into an unmapped area. Stop copying
2772 * at that point. On the next pass through the big
2773 * loop, we'll likely end up getting a zero-length
2774 * write and bailing out of it.
2779 cur_len = save_len - cur_len;
2783 * If we have no data to send, then that probably means that
2784 * the copy above failed altogether. That's most likely because
2785 * the address in the iovec was bogus. Return -EFAULT and let
2786 * the caller free anything we allocated and bail out.
2792 * i + 1 now represents the number of pages we actually used in
2793 * the copy phase above.
2800 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
2801 struct cifs_aio_ctx *ctx)
2804 struct cifs_credits credits;
2806 struct TCP_Server_Info *server = wdata->server;
2809 if (wdata->cfile->invalidHandle) {
2810 rc = cifs_reopen_file(wdata->cfile, false);
2819 * Wait for credits to resend this wdata.
2820 * Note: we are attempting to resend the whole wdata not in
2824 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
2829 if (wsize < wdata->bytes) {
2830 add_credits_and_wake_if(server, &credits, 0);
2833 } while (wsize < wdata->bytes);
2834 wdata->credits = credits;
2836 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2839 if (wdata->cfile->invalidHandle)
2842 #ifdef CONFIG_CIFS_SMB_DIRECT
2844 wdata->mr->need_invalidate = true;
2845 smbd_deregister_mr(wdata->mr);
2849 rc = server->ops->async_writev(wdata,
2850 cifs_uncached_writedata_release);
2854 /* If the write was successfully sent, we are done */
2856 list_add_tail(&wdata->list, wdata_list);
2860 /* Roll back credits and retry if needed */
2861 add_credits_and_wake_if(server, &wdata->credits, 0);
2862 } while (rc == -EAGAIN);
2865 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2870 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2871 struct cifsFileInfo *open_file,
2872 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2873 struct cifs_aio_ctx *ctx)
2877 unsigned long nr_pages, num_pages, i;
2878 struct cifs_writedata *wdata;
2879 struct iov_iter saved_from = *from;
2880 loff_t saved_offset = offset;
2882 struct TCP_Server_Info *server;
2883 struct page **pagevec;
2887 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2888 pid = open_file->pid;
2890 pid = current->tgid;
2892 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
2897 struct cifs_credits credits_on_stack;
2898 struct cifs_credits *credits = &credits_on_stack;
2900 if (open_file->invalidHandle) {
2901 rc = cifs_reopen_file(open_file, false);
2908 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2913 cur_len = min_t(const size_t, len, wsize);
2915 if (ctx->direct_io) {
2918 result = iov_iter_get_pages_alloc(
2919 from, &pagevec, cur_len, &start);
2922 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
2923 result, iov_iter_type(from),
2924 from->iov_offset, from->count);
2928 add_credits_and_wake_if(server, credits, 0);
2931 cur_len = (size_t)result;
2932 iov_iter_advance(from, cur_len);
2935 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
2937 wdata = cifs_writedata_direct_alloc(pagevec,
2938 cifs_uncached_writev_complete);
2941 add_credits_and_wake_if(server, credits, 0);
2946 wdata->page_offset = start;
2949 cur_len - (PAGE_SIZE - start) -
2950 (nr_pages - 2) * PAGE_SIZE :
2953 nr_pages = get_numpages(wsize, len, &cur_len);
2954 wdata = cifs_writedata_alloc(nr_pages,
2955 cifs_uncached_writev_complete);
2958 add_credits_and_wake_if(server, credits, 0);
2962 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2964 kvfree(wdata->pages);
2966 add_credits_and_wake_if(server, credits, 0);
2970 num_pages = nr_pages;
2971 rc = wdata_fill_from_iovec(
2972 wdata, from, &cur_len, &num_pages);
2974 for (i = 0; i < nr_pages; i++)
2975 put_page(wdata->pages[i]);
2976 kvfree(wdata->pages);
2978 add_credits_and_wake_if(server, credits, 0);
2983 * Bring nr_pages down to the number of pages we
2984 * actually used, and free any pages that we didn't use.
2986 for ( ; nr_pages > num_pages; nr_pages--)
2987 put_page(wdata->pages[nr_pages - 1]);
2989 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
2992 wdata->sync_mode = WB_SYNC_ALL;
2993 wdata->nr_pages = nr_pages;
2994 wdata->offset = (__u64)offset;
2995 wdata->cfile = cifsFileInfo_get(open_file);
2996 wdata->server = server;
2998 wdata->bytes = cur_len;
2999 wdata->pagesz = PAGE_SIZE;
3000 wdata->credits = credits_on_stack;
3002 kref_get(&ctx->refcount);
3004 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3007 if (wdata->cfile->invalidHandle)
3010 rc = server->ops->async_writev(wdata,
3011 cifs_uncached_writedata_release);
3015 add_credits_and_wake_if(server, &wdata->credits, 0);
3016 kref_put(&wdata->refcount,
3017 cifs_uncached_writedata_release);
3018 if (rc == -EAGAIN) {
3020 iov_iter_advance(from, offset - saved_offset);
3026 list_add_tail(&wdata->list, wdata_list);
3035 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3037 struct cifs_writedata *wdata, *tmp;
3038 struct cifs_tcon *tcon;
3039 struct cifs_sb_info *cifs_sb;
3040 struct dentry *dentry = ctx->cfile->dentry;
3043 tcon = tlink_tcon(ctx->cfile->tlink);
3044 cifs_sb = CIFS_SB(dentry->d_sb);
3046 mutex_lock(&ctx->aio_mutex);
3048 if (list_empty(&ctx->list)) {
3049 mutex_unlock(&ctx->aio_mutex);
3055 * Wait for and collect replies for any successful sends in order of
3056 * increasing offset. Once an error is hit, then return without waiting
3057 * for any more replies.
3060 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3062 if (!try_wait_for_completion(&wdata->done)) {
3063 mutex_unlock(&ctx->aio_mutex);
3070 ctx->total_len += wdata->bytes;
3072 /* resend call if it's a retryable error */
3073 if (rc == -EAGAIN) {
3074 struct list_head tmp_list;
3075 struct iov_iter tmp_from = ctx->iter;
3077 INIT_LIST_HEAD(&tmp_list);
3078 list_del_init(&wdata->list);
3081 rc = cifs_resend_wdata(
3082 wdata, &tmp_list, ctx);
3084 iov_iter_advance(&tmp_from,
3085 wdata->offset - ctx->pos);
3087 rc = cifs_write_from_iter(wdata->offset,
3088 wdata->bytes, &tmp_from,
3089 ctx->cfile, cifs_sb, &tmp_list,
3092 kref_put(&wdata->refcount,
3093 cifs_uncached_writedata_release);
3096 list_splice(&tmp_list, &ctx->list);
3100 list_del_init(&wdata->list);
3101 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3104 cifs_stats_bytes_written(tcon, ctx->total_len);
3105 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3107 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3109 mutex_unlock(&ctx->aio_mutex);
3111 if (ctx->iocb && ctx->iocb->ki_complete)
3112 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3114 complete(&ctx->done);
3117 static ssize_t __cifs_writev(
3118 struct kiocb *iocb, struct iov_iter *from, bool direct)
3120 struct file *file = iocb->ki_filp;
3121 ssize_t total_written = 0;
3122 struct cifsFileInfo *cfile;
3123 struct cifs_tcon *tcon;
3124 struct cifs_sb_info *cifs_sb;
3125 struct cifs_aio_ctx *ctx;
3126 struct iov_iter saved_from = *from;
3127 size_t len = iov_iter_count(from);
3131 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3132 * In this case, fall back to non-direct write function.
3133 * this could be improved by getting pages directly in ITER_KVEC
3135 if (direct && iov_iter_is_kvec(from)) {
3136 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3140 rc = generic_write_checks(iocb, from);
3144 cifs_sb = CIFS_FILE_SB(file);
3145 cfile = file->private_data;
3146 tcon = tlink_tcon(cfile->tlink);
3148 if (!tcon->ses->server->ops->async_writev)
3151 ctx = cifs_aio_ctx_alloc();
3155 ctx->cfile = cifsFileInfo_get(cfile);
3157 if (!is_sync_kiocb(iocb))
3160 ctx->pos = iocb->ki_pos;
3163 ctx->direct_io = true;
3167 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3169 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3174 /* grab a lock here due to read response handlers can access ctx */
3175 mutex_lock(&ctx->aio_mutex);
3177 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3178 cfile, cifs_sb, &ctx->list, ctx);
3181 * If at least one write was successfully sent, then discard any rc
3182 * value from the later writes. If the other write succeeds, then
3183 * we'll end up returning whatever was written. If it fails, then
3184 * we'll get a new rc value from that.
3186 if (!list_empty(&ctx->list))
3189 mutex_unlock(&ctx->aio_mutex);
3192 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3196 if (!is_sync_kiocb(iocb)) {
3197 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3198 return -EIOCBQUEUED;
3201 rc = wait_for_completion_killable(&ctx->done);
3203 mutex_lock(&ctx->aio_mutex);
3204 ctx->rc = rc = -EINTR;
3205 total_written = ctx->total_len;
3206 mutex_unlock(&ctx->aio_mutex);
3209 total_written = ctx->total_len;
3212 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3214 if (unlikely(!total_written))
3217 iocb->ki_pos += total_written;
3218 return total_written;
3221 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3223 return __cifs_writev(iocb, from, true);
3226 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3228 return __cifs_writev(iocb, from, false);
3232 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3234 struct file *file = iocb->ki_filp;
3235 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3236 struct inode *inode = file->f_mapping->host;
3237 struct cifsInodeInfo *cinode = CIFS_I(inode);
3238 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3243 * We need to hold the sem to be sure nobody modifies lock list
3244 * with a brlock that prevents writing.
3246 down_read(&cinode->lock_sem);
3248 rc = generic_write_checks(iocb, from);
3252 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3253 server->vals->exclusive_lock_type, 0,
3254 NULL, CIFS_WRITE_OP))
3255 rc = __generic_file_write_iter(iocb, from);
3259 up_read(&cinode->lock_sem);
3260 inode_unlock(inode);
3263 rc = generic_write_sync(iocb, rc);
3268 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3270 struct inode *inode = file_inode(iocb->ki_filp);
3271 struct cifsInodeInfo *cinode = CIFS_I(inode);
3272 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3273 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3274 iocb->ki_filp->private_data;
3275 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3278 written = cifs_get_writer(cinode);
3282 if (CIFS_CACHE_WRITE(cinode)) {
3283 if (cap_unix(tcon->ses) &&
3284 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3285 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3286 written = generic_file_write_iter(iocb, from);
3289 written = cifs_writev(iocb, from);
3293 * For non-oplocked files in strict cache mode we need to write the data
3294 * to the server exactly from the pos to pos+len-1 rather than flush all
3295 * affected pages because it may cause a error with mandatory locks on
3296 * these pages but not on the region from pos to ppos+len-1.
3298 written = cifs_user_writev(iocb, from);
3299 if (CIFS_CACHE_READ(cinode)) {
3301 * We have read level caching and we have just sent a write
3302 * request to the server thus making data in the cache stale.
3303 * Zap the cache and set oplock/lease level to NONE to avoid
3304 * reading stale data from the cache. All subsequent read
3305 * operations will read new data from the server.
3307 cifs_zap_mapping(inode);
3308 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3313 cifs_put_writer(cinode);
3317 static struct cifs_readdata *
3318 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3320 struct cifs_readdata *rdata;
3322 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3323 if (rdata != NULL) {
3324 rdata->pages = pages;
3325 kref_init(&rdata->refcount);
3326 INIT_LIST_HEAD(&rdata->list);
3327 init_completion(&rdata->done);
3328 INIT_WORK(&rdata->work, complete);
3334 static struct cifs_readdata *
3335 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3337 struct page **pages =
3338 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3339 struct cifs_readdata *ret = NULL;
3342 ret = cifs_readdata_direct_alloc(pages, complete);
3351 cifs_readdata_release(struct kref *refcount)
3353 struct cifs_readdata *rdata = container_of(refcount,
3354 struct cifs_readdata, refcount);
3355 #ifdef CONFIG_CIFS_SMB_DIRECT
3357 smbd_deregister_mr(rdata->mr);
3362 cifsFileInfo_put(rdata->cfile);
3364 kvfree(rdata->pages);
3369 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3375 for (i = 0; i < nr_pages; i++) {
3376 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3381 rdata->pages[i] = page;
3385 unsigned int nr_page_failed = i;
3387 for (i = 0; i < nr_page_failed; i++) {
3388 put_page(rdata->pages[i]);
3389 rdata->pages[i] = NULL;
3396 cifs_uncached_readdata_release(struct kref *refcount)
3398 struct cifs_readdata *rdata = container_of(refcount,
3399 struct cifs_readdata, refcount);
3402 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3403 for (i = 0; i < rdata->nr_pages; i++) {
3404 put_page(rdata->pages[i]);
3406 cifs_readdata_release(refcount);
3410 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3411 * @rdata: the readdata response with list of pages holding data
3412 * @iter: destination for our data
3414 * This function copies data from a list of pages in a readdata response into
3415 * an array of iovecs. It will first calculate where the data should go
3416 * based on the info in the readdata and then copy the data into that spot.
3419 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3421 size_t remaining = rdata->got_bytes;
3424 for (i = 0; i < rdata->nr_pages; i++) {
3425 struct page *page = rdata->pages[i];
3426 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3429 if (unlikely(iov_iter_is_pipe(iter))) {
3430 void *addr = kmap_atomic(page);
3432 written = copy_to_iter(addr, copy, iter);
3433 kunmap_atomic(addr);
3435 written = copy_page_to_iter(page, 0, copy, iter);
3436 remaining -= written;
3437 if (written < copy && iov_iter_count(iter) > 0)
3440 return remaining ? -EFAULT : 0;
3443 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3446 cifs_uncached_readv_complete(struct work_struct *work)
3448 struct cifs_readdata *rdata = container_of(work,
3449 struct cifs_readdata, work);
3451 complete(&rdata->done);
3452 collect_uncached_read_data(rdata->ctx);
3453 /* the below call can possibly free the last ref to aio ctx */
3454 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3458 uncached_fill_pages(struct TCP_Server_Info *server,
3459 struct cifs_readdata *rdata, struct iov_iter *iter,
3464 unsigned int nr_pages = rdata->nr_pages;
3465 unsigned int page_offset = rdata->page_offset;
3467 rdata->got_bytes = 0;
3468 rdata->tailsz = PAGE_SIZE;
3469 for (i = 0; i < nr_pages; i++) {
3470 struct page *page = rdata->pages[i];
3472 unsigned int segment_size = rdata->pagesz;
3475 segment_size -= page_offset;
3481 /* no need to hold page hostage */
3482 rdata->pages[i] = NULL;
3489 if (len >= segment_size)
3490 /* enough data to fill the page */
3493 rdata->tailsz = len;
3497 result = copy_page_from_iter(
3498 page, page_offset, n, iter);
3499 #ifdef CONFIG_CIFS_SMB_DIRECT
3504 result = cifs_read_page_from_socket(
3505 server, page, page_offset, n);
3509 rdata->got_bytes += result;
3512 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3513 rdata->got_bytes : result;
3517 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3518 struct cifs_readdata *rdata, unsigned int len)
3520 return uncached_fill_pages(server, rdata, NULL, len);
3524 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3525 struct cifs_readdata *rdata,
3526 struct iov_iter *iter)
3528 return uncached_fill_pages(server, rdata, iter, iter->count);
3531 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3532 struct list_head *rdata_list,
3533 struct cifs_aio_ctx *ctx)
3536 struct cifs_credits credits;
3538 struct TCP_Server_Info *server;
3540 /* XXX: should we pick a new channel here? */
3541 server = rdata->server;
3544 if (rdata->cfile->invalidHandle) {
3545 rc = cifs_reopen_file(rdata->cfile, true);
3553 * Wait for credits to resend this rdata.
3554 * Note: we are attempting to resend the whole rdata not in
3558 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3564 if (rsize < rdata->bytes) {
3565 add_credits_and_wake_if(server, &credits, 0);
3568 } while (rsize < rdata->bytes);
3569 rdata->credits = credits;
3571 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3573 if (rdata->cfile->invalidHandle)
3576 #ifdef CONFIG_CIFS_SMB_DIRECT
3578 rdata->mr->need_invalidate = true;
3579 smbd_deregister_mr(rdata->mr);
3583 rc = server->ops->async_readv(rdata);
3587 /* If the read was successfully sent, we are done */
3589 /* Add to aio pending list */
3590 list_add_tail(&rdata->list, rdata_list);
3594 /* Roll back credits and retry if needed */
3595 add_credits_and_wake_if(server, &rdata->credits, 0);
3596 } while (rc == -EAGAIN);
3599 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3604 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3605 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3606 struct cifs_aio_ctx *ctx)
3608 struct cifs_readdata *rdata;
3609 unsigned int npages, rsize;
3610 struct cifs_credits credits_on_stack;
3611 struct cifs_credits *credits = &credits_on_stack;
3615 struct TCP_Server_Info *server;
3616 struct page **pagevec;
3618 struct iov_iter direct_iov = ctx->iter;
3620 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3622 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3623 pid = open_file->pid;
3625 pid = current->tgid;
3628 iov_iter_advance(&direct_iov, offset - ctx->pos);
3631 if (open_file->invalidHandle) {
3632 rc = cifs_reopen_file(open_file, true);
3639 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
3644 cur_len = min_t(const size_t, len, rsize);
3646 if (ctx->direct_io) {
3649 result = iov_iter_get_pages_alloc(
3650 &direct_iov, &pagevec,
3654 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3655 result, iov_iter_type(&direct_iov),
3656 direct_iov.iov_offset,
3661 add_credits_and_wake_if(server, credits, 0);
3664 cur_len = (size_t)result;
3665 iov_iter_advance(&direct_iov, cur_len);
3667 rdata = cifs_readdata_direct_alloc(
3668 pagevec, cifs_uncached_readv_complete);
3670 add_credits_and_wake_if(server, credits, 0);
3675 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
3676 rdata->page_offset = start;
3677 rdata->tailsz = npages > 1 ?
3678 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
3683 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
3684 /* allocate a readdata struct */
3685 rdata = cifs_readdata_alloc(npages,
3686 cifs_uncached_readv_complete);
3688 add_credits_and_wake_if(server, credits, 0);
3693 rc = cifs_read_allocate_pages(rdata, npages);
3695 kvfree(rdata->pages);
3697 add_credits_and_wake_if(server, credits, 0);
3701 rdata->tailsz = PAGE_SIZE;
3704 rdata->server = server;
3705 rdata->cfile = cifsFileInfo_get(open_file);
3706 rdata->nr_pages = npages;
3707 rdata->offset = offset;
3708 rdata->bytes = cur_len;
3710 rdata->pagesz = PAGE_SIZE;
3711 rdata->read_into_pages = cifs_uncached_read_into_pages;
3712 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
3713 rdata->credits = credits_on_stack;
3715 kref_get(&ctx->refcount);
3717 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3720 if (rdata->cfile->invalidHandle)
3723 rc = server->ops->async_readv(rdata);
3727 add_credits_and_wake_if(server, &rdata->credits, 0);
3728 kref_put(&rdata->refcount,
3729 cifs_uncached_readdata_release);
3730 if (rc == -EAGAIN) {
3731 iov_iter_revert(&direct_iov, cur_len);
3737 list_add_tail(&rdata->list, rdata_list);
3746 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3748 struct cifs_readdata *rdata, *tmp;
3749 struct iov_iter *to = &ctx->iter;
3750 struct cifs_sb_info *cifs_sb;
3753 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
3755 mutex_lock(&ctx->aio_mutex);
3757 if (list_empty(&ctx->list)) {
3758 mutex_unlock(&ctx->aio_mutex);
3763 /* the loop below should proceed in the order of increasing offsets */
3765 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
3767 if (!try_wait_for_completion(&rdata->done)) {
3768 mutex_unlock(&ctx->aio_mutex);
3772 if (rdata->result == -EAGAIN) {
3773 /* resend call if it's a retryable error */
3774 struct list_head tmp_list;
3775 unsigned int got_bytes = rdata->got_bytes;
3777 list_del_init(&rdata->list);
3778 INIT_LIST_HEAD(&tmp_list);
3781 * Got a part of data and then reconnect has
3782 * happened -- fill the buffer and continue
3785 if (got_bytes && got_bytes < rdata->bytes) {
3787 if (!ctx->direct_io)
3788 rc = cifs_readdata_to_iov(rdata, to);
3790 kref_put(&rdata->refcount,
3791 cifs_uncached_readdata_release);
3796 if (ctx->direct_io) {
3798 * Re-use rdata as this is a
3801 rc = cifs_resend_rdata(
3805 rc = cifs_send_async_read(
3806 rdata->offset + got_bytes,
3807 rdata->bytes - got_bytes,
3808 rdata->cfile, cifs_sb,
3811 kref_put(&rdata->refcount,
3812 cifs_uncached_readdata_release);
3815 list_splice(&tmp_list, &ctx->list);
3818 } else if (rdata->result)
3820 else if (!ctx->direct_io)
3821 rc = cifs_readdata_to_iov(rdata, to);
3823 /* if there was a short read -- discard anything left */
3824 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3827 ctx->total_len += rdata->got_bytes;
3829 list_del_init(&rdata->list);
3830 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3833 if (!ctx->direct_io)
3834 ctx->total_len = ctx->len - iov_iter_count(to);
3836 /* mask nodata case */
3840 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
3842 mutex_unlock(&ctx->aio_mutex);
3844 if (ctx->iocb && ctx->iocb->ki_complete)
3845 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3847 complete(&ctx->done);
3850 static ssize_t __cifs_readv(
3851 struct kiocb *iocb, struct iov_iter *to, bool direct)
3854 struct file *file = iocb->ki_filp;
3855 struct cifs_sb_info *cifs_sb;
3856 struct cifsFileInfo *cfile;
3857 struct cifs_tcon *tcon;
3858 ssize_t rc, total_read = 0;
3859 loff_t offset = iocb->ki_pos;
3860 struct cifs_aio_ctx *ctx;
3863 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
3864 * fall back to data copy read path
3865 * this could be improved by getting pages directly in ITER_KVEC
3867 if (direct && iov_iter_is_kvec(to)) {
3868 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
3872 len = iov_iter_count(to);
3876 cifs_sb = CIFS_FILE_SB(file);
3877 cfile = file->private_data;
3878 tcon = tlink_tcon(cfile->tlink);
3880 if (!tcon->ses->server->ops->async_readv)
3883 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3884 cifs_dbg(FYI, "attempting read on write only file instance\n");
3886 ctx = cifs_aio_ctx_alloc();
3890 ctx->cfile = cifsFileInfo_get(cfile);
3892 if (!is_sync_kiocb(iocb))
3895 if (iter_is_iovec(to))
3896 ctx->should_dirty = true;
3900 ctx->direct_io = true;
3904 rc = setup_aio_ctx_iter(ctx, to, READ);
3906 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3912 /* grab a lock here due to read response handlers can access ctx */
3913 mutex_lock(&ctx->aio_mutex);
3915 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
3917 /* if at least one read request send succeeded, then reset rc */
3918 if (!list_empty(&ctx->list))
3921 mutex_unlock(&ctx->aio_mutex);
3924 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3928 if (!is_sync_kiocb(iocb)) {
3929 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3930 return -EIOCBQUEUED;
3933 rc = wait_for_completion_killable(&ctx->done);
3935 mutex_lock(&ctx->aio_mutex);
3936 ctx->rc = rc = -EINTR;
3937 total_read = ctx->total_len;
3938 mutex_unlock(&ctx->aio_mutex);
3941 total_read = ctx->total_len;
3944 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3947 iocb->ki_pos += total_read;
3953 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
3955 return __cifs_readv(iocb, to, true);
3958 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
3960 return __cifs_readv(iocb, to, false);
3964 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
3966 struct inode *inode = file_inode(iocb->ki_filp);
3967 struct cifsInodeInfo *cinode = CIFS_I(inode);
3968 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3969 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3970 iocb->ki_filp->private_data;
3971 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3975 * In strict cache mode we need to read from the server all the time
3976 * if we don't have level II oplock because the server can delay mtime
3977 * change - so we can't make a decision about inode invalidating.
3978 * And we can also fail with pagereading if there are mandatory locks
3979 * on pages affected by this read but not on the region from pos to
3982 if (!CIFS_CACHE_READ(cinode))
3983 return cifs_user_readv(iocb, to);
3985 if (cap_unix(tcon->ses) &&
3986 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3987 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
3988 return generic_file_read_iter(iocb, to);
3991 * We need to hold the sem to be sure nobody modifies lock list
3992 * with a brlock that prevents reading.
3994 down_read(&cinode->lock_sem);
3995 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
3996 tcon->ses->server->vals->shared_lock_type,
3997 0, NULL, CIFS_READ_OP))
3998 rc = generic_file_read_iter(iocb, to);
3999 up_read(&cinode->lock_sem);
4004 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4007 unsigned int bytes_read = 0;
4008 unsigned int total_read;
4009 unsigned int current_read_size;
4011 struct cifs_sb_info *cifs_sb;
4012 struct cifs_tcon *tcon;
4013 struct TCP_Server_Info *server;
4016 struct cifsFileInfo *open_file;
4017 struct cifs_io_parms io_parms = {0};
4018 int buf_type = CIFS_NO_BUFFER;
4022 cifs_sb = CIFS_FILE_SB(file);
4024 /* FIXME: set up handlers for larger reads and/or convert to async */
4025 rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
4027 if (file->private_data == NULL) {
4032 open_file = file->private_data;
4033 tcon = tlink_tcon(open_file->tlink);
4034 server = cifs_pick_channel(tcon->ses);
4036 if (!server->ops->sync_read) {
4041 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4042 pid = open_file->pid;
4044 pid = current->tgid;
4046 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4047 cifs_dbg(FYI, "attempting read on write only file instance\n");
4049 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4050 total_read += bytes_read, cur_offset += bytes_read) {
4052 current_read_size = min_t(uint, read_size - total_read,
4055 * For windows me and 9x we do not want to request more
4056 * than it negotiated since it will refuse the read
4059 if (!(tcon->ses->capabilities &
4060 tcon->ses->server->vals->cap_large_files)) {
4061 current_read_size = min_t(uint,
4062 current_read_size, CIFSMaxBufSize);
4064 if (open_file->invalidHandle) {
4065 rc = cifs_reopen_file(open_file, true);
4070 io_parms.tcon = tcon;
4071 io_parms.offset = *offset;
4072 io_parms.length = current_read_size;
4073 io_parms.server = server;
4074 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4075 &bytes_read, &cur_offset,
4077 } while (rc == -EAGAIN);
4079 if (rc || (bytes_read == 0)) {
4087 cifs_stats_bytes_read(tcon, total_read);
4088 *offset += bytes_read;
4096 * If the page is mmap'ed into a process' page tables, then we need to make
4097 * sure that it doesn't change while being written back.
4100 cifs_page_mkwrite(struct vm_fault *vmf)
4102 struct page *page = vmf->page;
4105 return VM_FAULT_LOCKED;
4108 static const struct vm_operations_struct cifs_file_vm_ops = {
4109 .fault = filemap_fault,
4110 .map_pages = filemap_map_pages,
4111 .page_mkwrite = cifs_page_mkwrite,
4114 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4117 struct inode *inode = file_inode(file);
4121 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4122 rc = cifs_zap_mapping(inode);
4124 rc = generic_file_mmap(file, vma);
4126 vma->vm_ops = &cifs_file_vm_ops;
4132 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4138 rc = cifs_revalidate_file(file);
4140 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4143 rc = generic_file_mmap(file, vma);
4145 vma->vm_ops = &cifs_file_vm_ops;
4152 cifs_readv_complete(struct work_struct *work)
4154 unsigned int i, got_bytes;
4155 struct cifs_readdata *rdata = container_of(work,
4156 struct cifs_readdata, work);
4158 got_bytes = rdata->got_bytes;
4159 for (i = 0; i < rdata->nr_pages; i++) {
4160 struct page *page = rdata->pages[i];
4162 lru_cache_add(page);
4164 if (rdata->result == 0 ||
4165 (rdata->result == -EAGAIN && got_bytes)) {
4166 flush_dcache_page(page);
4167 SetPageUptodate(page);
4172 if (rdata->result == 0 ||
4173 (rdata->result == -EAGAIN && got_bytes))
4174 cifs_readpage_to_fscache(rdata->mapping->host, page);
4176 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4179 rdata->pages[i] = NULL;
4181 kref_put(&rdata->refcount, cifs_readdata_release);
4185 readpages_fill_pages(struct TCP_Server_Info *server,
4186 struct cifs_readdata *rdata, struct iov_iter *iter,
4193 unsigned int nr_pages = rdata->nr_pages;
4194 unsigned int page_offset = rdata->page_offset;
4196 /* determine the eof that the server (probably) has */
4197 eof = CIFS_I(rdata->mapping->host)->server_eof;
4198 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4199 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4201 rdata->got_bytes = 0;
4202 rdata->tailsz = PAGE_SIZE;
4203 for (i = 0; i < nr_pages; i++) {
4204 struct page *page = rdata->pages[i];
4205 unsigned int to_read = rdata->pagesz;
4209 to_read -= page_offset;
4215 if (len >= to_read) {
4217 } else if (len > 0) {
4218 /* enough for partial page, fill and zero the rest */
4219 zero_user(page, len + page_offset, to_read - len);
4220 n = rdata->tailsz = len;
4222 } else if (page->index > eof_index) {
4224 * The VFS will not try to do readahead past the
4225 * i_size, but it's possible that we have outstanding
4226 * writes with gaps in the middle and the i_size hasn't
4227 * caught up yet. Populate those with zeroed out pages
4228 * to prevent the VFS from repeatedly attempting to
4229 * fill them until the writes are flushed.
4231 zero_user(page, 0, PAGE_SIZE);
4232 lru_cache_add(page);
4233 flush_dcache_page(page);
4234 SetPageUptodate(page);
4237 rdata->pages[i] = NULL;
4241 /* no need to hold page hostage */
4242 lru_cache_add(page);
4245 rdata->pages[i] = NULL;
4251 result = copy_page_from_iter(
4252 page, page_offset, n, iter);
4253 #ifdef CONFIG_CIFS_SMB_DIRECT
4258 result = cifs_read_page_from_socket(
4259 server, page, page_offset, n);
4263 rdata->got_bytes += result;
4266 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4267 rdata->got_bytes : result;
4271 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4272 struct cifs_readdata *rdata, unsigned int len)
4274 return readpages_fill_pages(server, rdata, NULL, len);
4278 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4279 struct cifs_readdata *rdata,
4280 struct iov_iter *iter)
4282 return readpages_fill_pages(server, rdata, iter, iter->count);
4286 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4287 unsigned int rsize, struct list_head *tmplist,
4288 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4290 struct page *page, *tpage;
4291 unsigned int expected_index;
4293 gfp_t gfp = readahead_gfp_mask(mapping);
4295 INIT_LIST_HEAD(tmplist);
4297 page = lru_to_page(page_list);
4300 * Lock the page and put it in the cache. Since no one else
4301 * should have access to this page, we're safe to simply set
4302 * PG_locked without checking it first.
4304 __SetPageLocked(page);
4305 rc = add_to_page_cache_locked(page, mapping,
4308 /* give up if we can't stick it in the cache */
4310 __ClearPageLocked(page);
4314 /* move first page to the tmplist */
4315 *offset = (loff_t)page->index << PAGE_SHIFT;
4318 list_move_tail(&page->lru, tmplist);
4320 /* now try and add more pages onto the request */
4321 expected_index = page->index + 1;
4322 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4323 /* discontinuity ? */
4324 if (page->index != expected_index)
4327 /* would this page push the read over the rsize? */
4328 if (*bytes + PAGE_SIZE > rsize)
4331 __SetPageLocked(page);
4332 rc = add_to_page_cache_locked(page, mapping, page->index, gfp);
4334 __ClearPageLocked(page);
4337 list_move_tail(&page->lru, tmplist);
4338 (*bytes) += PAGE_SIZE;
4345 static int cifs_readpages(struct file *file, struct address_space *mapping,
4346 struct list_head *page_list, unsigned num_pages)
4350 struct list_head tmplist;
4351 struct cifsFileInfo *open_file = file->private_data;
4352 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4353 struct TCP_Server_Info *server;
4359 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4360 * immediately if the cookie is negative
4362 * After this point, every page in the list might have PG_fscache set,
4363 * so we will need to clean that up off of every page we don't use.
4365 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4372 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4373 pid = open_file->pid;
4375 pid = current->tgid;
4378 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4380 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4381 __func__, file, mapping, num_pages);
4384 * Start with the page at end of list and move it to private
4385 * list. Do the same with any following pages until we hit
4386 * the rsize limit, hit an index discontinuity, or run out of
4387 * pages. Issue the async read and then start the loop again
4388 * until the list is empty.
4390 * Note that list order is important. The page_list is in
4391 * the order of declining indexes. When we put the pages in
4392 * the rdata->pages, then we want them in increasing order.
4394 while (!list_empty(page_list) && !err) {
4395 unsigned int i, nr_pages, bytes, rsize;
4397 struct page *page, *tpage;
4398 struct cifs_readdata *rdata;
4399 struct cifs_credits credits_on_stack;
4400 struct cifs_credits *credits = &credits_on_stack;
4402 if (open_file->invalidHandle) {
4403 rc = cifs_reopen_file(open_file, true);
4410 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
4416 * Give up immediately if rsize is too small to read an entire
4417 * page. The VFS will fall back to readpage. We should never
4418 * reach this point however since we set ra_pages to 0 when the
4419 * rsize is smaller than a cache page.
4421 if (unlikely(rsize < PAGE_SIZE)) {
4422 add_credits_and_wake_if(server, credits, 0);
4428 err = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4429 &nr_pages, &offset, &bytes);
4431 add_credits_and_wake_if(server, credits, 0);
4435 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4437 /* best to give up if we're out of mem */
4438 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4439 list_del(&page->lru);
4440 lru_cache_add(page);
4445 add_credits_and_wake_if(server, credits, 0);
4449 rdata->cfile = cifsFileInfo_get(open_file);
4450 rdata->server = server;
4451 rdata->mapping = mapping;
4452 rdata->offset = offset;
4453 rdata->bytes = bytes;
4455 rdata->pagesz = PAGE_SIZE;
4456 rdata->tailsz = PAGE_SIZE;
4457 rdata->read_into_pages = cifs_readpages_read_into_pages;
4458 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4459 rdata->credits = credits_on_stack;
4461 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4462 list_del(&page->lru);
4463 rdata->pages[rdata->nr_pages++] = page;
4466 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4469 if (rdata->cfile->invalidHandle)
4472 rc = server->ops->async_readv(rdata);
4476 add_credits_and_wake_if(server, &rdata->credits, 0);
4477 for (i = 0; i < rdata->nr_pages; i++) {
4478 page = rdata->pages[i];
4479 lru_cache_add(page);
4483 /* Fallback to the readpage in error/reconnect cases */
4484 kref_put(&rdata->refcount, cifs_readdata_release);
4488 kref_put(&rdata->refcount, cifs_readdata_release);
4491 /* Any pages that have been shown to fscache but didn't get added to
4492 * the pagecache must be uncached before they get returned to the
4495 cifs_fscache_readpages_cancel(mapping->host, page_list);
4501 * cifs_readpage_worker must be called with the page pinned
4503 static int cifs_readpage_worker(struct file *file, struct page *page,
4509 /* Is the page cached? */
4510 rc = cifs_readpage_from_fscache(file_inode(file), page);
4514 read_data = kmap(page);
4515 /* for reads over a certain size could initiate async read ahead */
4517 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4522 cifs_dbg(FYI, "Bytes read %d\n", rc);
4524 /* we do not want atime to be less than mtime, it broke some apps */
4525 file_inode(file)->i_atime = current_time(file_inode(file));
4526 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4527 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4529 file_inode(file)->i_atime = current_time(file_inode(file));
4532 memset(read_data + rc, 0, PAGE_SIZE - rc);
4534 flush_dcache_page(page);
4535 SetPageUptodate(page);
4537 /* send this page to the cache */
4538 cifs_readpage_to_fscache(file_inode(file), page);
4550 static int cifs_readpage(struct file *file, struct page *page)
4552 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
4558 if (file->private_data == NULL) {
4564 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4565 page, (int)offset, (int)offset);
4567 rc = cifs_readpage_worker(file, page, &offset);
4573 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4575 struct cifsFileInfo *open_file;
4577 spin_lock(&cifs_inode->open_file_lock);
4578 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4579 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4580 spin_unlock(&cifs_inode->open_file_lock);
4584 spin_unlock(&cifs_inode->open_file_lock);
4588 /* We do not want to update the file size from server for inodes
4589 open for write - to avoid races with writepage extending
4590 the file - in the future we could consider allowing
4591 refreshing the inode only on increases in the file size
4592 but this is tricky to do without racing with writebehind
4593 page caching in the current Linux kernel design */
4594 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4599 if (is_inode_writable(cifsInode)) {
4600 /* This inode is open for write at least once */
4601 struct cifs_sb_info *cifs_sb;
4603 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4604 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4605 /* since no page cache to corrupt on directio
4606 we can change size safely */
4610 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4618 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4619 loff_t pos, unsigned len, unsigned flags,
4620 struct page **pagep, void **fsdata)
4623 pgoff_t index = pos >> PAGE_SHIFT;
4624 loff_t offset = pos & (PAGE_SIZE - 1);
4625 loff_t page_start = pos & PAGE_MASK;
4630 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4633 page = grab_cache_page_write_begin(mapping, index, flags);
4639 if (PageUptodate(page))
4643 * If we write a full page it will be up to date, no need to read from
4644 * the server. If the write is short, we'll end up doing a sync write
4647 if (len == PAGE_SIZE)
4651 * optimize away the read when we have an oplock, and we're not
4652 * expecting to use any of the data we'd be reading in. That
4653 * is, when the page lies beyond the EOF, or straddles the EOF
4654 * and the write will cover all of the existing data.
4656 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4657 i_size = i_size_read(mapping->host);
4658 if (page_start >= i_size ||
4659 (offset == 0 && (pos + len) >= i_size)) {
4660 zero_user_segments(page, 0, offset,
4664 * PageChecked means that the parts of the page
4665 * to which we're not writing are considered up
4666 * to date. Once the data is copied to the
4667 * page, it can be set uptodate.
4669 SetPageChecked(page);
4674 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4676 * might as well read a page, it is fast enough. If we get
4677 * an error, we don't need to return it. cifs_write_end will
4678 * do a sync write instead since PG_uptodate isn't set.
4680 cifs_readpage_worker(file, page, &page_start);
4685 /* we could try using another file handle if there is one -
4686 but how would we lock it to prevent close of that handle
4687 racing with this read? In any case
4688 this will be written out by write_end so is fine */
4695 static int cifs_release_page(struct page *page, gfp_t gfp)
4697 if (PagePrivate(page))
4700 return cifs_fscache_release_page(page, gfp);
4703 static void cifs_invalidate_page(struct page *page, unsigned int offset,
4704 unsigned int length)
4706 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4708 if (offset == 0 && length == PAGE_SIZE)
4709 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4712 static int cifs_launder_page(struct page *page)
4715 loff_t range_start = page_offset(page);
4716 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
4717 struct writeback_control wbc = {
4718 .sync_mode = WB_SYNC_ALL,
4720 .range_start = range_start,
4721 .range_end = range_end,
4724 cifs_dbg(FYI, "Launder page: %p\n", page);
4726 if (clear_page_dirty_for_io(page))
4727 rc = cifs_writepage_locked(page, &wbc);
4729 cifs_fscache_invalidate_page(page, page->mapping->host);
4733 void cifs_oplock_break(struct work_struct *work)
4735 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4737 struct inode *inode = d_inode(cfile->dentry);
4738 struct cifsInodeInfo *cinode = CIFS_I(inode);
4739 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4740 struct TCP_Server_Info *server = tcon->ses->server;
4742 bool purge_cache = false;
4744 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4745 TASK_UNINTERRUPTIBLE);
4747 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4748 cfile->oplock_epoch, &purge_cache);
4750 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4751 cifs_has_mand_locks(cinode)) {
4752 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4757 if (inode && S_ISREG(inode->i_mode)) {
4758 if (CIFS_CACHE_READ(cinode))
4759 break_lease(inode, O_RDONLY);
4761 break_lease(inode, O_WRONLY);
4762 rc = filemap_fdatawrite(inode->i_mapping);
4763 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4764 rc = filemap_fdatawait(inode->i_mapping);
4765 mapping_set_error(inode->i_mapping, rc);
4766 cifs_zap_mapping(inode);
4768 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4769 if (CIFS_CACHE_WRITE(cinode))
4770 goto oplock_break_ack;
4773 rc = cifs_push_locks(cfile);
4775 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4779 * releasing stale oplock after recent reconnect of smb session using
4780 * a now incorrect file handle is not a data integrity issue but do
4781 * not bother sending an oplock release if session to server still is
4782 * disconnected since oplock already released by the server
4784 if (!cfile->oplock_break_cancelled) {
4785 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4787 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4789 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4790 cifs_done_oplock_break(cinode);
4794 * The presence of cifs_direct_io() in the address space ops vector
4795 * allowes open() O_DIRECT flags which would have failed otherwise.
4797 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4798 * so this method should never be called.
4800 * Direct IO is not yet supported in the cached mode.
4803 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4807 * Eventually need to support direct IO for non forcedirectio mounts
4812 static int cifs_swap_activate(struct swap_info_struct *sis,
4813 struct file *swap_file, sector_t *span)
4815 struct cifsFileInfo *cfile = swap_file->private_data;
4816 struct inode *inode = swap_file->f_mapping->host;
4817 unsigned long blocks;
4820 cifs_dbg(FYI, "swap activate\n");
4822 spin_lock(&inode->i_lock);
4823 blocks = inode->i_blocks;
4824 isize = inode->i_size;
4825 spin_unlock(&inode->i_lock);
4826 if (blocks*512 < isize) {
4827 pr_warn("swap activate: swapfile has holes\n");
4832 pr_warn_once("Swap support over SMB3 is experimental\n");
4835 * TODO: consider adding ACL (or documenting how) to prevent other
4836 * users (on this or other systems) from reading it
4840 /* TODO: add sk_set_memalloc(inet) or similar */
4843 cfile->swapfile = true;
4845 * TODO: Since file already open, we can't open with DENY_ALL here
4846 * but we could add call to grab a byte range lock to prevent others
4847 * from reading or writing the file
4853 static void cifs_swap_deactivate(struct file *file)
4855 struct cifsFileInfo *cfile = file->private_data;
4857 cifs_dbg(FYI, "swap deactivate\n");
4859 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
4862 cfile->swapfile = false;
4864 /* do we need to unpin (or unlock) the file */
4867 const struct address_space_operations cifs_addr_ops = {
4868 .readpage = cifs_readpage,
4869 .readpages = cifs_readpages,
4870 .writepage = cifs_writepage,
4871 .writepages = cifs_writepages,
4872 .write_begin = cifs_write_begin,
4873 .write_end = cifs_write_end,
4874 .set_page_dirty = __set_page_dirty_nobuffers,
4875 .releasepage = cifs_release_page,
4876 .direct_IO = cifs_direct_io,
4877 .invalidatepage = cifs_invalidate_page,
4878 .launder_page = cifs_launder_page,
4880 * TODO: investigate and if useful we could add an cifs_migratePage
4881 * helper (under an CONFIG_MIGRATION) in the future, and also
4882 * investigate and add an is_dirty_writeback helper if needed
4884 .swap_activate = cifs_swap_activate,
4885 .swap_deactivate = cifs_swap_deactivate,
4889 * cifs_readpages requires the server to support a buffer large enough to
4890 * contain the header plus one complete page of data. Otherwise, we need
4891 * to leave cifs_readpages out of the address space operations.
4893 const struct address_space_operations cifs_addr_ops_smallbuf = {
4894 .readpage = cifs_readpage,
4895 .writepage = cifs_writepage,
4896 .writepages = cifs_writepages,
4897 .write_begin = cifs_write_begin,
4898 .write_end = cifs_write_end,
4899 .set_page_dirty = __set_page_dirty_nobuffers,
4900 .releasepage = cifs_release_page,
4901 .invalidatepage = cifs_invalidate_page,
4902 .launder_page = cifs_launder_page,