4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/backing-dev.h>
26 #include <linux/stat.h>
27 #include <linux/fcntl.h>
28 #include <linux/pagemap.h>
29 #include <linux/pagevec.h>
30 #include <linux/writeback.h>
31 #include <linux/task_io_accounting_ops.h>
32 #include <linux/delay.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
37 #include <asm/div64.h>
41 #include "cifsproto.h"
42 #include "cifs_unicode.h"
43 #include "cifs_debug.h"
44 #include "cifs_fs_sb.h"
46 #include "smbdirect.h"
48 static inline int cifs_convert_flags(unsigned int flags)
50 if ((flags & O_ACCMODE) == O_RDONLY)
52 else if ((flags & O_ACCMODE) == O_WRONLY)
54 else if ((flags & O_ACCMODE) == O_RDWR) {
55 /* GENERIC_ALL is too much permission to request
56 can cause unnecessary access denied on create */
57 /* return GENERIC_ALL; */
58 return (GENERIC_READ | GENERIC_WRITE);
61 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
62 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
66 static u32 cifs_posix_convert_flags(unsigned int flags)
70 if ((flags & O_ACCMODE) == O_RDONLY)
71 posix_flags = SMB_O_RDONLY;
72 else if ((flags & O_ACCMODE) == O_WRONLY)
73 posix_flags = SMB_O_WRONLY;
74 else if ((flags & O_ACCMODE) == O_RDWR)
75 posix_flags = SMB_O_RDWR;
77 if (flags & O_CREAT) {
78 posix_flags |= SMB_O_CREAT;
80 posix_flags |= SMB_O_EXCL;
81 } else if (flags & O_EXCL)
82 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
83 current->comm, current->tgid);
86 posix_flags |= SMB_O_TRUNC;
87 /* be safe and imply O_SYNC for O_DSYNC */
89 posix_flags |= SMB_O_SYNC;
90 if (flags & O_DIRECTORY)
91 posix_flags |= SMB_O_DIRECTORY;
92 if (flags & O_NOFOLLOW)
93 posix_flags |= SMB_O_NOFOLLOW;
95 posix_flags |= SMB_O_DIRECT;
100 static inline int cifs_get_disposition(unsigned int flags)
102 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
104 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
105 return FILE_OVERWRITE_IF;
106 else if ((flags & O_CREAT) == O_CREAT)
108 else if ((flags & O_TRUNC) == O_TRUNC)
109 return FILE_OVERWRITE;
114 int cifs_posix_open(char *full_path, struct inode **pinode,
115 struct super_block *sb, int mode, unsigned int f_flags,
116 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
119 FILE_UNIX_BASIC_INFO *presp_data;
120 __u32 posix_flags = 0;
121 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
122 struct cifs_fattr fattr;
123 struct tcon_link *tlink;
124 struct cifs_tcon *tcon;
126 cifs_dbg(FYI, "posix open %s\n", full_path);
128 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
129 if (presp_data == NULL)
132 tlink = cifs_sb_tlink(cifs_sb);
138 tcon = tlink_tcon(tlink);
139 mode &= ~current_umask();
141 posix_flags = cifs_posix_convert_flags(f_flags);
142 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
143 poplock, full_path, cifs_sb->local_nls,
144 cifs_remap(cifs_sb));
145 cifs_put_tlink(tlink);
150 if (presp_data->Type == cpu_to_le32(-1))
151 goto posix_open_ret; /* open ok, caller does qpathinfo */
154 goto posix_open_ret; /* caller does not need info */
156 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
158 /* get new inode and set it up */
159 if (*pinode == NULL) {
160 cifs_fill_uniqueid(sb, &fattr);
161 *pinode = cifs_iget(sb, &fattr);
167 cifs_fattr_to_inode(*pinode, &fattr);
176 cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
177 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
178 struct cifs_fid *fid, unsigned int xid)
183 int create_options = CREATE_NOT_DIR;
185 struct TCP_Server_Info *server = tcon->ses->server;
186 struct cifs_open_parms oparms;
188 if (!server->ops->open)
191 desired_access = cifs_convert_flags(f_flags);
193 /*********************************************************************
194 * open flag mapping table:
196 * POSIX Flag CIFS Disposition
197 * ---------- ----------------
198 * O_CREAT FILE_OPEN_IF
199 * O_CREAT | O_EXCL FILE_CREATE
200 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
201 * O_TRUNC FILE_OVERWRITE
202 * none of the above FILE_OPEN
204 * Note that there is not a direct match between disposition
205 * FILE_SUPERSEDE (ie create whether or not file exists although
206 * O_CREAT | O_TRUNC is similar but truncates the existing
207 * file rather than creating a new file as FILE_SUPERSEDE does
208 * (which uses the attributes / metadata passed in on open call)
210 *? O_SYNC is a reasonable match to CIFS writethrough flag
211 *? and the read write flags match reasonably. O_LARGEFILE
212 *? is irrelevant because largefile support is always used
213 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
214 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
215 *********************************************************************/
217 disposition = cifs_get_disposition(f_flags);
219 /* BB pass O_SYNC flag through on file attributes .. BB */
221 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
225 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_getattr)
500 server->ops->close_getattr(xid, tcon, cifs_file);
501 else if (server->ops->close)
502 server->ops->close(xid, tcon, &cifs_file->fid);
506 if (oplock_break_cancelled)
507 cifs_done_oplock_break(cifsi);
509 cifs_del_pending_open(&open);
512 queue_work(fileinfo_put_wq, &cifs_file->put);
514 cifsFileInfo_put_final(cifs_file);
517 int cifs_open(struct inode *inode, struct file *file)
523 struct cifs_sb_info *cifs_sb;
524 struct TCP_Server_Info *server;
525 struct cifs_tcon *tcon;
526 struct tcon_link *tlink;
527 struct cifsFileInfo *cfile = NULL;
528 char *full_path = NULL;
529 bool posix_open_ok = false;
531 struct cifs_pending_open open;
535 cifs_sb = CIFS_SB(inode->i_sb);
536 tlink = cifs_sb_tlink(cifs_sb);
539 return PTR_ERR(tlink);
541 tcon = tlink_tcon(tlink);
542 server = tcon->ses->server;
544 full_path = build_path_from_dentry(file_dentry(file));
545 if (full_path == NULL) {
550 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
551 inode, file->f_flags, full_path);
553 if (file->f_flags & O_DIRECT &&
554 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
555 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
556 file->f_op = &cifs_file_direct_nobrl_ops;
558 file->f_op = &cifs_file_direct_ops;
566 if (!tcon->broken_posix_open && tcon->unix_ext &&
567 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
568 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
569 /* can not refresh inode info since size could be stale */
570 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
571 cifs_sb->mnt_file_mode /* ignored */,
572 file->f_flags, &oplock, &fid.netfid, xid);
574 cifs_dbg(FYI, "posix open succeeded\n");
575 posix_open_ok = true;
576 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
577 if (tcon->ses->serverNOS)
578 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",
579 tcon->ses->serverName,
580 tcon->ses->serverNOS);
581 tcon->broken_posix_open = true;
582 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
583 (rc != -EOPNOTSUPP)) /* path not found or net err */
586 * Else fallthrough to retry open the old way on network i/o
591 if (server->ops->get_lease_key)
592 server->ops->get_lease_key(inode, &fid);
594 cifs_add_pending_open(&fid, tlink, &open);
596 if (!posix_open_ok) {
597 if (server->ops->get_lease_key)
598 server->ops->get_lease_key(inode, &fid);
600 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
601 file->f_flags, &oplock, &fid, xid);
603 cifs_del_pending_open(&open);
608 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
610 if (server->ops->close)
611 server->ops->close(xid, tcon, &fid);
612 cifs_del_pending_open(&open);
617 cifs_fscache_set_inode_cookie(inode, file);
619 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
621 * Time to set mode which we can not set earlier due to
622 * problems creating new read-only files.
624 struct cifs_unix_set_info_args args = {
625 .mode = inode->i_mode,
626 .uid = INVALID_UID, /* no change */
627 .gid = INVALID_GID, /* no change */
628 .ctime = NO_CHANGE_64,
629 .atime = NO_CHANGE_64,
630 .mtime = NO_CHANGE_64,
633 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
640 cifs_put_tlink(tlink);
644 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
647 * Try to reacquire byte range locks that were released when session
648 * to server was lost.
651 cifs_relock_file(struct cifsFileInfo *cfile)
653 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
654 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
655 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
658 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
659 if (cinode->can_cache_brlcks) {
660 /* can cache locks - no need to relock */
661 up_read(&cinode->lock_sem);
665 if (cap_unix(tcon->ses) &&
666 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
667 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
668 rc = cifs_push_posix_locks(cfile);
670 rc = tcon->ses->server->ops->push_mand_locks(cfile);
672 up_read(&cinode->lock_sem);
677 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
682 struct cifs_sb_info *cifs_sb;
683 struct cifs_tcon *tcon;
684 struct TCP_Server_Info *server;
685 struct cifsInodeInfo *cinode;
687 char *full_path = NULL;
689 int disposition = FILE_OPEN;
690 int create_options = CREATE_NOT_DIR;
691 struct cifs_open_parms oparms;
694 mutex_lock(&cfile->fh_mutex);
695 if (!cfile->invalidHandle) {
696 mutex_unlock(&cfile->fh_mutex);
702 inode = d_inode(cfile->dentry);
703 cifs_sb = CIFS_SB(inode->i_sb);
704 tcon = tlink_tcon(cfile->tlink);
705 server = tcon->ses->server;
708 * Can not grab rename sem here because various ops, including those
709 * that already have the rename sem can end up causing writepage to get
710 * called and if the server was down that means we end up here, and we
711 * can never tell if the caller already has the rename_sem.
713 full_path = build_path_from_dentry(cfile->dentry);
714 if (full_path == NULL) {
716 mutex_unlock(&cfile->fh_mutex);
721 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
722 inode, cfile->f_flags, full_path);
724 if (tcon->ses->server->oplocks)
729 if (tcon->unix_ext && cap_unix(tcon->ses) &&
730 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
731 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
733 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
734 * original open. Must mask them off for a reopen.
736 unsigned int oflags = cfile->f_flags &
737 ~(O_CREAT | O_EXCL | O_TRUNC);
739 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
740 cifs_sb->mnt_file_mode /* ignored */,
741 oflags, &oplock, &cfile->fid.netfid, xid);
743 cifs_dbg(FYI, "posix reopen succeeded\n");
744 oparms.reconnect = true;
748 * fallthrough to retry open the old way on errors, especially
749 * in the reconnect path it is important to retry hard
753 desired_access = cifs_convert_flags(cfile->f_flags);
755 if (backup_cred(cifs_sb))
756 create_options |= CREATE_OPEN_BACKUP_INTENT;
758 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
759 if (cfile->f_flags & O_SYNC)
760 create_options |= CREATE_WRITE_THROUGH;
762 if (cfile->f_flags & O_DIRECT)
763 create_options |= CREATE_NO_BUFFER;
765 if (server->ops->get_lease_key)
766 server->ops->get_lease_key(inode, &cfile->fid);
769 oparms.cifs_sb = cifs_sb;
770 oparms.desired_access = desired_access;
771 oparms.create_options = create_options;
772 oparms.disposition = disposition;
773 oparms.path = full_path;
774 oparms.fid = &cfile->fid;
775 oparms.reconnect = true;
778 * Can not refresh inode by passing in file_info buf to be returned by
779 * ops->open and then calling get_inode_info with returned buf since
780 * file might have write behind data that needs to be flushed and server
781 * version of file size can be stale. If we knew for sure that inode was
782 * not dirty locally we could do this.
784 rc = server->ops->open(xid, &oparms, &oplock, NULL);
785 if (rc == -ENOENT && oparms.reconnect == false) {
786 /* durable handle timeout is expired - open the file again */
787 rc = server->ops->open(xid, &oparms, &oplock, NULL);
788 /* indicate that we need to relock the file */
789 oparms.reconnect = true;
793 mutex_unlock(&cfile->fh_mutex);
794 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
795 cifs_dbg(FYI, "oplock: %d\n", oplock);
796 goto reopen_error_exit;
800 cfile->invalidHandle = false;
801 mutex_unlock(&cfile->fh_mutex);
802 cinode = CIFS_I(inode);
805 rc = filemap_write_and_wait(inode->i_mapping);
806 if (!is_interrupt_error(rc))
807 mapping_set_error(inode->i_mapping, rc);
810 rc = cifs_get_inode_info_unix(&inode, full_path,
813 rc = cifs_get_inode_info(&inode, full_path, NULL,
814 inode->i_sb, xid, NULL);
817 * Else we are writing out data to server already and could deadlock if
818 * we tried to flush data, and since we do not know if we have data that
819 * would invalidate the current end of file on the server we can not go
820 * to the server to get the new inode info.
824 * If the server returned a read oplock and we have mandatory brlocks,
825 * set oplock level to None.
827 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
828 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
832 server->ops->set_fid(cfile, &cfile->fid, oplock);
833 if (oparms.reconnect)
834 cifs_relock_file(cfile);
842 int cifs_close(struct inode *inode, struct file *file)
844 if (file->private_data != NULL) {
845 _cifsFileInfo_put(file->private_data, true, false);
846 file->private_data = NULL;
849 /* return code from the ->release op is always ignored */
854 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
856 struct cifsFileInfo *open_file;
857 struct list_head *tmp;
858 struct list_head *tmp1;
859 struct list_head tmp_list;
861 if (!tcon->use_persistent || !tcon->need_reopen_files)
864 tcon->need_reopen_files = false;
866 cifs_dbg(FYI, "Reopen persistent handles");
867 INIT_LIST_HEAD(&tmp_list);
869 /* list all files open on tree connection, reopen resilient handles */
870 spin_lock(&tcon->open_file_lock);
871 list_for_each(tmp, &tcon->openFileList) {
872 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
873 if (!open_file->invalidHandle)
875 cifsFileInfo_get(open_file);
876 list_add_tail(&open_file->rlist, &tmp_list);
878 spin_unlock(&tcon->open_file_lock);
880 list_for_each_safe(tmp, tmp1, &tmp_list) {
881 open_file = list_entry(tmp, struct cifsFileInfo, rlist);
882 if (cifs_reopen_file(open_file, false /* do not flush */))
883 tcon->need_reopen_files = true;
884 list_del_init(&open_file->rlist);
885 cifsFileInfo_put(open_file);
889 int cifs_closedir(struct inode *inode, struct file *file)
893 struct cifsFileInfo *cfile = file->private_data;
894 struct cifs_tcon *tcon;
895 struct TCP_Server_Info *server;
898 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
904 tcon = tlink_tcon(cfile->tlink);
905 server = tcon->ses->server;
907 cifs_dbg(FYI, "Freeing private data in close dir\n");
908 spin_lock(&cfile->file_info_lock);
909 if (server->ops->dir_needs_close(cfile)) {
910 cfile->invalidHandle = true;
911 spin_unlock(&cfile->file_info_lock);
912 if (server->ops->close_dir)
913 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
916 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
917 /* not much we can do if it fails anyway, ignore rc */
920 spin_unlock(&cfile->file_info_lock);
922 buf = cfile->srch_inf.ntwrk_buf_start;
924 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
925 cfile->srch_inf.ntwrk_buf_start = NULL;
926 if (cfile->srch_inf.smallBuf)
927 cifs_small_buf_release(buf);
929 cifs_buf_release(buf);
932 cifs_put_tlink(cfile->tlink);
933 kfree(file->private_data);
934 file->private_data = NULL;
935 /* BB can we lock the filestruct while this is going on? */
940 static struct cifsLockInfo *
941 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
943 struct cifsLockInfo *lock =
944 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
947 lock->offset = offset;
948 lock->length = length;
950 lock->pid = current->tgid;
952 INIT_LIST_HEAD(&lock->blist);
953 init_waitqueue_head(&lock->block_q);
958 cifs_del_lock_waiters(struct cifsLockInfo *lock)
960 struct cifsLockInfo *li, *tmp;
961 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
962 list_del_init(&li->blist);
963 wake_up(&li->block_q);
967 #define CIFS_LOCK_OP 0
968 #define CIFS_READ_OP 1
969 #define CIFS_WRITE_OP 2
971 /* @rw_check : 0 - no op, 1 - read, 2 - write */
973 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
974 __u64 length, __u8 type, __u16 flags,
975 struct cifsFileInfo *cfile,
976 struct cifsLockInfo **conf_lock, int rw_check)
978 struct cifsLockInfo *li;
979 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
980 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
982 list_for_each_entry(li, &fdlocks->locks, llist) {
983 if (offset + length <= li->offset ||
984 offset >= li->offset + li->length)
986 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
987 server->ops->compare_fids(cfile, cur_cfile)) {
988 /* shared lock prevents write op through the same fid */
989 if (!(li->type & server->vals->shared_lock_type) ||
990 rw_check != CIFS_WRITE_OP)
993 if ((type & server->vals->shared_lock_type) &&
994 ((server->ops->compare_fids(cfile, cur_cfile) &&
995 current->tgid == li->pid) || type == li->type))
997 if (rw_check == CIFS_LOCK_OP &&
998 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
999 server->ops->compare_fids(cfile, cur_cfile))
1009 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1010 __u8 type, __u16 flags,
1011 struct cifsLockInfo **conf_lock, int rw_check)
1014 struct cifs_fid_locks *cur;
1015 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1017 list_for_each_entry(cur, &cinode->llist, llist) {
1018 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1019 flags, cfile, conf_lock,
1029 * Check if there is another lock that prevents us to set the lock (mandatory
1030 * style). If such a lock exists, update the flock structure with its
1031 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1032 * or leave it the same if we can't. Returns 0 if we don't need to request to
1033 * the server or 1 otherwise.
1036 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1037 __u8 type, struct file_lock *flock)
1040 struct cifsLockInfo *conf_lock;
1041 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1042 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1045 down_read(&cinode->lock_sem);
1047 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1048 flock->fl_flags, &conf_lock,
1051 flock->fl_start = conf_lock->offset;
1052 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1053 flock->fl_pid = conf_lock->pid;
1054 if (conf_lock->type & server->vals->shared_lock_type)
1055 flock->fl_type = F_RDLCK;
1057 flock->fl_type = F_WRLCK;
1058 } else if (!cinode->can_cache_brlcks)
1061 flock->fl_type = F_UNLCK;
1063 up_read(&cinode->lock_sem);
1068 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1070 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1071 cifs_down_write(&cinode->lock_sem);
1072 list_add_tail(&lock->llist, &cfile->llist->locks);
1073 up_write(&cinode->lock_sem);
1077 * Set the byte-range lock (mandatory style). Returns:
1078 * 1) 0, if we set the lock and don't need to request to the server;
1079 * 2) 1, if no locks prevent us but we need to request to the server;
1080 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1083 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1086 struct cifsLockInfo *conf_lock;
1087 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1093 cifs_down_write(&cinode->lock_sem);
1095 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1096 lock->type, lock->flags, &conf_lock,
1098 if (!exist && cinode->can_cache_brlcks) {
1099 list_add_tail(&lock->llist, &cfile->llist->locks);
1100 up_write(&cinode->lock_sem);
1109 list_add_tail(&lock->blist, &conf_lock->blist);
1110 up_write(&cinode->lock_sem);
1111 rc = wait_event_interruptible(lock->block_q,
1112 (lock->blist.prev == &lock->blist) &&
1113 (lock->blist.next == &lock->blist));
1116 cifs_down_write(&cinode->lock_sem);
1117 list_del_init(&lock->blist);
1120 up_write(&cinode->lock_sem);
1125 * Check if there is another lock that prevents us to set the lock (posix
1126 * style). If such a lock exists, update the flock structure with its
1127 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1128 * or leave it the same if we can't. Returns 0 if we don't need to request to
1129 * the server or 1 otherwise.
1132 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1135 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1136 unsigned char saved_type = flock->fl_type;
1138 if ((flock->fl_flags & FL_POSIX) == 0)
1141 down_read(&cinode->lock_sem);
1142 posix_test_lock(file, flock);
1144 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1145 flock->fl_type = saved_type;
1149 up_read(&cinode->lock_sem);
1154 * Set the byte-range lock (posix style). Returns:
1155 * 1) 0, if we set the lock and don't need to request to the server;
1156 * 2) 1, if we need to request to the server;
1157 * 3) <0, if the error occurs while setting the lock.
1160 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1162 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1165 if ((flock->fl_flags & FL_POSIX) == 0)
1169 cifs_down_write(&cinode->lock_sem);
1170 if (!cinode->can_cache_brlcks) {
1171 up_write(&cinode->lock_sem);
1175 rc = posix_lock_file(file, flock, NULL);
1176 up_write(&cinode->lock_sem);
1177 if (rc == FILE_LOCK_DEFERRED) {
1178 rc = wait_event_interruptible(flock->fl_wait, !flock->fl_blocker);
1181 locks_delete_block(flock);
1187 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1190 int rc = 0, stored_rc;
1191 struct cifsLockInfo *li, *tmp;
1192 struct cifs_tcon *tcon;
1193 unsigned int num, max_num, max_buf;
1194 LOCKING_ANDX_RANGE *buf, *cur;
1195 static const int types[] = {
1196 LOCKING_ANDX_LARGE_FILES,
1197 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1202 tcon = tlink_tcon(cfile->tlink);
1205 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1206 * and check it before using.
1208 max_buf = tcon->ses->server->maxBuf;
1209 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1214 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1216 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1218 max_num = (max_buf - sizeof(struct smb_hdr)) /
1219 sizeof(LOCKING_ANDX_RANGE);
1220 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1226 for (i = 0; i < 2; i++) {
1229 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1230 if (li->type != types[i])
1232 cur->Pid = cpu_to_le16(li->pid);
1233 cur->LengthLow = cpu_to_le32((u32)li->length);
1234 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1235 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1236 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1237 if (++num == max_num) {
1238 stored_rc = cifs_lockv(xid, tcon,
1240 (__u8)li->type, 0, num,
1251 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1252 (__u8)types[i], 0, num, buf);
1264 hash_lockowner(fl_owner_t owner)
1266 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1269 struct lock_to_push {
1270 struct list_head llist;
1279 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1281 struct inode *inode = d_inode(cfile->dentry);
1282 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1283 struct file_lock *flock;
1284 struct file_lock_context *flctx = inode->i_flctx;
1285 unsigned int count = 0, i;
1286 int rc = 0, xid, type;
1287 struct list_head locks_to_send, *el;
1288 struct lock_to_push *lck, *tmp;
1296 spin_lock(&flctx->flc_lock);
1297 list_for_each(el, &flctx->flc_posix) {
1300 spin_unlock(&flctx->flc_lock);
1302 INIT_LIST_HEAD(&locks_to_send);
1305 * Allocating count locks is enough because no FL_POSIX locks can be
1306 * added to the list while we are holding cinode->lock_sem that
1307 * protects locking operations of this inode.
1309 for (i = 0; i < count; i++) {
1310 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1315 list_add_tail(&lck->llist, &locks_to_send);
1318 el = locks_to_send.next;
1319 spin_lock(&flctx->flc_lock);
1320 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1321 if (el == &locks_to_send) {
1323 * The list ended. We don't have enough allocated
1324 * structures - something is really wrong.
1326 cifs_dbg(VFS, "Can't push all brlocks!\n");
1329 length = 1 + flock->fl_end - flock->fl_start;
1330 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1334 lck = list_entry(el, struct lock_to_push, llist);
1335 lck->pid = hash_lockowner(flock->fl_owner);
1336 lck->netfid = cfile->fid.netfid;
1337 lck->length = length;
1339 lck->offset = flock->fl_start;
1341 spin_unlock(&flctx->flc_lock);
1343 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1346 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1347 lck->offset, lck->length, NULL,
1351 list_del(&lck->llist);
1359 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1360 list_del(&lck->llist);
1367 cifs_push_locks(struct cifsFileInfo *cfile)
1369 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1370 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1371 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1374 /* we are going to update can_cache_brlcks here - need a write access */
1375 cifs_down_write(&cinode->lock_sem);
1376 if (!cinode->can_cache_brlcks) {
1377 up_write(&cinode->lock_sem);
1381 if (cap_unix(tcon->ses) &&
1382 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1383 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1384 rc = cifs_push_posix_locks(cfile);
1386 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1388 cinode->can_cache_brlcks = false;
1389 up_write(&cinode->lock_sem);
1394 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1395 bool *wait_flag, struct TCP_Server_Info *server)
1397 if (flock->fl_flags & FL_POSIX)
1398 cifs_dbg(FYI, "Posix\n");
1399 if (flock->fl_flags & FL_FLOCK)
1400 cifs_dbg(FYI, "Flock\n");
1401 if (flock->fl_flags & FL_SLEEP) {
1402 cifs_dbg(FYI, "Blocking lock\n");
1405 if (flock->fl_flags & FL_ACCESS)
1406 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1407 if (flock->fl_flags & FL_LEASE)
1408 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1409 if (flock->fl_flags &
1410 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1411 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1412 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1414 *type = server->vals->large_lock_type;
1415 if (flock->fl_type == F_WRLCK) {
1416 cifs_dbg(FYI, "F_WRLCK\n");
1417 *type |= server->vals->exclusive_lock_type;
1419 } else if (flock->fl_type == F_UNLCK) {
1420 cifs_dbg(FYI, "F_UNLCK\n");
1421 *type |= server->vals->unlock_lock_type;
1423 /* Check if unlock includes more than one lock range */
1424 } else if (flock->fl_type == F_RDLCK) {
1425 cifs_dbg(FYI, "F_RDLCK\n");
1426 *type |= server->vals->shared_lock_type;
1428 } else if (flock->fl_type == F_EXLCK) {
1429 cifs_dbg(FYI, "F_EXLCK\n");
1430 *type |= server->vals->exclusive_lock_type;
1432 } else if (flock->fl_type == F_SHLCK) {
1433 cifs_dbg(FYI, "F_SHLCK\n");
1434 *type |= server->vals->shared_lock_type;
1437 cifs_dbg(FYI, "Unknown type of lock\n");
1441 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1442 bool wait_flag, bool posix_lck, unsigned int xid)
1445 __u64 length = 1 + flock->fl_end - flock->fl_start;
1446 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1447 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1448 struct TCP_Server_Info *server = tcon->ses->server;
1449 __u16 netfid = cfile->fid.netfid;
1452 int posix_lock_type;
1454 rc = cifs_posix_lock_test(file, flock);
1458 if (type & server->vals->shared_lock_type)
1459 posix_lock_type = CIFS_RDLCK;
1461 posix_lock_type = CIFS_WRLCK;
1462 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1463 hash_lockowner(flock->fl_owner),
1464 flock->fl_start, length, flock,
1465 posix_lock_type, wait_flag);
1469 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1473 /* BB we could chain these into one lock request BB */
1474 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1477 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1479 flock->fl_type = F_UNLCK;
1481 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1486 if (type & server->vals->shared_lock_type) {
1487 flock->fl_type = F_WRLCK;
1491 type &= ~server->vals->exclusive_lock_type;
1493 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1494 type | server->vals->shared_lock_type,
1497 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1498 type | server->vals->shared_lock_type, 0, 1, false);
1499 flock->fl_type = F_RDLCK;
1501 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1504 flock->fl_type = F_WRLCK;
1510 cifs_move_llist(struct list_head *source, struct list_head *dest)
1512 struct list_head *li, *tmp;
1513 list_for_each_safe(li, tmp, source)
1514 list_move(li, dest);
1518 cifs_free_llist(struct list_head *llist)
1520 struct cifsLockInfo *li, *tmp;
1521 list_for_each_entry_safe(li, tmp, llist, llist) {
1522 cifs_del_lock_waiters(li);
1523 list_del(&li->llist);
1529 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1532 int rc = 0, stored_rc;
1533 static const int types[] = {
1534 LOCKING_ANDX_LARGE_FILES,
1535 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1538 unsigned int max_num, num, max_buf;
1539 LOCKING_ANDX_RANGE *buf, *cur;
1540 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1541 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1542 struct cifsLockInfo *li, *tmp;
1543 __u64 length = 1 + flock->fl_end - flock->fl_start;
1544 struct list_head tmp_llist;
1546 INIT_LIST_HEAD(&tmp_llist);
1549 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1550 * and check it before using.
1552 max_buf = tcon->ses->server->maxBuf;
1553 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1556 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1558 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1560 max_num = (max_buf - sizeof(struct smb_hdr)) /
1561 sizeof(LOCKING_ANDX_RANGE);
1562 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1566 cifs_down_write(&cinode->lock_sem);
1567 for (i = 0; i < 2; i++) {
1570 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1571 if (flock->fl_start > li->offset ||
1572 (flock->fl_start + length) <
1573 (li->offset + li->length))
1575 if (current->tgid != li->pid)
1577 if (types[i] != li->type)
1579 if (cinode->can_cache_brlcks) {
1581 * We can cache brlock requests - simply remove
1582 * a lock from the file's list.
1584 list_del(&li->llist);
1585 cifs_del_lock_waiters(li);
1589 cur->Pid = cpu_to_le16(li->pid);
1590 cur->LengthLow = cpu_to_le32((u32)li->length);
1591 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1592 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1593 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1595 * We need to save a lock here to let us add it again to
1596 * the file's list if the unlock range request fails on
1599 list_move(&li->llist, &tmp_llist);
1600 if (++num == max_num) {
1601 stored_rc = cifs_lockv(xid, tcon,
1603 li->type, num, 0, buf);
1606 * We failed on the unlock range
1607 * request - add all locks from the tmp
1608 * list to the head of the file's list.
1610 cifs_move_llist(&tmp_llist,
1611 &cfile->llist->locks);
1615 * The unlock range request succeed -
1616 * free the tmp list.
1618 cifs_free_llist(&tmp_llist);
1625 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1626 types[i], num, 0, buf);
1628 cifs_move_llist(&tmp_llist,
1629 &cfile->llist->locks);
1632 cifs_free_llist(&tmp_llist);
1636 up_write(&cinode->lock_sem);
1642 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1643 bool wait_flag, bool posix_lck, int lock, int unlock,
1647 __u64 length = 1 + flock->fl_end - flock->fl_start;
1648 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1649 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1650 struct TCP_Server_Info *server = tcon->ses->server;
1651 struct inode *inode = d_inode(cfile->dentry);
1654 int posix_lock_type;
1656 rc = cifs_posix_lock_set(file, flock);
1660 if (type & server->vals->shared_lock_type)
1661 posix_lock_type = CIFS_RDLCK;
1663 posix_lock_type = CIFS_WRLCK;
1666 posix_lock_type = CIFS_UNLCK;
1668 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1669 hash_lockowner(flock->fl_owner),
1670 flock->fl_start, length,
1671 NULL, posix_lock_type, wait_flag);
1676 struct cifsLockInfo *lock;
1678 lock = cifs_lock_init(flock->fl_start, length, type,
1683 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1692 * Windows 7 server can delay breaking lease from read to None
1693 * if we set a byte-range lock on a file - break it explicitly
1694 * before sending the lock to the server to be sure the next
1695 * read won't conflict with non-overlapted locks due to
1698 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1699 CIFS_CACHE_READ(CIFS_I(inode))) {
1700 cifs_zap_mapping(inode);
1701 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1703 CIFS_I(inode)->oplock = 0;
1706 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1707 type, 1, 0, wait_flag);
1713 cifs_lock_add(cfile, lock);
1715 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1718 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1720 * If this is a request to remove all locks because we
1721 * are closing the file, it doesn't matter if the
1722 * unlocking failed as both cifs.ko and the SMB server
1723 * remove the lock on file close
1726 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1727 if (!(flock->fl_flags & FL_CLOSE))
1730 rc = locks_lock_file_wait(file, flock);
1735 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1738 int lock = 0, unlock = 0;
1739 bool wait_flag = false;
1740 bool posix_lck = false;
1741 struct cifs_sb_info *cifs_sb;
1742 struct cifs_tcon *tcon;
1743 struct cifsFileInfo *cfile;
1749 if (!(fl->fl_flags & FL_FLOCK))
1752 cfile = (struct cifsFileInfo *)file->private_data;
1753 tcon = tlink_tcon(cfile->tlink);
1755 cifs_read_flock(fl, &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 if (!lock && !unlock) {
1766 * if no lock or unlock then nothing to do since we do not
1773 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1781 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1784 int lock = 0, unlock = 0;
1785 bool wait_flag = false;
1786 bool posix_lck = false;
1787 struct cifs_sb_info *cifs_sb;
1788 struct cifs_tcon *tcon;
1789 struct cifsFileInfo *cfile;
1795 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1796 cmd, flock->fl_flags, flock->fl_type,
1797 flock->fl_start, flock->fl_end);
1799 cfile = (struct cifsFileInfo *)file->private_data;
1800 tcon = tlink_tcon(cfile->tlink);
1802 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1804 cifs_sb = CIFS_FILE_SB(file);
1806 if (cap_unix(tcon->ses) &&
1807 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1808 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1811 * BB add code here to normalize offset and length to account for
1812 * negative length which we can not accept over the wire.
1814 if (IS_GETLK(cmd)) {
1815 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1820 if (!lock && !unlock) {
1822 * if no lock or unlock then nothing to do since we do not
1829 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1836 * update the file size (if needed) after a write. Should be called with
1837 * the inode->i_lock held
1840 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1841 unsigned int bytes_written)
1843 loff_t end_of_write = offset + bytes_written;
1845 if (end_of_write > cifsi->server_eof)
1846 cifsi->server_eof = end_of_write;
1850 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1851 size_t write_size, loff_t *offset)
1854 unsigned int bytes_written = 0;
1855 unsigned int total_written;
1856 struct cifs_tcon *tcon;
1857 struct TCP_Server_Info *server;
1859 struct dentry *dentry = open_file->dentry;
1860 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
1861 struct cifs_io_parms io_parms;
1863 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1864 write_size, *offset, dentry);
1866 tcon = tlink_tcon(open_file->tlink);
1867 server = tcon->ses->server;
1869 if (!server->ops->sync_write)
1874 for (total_written = 0; write_size > total_written;
1875 total_written += bytes_written) {
1877 while (rc == -EAGAIN) {
1881 if (open_file->invalidHandle) {
1882 /* we could deadlock if we called
1883 filemap_fdatawait from here so tell
1884 reopen_file not to flush data to
1886 rc = cifs_reopen_file(open_file, false);
1891 len = min(server->ops->wp_retry_size(d_inode(dentry)),
1892 (unsigned int)write_size - total_written);
1893 /* iov[0] is reserved for smb header */
1894 iov[1].iov_base = (char *)write_data + total_written;
1895 iov[1].iov_len = len;
1897 io_parms.tcon = tcon;
1898 io_parms.offset = *offset;
1899 io_parms.length = len;
1900 rc = server->ops->sync_write(xid, &open_file->fid,
1901 &io_parms, &bytes_written, iov, 1);
1903 if (rc || (bytes_written == 0)) {
1911 spin_lock(&d_inode(dentry)->i_lock);
1912 cifs_update_eof(cifsi, *offset, bytes_written);
1913 spin_unlock(&d_inode(dentry)->i_lock);
1914 *offset += bytes_written;
1918 cifs_stats_bytes_written(tcon, total_written);
1920 if (total_written > 0) {
1921 spin_lock(&d_inode(dentry)->i_lock);
1922 if (*offset > d_inode(dentry)->i_size)
1923 i_size_write(d_inode(dentry), *offset);
1924 spin_unlock(&d_inode(dentry)->i_lock);
1926 mark_inode_dirty_sync(d_inode(dentry));
1928 return total_written;
1931 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1934 struct cifsFileInfo *open_file = NULL;
1935 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1937 /* only filter by fsuid on multiuser mounts */
1938 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1941 spin_lock(&cifs_inode->open_file_lock);
1942 /* we could simply get the first_list_entry since write-only entries
1943 are always at the end of the list but since the first entry might
1944 have a close pending, we go through the whole list */
1945 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1946 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1948 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
1949 if (!open_file->invalidHandle) {
1950 /* found a good file */
1951 /* lock it so it will not be closed on us */
1952 cifsFileInfo_get(open_file);
1953 spin_unlock(&cifs_inode->open_file_lock);
1955 } /* else might as well continue, and look for
1956 another, or simply have the caller reopen it
1957 again rather than trying to fix this handle */
1958 } else /* write only file */
1959 break; /* write only files are last so must be done */
1961 spin_unlock(&cifs_inode->open_file_lock);
1965 /* Return -EBADF if no handle is found and general rc otherwise */
1967 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, bool fsuid_only,
1968 struct cifsFileInfo **ret_file)
1970 struct cifsFileInfo *open_file, *inv_file = NULL;
1971 struct cifs_sb_info *cifs_sb;
1972 bool any_available = false;
1974 unsigned int refind = 0;
1979 * Having a null inode here (because mapping->host was set to zero by
1980 * the VFS or MM) should not happen but we had reports of on oops (due
1981 * to it being zero) during stress testcases so we need to check for it
1984 if (cifs_inode == NULL) {
1985 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
1990 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1992 /* only filter by fsuid on multiuser mounts */
1993 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1996 spin_lock(&cifs_inode->open_file_lock);
1998 if (refind > MAX_REOPEN_ATT) {
1999 spin_unlock(&cifs_inode->open_file_lock);
2002 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2003 if (!any_available && open_file->pid != current->tgid)
2005 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2007 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2008 if (!open_file->invalidHandle) {
2009 /* found a good writable file */
2010 cifsFileInfo_get(open_file);
2011 spin_unlock(&cifs_inode->open_file_lock);
2012 *ret_file = open_file;
2016 inv_file = open_file;
2020 /* couldn't find useable FH with same pid, try any available */
2021 if (!any_available) {
2022 any_available = true;
2023 goto refind_writable;
2027 any_available = false;
2028 cifsFileInfo_get(inv_file);
2031 spin_unlock(&cifs_inode->open_file_lock);
2034 rc = cifs_reopen_file(inv_file, false);
2036 *ret_file = inv_file;
2040 spin_lock(&cifs_inode->open_file_lock);
2041 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2042 spin_unlock(&cifs_inode->open_file_lock);
2043 cifsFileInfo_put(inv_file);
2046 spin_lock(&cifs_inode->open_file_lock);
2047 goto refind_writable;
2053 struct cifsFileInfo *
2054 find_writable_file(struct cifsInodeInfo *cifs_inode, bool fsuid_only)
2056 struct cifsFileInfo *cfile;
2059 rc = cifs_get_writable_file(cifs_inode, fsuid_only, &cfile);
2061 cifs_dbg(FYI, "couldn't find writable handle rc=%d", rc);
2067 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2068 struct cifsFileInfo **ret_file)
2070 struct list_head *tmp;
2071 struct cifsFileInfo *cfile;
2072 struct cifsInodeInfo *cinode;
2077 spin_lock(&tcon->open_file_lock);
2078 list_for_each(tmp, &tcon->openFileList) {
2079 cfile = list_entry(tmp, struct cifsFileInfo,
2081 full_path = build_path_from_dentry(cfile->dentry);
2082 if (full_path == NULL) {
2083 spin_unlock(&tcon->open_file_lock);
2086 if (strcmp(full_path, name)) {
2092 cinode = CIFS_I(d_inode(cfile->dentry));
2093 spin_unlock(&tcon->open_file_lock);
2094 return cifs_get_writable_file(cinode, 0, ret_file);
2097 spin_unlock(&tcon->open_file_lock);
2102 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2103 struct cifsFileInfo **ret_file)
2105 struct list_head *tmp;
2106 struct cifsFileInfo *cfile;
2107 struct cifsInodeInfo *cinode;
2112 spin_lock(&tcon->open_file_lock);
2113 list_for_each(tmp, &tcon->openFileList) {
2114 cfile = list_entry(tmp, struct cifsFileInfo,
2116 full_path = build_path_from_dentry(cfile->dentry);
2117 if (full_path == NULL) {
2118 spin_unlock(&tcon->open_file_lock);
2121 if (strcmp(full_path, name)) {
2127 cinode = CIFS_I(d_inode(cfile->dentry));
2128 spin_unlock(&tcon->open_file_lock);
2129 *ret_file = find_readable_file(cinode, 0);
2130 return *ret_file ? 0 : -ENOENT;
2133 spin_unlock(&tcon->open_file_lock);
2137 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2139 struct address_space *mapping = page->mapping;
2140 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2143 int bytes_written = 0;
2144 struct inode *inode;
2145 struct cifsFileInfo *open_file;
2147 if (!mapping || !mapping->host)
2150 inode = page->mapping->host;
2152 offset += (loff_t)from;
2153 write_data = kmap(page);
2156 if ((to > PAGE_SIZE) || (from > to)) {
2161 /* racing with truncate? */
2162 if (offset > mapping->host->i_size) {
2164 return 0; /* don't care */
2167 /* check to make sure that we are not extending the file */
2168 if (mapping->host->i_size - offset < (loff_t)to)
2169 to = (unsigned)(mapping->host->i_size - offset);
2171 rc = cifs_get_writable_file(CIFS_I(mapping->host), false, &open_file);
2173 bytes_written = cifs_write(open_file, open_file->pid,
2174 write_data, to - from, &offset);
2175 cifsFileInfo_put(open_file);
2176 /* Does mm or vfs already set times? */
2177 inode->i_atime = inode->i_mtime = current_time(inode);
2178 if ((bytes_written > 0) && (offset))
2180 else if (bytes_written < 0)
2185 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2186 if (!is_retryable_error(rc))
2194 static struct cifs_writedata *
2195 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2196 pgoff_t end, pgoff_t *index,
2197 unsigned int *found_pages)
2199 struct cifs_writedata *wdata;
2201 wdata = cifs_writedata_alloc((unsigned int)tofind,
2202 cifs_writev_complete);
2206 *found_pages = find_get_pages_range_tag(mapping, index, end,
2207 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2212 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2213 struct address_space *mapping,
2214 struct writeback_control *wbc,
2215 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2217 unsigned int nr_pages = 0, i;
2220 for (i = 0; i < found_pages; i++) {
2221 page = wdata->pages[i];
2223 * At this point we hold neither the i_pages lock nor the
2224 * page lock: the page may be truncated or invalidated
2225 * (changing page->mapping to NULL), or even swizzled
2226 * back from swapper_space to tmpfs file mapping
2231 else if (!trylock_page(page))
2234 if (unlikely(page->mapping != mapping)) {
2239 if (!wbc->range_cyclic && page->index > end) {
2245 if (*next && (page->index != *next)) {
2246 /* Not next consecutive page */
2251 if (wbc->sync_mode != WB_SYNC_NONE)
2252 wait_on_page_writeback(page);
2254 if (PageWriteback(page) ||
2255 !clear_page_dirty_for_io(page)) {
2261 * This actually clears the dirty bit in the radix tree.
2262 * See cifs_writepage() for more commentary.
2264 set_page_writeback(page);
2265 if (page_offset(page) >= i_size_read(mapping->host)) {
2268 end_page_writeback(page);
2272 wdata->pages[i] = page;
2273 *next = page->index + 1;
2277 /* reset index to refind any pages skipped */
2279 *index = wdata->pages[0]->index + 1;
2281 /* put any pages we aren't going to use */
2282 for (i = nr_pages; i < found_pages; i++) {
2283 put_page(wdata->pages[i]);
2284 wdata->pages[i] = NULL;
2291 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2292 struct address_space *mapping, struct writeback_control *wbc)
2295 struct TCP_Server_Info *server =
2296 tlink_tcon(wdata->cfile->tlink)->ses->server;
2298 wdata->sync_mode = wbc->sync_mode;
2299 wdata->nr_pages = nr_pages;
2300 wdata->offset = page_offset(wdata->pages[0]);
2301 wdata->pagesz = PAGE_SIZE;
2302 wdata->tailsz = min(i_size_read(mapping->host) -
2303 page_offset(wdata->pages[nr_pages - 1]),
2305 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2306 wdata->pid = wdata->cfile->pid;
2308 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2312 if (wdata->cfile->invalidHandle)
2315 rc = server->ops->async_writev(wdata, cifs_writedata_release);
2320 static int cifs_writepages(struct address_space *mapping,
2321 struct writeback_control *wbc)
2323 struct inode *inode = mapping->host;
2324 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2325 struct TCP_Server_Info *server;
2326 bool done = false, scanned = false, range_whole = false;
2328 struct cifs_writedata *wdata;
2329 struct cifsFileInfo *cfile = NULL;
2335 * If wsize is smaller than the page cache size, default to writing
2336 * one page at a time via cifs_writepage
2338 if (cifs_sb->wsize < PAGE_SIZE)
2339 return generic_writepages(mapping, wbc);
2342 if (wbc->range_cyclic) {
2343 index = mapping->writeback_index; /* Start from prev offset */
2346 index = wbc->range_start >> PAGE_SHIFT;
2347 end = wbc->range_end >> PAGE_SHIFT;
2348 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2352 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
2354 while (!done && index <= end) {
2355 unsigned int i, nr_pages, found_pages, wsize;
2356 pgoff_t next = 0, tofind, saved_index = index;
2357 struct cifs_credits credits_on_stack;
2358 struct cifs_credits *credits = &credits_on_stack;
2359 int get_file_rc = 0;
2362 cifsFileInfo_put(cfile);
2364 rc = cifs_get_writable_file(CIFS_I(inode), false, &cfile);
2366 /* in case of an error store it to return later */
2370 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2377 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2379 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2384 add_credits_and_wake_if(server, credits, 0);
2388 if (found_pages == 0) {
2389 kref_put(&wdata->refcount, cifs_writedata_release);
2390 add_credits_and_wake_if(server, credits, 0);
2394 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2395 end, &index, &next, &done);
2397 /* nothing to write? */
2398 if (nr_pages == 0) {
2399 kref_put(&wdata->refcount, cifs_writedata_release);
2400 add_credits_and_wake_if(server, credits, 0);
2404 wdata->credits = credits_on_stack;
2405 wdata->cfile = cfile;
2408 if (!wdata->cfile) {
2409 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2411 if (is_retryable_error(get_file_rc))
2416 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2418 for (i = 0; i < nr_pages; ++i)
2419 unlock_page(wdata->pages[i]);
2421 /* send failure -- clean up the mess */
2423 add_credits_and_wake_if(server, &wdata->credits, 0);
2424 for (i = 0; i < nr_pages; ++i) {
2425 if (is_retryable_error(rc))
2426 redirty_page_for_writepage(wbc,
2429 SetPageError(wdata->pages[i]);
2430 end_page_writeback(wdata->pages[i]);
2431 put_page(wdata->pages[i]);
2433 if (!is_retryable_error(rc))
2434 mapping_set_error(mapping, rc);
2436 kref_put(&wdata->refcount, cifs_writedata_release);
2438 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2439 index = saved_index;
2443 /* Return immediately if we received a signal during writing */
2444 if (is_interrupt_error(rc)) {
2449 if (rc != 0 && saved_rc == 0)
2452 wbc->nr_to_write -= nr_pages;
2453 if (wbc->nr_to_write <= 0)
2459 if (!scanned && !done) {
2461 * We hit the last page and there is more work to be done: wrap
2462 * back to the start of the file
2472 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2473 mapping->writeback_index = index;
2476 cifsFileInfo_put(cfile);
2482 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2488 /* BB add check for wbc flags */
2490 if (!PageUptodate(page))
2491 cifs_dbg(FYI, "ppw - page not up to date\n");
2494 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2496 * A writepage() implementation always needs to do either this,
2497 * or re-dirty the page with "redirty_page_for_writepage()" in
2498 * the case of a failure.
2500 * Just unlocking the page will cause the radix tree tag-bits
2501 * to fail to update with the state of the page correctly.
2503 set_page_writeback(page);
2505 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2506 if (is_retryable_error(rc)) {
2507 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2509 redirty_page_for_writepage(wbc, page);
2510 } else if (rc != 0) {
2512 mapping_set_error(page->mapping, rc);
2514 SetPageUptodate(page);
2516 end_page_writeback(page);
2522 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2524 int rc = cifs_writepage_locked(page, wbc);
2529 static int cifs_write_end(struct file *file, struct address_space *mapping,
2530 loff_t pos, unsigned len, unsigned copied,
2531 struct page *page, void *fsdata)
2534 struct inode *inode = mapping->host;
2535 struct cifsFileInfo *cfile = file->private_data;
2536 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2539 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2542 pid = current->tgid;
2544 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2547 if (PageChecked(page)) {
2549 SetPageUptodate(page);
2550 ClearPageChecked(page);
2551 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2552 SetPageUptodate(page);
2554 if (!PageUptodate(page)) {
2556 unsigned offset = pos & (PAGE_SIZE - 1);
2560 /* this is probably better than directly calling
2561 partialpage_write since in this function the file handle is
2562 known which we might as well leverage */
2563 /* BB check if anything else missing out of ppw
2564 such as updating last write time */
2565 page_data = kmap(page);
2566 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2567 /* if (rc < 0) should we set writebehind rc? */
2574 set_page_dirty(page);
2578 spin_lock(&inode->i_lock);
2579 if (pos > inode->i_size)
2580 i_size_write(inode, pos);
2581 spin_unlock(&inode->i_lock);
2590 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2595 struct cifs_tcon *tcon;
2596 struct TCP_Server_Info *server;
2597 struct cifsFileInfo *smbfile = file->private_data;
2598 struct inode *inode = file_inode(file);
2599 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2601 rc = file_write_and_wait_range(file, start, end);
2607 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2610 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2611 rc = cifs_zap_mapping(inode);
2613 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2614 rc = 0; /* don't care about it in fsync */
2618 tcon = tlink_tcon(smbfile->tlink);
2619 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2620 server = tcon->ses->server;
2621 if (server->ops->flush)
2622 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2631 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2635 struct cifs_tcon *tcon;
2636 struct TCP_Server_Info *server;
2637 struct cifsFileInfo *smbfile = file->private_data;
2638 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2640 rc = file_write_and_wait_range(file, start, end);
2646 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2649 tcon = tlink_tcon(smbfile->tlink);
2650 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2651 server = tcon->ses->server;
2652 if (server->ops->flush)
2653 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2663 * As file closes, flush all cached write data for this inode checking
2664 * for write behind errors.
2666 int cifs_flush(struct file *file, fl_owner_t id)
2668 struct inode *inode = file_inode(file);
2671 if (file->f_mode & FMODE_WRITE)
2672 rc = filemap_write_and_wait(inode->i_mapping);
2674 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2680 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2685 for (i = 0; i < num_pages; i++) {
2686 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2689 * save number of pages we have already allocated and
2690 * return with ENOMEM error
2699 for (i = 0; i < num_pages; i++)
2706 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2711 clen = min_t(const size_t, len, wsize);
2712 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
2721 cifs_uncached_writedata_release(struct kref *refcount)
2724 struct cifs_writedata *wdata = container_of(refcount,
2725 struct cifs_writedata, refcount);
2727 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
2728 for (i = 0; i < wdata->nr_pages; i++)
2729 put_page(wdata->pages[i]);
2730 cifs_writedata_release(refcount);
2733 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2736 cifs_uncached_writev_complete(struct work_struct *work)
2738 struct cifs_writedata *wdata = container_of(work,
2739 struct cifs_writedata, work);
2740 struct inode *inode = d_inode(wdata->cfile->dentry);
2741 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2743 spin_lock(&inode->i_lock);
2744 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2745 if (cifsi->server_eof > inode->i_size)
2746 i_size_write(inode, cifsi->server_eof);
2747 spin_unlock(&inode->i_lock);
2749 complete(&wdata->done);
2750 collect_uncached_write_data(wdata->ctx);
2751 /* the below call can possibly free the last ref to aio ctx */
2752 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2756 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2757 size_t *len, unsigned long *num_pages)
2759 size_t save_len, copied, bytes, cur_len = *len;
2760 unsigned long i, nr_pages = *num_pages;
2763 for (i = 0; i < nr_pages; i++) {
2764 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2765 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2768 * If we didn't copy as much as we expected, then that
2769 * may mean we trod into an unmapped area. Stop copying
2770 * at that point. On the next pass through the big
2771 * loop, we'll likely end up getting a zero-length
2772 * write and bailing out of it.
2777 cur_len = save_len - cur_len;
2781 * If we have no data to send, then that probably means that
2782 * the copy above failed altogether. That's most likely because
2783 * the address in the iovec was bogus. Return -EFAULT and let
2784 * the caller free anything we allocated and bail out.
2790 * i + 1 now represents the number of pages we actually used in
2791 * the copy phase above.
2798 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
2799 struct cifs_aio_ctx *ctx)
2802 struct cifs_credits credits;
2804 struct TCP_Server_Info *server =
2805 tlink_tcon(wdata->cfile->tlink)->ses->server;
2808 if (wdata->cfile->invalidHandle) {
2809 rc = cifs_reopen_file(wdata->cfile, false);
2818 * Wait for credits to resend this wdata.
2819 * Note: we are attempting to resend the whole wdata not in
2823 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
2828 if (wsize < wdata->bytes) {
2829 add_credits_and_wake_if(server, &credits, 0);
2832 } while (wsize < wdata->bytes);
2833 wdata->credits = credits;
2835 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2838 if (wdata->cfile->invalidHandle)
2841 #ifdef CONFIG_CIFS_SMB_DIRECT
2843 wdata->mr->need_invalidate = true;
2844 smbd_deregister_mr(wdata->mr);
2848 rc = server->ops->async_writev(wdata,
2849 cifs_uncached_writedata_release);
2853 /* If the write was successfully sent, we are done */
2855 list_add_tail(&wdata->list, wdata_list);
2859 /* Roll back credits and retry if needed */
2860 add_credits_and_wake_if(server, &wdata->credits, 0);
2861 } while (rc == -EAGAIN);
2864 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2869 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2870 struct cifsFileInfo *open_file,
2871 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2872 struct cifs_aio_ctx *ctx)
2876 unsigned long nr_pages, num_pages, i;
2877 struct cifs_writedata *wdata;
2878 struct iov_iter saved_from = *from;
2879 loff_t saved_offset = offset;
2881 struct TCP_Server_Info *server;
2882 struct page **pagevec;
2886 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2887 pid = open_file->pid;
2889 pid = current->tgid;
2891 server = tlink_tcon(open_file->tlink)->ses->server;
2896 struct cifs_credits credits_on_stack;
2897 struct cifs_credits *credits = &credits_on_stack;
2899 if (open_file->invalidHandle) {
2900 rc = cifs_reopen_file(open_file, false);
2907 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2912 cur_len = min_t(const size_t, len, wsize);
2914 if (ctx->direct_io) {
2917 result = iov_iter_get_pages_alloc(
2918 from, &pagevec, cur_len, &start);
2921 "direct_writev couldn't get user pages "
2922 "(rc=%zd) iter type %d iov_offset %zd "
2925 from->iov_offset, from->count);
2929 add_credits_and_wake_if(server, credits, 0);
2932 cur_len = (size_t)result;
2933 iov_iter_advance(from, cur_len);
2936 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
2938 wdata = cifs_writedata_direct_alloc(pagevec,
2939 cifs_uncached_writev_complete);
2942 add_credits_and_wake_if(server, credits, 0);
2947 wdata->page_offset = start;
2950 cur_len - (PAGE_SIZE - start) -
2951 (nr_pages - 2) * PAGE_SIZE :
2954 nr_pages = get_numpages(wsize, len, &cur_len);
2955 wdata = cifs_writedata_alloc(nr_pages,
2956 cifs_uncached_writev_complete);
2959 add_credits_and_wake_if(server, credits, 0);
2963 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2965 kvfree(wdata->pages);
2967 add_credits_and_wake_if(server, credits, 0);
2971 num_pages = nr_pages;
2972 rc = wdata_fill_from_iovec(
2973 wdata, from, &cur_len, &num_pages);
2975 for (i = 0; i < nr_pages; i++)
2976 put_page(wdata->pages[i]);
2977 kvfree(wdata->pages);
2979 add_credits_and_wake_if(server, credits, 0);
2984 * Bring nr_pages down to the number of pages we
2985 * actually used, and free any pages that we didn't use.
2987 for ( ; nr_pages > num_pages; nr_pages--)
2988 put_page(wdata->pages[nr_pages - 1]);
2990 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
2993 wdata->sync_mode = WB_SYNC_ALL;
2994 wdata->nr_pages = nr_pages;
2995 wdata->offset = (__u64)offset;
2996 wdata->cfile = cifsFileInfo_get(open_file);
2998 wdata->bytes = cur_len;
2999 wdata->pagesz = PAGE_SIZE;
3000 wdata->credits = credits_on_stack;
3002 kref_get(&ctx->refcount);
3004 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3007 if (wdata->cfile->invalidHandle)
3010 rc = server->ops->async_writev(wdata,
3011 cifs_uncached_writedata_release);
3015 add_credits_and_wake_if(server, &wdata->credits, 0);
3016 kref_put(&wdata->refcount,
3017 cifs_uncached_writedata_release);
3018 if (rc == -EAGAIN) {
3020 iov_iter_advance(from, offset - saved_offset);
3026 list_add_tail(&wdata->list, wdata_list);
3035 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3037 struct cifs_writedata *wdata, *tmp;
3038 struct cifs_tcon *tcon;
3039 struct cifs_sb_info *cifs_sb;
3040 struct dentry *dentry = ctx->cfile->dentry;
3043 tcon = tlink_tcon(ctx->cfile->tlink);
3044 cifs_sb = CIFS_SB(dentry->d_sb);
3046 mutex_lock(&ctx->aio_mutex);
3048 if (list_empty(&ctx->list)) {
3049 mutex_unlock(&ctx->aio_mutex);
3055 * Wait for and collect replies for any successful sends in order of
3056 * increasing offset. Once an error is hit, then return without waiting
3057 * for any more replies.
3060 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3062 if (!try_wait_for_completion(&wdata->done)) {
3063 mutex_unlock(&ctx->aio_mutex);
3070 ctx->total_len += wdata->bytes;
3072 /* resend call if it's a retryable error */
3073 if (rc == -EAGAIN) {
3074 struct list_head tmp_list;
3075 struct iov_iter tmp_from = ctx->iter;
3077 INIT_LIST_HEAD(&tmp_list);
3078 list_del_init(&wdata->list);
3081 rc = cifs_resend_wdata(
3082 wdata, &tmp_list, ctx);
3084 iov_iter_advance(&tmp_from,
3085 wdata->offset - ctx->pos);
3087 rc = cifs_write_from_iter(wdata->offset,
3088 wdata->bytes, &tmp_from,
3089 ctx->cfile, cifs_sb, &tmp_list,
3092 kref_put(&wdata->refcount,
3093 cifs_uncached_writedata_release);
3096 list_splice(&tmp_list, &ctx->list);
3100 list_del_init(&wdata->list);
3101 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3104 cifs_stats_bytes_written(tcon, ctx->total_len);
3105 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3107 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3109 mutex_unlock(&ctx->aio_mutex);
3111 if (ctx->iocb && ctx->iocb->ki_complete)
3112 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3114 complete(&ctx->done);
3117 static ssize_t __cifs_writev(
3118 struct kiocb *iocb, struct iov_iter *from, bool direct)
3120 struct file *file = iocb->ki_filp;
3121 ssize_t total_written = 0;
3122 struct cifsFileInfo *cfile;
3123 struct cifs_tcon *tcon;
3124 struct cifs_sb_info *cifs_sb;
3125 struct cifs_aio_ctx *ctx;
3126 struct iov_iter saved_from = *from;
3127 size_t len = iov_iter_count(from);
3131 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3132 * In this case, fall back to non-direct write function.
3133 * this could be improved by getting pages directly in ITER_KVEC
3135 if (direct && from->type & ITER_KVEC) {
3136 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3140 rc = generic_write_checks(iocb, from);
3144 cifs_sb = CIFS_FILE_SB(file);
3145 cfile = file->private_data;
3146 tcon = tlink_tcon(cfile->tlink);
3148 if (!tcon->ses->server->ops->async_writev)
3151 ctx = cifs_aio_ctx_alloc();
3155 ctx->cfile = cifsFileInfo_get(cfile);
3157 if (!is_sync_kiocb(iocb))
3160 ctx->pos = iocb->ki_pos;
3163 ctx->direct_io = true;
3167 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3169 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3174 /* grab a lock here due to read response handlers can access ctx */
3175 mutex_lock(&ctx->aio_mutex);
3177 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3178 cfile, cifs_sb, &ctx->list, ctx);
3181 * If at least one write was successfully sent, then discard any rc
3182 * value from the later writes. If the other write succeeds, then
3183 * we'll end up returning whatever was written. If it fails, then
3184 * we'll get a new rc value from that.
3186 if (!list_empty(&ctx->list))
3189 mutex_unlock(&ctx->aio_mutex);
3192 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3196 if (!is_sync_kiocb(iocb)) {
3197 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3198 return -EIOCBQUEUED;
3201 rc = wait_for_completion_killable(&ctx->done);
3203 mutex_lock(&ctx->aio_mutex);
3204 ctx->rc = rc = -EINTR;
3205 total_written = ctx->total_len;
3206 mutex_unlock(&ctx->aio_mutex);
3209 total_written = ctx->total_len;
3212 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3214 if (unlikely(!total_written))
3217 iocb->ki_pos += total_written;
3218 return total_written;
3221 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3223 return __cifs_writev(iocb, from, true);
3226 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3228 return __cifs_writev(iocb, from, false);
3232 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3234 struct file *file = iocb->ki_filp;
3235 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3236 struct inode *inode = file->f_mapping->host;
3237 struct cifsInodeInfo *cinode = CIFS_I(inode);
3238 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3243 * We need to hold the sem to be sure nobody modifies lock list
3244 * with a brlock that prevents writing.
3246 down_read(&cinode->lock_sem);
3248 rc = generic_write_checks(iocb, from);
3252 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3253 server->vals->exclusive_lock_type, 0,
3254 NULL, CIFS_WRITE_OP))
3255 rc = __generic_file_write_iter(iocb, from);
3259 up_read(&cinode->lock_sem);
3260 inode_unlock(inode);
3263 rc = generic_write_sync(iocb, rc);
3268 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3270 struct inode *inode = file_inode(iocb->ki_filp);
3271 struct cifsInodeInfo *cinode = CIFS_I(inode);
3272 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3273 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3274 iocb->ki_filp->private_data;
3275 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3278 written = cifs_get_writer(cinode);
3282 if (CIFS_CACHE_WRITE(cinode)) {
3283 if (cap_unix(tcon->ses) &&
3284 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3285 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3286 written = generic_file_write_iter(iocb, from);
3289 written = cifs_writev(iocb, from);
3293 * For non-oplocked files in strict cache mode we need to write the data
3294 * to the server exactly from the pos to pos+len-1 rather than flush all
3295 * affected pages because it may cause a error with mandatory locks on
3296 * these pages but not on the region from pos to ppos+len-1.
3298 written = cifs_user_writev(iocb, from);
3299 if (CIFS_CACHE_READ(cinode)) {
3301 * We have read level caching and we have just sent a write
3302 * request to the server thus making data in the cache stale.
3303 * Zap the cache and set oplock/lease level to NONE to avoid
3304 * reading stale data from the cache. All subsequent read
3305 * operations will read new data from the server.
3307 cifs_zap_mapping(inode);
3308 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3313 cifs_put_writer(cinode);
3317 static struct cifs_readdata *
3318 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3320 struct cifs_readdata *rdata;
3322 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3323 if (rdata != NULL) {
3324 rdata->pages = pages;
3325 kref_init(&rdata->refcount);
3326 INIT_LIST_HEAD(&rdata->list);
3327 init_completion(&rdata->done);
3328 INIT_WORK(&rdata->work, complete);
3334 static struct cifs_readdata *
3335 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3337 struct page **pages =
3338 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3339 struct cifs_readdata *ret = NULL;
3342 ret = cifs_readdata_direct_alloc(pages, complete);
3351 cifs_readdata_release(struct kref *refcount)
3353 struct cifs_readdata *rdata = container_of(refcount,
3354 struct cifs_readdata, refcount);
3355 #ifdef CONFIG_CIFS_SMB_DIRECT
3357 smbd_deregister_mr(rdata->mr);
3362 cifsFileInfo_put(rdata->cfile);
3364 kvfree(rdata->pages);
3369 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3375 for (i = 0; i < nr_pages; i++) {
3376 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3381 rdata->pages[i] = page;
3385 unsigned int nr_page_failed = i;
3387 for (i = 0; i < nr_page_failed; i++) {
3388 put_page(rdata->pages[i]);
3389 rdata->pages[i] = NULL;
3396 cifs_uncached_readdata_release(struct kref *refcount)
3398 struct cifs_readdata *rdata = container_of(refcount,
3399 struct cifs_readdata, refcount);
3402 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3403 for (i = 0; i < rdata->nr_pages; i++) {
3404 put_page(rdata->pages[i]);
3406 cifs_readdata_release(refcount);
3410 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3411 * @rdata: the readdata response with list of pages holding data
3412 * @iter: destination for our data
3414 * This function copies data from a list of pages in a readdata response into
3415 * an array of iovecs. It will first calculate where the data should go
3416 * based on the info in the readdata and then copy the data into that spot.
3419 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3421 size_t remaining = rdata->got_bytes;
3424 for (i = 0; i < rdata->nr_pages; i++) {
3425 struct page *page = rdata->pages[i];
3426 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3429 if (unlikely(iov_iter_is_pipe(iter))) {
3430 void *addr = kmap_atomic(page);
3432 written = copy_to_iter(addr, copy, iter);
3433 kunmap_atomic(addr);
3435 written = copy_page_to_iter(page, 0, copy, iter);
3436 remaining -= written;
3437 if (written < copy && iov_iter_count(iter) > 0)
3440 return remaining ? -EFAULT : 0;
3443 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3446 cifs_uncached_readv_complete(struct work_struct *work)
3448 struct cifs_readdata *rdata = container_of(work,
3449 struct cifs_readdata, work);
3451 complete(&rdata->done);
3452 collect_uncached_read_data(rdata->ctx);
3453 /* the below call can possibly free the last ref to aio ctx */
3454 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3458 uncached_fill_pages(struct TCP_Server_Info *server,
3459 struct cifs_readdata *rdata, struct iov_iter *iter,
3464 unsigned int nr_pages = rdata->nr_pages;
3465 unsigned int page_offset = rdata->page_offset;
3467 rdata->got_bytes = 0;
3468 rdata->tailsz = PAGE_SIZE;
3469 for (i = 0; i < nr_pages; i++) {
3470 struct page *page = rdata->pages[i];
3472 unsigned int segment_size = rdata->pagesz;
3475 segment_size -= page_offset;
3481 /* no need to hold page hostage */
3482 rdata->pages[i] = NULL;
3489 if (len >= segment_size)
3490 /* enough data to fill the page */
3493 rdata->tailsz = len;
3497 result = copy_page_from_iter(
3498 page, page_offset, n, iter);
3499 #ifdef CONFIG_CIFS_SMB_DIRECT
3504 result = cifs_read_page_from_socket(
3505 server, page, page_offset, n);
3509 rdata->got_bytes += result;
3512 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3513 rdata->got_bytes : result;
3517 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3518 struct cifs_readdata *rdata, unsigned int len)
3520 return uncached_fill_pages(server, rdata, NULL, len);
3524 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3525 struct cifs_readdata *rdata,
3526 struct iov_iter *iter)
3528 return uncached_fill_pages(server, rdata, iter, iter->count);
3531 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3532 struct list_head *rdata_list,
3533 struct cifs_aio_ctx *ctx)
3536 struct cifs_credits credits;
3538 struct TCP_Server_Info *server =
3539 tlink_tcon(rdata->cfile->tlink)->ses->server;
3542 if (rdata->cfile->invalidHandle) {
3543 rc = cifs_reopen_file(rdata->cfile, true);
3551 * Wait for credits to resend this rdata.
3552 * Note: we are attempting to resend the whole rdata not in
3556 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3562 if (rsize < rdata->bytes) {
3563 add_credits_and_wake_if(server, &credits, 0);
3566 } while (rsize < rdata->bytes);
3567 rdata->credits = credits;
3569 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3571 if (rdata->cfile->invalidHandle)
3574 #ifdef CONFIG_CIFS_SMB_DIRECT
3576 rdata->mr->need_invalidate = true;
3577 smbd_deregister_mr(rdata->mr);
3581 rc = server->ops->async_readv(rdata);
3585 /* If the read was successfully sent, we are done */
3587 /* Add to aio pending list */
3588 list_add_tail(&rdata->list, rdata_list);
3592 /* Roll back credits and retry if needed */
3593 add_credits_and_wake_if(server, &rdata->credits, 0);
3594 } while (rc == -EAGAIN);
3597 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3602 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3603 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3604 struct cifs_aio_ctx *ctx)
3606 struct cifs_readdata *rdata;
3607 unsigned int npages, rsize;
3608 struct cifs_credits credits_on_stack;
3609 struct cifs_credits *credits = &credits_on_stack;
3613 struct TCP_Server_Info *server;
3614 struct page **pagevec;
3616 struct iov_iter direct_iov = ctx->iter;
3618 server = tlink_tcon(open_file->tlink)->ses->server;
3620 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3621 pid = open_file->pid;
3623 pid = current->tgid;
3626 iov_iter_advance(&direct_iov, offset - ctx->pos);
3629 if (open_file->invalidHandle) {
3630 rc = cifs_reopen_file(open_file, true);
3637 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
3642 cur_len = min_t(const size_t, len, rsize);
3644 if (ctx->direct_io) {
3647 result = iov_iter_get_pages_alloc(
3648 &direct_iov, &pagevec,
3652 "couldn't get user pages (rc=%zd)"
3654 " iov_offset %zd count %zd\n",
3655 result, direct_iov.type,
3656 direct_iov.iov_offset,
3661 add_credits_and_wake_if(server, credits, 0);
3664 cur_len = (size_t)result;
3665 iov_iter_advance(&direct_iov, cur_len);
3667 rdata = cifs_readdata_direct_alloc(
3668 pagevec, cifs_uncached_readv_complete);
3670 add_credits_and_wake_if(server, credits, 0);
3675 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
3676 rdata->page_offset = start;
3677 rdata->tailsz = npages > 1 ?
3678 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
3683 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
3684 /* allocate a readdata struct */
3685 rdata = cifs_readdata_alloc(npages,
3686 cifs_uncached_readv_complete);
3688 add_credits_and_wake_if(server, credits, 0);
3693 rc = cifs_read_allocate_pages(rdata, npages);
3695 kvfree(rdata->pages);
3697 add_credits_and_wake_if(server, credits, 0);
3701 rdata->tailsz = PAGE_SIZE;
3704 rdata->cfile = cifsFileInfo_get(open_file);
3705 rdata->nr_pages = npages;
3706 rdata->offset = offset;
3707 rdata->bytes = cur_len;
3709 rdata->pagesz = PAGE_SIZE;
3710 rdata->read_into_pages = cifs_uncached_read_into_pages;
3711 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
3712 rdata->credits = credits_on_stack;
3714 kref_get(&ctx->refcount);
3716 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3719 if (rdata->cfile->invalidHandle)
3722 rc = server->ops->async_readv(rdata);
3726 add_credits_and_wake_if(server, &rdata->credits, 0);
3727 kref_put(&rdata->refcount,
3728 cifs_uncached_readdata_release);
3729 if (rc == -EAGAIN) {
3730 iov_iter_revert(&direct_iov, cur_len);
3736 list_add_tail(&rdata->list, rdata_list);
3745 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3747 struct cifs_readdata *rdata, *tmp;
3748 struct iov_iter *to = &ctx->iter;
3749 struct cifs_sb_info *cifs_sb;
3752 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
3754 mutex_lock(&ctx->aio_mutex);
3756 if (list_empty(&ctx->list)) {
3757 mutex_unlock(&ctx->aio_mutex);
3762 /* the loop below should proceed in the order of increasing offsets */
3764 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
3766 if (!try_wait_for_completion(&rdata->done)) {
3767 mutex_unlock(&ctx->aio_mutex);
3771 if (rdata->result == -EAGAIN) {
3772 /* resend call if it's a retryable error */
3773 struct list_head tmp_list;
3774 unsigned int got_bytes = rdata->got_bytes;
3776 list_del_init(&rdata->list);
3777 INIT_LIST_HEAD(&tmp_list);
3780 * Got a part of data and then reconnect has
3781 * happened -- fill the buffer and continue
3784 if (got_bytes && got_bytes < rdata->bytes) {
3786 if (!ctx->direct_io)
3787 rc = cifs_readdata_to_iov(rdata, to);
3789 kref_put(&rdata->refcount,
3790 cifs_uncached_readdata_release);
3795 if (ctx->direct_io) {
3797 * Re-use rdata as this is a
3800 rc = cifs_resend_rdata(
3804 rc = cifs_send_async_read(
3805 rdata->offset + got_bytes,
3806 rdata->bytes - got_bytes,
3807 rdata->cfile, cifs_sb,
3810 kref_put(&rdata->refcount,
3811 cifs_uncached_readdata_release);
3814 list_splice(&tmp_list, &ctx->list);
3817 } else if (rdata->result)
3819 else if (!ctx->direct_io)
3820 rc = cifs_readdata_to_iov(rdata, to);
3822 /* if there was a short read -- discard anything left */
3823 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3826 ctx->total_len += rdata->got_bytes;
3828 list_del_init(&rdata->list);
3829 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3832 if (!ctx->direct_io)
3833 ctx->total_len = ctx->len - iov_iter_count(to);
3835 /* mask nodata case */
3839 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3841 mutex_unlock(&ctx->aio_mutex);
3843 if (ctx->iocb && ctx->iocb->ki_complete)
3844 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3846 complete(&ctx->done);
3849 static ssize_t __cifs_readv(
3850 struct kiocb *iocb, struct iov_iter *to, bool direct)
3853 struct file *file = iocb->ki_filp;
3854 struct cifs_sb_info *cifs_sb;
3855 struct cifsFileInfo *cfile;
3856 struct cifs_tcon *tcon;
3857 ssize_t rc, total_read = 0;
3858 loff_t offset = iocb->ki_pos;
3859 struct cifs_aio_ctx *ctx;
3862 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
3863 * fall back to data copy read path
3864 * this could be improved by getting pages directly in ITER_KVEC
3866 if (direct && to->type & ITER_KVEC) {
3867 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
3871 len = iov_iter_count(to);
3875 cifs_sb = CIFS_FILE_SB(file);
3876 cfile = file->private_data;
3877 tcon = tlink_tcon(cfile->tlink);
3879 if (!tcon->ses->server->ops->async_readv)
3882 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3883 cifs_dbg(FYI, "attempting read on write only file instance\n");
3885 ctx = cifs_aio_ctx_alloc();
3889 ctx->cfile = cifsFileInfo_get(cfile);
3891 if (!is_sync_kiocb(iocb))
3894 if (iter_is_iovec(to))
3895 ctx->should_dirty = true;
3899 ctx->direct_io = true;
3903 rc = setup_aio_ctx_iter(ctx, to, READ);
3905 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3911 /* grab a lock here due to read response handlers can access ctx */
3912 mutex_lock(&ctx->aio_mutex);
3914 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
3916 /* if at least one read request send succeeded, then reset rc */
3917 if (!list_empty(&ctx->list))
3920 mutex_unlock(&ctx->aio_mutex);
3923 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3927 if (!is_sync_kiocb(iocb)) {
3928 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3929 return -EIOCBQUEUED;
3932 rc = wait_for_completion_killable(&ctx->done);
3934 mutex_lock(&ctx->aio_mutex);
3935 ctx->rc = rc = -EINTR;
3936 total_read = ctx->total_len;
3937 mutex_unlock(&ctx->aio_mutex);
3940 total_read = ctx->total_len;
3943 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3946 iocb->ki_pos += total_read;
3952 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
3954 return __cifs_readv(iocb, to, true);
3957 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
3959 return __cifs_readv(iocb, to, false);
3963 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
3965 struct inode *inode = file_inode(iocb->ki_filp);
3966 struct cifsInodeInfo *cinode = CIFS_I(inode);
3967 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3968 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3969 iocb->ki_filp->private_data;
3970 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3974 * In strict cache mode we need to read from the server all the time
3975 * if we don't have level II oplock because the server can delay mtime
3976 * change - so we can't make a decision about inode invalidating.
3977 * And we can also fail with pagereading if there are mandatory locks
3978 * on pages affected by this read but not on the region from pos to
3981 if (!CIFS_CACHE_READ(cinode))
3982 return cifs_user_readv(iocb, to);
3984 if (cap_unix(tcon->ses) &&
3985 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3986 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
3987 return generic_file_read_iter(iocb, to);
3990 * We need to hold the sem to be sure nobody modifies lock list
3991 * with a brlock that prevents reading.
3993 down_read(&cinode->lock_sem);
3994 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
3995 tcon->ses->server->vals->shared_lock_type,
3996 0, NULL, CIFS_READ_OP))
3997 rc = generic_file_read_iter(iocb, to);
3998 up_read(&cinode->lock_sem);
4003 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4006 unsigned int bytes_read = 0;
4007 unsigned int total_read;
4008 unsigned int current_read_size;
4010 struct cifs_sb_info *cifs_sb;
4011 struct cifs_tcon *tcon;
4012 struct TCP_Server_Info *server;
4015 struct cifsFileInfo *open_file;
4016 struct cifs_io_parms io_parms;
4017 int buf_type = CIFS_NO_BUFFER;
4021 cifs_sb = CIFS_FILE_SB(file);
4023 /* FIXME: set up handlers for larger reads and/or convert to async */
4024 rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
4026 if (file->private_data == NULL) {
4031 open_file = file->private_data;
4032 tcon = tlink_tcon(open_file->tlink);
4033 server = tcon->ses->server;
4035 if (!server->ops->sync_read) {
4040 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4041 pid = open_file->pid;
4043 pid = current->tgid;
4045 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4046 cifs_dbg(FYI, "attempting read on write only file instance\n");
4048 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4049 total_read += bytes_read, cur_offset += bytes_read) {
4051 current_read_size = min_t(uint, read_size - total_read,
4054 * For windows me and 9x we do not want to request more
4055 * than it negotiated since it will refuse the read
4058 if ((tcon->ses) && !(tcon->ses->capabilities &
4059 tcon->ses->server->vals->cap_large_files)) {
4060 current_read_size = min_t(uint,
4061 current_read_size, CIFSMaxBufSize);
4063 if (open_file->invalidHandle) {
4064 rc = cifs_reopen_file(open_file, true);
4069 io_parms.tcon = tcon;
4070 io_parms.offset = *offset;
4071 io_parms.length = current_read_size;
4072 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4073 &bytes_read, &cur_offset,
4075 } while (rc == -EAGAIN);
4077 if (rc || (bytes_read == 0)) {
4085 cifs_stats_bytes_read(tcon, total_read);
4086 *offset += bytes_read;
4094 * If the page is mmap'ed into a process' page tables, then we need to make
4095 * sure that it doesn't change while being written back.
4098 cifs_page_mkwrite(struct vm_fault *vmf)
4100 struct page *page = vmf->page;
4103 return VM_FAULT_LOCKED;
4106 static const struct vm_operations_struct cifs_file_vm_ops = {
4107 .fault = filemap_fault,
4108 .map_pages = filemap_map_pages,
4109 .page_mkwrite = cifs_page_mkwrite,
4112 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4115 struct inode *inode = file_inode(file);
4119 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4120 rc = cifs_zap_mapping(inode);
4122 rc = generic_file_mmap(file, vma);
4124 vma->vm_ops = &cifs_file_vm_ops;
4130 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4136 rc = cifs_revalidate_file(file);
4138 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4141 rc = generic_file_mmap(file, vma);
4143 vma->vm_ops = &cifs_file_vm_ops;
4150 cifs_readv_complete(struct work_struct *work)
4152 unsigned int i, got_bytes;
4153 struct cifs_readdata *rdata = container_of(work,
4154 struct cifs_readdata, work);
4156 got_bytes = rdata->got_bytes;
4157 for (i = 0; i < rdata->nr_pages; i++) {
4158 struct page *page = rdata->pages[i];
4160 lru_cache_add_file(page);
4162 if (rdata->result == 0 ||
4163 (rdata->result == -EAGAIN && got_bytes)) {
4164 flush_dcache_page(page);
4165 SetPageUptodate(page);
4170 if (rdata->result == 0 ||
4171 (rdata->result == -EAGAIN && got_bytes))
4172 cifs_readpage_to_fscache(rdata->mapping->host, page);
4174 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4177 rdata->pages[i] = NULL;
4179 kref_put(&rdata->refcount, cifs_readdata_release);
4183 readpages_fill_pages(struct TCP_Server_Info *server,
4184 struct cifs_readdata *rdata, struct iov_iter *iter,
4191 unsigned int nr_pages = rdata->nr_pages;
4192 unsigned int page_offset = rdata->page_offset;
4194 /* determine the eof that the server (probably) has */
4195 eof = CIFS_I(rdata->mapping->host)->server_eof;
4196 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4197 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4199 rdata->got_bytes = 0;
4200 rdata->tailsz = PAGE_SIZE;
4201 for (i = 0; i < nr_pages; i++) {
4202 struct page *page = rdata->pages[i];
4203 unsigned int to_read = rdata->pagesz;
4207 to_read -= page_offset;
4213 if (len >= to_read) {
4215 } else if (len > 0) {
4216 /* enough for partial page, fill and zero the rest */
4217 zero_user(page, len + page_offset, to_read - len);
4218 n = rdata->tailsz = len;
4220 } else if (page->index > eof_index) {
4222 * The VFS will not try to do readahead past the
4223 * i_size, but it's possible that we have outstanding
4224 * writes with gaps in the middle and the i_size hasn't
4225 * caught up yet. Populate those with zeroed out pages
4226 * to prevent the VFS from repeatedly attempting to
4227 * fill them until the writes are flushed.
4229 zero_user(page, 0, PAGE_SIZE);
4230 lru_cache_add_file(page);
4231 flush_dcache_page(page);
4232 SetPageUptodate(page);
4235 rdata->pages[i] = NULL;
4239 /* no need to hold page hostage */
4240 lru_cache_add_file(page);
4243 rdata->pages[i] = NULL;
4249 result = copy_page_from_iter(
4250 page, page_offset, n, iter);
4251 #ifdef CONFIG_CIFS_SMB_DIRECT
4256 result = cifs_read_page_from_socket(
4257 server, page, page_offset, n);
4261 rdata->got_bytes += result;
4264 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4265 rdata->got_bytes : result;
4269 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4270 struct cifs_readdata *rdata, unsigned int len)
4272 return readpages_fill_pages(server, rdata, NULL, len);
4276 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4277 struct cifs_readdata *rdata,
4278 struct iov_iter *iter)
4280 return readpages_fill_pages(server, rdata, iter, iter->count);
4284 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4285 unsigned int rsize, struct list_head *tmplist,
4286 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4288 struct page *page, *tpage;
4289 unsigned int expected_index;
4291 gfp_t gfp = readahead_gfp_mask(mapping);
4293 INIT_LIST_HEAD(tmplist);
4295 page = lru_to_page(page_list);
4298 * Lock the page and put it in the cache. Since no one else
4299 * should have access to this page, we're safe to simply set
4300 * PG_locked without checking it first.
4302 __SetPageLocked(page);
4303 rc = add_to_page_cache_locked(page, mapping,
4306 /* give up if we can't stick it in the cache */
4308 __ClearPageLocked(page);
4312 /* move first page to the tmplist */
4313 *offset = (loff_t)page->index << PAGE_SHIFT;
4316 list_move_tail(&page->lru, tmplist);
4318 /* now try and add more pages onto the request */
4319 expected_index = page->index + 1;
4320 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4321 /* discontinuity ? */
4322 if (page->index != expected_index)
4325 /* would this page push the read over the rsize? */
4326 if (*bytes + PAGE_SIZE > rsize)
4329 __SetPageLocked(page);
4330 if (add_to_page_cache_locked(page, mapping, page->index, gfp)) {
4331 __ClearPageLocked(page);
4334 list_move_tail(&page->lru, tmplist);
4335 (*bytes) += PAGE_SIZE;
4342 static int cifs_readpages(struct file *file, struct address_space *mapping,
4343 struct list_head *page_list, unsigned num_pages)
4346 struct list_head tmplist;
4347 struct cifsFileInfo *open_file = file->private_data;
4348 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4349 struct TCP_Server_Info *server;
4355 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4356 * immediately if the cookie is negative
4358 * After this point, every page in the list might have PG_fscache set,
4359 * so we will need to clean that up off of every page we don't use.
4361 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4368 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4369 pid = open_file->pid;
4371 pid = current->tgid;
4374 server = tlink_tcon(open_file->tlink)->ses->server;
4376 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4377 __func__, file, mapping, num_pages);
4380 * Start with the page at end of list and move it to private
4381 * list. Do the same with any following pages until we hit
4382 * the rsize limit, hit an index discontinuity, or run out of
4383 * pages. Issue the async read and then start the loop again
4384 * until the list is empty.
4386 * Note that list order is important. The page_list is in
4387 * the order of declining indexes. When we put the pages in
4388 * the rdata->pages, then we want them in increasing order.
4390 while (!list_empty(page_list)) {
4391 unsigned int i, nr_pages, bytes, rsize;
4393 struct page *page, *tpage;
4394 struct cifs_readdata *rdata;
4395 struct cifs_credits credits_on_stack;
4396 struct cifs_credits *credits = &credits_on_stack;
4398 if (open_file->invalidHandle) {
4399 rc = cifs_reopen_file(open_file, true);
4406 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
4412 * Give up immediately if rsize is too small to read an entire
4413 * page. The VFS will fall back to readpage. We should never
4414 * reach this point however since we set ra_pages to 0 when the
4415 * rsize is smaller than a cache page.
4417 if (unlikely(rsize < PAGE_SIZE)) {
4418 add_credits_and_wake_if(server, credits, 0);
4423 rc = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4424 &nr_pages, &offset, &bytes);
4426 add_credits_and_wake_if(server, credits, 0);
4430 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4432 /* best to give up if we're out of mem */
4433 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4434 list_del(&page->lru);
4435 lru_cache_add_file(page);
4440 add_credits_and_wake_if(server, credits, 0);
4444 rdata->cfile = cifsFileInfo_get(open_file);
4445 rdata->mapping = mapping;
4446 rdata->offset = offset;
4447 rdata->bytes = bytes;
4449 rdata->pagesz = PAGE_SIZE;
4450 rdata->tailsz = PAGE_SIZE;
4451 rdata->read_into_pages = cifs_readpages_read_into_pages;
4452 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4453 rdata->credits = credits_on_stack;
4455 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4456 list_del(&page->lru);
4457 rdata->pages[rdata->nr_pages++] = page;
4460 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4463 if (rdata->cfile->invalidHandle)
4466 rc = server->ops->async_readv(rdata);
4470 add_credits_and_wake_if(server, &rdata->credits, 0);
4471 for (i = 0; i < rdata->nr_pages; i++) {
4472 page = rdata->pages[i];
4473 lru_cache_add_file(page);
4477 /* Fallback to the readpage in error/reconnect cases */
4478 kref_put(&rdata->refcount, cifs_readdata_release);
4482 kref_put(&rdata->refcount, cifs_readdata_release);
4485 /* Any pages that have been shown to fscache but didn't get added to
4486 * the pagecache must be uncached before they get returned to the
4489 cifs_fscache_readpages_cancel(mapping->host, page_list);
4495 * cifs_readpage_worker must be called with the page pinned
4497 static int cifs_readpage_worker(struct file *file, struct page *page,
4503 /* Is the page cached? */
4504 rc = cifs_readpage_from_fscache(file_inode(file), page);
4508 read_data = kmap(page);
4509 /* for reads over a certain size could initiate async read ahead */
4511 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4516 cifs_dbg(FYI, "Bytes read %d\n", rc);
4518 /* we do not want atime to be less than mtime, it broke some apps */
4519 file_inode(file)->i_atime = current_time(file_inode(file));
4520 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4521 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4523 file_inode(file)->i_atime = current_time(file_inode(file));
4526 memset(read_data + rc, 0, PAGE_SIZE - rc);
4528 flush_dcache_page(page);
4529 SetPageUptodate(page);
4531 /* send this page to the cache */
4532 cifs_readpage_to_fscache(file_inode(file), page);
4544 static int cifs_readpage(struct file *file, struct page *page)
4546 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
4552 if (file->private_data == NULL) {
4558 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4559 page, (int)offset, (int)offset);
4561 rc = cifs_readpage_worker(file, page, &offset);
4567 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4569 struct cifsFileInfo *open_file;
4571 spin_lock(&cifs_inode->open_file_lock);
4572 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4573 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4574 spin_unlock(&cifs_inode->open_file_lock);
4578 spin_unlock(&cifs_inode->open_file_lock);
4582 /* We do not want to update the file size from server for inodes
4583 open for write - to avoid races with writepage extending
4584 the file - in the future we could consider allowing
4585 refreshing the inode only on increases in the file size
4586 but this is tricky to do without racing with writebehind
4587 page caching in the current Linux kernel design */
4588 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4593 if (is_inode_writable(cifsInode)) {
4594 /* This inode is open for write at least once */
4595 struct cifs_sb_info *cifs_sb;
4597 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4598 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4599 /* since no page cache to corrupt on directio
4600 we can change size safely */
4604 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4612 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4613 loff_t pos, unsigned len, unsigned flags,
4614 struct page **pagep, void **fsdata)
4617 pgoff_t index = pos >> PAGE_SHIFT;
4618 loff_t offset = pos & (PAGE_SIZE - 1);
4619 loff_t page_start = pos & PAGE_MASK;
4624 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4627 page = grab_cache_page_write_begin(mapping, index, flags);
4633 if (PageUptodate(page))
4637 * If we write a full page it will be up to date, no need to read from
4638 * the server. If the write is short, we'll end up doing a sync write
4641 if (len == PAGE_SIZE)
4645 * optimize away the read when we have an oplock, and we're not
4646 * expecting to use any of the data we'd be reading in. That
4647 * is, when the page lies beyond the EOF, or straddles the EOF
4648 * and the write will cover all of the existing data.
4650 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4651 i_size = i_size_read(mapping->host);
4652 if (page_start >= i_size ||
4653 (offset == 0 && (pos + len) >= i_size)) {
4654 zero_user_segments(page, 0, offset,
4658 * PageChecked means that the parts of the page
4659 * to which we're not writing are considered up
4660 * to date. Once the data is copied to the
4661 * page, it can be set uptodate.
4663 SetPageChecked(page);
4668 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4670 * might as well read a page, it is fast enough. If we get
4671 * an error, we don't need to return it. cifs_write_end will
4672 * do a sync write instead since PG_uptodate isn't set.
4674 cifs_readpage_worker(file, page, &page_start);
4679 /* we could try using another file handle if there is one -
4680 but how would we lock it to prevent close of that handle
4681 racing with this read? In any case
4682 this will be written out by write_end so is fine */
4689 static int cifs_release_page(struct page *page, gfp_t gfp)
4691 if (PagePrivate(page))
4694 return cifs_fscache_release_page(page, gfp);
4697 static void cifs_invalidate_page(struct page *page, unsigned int offset,
4698 unsigned int length)
4700 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4702 if (offset == 0 && length == PAGE_SIZE)
4703 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4706 static int cifs_launder_page(struct page *page)
4709 loff_t range_start = page_offset(page);
4710 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
4711 struct writeback_control wbc = {
4712 .sync_mode = WB_SYNC_ALL,
4714 .range_start = range_start,
4715 .range_end = range_end,
4718 cifs_dbg(FYI, "Launder page: %p\n", page);
4720 if (clear_page_dirty_for_io(page))
4721 rc = cifs_writepage_locked(page, &wbc);
4723 cifs_fscache_invalidate_page(page, page->mapping->host);
4727 void cifs_oplock_break(struct work_struct *work)
4729 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4731 struct inode *inode = d_inode(cfile->dentry);
4732 struct cifsInodeInfo *cinode = CIFS_I(inode);
4733 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4734 struct TCP_Server_Info *server = tcon->ses->server;
4736 bool purge_cache = false;
4738 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4739 TASK_UNINTERRUPTIBLE);
4741 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4742 cfile->oplock_epoch, &purge_cache);
4744 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4745 cifs_has_mand_locks(cinode)) {
4746 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4751 if (inode && S_ISREG(inode->i_mode)) {
4752 if (CIFS_CACHE_READ(cinode))
4753 break_lease(inode, O_RDONLY);
4755 break_lease(inode, O_WRONLY);
4756 rc = filemap_fdatawrite(inode->i_mapping);
4757 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4758 rc = filemap_fdatawait(inode->i_mapping);
4759 mapping_set_error(inode->i_mapping, rc);
4760 cifs_zap_mapping(inode);
4762 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4763 if (CIFS_CACHE_WRITE(cinode))
4764 goto oplock_break_ack;
4767 rc = cifs_push_locks(cfile);
4769 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4773 * releasing stale oplock after recent reconnect of smb session using
4774 * a now incorrect file handle is not a data integrity issue but do
4775 * not bother sending an oplock release if session to server still is
4776 * disconnected since oplock already released by the server
4778 if (!cfile->oplock_break_cancelled) {
4779 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4781 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4783 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4784 cifs_done_oplock_break(cinode);
4788 * The presence of cifs_direct_io() in the address space ops vector
4789 * allowes open() O_DIRECT flags which would have failed otherwise.
4791 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4792 * so this method should never be called.
4794 * Direct IO is not yet supported in the cached mode.
4797 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4801 * Eventually need to support direct IO for non forcedirectio mounts
4807 const struct address_space_operations cifs_addr_ops = {
4808 .readpage = cifs_readpage,
4809 .readpages = cifs_readpages,
4810 .writepage = cifs_writepage,
4811 .writepages = cifs_writepages,
4812 .write_begin = cifs_write_begin,
4813 .write_end = cifs_write_end,
4814 .set_page_dirty = __set_page_dirty_nobuffers,
4815 .releasepage = cifs_release_page,
4816 .direct_IO = cifs_direct_io,
4817 .invalidatepage = cifs_invalidate_page,
4818 .launder_page = cifs_launder_page,
4822 * cifs_readpages requires the server to support a buffer large enough to
4823 * contain the header plus one complete page of data. Otherwise, we need
4824 * to leave cifs_readpages out of the address space operations.
4826 const struct address_space_operations cifs_addr_ops_smallbuf = {
4827 .readpage = cifs_readpage,
4828 .writepage = cifs_writepage,
4829 .writepages = cifs_writepages,
4830 .write_begin = cifs_write_begin,
4831 .write_end = cifs_write_end,
4832 .set_page_dirty = __set_page_dirty_nobuffers,
4833 .releasepage = cifs_release_page,
4834 .invalidatepage = cifs_invalidate_page,
4835 .launder_page = cifs_launder_page,