4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/backing-dev.h>
26 #include <linux/stat.h>
27 #include <linux/fcntl.h>
28 #include <linux/pagemap.h>
29 #include <linux/pagevec.h>
30 #include <linux/writeback.h>
31 #include <linux/task_io_accounting_ops.h>
32 #include <linux/delay.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
37 #include <asm/div64.h>
41 #include "cifsproto.h"
42 #include "cifs_unicode.h"
43 #include "cifs_debug.h"
44 #include "cifs_fs_sb.h"
46 #include "smbdirect.h"
48 static inline int cifs_convert_flags(unsigned int flags)
50 if ((flags & O_ACCMODE) == O_RDONLY)
52 else if ((flags & O_ACCMODE) == O_WRONLY)
54 else if ((flags & O_ACCMODE) == O_RDWR) {
55 /* GENERIC_ALL is too much permission to request
56 can cause unnecessary access denied on create */
57 /* return GENERIC_ALL; */
58 return (GENERIC_READ | GENERIC_WRITE);
61 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
62 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
66 static u32 cifs_posix_convert_flags(unsigned int flags)
70 if ((flags & O_ACCMODE) == O_RDONLY)
71 posix_flags = SMB_O_RDONLY;
72 else if ((flags & O_ACCMODE) == O_WRONLY)
73 posix_flags = SMB_O_WRONLY;
74 else if ((flags & O_ACCMODE) == O_RDWR)
75 posix_flags = SMB_O_RDWR;
77 if (flags & O_CREAT) {
78 posix_flags |= SMB_O_CREAT;
80 posix_flags |= SMB_O_EXCL;
81 } else if (flags & O_EXCL)
82 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
83 current->comm, current->tgid);
86 posix_flags |= SMB_O_TRUNC;
87 /* be safe and imply O_SYNC for O_DSYNC */
89 posix_flags |= SMB_O_SYNC;
90 if (flags & O_DIRECTORY)
91 posix_flags |= SMB_O_DIRECTORY;
92 if (flags & O_NOFOLLOW)
93 posix_flags |= SMB_O_NOFOLLOW;
95 posix_flags |= SMB_O_DIRECT;
100 static inline int cifs_get_disposition(unsigned int flags)
102 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
104 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
105 return FILE_OVERWRITE_IF;
106 else if ((flags & O_CREAT) == O_CREAT)
108 else if ((flags & O_TRUNC) == O_TRUNC)
109 return FILE_OVERWRITE;
114 int cifs_posix_open(char *full_path, struct inode **pinode,
115 struct super_block *sb, int mode, unsigned int f_flags,
116 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
119 FILE_UNIX_BASIC_INFO *presp_data;
120 __u32 posix_flags = 0;
121 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
122 struct cifs_fattr fattr;
123 struct tcon_link *tlink;
124 struct cifs_tcon *tcon;
126 cifs_dbg(FYI, "posix open %s\n", full_path);
128 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
129 if (presp_data == NULL)
132 tlink = cifs_sb_tlink(cifs_sb);
138 tcon = tlink_tcon(tlink);
139 mode &= ~current_umask();
141 posix_flags = cifs_posix_convert_flags(f_flags);
142 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
143 poplock, full_path, cifs_sb->local_nls,
144 cifs_remap(cifs_sb));
145 cifs_put_tlink(tlink);
150 if (presp_data->Type == cpu_to_le32(-1))
151 goto posix_open_ret; /* open ok, caller does qpathinfo */
154 goto posix_open_ret; /* caller does not need info */
156 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
158 /* get new inode and set it up */
159 if (*pinode == NULL) {
160 cifs_fill_uniqueid(sb, &fattr);
161 *pinode = cifs_iget(sb, &fattr);
167 cifs_revalidate_mapping(*pinode);
168 cifs_fattr_to_inode(*pinode, &fattr);
177 cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
178 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
179 struct cifs_fid *fid, unsigned int xid)
184 int create_options = CREATE_NOT_DIR;
186 struct TCP_Server_Info *server = tcon->ses->server;
187 struct cifs_open_parms oparms;
189 if (!server->ops->open)
192 desired_access = cifs_convert_flags(f_flags);
194 /*********************************************************************
195 * open flag mapping table:
197 * POSIX Flag CIFS Disposition
198 * ---------- ----------------
199 * O_CREAT FILE_OPEN_IF
200 * O_CREAT | O_EXCL FILE_CREATE
201 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
202 * O_TRUNC FILE_OVERWRITE
203 * none of the above FILE_OPEN
205 * Note that there is not a direct match between disposition
206 * FILE_SUPERSEDE (ie create whether or not file exists although
207 * O_CREAT | O_TRUNC is similar but truncates the existing
208 * file rather than creating a new file as FILE_SUPERSEDE does
209 * (which uses the attributes / metadata passed in on open call)
211 *? O_SYNC is a reasonable match to CIFS writethrough flag
212 *? and the read write flags match reasonably. O_LARGEFILE
213 *? is irrelevant because largefile support is always used
214 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
215 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
216 *********************************************************************/
218 disposition = cifs_get_disposition(f_flags);
220 /* BB pass O_SYNC flag through on file attributes .. BB */
222 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
226 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
227 if (f_flags & O_SYNC)
228 create_options |= CREATE_WRITE_THROUGH;
230 if (f_flags & O_DIRECT)
231 create_options |= CREATE_NO_BUFFER;
234 oparms.cifs_sb = cifs_sb;
235 oparms.desired_access = desired_access;
236 oparms.create_options = cifs_create_options(cifs_sb, create_options);
237 oparms.disposition = disposition;
238 oparms.path = full_path;
240 oparms.reconnect = false;
242 rc = server->ops->open(xid, &oparms, oplock, buf);
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)
498 server->ops->close(xid, tcon, &cifs_file->fid);
502 if (oplock_break_cancelled)
503 cifs_done_oplock_break(cifsi);
505 cifs_del_pending_open(&open);
508 queue_work(fileinfo_put_wq, &cifs_file->put);
510 cifsFileInfo_put_final(cifs_file);
513 int cifs_open(struct inode *inode, struct file *file)
519 struct cifs_sb_info *cifs_sb;
520 struct TCP_Server_Info *server;
521 struct cifs_tcon *tcon;
522 struct tcon_link *tlink;
523 struct cifsFileInfo *cfile = NULL;
524 char *full_path = NULL;
525 bool posix_open_ok = false;
527 struct cifs_pending_open open;
531 cifs_sb = CIFS_SB(inode->i_sb);
532 tlink = cifs_sb_tlink(cifs_sb);
535 return PTR_ERR(tlink);
537 tcon = tlink_tcon(tlink);
538 server = tcon->ses->server;
540 full_path = build_path_from_dentry(file_dentry(file));
541 if (full_path == NULL) {
546 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
547 inode, file->f_flags, full_path);
549 if (file->f_flags & O_DIRECT &&
550 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
551 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
552 file->f_op = &cifs_file_direct_nobrl_ops;
554 file->f_op = &cifs_file_direct_ops;
562 if (!tcon->broken_posix_open && tcon->unix_ext &&
563 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
564 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
565 /* can not refresh inode info since size could be stale */
566 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
567 cifs_sb->mnt_file_mode /* ignored */,
568 file->f_flags, &oplock, &fid.netfid, xid);
570 cifs_dbg(FYI, "posix open succeeded\n");
571 posix_open_ok = true;
572 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
573 if (tcon->ses->serverNOS)
574 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",
575 tcon->ses->serverName,
576 tcon->ses->serverNOS);
577 tcon->broken_posix_open = true;
578 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
579 (rc != -EOPNOTSUPP)) /* path not found or net err */
582 * Else fallthrough to retry open the old way on network i/o
587 if (server->ops->get_lease_key)
588 server->ops->get_lease_key(inode, &fid);
590 cifs_add_pending_open(&fid, tlink, &open);
592 if (!posix_open_ok) {
593 if (server->ops->get_lease_key)
594 server->ops->get_lease_key(inode, &fid);
596 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
597 file->f_flags, &oplock, &fid, xid);
599 cifs_del_pending_open(&open);
604 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
606 if (server->ops->close)
607 server->ops->close(xid, tcon, &fid);
608 cifs_del_pending_open(&open);
613 cifs_fscache_set_inode_cookie(inode, file);
615 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
617 * Time to set mode which we can not set earlier due to
618 * problems creating new read-only files.
620 struct cifs_unix_set_info_args args = {
621 .mode = inode->i_mode,
622 .uid = INVALID_UID, /* no change */
623 .gid = INVALID_GID, /* no change */
624 .ctime = NO_CHANGE_64,
625 .atime = NO_CHANGE_64,
626 .mtime = NO_CHANGE_64,
629 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
636 cifs_put_tlink(tlink);
640 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
643 * Try to reacquire byte range locks that were released when session
644 * to server was lost.
647 cifs_relock_file(struct cifsFileInfo *cfile)
649 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
650 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
651 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
654 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
655 if (cinode->can_cache_brlcks) {
656 /* can cache locks - no need to relock */
657 up_read(&cinode->lock_sem);
661 if (cap_unix(tcon->ses) &&
662 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
663 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
664 rc = cifs_push_posix_locks(cfile);
666 rc = tcon->ses->server->ops->push_mand_locks(cfile);
668 up_read(&cinode->lock_sem);
673 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
678 struct cifs_sb_info *cifs_sb;
679 struct cifs_tcon *tcon;
680 struct TCP_Server_Info *server;
681 struct cifsInodeInfo *cinode;
683 char *full_path = NULL;
685 int disposition = FILE_OPEN;
686 int create_options = CREATE_NOT_DIR;
687 struct cifs_open_parms oparms;
690 mutex_lock(&cfile->fh_mutex);
691 if (!cfile->invalidHandle) {
692 mutex_unlock(&cfile->fh_mutex);
698 inode = d_inode(cfile->dentry);
699 cifs_sb = CIFS_SB(inode->i_sb);
700 tcon = tlink_tcon(cfile->tlink);
701 server = tcon->ses->server;
704 * Can not grab rename sem here because various ops, including those
705 * that already have the rename sem can end up causing writepage to get
706 * called and if the server was down that means we end up here, and we
707 * can never tell if the caller already has the rename_sem.
709 full_path = build_path_from_dentry(cfile->dentry);
710 if (full_path == NULL) {
712 mutex_unlock(&cfile->fh_mutex);
717 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
718 inode, cfile->f_flags, full_path);
720 if (tcon->ses->server->oplocks)
725 if (tcon->unix_ext && cap_unix(tcon->ses) &&
726 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
727 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
729 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
730 * original open. Must mask them off for a reopen.
732 unsigned int oflags = cfile->f_flags &
733 ~(O_CREAT | O_EXCL | O_TRUNC);
735 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
736 cifs_sb->mnt_file_mode /* ignored */,
737 oflags, &oplock, &cfile->fid.netfid, xid);
739 cifs_dbg(FYI, "posix reopen succeeded\n");
740 oparms.reconnect = true;
744 * fallthrough to retry open the old way on errors, especially
745 * in the reconnect path it is important to retry hard
749 desired_access = cifs_convert_flags(cfile->f_flags);
751 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
752 if (cfile->f_flags & O_SYNC)
753 create_options |= CREATE_WRITE_THROUGH;
755 if (cfile->f_flags & O_DIRECT)
756 create_options |= CREATE_NO_BUFFER;
758 if (server->ops->get_lease_key)
759 server->ops->get_lease_key(inode, &cfile->fid);
762 oparms.cifs_sb = cifs_sb;
763 oparms.desired_access = desired_access;
764 oparms.create_options = cifs_create_options(cifs_sb, create_options);
765 oparms.disposition = disposition;
766 oparms.path = full_path;
767 oparms.fid = &cfile->fid;
768 oparms.reconnect = true;
771 * Can not refresh inode by passing in file_info buf to be returned by
772 * ops->open and then calling get_inode_info with returned buf since
773 * file might have write behind data that needs to be flushed and server
774 * version of file size can be stale. If we knew for sure that inode was
775 * not dirty locally we could do this.
777 rc = server->ops->open(xid, &oparms, &oplock, NULL);
778 if (rc == -ENOENT && oparms.reconnect == false) {
779 /* durable handle timeout is expired - open the file again */
780 rc = server->ops->open(xid, &oparms, &oplock, NULL);
781 /* indicate that we need to relock the file */
782 oparms.reconnect = true;
786 mutex_unlock(&cfile->fh_mutex);
787 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
788 cifs_dbg(FYI, "oplock: %d\n", oplock);
789 goto reopen_error_exit;
793 cfile->invalidHandle = false;
794 mutex_unlock(&cfile->fh_mutex);
795 cinode = CIFS_I(inode);
798 rc = filemap_write_and_wait(inode->i_mapping);
799 if (!is_interrupt_error(rc))
800 mapping_set_error(inode->i_mapping, rc);
803 rc = cifs_get_inode_info_unix(&inode, full_path,
806 rc = cifs_get_inode_info(&inode, full_path, NULL,
807 inode->i_sb, xid, NULL);
810 * Else we are writing out data to server already and could deadlock if
811 * we tried to flush data, and since we do not know if we have data that
812 * would invalidate the current end of file on the server we can not go
813 * to the server to get the new inode info.
817 * If the server returned a read oplock and we have mandatory brlocks,
818 * set oplock level to None.
820 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
821 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
825 server->ops->set_fid(cfile, &cfile->fid, oplock);
826 if (oparms.reconnect)
827 cifs_relock_file(cfile);
835 int cifs_close(struct inode *inode, struct file *file)
837 if (file->private_data != NULL) {
838 _cifsFileInfo_put(file->private_data, true, false);
839 file->private_data = NULL;
842 /* return code from the ->release op is always ignored */
847 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
849 struct cifsFileInfo *open_file;
850 struct list_head *tmp;
851 struct list_head *tmp1;
852 struct list_head tmp_list;
854 if (!tcon->use_persistent || !tcon->need_reopen_files)
857 tcon->need_reopen_files = false;
859 cifs_dbg(FYI, "Reopen persistent handles");
860 INIT_LIST_HEAD(&tmp_list);
862 /* list all files open on tree connection, reopen resilient handles */
863 spin_lock(&tcon->open_file_lock);
864 list_for_each(tmp, &tcon->openFileList) {
865 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
866 if (!open_file->invalidHandle)
868 cifsFileInfo_get(open_file);
869 list_add_tail(&open_file->rlist, &tmp_list);
871 spin_unlock(&tcon->open_file_lock);
873 list_for_each_safe(tmp, tmp1, &tmp_list) {
874 open_file = list_entry(tmp, struct cifsFileInfo, rlist);
875 if (cifs_reopen_file(open_file, false /* do not flush */))
876 tcon->need_reopen_files = true;
877 list_del_init(&open_file->rlist);
878 cifsFileInfo_put(open_file);
882 int cifs_closedir(struct inode *inode, struct file *file)
886 struct cifsFileInfo *cfile = file->private_data;
887 struct cifs_tcon *tcon;
888 struct TCP_Server_Info *server;
891 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
897 tcon = tlink_tcon(cfile->tlink);
898 server = tcon->ses->server;
900 cifs_dbg(FYI, "Freeing private data in close dir\n");
901 spin_lock(&cfile->file_info_lock);
902 if (server->ops->dir_needs_close(cfile)) {
903 cfile->invalidHandle = true;
904 spin_unlock(&cfile->file_info_lock);
905 if (server->ops->close_dir)
906 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
909 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
910 /* not much we can do if it fails anyway, ignore rc */
913 spin_unlock(&cfile->file_info_lock);
915 buf = cfile->srch_inf.ntwrk_buf_start;
917 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
918 cfile->srch_inf.ntwrk_buf_start = NULL;
919 if (cfile->srch_inf.smallBuf)
920 cifs_small_buf_release(buf);
922 cifs_buf_release(buf);
925 cifs_put_tlink(cfile->tlink);
926 kfree(file->private_data);
927 file->private_data = NULL;
928 /* BB can we lock the filestruct while this is going on? */
933 static struct cifsLockInfo *
934 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
936 struct cifsLockInfo *lock =
937 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
940 lock->offset = offset;
941 lock->length = length;
943 lock->pid = current->tgid;
945 INIT_LIST_HEAD(&lock->blist);
946 init_waitqueue_head(&lock->block_q);
951 cifs_del_lock_waiters(struct cifsLockInfo *lock)
953 struct cifsLockInfo *li, *tmp;
954 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
955 list_del_init(&li->blist);
956 wake_up(&li->block_q);
960 #define CIFS_LOCK_OP 0
961 #define CIFS_READ_OP 1
962 #define CIFS_WRITE_OP 2
964 /* @rw_check : 0 - no op, 1 - read, 2 - write */
966 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
967 __u64 length, __u8 type, __u16 flags,
968 struct cifsFileInfo *cfile,
969 struct cifsLockInfo **conf_lock, int rw_check)
971 struct cifsLockInfo *li;
972 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
973 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
975 list_for_each_entry(li, &fdlocks->locks, llist) {
976 if (offset + length <= li->offset ||
977 offset >= li->offset + li->length)
979 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
980 server->ops->compare_fids(cfile, cur_cfile)) {
981 /* shared lock prevents write op through the same fid */
982 if (!(li->type & server->vals->shared_lock_type) ||
983 rw_check != CIFS_WRITE_OP)
986 if ((type & server->vals->shared_lock_type) &&
987 ((server->ops->compare_fids(cfile, cur_cfile) &&
988 current->tgid == li->pid) || type == li->type))
990 if (rw_check == CIFS_LOCK_OP &&
991 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
992 server->ops->compare_fids(cfile, cur_cfile))
1002 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1003 __u8 type, __u16 flags,
1004 struct cifsLockInfo **conf_lock, int rw_check)
1007 struct cifs_fid_locks *cur;
1008 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1010 list_for_each_entry(cur, &cinode->llist, llist) {
1011 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1012 flags, cfile, conf_lock,
1022 * Check if there is another lock that prevents us to set the lock (mandatory
1023 * style). If such a lock exists, update the flock structure with its
1024 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1025 * or leave it the same if we can't. Returns 0 if we don't need to request to
1026 * the server or 1 otherwise.
1029 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1030 __u8 type, struct file_lock *flock)
1033 struct cifsLockInfo *conf_lock;
1034 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1035 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1038 down_read(&cinode->lock_sem);
1040 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1041 flock->fl_flags, &conf_lock,
1044 flock->fl_start = conf_lock->offset;
1045 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1046 flock->fl_pid = conf_lock->pid;
1047 if (conf_lock->type & server->vals->shared_lock_type)
1048 flock->fl_type = F_RDLCK;
1050 flock->fl_type = F_WRLCK;
1051 } else if (!cinode->can_cache_brlcks)
1054 flock->fl_type = F_UNLCK;
1056 up_read(&cinode->lock_sem);
1061 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1063 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1064 cifs_down_write(&cinode->lock_sem);
1065 list_add_tail(&lock->llist, &cfile->llist->locks);
1066 up_write(&cinode->lock_sem);
1070 * Set the byte-range lock (mandatory style). Returns:
1071 * 1) 0, if we set the lock and don't need to request to the server;
1072 * 2) 1, if no locks prevent us but we need to request to the server;
1073 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1076 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1079 struct cifsLockInfo *conf_lock;
1080 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1086 cifs_down_write(&cinode->lock_sem);
1088 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1089 lock->type, lock->flags, &conf_lock,
1091 if (!exist && cinode->can_cache_brlcks) {
1092 list_add_tail(&lock->llist, &cfile->llist->locks);
1093 up_write(&cinode->lock_sem);
1102 list_add_tail(&lock->blist, &conf_lock->blist);
1103 up_write(&cinode->lock_sem);
1104 rc = wait_event_interruptible(lock->block_q,
1105 (lock->blist.prev == &lock->blist) &&
1106 (lock->blist.next == &lock->blist));
1109 cifs_down_write(&cinode->lock_sem);
1110 list_del_init(&lock->blist);
1113 up_write(&cinode->lock_sem);
1118 * Check if there is another lock that prevents us to set the lock (posix
1119 * style). If such a lock exists, update the flock structure with its
1120 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1121 * or leave it the same if we can't. Returns 0 if we don't need to request to
1122 * the server or 1 otherwise.
1125 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1128 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1129 unsigned char saved_type = flock->fl_type;
1131 if ((flock->fl_flags & FL_POSIX) == 0)
1134 down_read(&cinode->lock_sem);
1135 posix_test_lock(file, flock);
1137 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1138 flock->fl_type = saved_type;
1142 up_read(&cinode->lock_sem);
1147 * Set the byte-range lock (posix style). Returns:
1148 * 1) 0, if we set the lock and don't need to request to the server;
1149 * 2) 1, if we need to request to the server;
1150 * 3) <0, if the error occurs while setting the lock.
1153 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1155 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1158 if ((flock->fl_flags & FL_POSIX) == 0)
1162 cifs_down_write(&cinode->lock_sem);
1163 if (!cinode->can_cache_brlcks) {
1164 up_write(&cinode->lock_sem);
1168 rc = posix_lock_file(file, flock, NULL);
1169 up_write(&cinode->lock_sem);
1170 if (rc == FILE_LOCK_DEFERRED) {
1171 rc = wait_event_interruptible(flock->fl_wait,
1172 list_empty(&flock->fl_blocked_member));
1175 locks_delete_block(flock);
1181 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1184 int rc = 0, stored_rc;
1185 struct cifsLockInfo *li, *tmp;
1186 struct cifs_tcon *tcon;
1187 unsigned int num, max_num, max_buf;
1188 LOCKING_ANDX_RANGE *buf, *cur;
1189 static const int types[] = {
1190 LOCKING_ANDX_LARGE_FILES,
1191 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1196 tcon = tlink_tcon(cfile->tlink);
1199 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1200 * and check it before using.
1202 max_buf = tcon->ses->server->maxBuf;
1203 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1208 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1210 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1212 max_num = (max_buf - sizeof(struct smb_hdr)) /
1213 sizeof(LOCKING_ANDX_RANGE);
1214 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1220 for (i = 0; i < 2; i++) {
1223 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1224 if (li->type != types[i])
1226 cur->Pid = cpu_to_le16(li->pid);
1227 cur->LengthLow = cpu_to_le32((u32)li->length);
1228 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1229 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1230 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1231 if (++num == max_num) {
1232 stored_rc = cifs_lockv(xid, tcon,
1234 (__u8)li->type, 0, num,
1245 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1246 (__u8)types[i], 0, num, buf);
1258 hash_lockowner(fl_owner_t owner)
1260 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1263 struct lock_to_push {
1264 struct list_head llist;
1273 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1275 struct inode *inode = d_inode(cfile->dentry);
1276 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1277 struct file_lock *flock;
1278 struct file_lock_context *flctx = inode->i_flctx;
1279 unsigned int count = 0, i;
1280 int rc = 0, xid, type;
1281 struct list_head locks_to_send, *el;
1282 struct lock_to_push *lck, *tmp;
1290 spin_lock(&flctx->flc_lock);
1291 list_for_each(el, &flctx->flc_posix) {
1294 spin_unlock(&flctx->flc_lock);
1296 INIT_LIST_HEAD(&locks_to_send);
1299 * Allocating count locks is enough because no FL_POSIX locks can be
1300 * added to the list while we are holding cinode->lock_sem that
1301 * protects locking operations of this inode.
1303 for (i = 0; i < count; i++) {
1304 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1309 list_add_tail(&lck->llist, &locks_to_send);
1312 el = locks_to_send.next;
1313 spin_lock(&flctx->flc_lock);
1314 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1315 if (el == &locks_to_send) {
1317 * The list ended. We don't have enough allocated
1318 * structures - something is really wrong.
1320 cifs_dbg(VFS, "Can't push all brlocks!\n");
1323 length = 1 + flock->fl_end - flock->fl_start;
1324 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1328 lck = list_entry(el, struct lock_to_push, llist);
1329 lck->pid = hash_lockowner(flock->fl_owner);
1330 lck->netfid = cfile->fid.netfid;
1331 lck->length = length;
1333 lck->offset = flock->fl_start;
1335 spin_unlock(&flctx->flc_lock);
1337 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1340 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1341 lck->offset, lck->length, NULL,
1345 list_del(&lck->llist);
1353 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1354 list_del(&lck->llist);
1361 cifs_push_locks(struct cifsFileInfo *cfile)
1363 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1364 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1365 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1368 /* we are going to update can_cache_brlcks here - need a write access */
1369 cifs_down_write(&cinode->lock_sem);
1370 if (!cinode->can_cache_brlcks) {
1371 up_write(&cinode->lock_sem);
1375 if (cap_unix(tcon->ses) &&
1376 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1377 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1378 rc = cifs_push_posix_locks(cfile);
1380 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1382 cinode->can_cache_brlcks = false;
1383 up_write(&cinode->lock_sem);
1388 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1389 bool *wait_flag, struct TCP_Server_Info *server)
1391 if (flock->fl_flags & FL_POSIX)
1392 cifs_dbg(FYI, "Posix\n");
1393 if (flock->fl_flags & FL_FLOCK)
1394 cifs_dbg(FYI, "Flock\n");
1395 if (flock->fl_flags & FL_SLEEP) {
1396 cifs_dbg(FYI, "Blocking lock\n");
1399 if (flock->fl_flags & FL_ACCESS)
1400 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1401 if (flock->fl_flags & FL_LEASE)
1402 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1403 if (flock->fl_flags &
1404 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1405 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1406 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1408 *type = server->vals->large_lock_type;
1409 if (flock->fl_type == F_WRLCK) {
1410 cifs_dbg(FYI, "F_WRLCK\n");
1411 *type |= server->vals->exclusive_lock_type;
1413 } else if (flock->fl_type == F_UNLCK) {
1414 cifs_dbg(FYI, "F_UNLCK\n");
1415 *type |= server->vals->unlock_lock_type;
1417 /* Check if unlock includes more than one lock range */
1418 } else if (flock->fl_type == F_RDLCK) {
1419 cifs_dbg(FYI, "F_RDLCK\n");
1420 *type |= server->vals->shared_lock_type;
1422 } else if (flock->fl_type == F_EXLCK) {
1423 cifs_dbg(FYI, "F_EXLCK\n");
1424 *type |= server->vals->exclusive_lock_type;
1426 } else if (flock->fl_type == F_SHLCK) {
1427 cifs_dbg(FYI, "F_SHLCK\n");
1428 *type |= server->vals->shared_lock_type;
1431 cifs_dbg(FYI, "Unknown type of lock\n");
1435 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1436 bool wait_flag, bool posix_lck, unsigned int xid)
1439 __u64 length = 1 + flock->fl_end - flock->fl_start;
1440 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1441 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1442 struct TCP_Server_Info *server = tcon->ses->server;
1443 __u16 netfid = cfile->fid.netfid;
1446 int posix_lock_type;
1448 rc = cifs_posix_lock_test(file, flock);
1452 if (type & server->vals->shared_lock_type)
1453 posix_lock_type = CIFS_RDLCK;
1455 posix_lock_type = CIFS_WRLCK;
1456 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1457 hash_lockowner(flock->fl_owner),
1458 flock->fl_start, length, flock,
1459 posix_lock_type, wait_flag);
1463 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1467 /* BB we could chain these into one lock request BB */
1468 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1471 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1473 flock->fl_type = F_UNLCK;
1475 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1480 if (type & server->vals->shared_lock_type) {
1481 flock->fl_type = F_WRLCK;
1485 type &= ~server->vals->exclusive_lock_type;
1487 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1488 type | server->vals->shared_lock_type,
1491 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1492 type | server->vals->shared_lock_type, 0, 1, false);
1493 flock->fl_type = F_RDLCK;
1495 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1498 flock->fl_type = F_WRLCK;
1504 cifs_move_llist(struct list_head *source, struct list_head *dest)
1506 struct list_head *li, *tmp;
1507 list_for_each_safe(li, tmp, source)
1508 list_move(li, dest);
1512 cifs_free_llist(struct list_head *llist)
1514 struct cifsLockInfo *li, *tmp;
1515 list_for_each_entry_safe(li, tmp, llist, llist) {
1516 cifs_del_lock_waiters(li);
1517 list_del(&li->llist);
1523 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1526 int rc = 0, stored_rc;
1527 static const int types[] = {
1528 LOCKING_ANDX_LARGE_FILES,
1529 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1532 unsigned int max_num, num, max_buf;
1533 LOCKING_ANDX_RANGE *buf, *cur;
1534 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1535 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1536 struct cifsLockInfo *li, *tmp;
1537 __u64 length = 1 + flock->fl_end - flock->fl_start;
1538 struct list_head tmp_llist;
1540 INIT_LIST_HEAD(&tmp_llist);
1543 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1544 * and check it before using.
1546 max_buf = tcon->ses->server->maxBuf;
1547 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1550 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1552 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1554 max_num = (max_buf - sizeof(struct smb_hdr)) /
1555 sizeof(LOCKING_ANDX_RANGE);
1556 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1560 cifs_down_write(&cinode->lock_sem);
1561 for (i = 0; i < 2; i++) {
1564 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1565 if (flock->fl_start > li->offset ||
1566 (flock->fl_start + length) <
1567 (li->offset + li->length))
1569 if (current->tgid != li->pid)
1571 if (types[i] != li->type)
1573 if (cinode->can_cache_brlcks) {
1575 * We can cache brlock requests - simply remove
1576 * a lock from the file's list.
1578 list_del(&li->llist);
1579 cifs_del_lock_waiters(li);
1583 cur->Pid = cpu_to_le16(li->pid);
1584 cur->LengthLow = cpu_to_le32((u32)li->length);
1585 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1586 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1587 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1589 * We need to save a lock here to let us add it again to
1590 * the file's list if the unlock range request fails on
1593 list_move(&li->llist, &tmp_llist);
1594 if (++num == max_num) {
1595 stored_rc = cifs_lockv(xid, tcon,
1597 li->type, num, 0, buf);
1600 * We failed on the unlock range
1601 * request - add all locks from the tmp
1602 * list to the head of the file's list.
1604 cifs_move_llist(&tmp_llist,
1605 &cfile->llist->locks);
1609 * The unlock range request succeed -
1610 * free the tmp list.
1612 cifs_free_llist(&tmp_llist);
1619 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1620 types[i], num, 0, buf);
1622 cifs_move_llist(&tmp_llist,
1623 &cfile->llist->locks);
1626 cifs_free_llist(&tmp_llist);
1630 up_write(&cinode->lock_sem);
1636 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1637 bool wait_flag, bool posix_lck, int lock, int unlock,
1641 __u64 length = 1 + flock->fl_end - flock->fl_start;
1642 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1643 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1644 struct TCP_Server_Info *server = tcon->ses->server;
1645 struct inode *inode = d_inode(cfile->dentry);
1648 int posix_lock_type;
1650 rc = cifs_posix_lock_set(file, flock);
1654 if (type & server->vals->shared_lock_type)
1655 posix_lock_type = CIFS_RDLCK;
1657 posix_lock_type = CIFS_WRLCK;
1660 posix_lock_type = CIFS_UNLCK;
1662 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1663 hash_lockowner(flock->fl_owner),
1664 flock->fl_start, length,
1665 NULL, posix_lock_type, wait_flag);
1670 struct cifsLockInfo *lock;
1672 lock = cifs_lock_init(flock->fl_start, length, type,
1677 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1686 * Windows 7 server can delay breaking lease from read to None
1687 * if we set a byte-range lock on a file - break it explicitly
1688 * before sending the lock to the server to be sure the next
1689 * read won't conflict with non-overlapted locks due to
1692 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1693 CIFS_CACHE_READ(CIFS_I(inode))) {
1694 cifs_zap_mapping(inode);
1695 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1697 CIFS_I(inode)->oplock = 0;
1700 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1701 type, 1, 0, wait_flag);
1707 cifs_lock_add(cfile, lock);
1709 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1712 if (flock->fl_flags & FL_POSIX) {
1714 * If this is a request to remove all locks because we
1715 * are closing the file, it doesn't matter if the
1716 * unlocking failed as both cifs.ko and the SMB server
1717 * remove the lock on file close
1720 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1721 if (!(flock->fl_flags & FL_CLOSE))
1724 rc = locks_lock_file_wait(file, flock);
1729 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1732 int lock = 0, unlock = 0;
1733 bool wait_flag = false;
1734 bool posix_lck = false;
1735 struct cifs_sb_info *cifs_sb;
1736 struct cifs_tcon *tcon;
1737 struct cifsFileInfo *cfile;
1743 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1744 cmd, flock->fl_flags, flock->fl_type,
1745 flock->fl_start, flock->fl_end);
1747 cfile = (struct cifsFileInfo *)file->private_data;
1748 tcon = tlink_tcon(cfile->tlink);
1750 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1752 cifs_sb = CIFS_FILE_SB(file);
1754 if (cap_unix(tcon->ses) &&
1755 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1756 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1759 * BB add code here to normalize offset and length to account for
1760 * negative length which we can not accept over the wire.
1762 if (IS_GETLK(cmd)) {
1763 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1768 if (!lock && !unlock) {
1770 * if no lock or unlock then nothing to do since we do not
1777 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1784 * update the file size (if needed) after a write. Should be called with
1785 * the inode->i_lock held
1788 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1789 unsigned int bytes_written)
1791 loff_t end_of_write = offset + bytes_written;
1793 if (end_of_write > cifsi->server_eof)
1794 cifsi->server_eof = end_of_write;
1798 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1799 size_t write_size, loff_t *offset)
1802 unsigned int bytes_written = 0;
1803 unsigned int total_written;
1804 struct cifs_tcon *tcon;
1805 struct TCP_Server_Info *server;
1807 struct dentry *dentry = open_file->dentry;
1808 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
1809 struct cifs_io_parms io_parms;
1811 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1812 write_size, *offset, dentry);
1814 tcon = tlink_tcon(open_file->tlink);
1815 server = tcon->ses->server;
1817 if (!server->ops->sync_write)
1822 for (total_written = 0; write_size > total_written;
1823 total_written += bytes_written) {
1825 while (rc == -EAGAIN) {
1829 if (open_file->invalidHandle) {
1830 /* we could deadlock if we called
1831 filemap_fdatawait from here so tell
1832 reopen_file not to flush data to
1834 rc = cifs_reopen_file(open_file, false);
1839 len = min(server->ops->wp_retry_size(d_inode(dentry)),
1840 (unsigned int)write_size - total_written);
1841 /* iov[0] is reserved for smb header */
1842 iov[1].iov_base = (char *)write_data + total_written;
1843 iov[1].iov_len = len;
1845 io_parms.tcon = tcon;
1846 io_parms.offset = *offset;
1847 io_parms.length = len;
1848 rc = server->ops->sync_write(xid, &open_file->fid,
1849 &io_parms, &bytes_written, iov, 1);
1851 if (rc || (bytes_written == 0)) {
1859 spin_lock(&d_inode(dentry)->i_lock);
1860 cifs_update_eof(cifsi, *offset, bytes_written);
1861 spin_unlock(&d_inode(dentry)->i_lock);
1862 *offset += bytes_written;
1866 cifs_stats_bytes_written(tcon, total_written);
1868 if (total_written > 0) {
1869 spin_lock(&d_inode(dentry)->i_lock);
1870 if (*offset > d_inode(dentry)->i_size)
1871 i_size_write(d_inode(dentry), *offset);
1872 spin_unlock(&d_inode(dentry)->i_lock);
1874 mark_inode_dirty_sync(d_inode(dentry));
1876 return total_written;
1879 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1882 struct cifsFileInfo *open_file = NULL;
1883 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1885 /* only filter by fsuid on multiuser mounts */
1886 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1889 spin_lock(&cifs_inode->open_file_lock);
1890 /* we could simply get the first_list_entry since write-only entries
1891 are always at the end of the list but since the first entry might
1892 have a close pending, we go through the whole list */
1893 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1894 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1896 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
1897 if (!open_file->invalidHandle) {
1898 /* found a good file */
1899 /* lock it so it will not be closed on us */
1900 cifsFileInfo_get(open_file);
1901 spin_unlock(&cifs_inode->open_file_lock);
1903 } /* else might as well continue, and look for
1904 another, or simply have the caller reopen it
1905 again rather than trying to fix this handle */
1906 } else /* write only file */
1907 break; /* write only files are last so must be done */
1909 spin_unlock(&cifs_inode->open_file_lock);
1913 /* Return -EBADF if no handle is found and general rc otherwise */
1915 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
1916 struct cifsFileInfo **ret_file)
1918 struct cifsFileInfo *open_file, *inv_file = NULL;
1919 struct cifs_sb_info *cifs_sb;
1920 bool any_available = false;
1922 unsigned int refind = 0;
1923 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
1924 bool with_delete = flags & FIND_WR_WITH_DELETE;
1928 * Having a null inode here (because mapping->host was set to zero by
1929 * the VFS or MM) should not happen but we had reports of on oops (due
1930 * to it being zero) during stress testcases so we need to check for it
1933 if (cifs_inode == NULL) {
1934 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
1939 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1941 /* only filter by fsuid on multiuser mounts */
1942 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1945 spin_lock(&cifs_inode->open_file_lock);
1947 if (refind > MAX_REOPEN_ATT) {
1948 spin_unlock(&cifs_inode->open_file_lock);
1951 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1952 if (!any_available && open_file->pid != current->tgid)
1954 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1956 if (with_delete && !(open_file->fid.access & DELETE))
1958 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
1959 if (!open_file->invalidHandle) {
1960 /* found a good writable file */
1961 cifsFileInfo_get(open_file);
1962 spin_unlock(&cifs_inode->open_file_lock);
1963 *ret_file = open_file;
1967 inv_file = open_file;
1971 /* couldn't find useable FH with same pid, try any available */
1972 if (!any_available) {
1973 any_available = true;
1974 goto refind_writable;
1978 any_available = false;
1979 cifsFileInfo_get(inv_file);
1982 spin_unlock(&cifs_inode->open_file_lock);
1985 rc = cifs_reopen_file(inv_file, false);
1987 *ret_file = inv_file;
1991 spin_lock(&cifs_inode->open_file_lock);
1992 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
1993 spin_unlock(&cifs_inode->open_file_lock);
1994 cifsFileInfo_put(inv_file);
1997 spin_lock(&cifs_inode->open_file_lock);
1998 goto refind_writable;
2004 struct cifsFileInfo *
2005 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2007 struct cifsFileInfo *cfile;
2010 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2012 cifs_dbg(FYI, "couldn't find writable handle rc=%d", rc);
2018 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2020 struct cifsFileInfo **ret_file)
2022 struct list_head *tmp;
2023 struct cifsFileInfo *cfile;
2024 struct cifsInodeInfo *cinode;
2029 spin_lock(&tcon->open_file_lock);
2030 list_for_each(tmp, &tcon->openFileList) {
2031 cfile = list_entry(tmp, struct cifsFileInfo,
2033 full_path = build_path_from_dentry(cfile->dentry);
2034 if (full_path == NULL) {
2035 spin_unlock(&tcon->open_file_lock);
2038 if (strcmp(full_path, name)) {
2044 cinode = CIFS_I(d_inode(cfile->dentry));
2045 spin_unlock(&tcon->open_file_lock);
2046 return cifs_get_writable_file(cinode, flags, ret_file);
2049 spin_unlock(&tcon->open_file_lock);
2054 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2055 struct cifsFileInfo **ret_file)
2057 struct list_head *tmp;
2058 struct cifsFileInfo *cfile;
2059 struct cifsInodeInfo *cinode;
2064 spin_lock(&tcon->open_file_lock);
2065 list_for_each(tmp, &tcon->openFileList) {
2066 cfile = list_entry(tmp, struct cifsFileInfo,
2068 full_path = build_path_from_dentry(cfile->dentry);
2069 if (full_path == NULL) {
2070 spin_unlock(&tcon->open_file_lock);
2073 if (strcmp(full_path, name)) {
2079 cinode = CIFS_I(d_inode(cfile->dentry));
2080 spin_unlock(&tcon->open_file_lock);
2081 *ret_file = find_readable_file(cinode, 0);
2082 return *ret_file ? 0 : -ENOENT;
2085 spin_unlock(&tcon->open_file_lock);
2089 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2091 struct address_space *mapping = page->mapping;
2092 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2095 int bytes_written = 0;
2096 struct inode *inode;
2097 struct cifsFileInfo *open_file;
2099 if (!mapping || !mapping->host)
2102 inode = page->mapping->host;
2104 offset += (loff_t)from;
2105 write_data = kmap(page);
2108 if ((to > PAGE_SIZE) || (from > to)) {
2113 /* racing with truncate? */
2114 if (offset > mapping->host->i_size) {
2116 return 0; /* don't care */
2119 /* check to make sure that we are not extending the file */
2120 if (mapping->host->i_size - offset < (loff_t)to)
2121 to = (unsigned)(mapping->host->i_size - offset);
2123 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2126 bytes_written = cifs_write(open_file, open_file->pid,
2127 write_data, to - from, &offset);
2128 cifsFileInfo_put(open_file);
2129 /* Does mm or vfs already set times? */
2130 inode->i_atime = inode->i_mtime = current_time(inode);
2131 if ((bytes_written > 0) && (offset))
2133 else if (bytes_written < 0)
2138 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2139 if (!is_retryable_error(rc))
2147 static struct cifs_writedata *
2148 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2149 pgoff_t end, pgoff_t *index,
2150 unsigned int *found_pages)
2152 struct cifs_writedata *wdata;
2154 wdata = cifs_writedata_alloc((unsigned int)tofind,
2155 cifs_writev_complete);
2159 *found_pages = find_get_pages_range_tag(mapping, index, end,
2160 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2165 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2166 struct address_space *mapping,
2167 struct writeback_control *wbc,
2168 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2170 unsigned int nr_pages = 0, i;
2173 for (i = 0; i < found_pages; i++) {
2174 page = wdata->pages[i];
2176 * At this point we hold neither the i_pages lock nor the
2177 * page lock: the page may be truncated or invalidated
2178 * (changing page->mapping to NULL), or even swizzled
2179 * back from swapper_space to tmpfs file mapping
2184 else if (!trylock_page(page))
2187 if (unlikely(page->mapping != mapping)) {
2192 if (!wbc->range_cyclic && page->index > end) {
2198 if (*next && (page->index != *next)) {
2199 /* Not next consecutive page */
2204 if (wbc->sync_mode != WB_SYNC_NONE)
2205 wait_on_page_writeback(page);
2207 if (PageWriteback(page) ||
2208 !clear_page_dirty_for_io(page)) {
2214 * This actually clears the dirty bit in the radix tree.
2215 * See cifs_writepage() for more commentary.
2217 set_page_writeback(page);
2218 if (page_offset(page) >= i_size_read(mapping->host)) {
2221 end_page_writeback(page);
2225 wdata->pages[i] = page;
2226 *next = page->index + 1;
2230 /* reset index to refind any pages skipped */
2232 *index = wdata->pages[0]->index + 1;
2234 /* put any pages we aren't going to use */
2235 for (i = nr_pages; i < found_pages; i++) {
2236 put_page(wdata->pages[i]);
2237 wdata->pages[i] = NULL;
2244 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2245 struct address_space *mapping, struct writeback_control *wbc)
2248 struct TCP_Server_Info *server =
2249 tlink_tcon(wdata->cfile->tlink)->ses->server;
2251 wdata->sync_mode = wbc->sync_mode;
2252 wdata->nr_pages = nr_pages;
2253 wdata->offset = page_offset(wdata->pages[0]);
2254 wdata->pagesz = PAGE_SIZE;
2255 wdata->tailsz = min(i_size_read(mapping->host) -
2256 page_offset(wdata->pages[nr_pages - 1]),
2258 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2259 wdata->pid = wdata->cfile->pid;
2261 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2265 if (wdata->cfile->invalidHandle)
2268 rc = server->ops->async_writev(wdata, cifs_writedata_release);
2273 static int cifs_writepages(struct address_space *mapping,
2274 struct writeback_control *wbc)
2276 struct inode *inode = mapping->host;
2277 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2278 struct TCP_Server_Info *server;
2279 bool done = false, scanned = false, range_whole = false;
2281 struct cifs_writedata *wdata;
2282 struct cifsFileInfo *cfile = NULL;
2288 * If wsize is smaller than the page cache size, default to writing
2289 * one page at a time via cifs_writepage
2291 if (cifs_sb->wsize < PAGE_SIZE)
2292 return generic_writepages(mapping, wbc);
2295 if (wbc->range_cyclic) {
2296 index = mapping->writeback_index; /* Start from prev offset */
2299 index = wbc->range_start >> PAGE_SHIFT;
2300 end = wbc->range_end >> PAGE_SHIFT;
2301 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2305 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
2307 while (!done && index <= end) {
2308 unsigned int i, nr_pages, found_pages, wsize;
2309 pgoff_t next = 0, tofind, saved_index = index;
2310 struct cifs_credits credits_on_stack;
2311 struct cifs_credits *credits = &credits_on_stack;
2312 int get_file_rc = 0;
2315 cifsFileInfo_put(cfile);
2317 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2319 /* in case of an error store it to return later */
2323 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2330 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2332 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2337 add_credits_and_wake_if(server, credits, 0);
2341 if (found_pages == 0) {
2342 kref_put(&wdata->refcount, cifs_writedata_release);
2343 add_credits_and_wake_if(server, credits, 0);
2347 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2348 end, &index, &next, &done);
2350 /* nothing to write? */
2351 if (nr_pages == 0) {
2352 kref_put(&wdata->refcount, cifs_writedata_release);
2353 add_credits_and_wake_if(server, credits, 0);
2357 wdata->credits = credits_on_stack;
2358 wdata->cfile = cfile;
2361 if (!wdata->cfile) {
2362 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2364 if (is_retryable_error(get_file_rc))
2369 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2371 for (i = 0; i < nr_pages; ++i)
2372 unlock_page(wdata->pages[i]);
2374 /* send failure -- clean up the mess */
2376 add_credits_and_wake_if(server, &wdata->credits, 0);
2377 for (i = 0; i < nr_pages; ++i) {
2378 if (is_retryable_error(rc))
2379 redirty_page_for_writepage(wbc,
2382 SetPageError(wdata->pages[i]);
2383 end_page_writeback(wdata->pages[i]);
2384 put_page(wdata->pages[i]);
2386 if (!is_retryable_error(rc))
2387 mapping_set_error(mapping, rc);
2389 kref_put(&wdata->refcount, cifs_writedata_release);
2391 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2392 index = saved_index;
2396 /* Return immediately if we received a signal during writing */
2397 if (is_interrupt_error(rc)) {
2402 if (rc != 0 && saved_rc == 0)
2405 wbc->nr_to_write -= nr_pages;
2406 if (wbc->nr_to_write <= 0)
2412 if (!scanned && !done) {
2414 * We hit the last page and there is more work to be done: wrap
2415 * back to the start of the file
2425 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2426 mapping->writeback_index = index;
2429 cifsFileInfo_put(cfile);
2435 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2441 /* BB add check for wbc flags */
2443 if (!PageUptodate(page))
2444 cifs_dbg(FYI, "ppw - page not up to date\n");
2447 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2449 * A writepage() implementation always needs to do either this,
2450 * or re-dirty the page with "redirty_page_for_writepage()" in
2451 * the case of a failure.
2453 * Just unlocking the page will cause the radix tree tag-bits
2454 * to fail to update with the state of the page correctly.
2456 set_page_writeback(page);
2458 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2459 if (is_retryable_error(rc)) {
2460 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2462 redirty_page_for_writepage(wbc, page);
2463 } else if (rc != 0) {
2465 mapping_set_error(page->mapping, rc);
2467 SetPageUptodate(page);
2469 end_page_writeback(page);
2475 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2477 int rc = cifs_writepage_locked(page, wbc);
2482 static int cifs_write_end(struct file *file, struct address_space *mapping,
2483 loff_t pos, unsigned len, unsigned copied,
2484 struct page *page, void *fsdata)
2487 struct inode *inode = mapping->host;
2488 struct cifsFileInfo *cfile = file->private_data;
2489 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2492 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2495 pid = current->tgid;
2497 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2500 if (PageChecked(page)) {
2502 SetPageUptodate(page);
2503 ClearPageChecked(page);
2504 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2505 SetPageUptodate(page);
2507 if (!PageUptodate(page)) {
2509 unsigned offset = pos & (PAGE_SIZE - 1);
2513 /* this is probably better than directly calling
2514 partialpage_write since in this function the file handle is
2515 known which we might as well leverage */
2516 /* BB check if anything else missing out of ppw
2517 such as updating last write time */
2518 page_data = kmap(page);
2519 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2520 /* if (rc < 0) should we set writebehind rc? */
2527 set_page_dirty(page);
2531 spin_lock(&inode->i_lock);
2532 if (pos > inode->i_size)
2533 i_size_write(inode, pos);
2534 spin_unlock(&inode->i_lock);
2543 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2548 struct cifs_tcon *tcon;
2549 struct TCP_Server_Info *server;
2550 struct cifsFileInfo *smbfile = file->private_data;
2551 struct inode *inode = file_inode(file);
2552 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2554 rc = file_write_and_wait_range(file, start, end);
2560 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2563 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2564 rc = cifs_zap_mapping(inode);
2566 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2567 rc = 0; /* don't care about it in fsync */
2571 tcon = tlink_tcon(smbfile->tlink);
2572 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2573 server = tcon->ses->server;
2574 if (server->ops->flush == NULL) {
2576 goto strict_fsync_exit;
2579 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2580 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
2582 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2583 cifsFileInfo_put(smbfile);
2585 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2587 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2595 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2599 struct cifs_tcon *tcon;
2600 struct TCP_Server_Info *server;
2601 struct cifsFileInfo *smbfile = file->private_data;
2602 struct inode *inode = file_inode(file);
2603 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2605 rc = file_write_and_wait_range(file, start, end);
2611 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2614 tcon = tlink_tcon(smbfile->tlink);
2615 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2616 server = tcon->ses->server;
2617 if (server->ops->flush == NULL) {
2622 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2623 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
2625 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2626 cifsFileInfo_put(smbfile);
2628 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2630 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2639 * As file closes, flush all cached write data for this inode checking
2640 * for write behind errors.
2642 int cifs_flush(struct file *file, fl_owner_t id)
2644 struct inode *inode = file_inode(file);
2647 if (file->f_mode & FMODE_WRITE)
2648 rc = filemap_write_and_wait(inode->i_mapping);
2650 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2656 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2661 for (i = 0; i < num_pages; i++) {
2662 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2665 * save number of pages we have already allocated and
2666 * return with ENOMEM error
2675 for (i = 0; i < num_pages; i++)
2682 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2687 clen = min_t(const size_t, len, wsize);
2688 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
2697 cifs_uncached_writedata_release(struct kref *refcount)
2700 struct cifs_writedata *wdata = container_of(refcount,
2701 struct cifs_writedata, refcount);
2703 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
2704 for (i = 0; i < wdata->nr_pages; i++)
2705 put_page(wdata->pages[i]);
2706 cifs_writedata_release(refcount);
2709 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2712 cifs_uncached_writev_complete(struct work_struct *work)
2714 struct cifs_writedata *wdata = container_of(work,
2715 struct cifs_writedata, work);
2716 struct inode *inode = d_inode(wdata->cfile->dentry);
2717 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2719 spin_lock(&inode->i_lock);
2720 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2721 if (cifsi->server_eof > inode->i_size)
2722 i_size_write(inode, cifsi->server_eof);
2723 spin_unlock(&inode->i_lock);
2725 complete(&wdata->done);
2726 collect_uncached_write_data(wdata->ctx);
2727 /* the below call can possibly free the last ref to aio ctx */
2728 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2732 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2733 size_t *len, unsigned long *num_pages)
2735 size_t save_len, copied, bytes, cur_len = *len;
2736 unsigned long i, nr_pages = *num_pages;
2739 for (i = 0; i < nr_pages; i++) {
2740 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2741 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2744 * If we didn't copy as much as we expected, then that
2745 * may mean we trod into an unmapped area. Stop copying
2746 * at that point. On the next pass through the big
2747 * loop, we'll likely end up getting a zero-length
2748 * write and bailing out of it.
2753 cur_len = save_len - cur_len;
2757 * If we have no data to send, then that probably means that
2758 * the copy above failed altogether. That's most likely because
2759 * the address in the iovec was bogus. Return -EFAULT and let
2760 * the caller free anything we allocated and bail out.
2766 * i + 1 now represents the number of pages we actually used in
2767 * the copy phase above.
2774 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
2775 struct cifs_aio_ctx *ctx)
2778 struct cifs_credits credits;
2780 struct TCP_Server_Info *server =
2781 tlink_tcon(wdata->cfile->tlink)->ses->server;
2784 if (wdata->cfile->invalidHandle) {
2785 rc = cifs_reopen_file(wdata->cfile, false);
2794 * Wait for credits to resend this wdata.
2795 * Note: we are attempting to resend the whole wdata not in
2799 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
2804 if (wsize < wdata->bytes) {
2805 add_credits_and_wake_if(server, &credits, 0);
2808 } while (wsize < wdata->bytes);
2809 wdata->credits = credits;
2811 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2814 if (wdata->cfile->invalidHandle)
2817 rc = server->ops->async_writev(wdata,
2818 cifs_uncached_writedata_release);
2821 /* If the write was successfully sent, we are done */
2823 list_add_tail(&wdata->list, wdata_list);
2827 /* Roll back credits and retry if needed */
2828 add_credits_and_wake_if(server, &wdata->credits, 0);
2829 } while (rc == -EAGAIN);
2832 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2837 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2838 struct cifsFileInfo *open_file,
2839 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2840 struct cifs_aio_ctx *ctx)
2844 unsigned long nr_pages, num_pages, i;
2845 struct cifs_writedata *wdata;
2846 struct iov_iter saved_from = *from;
2847 loff_t saved_offset = offset;
2849 struct TCP_Server_Info *server;
2850 struct page **pagevec;
2854 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2855 pid = open_file->pid;
2857 pid = current->tgid;
2859 server = tlink_tcon(open_file->tlink)->ses->server;
2864 struct cifs_credits credits_on_stack;
2865 struct cifs_credits *credits = &credits_on_stack;
2867 if (open_file->invalidHandle) {
2868 rc = cifs_reopen_file(open_file, false);
2875 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2880 cur_len = min_t(const size_t, len, wsize);
2882 if (ctx->direct_io) {
2885 result = iov_iter_get_pages_alloc(
2886 from, &pagevec, cur_len, &start);
2889 "direct_writev couldn't get user pages "
2890 "(rc=%zd) iter type %d iov_offset %zd "
2893 from->iov_offset, from->count);
2897 add_credits_and_wake_if(server, credits, 0);
2900 cur_len = (size_t)result;
2901 iov_iter_advance(from, cur_len);
2904 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
2906 wdata = cifs_writedata_direct_alloc(pagevec,
2907 cifs_uncached_writev_complete);
2910 add_credits_and_wake_if(server, credits, 0);
2915 wdata->page_offset = start;
2918 cur_len - (PAGE_SIZE - start) -
2919 (nr_pages - 2) * PAGE_SIZE :
2922 nr_pages = get_numpages(wsize, len, &cur_len);
2923 wdata = cifs_writedata_alloc(nr_pages,
2924 cifs_uncached_writev_complete);
2927 add_credits_and_wake_if(server, credits, 0);
2931 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2933 kvfree(wdata->pages);
2935 add_credits_and_wake_if(server, credits, 0);
2939 num_pages = nr_pages;
2940 rc = wdata_fill_from_iovec(
2941 wdata, from, &cur_len, &num_pages);
2943 for (i = 0; i < nr_pages; i++)
2944 put_page(wdata->pages[i]);
2945 kvfree(wdata->pages);
2947 add_credits_and_wake_if(server, credits, 0);
2952 * Bring nr_pages down to the number of pages we
2953 * actually used, and free any pages that we didn't use.
2955 for ( ; nr_pages > num_pages; nr_pages--)
2956 put_page(wdata->pages[nr_pages - 1]);
2958 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
2961 wdata->sync_mode = WB_SYNC_ALL;
2962 wdata->nr_pages = nr_pages;
2963 wdata->offset = (__u64)offset;
2964 wdata->cfile = cifsFileInfo_get(open_file);
2966 wdata->bytes = cur_len;
2967 wdata->pagesz = PAGE_SIZE;
2968 wdata->credits = credits_on_stack;
2970 kref_get(&ctx->refcount);
2972 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2975 if (wdata->cfile->invalidHandle)
2978 rc = server->ops->async_writev(wdata,
2979 cifs_uncached_writedata_release);
2983 add_credits_and_wake_if(server, &wdata->credits, 0);
2984 kref_put(&wdata->refcount,
2985 cifs_uncached_writedata_release);
2986 if (rc == -EAGAIN) {
2988 iov_iter_advance(from, offset - saved_offset);
2994 list_add_tail(&wdata->list, wdata_list);
3003 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3005 struct cifs_writedata *wdata, *tmp;
3006 struct cifs_tcon *tcon;
3007 struct cifs_sb_info *cifs_sb;
3008 struct dentry *dentry = ctx->cfile->dentry;
3011 tcon = tlink_tcon(ctx->cfile->tlink);
3012 cifs_sb = CIFS_SB(dentry->d_sb);
3014 mutex_lock(&ctx->aio_mutex);
3016 if (list_empty(&ctx->list)) {
3017 mutex_unlock(&ctx->aio_mutex);
3023 * Wait for and collect replies for any successful sends in order of
3024 * increasing offset. Once an error is hit, then return without waiting
3025 * for any more replies.
3028 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3030 if (!try_wait_for_completion(&wdata->done)) {
3031 mutex_unlock(&ctx->aio_mutex);
3038 ctx->total_len += wdata->bytes;
3040 /* resend call if it's a retryable error */
3041 if (rc == -EAGAIN) {
3042 struct list_head tmp_list;
3043 struct iov_iter tmp_from = ctx->iter;
3045 INIT_LIST_HEAD(&tmp_list);
3046 list_del_init(&wdata->list);
3049 rc = cifs_resend_wdata(
3050 wdata, &tmp_list, ctx);
3052 iov_iter_advance(&tmp_from,
3053 wdata->offset - ctx->pos);
3055 rc = cifs_write_from_iter(wdata->offset,
3056 wdata->bytes, &tmp_from,
3057 ctx->cfile, cifs_sb, &tmp_list,
3060 kref_put(&wdata->refcount,
3061 cifs_uncached_writedata_release);
3064 list_splice(&tmp_list, &ctx->list);
3068 list_del_init(&wdata->list);
3069 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3072 cifs_stats_bytes_written(tcon, ctx->total_len);
3073 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3075 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3077 mutex_unlock(&ctx->aio_mutex);
3079 if (ctx->iocb && ctx->iocb->ki_complete)
3080 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3082 complete(&ctx->done);
3085 static ssize_t __cifs_writev(
3086 struct kiocb *iocb, struct iov_iter *from, bool direct)
3088 struct file *file = iocb->ki_filp;
3089 ssize_t total_written = 0;
3090 struct cifsFileInfo *cfile;
3091 struct cifs_tcon *tcon;
3092 struct cifs_sb_info *cifs_sb;
3093 struct cifs_aio_ctx *ctx;
3094 struct iov_iter saved_from = *from;
3095 size_t len = iov_iter_count(from);
3099 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3100 * In this case, fall back to non-direct write function.
3101 * this could be improved by getting pages directly in ITER_KVEC
3103 if (direct && from->type & ITER_KVEC) {
3104 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3108 rc = generic_write_checks(iocb, from);
3112 cifs_sb = CIFS_FILE_SB(file);
3113 cfile = file->private_data;
3114 tcon = tlink_tcon(cfile->tlink);
3116 if (!tcon->ses->server->ops->async_writev)
3119 ctx = cifs_aio_ctx_alloc();
3123 ctx->cfile = cifsFileInfo_get(cfile);
3125 if (!is_sync_kiocb(iocb))
3128 ctx->pos = iocb->ki_pos;
3131 ctx->direct_io = true;
3135 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3137 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3142 /* grab a lock here due to read response handlers can access ctx */
3143 mutex_lock(&ctx->aio_mutex);
3145 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3146 cfile, cifs_sb, &ctx->list, ctx);
3149 * If at least one write was successfully sent, then discard any rc
3150 * value from the later writes. If the other write succeeds, then
3151 * we'll end up returning whatever was written. If it fails, then
3152 * we'll get a new rc value from that.
3154 if (!list_empty(&ctx->list))
3157 mutex_unlock(&ctx->aio_mutex);
3160 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3164 if (!is_sync_kiocb(iocb)) {
3165 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3166 return -EIOCBQUEUED;
3169 rc = wait_for_completion_killable(&ctx->done);
3171 mutex_lock(&ctx->aio_mutex);
3172 ctx->rc = rc = -EINTR;
3173 total_written = ctx->total_len;
3174 mutex_unlock(&ctx->aio_mutex);
3177 total_written = ctx->total_len;
3180 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3182 if (unlikely(!total_written))
3185 iocb->ki_pos += total_written;
3186 return total_written;
3189 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3191 struct file *file = iocb->ki_filp;
3193 cifs_revalidate_mapping(file->f_inode);
3194 return __cifs_writev(iocb, from, true);
3197 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3199 return __cifs_writev(iocb, from, false);
3203 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3205 struct file *file = iocb->ki_filp;
3206 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3207 struct inode *inode = file->f_mapping->host;
3208 struct cifsInodeInfo *cinode = CIFS_I(inode);
3209 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3214 * We need to hold the sem to be sure nobody modifies lock list
3215 * with a brlock that prevents writing.
3217 down_read(&cinode->lock_sem);
3219 rc = generic_write_checks(iocb, from);
3223 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3224 server->vals->exclusive_lock_type, 0,
3225 NULL, CIFS_WRITE_OP))
3226 rc = __generic_file_write_iter(iocb, from);
3230 up_read(&cinode->lock_sem);
3231 inode_unlock(inode);
3234 rc = generic_write_sync(iocb, rc);
3239 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3241 struct inode *inode = file_inode(iocb->ki_filp);
3242 struct cifsInodeInfo *cinode = CIFS_I(inode);
3243 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3244 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3245 iocb->ki_filp->private_data;
3246 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3249 written = cifs_get_writer(cinode);
3253 if (CIFS_CACHE_WRITE(cinode)) {
3254 if (cap_unix(tcon->ses) &&
3255 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3256 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3257 written = generic_file_write_iter(iocb, from);
3260 written = cifs_writev(iocb, from);
3264 * For non-oplocked files in strict cache mode we need to write the data
3265 * to the server exactly from the pos to pos+len-1 rather than flush all
3266 * affected pages because it may cause a error with mandatory locks on
3267 * these pages but not on the region from pos to ppos+len-1.
3269 written = cifs_user_writev(iocb, from);
3270 if (CIFS_CACHE_READ(cinode)) {
3272 * We have read level caching and we have just sent a write
3273 * request to the server thus making data in the cache stale.
3274 * Zap the cache and set oplock/lease level to NONE to avoid
3275 * reading stale data from the cache. All subsequent read
3276 * operations will read new data from the server.
3278 cifs_zap_mapping(inode);
3279 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3284 cifs_put_writer(cinode);
3288 static struct cifs_readdata *
3289 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3291 struct cifs_readdata *rdata;
3293 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3294 if (rdata != NULL) {
3295 rdata->pages = pages;
3296 kref_init(&rdata->refcount);
3297 INIT_LIST_HEAD(&rdata->list);
3298 init_completion(&rdata->done);
3299 INIT_WORK(&rdata->work, complete);
3305 static struct cifs_readdata *
3306 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3308 struct page **pages =
3309 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3310 struct cifs_readdata *ret = NULL;
3313 ret = cifs_readdata_direct_alloc(pages, complete);
3322 cifs_readdata_release(struct kref *refcount)
3324 struct cifs_readdata *rdata = container_of(refcount,
3325 struct cifs_readdata, refcount);
3326 #ifdef CONFIG_CIFS_SMB_DIRECT
3328 smbd_deregister_mr(rdata->mr);
3333 cifsFileInfo_put(rdata->cfile);
3335 kvfree(rdata->pages);
3340 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3346 for (i = 0; i < nr_pages; i++) {
3347 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3352 rdata->pages[i] = page;
3356 unsigned int nr_page_failed = i;
3358 for (i = 0; i < nr_page_failed; i++) {
3359 put_page(rdata->pages[i]);
3360 rdata->pages[i] = NULL;
3367 cifs_uncached_readdata_release(struct kref *refcount)
3369 struct cifs_readdata *rdata = container_of(refcount,
3370 struct cifs_readdata, refcount);
3373 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3374 for (i = 0; i < rdata->nr_pages; i++) {
3375 put_page(rdata->pages[i]);
3377 cifs_readdata_release(refcount);
3381 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3382 * @rdata: the readdata response with list of pages holding data
3383 * @iter: destination for our data
3385 * This function copies data from a list of pages in a readdata response into
3386 * an array of iovecs. It will first calculate where the data should go
3387 * based on the info in the readdata and then copy the data into that spot.
3390 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3392 size_t remaining = rdata->got_bytes;
3395 for (i = 0; i < rdata->nr_pages; i++) {
3396 struct page *page = rdata->pages[i];
3397 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3400 if (unlikely(iov_iter_is_pipe(iter))) {
3401 void *addr = kmap_atomic(page);
3403 written = copy_to_iter(addr, copy, iter);
3404 kunmap_atomic(addr);
3406 written = copy_page_to_iter(page, 0, copy, iter);
3407 remaining -= written;
3408 if (written < copy && iov_iter_count(iter) > 0)
3411 return remaining ? -EFAULT : 0;
3414 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3417 cifs_uncached_readv_complete(struct work_struct *work)
3419 struct cifs_readdata *rdata = container_of(work,
3420 struct cifs_readdata, work);
3422 complete(&rdata->done);
3423 collect_uncached_read_data(rdata->ctx);
3424 /* the below call can possibly free the last ref to aio ctx */
3425 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3429 uncached_fill_pages(struct TCP_Server_Info *server,
3430 struct cifs_readdata *rdata, struct iov_iter *iter,
3435 unsigned int nr_pages = rdata->nr_pages;
3436 unsigned int page_offset = rdata->page_offset;
3438 rdata->got_bytes = 0;
3439 rdata->tailsz = PAGE_SIZE;
3440 for (i = 0; i < nr_pages; i++) {
3441 struct page *page = rdata->pages[i];
3443 unsigned int segment_size = rdata->pagesz;
3446 segment_size -= page_offset;
3452 /* no need to hold page hostage */
3453 rdata->pages[i] = NULL;
3460 if (len >= segment_size)
3461 /* enough data to fill the page */
3464 rdata->tailsz = len;
3468 result = copy_page_from_iter(
3469 page, page_offset, n, iter);
3470 #ifdef CONFIG_CIFS_SMB_DIRECT
3475 result = cifs_read_page_from_socket(
3476 server, page, page_offset, n);
3480 rdata->got_bytes += result;
3483 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3484 rdata->got_bytes : result;
3488 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3489 struct cifs_readdata *rdata, unsigned int len)
3491 return uncached_fill_pages(server, rdata, NULL, len);
3495 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3496 struct cifs_readdata *rdata,
3497 struct iov_iter *iter)
3499 return uncached_fill_pages(server, rdata, iter, iter->count);
3502 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3503 struct list_head *rdata_list,
3504 struct cifs_aio_ctx *ctx)
3507 struct cifs_credits credits;
3509 struct TCP_Server_Info *server =
3510 tlink_tcon(rdata->cfile->tlink)->ses->server;
3513 if (rdata->cfile->invalidHandle) {
3514 rc = cifs_reopen_file(rdata->cfile, true);
3522 * Wait for credits to resend this rdata.
3523 * Note: we are attempting to resend the whole rdata not in
3527 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3533 if (rsize < rdata->bytes) {
3534 add_credits_and_wake_if(server, &credits, 0);
3537 } while (rsize < rdata->bytes);
3538 rdata->credits = credits;
3540 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3542 if (rdata->cfile->invalidHandle)
3545 rc = server->ops->async_readv(rdata);
3548 /* If the read was successfully sent, we are done */
3550 /* Add to aio pending list */
3551 list_add_tail(&rdata->list, rdata_list);
3555 /* Roll back credits and retry if needed */
3556 add_credits_and_wake_if(server, &rdata->credits, 0);
3557 } while (rc == -EAGAIN);
3560 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3565 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3566 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3567 struct cifs_aio_ctx *ctx)
3569 struct cifs_readdata *rdata;
3570 unsigned int npages, rsize;
3571 struct cifs_credits credits_on_stack;
3572 struct cifs_credits *credits = &credits_on_stack;
3576 struct TCP_Server_Info *server;
3577 struct page **pagevec;
3579 struct iov_iter direct_iov = ctx->iter;
3581 server = tlink_tcon(open_file->tlink)->ses->server;
3583 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3584 pid = open_file->pid;
3586 pid = current->tgid;
3589 iov_iter_advance(&direct_iov, offset - ctx->pos);
3592 if (open_file->invalidHandle) {
3593 rc = cifs_reopen_file(open_file, true);
3600 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
3605 cur_len = min_t(const size_t, len, rsize);
3607 if (ctx->direct_io) {
3610 result = iov_iter_get_pages_alloc(
3611 &direct_iov, &pagevec,
3615 "couldn't get user pages (rc=%zd)"
3617 " iov_offset %zd count %zd\n",
3618 result, direct_iov.type,
3619 direct_iov.iov_offset,
3624 add_credits_and_wake_if(server, credits, 0);
3627 cur_len = (size_t)result;
3628 iov_iter_advance(&direct_iov, cur_len);
3630 rdata = cifs_readdata_direct_alloc(
3631 pagevec, cifs_uncached_readv_complete);
3633 add_credits_and_wake_if(server, credits, 0);
3638 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
3639 rdata->page_offset = start;
3640 rdata->tailsz = npages > 1 ?
3641 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
3646 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
3647 /* allocate a readdata struct */
3648 rdata = cifs_readdata_alloc(npages,
3649 cifs_uncached_readv_complete);
3651 add_credits_and_wake_if(server, credits, 0);
3656 rc = cifs_read_allocate_pages(rdata, npages);
3658 kvfree(rdata->pages);
3660 add_credits_and_wake_if(server, credits, 0);
3664 rdata->tailsz = PAGE_SIZE;
3667 rdata->cfile = cifsFileInfo_get(open_file);
3668 rdata->nr_pages = npages;
3669 rdata->offset = offset;
3670 rdata->bytes = cur_len;
3672 rdata->pagesz = PAGE_SIZE;
3673 rdata->read_into_pages = cifs_uncached_read_into_pages;
3674 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
3675 rdata->credits = credits_on_stack;
3677 kref_get(&ctx->refcount);
3679 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3682 if (rdata->cfile->invalidHandle)
3685 rc = server->ops->async_readv(rdata);
3689 add_credits_and_wake_if(server, &rdata->credits, 0);
3690 kref_put(&rdata->refcount,
3691 cifs_uncached_readdata_release);
3692 if (rc == -EAGAIN) {
3693 iov_iter_revert(&direct_iov, cur_len);
3699 list_add_tail(&rdata->list, rdata_list);
3708 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3710 struct cifs_readdata *rdata, *tmp;
3711 struct iov_iter *to = &ctx->iter;
3712 struct cifs_sb_info *cifs_sb;
3715 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
3717 mutex_lock(&ctx->aio_mutex);
3719 if (list_empty(&ctx->list)) {
3720 mutex_unlock(&ctx->aio_mutex);
3725 /* the loop below should proceed in the order of increasing offsets */
3727 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
3729 if (!try_wait_for_completion(&rdata->done)) {
3730 mutex_unlock(&ctx->aio_mutex);
3734 if (rdata->result == -EAGAIN) {
3735 /* resend call if it's a retryable error */
3736 struct list_head tmp_list;
3737 unsigned int got_bytes = rdata->got_bytes;
3739 list_del_init(&rdata->list);
3740 INIT_LIST_HEAD(&tmp_list);
3743 * Got a part of data and then reconnect has
3744 * happened -- fill the buffer and continue
3747 if (got_bytes && got_bytes < rdata->bytes) {
3749 if (!ctx->direct_io)
3750 rc = cifs_readdata_to_iov(rdata, to);
3752 kref_put(&rdata->refcount,
3753 cifs_uncached_readdata_release);
3758 if (ctx->direct_io) {
3760 * Re-use rdata as this is a
3763 rc = cifs_resend_rdata(
3767 rc = cifs_send_async_read(
3768 rdata->offset + got_bytes,
3769 rdata->bytes - got_bytes,
3770 rdata->cfile, cifs_sb,
3773 kref_put(&rdata->refcount,
3774 cifs_uncached_readdata_release);
3777 list_splice(&tmp_list, &ctx->list);
3780 } else if (rdata->result)
3782 else if (!ctx->direct_io)
3783 rc = cifs_readdata_to_iov(rdata, to);
3785 /* if there was a short read -- discard anything left */
3786 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3789 ctx->total_len += rdata->got_bytes;
3791 list_del_init(&rdata->list);
3792 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3795 if (!ctx->direct_io)
3796 ctx->total_len = ctx->len - iov_iter_count(to);
3798 /* mask nodata case */
3802 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
3804 mutex_unlock(&ctx->aio_mutex);
3806 if (ctx->iocb && ctx->iocb->ki_complete)
3807 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3809 complete(&ctx->done);
3812 static ssize_t __cifs_readv(
3813 struct kiocb *iocb, struct iov_iter *to, bool direct)
3816 struct file *file = iocb->ki_filp;
3817 struct cifs_sb_info *cifs_sb;
3818 struct cifsFileInfo *cfile;
3819 struct cifs_tcon *tcon;
3820 ssize_t rc, total_read = 0;
3821 loff_t offset = iocb->ki_pos;
3822 struct cifs_aio_ctx *ctx;
3825 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
3826 * fall back to data copy read path
3827 * this could be improved by getting pages directly in ITER_KVEC
3829 if (direct && to->type & ITER_KVEC) {
3830 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
3834 len = iov_iter_count(to);
3838 cifs_sb = CIFS_FILE_SB(file);
3839 cfile = file->private_data;
3840 tcon = tlink_tcon(cfile->tlink);
3842 if (!tcon->ses->server->ops->async_readv)
3845 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3846 cifs_dbg(FYI, "attempting read on write only file instance\n");
3848 ctx = cifs_aio_ctx_alloc();
3852 ctx->cfile = cifsFileInfo_get(cfile);
3854 if (!is_sync_kiocb(iocb))
3857 if (iter_is_iovec(to))
3858 ctx->should_dirty = true;
3862 ctx->direct_io = true;
3866 rc = setup_aio_ctx_iter(ctx, to, READ);
3868 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3875 rc = filemap_write_and_wait_range(file->f_inode->i_mapping,
3876 offset, offset + len - 1);
3878 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3883 /* grab a lock here due to read response handlers can access ctx */
3884 mutex_lock(&ctx->aio_mutex);
3886 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
3888 /* if at least one read request send succeeded, then reset rc */
3889 if (!list_empty(&ctx->list))
3892 mutex_unlock(&ctx->aio_mutex);
3895 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3899 if (!is_sync_kiocb(iocb)) {
3900 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3901 return -EIOCBQUEUED;
3904 rc = wait_for_completion_killable(&ctx->done);
3906 mutex_lock(&ctx->aio_mutex);
3907 ctx->rc = rc = -EINTR;
3908 total_read = ctx->total_len;
3909 mutex_unlock(&ctx->aio_mutex);
3912 total_read = ctx->total_len;
3915 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3918 iocb->ki_pos += total_read;
3924 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
3926 return __cifs_readv(iocb, to, true);
3929 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
3931 return __cifs_readv(iocb, to, false);
3935 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
3937 struct inode *inode = file_inode(iocb->ki_filp);
3938 struct cifsInodeInfo *cinode = CIFS_I(inode);
3939 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3940 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3941 iocb->ki_filp->private_data;
3942 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3946 * In strict cache mode we need to read from the server all the time
3947 * if we don't have level II oplock because the server can delay mtime
3948 * change - so we can't make a decision about inode invalidating.
3949 * And we can also fail with pagereading if there are mandatory locks
3950 * on pages affected by this read but not on the region from pos to
3953 if (!CIFS_CACHE_READ(cinode))
3954 return cifs_user_readv(iocb, to);
3956 if (cap_unix(tcon->ses) &&
3957 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3958 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
3959 return generic_file_read_iter(iocb, to);
3962 * We need to hold the sem to be sure nobody modifies lock list
3963 * with a brlock that prevents reading.
3965 down_read(&cinode->lock_sem);
3966 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
3967 tcon->ses->server->vals->shared_lock_type,
3968 0, NULL, CIFS_READ_OP))
3969 rc = generic_file_read_iter(iocb, to);
3970 up_read(&cinode->lock_sem);
3975 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
3978 unsigned int bytes_read = 0;
3979 unsigned int total_read;
3980 unsigned int current_read_size;
3982 struct cifs_sb_info *cifs_sb;
3983 struct cifs_tcon *tcon;
3984 struct TCP_Server_Info *server;
3987 struct cifsFileInfo *open_file;
3988 struct cifs_io_parms io_parms;
3989 int buf_type = CIFS_NO_BUFFER;
3993 cifs_sb = CIFS_FILE_SB(file);
3995 /* FIXME: set up handlers for larger reads and/or convert to async */
3996 rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
3998 if (file->private_data == NULL) {
4003 open_file = file->private_data;
4004 tcon = tlink_tcon(open_file->tlink);
4005 server = tcon->ses->server;
4007 if (!server->ops->sync_read) {
4012 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4013 pid = open_file->pid;
4015 pid = current->tgid;
4017 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4018 cifs_dbg(FYI, "attempting read on write only file instance\n");
4020 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4021 total_read += bytes_read, cur_offset += bytes_read) {
4023 current_read_size = min_t(uint, read_size - total_read,
4026 * For windows me and 9x we do not want to request more
4027 * than it negotiated since it will refuse the read
4030 if (!(tcon->ses->capabilities &
4031 tcon->ses->server->vals->cap_large_files)) {
4032 current_read_size = min_t(uint,
4033 current_read_size, CIFSMaxBufSize);
4035 if (open_file->invalidHandle) {
4036 rc = cifs_reopen_file(open_file, true);
4041 io_parms.tcon = tcon;
4042 io_parms.offset = *offset;
4043 io_parms.length = current_read_size;
4044 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4045 &bytes_read, &cur_offset,
4047 } while (rc == -EAGAIN);
4049 if (rc || (bytes_read == 0)) {
4057 cifs_stats_bytes_read(tcon, total_read);
4058 *offset += bytes_read;
4066 * If the page is mmap'ed into a process' page tables, then we need to make
4067 * sure that it doesn't change while being written back.
4070 cifs_page_mkwrite(struct vm_fault *vmf)
4072 struct page *page = vmf->page;
4075 return VM_FAULT_LOCKED;
4078 static const struct vm_operations_struct cifs_file_vm_ops = {
4079 .fault = filemap_fault,
4080 .map_pages = filemap_map_pages,
4081 .page_mkwrite = cifs_page_mkwrite,
4084 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4087 struct inode *inode = file_inode(file);
4091 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4092 rc = cifs_zap_mapping(inode);
4094 rc = generic_file_mmap(file, vma);
4096 vma->vm_ops = &cifs_file_vm_ops;
4102 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4108 rc = cifs_revalidate_file(file);
4110 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4113 rc = generic_file_mmap(file, vma);
4115 vma->vm_ops = &cifs_file_vm_ops;
4122 cifs_readv_complete(struct work_struct *work)
4124 unsigned int i, got_bytes;
4125 struct cifs_readdata *rdata = container_of(work,
4126 struct cifs_readdata, work);
4128 got_bytes = rdata->got_bytes;
4129 for (i = 0; i < rdata->nr_pages; i++) {
4130 struct page *page = rdata->pages[i];
4132 lru_cache_add_file(page);
4134 if (rdata->result == 0 ||
4135 (rdata->result == -EAGAIN && got_bytes)) {
4136 flush_dcache_page(page);
4137 SetPageUptodate(page);
4142 if (rdata->result == 0 ||
4143 (rdata->result == -EAGAIN && got_bytes))
4144 cifs_readpage_to_fscache(rdata->mapping->host, page);
4146 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4149 rdata->pages[i] = NULL;
4151 kref_put(&rdata->refcount, cifs_readdata_release);
4155 readpages_fill_pages(struct TCP_Server_Info *server,
4156 struct cifs_readdata *rdata, struct iov_iter *iter,
4163 unsigned int nr_pages = rdata->nr_pages;
4164 unsigned int page_offset = rdata->page_offset;
4166 /* determine the eof that the server (probably) has */
4167 eof = CIFS_I(rdata->mapping->host)->server_eof;
4168 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4169 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4171 rdata->got_bytes = 0;
4172 rdata->tailsz = PAGE_SIZE;
4173 for (i = 0; i < nr_pages; i++) {
4174 struct page *page = rdata->pages[i];
4175 unsigned int to_read = rdata->pagesz;
4179 to_read -= page_offset;
4185 if (len >= to_read) {
4187 } else if (len > 0) {
4188 /* enough for partial page, fill and zero the rest */
4189 zero_user(page, len + page_offset, to_read - len);
4190 n = rdata->tailsz = len;
4192 } else if (page->index > eof_index) {
4194 * The VFS will not try to do readahead past the
4195 * i_size, but it's possible that we have outstanding
4196 * writes with gaps in the middle and the i_size hasn't
4197 * caught up yet. Populate those with zeroed out pages
4198 * to prevent the VFS from repeatedly attempting to
4199 * fill them until the writes are flushed.
4201 zero_user(page, 0, PAGE_SIZE);
4202 lru_cache_add_file(page);
4203 flush_dcache_page(page);
4204 SetPageUptodate(page);
4207 rdata->pages[i] = NULL;
4211 /* no need to hold page hostage */
4212 lru_cache_add_file(page);
4215 rdata->pages[i] = NULL;
4221 result = copy_page_from_iter(
4222 page, page_offset, n, iter);
4223 #ifdef CONFIG_CIFS_SMB_DIRECT
4228 result = cifs_read_page_from_socket(
4229 server, page, page_offset, n);
4233 rdata->got_bytes += result;
4236 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4237 rdata->got_bytes : result;
4241 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4242 struct cifs_readdata *rdata, unsigned int len)
4244 return readpages_fill_pages(server, rdata, NULL, len);
4248 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4249 struct cifs_readdata *rdata,
4250 struct iov_iter *iter)
4252 return readpages_fill_pages(server, rdata, iter, iter->count);
4256 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4257 unsigned int rsize, struct list_head *tmplist,
4258 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4260 struct page *page, *tpage;
4261 unsigned int expected_index;
4263 gfp_t gfp = readahead_gfp_mask(mapping);
4265 INIT_LIST_HEAD(tmplist);
4267 page = lru_to_page(page_list);
4270 * Lock the page and put it in the cache. Since no one else
4271 * should have access to this page, we're safe to simply set
4272 * PG_locked without checking it first.
4274 __SetPageLocked(page);
4275 rc = add_to_page_cache_locked(page, mapping,
4278 /* give up if we can't stick it in the cache */
4280 __ClearPageLocked(page);
4284 /* move first page to the tmplist */
4285 *offset = (loff_t)page->index << PAGE_SHIFT;
4288 list_move_tail(&page->lru, tmplist);
4290 /* now try and add more pages onto the request */
4291 expected_index = page->index + 1;
4292 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4293 /* discontinuity ? */
4294 if (page->index != expected_index)
4297 /* would this page push the read over the rsize? */
4298 if (*bytes + PAGE_SIZE > rsize)
4301 __SetPageLocked(page);
4302 rc = add_to_page_cache_locked(page, mapping, page->index, gfp);
4304 __ClearPageLocked(page);
4307 list_move_tail(&page->lru, tmplist);
4308 (*bytes) += PAGE_SIZE;
4315 static int cifs_readpages(struct file *file, struct address_space *mapping,
4316 struct list_head *page_list, unsigned num_pages)
4320 struct list_head tmplist;
4321 struct cifsFileInfo *open_file = file->private_data;
4322 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4323 struct TCP_Server_Info *server;
4329 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4330 * immediately if the cookie is negative
4332 * After this point, every page in the list might have PG_fscache set,
4333 * so we will need to clean that up off of every page we don't use.
4335 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4342 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4343 pid = open_file->pid;
4345 pid = current->tgid;
4348 server = tlink_tcon(open_file->tlink)->ses->server;
4350 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4351 __func__, file, mapping, num_pages);
4354 * Start with the page at end of list and move it to private
4355 * list. Do the same with any following pages until we hit
4356 * the rsize limit, hit an index discontinuity, or run out of
4357 * pages. Issue the async read and then start the loop again
4358 * until the list is empty.
4360 * Note that list order is important. The page_list is in
4361 * the order of declining indexes. When we put the pages in
4362 * the rdata->pages, then we want them in increasing order.
4364 while (!list_empty(page_list) && !err) {
4365 unsigned int i, nr_pages, bytes, rsize;
4367 struct page *page, *tpage;
4368 struct cifs_readdata *rdata;
4369 struct cifs_credits credits_on_stack;
4370 struct cifs_credits *credits = &credits_on_stack;
4372 if (open_file->invalidHandle) {
4373 rc = cifs_reopen_file(open_file, true);
4380 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
4386 * Give up immediately if rsize is too small to read an entire
4387 * page. The VFS will fall back to readpage. We should never
4388 * reach this point however since we set ra_pages to 0 when the
4389 * rsize is smaller than a cache page.
4391 if (unlikely(rsize < PAGE_SIZE)) {
4392 add_credits_and_wake_if(server, credits, 0);
4398 err = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4399 &nr_pages, &offset, &bytes);
4401 add_credits_and_wake_if(server, credits, 0);
4405 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4407 /* best to give up if we're out of mem */
4408 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4409 list_del(&page->lru);
4410 lru_cache_add_file(page);
4415 add_credits_and_wake_if(server, credits, 0);
4419 rdata->cfile = cifsFileInfo_get(open_file);
4420 rdata->mapping = mapping;
4421 rdata->offset = offset;
4422 rdata->bytes = bytes;
4424 rdata->pagesz = PAGE_SIZE;
4425 rdata->tailsz = PAGE_SIZE;
4426 rdata->read_into_pages = cifs_readpages_read_into_pages;
4427 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4428 rdata->credits = credits_on_stack;
4430 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4431 list_del(&page->lru);
4432 rdata->pages[rdata->nr_pages++] = page;
4435 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4438 if (rdata->cfile->invalidHandle)
4441 rc = server->ops->async_readv(rdata);
4445 add_credits_and_wake_if(server, &rdata->credits, 0);
4446 for (i = 0; i < rdata->nr_pages; i++) {
4447 page = rdata->pages[i];
4448 lru_cache_add_file(page);
4452 /* Fallback to the readpage in error/reconnect cases */
4453 kref_put(&rdata->refcount, cifs_readdata_release);
4457 kref_put(&rdata->refcount, cifs_readdata_release);
4460 /* Any pages that have been shown to fscache but didn't get added to
4461 * the pagecache must be uncached before they get returned to the
4464 cifs_fscache_readpages_cancel(mapping->host, page_list);
4470 * cifs_readpage_worker must be called with the page pinned
4472 static int cifs_readpage_worker(struct file *file, struct page *page,
4478 /* Is the page cached? */
4479 rc = cifs_readpage_from_fscache(file_inode(file), page);
4483 read_data = kmap(page);
4484 /* for reads over a certain size could initiate async read ahead */
4486 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4491 cifs_dbg(FYI, "Bytes read %d\n", rc);
4493 /* we do not want atime to be less than mtime, it broke some apps */
4494 file_inode(file)->i_atime = current_time(file_inode(file));
4495 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4496 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4498 file_inode(file)->i_atime = current_time(file_inode(file));
4501 memset(read_data + rc, 0, PAGE_SIZE - rc);
4503 flush_dcache_page(page);
4504 SetPageUptodate(page);
4506 /* send this page to the cache */
4507 cifs_readpage_to_fscache(file_inode(file), page);
4519 static int cifs_readpage(struct file *file, struct page *page)
4521 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
4527 if (file->private_data == NULL) {
4533 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4534 page, (int)offset, (int)offset);
4536 rc = cifs_readpage_worker(file, page, &offset);
4542 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4544 struct cifsFileInfo *open_file;
4546 spin_lock(&cifs_inode->open_file_lock);
4547 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4548 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4549 spin_unlock(&cifs_inode->open_file_lock);
4553 spin_unlock(&cifs_inode->open_file_lock);
4557 /* We do not want to update the file size from server for inodes
4558 open for write - to avoid races with writepage extending
4559 the file - in the future we could consider allowing
4560 refreshing the inode only on increases in the file size
4561 but this is tricky to do without racing with writebehind
4562 page caching in the current Linux kernel design */
4563 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4568 if (is_inode_writable(cifsInode)) {
4569 /* This inode is open for write at least once */
4570 struct cifs_sb_info *cifs_sb;
4572 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4573 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4574 /* since no page cache to corrupt on directio
4575 we can change size safely */
4579 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4587 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4588 loff_t pos, unsigned len, unsigned flags,
4589 struct page **pagep, void **fsdata)
4592 pgoff_t index = pos >> PAGE_SHIFT;
4593 loff_t offset = pos & (PAGE_SIZE - 1);
4594 loff_t page_start = pos & PAGE_MASK;
4599 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4602 page = grab_cache_page_write_begin(mapping, index, flags);
4608 if (PageUptodate(page))
4612 * If we write a full page it will be up to date, no need to read from
4613 * the server. If the write is short, we'll end up doing a sync write
4616 if (len == PAGE_SIZE)
4620 * optimize away the read when we have an oplock, and we're not
4621 * expecting to use any of the data we'd be reading in. That
4622 * is, when the page lies beyond the EOF, or straddles the EOF
4623 * and the write will cover all of the existing data.
4625 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4626 i_size = i_size_read(mapping->host);
4627 if (page_start >= i_size ||
4628 (offset == 0 && (pos + len) >= i_size)) {
4629 zero_user_segments(page, 0, offset,
4633 * PageChecked means that the parts of the page
4634 * to which we're not writing are considered up
4635 * to date. Once the data is copied to the
4636 * page, it can be set uptodate.
4638 SetPageChecked(page);
4643 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4645 * might as well read a page, it is fast enough. If we get
4646 * an error, we don't need to return it. cifs_write_end will
4647 * do a sync write instead since PG_uptodate isn't set.
4649 cifs_readpage_worker(file, page, &page_start);
4654 /* we could try using another file handle if there is one -
4655 but how would we lock it to prevent close of that handle
4656 racing with this read? In any case
4657 this will be written out by write_end so is fine */
4664 static int cifs_release_page(struct page *page, gfp_t gfp)
4666 if (PagePrivate(page))
4669 return cifs_fscache_release_page(page, gfp);
4672 static void cifs_invalidate_page(struct page *page, unsigned int offset,
4673 unsigned int length)
4675 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4677 if (offset == 0 && length == PAGE_SIZE)
4678 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4681 static int cifs_launder_page(struct page *page)
4684 loff_t range_start = page_offset(page);
4685 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
4686 struct writeback_control wbc = {
4687 .sync_mode = WB_SYNC_ALL,
4689 .range_start = range_start,
4690 .range_end = range_end,
4693 cifs_dbg(FYI, "Launder page: %p\n", page);
4695 if (clear_page_dirty_for_io(page))
4696 rc = cifs_writepage_locked(page, &wbc);
4698 cifs_fscache_invalidate_page(page, page->mapping->host);
4702 void cifs_oplock_break(struct work_struct *work)
4704 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4706 struct inode *inode = d_inode(cfile->dentry);
4707 struct cifsInodeInfo *cinode = CIFS_I(inode);
4708 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4709 struct TCP_Server_Info *server = tcon->ses->server;
4711 bool purge_cache = false;
4713 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4714 TASK_UNINTERRUPTIBLE);
4716 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4717 cfile->oplock_epoch, &purge_cache);
4719 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4720 cifs_has_mand_locks(cinode)) {
4721 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4726 if (inode && S_ISREG(inode->i_mode)) {
4727 if (CIFS_CACHE_READ(cinode))
4728 break_lease(inode, O_RDONLY);
4730 break_lease(inode, O_WRONLY);
4731 rc = filemap_fdatawrite(inode->i_mapping);
4732 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4733 rc = filemap_fdatawait(inode->i_mapping);
4734 mapping_set_error(inode->i_mapping, rc);
4735 cifs_zap_mapping(inode);
4737 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4738 if (CIFS_CACHE_WRITE(cinode))
4739 goto oplock_break_ack;
4742 rc = cifs_push_locks(cfile);
4744 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4748 * releasing stale oplock after recent reconnect of smb session using
4749 * a now incorrect file handle is not a data integrity issue but do
4750 * not bother sending an oplock release if session to server still is
4751 * disconnected since oplock already released by the server
4753 if (!cfile->oplock_break_cancelled) {
4754 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4756 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4758 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4759 cifs_done_oplock_break(cinode);
4763 * The presence of cifs_direct_io() in the address space ops vector
4764 * allowes open() O_DIRECT flags which would have failed otherwise.
4766 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4767 * so this method should never be called.
4769 * Direct IO is not yet supported in the cached mode.
4772 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4776 * Eventually need to support direct IO for non forcedirectio mounts
4782 const struct address_space_operations cifs_addr_ops = {
4783 .readpage = cifs_readpage,
4784 .readpages = cifs_readpages,
4785 .writepage = cifs_writepage,
4786 .writepages = cifs_writepages,
4787 .write_begin = cifs_write_begin,
4788 .write_end = cifs_write_end,
4789 .set_page_dirty = __set_page_dirty_nobuffers,
4790 .releasepage = cifs_release_page,
4791 .direct_IO = cifs_direct_io,
4792 .invalidatepage = cifs_invalidate_page,
4793 .launder_page = cifs_launder_page,
4797 * cifs_readpages requires the server to support a buffer large enough to
4798 * contain the header plus one complete page of data. Otherwise, we need
4799 * to leave cifs_readpages out of the address space operations.
4801 const struct address_space_operations cifs_addr_ops_smallbuf = {
4802 .readpage = cifs_readpage,
4803 .writepage = cifs_writepage,
4804 .writepages = cifs_writepages,
4805 .write_begin = cifs_write_begin,
4806 .write_end = cifs_write_end,
4807 .set_page_dirty = __set_page_dirty_nobuffers,
4808 .releasepage = cifs_release_page,
4809 .invalidatepage = cifs_invalidate_page,
4810 .launder_page = cifs_launder_page,