4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/backing-dev.h>
26 #include <linux/stat.h>
27 #include <linux/fcntl.h>
28 #include <linux/pagemap.h>
29 #include <linux/pagevec.h>
30 #include <linux/writeback.h>
31 #include <linux/task_io_accounting_ops.h>
32 #include <linux/delay.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
37 #include <asm/div64.h>
41 #include "cifsproto.h"
42 #include "cifs_unicode.h"
43 #include "cifs_debug.h"
44 #include "cifs_fs_sb.h"
46 #include "smbdirect.h"
48 static inline int cifs_convert_flags(unsigned int flags)
50 if ((flags & O_ACCMODE) == O_RDONLY)
52 else if ((flags & O_ACCMODE) == O_WRONLY)
54 else if ((flags & O_ACCMODE) == O_RDWR) {
55 /* GENERIC_ALL is too much permission to request
56 can cause unnecessary access denied on create */
57 /* return GENERIC_ALL; */
58 return (GENERIC_READ | GENERIC_WRITE);
61 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
62 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
66 static u32 cifs_posix_convert_flags(unsigned int flags)
70 if ((flags & O_ACCMODE) == O_RDONLY)
71 posix_flags = SMB_O_RDONLY;
72 else if ((flags & O_ACCMODE) == O_WRONLY)
73 posix_flags = SMB_O_WRONLY;
74 else if ((flags & O_ACCMODE) == O_RDWR)
75 posix_flags = SMB_O_RDWR;
77 if (flags & O_CREAT) {
78 posix_flags |= SMB_O_CREAT;
80 posix_flags |= SMB_O_EXCL;
81 } else if (flags & O_EXCL)
82 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
83 current->comm, current->tgid);
86 posix_flags |= SMB_O_TRUNC;
87 /* be safe and imply O_SYNC for O_DSYNC */
89 posix_flags |= SMB_O_SYNC;
90 if (flags & O_DIRECTORY)
91 posix_flags |= SMB_O_DIRECTORY;
92 if (flags & O_NOFOLLOW)
93 posix_flags |= SMB_O_NOFOLLOW;
95 posix_flags |= SMB_O_DIRECT;
100 static inline int cifs_get_disposition(unsigned int flags)
102 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
104 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
105 return FILE_OVERWRITE_IF;
106 else if ((flags & O_CREAT) == O_CREAT)
108 else if ((flags & O_TRUNC) == O_TRUNC)
109 return FILE_OVERWRITE;
114 int cifs_posix_open(char *full_path, struct inode **pinode,
115 struct super_block *sb, int mode, unsigned int f_flags,
116 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
119 FILE_UNIX_BASIC_INFO *presp_data;
120 __u32 posix_flags = 0;
121 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
122 struct cifs_fattr fattr;
123 struct tcon_link *tlink;
124 struct cifs_tcon *tcon;
126 cifs_dbg(FYI, "posix open %s\n", full_path);
128 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
129 if (presp_data == NULL)
132 tlink = cifs_sb_tlink(cifs_sb);
138 tcon = tlink_tcon(tlink);
139 mode &= ~current_umask();
141 posix_flags = cifs_posix_convert_flags(f_flags);
142 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
143 poplock, full_path, cifs_sb->local_nls,
144 cifs_remap(cifs_sb));
145 cifs_put_tlink(tlink);
150 if (presp_data->Type == cpu_to_le32(-1))
151 goto posix_open_ret; /* open ok, caller does qpathinfo */
154 goto posix_open_ret; /* caller does not need info */
156 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
158 /* get new inode and set it up */
159 if (*pinode == NULL) {
160 cifs_fill_uniqueid(sb, &fattr);
161 *pinode = cifs_iget(sb, &fattr);
167 cifs_fattr_to_inode(*pinode, &fattr);
176 cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
177 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
178 struct cifs_fid *fid, unsigned int xid)
183 int create_options = CREATE_NOT_DIR;
185 struct TCP_Server_Info *server = tcon->ses->server;
186 struct cifs_open_parms oparms;
188 if (!server->ops->open)
191 desired_access = cifs_convert_flags(f_flags);
193 /*********************************************************************
194 * open flag mapping table:
196 * POSIX Flag CIFS Disposition
197 * ---------- ----------------
198 * O_CREAT FILE_OPEN_IF
199 * O_CREAT | O_EXCL FILE_CREATE
200 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
201 * O_TRUNC FILE_OVERWRITE
202 * none of the above FILE_OPEN
204 * Note that there is not a direct match between disposition
205 * FILE_SUPERSEDE (ie create whether or not file exists although
206 * O_CREAT | O_TRUNC is similar but truncates the existing
207 * file rather than creating a new file as FILE_SUPERSEDE does
208 * (which uses the attributes / metadata passed in on open call)
210 *? O_SYNC is a reasonable match to CIFS writethrough flag
211 *? and the read write flags match reasonably. O_LARGEFILE
212 *? is irrelevant because largefile support is always used
213 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
214 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
215 *********************************************************************/
217 disposition = cifs_get_disposition(f_flags);
219 /* BB pass O_SYNC flag through on file attributes .. BB */
221 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
225 if (backup_cred(cifs_sb))
226 create_options |= CREATE_OPEN_BACKUP_INTENT;
228 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
229 if (f_flags & O_SYNC)
230 create_options |= CREATE_WRITE_THROUGH;
232 if (f_flags & O_DIRECT)
233 create_options |= CREATE_NO_BUFFER;
236 oparms.cifs_sb = cifs_sb;
237 oparms.desired_access = desired_access;
238 oparms.create_options = create_options;
239 oparms.disposition = disposition;
240 oparms.path = full_path;
242 oparms.reconnect = false;
244 rc = server->ops->open(xid, &oparms, oplock, buf);
250 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
253 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
257 server->ops->close(xid, tcon, fid);
268 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
270 struct cifs_fid_locks *cur;
271 bool has_locks = false;
273 down_read(&cinode->lock_sem);
274 list_for_each_entry(cur, &cinode->llist, llist) {
275 if (!list_empty(&cur->locks)) {
280 up_read(&cinode->lock_sem);
285 cifs_down_write(struct rw_semaphore *sem)
287 while (!down_write_trylock(sem))
291 static void cifsFileInfo_put_work(struct work_struct *work);
293 struct cifsFileInfo *
294 cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
295 struct tcon_link *tlink, __u32 oplock)
297 struct dentry *dentry = file_dentry(file);
298 struct inode *inode = d_inode(dentry);
299 struct cifsInodeInfo *cinode = CIFS_I(inode);
300 struct cifsFileInfo *cfile;
301 struct cifs_fid_locks *fdlocks;
302 struct cifs_tcon *tcon = tlink_tcon(tlink);
303 struct TCP_Server_Info *server = tcon->ses->server;
305 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
309 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
315 INIT_LIST_HEAD(&fdlocks->locks);
316 fdlocks->cfile = cfile;
317 cfile->llist = fdlocks;
320 cfile->pid = current->tgid;
321 cfile->uid = current_fsuid();
322 cfile->dentry = dget(dentry);
323 cfile->f_flags = file->f_flags;
324 cfile->invalidHandle = false;
325 cfile->tlink = cifs_get_tlink(tlink);
326 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
327 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
328 mutex_init(&cfile->fh_mutex);
329 spin_lock_init(&cfile->file_info_lock);
331 cifs_sb_active(inode->i_sb);
334 * If the server returned a read oplock and we have mandatory brlocks,
335 * set oplock level to None.
337 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
338 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
342 cifs_down_write(&cinode->lock_sem);
343 list_add(&fdlocks->llist, &cinode->llist);
344 up_write(&cinode->lock_sem);
346 spin_lock(&tcon->open_file_lock);
347 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
348 oplock = fid->pending_open->oplock;
349 list_del(&fid->pending_open->olist);
351 fid->purge_cache = false;
352 server->ops->set_fid(cfile, fid, oplock);
354 list_add(&cfile->tlist, &tcon->openFileList);
355 atomic_inc(&tcon->num_local_opens);
357 /* if readable file instance put first in list*/
358 spin_lock(&cinode->open_file_lock);
359 if (file->f_mode & FMODE_READ)
360 list_add(&cfile->flist, &cinode->openFileList);
362 list_add_tail(&cfile->flist, &cinode->openFileList);
363 spin_unlock(&cinode->open_file_lock);
364 spin_unlock(&tcon->open_file_lock);
366 if (fid->purge_cache)
367 cifs_zap_mapping(inode);
369 file->private_data = cfile;
373 struct cifsFileInfo *
374 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
376 spin_lock(&cifs_file->file_info_lock);
377 cifsFileInfo_get_locked(cifs_file);
378 spin_unlock(&cifs_file->file_info_lock);
382 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
384 struct inode *inode = d_inode(cifs_file->dentry);
385 struct cifsInodeInfo *cifsi = CIFS_I(inode);
386 struct cifsLockInfo *li, *tmp;
387 struct super_block *sb = inode->i_sb;
390 * Delete any outstanding lock records. We'll lose them when the file
393 cifs_down_write(&cifsi->lock_sem);
394 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
395 list_del(&li->llist);
396 cifs_del_lock_waiters(li);
399 list_del(&cifs_file->llist->llist);
400 kfree(cifs_file->llist);
401 up_write(&cifsi->lock_sem);
403 cifs_put_tlink(cifs_file->tlink);
404 dput(cifs_file->dentry);
405 cifs_sb_deactive(sb);
409 static void cifsFileInfo_put_work(struct work_struct *work)
411 struct cifsFileInfo *cifs_file = container_of(work,
412 struct cifsFileInfo, put);
414 cifsFileInfo_put_final(cifs_file);
418 * cifsFileInfo_put - release a reference of file priv data
420 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
422 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
424 _cifsFileInfo_put(cifs_file, true, true);
428 * _cifsFileInfo_put - release a reference of file priv data
430 * This may involve closing the filehandle @cifs_file out on the
431 * server. Must be called without holding tcon->open_file_lock,
432 * cinode->open_file_lock and cifs_file->file_info_lock.
434 * If @wait_for_oplock_handler is true and we are releasing the last
435 * reference, wait for any running oplock break handler of the file
436 * and cancel any pending one. If calling this function from the
437 * oplock break handler, you need to pass false.
440 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
441 bool wait_oplock_handler, bool offload)
443 struct inode *inode = d_inode(cifs_file->dentry);
444 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
445 struct TCP_Server_Info *server = tcon->ses->server;
446 struct cifsInodeInfo *cifsi = CIFS_I(inode);
447 struct super_block *sb = inode->i_sb;
448 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
450 struct cifs_pending_open open;
451 bool oplock_break_cancelled;
453 spin_lock(&tcon->open_file_lock);
454 spin_lock(&cifsi->open_file_lock);
455 spin_lock(&cifs_file->file_info_lock);
456 if (--cifs_file->count > 0) {
457 spin_unlock(&cifs_file->file_info_lock);
458 spin_unlock(&cifsi->open_file_lock);
459 spin_unlock(&tcon->open_file_lock);
462 spin_unlock(&cifs_file->file_info_lock);
464 if (server->ops->get_lease_key)
465 server->ops->get_lease_key(inode, &fid);
467 /* store open in pending opens to make sure we don't miss lease break */
468 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
470 /* remove it from the lists */
471 list_del(&cifs_file->flist);
472 list_del(&cifs_file->tlist);
473 atomic_dec(&tcon->num_local_opens);
475 if (list_empty(&cifsi->openFileList)) {
476 cifs_dbg(FYI, "closing last open instance for inode %p\n",
477 d_inode(cifs_file->dentry));
479 * In strict cache mode we need invalidate mapping on the last
480 * close because it may cause a error when we open this file
481 * again and get at least level II oplock.
483 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
484 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
485 cifs_set_oplock_level(cifsi, 0);
488 spin_unlock(&cifsi->open_file_lock);
489 spin_unlock(&tcon->open_file_lock);
491 oplock_break_cancelled = wait_oplock_handler ?
492 cancel_work_sync(&cifs_file->oplock_break) : false;
494 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
495 struct TCP_Server_Info *server = tcon->ses->server;
499 if (server->ops->close)
500 server->ops->close(xid, tcon, &cifs_file->fid);
504 if (oplock_break_cancelled)
505 cifs_done_oplock_break(cifsi);
507 cifs_del_pending_open(&open);
510 queue_work(fileinfo_put_wq, &cifs_file->put);
512 cifsFileInfo_put_final(cifs_file);
515 int cifs_open(struct inode *inode, struct file *file)
521 struct cifs_sb_info *cifs_sb;
522 struct TCP_Server_Info *server;
523 struct cifs_tcon *tcon;
524 struct tcon_link *tlink;
525 struct cifsFileInfo *cfile = NULL;
526 char *full_path = NULL;
527 bool posix_open_ok = false;
529 struct cifs_pending_open open;
533 cifs_sb = CIFS_SB(inode->i_sb);
534 tlink = cifs_sb_tlink(cifs_sb);
537 return PTR_ERR(tlink);
539 tcon = tlink_tcon(tlink);
540 server = tcon->ses->server;
542 full_path = build_path_from_dentry(file_dentry(file));
543 if (full_path == NULL) {
548 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
549 inode, file->f_flags, full_path);
551 if (file->f_flags & O_DIRECT &&
552 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
553 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
554 file->f_op = &cifs_file_direct_nobrl_ops;
556 file->f_op = &cifs_file_direct_ops;
564 if (!tcon->broken_posix_open && tcon->unix_ext &&
565 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
566 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
567 /* can not refresh inode info since size could be stale */
568 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
569 cifs_sb->mnt_file_mode /* ignored */,
570 file->f_flags, &oplock, &fid.netfid, xid);
572 cifs_dbg(FYI, "posix open succeeded\n");
573 posix_open_ok = true;
574 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
575 if (tcon->ses->serverNOS)
576 cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, disabling posix open support. Check if server update available.\n",
577 tcon->ses->serverName,
578 tcon->ses->serverNOS);
579 tcon->broken_posix_open = true;
580 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
581 (rc != -EOPNOTSUPP)) /* path not found or net err */
584 * Else fallthrough to retry open the old way on network i/o
589 if (server->ops->get_lease_key)
590 server->ops->get_lease_key(inode, &fid);
592 cifs_add_pending_open(&fid, tlink, &open);
594 if (!posix_open_ok) {
595 if (server->ops->get_lease_key)
596 server->ops->get_lease_key(inode, &fid);
598 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
599 file->f_flags, &oplock, &fid, xid);
601 cifs_del_pending_open(&open);
606 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
608 if (server->ops->close)
609 server->ops->close(xid, tcon, &fid);
610 cifs_del_pending_open(&open);
615 cifs_fscache_set_inode_cookie(inode, file);
617 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
619 * Time to set mode which we can not set earlier due to
620 * problems creating new read-only files.
622 struct cifs_unix_set_info_args args = {
623 .mode = inode->i_mode,
624 .uid = INVALID_UID, /* no change */
625 .gid = INVALID_GID, /* no change */
626 .ctime = NO_CHANGE_64,
627 .atime = NO_CHANGE_64,
628 .mtime = NO_CHANGE_64,
631 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
638 cifs_put_tlink(tlink);
642 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
645 * Try to reacquire byte range locks that were released when session
646 * to server was lost.
649 cifs_relock_file(struct cifsFileInfo *cfile)
651 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
652 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
653 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
656 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
657 if (cinode->can_cache_brlcks) {
658 /* can cache locks - no need to relock */
659 up_read(&cinode->lock_sem);
663 if (cap_unix(tcon->ses) &&
664 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
665 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
666 rc = cifs_push_posix_locks(cfile);
668 rc = tcon->ses->server->ops->push_mand_locks(cfile);
670 up_read(&cinode->lock_sem);
675 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
680 struct cifs_sb_info *cifs_sb;
681 struct cifs_tcon *tcon;
682 struct TCP_Server_Info *server;
683 struct cifsInodeInfo *cinode;
685 char *full_path = NULL;
687 int disposition = FILE_OPEN;
688 int create_options = CREATE_NOT_DIR;
689 struct cifs_open_parms oparms;
692 mutex_lock(&cfile->fh_mutex);
693 if (!cfile->invalidHandle) {
694 mutex_unlock(&cfile->fh_mutex);
700 inode = d_inode(cfile->dentry);
701 cifs_sb = CIFS_SB(inode->i_sb);
702 tcon = tlink_tcon(cfile->tlink);
703 server = tcon->ses->server;
706 * Can not grab rename sem here because various ops, including those
707 * that already have the rename sem can end up causing writepage to get
708 * called and if the server was down that means we end up here, and we
709 * can never tell if the caller already has the rename_sem.
711 full_path = build_path_from_dentry(cfile->dentry);
712 if (full_path == NULL) {
714 mutex_unlock(&cfile->fh_mutex);
719 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
720 inode, cfile->f_flags, full_path);
722 if (tcon->ses->server->oplocks)
727 if (tcon->unix_ext && cap_unix(tcon->ses) &&
728 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
729 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
731 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
732 * original open. Must mask them off for a reopen.
734 unsigned int oflags = cfile->f_flags &
735 ~(O_CREAT | O_EXCL | O_TRUNC);
737 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
738 cifs_sb->mnt_file_mode /* ignored */,
739 oflags, &oplock, &cfile->fid.netfid, xid);
741 cifs_dbg(FYI, "posix reopen succeeded\n");
742 oparms.reconnect = true;
746 * fallthrough to retry open the old way on errors, especially
747 * in the reconnect path it is important to retry hard
751 desired_access = cifs_convert_flags(cfile->f_flags);
753 if (backup_cred(cifs_sb))
754 create_options |= CREATE_OPEN_BACKUP_INTENT;
756 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
757 if (cfile->f_flags & O_SYNC)
758 create_options |= CREATE_WRITE_THROUGH;
760 if (cfile->f_flags & O_DIRECT)
761 create_options |= CREATE_NO_BUFFER;
763 if (server->ops->get_lease_key)
764 server->ops->get_lease_key(inode, &cfile->fid);
767 oparms.cifs_sb = cifs_sb;
768 oparms.desired_access = desired_access;
769 oparms.create_options = create_options;
770 oparms.disposition = disposition;
771 oparms.path = full_path;
772 oparms.fid = &cfile->fid;
773 oparms.reconnect = true;
776 * Can not refresh inode by passing in file_info buf to be returned by
777 * ops->open and then calling get_inode_info with returned buf since
778 * file might have write behind data that needs to be flushed and server
779 * version of file size can be stale. If we knew for sure that inode was
780 * not dirty locally we could do this.
782 rc = server->ops->open(xid, &oparms, &oplock, NULL);
783 if (rc == -ENOENT && oparms.reconnect == false) {
784 /* durable handle timeout is expired - open the file again */
785 rc = server->ops->open(xid, &oparms, &oplock, NULL);
786 /* indicate that we need to relock the file */
787 oparms.reconnect = true;
791 mutex_unlock(&cfile->fh_mutex);
792 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
793 cifs_dbg(FYI, "oplock: %d\n", oplock);
794 goto reopen_error_exit;
798 cfile->invalidHandle = false;
799 mutex_unlock(&cfile->fh_mutex);
800 cinode = CIFS_I(inode);
803 rc = filemap_write_and_wait(inode->i_mapping);
804 if (!is_interrupt_error(rc))
805 mapping_set_error(inode->i_mapping, rc);
808 rc = cifs_get_inode_info_unix(&inode, full_path,
811 rc = cifs_get_inode_info(&inode, full_path, NULL,
812 inode->i_sb, xid, NULL);
815 * Else we are writing out data to server already and could deadlock if
816 * we tried to flush data, and since we do not know if we have data that
817 * would invalidate the current end of file on the server we can not go
818 * to the server to get the new inode info.
822 * If the server returned a read oplock and we have mandatory brlocks,
823 * set oplock level to None.
825 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
826 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
830 server->ops->set_fid(cfile, &cfile->fid, oplock);
831 if (oparms.reconnect)
832 cifs_relock_file(cfile);
840 int cifs_close(struct inode *inode, struct file *file)
842 if (file->private_data != NULL) {
843 _cifsFileInfo_put(file->private_data, true, false);
844 file->private_data = NULL;
847 /* return code from the ->release op is always ignored */
852 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
854 struct cifsFileInfo *open_file;
855 struct list_head *tmp;
856 struct list_head *tmp1;
857 struct list_head tmp_list;
859 if (!tcon->use_persistent || !tcon->need_reopen_files)
862 tcon->need_reopen_files = false;
864 cifs_dbg(FYI, "Reopen persistent handles");
865 INIT_LIST_HEAD(&tmp_list);
867 /* list all files open on tree connection, reopen resilient handles */
868 spin_lock(&tcon->open_file_lock);
869 list_for_each(tmp, &tcon->openFileList) {
870 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
871 if (!open_file->invalidHandle)
873 cifsFileInfo_get(open_file);
874 list_add_tail(&open_file->rlist, &tmp_list);
876 spin_unlock(&tcon->open_file_lock);
878 list_for_each_safe(tmp, tmp1, &tmp_list) {
879 open_file = list_entry(tmp, struct cifsFileInfo, rlist);
880 if (cifs_reopen_file(open_file, false /* do not flush */))
881 tcon->need_reopen_files = true;
882 list_del_init(&open_file->rlist);
883 cifsFileInfo_put(open_file);
887 int cifs_closedir(struct inode *inode, struct file *file)
891 struct cifsFileInfo *cfile = file->private_data;
892 struct cifs_tcon *tcon;
893 struct TCP_Server_Info *server;
896 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
902 tcon = tlink_tcon(cfile->tlink);
903 server = tcon->ses->server;
905 cifs_dbg(FYI, "Freeing private data in close dir\n");
906 spin_lock(&cfile->file_info_lock);
907 if (server->ops->dir_needs_close(cfile)) {
908 cfile->invalidHandle = true;
909 spin_unlock(&cfile->file_info_lock);
910 if (server->ops->close_dir)
911 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
914 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
915 /* not much we can do if it fails anyway, ignore rc */
918 spin_unlock(&cfile->file_info_lock);
920 buf = cfile->srch_inf.ntwrk_buf_start;
922 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
923 cfile->srch_inf.ntwrk_buf_start = NULL;
924 if (cfile->srch_inf.smallBuf)
925 cifs_small_buf_release(buf);
927 cifs_buf_release(buf);
930 cifs_put_tlink(cfile->tlink);
931 kfree(file->private_data);
932 file->private_data = NULL;
933 /* BB can we lock the filestruct while this is going on? */
938 static struct cifsLockInfo *
939 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
941 struct cifsLockInfo *lock =
942 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
945 lock->offset = offset;
946 lock->length = length;
948 lock->pid = current->tgid;
950 INIT_LIST_HEAD(&lock->blist);
951 init_waitqueue_head(&lock->block_q);
956 cifs_del_lock_waiters(struct cifsLockInfo *lock)
958 struct cifsLockInfo *li, *tmp;
959 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
960 list_del_init(&li->blist);
961 wake_up(&li->block_q);
965 #define CIFS_LOCK_OP 0
966 #define CIFS_READ_OP 1
967 #define CIFS_WRITE_OP 2
969 /* @rw_check : 0 - no op, 1 - read, 2 - write */
971 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
972 __u64 length, __u8 type, __u16 flags,
973 struct cifsFileInfo *cfile,
974 struct cifsLockInfo **conf_lock, int rw_check)
976 struct cifsLockInfo *li;
977 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
978 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
980 list_for_each_entry(li, &fdlocks->locks, llist) {
981 if (offset + length <= li->offset ||
982 offset >= li->offset + li->length)
984 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
985 server->ops->compare_fids(cfile, cur_cfile)) {
986 /* shared lock prevents write op through the same fid */
987 if (!(li->type & server->vals->shared_lock_type) ||
988 rw_check != CIFS_WRITE_OP)
991 if ((type & server->vals->shared_lock_type) &&
992 ((server->ops->compare_fids(cfile, cur_cfile) &&
993 current->tgid == li->pid) || type == li->type))
995 if (rw_check == CIFS_LOCK_OP &&
996 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
997 server->ops->compare_fids(cfile, cur_cfile))
1007 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1008 __u8 type, __u16 flags,
1009 struct cifsLockInfo **conf_lock, int rw_check)
1012 struct cifs_fid_locks *cur;
1013 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1015 list_for_each_entry(cur, &cinode->llist, llist) {
1016 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1017 flags, cfile, conf_lock,
1027 * Check if there is another lock that prevents us to set the lock (mandatory
1028 * style). If such a lock exists, update the flock structure with its
1029 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1030 * or leave it the same if we can't. Returns 0 if we don't need to request to
1031 * the server or 1 otherwise.
1034 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1035 __u8 type, struct file_lock *flock)
1038 struct cifsLockInfo *conf_lock;
1039 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1040 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1043 down_read(&cinode->lock_sem);
1045 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1046 flock->fl_flags, &conf_lock,
1049 flock->fl_start = conf_lock->offset;
1050 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1051 flock->fl_pid = conf_lock->pid;
1052 if (conf_lock->type & server->vals->shared_lock_type)
1053 flock->fl_type = F_RDLCK;
1055 flock->fl_type = F_WRLCK;
1056 } else if (!cinode->can_cache_brlcks)
1059 flock->fl_type = F_UNLCK;
1061 up_read(&cinode->lock_sem);
1066 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1068 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1069 cifs_down_write(&cinode->lock_sem);
1070 list_add_tail(&lock->llist, &cfile->llist->locks);
1071 up_write(&cinode->lock_sem);
1075 * Set the byte-range lock (mandatory style). Returns:
1076 * 1) 0, if we set the lock and don't need to request to the server;
1077 * 2) 1, if no locks prevent us but we need to request to the server;
1078 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1081 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1084 struct cifsLockInfo *conf_lock;
1085 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1091 cifs_down_write(&cinode->lock_sem);
1093 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1094 lock->type, lock->flags, &conf_lock,
1096 if (!exist && cinode->can_cache_brlcks) {
1097 list_add_tail(&lock->llist, &cfile->llist->locks);
1098 up_write(&cinode->lock_sem);
1107 list_add_tail(&lock->blist, &conf_lock->blist);
1108 up_write(&cinode->lock_sem);
1109 rc = wait_event_interruptible(lock->block_q,
1110 (lock->blist.prev == &lock->blist) &&
1111 (lock->blist.next == &lock->blist));
1114 cifs_down_write(&cinode->lock_sem);
1115 list_del_init(&lock->blist);
1118 up_write(&cinode->lock_sem);
1123 * Check if there is another lock that prevents us to set the lock (posix
1124 * style). If such a lock exists, update the flock structure with its
1125 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1126 * or leave it the same if we can't. Returns 0 if we don't need to request to
1127 * the server or 1 otherwise.
1130 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1133 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1134 unsigned char saved_type = flock->fl_type;
1136 if ((flock->fl_flags & FL_POSIX) == 0)
1139 down_read(&cinode->lock_sem);
1140 posix_test_lock(file, flock);
1142 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1143 flock->fl_type = saved_type;
1147 up_read(&cinode->lock_sem);
1152 * Set the byte-range lock (posix style). Returns:
1153 * 1) 0, if we set the lock and don't need to request to the server;
1154 * 2) 1, if we need to request to the server;
1155 * 3) <0, if the error occurs while setting the lock.
1158 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1160 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1163 if ((flock->fl_flags & FL_POSIX) == 0)
1167 cifs_down_write(&cinode->lock_sem);
1168 if (!cinode->can_cache_brlcks) {
1169 up_write(&cinode->lock_sem);
1173 rc = posix_lock_file(file, flock, NULL);
1174 up_write(&cinode->lock_sem);
1175 if (rc == FILE_LOCK_DEFERRED) {
1176 rc = wait_event_interruptible(flock->fl_wait,
1177 list_empty(&flock->fl_blocked_member));
1180 locks_delete_block(flock);
1186 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1189 int rc = 0, stored_rc;
1190 struct cifsLockInfo *li, *tmp;
1191 struct cifs_tcon *tcon;
1192 unsigned int num, max_num, max_buf;
1193 LOCKING_ANDX_RANGE *buf, *cur;
1194 static const int types[] = {
1195 LOCKING_ANDX_LARGE_FILES,
1196 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1201 tcon = tlink_tcon(cfile->tlink);
1204 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1205 * and check it before using.
1207 max_buf = tcon->ses->server->maxBuf;
1208 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1213 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1215 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1217 max_num = (max_buf - sizeof(struct smb_hdr)) /
1218 sizeof(LOCKING_ANDX_RANGE);
1219 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1225 for (i = 0; i < 2; i++) {
1228 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1229 if (li->type != types[i])
1231 cur->Pid = cpu_to_le16(li->pid);
1232 cur->LengthLow = cpu_to_le32((u32)li->length);
1233 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1234 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1235 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1236 if (++num == max_num) {
1237 stored_rc = cifs_lockv(xid, tcon,
1239 (__u8)li->type, 0, num,
1250 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1251 (__u8)types[i], 0, num, buf);
1263 hash_lockowner(fl_owner_t owner)
1265 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1268 struct lock_to_push {
1269 struct list_head llist;
1278 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1280 struct inode *inode = d_inode(cfile->dentry);
1281 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1282 struct file_lock *flock;
1283 struct file_lock_context *flctx = inode->i_flctx;
1284 unsigned int count = 0, i;
1285 int rc = 0, xid, type;
1286 struct list_head locks_to_send, *el;
1287 struct lock_to_push *lck, *tmp;
1295 spin_lock(&flctx->flc_lock);
1296 list_for_each(el, &flctx->flc_posix) {
1299 spin_unlock(&flctx->flc_lock);
1301 INIT_LIST_HEAD(&locks_to_send);
1304 * Allocating count locks is enough because no FL_POSIX locks can be
1305 * added to the list while we are holding cinode->lock_sem that
1306 * protects locking operations of this inode.
1308 for (i = 0; i < count; i++) {
1309 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1314 list_add_tail(&lck->llist, &locks_to_send);
1317 el = locks_to_send.next;
1318 spin_lock(&flctx->flc_lock);
1319 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1320 if (el == &locks_to_send) {
1322 * The list ended. We don't have enough allocated
1323 * structures - something is really wrong.
1325 cifs_dbg(VFS, "Can't push all brlocks!\n");
1328 length = 1 + flock->fl_end - flock->fl_start;
1329 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1333 lck = list_entry(el, struct lock_to_push, llist);
1334 lck->pid = hash_lockowner(flock->fl_owner);
1335 lck->netfid = cfile->fid.netfid;
1336 lck->length = length;
1338 lck->offset = flock->fl_start;
1340 spin_unlock(&flctx->flc_lock);
1342 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1345 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1346 lck->offset, lck->length, NULL,
1350 list_del(&lck->llist);
1358 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1359 list_del(&lck->llist);
1366 cifs_push_locks(struct cifsFileInfo *cfile)
1368 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1369 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1370 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1373 /* we are going to update can_cache_brlcks here - need a write access */
1374 cifs_down_write(&cinode->lock_sem);
1375 if (!cinode->can_cache_brlcks) {
1376 up_write(&cinode->lock_sem);
1380 if (cap_unix(tcon->ses) &&
1381 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1382 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1383 rc = cifs_push_posix_locks(cfile);
1385 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1387 cinode->can_cache_brlcks = false;
1388 up_write(&cinode->lock_sem);
1393 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1394 bool *wait_flag, struct TCP_Server_Info *server)
1396 if (flock->fl_flags & FL_POSIX)
1397 cifs_dbg(FYI, "Posix\n");
1398 if (flock->fl_flags & FL_FLOCK)
1399 cifs_dbg(FYI, "Flock\n");
1400 if (flock->fl_flags & FL_SLEEP) {
1401 cifs_dbg(FYI, "Blocking lock\n");
1404 if (flock->fl_flags & FL_ACCESS)
1405 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1406 if (flock->fl_flags & FL_LEASE)
1407 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1408 if (flock->fl_flags &
1409 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1410 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1411 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1413 *type = server->vals->large_lock_type;
1414 if (flock->fl_type == F_WRLCK) {
1415 cifs_dbg(FYI, "F_WRLCK\n");
1416 *type |= server->vals->exclusive_lock_type;
1418 } else if (flock->fl_type == F_UNLCK) {
1419 cifs_dbg(FYI, "F_UNLCK\n");
1420 *type |= server->vals->unlock_lock_type;
1422 /* Check if unlock includes more than one lock range */
1423 } else if (flock->fl_type == F_RDLCK) {
1424 cifs_dbg(FYI, "F_RDLCK\n");
1425 *type |= server->vals->shared_lock_type;
1427 } else if (flock->fl_type == F_EXLCK) {
1428 cifs_dbg(FYI, "F_EXLCK\n");
1429 *type |= server->vals->exclusive_lock_type;
1431 } else if (flock->fl_type == F_SHLCK) {
1432 cifs_dbg(FYI, "F_SHLCK\n");
1433 *type |= server->vals->shared_lock_type;
1436 cifs_dbg(FYI, "Unknown type of lock\n");
1440 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1441 bool wait_flag, bool posix_lck, unsigned int xid)
1444 __u64 length = 1 + flock->fl_end - flock->fl_start;
1445 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1446 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1447 struct TCP_Server_Info *server = tcon->ses->server;
1448 __u16 netfid = cfile->fid.netfid;
1451 int posix_lock_type;
1453 rc = cifs_posix_lock_test(file, flock);
1457 if (type & server->vals->shared_lock_type)
1458 posix_lock_type = CIFS_RDLCK;
1460 posix_lock_type = CIFS_WRLCK;
1461 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1462 hash_lockowner(flock->fl_owner),
1463 flock->fl_start, length, flock,
1464 posix_lock_type, wait_flag);
1468 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1472 /* BB we could chain these into one lock request BB */
1473 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1476 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1478 flock->fl_type = F_UNLCK;
1480 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1485 if (type & server->vals->shared_lock_type) {
1486 flock->fl_type = F_WRLCK;
1490 type &= ~server->vals->exclusive_lock_type;
1492 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1493 type | server->vals->shared_lock_type,
1496 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1497 type | server->vals->shared_lock_type, 0, 1, false);
1498 flock->fl_type = F_RDLCK;
1500 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1503 flock->fl_type = F_WRLCK;
1509 cifs_move_llist(struct list_head *source, struct list_head *dest)
1511 struct list_head *li, *tmp;
1512 list_for_each_safe(li, tmp, source)
1513 list_move(li, dest);
1517 cifs_free_llist(struct list_head *llist)
1519 struct cifsLockInfo *li, *tmp;
1520 list_for_each_entry_safe(li, tmp, llist, llist) {
1521 cifs_del_lock_waiters(li);
1522 list_del(&li->llist);
1528 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1531 int rc = 0, stored_rc;
1532 static const int types[] = {
1533 LOCKING_ANDX_LARGE_FILES,
1534 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1537 unsigned int max_num, num, max_buf;
1538 LOCKING_ANDX_RANGE *buf, *cur;
1539 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1540 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1541 struct cifsLockInfo *li, *tmp;
1542 __u64 length = 1 + flock->fl_end - flock->fl_start;
1543 struct list_head tmp_llist;
1545 INIT_LIST_HEAD(&tmp_llist);
1548 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1549 * and check it before using.
1551 max_buf = tcon->ses->server->maxBuf;
1552 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1555 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1557 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1559 max_num = (max_buf - sizeof(struct smb_hdr)) /
1560 sizeof(LOCKING_ANDX_RANGE);
1561 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1565 cifs_down_write(&cinode->lock_sem);
1566 for (i = 0; i < 2; i++) {
1569 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1570 if (flock->fl_start > li->offset ||
1571 (flock->fl_start + length) <
1572 (li->offset + li->length))
1574 if (current->tgid != li->pid)
1576 if (types[i] != li->type)
1578 if (cinode->can_cache_brlcks) {
1580 * We can cache brlock requests - simply remove
1581 * a lock from the file's list.
1583 list_del(&li->llist);
1584 cifs_del_lock_waiters(li);
1588 cur->Pid = cpu_to_le16(li->pid);
1589 cur->LengthLow = cpu_to_le32((u32)li->length);
1590 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1591 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1592 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1594 * We need to save a lock here to let us add it again to
1595 * the file's list if the unlock range request fails on
1598 list_move(&li->llist, &tmp_llist);
1599 if (++num == max_num) {
1600 stored_rc = cifs_lockv(xid, tcon,
1602 li->type, num, 0, buf);
1605 * We failed on the unlock range
1606 * request - add all locks from the tmp
1607 * list to the head of the file's list.
1609 cifs_move_llist(&tmp_llist,
1610 &cfile->llist->locks);
1614 * The unlock range request succeed -
1615 * free the tmp list.
1617 cifs_free_llist(&tmp_llist);
1624 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1625 types[i], num, 0, buf);
1627 cifs_move_llist(&tmp_llist,
1628 &cfile->llist->locks);
1631 cifs_free_llist(&tmp_llist);
1635 up_write(&cinode->lock_sem);
1641 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1642 bool wait_flag, bool posix_lck, int lock, int unlock,
1646 __u64 length = 1 + flock->fl_end - flock->fl_start;
1647 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1648 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1649 struct TCP_Server_Info *server = tcon->ses->server;
1650 struct inode *inode = d_inode(cfile->dentry);
1653 int posix_lock_type;
1655 rc = cifs_posix_lock_set(file, flock);
1659 if (type & server->vals->shared_lock_type)
1660 posix_lock_type = CIFS_RDLCK;
1662 posix_lock_type = CIFS_WRLCK;
1665 posix_lock_type = CIFS_UNLCK;
1667 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1668 hash_lockowner(flock->fl_owner),
1669 flock->fl_start, length,
1670 NULL, posix_lock_type, wait_flag);
1675 struct cifsLockInfo *lock;
1677 lock = cifs_lock_init(flock->fl_start, length, type,
1682 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1691 * Windows 7 server can delay breaking lease from read to None
1692 * if we set a byte-range lock on a file - break it explicitly
1693 * before sending the lock to the server to be sure the next
1694 * read won't conflict with non-overlapted locks due to
1697 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1698 CIFS_CACHE_READ(CIFS_I(inode))) {
1699 cifs_zap_mapping(inode);
1700 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1702 CIFS_I(inode)->oplock = 0;
1705 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1706 type, 1, 0, wait_flag);
1712 cifs_lock_add(cfile, lock);
1714 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1717 if (flock->fl_flags & FL_POSIX) {
1719 * If this is a request to remove all locks because we
1720 * are closing the file, it doesn't matter if the
1721 * unlocking failed as both cifs.ko and the SMB server
1722 * remove the lock on file close
1725 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1726 if (!(flock->fl_flags & FL_CLOSE))
1729 rc = locks_lock_file_wait(file, flock);
1734 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1737 int lock = 0, unlock = 0;
1738 bool wait_flag = false;
1739 bool posix_lck = false;
1740 struct cifs_sb_info *cifs_sb;
1741 struct cifs_tcon *tcon;
1742 struct cifsFileInfo *cfile;
1748 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1749 cmd, flock->fl_flags, flock->fl_type,
1750 flock->fl_start, flock->fl_end);
1752 cfile = (struct cifsFileInfo *)file->private_data;
1753 tcon = tlink_tcon(cfile->tlink);
1755 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1757 cifs_sb = CIFS_FILE_SB(file);
1759 if (cap_unix(tcon->ses) &&
1760 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1761 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1764 * BB add code here to normalize offset and length to account for
1765 * negative length which we can not accept over the wire.
1767 if (IS_GETLK(cmd)) {
1768 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1773 if (!lock && !unlock) {
1775 * if no lock or unlock then nothing to do since we do not
1782 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1789 * update the file size (if needed) after a write. Should be called with
1790 * the inode->i_lock held
1793 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1794 unsigned int bytes_written)
1796 loff_t end_of_write = offset + bytes_written;
1798 if (end_of_write > cifsi->server_eof)
1799 cifsi->server_eof = end_of_write;
1803 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1804 size_t write_size, loff_t *offset)
1807 unsigned int bytes_written = 0;
1808 unsigned int total_written;
1809 struct cifs_tcon *tcon;
1810 struct TCP_Server_Info *server;
1812 struct dentry *dentry = open_file->dentry;
1813 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
1814 struct cifs_io_parms io_parms;
1816 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1817 write_size, *offset, dentry);
1819 tcon = tlink_tcon(open_file->tlink);
1820 server = tcon->ses->server;
1822 if (!server->ops->sync_write)
1827 for (total_written = 0; write_size > total_written;
1828 total_written += bytes_written) {
1830 while (rc == -EAGAIN) {
1834 if (open_file->invalidHandle) {
1835 /* we could deadlock if we called
1836 filemap_fdatawait from here so tell
1837 reopen_file not to flush data to
1839 rc = cifs_reopen_file(open_file, false);
1844 len = min(server->ops->wp_retry_size(d_inode(dentry)),
1845 (unsigned int)write_size - total_written);
1846 /* iov[0] is reserved for smb header */
1847 iov[1].iov_base = (char *)write_data + total_written;
1848 iov[1].iov_len = len;
1850 io_parms.tcon = tcon;
1851 io_parms.offset = *offset;
1852 io_parms.length = len;
1853 rc = server->ops->sync_write(xid, &open_file->fid,
1854 &io_parms, &bytes_written, iov, 1);
1856 if (rc || (bytes_written == 0)) {
1864 spin_lock(&d_inode(dentry)->i_lock);
1865 cifs_update_eof(cifsi, *offset, bytes_written);
1866 spin_unlock(&d_inode(dentry)->i_lock);
1867 *offset += bytes_written;
1871 cifs_stats_bytes_written(tcon, total_written);
1873 if (total_written > 0) {
1874 spin_lock(&d_inode(dentry)->i_lock);
1875 if (*offset > d_inode(dentry)->i_size)
1876 i_size_write(d_inode(dentry), *offset);
1877 spin_unlock(&d_inode(dentry)->i_lock);
1879 mark_inode_dirty_sync(d_inode(dentry));
1881 return total_written;
1884 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1887 struct cifsFileInfo *open_file = NULL;
1888 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1890 /* only filter by fsuid on multiuser mounts */
1891 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1894 spin_lock(&cifs_inode->open_file_lock);
1895 /* we could simply get the first_list_entry since write-only entries
1896 are always at the end of the list but since the first entry might
1897 have a close pending, we go through the whole list */
1898 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1899 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1901 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
1902 if (!open_file->invalidHandle) {
1903 /* found a good file */
1904 /* lock it so it will not be closed on us */
1905 cifsFileInfo_get(open_file);
1906 spin_unlock(&cifs_inode->open_file_lock);
1908 } /* else might as well continue, and look for
1909 another, or simply have the caller reopen it
1910 again rather than trying to fix this handle */
1911 } else /* write only file */
1912 break; /* write only files are last so must be done */
1914 spin_unlock(&cifs_inode->open_file_lock);
1918 /* Return -EBADF if no handle is found and general rc otherwise */
1920 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
1921 struct cifsFileInfo **ret_file)
1923 struct cifsFileInfo *open_file, *inv_file = NULL;
1924 struct cifs_sb_info *cifs_sb;
1925 bool any_available = false;
1927 unsigned int refind = 0;
1928 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
1929 bool with_delete = flags & FIND_WR_WITH_DELETE;
1933 * Having a null inode here (because mapping->host was set to zero by
1934 * the VFS or MM) should not happen but we had reports of on oops (due
1935 * to it being zero) during stress testcases so we need to check for it
1938 if (cifs_inode == NULL) {
1939 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
1944 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1946 /* only filter by fsuid on multiuser mounts */
1947 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1950 spin_lock(&cifs_inode->open_file_lock);
1952 if (refind > MAX_REOPEN_ATT) {
1953 spin_unlock(&cifs_inode->open_file_lock);
1956 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1957 if (!any_available && open_file->pid != current->tgid)
1959 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1961 if (with_delete && !(open_file->fid.access & DELETE))
1963 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
1964 if (!open_file->invalidHandle) {
1965 /* found a good writable file */
1966 cifsFileInfo_get(open_file);
1967 spin_unlock(&cifs_inode->open_file_lock);
1968 *ret_file = open_file;
1972 inv_file = open_file;
1976 /* couldn't find useable FH with same pid, try any available */
1977 if (!any_available) {
1978 any_available = true;
1979 goto refind_writable;
1983 any_available = false;
1984 cifsFileInfo_get(inv_file);
1987 spin_unlock(&cifs_inode->open_file_lock);
1990 rc = cifs_reopen_file(inv_file, false);
1992 *ret_file = inv_file;
1996 spin_lock(&cifs_inode->open_file_lock);
1997 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
1998 spin_unlock(&cifs_inode->open_file_lock);
1999 cifsFileInfo_put(inv_file);
2002 spin_lock(&cifs_inode->open_file_lock);
2003 goto refind_writable;
2009 struct cifsFileInfo *
2010 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2012 struct cifsFileInfo *cfile;
2015 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2017 cifs_dbg(FYI, "couldn't find writable handle rc=%d", rc);
2023 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2025 struct cifsFileInfo **ret_file)
2027 struct list_head *tmp;
2028 struct cifsFileInfo *cfile;
2029 struct cifsInodeInfo *cinode;
2034 spin_lock(&tcon->open_file_lock);
2035 list_for_each(tmp, &tcon->openFileList) {
2036 cfile = list_entry(tmp, struct cifsFileInfo,
2038 full_path = build_path_from_dentry(cfile->dentry);
2039 if (full_path == NULL) {
2040 spin_unlock(&tcon->open_file_lock);
2043 if (strcmp(full_path, name)) {
2049 cinode = CIFS_I(d_inode(cfile->dentry));
2050 spin_unlock(&tcon->open_file_lock);
2051 return cifs_get_writable_file(cinode, flags, ret_file);
2054 spin_unlock(&tcon->open_file_lock);
2059 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2060 struct cifsFileInfo **ret_file)
2062 struct list_head *tmp;
2063 struct cifsFileInfo *cfile;
2064 struct cifsInodeInfo *cinode;
2069 spin_lock(&tcon->open_file_lock);
2070 list_for_each(tmp, &tcon->openFileList) {
2071 cfile = list_entry(tmp, struct cifsFileInfo,
2073 full_path = build_path_from_dentry(cfile->dentry);
2074 if (full_path == NULL) {
2075 spin_unlock(&tcon->open_file_lock);
2078 if (strcmp(full_path, name)) {
2084 cinode = CIFS_I(d_inode(cfile->dentry));
2085 spin_unlock(&tcon->open_file_lock);
2086 *ret_file = find_readable_file(cinode, 0);
2087 return *ret_file ? 0 : -ENOENT;
2090 spin_unlock(&tcon->open_file_lock);
2094 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2096 struct address_space *mapping = page->mapping;
2097 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2100 int bytes_written = 0;
2101 struct inode *inode;
2102 struct cifsFileInfo *open_file;
2104 if (!mapping || !mapping->host)
2107 inode = page->mapping->host;
2109 offset += (loff_t)from;
2110 write_data = kmap(page);
2113 if ((to > PAGE_SIZE) || (from > to)) {
2118 /* racing with truncate? */
2119 if (offset > mapping->host->i_size) {
2121 return 0; /* don't care */
2124 /* check to make sure that we are not extending the file */
2125 if (mapping->host->i_size - offset < (loff_t)to)
2126 to = (unsigned)(mapping->host->i_size - offset);
2128 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2131 bytes_written = cifs_write(open_file, open_file->pid,
2132 write_data, to - from, &offset);
2133 cifsFileInfo_put(open_file);
2134 /* Does mm or vfs already set times? */
2135 inode->i_atime = inode->i_mtime = current_time(inode);
2136 if ((bytes_written > 0) && (offset))
2138 else if (bytes_written < 0)
2143 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2144 if (!is_retryable_error(rc))
2152 static struct cifs_writedata *
2153 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2154 pgoff_t end, pgoff_t *index,
2155 unsigned int *found_pages)
2157 struct cifs_writedata *wdata;
2159 wdata = cifs_writedata_alloc((unsigned int)tofind,
2160 cifs_writev_complete);
2164 *found_pages = find_get_pages_range_tag(mapping, index, end,
2165 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2170 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2171 struct address_space *mapping,
2172 struct writeback_control *wbc,
2173 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2175 unsigned int nr_pages = 0, i;
2178 for (i = 0; i < found_pages; i++) {
2179 page = wdata->pages[i];
2181 * At this point we hold neither the i_pages lock nor the
2182 * page lock: the page may be truncated or invalidated
2183 * (changing page->mapping to NULL), or even swizzled
2184 * back from swapper_space to tmpfs file mapping
2189 else if (!trylock_page(page))
2192 if (unlikely(page->mapping != mapping)) {
2197 if (!wbc->range_cyclic && page->index > end) {
2203 if (*next && (page->index != *next)) {
2204 /* Not next consecutive page */
2209 if (wbc->sync_mode != WB_SYNC_NONE)
2210 wait_on_page_writeback(page);
2212 if (PageWriteback(page) ||
2213 !clear_page_dirty_for_io(page)) {
2219 * This actually clears the dirty bit in the radix tree.
2220 * See cifs_writepage() for more commentary.
2222 set_page_writeback(page);
2223 if (page_offset(page) >= i_size_read(mapping->host)) {
2226 end_page_writeback(page);
2230 wdata->pages[i] = page;
2231 *next = page->index + 1;
2235 /* reset index to refind any pages skipped */
2237 *index = wdata->pages[0]->index + 1;
2239 /* put any pages we aren't going to use */
2240 for (i = nr_pages; i < found_pages; i++) {
2241 put_page(wdata->pages[i]);
2242 wdata->pages[i] = NULL;
2249 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2250 struct address_space *mapping, struct writeback_control *wbc)
2253 struct TCP_Server_Info *server =
2254 tlink_tcon(wdata->cfile->tlink)->ses->server;
2256 wdata->sync_mode = wbc->sync_mode;
2257 wdata->nr_pages = nr_pages;
2258 wdata->offset = page_offset(wdata->pages[0]);
2259 wdata->pagesz = PAGE_SIZE;
2260 wdata->tailsz = min(i_size_read(mapping->host) -
2261 page_offset(wdata->pages[nr_pages - 1]),
2263 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2264 wdata->pid = wdata->cfile->pid;
2266 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2270 if (wdata->cfile->invalidHandle)
2273 rc = server->ops->async_writev(wdata, cifs_writedata_release);
2278 static int cifs_writepages(struct address_space *mapping,
2279 struct writeback_control *wbc)
2281 struct inode *inode = mapping->host;
2282 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2283 struct TCP_Server_Info *server;
2284 bool done = false, scanned = false, range_whole = false;
2286 struct cifs_writedata *wdata;
2287 struct cifsFileInfo *cfile = NULL;
2293 * If wsize is smaller than the page cache size, default to writing
2294 * one page at a time via cifs_writepage
2296 if (cifs_sb->wsize < PAGE_SIZE)
2297 return generic_writepages(mapping, wbc);
2300 if (wbc->range_cyclic) {
2301 index = mapping->writeback_index; /* Start from prev offset */
2304 index = wbc->range_start >> PAGE_SHIFT;
2305 end = wbc->range_end >> PAGE_SHIFT;
2306 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2310 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
2312 while (!done && index <= end) {
2313 unsigned int i, nr_pages, found_pages, wsize;
2314 pgoff_t next = 0, tofind, saved_index = index;
2315 struct cifs_credits credits_on_stack;
2316 struct cifs_credits *credits = &credits_on_stack;
2317 int get_file_rc = 0;
2320 cifsFileInfo_put(cfile);
2322 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2324 /* in case of an error store it to return later */
2328 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2335 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2337 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2342 add_credits_and_wake_if(server, credits, 0);
2346 if (found_pages == 0) {
2347 kref_put(&wdata->refcount, cifs_writedata_release);
2348 add_credits_and_wake_if(server, credits, 0);
2352 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2353 end, &index, &next, &done);
2355 /* nothing to write? */
2356 if (nr_pages == 0) {
2357 kref_put(&wdata->refcount, cifs_writedata_release);
2358 add_credits_and_wake_if(server, credits, 0);
2362 wdata->credits = credits_on_stack;
2363 wdata->cfile = cfile;
2366 if (!wdata->cfile) {
2367 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2369 if (is_retryable_error(get_file_rc))
2374 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2376 for (i = 0; i < nr_pages; ++i)
2377 unlock_page(wdata->pages[i]);
2379 /* send failure -- clean up the mess */
2381 add_credits_and_wake_if(server, &wdata->credits, 0);
2382 for (i = 0; i < nr_pages; ++i) {
2383 if (is_retryable_error(rc))
2384 redirty_page_for_writepage(wbc,
2387 SetPageError(wdata->pages[i]);
2388 end_page_writeback(wdata->pages[i]);
2389 put_page(wdata->pages[i]);
2391 if (!is_retryable_error(rc))
2392 mapping_set_error(mapping, rc);
2394 kref_put(&wdata->refcount, cifs_writedata_release);
2396 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2397 index = saved_index;
2401 /* Return immediately if we received a signal during writing */
2402 if (is_interrupt_error(rc)) {
2407 if (rc != 0 && saved_rc == 0)
2410 wbc->nr_to_write -= nr_pages;
2411 if (wbc->nr_to_write <= 0)
2417 if (!scanned && !done) {
2419 * We hit the last page and there is more work to be done: wrap
2420 * back to the start of the file
2430 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2431 mapping->writeback_index = index;
2434 cifsFileInfo_put(cfile);
2440 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2446 /* BB add check for wbc flags */
2448 if (!PageUptodate(page))
2449 cifs_dbg(FYI, "ppw - page not up to date\n");
2452 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2454 * A writepage() implementation always needs to do either this,
2455 * or re-dirty the page with "redirty_page_for_writepage()" in
2456 * the case of a failure.
2458 * Just unlocking the page will cause the radix tree tag-bits
2459 * to fail to update with the state of the page correctly.
2461 set_page_writeback(page);
2463 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2464 if (is_retryable_error(rc)) {
2465 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2467 redirty_page_for_writepage(wbc, page);
2468 } else if (rc != 0) {
2470 mapping_set_error(page->mapping, rc);
2472 SetPageUptodate(page);
2474 end_page_writeback(page);
2480 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2482 int rc = cifs_writepage_locked(page, wbc);
2487 static int cifs_write_end(struct file *file, struct address_space *mapping,
2488 loff_t pos, unsigned len, unsigned copied,
2489 struct page *page, void *fsdata)
2492 struct inode *inode = mapping->host;
2493 struct cifsFileInfo *cfile = file->private_data;
2494 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2497 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2500 pid = current->tgid;
2502 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2505 if (PageChecked(page)) {
2507 SetPageUptodate(page);
2508 ClearPageChecked(page);
2509 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2510 SetPageUptodate(page);
2512 if (!PageUptodate(page)) {
2514 unsigned offset = pos & (PAGE_SIZE - 1);
2518 /* this is probably better than directly calling
2519 partialpage_write since in this function the file handle is
2520 known which we might as well leverage */
2521 /* BB check if anything else missing out of ppw
2522 such as updating last write time */
2523 page_data = kmap(page);
2524 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2525 /* if (rc < 0) should we set writebehind rc? */
2532 set_page_dirty(page);
2536 spin_lock(&inode->i_lock);
2537 if (pos > inode->i_size)
2538 i_size_write(inode, pos);
2539 spin_unlock(&inode->i_lock);
2548 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2553 struct cifs_tcon *tcon;
2554 struct TCP_Server_Info *server;
2555 struct cifsFileInfo *smbfile = file->private_data;
2556 struct inode *inode = file_inode(file);
2557 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2559 rc = file_write_and_wait_range(file, start, end);
2565 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2568 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2569 rc = cifs_zap_mapping(inode);
2571 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2572 rc = 0; /* don't care about it in fsync */
2576 tcon = tlink_tcon(smbfile->tlink);
2577 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2578 server = tcon->ses->server;
2579 if (server->ops->flush)
2580 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2589 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2593 struct cifs_tcon *tcon;
2594 struct TCP_Server_Info *server;
2595 struct cifsFileInfo *smbfile = file->private_data;
2596 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2598 rc = file_write_and_wait_range(file, start, end);
2604 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2607 tcon = tlink_tcon(smbfile->tlink);
2608 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2609 server = tcon->ses->server;
2610 if (server->ops->flush)
2611 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2621 * As file closes, flush all cached write data for this inode checking
2622 * for write behind errors.
2624 int cifs_flush(struct file *file, fl_owner_t id)
2626 struct inode *inode = file_inode(file);
2629 if (file->f_mode & FMODE_WRITE)
2630 rc = filemap_write_and_wait(inode->i_mapping);
2632 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2638 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2643 for (i = 0; i < num_pages; i++) {
2644 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2647 * save number of pages we have already allocated and
2648 * return with ENOMEM error
2657 for (i = 0; i < num_pages; i++)
2664 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2669 clen = min_t(const size_t, len, wsize);
2670 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
2679 cifs_uncached_writedata_release(struct kref *refcount)
2682 struct cifs_writedata *wdata = container_of(refcount,
2683 struct cifs_writedata, refcount);
2685 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
2686 for (i = 0; i < wdata->nr_pages; i++)
2687 put_page(wdata->pages[i]);
2688 cifs_writedata_release(refcount);
2691 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2694 cifs_uncached_writev_complete(struct work_struct *work)
2696 struct cifs_writedata *wdata = container_of(work,
2697 struct cifs_writedata, work);
2698 struct inode *inode = d_inode(wdata->cfile->dentry);
2699 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2701 spin_lock(&inode->i_lock);
2702 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2703 if (cifsi->server_eof > inode->i_size)
2704 i_size_write(inode, cifsi->server_eof);
2705 spin_unlock(&inode->i_lock);
2707 complete(&wdata->done);
2708 collect_uncached_write_data(wdata->ctx);
2709 /* the below call can possibly free the last ref to aio ctx */
2710 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2714 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2715 size_t *len, unsigned long *num_pages)
2717 size_t save_len, copied, bytes, cur_len = *len;
2718 unsigned long i, nr_pages = *num_pages;
2721 for (i = 0; i < nr_pages; i++) {
2722 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2723 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2726 * If we didn't copy as much as we expected, then that
2727 * may mean we trod into an unmapped area. Stop copying
2728 * at that point. On the next pass through the big
2729 * loop, we'll likely end up getting a zero-length
2730 * write and bailing out of it.
2735 cur_len = save_len - cur_len;
2739 * If we have no data to send, then that probably means that
2740 * the copy above failed altogether. That's most likely because
2741 * the address in the iovec was bogus. Return -EFAULT and let
2742 * the caller free anything we allocated and bail out.
2748 * i + 1 now represents the number of pages we actually used in
2749 * the copy phase above.
2756 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
2757 struct cifs_aio_ctx *ctx)
2760 struct cifs_credits credits;
2762 struct TCP_Server_Info *server =
2763 tlink_tcon(wdata->cfile->tlink)->ses->server;
2766 if (wdata->cfile->invalidHandle) {
2767 rc = cifs_reopen_file(wdata->cfile, false);
2776 * Wait for credits to resend this wdata.
2777 * Note: we are attempting to resend the whole wdata not in
2781 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
2786 if (wsize < wdata->bytes) {
2787 add_credits_and_wake_if(server, &credits, 0);
2790 } while (wsize < wdata->bytes);
2791 wdata->credits = credits;
2793 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2796 if (wdata->cfile->invalidHandle)
2799 rc = server->ops->async_writev(wdata,
2800 cifs_uncached_writedata_release);
2803 /* If the write was successfully sent, we are done */
2805 list_add_tail(&wdata->list, wdata_list);
2809 /* Roll back credits and retry if needed */
2810 add_credits_and_wake_if(server, &wdata->credits, 0);
2811 } while (rc == -EAGAIN);
2814 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2819 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2820 struct cifsFileInfo *open_file,
2821 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2822 struct cifs_aio_ctx *ctx)
2826 unsigned long nr_pages, num_pages, i;
2827 struct cifs_writedata *wdata;
2828 struct iov_iter saved_from = *from;
2829 loff_t saved_offset = offset;
2831 struct TCP_Server_Info *server;
2832 struct page **pagevec;
2836 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2837 pid = open_file->pid;
2839 pid = current->tgid;
2841 server = tlink_tcon(open_file->tlink)->ses->server;
2846 struct cifs_credits credits_on_stack;
2847 struct cifs_credits *credits = &credits_on_stack;
2849 if (open_file->invalidHandle) {
2850 rc = cifs_reopen_file(open_file, false);
2857 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2862 cur_len = min_t(const size_t, len, wsize);
2864 if (ctx->direct_io) {
2867 result = iov_iter_get_pages_alloc(
2868 from, &pagevec, cur_len, &start);
2871 "direct_writev couldn't get user pages "
2872 "(rc=%zd) iter type %d iov_offset %zd "
2875 from->iov_offset, from->count);
2879 add_credits_and_wake_if(server, credits, 0);
2882 cur_len = (size_t)result;
2883 iov_iter_advance(from, cur_len);
2886 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
2888 wdata = cifs_writedata_direct_alloc(pagevec,
2889 cifs_uncached_writev_complete);
2892 add_credits_and_wake_if(server, credits, 0);
2897 wdata->page_offset = start;
2900 cur_len - (PAGE_SIZE - start) -
2901 (nr_pages - 2) * PAGE_SIZE :
2904 nr_pages = get_numpages(wsize, len, &cur_len);
2905 wdata = cifs_writedata_alloc(nr_pages,
2906 cifs_uncached_writev_complete);
2909 add_credits_and_wake_if(server, credits, 0);
2913 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2915 kvfree(wdata->pages);
2917 add_credits_and_wake_if(server, credits, 0);
2921 num_pages = nr_pages;
2922 rc = wdata_fill_from_iovec(
2923 wdata, from, &cur_len, &num_pages);
2925 for (i = 0; i < nr_pages; i++)
2926 put_page(wdata->pages[i]);
2927 kvfree(wdata->pages);
2929 add_credits_and_wake_if(server, credits, 0);
2934 * Bring nr_pages down to the number of pages we
2935 * actually used, and free any pages that we didn't use.
2937 for ( ; nr_pages > num_pages; nr_pages--)
2938 put_page(wdata->pages[nr_pages - 1]);
2940 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
2943 wdata->sync_mode = WB_SYNC_ALL;
2944 wdata->nr_pages = nr_pages;
2945 wdata->offset = (__u64)offset;
2946 wdata->cfile = cifsFileInfo_get(open_file);
2948 wdata->bytes = cur_len;
2949 wdata->pagesz = PAGE_SIZE;
2950 wdata->credits = credits_on_stack;
2952 kref_get(&ctx->refcount);
2954 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2957 if (wdata->cfile->invalidHandle)
2960 rc = server->ops->async_writev(wdata,
2961 cifs_uncached_writedata_release);
2965 add_credits_and_wake_if(server, &wdata->credits, 0);
2966 kref_put(&wdata->refcount,
2967 cifs_uncached_writedata_release);
2968 if (rc == -EAGAIN) {
2970 iov_iter_advance(from, offset - saved_offset);
2976 list_add_tail(&wdata->list, wdata_list);
2985 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
2987 struct cifs_writedata *wdata, *tmp;
2988 struct cifs_tcon *tcon;
2989 struct cifs_sb_info *cifs_sb;
2990 struct dentry *dentry = ctx->cfile->dentry;
2993 tcon = tlink_tcon(ctx->cfile->tlink);
2994 cifs_sb = CIFS_SB(dentry->d_sb);
2996 mutex_lock(&ctx->aio_mutex);
2998 if (list_empty(&ctx->list)) {
2999 mutex_unlock(&ctx->aio_mutex);
3005 * Wait for and collect replies for any successful sends in order of
3006 * increasing offset. Once an error is hit, then return without waiting
3007 * for any more replies.
3010 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3012 if (!try_wait_for_completion(&wdata->done)) {
3013 mutex_unlock(&ctx->aio_mutex);
3020 ctx->total_len += wdata->bytes;
3022 /* resend call if it's a retryable error */
3023 if (rc == -EAGAIN) {
3024 struct list_head tmp_list;
3025 struct iov_iter tmp_from = ctx->iter;
3027 INIT_LIST_HEAD(&tmp_list);
3028 list_del_init(&wdata->list);
3031 rc = cifs_resend_wdata(
3032 wdata, &tmp_list, ctx);
3034 iov_iter_advance(&tmp_from,
3035 wdata->offset - ctx->pos);
3037 rc = cifs_write_from_iter(wdata->offset,
3038 wdata->bytes, &tmp_from,
3039 ctx->cfile, cifs_sb, &tmp_list,
3042 kref_put(&wdata->refcount,
3043 cifs_uncached_writedata_release);
3046 list_splice(&tmp_list, &ctx->list);
3050 list_del_init(&wdata->list);
3051 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3054 cifs_stats_bytes_written(tcon, ctx->total_len);
3055 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3057 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3059 mutex_unlock(&ctx->aio_mutex);
3061 if (ctx->iocb && ctx->iocb->ki_complete)
3062 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3064 complete(&ctx->done);
3067 static ssize_t __cifs_writev(
3068 struct kiocb *iocb, struct iov_iter *from, bool direct)
3070 struct file *file = iocb->ki_filp;
3071 ssize_t total_written = 0;
3072 struct cifsFileInfo *cfile;
3073 struct cifs_tcon *tcon;
3074 struct cifs_sb_info *cifs_sb;
3075 struct cifs_aio_ctx *ctx;
3076 struct iov_iter saved_from = *from;
3077 size_t len = iov_iter_count(from);
3081 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3082 * In this case, fall back to non-direct write function.
3083 * this could be improved by getting pages directly in ITER_KVEC
3085 if (direct && from->type & ITER_KVEC) {
3086 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3090 rc = generic_write_checks(iocb, from);
3094 cifs_sb = CIFS_FILE_SB(file);
3095 cfile = file->private_data;
3096 tcon = tlink_tcon(cfile->tlink);
3098 if (!tcon->ses->server->ops->async_writev)
3101 ctx = cifs_aio_ctx_alloc();
3105 ctx->cfile = cifsFileInfo_get(cfile);
3107 if (!is_sync_kiocb(iocb))
3110 ctx->pos = iocb->ki_pos;
3113 ctx->direct_io = true;
3117 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3119 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3124 /* grab a lock here due to read response handlers can access ctx */
3125 mutex_lock(&ctx->aio_mutex);
3127 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3128 cfile, cifs_sb, &ctx->list, ctx);
3131 * If at least one write was successfully sent, then discard any rc
3132 * value from the later writes. If the other write succeeds, then
3133 * we'll end up returning whatever was written. If it fails, then
3134 * we'll get a new rc value from that.
3136 if (!list_empty(&ctx->list))
3139 mutex_unlock(&ctx->aio_mutex);
3142 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3146 if (!is_sync_kiocb(iocb)) {
3147 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3148 return -EIOCBQUEUED;
3151 rc = wait_for_completion_killable(&ctx->done);
3153 mutex_lock(&ctx->aio_mutex);
3154 ctx->rc = rc = -EINTR;
3155 total_written = ctx->total_len;
3156 mutex_unlock(&ctx->aio_mutex);
3159 total_written = ctx->total_len;
3162 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3164 if (unlikely(!total_written))
3167 iocb->ki_pos += total_written;
3168 return total_written;
3171 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3173 return __cifs_writev(iocb, from, true);
3176 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3178 return __cifs_writev(iocb, from, false);
3182 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3184 struct file *file = iocb->ki_filp;
3185 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3186 struct inode *inode = file->f_mapping->host;
3187 struct cifsInodeInfo *cinode = CIFS_I(inode);
3188 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3193 * We need to hold the sem to be sure nobody modifies lock list
3194 * with a brlock that prevents writing.
3196 down_read(&cinode->lock_sem);
3198 rc = generic_write_checks(iocb, from);
3202 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3203 server->vals->exclusive_lock_type, 0,
3204 NULL, CIFS_WRITE_OP))
3205 rc = __generic_file_write_iter(iocb, from);
3209 up_read(&cinode->lock_sem);
3210 inode_unlock(inode);
3213 rc = generic_write_sync(iocb, rc);
3218 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3220 struct inode *inode = file_inode(iocb->ki_filp);
3221 struct cifsInodeInfo *cinode = CIFS_I(inode);
3222 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3223 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3224 iocb->ki_filp->private_data;
3225 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3228 written = cifs_get_writer(cinode);
3232 if (CIFS_CACHE_WRITE(cinode)) {
3233 if (cap_unix(tcon->ses) &&
3234 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3235 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3236 written = generic_file_write_iter(iocb, from);
3239 written = cifs_writev(iocb, from);
3243 * For non-oplocked files in strict cache mode we need to write the data
3244 * to the server exactly from the pos to pos+len-1 rather than flush all
3245 * affected pages because it may cause a error with mandatory locks on
3246 * these pages but not on the region from pos to ppos+len-1.
3248 written = cifs_user_writev(iocb, from);
3249 if (CIFS_CACHE_READ(cinode)) {
3251 * We have read level caching and we have just sent a write
3252 * request to the server thus making data in the cache stale.
3253 * Zap the cache and set oplock/lease level to NONE to avoid
3254 * reading stale data from the cache. All subsequent read
3255 * operations will read new data from the server.
3257 cifs_zap_mapping(inode);
3258 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3263 cifs_put_writer(cinode);
3267 static struct cifs_readdata *
3268 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3270 struct cifs_readdata *rdata;
3272 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3273 if (rdata != NULL) {
3274 rdata->pages = pages;
3275 kref_init(&rdata->refcount);
3276 INIT_LIST_HEAD(&rdata->list);
3277 init_completion(&rdata->done);
3278 INIT_WORK(&rdata->work, complete);
3284 static struct cifs_readdata *
3285 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3287 struct page **pages =
3288 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3289 struct cifs_readdata *ret = NULL;
3292 ret = cifs_readdata_direct_alloc(pages, complete);
3301 cifs_readdata_release(struct kref *refcount)
3303 struct cifs_readdata *rdata = container_of(refcount,
3304 struct cifs_readdata, refcount);
3305 #ifdef CONFIG_CIFS_SMB_DIRECT
3307 smbd_deregister_mr(rdata->mr);
3312 cifsFileInfo_put(rdata->cfile);
3314 kvfree(rdata->pages);
3319 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3325 for (i = 0; i < nr_pages; i++) {
3326 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3331 rdata->pages[i] = page;
3335 unsigned int nr_page_failed = i;
3337 for (i = 0; i < nr_page_failed; i++) {
3338 put_page(rdata->pages[i]);
3339 rdata->pages[i] = NULL;
3346 cifs_uncached_readdata_release(struct kref *refcount)
3348 struct cifs_readdata *rdata = container_of(refcount,
3349 struct cifs_readdata, refcount);
3352 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3353 for (i = 0; i < rdata->nr_pages; i++) {
3354 put_page(rdata->pages[i]);
3356 cifs_readdata_release(refcount);
3360 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3361 * @rdata: the readdata response with list of pages holding data
3362 * @iter: destination for our data
3364 * This function copies data from a list of pages in a readdata response into
3365 * an array of iovecs. It will first calculate where the data should go
3366 * based on the info in the readdata and then copy the data into that spot.
3369 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3371 size_t remaining = rdata->got_bytes;
3374 for (i = 0; i < rdata->nr_pages; i++) {
3375 struct page *page = rdata->pages[i];
3376 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3379 if (unlikely(iov_iter_is_pipe(iter))) {
3380 void *addr = kmap_atomic(page);
3382 written = copy_to_iter(addr, copy, iter);
3383 kunmap_atomic(addr);
3385 written = copy_page_to_iter(page, 0, copy, iter);
3386 remaining -= written;
3387 if (written < copy && iov_iter_count(iter) > 0)
3390 return remaining ? -EFAULT : 0;
3393 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3396 cifs_uncached_readv_complete(struct work_struct *work)
3398 struct cifs_readdata *rdata = container_of(work,
3399 struct cifs_readdata, work);
3401 complete(&rdata->done);
3402 collect_uncached_read_data(rdata->ctx);
3403 /* the below call can possibly free the last ref to aio ctx */
3404 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3408 uncached_fill_pages(struct TCP_Server_Info *server,
3409 struct cifs_readdata *rdata, struct iov_iter *iter,
3414 unsigned int nr_pages = rdata->nr_pages;
3415 unsigned int page_offset = rdata->page_offset;
3417 rdata->got_bytes = 0;
3418 rdata->tailsz = PAGE_SIZE;
3419 for (i = 0; i < nr_pages; i++) {
3420 struct page *page = rdata->pages[i];
3422 unsigned int segment_size = rdata->pagesz;
3425 segment_size -= page_offset;
3431 /* no need to hold page hostage */
3432 rdata->pages[i] = NULL;
3439 if (len >= segment_size)
3440 /* enough data to fill the page */
3443 rdata->tailsz = len;
3447 result = copy_page_from_iter(
3448 page, page_offset, n, iter);
3449 #ifdef CONFIG_CIFS_SMB_DIRECT
3454 result = cifs_read_page_from_socket(
3455 server, page, page_offset, n);
3459 rdata->got_bytes += result;
3462 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3463 rdata->got_bytes : result;
3467 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3468 struct cifs_readdata *rdata, unsigned int len)
3470 return uncached_fill_pages(server, rdata, NULL, len);
3474 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3475 struct cifs_readdata *rdata,
3476 struct iov_iter *iter)
3478 return uncached_fill_pages(server, rdata, iter, iter->count);
3481 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3482 struct list_head *rdata_list,
3483 struct cifs_aio_ctx *ctx)
3486 struct cifs_credits credits;
3488 struct TCP_Server_Info *server =
3489 tlink_tcon(rdata->cfile->tlink)->ses->server;
3492 if (rdata->cfile->invalidHandle) {
3493 rc = cifs_reopen_file(rdata->cfile, true);
3501 * Wait for credits to resend this rdata.
3502 * Note: we are attempting to resend the whole rdata not in
3506 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3512 if (rsize < rdata->bytes) {
3513 add_credits_and_wake_if(server, &credits, 0);
3516 } while (rsize < rdata->bytes);
3517 rdata->credits = credits;
3519 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3521 if (rdata->cfile->invalidHandle)
3524 rc = server->ops->async_readv(rdata);
3527 /* If the read was successfully sent, we are done */
3529 /* Add to aio pending list */
3530 list_add_tail(&rdata->list, rdata_list);
3534 /* Roll back credits and retry if needed */
3535 add_credits_and_wake_if(server, &rdata->credits, 0);
3536 } while (rc == -EAGAIN);
3539 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3544 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3545 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3546 struct cifs_aio_ctx *ctx)
3548 struct cifs_readdata *rdata;
3549 unsigned int npages, rsize;
3550 struct cifs_credits credits_on_stack;
3551 struct cifs_credits *credits = &credits_on_stack;
3555 struct TCP_Server_Info *server;
3556 struct page **pagevec;
3558 struct iov_iter direct_iov = ctx->iter;
3560 server = tlink_tcon(open_file->tlink)->ses->server;
3562 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3563 pid = open_file->pid;
3565 pid = current->tgid;
3568 iov_iter_advance(&direct_iov, offset - ctx->pos);
3571 if (open_file->invalidHandle) {
3572 rc = cifs_reopen_file(open_file, true);
3579 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
3584 cur_len = min_t(const size_t, len, rsize);
3586 if (ctx->direct_io) {
3589 result = iov_iter_get_pages_alloc(
3590 &direct_iov, &pagevec,
3594 "couldn't get user pages (rc=%zd)"
3596 " iov_offset %zd count %zd\n",
3597 result, direct_iov.type,
3598 direct_iov.iov_offset,
3603 add_credits_and_wake_if(server, credits, 0);
3606 cur_len = (size_t)result;
3607 iov_iter_advance(&direct_iov, cur_len);
3609 rdata = cifs_readdata_direct_alloc(
3610 pagevec, cifs_uncached_readv_complete);
3612 add_credits_and_wake_if(server, credits, 0);
3617 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
3618 rdata->page_offset = start;
3619 rdata->tailsz = npages > 1 ?
3620 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
3625 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
3626 /* allocate a readdata struct */
3627 rdata = cifs_readdata_alloc(npages,
3628 cifs_uncached_readv_complete);
3630 add_credits_and_wake_if(server, credits, 0);
3635 rc = cifs_read_allocate_pages(rdata, npages);
3637 kvfree(rdata->pages);
3639 add_credits_and_wake_if(server, credits, 0);
3643 rdata->tailsz = PAGE_SIZE;
3646 rdata->cfile = cifsFileInfo_get(open_file);
3647 rdata->nr_pages = npages;
3648 rdata->offset = offset;
3649 rdata->bytes = cur_len;
3651 rdata->pagesz = PAGE_SIZE;
3652 rdata->read_into_pages = cifs_uncached_read_into_pages;
3653 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
3654 rdata->credits = credits_on_stack;
3656 kref_get(&ctx->refcount);
3658 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3661 if (rdata->cfile->invalidHandle)
3664 rc = server->ops->async_readv(rdata);
3668 add_credits_and_wake_if(server, &rdata->credits, 0);
3669 kref_put(&rdata->refcount,
3670 cifs_uncached_readdata_release);
3671 if (rc == -EAGAIN) {
3672 iov_iter_revert(&direct_iov, cur_len);
3678 list_add_tail(&rdata->list, rdata_list);
3687 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3689 struct cifs_readdata *rdata, *tmp;
3690 struct iov_iter *to = &ctx->iter;
3691 struct cifs_sb_info *cifs_sb;
3694 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
3696 mutex_lock(&ctx->aio_mutex);
3698 if (list_empty(&ctx->list)) {
3699 mutex_unlock(&ctx->aio_mutex);
3704 /* the loop below should proceed in the order of increasing offsets */
3706 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
3708 if (!try_wait_for_completion(&rdata->done)) {
3709 mutex_unlock(&ctx->aio_mutex);
3713 if (rdata->result == -EAGAIN) {
3714 /* resend call if it's a retryable error */
3715 struct list_head tmp_list;
3716 unsigned int got_bytes = rdata->got_bytes;
3718 list_del_init(&rdata->list);
3719 INIT_LIST_HEAD(&tmp_list);
3722 * Got a part of data and then reconnect has
3723 * happened -- fill the buffer and continue
3726 if (got_bytes && got_bytes < rdata->bytes) {
3728 if (!ctx->direct_io)
3729 rc = cifs_readdata_to_iov(rdata, to);
3731 kref_put(&rdata->refcount,
3732 cifs_uncached_readdata_release);
3737 if (ctx->direct_io) {
3739 * Re-use rdata as this is a
3742 rc = cifs_resend_rdata(
3746 rc = cifs_send_async_read(
3747 rdata->offset + got_bytes,
3748 rdata->bytes - got_bytes,
3749 rdata->cfile, cifs_sb,
3752 kref_put(&rdata->refcount,
3753 cifs_uncached_readdata_release);
3756 list_splice(&tmp_list, &ctx->list);
3759 } else if (rdata->result)
3761 else if (!ctx->direct_io)
3762 rc = cifs_readdata_to_iov(rdata, to);
3764 /* if there was a short read -- discard anything left */
3765 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3768 ctx->total_len += rdata->got_bytes;
3770 list_del_init(&rdata->list);
3771 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3774 if (!ctx->direct_io)
3775 ctx->total_len = ctx->len - iov_iter_count(to);
3777 /* mask nodata case */
3781 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
3783 mutex_unlock(&ctx->aio_mutex);
3785 if (ctx->iocb && ctx->iocb->ki_complete)
3786 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3788 complete(&ctx->done);
3791 static ssize_t __cifs_readv(
3792 struct kiocb *iocb, struct iov_iter *to, bool direct)
3795 struct file *file = iocb->ki_filp;
3796 struct cifs_sb_info *cifs_sb;
3797 struct cifsFileInfo *cfile;
3798 struct cifs_tcon *tcon;
3799 ssize_t rc, total_read = 0;
3800 loff_t offset = iocb->ki_pos;
3801 struct cifs_aio_ctx *ctx;
3804 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
3805 * fall back to data copy read path
3806 * this could be improved by getting pages directly in ITER_KVEC
3808 if (direct && to->type & ITER_KVEC) {
3809 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
3813 len = iov_iter_count(to);
3817 cifs_sb = CIFS_FILE_SB(file);
3818 cfile = file->private_data;
3819 tcon = tlink_tcon(cfile->tlink);
3821 if (!tcon->ses->server->ops->async_readv)
3824 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3825 cifs_dbg(FYI, "attempting read on write only file instance\n");
3827 ctx = cifs_aio_ctx_alloc();
3831 ctx->cfile = cifsFileInfo_get(cfile);
3833 if (!is_sync_kiocb(iocb))
3836 if (iter_is_iovec(to))
3837 ctx->should_dirty = true;
3841 ctx->direct_io = true;
3845 rc = setup_aio_ctx_iter(ctx, to, READ);
3847 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3853 /* grab a lock here due to read response handlers can access ctx */
3854 mutex_lock(&ctx->aio_mutex);
3856 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
3858 /* if at least one read request send succeeded, then reset rc */
3859 if (!list_empty(&ctx->list))
3862 mutex_unlock(&ctx->aio_mutex);
3865 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3869 if (!is_sync_kiocb(iocb)) {
3870 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3871 return -EIOCBQUEUED;
3874 rc = wait_for_completion_killable(&ctx->done);
3876 mutex_lock(&ctx->aio_mutex);
3877 ctx->rc = rc = -EINTR;
3878 total_read = ctx->total_len;
3879 mutex_unlock(&ctx->aio_mutex);
3882 total_read = ctx->total_len;
3885 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3888 iocb->ki_pos += total_read;
3894 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
3896 return __cifs_readv(iocb, to, true);
3899 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
3901 return __cifs_readv(iocb, to, false);
3905 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
3907 struct inode *inode = file_inode(iocb->ki_filp);
3908 struct cifsInodeInfo *cinode = CIFS_I(inode);
3909 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3910 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3911 iocb->ki_filp->private_data;
3912 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3916 * In strict cache mode we need to read from the server all the time
3917 * if we don't have level II oplock because the server can delay mtime
3918 * change - so we can't make a decision about inode invalidating.
3919 * And we can also fail with pagereading if there are mandatory locks
3920 * on pages affected by this read but not on the region from pos to
3923 if (!CIFS_CACHE_READ(cinode))
3924 return cifs_user_readv(iocb, to);
3926 if (cap_unix(tcon->ses) &&
3927 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3928 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
3929 return generic_file_read_iter(iocb, to);
3932 * We need to hold the sem to be sure nobody modifies lock list
3933 * with a brlock that prevents reading.
3935 down_read(&cinode->lock_sem);
3936 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
3937 tcon->ses->server->vals->shared_lock_type,
3938 0, NULL, CIFS_READ_OP))
3939 rc = generic_file_read_iter(iocb, to);
3940 up_read(&cinode->lock_sem);
3945 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
3948 unsigned int bytes_read = 0;
3949 unsigned int total_read;
3950 unsigned int current_read_size;
3952 struct cifs_sb_info *cifs_sb;
3953 struct cifs_tcon *tcon;
3954 struct TCP_Server_Info *server;
3957 struct cifsFileInfo *open_file;
3958 struct cifs_io_parms io_parms;
3959 int buf_type = CIFS_NO_BUFFER;
3963 cifs_sb = CIFS_FILE_SB(file);
3965 /* FIXME: set up handlers for larger reads and/or convert to async */
3966 rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
3968 if (file->private_data == NULL) {
3973 open_file = file->private_data;
3974 tcon = tlink_tcon(open_file->tlink);
3975 server = tcon->ses->server;
3977 if (!server->ops->sync_read) {
3982 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3983 pid = open_file->pid;
3985 pid = current->tgid;
3987 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3988 cifs_dbg(FYI, "attempting read on write only file instance\n");
3990 for (total_read = 0, cur_offset = read_data; read_size > total_read;
3991 total_read += bytes_read, cur_offset += bytes_read) {
3993 current_read_size = min_t(uint, read_size - total_read,
3996 * For windows me and 9x we do not want to request more
3997 * than it negotiated since it will refuse the read
4000 if ((tcon->ses) && !(tcon->ses->capabilities &
4001 tcon->ses->server->vals->cap_large_files)) {
4002 current_read_size = min_t(uint,
4003 current_read_size, CIFSMaxBufSize);
4005 if (open_file->invalidHandle) {
4006 rc = cifs_reopen_file(open_file, true);
4011 io_parms.tcon = tcon;
4012 io_parms.offset = *offset;
4013 io_parms.length = current_read_size;
4014 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4015 &bytes_read, &cur_offset,
4017 } while (rc == -EAGAIN);
4019 if (rc || (bytes_read == 0)) {
4027 cifs_stats_bytes_read(tcon, total_read);
4028 *offset += bytes_read;
4036 * If the page is mmap'ed into a process' page tables, then we need to make
4037 * sure that it doesn't change while being written back.
4040 cifs_page_mkwrite(struct vm_fault *vmf)
4042 struct page *page = vmf->page;
4045 return VM_FAULT_LOCKED;
4048 static const struct vm_operations_struct cifs_file_vm_ops = {
4049 .fault = filemap_fault,
4050 .map_pages = filemap_map_pages,
4051 .page_mkwrite = cifs_page_mkwrite,
4054 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4057 struct inode *inode = file_inode(file);
4061 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4062 rc = cifs_zap_mapping(inode);
4064 rc = generic_file_mmap(file, vma);
4066 vma->vm_ops = &cifs_file_vm_ops;
4072 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4078 rc = cifs_revalidate_file(file);
4080 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4083 rc = generic_file_mmap(file, vma);
4085 vma->vm_ops = &cifs_file_vm_ops;
4092 cifs_readv_complete(struct work_struct *work)
4094 unsigned int i, got_bytes;
4095 struct cifs_readdata *rdata = container_of(work,
4096 struct cifs_readdata, work);
4098 got_bytes = rdata->got_bytes;
4099 for (i = 0; i < rdata->nr_pages; i++) {
4100 struct page *page = rdata->pages[i];
4102 lru_cache_add_file(page);
4104 if (rdata->result == 0 ||
4105 (rdata->result == -EAGAIN && got_bytes)) {
4106 flush_dcache_page(page);
4107 SetPageUptodate(page);
4112 if (rdata->result == 0 ||
4113 (rdata->result == -EAGAIN && got_bytes))
4114 cifs_readpage_to_fscache(rdata->mapping->host, page);
4116 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4119 rdata->pages[i] = NULL;
4121 kref_put(&rdata->refcount, cifs_readdata_release);
4125 readpages_fill_pages(struct TCP_Server_Info *server,
4126 struct cifs_readdata *rdata, struct iov_iter *iter,
4133 unsigned int nr_pages = rdata->nr_pages;
4134 unsigned int page_offset = rdata->page_offset;
4136 /* determine the eof that the server (probably) has */
4137 eof = CIFS_I(rdata->mapping->host)->server_eof;
4138 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4139 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4141 rdata->got_bytes = 0;
4142 rdata->tailsz = PAGE_SIZE;
4143 for (i = 0; i < nr_pages; i++) {
4144 struct page *page = rdata->pages[i];
4145 unsigned int to_read = rdata->pagesz;
4149 to_read -= page_offset;
4155 if (len >= to_read) {
4157 } else if (len > 0) {
4158 /* enough for partial page, fill and zero the rest */
4159 zero_user(page, len + page_offset, to_read - len);
4160 n = rdata->tailsz = len;
4162 } else if (page->index > eof_index) {
4164 * The VFS will not try to do readahead past the
4165 * i_size, but it's possible that we have outstanding
4166 * writes with gaps in the middle and the i_size hasn't
4167 * caught up yet. Populate those with zeroed out pages
4168 * to prevent the VFS from repeatedly attempting to
4169 * fill them until the writes are flushed.
4171 zero_user(page, 0, PAGE_SIZE);
4172 lru_cache_add_file(page);
4173 flush_dcache_page(page);
4174 SetPageUptodate(page);
4177 rdata->pages[i] = NULL;
4181 /* no need to hold page hostage */
4182 lru_cache_add_file(page);
4185 rdata->pages[i] = NULL;
4191 result = copy_page_from_iter(
4192 page, page_offset, n, iter);
4193 #ifdef CONFIG_CIFS_SMB_DIRECT
4198 result = cifs_read_page_from_socket(
4199 server, page, page_offset, n);
4203 rdata->got_bytes += result;
4206 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4207 rdata->got_bytes : result;
4211 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4212 struct cifs_readdata *rdata, unsigned int len)
4214 return readpages_fill_pages(server, rdata, NULL, len);
4218 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4219 struct cifs_readdata *rdata,
4220 struct iov_iter *iter)
4222 return readpages_fill_pages(server, rdata, iter, iter->count);
4226 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4227 unsigned int rsize, struct list_head *tmplist,
4228 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4230 struct page *page, *tpage;
4231 unsigned int expected_index;
4233 gfp_t gfp = readahead_gfp_mask(mapping);
4235 INIT_LIST_HEAD(tmplist);
4237 page = lru_to_page(page_list);
4240 * Lock the page and put it in the cache. Since no one else
4241 * should have access to this page, we're safe to simply set
4242 * PG_locked without checking it first.
4244 __SetPageLocked(page);
4245 rc = add_to_page_cache_locked(page, mapping,
4248 /* give up if we can't stick it in the cache */
4250 __ClearPageLocked(page);
4254 /* move first page to the tmplist */
4255 *offset = (loff_t)page->index << PAGE_SHIFT;
4258 list_move_tail(&page->lru, tmplist);
4260 /* now try and add more pages onto the request */
4261 expected_index = page->index + 1;
4262 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4263 /* discontinuity ? */
4264 if (page->index != expected_index)
4267 /* would this page push the read over the rsize? */
4268 if (*bytes + PAGE_SIZE > rsize)
4271 __SetPageLocked(page);
4272 if (add_to_page_cache_locked(page, mapping, page->index, gfp)) {
4273 __ClearPageLocked(page);
4276 list_move_tail(&page->lru, tmplist);
4277 (*bytes) += PAGE_SIZE;
4284 static int cifs_readpages(struct file *file, struct address_space *mapping,
4285 struct list_head *page_list, unsigned num_pages)
4288 struct list_head tmplist;
4289 struct cifsFileInfo *open_file = file->private_data;
4290 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4291 struct TCP_Server_Info *server;
4297 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4298 * immediately if the cookie is negative
4300 * After this point, every page in the list might have PG_fscache set,
4301 * so we will need to clean that up off of every page we don't use.
4303 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4310 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4311 pid = open_file->pid;
4313 pid = current->tgid;
4316 server = tlink_tcon(open_file->tlink)->ses->server;
4318 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4319 __func__, file, mapping, num_pages);
4322 * Start with the page at end of list and move it to private
4323 * list. Do the same with any following pages until we hit
4324 * the rsize limit, hit an index discontinuity, or run out of
4325 * pages. Issue the async read and then start the loop again
4326 * until the list is empty.
4328 * Note that list order is important. The page_list is in
4329 * the order of declining indexes. When we put the pages in
4330 * the rdata->pages, then we want them in increasing order.
4332 while (!list_empty(page_list)) {
4333 unsigned int i, nr_pages, bytes, rsize;
4335 struct page *page, *tpage;
4336 struct cifs_readdata *rdata;
4337 struct cifs_credits credits_on_stack;
4338 struct cifs_credits *credits = &credits_on_stack;
4340 if (open_file->invalidHandle) {
4341 rc = cifs_reopen_file(open_file, true);
4348 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
4354 * Give up immediately if rsize is too small to read an entire
4355 * page. The VFS will fall back to readpage. We should never
4356 * reach this point however since we set ra_pages to 0 when the
4357 * rsize is smaller than a cache page.
4359 if (unlikely(rsize < PAGE_SIZE)) {
4360 add_credits_and_wake_if(server, credits, 0);
4365 rc = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4366 &nr_pages, &offset, &bytes);
4368 add_credits_and_wake_if(server, credits, 0);
4372 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4374 /* best to give up if we're out of mem */
4375 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4376 list_del(&page->lru);
4377 lru_cache_add_file(page);
4382 add_credits_and_wake_if(server, credits, 0);
4386 rdata->cfile = cifsFileInfo_get(open_file);
4387 rdata->mapping = mapping;
4388 rdata->offset = offset;
4389 rdata->bytes = bytes;
4391 rdata->pagesz = PAGE_SIZE;
4392 rdata->tailsz = PAGE_SIZE;
4393 rdata->read_into_pages = cifs_readpages_read_into_pages;
4394 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4395 rdata->credits = credits_on_stack;
4397 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4398 list_del(&page->lru);
4399 rdata->pages[rdata->nr_pages++] = page;
4402 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4405 if (rdata->cfile->invalidHandle)
4408 rc = server->ops->async_readv(rdata);
4412 add_credits_and_wake_if(server, &rdata->credits, 0);
4413 for (i = 0; i < rdata->nr_pages; i++) {
4414 page = rdata->pages[i];
4415 lru_cache_add_file(page);
4419 /* Fallback to the readpage in error/reconnect cases */
4420 kref_put(&rdata->refcount, cifs_readdata_release);
4424 kref_put(&rdata->refcount, cifs_readdata_release);
4427 /* Any pages that have been shown to fscache but didn't get added to
4428 * the pagecache must be uncached before they get returned to the
4431 cifs_fscache_readpages_cancel(mapping->host, page_list);
4437 * cifs_readpage_worker must be called with the page pinned
4439 static int cifs_readpage_worker(struct file *file, struct page *page,
4445 /* Is the page cached? */
4446 rc = cifs_readpage_from_fscache(file_inode(file), page);
4450 read_data = kmap(page);
4451 /* for reads over a certain size could initiate async read ahead */
4453 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4458 cifs_dbg(FYI, "Bytes read %d\n", rc);
4460 /* we do not want atime to be less than mtime, it broke some apps */
4461 file_inode(file)->i_atime = current_time(file_inode(file));
4462 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4463 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4465 file_inode(file)->i_atime = current_time(file_inode(file));
4468 memset(read_data + rc, 0, PAGE_SIZE - rc);
4470 flush_dcache_page(page);
4471 SetPageUptodate(page);
4473 /* send this page to the cache */
4474 cifs_readpage_to_fscache(file_inode(file), page);
4486 static int cifs_readpage(struct file *file, struct page *page)
4488 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
4494 if (file->private_data == NULL) {
4500 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4501 page, (int)offset, (int)offset);
4503 rc = cifs_readpage_worker(file, page, &offset);
4509 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4511 struct cifsFileInfo *open_file;
4513 spin_lock(&cifs_inode->open_file_lock);
4514 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4515 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4516 spin_unlock(&cifs_inode->open_file_lock);
4520 spin_unlock(&cifs_inode->open_file_lock);
4524 /* We do not want to update the file size from server for inodes
4525 open for write - to avoid races with writepage extending
4526 the file - in the future we could consider allowing
4527 refreshing the inode only on increases in the file size
4528 but this is tricky to do without racing with writebehind
4529 page caching in the current Linux kernel design */
4530 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4535 if (is_inode_writable(cifsInode)) {
4536 /* This inode is open for write at least once */
4537 struct cifs_sb_info *cifs_sb;
4539 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4540 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4541 /* since no page cache to corrupt on directio
4542 we can change size safely */
4546 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4554 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4555 loff_t pos, unsigned len, unsigned flags,
4556 struct page **pagep, void **fsdata)
4559 pgoff_t index = pos >> PAGE_SHIFT;
4560 loff_t offset = pos & (PAGE_SIZE - 1);
4561 loff_t page_start = pos & PAGE_MASK;
4566 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4569 page = grab_cache_page_write_begin(mapping, index, flags);
4575 if (PageUptodate(page))
4579 * If we write a full page it will be up to date, no need to read from
4580 * the server. If the write is short, we'll end up doing a sync write
4583 if (len == PAGE_SIZE)
4587 * optimize away the read when we have an oplock, and we're not
4588 * expecting to use any of the data we'd be reading in. That
4589 * is, when the page lies beyond the EOF, or straddles the EOF
4590 * and the write will cover all of the existing data.
4592 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4593 i_size = i_size_read(mapping->host);
4594 if (page_start >= i_size ||
4595 (offset == 0 && (pos + len) >= i_size)) {
4596 zero_user_segments(page, 0, offset,
4600 * PageChecked means that the parts of the page
4601 * to which we're not writing are considered up
4602 * to date. Once the data is copied to the
4603 * page, it can be set uptodate.
4605 SetPageChecked(page);
4610 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4612 * might as well read a page, it is fast enough. If we get
4613 * an error, we don't need to return it. cifs_write_end will
4614 * do a sync write instead since PG_uptodate isn't set.
4616 cifs_readpage_worker(file, page, &page_start);
4621 /* we could try using another file handle if there is one -
4622 but how would we lock it to prevent close of that handle
4623 racing with this read? In any case
4624 this will be written out by write_end so is fine */
4631 static int cifs_release_page(struct page *page, gfp_t gfp)
4633 if (PagePrivate(page))
4636 return cifs_fscache_release_page(page, gfp);
4639 static void cifs_invalidate_page(struct page *page, unsigned int offset,
4640 unsigned int length)
4642 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4644 if (offset == 0 && length == PAGE_SIZE)
4645 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4648 static int cifs_launder_page(struct page *page)
4651 loff_t range_start = page_offset(page);
4652 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
4653 struct writeback_control wbc = {
4654 .sync_mode = WB_SYNC_ALL,
4656 .range_start = range_start,
4657 .range_end = range_end,
4660 cifs_dbg(FYI, "Launder page: %p\n", page);
4662 if (clear_page_dirty_for_io(page))
4663 rc = cifs_writepage_locked(page, &wbc);
4665 cifs_fscache_invalidate_page(page, page->mapping->host);
4669 void cifs_oplock_break(struct work_struct *work)
4671 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4673 struct inode *inode = d_inode(cfile->dentry);
4674 struct cifsInodeInfo *cinode = CIFS_I(inode);
4675 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4676 struct TCP_Server_Info *server = tcon->ses->server;
4679 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4680 TASK_UNINTERRUPTIBLE);
4682 server->ops->downgrade_oplock(server, cinode,
4683 test_bit(CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, &cinode->flags));
4685 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4686 cifs_has_mand_locks(cinode)) {
4687 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4692 if (inode && S_ISREG(inode->i_mode)) {
4693 if (CIFS_CACHE_READ(cinode))
4694 break_lease(inode, O_RDONLY);
4696 break_lease(inode, O_WRONLY);
4697 rc = filemap_fdatawrite(inode->i_mapping);
4698 if (!CIFS_CACHE_READ(cinode)) {
4699 rc = filemap_fdatawait(inode->i_mapping);
4700 mapping_set_error(inode->i_mapping, rc);
4701 cifs_zap_mapping(inode);
4703 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4706 rc = cifs_push_locks(cfile);
4708 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4711 * releasing stale oplock after recent reconnect of smb session using
4712 * a now incorrect file handle is not a data integrity issue but do
4713 * not bother sending an oplock release if session to server still is
4714 * disconnected since oplock already released by the server
4716 if (!cfile->oplock_break_cancelled) {
4717 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4719 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4721 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4722 cifs_done_oplock_break(cinode);
4726 * The presence of cifs_direct_io() in the address space ops vector
4727 * allowes open() O_DIRECT flags which would have failed otherwise.
4729 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4730 * so this method should never be called.
4732 * Direct IO is not yet supported in the cached mode.
4735 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4739 * Eventually need to support direct IO for non forcedirectio mounts
4745 const struct address_space_operations cifs_addr_ops = {
4746 .readpage = cifs_readpage,
4747 .readpages = cifs_readpages,
4748 .writepage = cifs_writepage,
4749 .writepages = cifs_writepages,
4750 .write_begin = cifs_write_begin,
4751 .write_end = cifs_write_end,
4752 .set_page_dirty = __set_page_dirty_nobuffers,
4753 .releasepage = cifs_release_page,
4754 .direct_IO = cifs_direct_io,
4755 .invalidatepage = cifs_invalidate_page,
4756 .launder_page = cifs_launder_page,
4760 * cifs_readpages requires the server to support a buffer large enough to
4761 * contain the header plus one complete page of data. Otherwise, we need
4762 * to leave cifs_readpages out of the address space operations.
4764 const struct address_space_operations cifs_addr_ops_smallbuf = {
4765 .readpage = cifs_readpage,
4766 .writepage = cifs_writepage,
4767 .writepages = cifs_writepages,
4768 .write_begin = cifs_write_begin,
4769 .write_end = cifs_write_end,
4770 .set_page_dirty = __set_page_dirty_nobuffers,
4771 .releasepage = cifs_release_page,
4772 .invalidatepage = cifs_invalidate_page,
4773 .launder_page = cifs_launder_page,