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"
47 #include "fs_context.h"
49 static inline int cifs_convert_flags(unsigned int flags)
51 if ((flags & O_ACCMODE) == O_RDONLY)
53 else if ((flags & O_ACCMODE) == O_WRONLY)
55 else if ((flags & O_ACCMODE) == O_RDWR) {
56 /* GENERIC_ALL is too much permission to request
57 can cause unnecessary access denied on create */
58 /* return GENERIC_ALL; */
59 return (GENERIC_READ | GENERIC_WRITE);
62 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
63 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
67 static u32 cifs_posix_convert_flags(unsigned int flags)
71 if ((flags & O_ACCMODE) == O_RDONLY)
72 posix_flags = SMB_O_RDONLY;
73 else if ((flags & O_ACCMODE) == O_WRONLY)
74 posix_flags = SMB_O_WRONLY;
75 else if ((flags & O_ACCMODE) == O_RDWR)
76 posix_flags = SMB_O_RDWR;
78 if (flags & O_CREAT) {
79 posix_flags |= SMB_O_CREAT;
81 posix_flags |= SMB_O_EXCL;
82 } else if (flags & O_EXCL)
83 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
84 current->comm, current->tgid);
87 posix_flags |= SMB_O_TRUNC;
88 /* be safe and imply O_SYNC for O_DSYNC */
90 posix_flags |= SMB_O_SYNC;
91 if (flags & O_DIRECTORY)
92 posix_flags |= SMB_O_DIRECTORY;
93 if (flags & O_NOFOLLOW)
94 posix_flags |= SMB_O_NOFOLLOW;
96 posix_flags |= SMB_O_DIRECT;
101 static inline int cifs_get_disposition(unsigned int flags)
103 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
105 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
106 return FILE_OVERWRITE_IF;
107 else if ((flags & O_CREAT) == O_CREAT)
109 else if ((flags & O_TRUNC) == O_TRUNC)
110 return FILE_OVERWRITE;
115 int cifs_posix_open(const char *full_path, struct inode **pinode,
116 struct super_block *sb, int mode, unsigned int f_flags,
117 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
120 FILE_UNIX_BASIC_INFO *presp_data;
121 __u32 posix_flags = 0;
122 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
123 struct cifs_fattr fattr;
124 struct tcon_link *tlink;
125 struct cifs_tcon *tcon;
127 cifs_dbg(FYI, "posix open %s\n", full_path);
129 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
130 if (presp_data == NULL)
133 tlink = cifs_sb_tlink(cifs_sb);
139 tcon = tlink_tcon(tlink);
140 mode &= ~current_umask();
142 posix_flags = cifs_posix_convert_flags(f_flags);
143 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
144 poplock, full_path, cifs_sb->local_nls,
145 cifs_remap(cifs_sb));
146 cifs_put_tlink(tlink);
151 if (presp_data->Type == cpu_to_le32(-1))
152 goto posix_open_ret; /* open ok, caller does qpathinfo */
155 goto posix_open_ret; /* caller does not need info */
157 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
159 /* get new inode and set it up */
160 if (*pinode == NULL) {
161 cifs_fill_uniqueid(sb, &fattr);
162 *pinode = cifs_iget(sb, &fattr);
168 cifs_revalidate_mapping(*pinode);
169 rc = cifs_fattr_to_inode(*pinode, &fattr);
178 cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
179 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
180 struct cifs_fid *fid, unsigned int xid)
185 int create_options = CREATE_NOT_DIR;
187 struct TCP_Server_Info *server = tcon->ses->server;
188 struct cifs_open_parms oparms;
190 if (!server->ops->open)
193 desired_access = cifs_convert_flags(f_flags);
195 /*********************************************************************
196 * open flag mapping table:
198 * POSIX Flag CIFS Disposition
199 * ---------- ----------------
200 * O_CREAT FILE_OPEN_IF
201 * O_CREAT | O_EXCL FILE_CREATE
202 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
203 * O_TRUNC FILE_OVERWRITE
204 * none of the above FILE_OPEN
206 * Note that there is not a direct match between disposition
207 * FILE_SUPERSEDE (ie create whether or not file exists although
208 * O_CREAT | O_TRUNC is similar but truncates the existing
209 * file rather than creating a new file as FILE_SUPERSEDE does
210 * (which uses the attributes / metadata passed in on open call)
212 *? O_SYNC is a reasonable match to CIFS writethrough flag
213 *? and the read write flags match reasonably. O_LARGEFILE
214 *? is irrelevant because largefile support is always used
215 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
216 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
217 *********************************************************************/
219 disposition = cifs_get_disposition(f_flags);
221 /* BB pass O_SYNC flag through on file attributes .. BB */
223 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
227 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
228 if (f_flags & O_SYNC)
229 create_options |= CREATE_WRITE_THROUGH;
231 if (f_flags & O_DIRECT)
232 create_options |= CREATE_NO_BUFFER;
235 oparms.cifs_sb = cifs_sb;
236 oparms.desired_access = desired_access;
237 oparms.create_options = cifs_create_options(cifs_sb, create_options);
238 oparms.disposition = disposition;
239 oparms.path = full_path;
241 oparms.reconnect = false;
243 rc = server->ops->open(xid, &oparms, oplock, buf);
248 /* TODO: Add support for calling posix query info but with passing in fid */
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 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
424 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
426 _cifsFileInfo_put(cifs_file, true, true);
430 * _cifsFileInfo_put - release a reference of file priv data
432 * This may involve closing the filehandle @cifs_file out on the
433 * server. Must be called without holding tcon->open_file_lock,
434 * cinode->open_file_lock and cifs_file->file_info_lock.
436 * If @wait_for_oplock_handler is true and we are releasing the last
437 * reference, wait for any running oplock break handler of the file
438 * and cancel any pending one.
440 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
441 * @wait_oplock_handler: must be false if called from oplock_break_handler
442 * @offload: not offloaded on close and oplock breaks
445 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
446 bool wait_oplock_handler, bool offload)
448 struct inode *inode = d_inode(cifs_file->dentry);
449 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
450 struct TCP_Server_Info *server = tcon->ses->server;
451 struct cifsInodeInfo *cifsi = CIFS_I(inode);
452 struct super_block *sb = inode->i_sb;
453 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
455 struct cifs_pending_open open;
456 bool oplock_break_cancelled;
458 spin_lock(&tcon->open_file_lock);
459 spin_lock(&cifsi->open_file_lock);
460 spin_lock(&cifs_file->file_info_lock);
461 if (--cifs_file->count > 0) {
462 spin_unlock(&cifs_file->file_info_lock);
463 spin_unlock(&cifsi->open_file_lock);
464 spin_unlock(&tcon->open_file_lock);
467 spin_unlock(&cifs_file->file_info_lock);
469 if (server->ops->get_lease_key)
470 server->ops->get_lease_key(inode, &fid);
472 /* store open in pending opens to make sure we don't miss lease break */
473 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
475 /* remove it from the lists */
476 list_del(&cifs_file->flist);
477 list_del(&cifs_file->tlist);
478 atomic_dec(&tcon->num_local_opens);
480 if (list_empty(&cifsi->openFileList)) {
481 cifs_dbg(FYI, "closing last open instance for inode %p\n",
482 d_inode(cifs_file->dentry));
484 * In strict cache mode we need invalidate mapping on the last
485 * close because it may cause a error when we open this file
486 * again and get at least level II oplock.
488 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
489 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
490 cifs_set_oplock_level(cifsi, 0);
493 spin_unlock(&cifsi->open_file_lock);
494 spin_unlock(&tcon->open_file_lock);
496 oplock_break_cancelled = wait_oplock_handler ?
497 cancel_work_sync(&cifs_file->oplock_break) : false;
499 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
500 struct TCP_Server_Info *server = tcon->ses->server;
504 if (server->ops->close_getattr)
505 server->ops->close_getattr(xid, tcon, cifs_file);
506 else if (server->ops->close)
507 server->ops->close(xid, tcon, &cifs_file->fid);
511 if (oplock_break_cancelled)
512 cifs_done_oplock_break(cifsi);
514 cifs_del_pending_open(&open);
517 queue_work(fileinfo_put_wq, &cifs_file->put);
519 cifsFileInfo_put_final(cifs_file);
522 int cifs_open(struct inode *inode, struct file *file)
528 struct cifs_sb_info *cifs_sb;
529 struct TCP_Server_Info *server;
530 struct cifs_tcon *tcon;
531 struct tcon_link *tlink;
532 struct cifsFileInfo *cfile = NULL;
534 const char *full_path;
535 bool posix_open_ok = false;
537 struct cifs_pending_open open;
541 cifs_sb = CIFS_SB(inode->i_sb);
542 tlink = cifs_sb_tlink(cifs_sb);
545 return PTR_ERR(tlink);
547 tcon = tlink_tcon(tlink);
548 server = tcon->ses->server;
550 page = alloc_dentry_path();
551 full_path = build_path_from_dentry(file_dentry(file), page);
552 if (IS_ERR(full_path)) {
553 rc = PTR_ERR(full_path);
557 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
558 inode, file->f_flags, full_path);
560 if (file->f_flags & O_DIRECT &&
561 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
562 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
563 file->f_op = &cifs_file_direct_nobrl_ops;
565 file->f_op = &cifs_file_direct_ops;
573 if (!tcon->broken_posix_open && tcon->unix_ext &&
574 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
575 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
576 /* can not refresh inode info since size could be stale */
577 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
578 cifs_sb->ctx->file_mode /* ignored */,
579 file->f_flags, &oplock, &fid.netfid, xid);
581 cifs_dbg(FYI, "posix open succeeded\n");
582 posix_open_ok = true;
583 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
584 if (tcon->ses->serverNOS)
585 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",
587 tcon->ses->serverNOS);
588 tcon->broken_posix_open = true;
589 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
590 (rc != -EOPNOTSUPP)) /* path not found or net err */
593 * Else fallthrough to retry open the old way on network i/o
598 if (server->ops->get_lease_key)
599 server->ops->get_lease_key(inode, &fid);
601 cifs_add_pending_open(&fid, tlink, &open);
603 if (!posix_open_ok) {
604 if (server->ops->get_lease_key)
605 server->ops->get_lease_key(inode, &fid);
607 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
608 file->f_flags, &oplock, &fid, xid);
610 cifs_del_pending_open(&open);
615 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
617 if (server->ops->close)
618 server->ops->close(xid, tcon, &fid);
619 cifs_del_pending_open(&open);
624 cifs_fscache_set_inode_cookie(inode, file);
626 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
628 * Time to set mode which we can not set earlier due to
629 * problems creating new read-only files.
631 struct cifs_unix_set_info_args args = {
632 .mode = inode->i_mode,
633 .uid = INVALID_UID, /* no change */
634 .gid = INVALID_GID, /* no change */
635 .ctime = NO_CHANGE_64,
636 .atime = NO_CHANGE_64,
637 .mtime = NO_CHANGE_64,
640 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
645 free_dentry_path(page);
647 cifs_put_tlink(tlink);
651 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
654 * Try to reacquire byte range locks that were released when session
655 * to server was lost.
658 cifs_relock_file(struct cifsFileInfo *cfile)
660 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
661 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
662 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
665 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
666 if (cinode->can_cache_brlcks) {
667 /* can cache locks - no need to relock */
668 up_read(&cinode->lock_sem);
672 if (cap_unix(tcon->ses) &&
673 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
674 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
675 rc = cifs_push_posix_locks(cfile);
677 rc = tcon->ses->server->ops->push_mand_locks(cfile);
679 up_read(&cinode->lock_sem);
684 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
689 struct cifs_sb_info *cifs_sb;
690 struct cifs_tcon *tcon;
691 struct TCP_Server_Info *server;
692 struct cifsInodeInfo *cinode;
695 const char *full_path;
697 int disposition = FILE_OPEN;
698 int create_options = CREATE_NOT_DIR;
699 struct cifs_open_parms oparms;
702 mutex_lock(&cfile->fh_mutex);
703 if (!cfile->invalidHandle) {
704 mutex_unlock(&cfile->fh_mutex);
709 inode = d_inode(cfile->dentry);
710 cifs_sb = CIFS_SB(inode->i_sb);
711 tcon = tlink_tcon(cfile->tlink);
712 server = tcon->ses->server;
715 * Can not grab rename sem here because various ops, including those
716 * that already have the rename sem can end up causing writepage to get
717 * called and if the server was down that means we end up here, and we
718 * can never tell if the caller already has the rename_sem.
720 page = alloc_dentry_path();
721 full_path = build_path_from_dentry(cfile->dentry, page);
722 if (IS_ERR(full_path)) {
723 mutex_unlock(&cfile->fh_mutex);
724 free_dentry_path(page);
726 return PTR_ERR(full_path);
729 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
730 inode, cfile->f_flags, full_path);
732 if (tcon->ses->server->oplocks)
737 if (tcon->unix_ext && cap_unix(tcon->ses) &&
738 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
739 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
741 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
742 * original open. Must mask them off for a reopen.
744 unsigned int oflags = cfile->f_flags &
745 ~(O_CREAT | O_EXCL | O_TRUNC);
747 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
748 cifs_sb->ctx->file_mode /* ignored */,
749 oflags, &oplock, &cfile->fid.netfid, xid);
751 cifs_dbg(FYI, "posix reopen succeeded\n");
752 oparms.reconnect = true;
756 * fallthrough to retry open the old way on errors, especially
757 * in the reconnect path it is important to retry hard
761 desired_access = cifs_convert_flags(cfile->f_flags);
763 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
764 if (cfile->f_flags & O_SYNC)
765 create_options |= CREATE_WRITE_THROUGH;
767 if (cfile->f_flags & O_DIRECT)
768 create_options |= CREATE_NO_BUFFER;
770 if (server->ops->get_lease_key)
771 server->ops->get_lease_key(inode, &cfile->fid);
774 oparms.cifs_sb = cifs_sb;
775 oparms.desired_access = desired_access;
776 oparms.create_options = cifs_create_options(cifs_sb, create_options);
777 oparms.disposition = disposition;
778 oparms.path = full_path;
779 oparms.fid = &cfile->fid;
780 oparms.reconnect = true;
783 * Can not refresh inode by passing in file_info buf to be returned by
784 * ops->open and then calling get_inode_info with returned buf since
785 * file might have write behind data that needs to be flushed and server
786 * version of file size can be stale. If we knew for sure that inode was
787 * not dirty locally we could do this.
789 rc = server->ops->open(xid, &oparms, &oplock, NULL);
790 if (rc == -ENOENT && oparms.reconnect == false) {
791 /* durable handle timeout is expired - open the file again */
792 rc = server->ops->open(xid, &oparms, &oplock, NULL);
793 /* indicate that we need to relock the file */
794 oparms.reconnect = true;
798 mutex_unlock(&cfile->fh_mutex);
799 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
800 cifs_dbg(FYI, "oplock: %d\n", oplock);
801 goto reopen_error_exit;
805 cfile->invalidHandle = false;
806 mutex_unlock(&cfile->fh_mutex);
807 cinode = CIFS_I(inode);
810 rc = filemap_write_and_wait(inode->i_mapping);
811 if (!is_interrupt_error(rc))
812 mapping_set_error(inode->i_mapping, rc);
814 if (tcon->posix_extensions)
815 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
816 else if (tcon->unix_ext)
817 rc = cifs_get_inode_info_unix(&inode, full_path,
820 rc = cifs_get_inode_info(&inode, full_path, NULL,
821 inode->i_sb, xid, NULL);
824 * Else we are writing out data to server already and could deadlock if
825 * we tried to flush data, and since we do not know if we have data that
826 * would invalidate the current end of file on the server we can not go
827 * to the server to get the new inode info.
831 * If the server returned a read oplock and we have mandatory brlocks,
832 * set oplock level to None.
834 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
835 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
839 server->ops->set_fid(cfile, &cfile->fid, oplock);
840 if (oparms.reconnect)
841 cifs_relock_file(cfile);
844 free_dentry_path(page);
849 int cifs_close(struct inode *inode, struct file *file)
851 if (file->private_data != NULL) {
852 _cifsFileInfo_put(file->private_data, true, false);
853 file->private_data = NULL;
856 /* return code from the ->release op is always ignored */
861 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
863 struct cifsFileInfo *open_file;
864 struct list_head *tmp;
865 struct list_head *tmp1;
866 struct list_head tmp_list;
868 if (!tcon->use_persistent || !tcon->need_reopen_files)
871 tcon->need_reopen_files = false;
873 cifs_dbg(FYI, "Reopen persistent handles\n");
874 INIT_LIST_HEAD(&tmp_list);
876 /* list all files open on tree connection, reopen resilient handles */
877 spin_lock(&tcon->open_file_lock);
878 list_for_each(tmp, &tcon->openFileList) {
879 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
880 if (!open_file->invalidHandle)
882 cifsFileInfo_get(open_file);
883 list_add_tail(&open_file->rlist, &tmp_list);
885 spin_unlock(&tcon->open_file_lock);
887 list_for_each_safe(tmp, tmp1, &tmp_list) {
888 open_file = list_entry(tmp, struct cifsFileInfo, rlist);
889 if (cifs_reopen_file(open_file, false /* do not flush */))
890 tcon->need_reopen_files = true;
891 list_del_init(&open_file->rlist);
892 cifsFileInfo_put(open_file);
896 int cifs_closedir(struct inode *inode, struct file *file)
900 struct cifsFileInfo *cfile = file->private_data;
901 struct cifs_tcon *tcon;
902 struct TCP_Server_Info *server;
905 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
911 tcon = tlink_tcon(cfile->tlink);
912 server = tcon->ses->server;
914 cifs_dbg(FYI, "Freeing private data in close dir\n");
915 spin_lock(&cfile->file_info_lock);
916 if (server->ops->dir_needs_close(cfile)) {
917 cfile->invalidHandle = true;
918 spin_unlock(&cfile->file_info_lock);
919 if (server->ops->close_dir)
920 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
923 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
924 /* not much we can do if it fails anyway, ignore rc */
927 spin_unlock(&cfile->file_info_lock);
929 buf = cfile->srch_inf.ntwrk_buf_start;
931 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
932 cfile->srch_inf.ntwrk_buf_start = NULL;
933 if (cfile->srch_inf.smallBuf)
934 cifs_small_buf_release(buf);
936 cifs_buf_release(buf);
939 cifs_put_tlink(cfile->tlink);
940 kfree(file->private_data);
941 file->private_data = NULL;
942 /* BB can we lock the filestruct while this is going on? */
947 static struct cifsLockInfo *
948 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
950 struct cifsLockInfo *lock =
951 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
954 lock->offset = offset;
955 lock->length = length;
957 lock->pid = current->tgid;
959 INIT_LIST_HEAD(&lock->blist);
960 init_waitqueue_head(&lock->block_q);
965 cifs_del_lock_waiters(struct cifsLockInfo *lock)
967 struct cifsLockInfo *li, *tmp;
968 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
969 list_del_init(&li->blist);
970 wake_up(&li->block_q);
974 #define CIFS_LOCK_OP 0
975 #define CIFS_READ_OP 1
976 #define CIFS_WRITE_OP 2
978 /* @rw_check : 0 - no op, 1 - read, 2 - write */
980 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
981 __u64 length, __u8 type, __u16 flags,
982 struct cifsFileInfo *cfile,
983 struct cifsLockInfo **conf_lock, int rw_check)
985 struct cifsLockInfo *li;
986 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
987 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
989 list_for_each_entry(li, &fdlocks->locks, llist) {
990 if (offset + length <= li->offset ||
991 offset >= li->offset + li->length)
993 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
994 server->ops->compare_fids(cfile, cur_cfile)) {
995 /* shared lock prevents write op through the same fid */
996 if (!(li->type & server->vals->shared_lock_type) ||
997 rw_check != CIFS_WRITE_OP)
1000 if ((type & server->vals->shared_lock_type) &&
1001 ((server->ops->compare_fids(cfile, cur_cfile) &&
1002 current->tgid == li->pid) || type == li->type))
1004 if (rw_check == CIFS_LOCK_OP &&
1005 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1006 server->ops->compare_fids(cfile, cur_cfile))
1016 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1017 __u8 type, __u16 flags,
1018 struct cifsLockInfo **conf_lock, int rw_check)
1021 struct cifs_fid_locks *cur;
1022 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1024 list_for_each_entry(cur, &cinode->llist, llist) {
1025 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1026 flags, cfile, conf_lock,
1036 * Check if there is another lock that prevents us to set the lock (mandatory
1037 * style). If such a lock exists, update the flock structure with its
1038 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1039 * or leave it the same if we can't. Returns 0 if we don't need to request to
1040 * the server or 1 otherwise.
1043 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1044 __u8 type, struct file_lock *flock)
1047 struct cifsLockInfo *conf_lock;
1048 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1049 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1052 down_read(&cinode->lock_sem);
1054 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1055 flock->fl_flags, &conf_lock,
1058 flock->fl_start = conf_lock->offset;
1059 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1060 flock->fl_pid = conf_lock->pid;
1061 if (conf_lock->type & server->vals->shared_lock_type)
1062 flock->fl_type = F_RDLCK;
1064 flock->fl_type = F_WRLCK;
1065 } else if (!cinode->can_cache_brlcks)
1068 flock->fl_type = F_UNLCK;
1070 up_read(&cinode->lock_sem);
1075 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1077 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1078 cifs_down_write(&cinode->lock_sem);
1079 list_add_tail(&lock->llist, &cfile->llist->locks);
1080 up_write(&cinode->lock_sem);
1084 * Set the byte-range lock (mandatory style). Returns:
1085 * 1) 0, if we set the lock and don't need to request to the server;
1086 * 2) 1, if no locks prevent us but we need to request to the server;
1087 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1090 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1093 struct cifsLockInfo *conf_lock;
1094 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1100 cifs_down_write(&cinode->lock_sem);
1102 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1103 lock->type, lock->flags, &conf_lock,
1105 if (!exist && cinode->can_cache_brlcks) {
1106 list_add_tail(&lock->llist, &cfile->llist->locks);
1107 up_write(&cinode->lock_sem);
1116 list_add_tail(&lock->blist, &conf_lock->blist);
1117 up_write(&cinode->lock_sem);
1118 rc = wait_event_interruptible(lock->block_q,
1119 (lock->blist.prev == &lock->blist) &&
1120 (lock->blist.next == &lock->blist));
1123 cifs_down_write(&cinode->lock_sem);
1124 list_del_init(&lock->blist);
1127 up_write(&cinode->lock_sem);
1132 * Check if there is another lock that prevents us to set the lock (posix
1133 * style). If such a lock exists, update the flock structure with its
1134 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1135 * or leave it the same if we can't. Returns 0 if we don't need to request to
1136 * the server or 1 otherwise.
1139 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1142 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1143 unsigned char saved_type = flock->fl_type;
1145 if ((flock->fl_flags & FL_POSIX) == 0)
1148 down_read(&cinode->lock_sem);
1149 posix_test_lock(file, flock);
1151 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1152 flock->fl_type = saved_type;
1156 up_read(&cinode->lock_sem);
1161 * Set the byte-range lock (posix style). Returns:
1162 * 1) <0, if the error occurs while setting the lock;
1163 * 2) 0, if we set the lock and don't need to request to the server;
1164 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1165 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1168 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1170 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1171 int rc = FILE_LOCK_DEFERRED + 1;
1173 if ((flock->fl_flags & FL_POSIX) == 0)
1176 cifs_down_write(&cinode->lock_sem);
1177 if (!cinode->can_cache_brlcks) {
1178 up_write(&cinode->lock_sem);
1182 rc = posix_lock_file(file, flock, NULL);
1183 up_write(&cinode->lock_sem);
1188 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1191 int rc = 0, stored_rc;
1192 struct cifsLockInfo *li, *tmp;
1193 struct cifs_tcon *tcon;
1194 unsigned int num, max_num, max_buf;
1195 LOCKING_ANDX_RANGE *buf, *cur;
1196 static const int types[] = {
1197 LOCKING_ANDX_LARGE_FILES,
1198 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1203 tcon = tlink_tcon(cfile->tlink);
1206 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1207 * and check it before using.
1209 max_buf = tcon->ses->server->maxBuf;
1210 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1215 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1217 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1219 max_num = (max_buf - sizeof(struct smb_hdr)) /
1220 sizeof(LOCKING_ANDX_RANGE);
1221 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1227 for (i = 0; i < 2; i++) {
1230 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1231 if (li->type != types[i])
1233 cur->Pid = cpu_to_le16(li->pid);
1234 cur->LengthLow = cpu_to_le32((u32)li->length);
1235 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1236 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1237 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1238 if (++num == max_num) {
1239 stored_rc = cifs_lockv(xid, tcon,
1241 (__u8)li->type, 0, num,
1252 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1253 (__u8)types[i], 0, num, buf);
1265 hash_lockowner(fl_owner_t owner)
1267 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1270 struct lock_to_push {
1271 struct list_head llist;
1280 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1282 struct inode *inode = d_inode(cfile->dentry);
1283 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1284 struct file_lock *flock;
1285 struct file_lock_context *flctx = inode->i_flctx;
1286 unsigned int count = 0, i;
1287 int rc = 0, xid, type;
1288 struct list_head locks_to_send, *el;
1289 struct lock_to_push *lck, *tmp;
1297 spin_lock(&flctx->flc_lock);
1298 list_for_each(el, &flctx->flc_posix) {
1301 spin_unlock(&flctx->flc_lock);
1303 INIT_LIST_HEAD(&locks_to_send);
1306 * Allocating count locks is enough because no FL_POSIX locks can be
1307 * added to the list while we are holding cinode->lock_sem that
1308 * protects locking operations of this inode.
1310 for (i = 0; i < count; i++) {
1311 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1316 list_add_tail(&lck->llist, &locks_to_send);
1319 el = locks_to_send.next;
1320 spin_lock(&flctx->flc_lock);
1321 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1322 if (el == &locks_to_send) {
1324 * The list ended. We don't have enough allocated
1325 * structures - something is really wrong.
1327 cifs_dbg(VFS, "Can't push all brlocks!\n");
1330 length = 1 + flock->fl_end - flock->fl_start;
1331 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1335 lck = list_entry(el, struct lock_to_push, llist);
1336 lck->pid = hash_lockowner(flock->fl_owner);
1337 lck->netfid = cfile->fid.netfid;
1338 lck->length = length;
1340 lck->offset = flock->fl_start;
1342 spin_unlock(&flctx->flc_lock);
1344 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1347 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1348 lck->offset, lck->length, NULL,
1352 list_del(&lck->llist);
1360 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1361 list_del(&lck->llist);
1368 cifs_push_locks(struct cifsFileInfo *cfile)
1370 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1371 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1372 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1375 /* we are going to update can_cache_brlcks here - need a write access */
1376 cifs_down_write(&cinode->lock_sem);
1377 if (!cinode->can_cache_brlcks) {
1378 up_write(&cinode->lock_sem);
1382 if (cap_unix(tcon->ses) &&
1383 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1384 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1385 rc = cifs_push_posix_locks(cfile);
1387 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1389 cinode->can_cache_brlcks = false;
1390 up_write(&cinode->lock_sem);
1395 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1396 bool *wait_flag, struct TCP_Server_Info *server)
1398 if (flock->fl_flags & FL_POSIX)
1399 cifs_dbg(FYI, "Posix\n");
1400 if (flock->fl_flags & FL_FLOCK)
1401 cifs_dbg(FYI, "Flock\n");
1402 if (flock->fl_flags & FL_SLEEP) {
1403 cifs_dbg(FYI, "Blocking lock\n");
1406 if (flock->fl_flags & FL_ACCESS)
1407 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1408 if (flock->fl_flags & FL_LEASE)
1409 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1410 if (flock->fl_flags &
1411 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1412 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1413 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1415 *type = server->vals->large_lock_type;
1416 if (flock->fl_type == F_WRLCK) {
1417 cifs_dbg(FYI, "F_WRLCK\n");
1418 *type |= server->vals->exclusive_lock_type;
1420 } else if (flock->fl_type == F_UNLCK) {
1421 cifs_dbg(FYI, "F_UNLCK\n");
1422 *type |= server->vals->unlock_lock_type;
1424 /* Check if unlock includes more than one lock range */
1425 } else if (flock->fl_type == F_RDLCK) {
1426 cifs_dbg(FYI, "F_RDLCK\n");
1427 *type |= server->vals->shared_lock_type;
1429 } else if (flock->fl_type == F_EXLCK) {
1430 cifs_dbg(FYI, "F_EXLCK\n");
1431 *type |= server->vals->exclusive_lock_type;
1433 } else if (flock->fl_type == F_SHLCK) {
1434 cifs_dbg(FYI, "F_SHLCK\n");
1435 *type |= server->vals->shared_lock_type;
1438 cifs_dbg(FYI, "Unknown type of lock\n");
1442 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1443 bool wait_flag, bool posix_lck, unsigned int xid)
1446 __u64 length = 1 + flock->fl_end - flock->fl_start;
1447 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1448 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1449 struct TCP_Server_Info *server = tcon->ses->server;
1450 __u16 netfid = cfile->fid.netfid;
1453 int posix_lock_type;
1455 rc = cifs_posix_lock_test(file, flock);
1459 if (type & server->vals->shared_lock_type)
1460 posix_lock_type = CIFS_RDLCK;
1462 posix_lock_type = CIFS_WRLCK;
1463 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1464 hash_lockowner(flock->fl_owner),
1465 flock->fl_start, length, flock,
1466 posix_lock_type, wait_flag);
1470 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1474 /* BB we could chain these into one lock request BB */
1475 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1478 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1480 flock->fl_type = F_UNLCK;
1482 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1487 if (type & server->vals->shared_lock_type) {
1488 flock->fl_type = F_WRLCK;
1492 type &= ~server->vals->exclusive_lock_type;
1494 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1495 type | server->vals->shared_lock_type,
1498 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1499 type | server->vals->shared_lock_type, 0, 1, false);
1500 flock->fl_type = F_RDLCK;
1502 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1505 flock->fl_type = F_WRLCK;
1511 cifs_move_llist(struct list_head *source, struct list_head *dest)
1513 struct list_head *li, *tmp;
1514 list_for_each_safe(li, tmp, source)
1515 list_move(li, dest);
1519 cifs_free_llist(struct list_head *llist)
1521 struct cifsLockInfo *li, *tmp;
1522 list_for_each_entry_safe(li, tmp, llist, llist) {
1523 cifs_del_lock_waiters(li);
1524 list_del(&li->llist);
1530 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1533 int rc = 0, stored_rc;
1534 static const int types[] = {
1535 LOCKING_ANDX_LARGE_FILES,
1536 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1539 unsigned int max_num, num, max_buf;
1540 LOCKING_ANDX_RANGE *buf, *cur;
1541 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1542 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1543 struct cifsLockInfo *li, *tmp;
1544 __u64 length = 1 + flock->fl_end - flock->fl_start;
1545 struct list_head tmp_llist;
1547 INIT_LIST_HEAD(&tmp_llist);
1550 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1551 * and check it before using.
1553 max_buf = tcon->ses->server->maxBuf;
1554 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1557 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1559 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1561 max_num = (max_buf - sizeof(struct smb_hdr)) /
1562 sizeof(LOCKING_ANDX_RANGE);
1563 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1567 cifs_down_write(&cinode->lock_sem);
1568 for (i = 0; i < 2; i++) {
1571 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1572 if (flock->fl_start > li->offset ||
1573 (flock->fl_start + length) <
1574 (li->offset + li->length))
1576 if (current->tgid != li->pid)
1578 if (types[i] != li->type)
1580 if (cinode->can_cache_brlcks) {
1582 * We can cache brlock requests - simply remove
1583 * a lock from the file's list.
1585 list_del(&li->llist);
1586 cifs_del_lock_waiters(li);
1590 cur->Pid = cpu_to_le16(li->pid);
1591 cur->LengthLow = cpu_to_le32((u32)li->length);
1592 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1593 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1594 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1596 * We need to save a lock here to let us add it again to
1597 * the file's list if the unlock range request fails on
1600 list_move(&li->llist, &tmp_llist);
1601 if (++num == max_num) {
1602 stored_rc = cifs_lockv(xid, tcon,
1604 li->type, num, 0, buf);
1607 * We failed on the unlock range
1608 * request - add all locks from the tmp
1609 * list to the head of the file's list.
1611 cifs_move_llist(&tmp_llist,
1612 &cfile->llist->locks);
1616 * The unlock range request succeed -
1617 * free the tmp list.
1619 cifs_free_llist(&tmp_llist);
1626 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1627 types[i], num, 0, buf);
1629 cifs_move_llist(&tmp_llist,
1630 &cfile->llist->locks);
1633 cifs_free_llist(&tmp_llist);
1637 up_write(&cinode->lock_sem);
1643 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1644 bool wait_flag, bool posix_lck, int lock, int unlock,
1648 __u64 length = 1 + flock->fl_end - flock->fl_start;
1649 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1650 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1651 struct TCP_Server_Info *server = tcon->ses->server;
1652 struct inode *inode = d_inode(cfile->dentry);
1655 int posix_lock_type;
1657 rc = cifs_posix_lock_set(file, flock);
1658 if (rc <= FILE_LOCK_DEFERRED)
1661 if (type & server->vals->shared_lock_type)
1662 posix_lock_type = CIFS_RDLCK;
1664 posix_lock_type = CIFS_WRLCK;
1667 posix_lock_type = CIFS_UNLCK;
1669 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1670 hash_lockowner(flock->fl_owner),
1671 flock->fl_start, length,
1672 NULL, posix_lock_type, wait_flag);
1677 struct cifsLockInfo *lock;
1679 lock = cifs_lock_init(flock->fl_start, length, type,
1684 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1693 * Windows 7 server can delay breaking lease from read to None
1694 * if we set a byte-range lock on a file - break it explicitly
1695 * before sending the lock to the server to be sure the next
1696 * read won't conflict with non-overlapted locks due to
1699 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1700 CIFS_CACHE_READ(CIFS_I(inode))) {
1701 cifs_zap_mapping(inode);
1702 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1704 CIFS_I(inode)->oplock = 0;
1707 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1708 type, 1, 0, wait_flag);
1714 cifs_lock_add(cfile, lock);
1716 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1719 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1721 * If this is a request to remove all locks because we
1722 * are closing the file, it doesn't matter if the
1723 * unlocking failed as both cifs.ko and the SMB server
1724 * remove the lock on file close
1727 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1728 if (!(flock->fl_flags & FL_CLOSE))
1731 rc = locks_lock_file_wait(file, flock);
1736 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1739 int lock = 0, unlock = 0;
1740 bool wait_flag = false;
1741 bool posix_lck = false;
1742 struct cifs_sb_info *cifs_sb;
1743 struct cifs_tcon *tcon;
1744 struct cifsFileInfo *cfile;
1750 if (!(fl->fl_flags & FL_FLOCK))
1753 cfile = (struct cifsFileInfo *)file->private_data;
1754 tcon = tlink_tcon(cfile->tlink);
1756 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
1758 cifs_sb = CIFS_FILE_SB(file);
1760 if (cap_unix(tcon->ses) &&
1761 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1762 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1765 if (!lock && !unlock) {
1767 * if no lock or unlock then nothing to do since we do not
1774 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1782 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1785 int lock = 0, unlock = 0;
1786 bool wait_flag = false;
1787 bool posix_lck = false;
1788 struct cifs_sb_info *cifs_sb;
1789 struct cifs_tcon *tcon;
1790 struct cifsFileInfo *cfile;
1796 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1797 cmd, flock->fl_flags, flock->fl_type,
1798 flock->fl_start, flock->fl_end);
1800 cfile = (struct cifsFileInfo *)file->private_data;
1801 tcon = tlink_tcon(cfile->tlink);
1803 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1805 cifs_sb = CIFS_FILE_SB(file);
1807 if (cap_unix(tcon->ses) &&
1808 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1809 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1812 * BB add code here to normalize offset and length to account for
1813 * negative length which we can not accept over the wire.
1815 if (IS_GETLK(cmd)) {
1816 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1821 if (!lock && !unlock) {
1823 * if no lock or unlock then nothing to do since we do not
1830 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1837 * update the file size (if needed) after a write. Should be called with
1838 * the inode->i_lock held
1841 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1842 unsigned int bytes_written)
1844 loff_t end_of_write = offset + bytes_written;
1846 if (end_of_write > cifsi->server_eof)
1847 cifsi->server_eof = end_of_write;
1851 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1852 size_t write_size, loff_t *offset)
1855 unsigned int bytes_written = 0;
1856 unsigned int total_written;
1857 struct cifs_tcon *tcon;
1858 struct TCP_Server_Info *server;
1860 struct dentry *dentry = open_file->dentry;
1861 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
1862 struct cifs_io_parms io_parms = {0};
1864 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1865 write_size, *offset, dentry);
1867 tcon = tlink_tcon(open_file->tlink);
1868 server = tcon->ses->server;
1870 if (!server->ops->sync_write)
1875 for (total_written = 0; write_size > total_written;
1876 total_written += bytes_written) {
1878 while (rc == -EAGAIN) {
1882 if (open_file->invalidHandle) {
1883 /* we could deadlock if we called
1884 filemap_fdatawait from here so tell
1885 reopen_file not to flush data to
1887 rc = cifs_reopen_file(open_file, false);
1892 len = min(server->ops->wp_retry_size(d_inode(dentry)),
1893 (unsigned int)write_size - total_written);
1894 /* iov[0] is reserved for smb header */
1895 iov[1].iov_base = (char *)write_data + total_written;
1896 iov[1].iov_len = len;
1898 io_parms.tcon = tcon;
1899 io_parms.offset = *offset;
1900 io_parms.length = len;
1901 rc = server->ops->sync_write(xid, &open_file->fid,
1902 &io_parms, &bytes_written, iov, 1);
1904 if (rc || (bytes_written == 0)) {
1912 spin_lock(&d_inode(dentry)->i_lock);
1913 cifs_update_eof(cifsi, *offset, bytes_written);
1914 spin_unlock(&d_inode(dentry)->i_lock);
1915 *offset += bytes_written;
1919 cifs_stats_bytes_written(tcon, total_written);
1921 if (total_written > 0) {
1922 spin_lock(&d_inode(dentry)->i_lock);
1923 if (*offset > d_inode(dentry)->i_size)
1924 i_size_write(d_inode(dentry), *offset);
1925 spin_unlock(&d_inode(dentry)->i_lock);
1927 mark_inode_dirty_sync(d_inode(dentry));
1929 return total_written;
1932 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1935 struct cifsFileInfo *open_file = NULL;
1936 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1938 /* only filter by fsuid on multiuser mounts */
1939 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1942 spin_lock(&cifs_inode->open_file_lock);
1943 /* we could simply get the first_list_entry since write-only entries
1944 are always at the end of the list but since the first entry might
1945 have a close pending, we go through the whole list */
1946 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1947 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
1949 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
1950 if (!open_file->invalidHandle) {
1951 /* found a good file */
1952 /* lock it so it will not be closed on us */
1953 cifsFileInfo_get(open_file);
1954 spin_unlock(&cifs_inode->open_file_lock);
1956 } /* else might as well continue, and look for
1957 another, or simply have the caller reopen it
1958 again rather than trying to fix this handle */
1959 } else /* write only file */
1960 break; /* write only files are last so must be done */
1962 spin_unlock(&cifs_inode->open_file_lock);
1966 /* Return -EBADF if no handle is found and general rc otherwise */
1968 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
1969 struct cifsFileInfo **ret_file)
1971 struct cifsFileInfo *open_file, *inv_file = NULL;
1972 struct cifs_sb_info *cifs_sb;
1973 bool any_available = false;
1975 unsigned int refind = 0;
1976 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
1977 bool with_delete = flags & FIND_WR_WITH_DELETE;
1981 * Having a null inode here (because mapping->host was set to zero by
1982 * the VFS or MM) should not happen but we had reports of on oops (due
1983 * to it being zero) during stress testcases so we need to check for it
1986 if (cifs_inode == NULL) {
1987 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
1992 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1994 /* only filter by fsuid on multiuser mounts */
1995 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1998 spin_lock(&cifs_inode->open_file_lock);
2000 if (refind > MAX_REOPEN_ATT) {
2001 spin_unlock(&cifs_inode->open_file_lock);
2004 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2005 if (!any_available && open_file->pid != current->tgid)
2007 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2009 if (with_delete && !(open_file->fid.access & DELETE))
2011 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2012 if (!open_file->invalidHandle) {
2013 /* found a good writable file */
2014 cifsFileInfo_get(open_file);
2015 spin_unlock(&cifs_inode->open_file_lock);
2016 *ret_file = open_file;
2020 inv_file = open_file;
2024 /* couldn't find useable FH with same pid, try any available */
2025 if (!any_available) {
2026 any_available = true;
2027 goto refind_writable;
2031 any_available = false;
2032 cifsFileInfo_get(inv_file);
2035 spin_unlock(&cifs_inode->open_file_lock);
2038 rc = cifs_reopen_file(inv_file, false);
2040 *ret_file = inv_file;
2044 spin_lock(&cifs_inode->open_file_lock);
2045 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2046 spin_unlock(&cifs_inode->open_file_lock);
2047 cifsFileInfo_put(inv_file);
2050 spin_lock(&cifs_inode->open_file_lock);
2051 goto refind_writable;
2057 struct cifsFileInfo *
2058 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2060 struct cifsFileInfo *cfile;
2063 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2065 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2071 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2073 struct cifsFileInfo **ret_file)
2075 struct cifsFileInfo *cfile;
2076 void *page = alloc_dentry_path();
2080 spin_lock(&tcon->open_file_lock);
2081 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2082 struct cifsInodeInfo *cinode;
2083 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2084 if (IS_ERR(full_path)) {
2085 spin_unlock(&tcon->open_file_lock);
2086 free_dentry_path(page);
2087 return PTR_ERR(full_path);
2089 if (strcmp(full_path, name))
2092 cinode = CIFS_I(d_inode(cfile->dentry));
2093 spin_unlock(&tcon->open_file_lock);
2094 free_dentry_path(page);
2095 return cifs_get_writable_file(cinode, flags, ret_file);
2098 spin_unlock(&tcon->open_file_lock);
2099 free_dentry_path(page);
2104 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2105 struct cifsFileInfo **ret_file)
2107 struct cifsFileInfo *cfile;
2108 void *page = alloc_dentry_path();
2112 spin_lock(&tcon->open_file_lock);
2113 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2114 struct cifsInodeInfo *cinode;
2115 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2116 if (IS_ERR(full_path)) {
2117 spin_unlock(&tcon->open_file_lock);
2118 free_dentry_path(page);
2119 return PTR_ERR(full_path);
2121 if (strcmp(full_path, name))
2124 cinode = CIFS_I(d_inode(cfile->dentry));
2125 spin_unlock(&tcon->open_file_lock);
2126 free_dentry_path(page);
2127 *ret_file = find_readable_file(cinode, 0);
2128 return *ret_file ? 0 : -ENOENT;
2131 spin_unlock(&tcon->open_file_lock);
2132 free_dentry_path(page);
2136 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2138 struct address_space *mapping = page->mapping;
2139 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2142 int bytes_written = 0;
2143 struct inode *inode;
2144 struct cifsFileInfo *open_file;
2146 if (!mapping || !mapping->host)
2149 inode = page->mapping->host;
2151 offset += (loff_t)from;
2152 write_data = kmap(page);
2155 if ((to > PAGE_SIZE) || (from > to)) {
2160 /* racing with truncate? */
2161 if (offset > mapping->host->i_size) {
2163 return 0; /* don't care */
2166 /* check to make sure that we are not extending the file */
2167 if (mapping->host->i_size - offset < (loff_t)to)
2168 to = (unsigned)(mapping->host->i_size - offset);
2170 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
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)
2296 wdata->sync_mode = wbc->sync_mode;
2297 wdata->nr_pages = nr_pages;
2298 wdata->offset = page_offset(wdata->pages[0]);
2299 wdata->pagesz = PAGE_SIZE;
2300 wdata->tailsz = min(i_size_read(mapping->host) -
2301 page_offset(wdata->pages[nr_pages - 1]),
2303 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2304 wdata->pid = wdata->cfile->pid;
2306 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2310 if (wdata->cfile->invalidHandle)
2313 rc = wdata->server->ops->async_writev(wdata,
2314 cifs_writedata_release);
2319 static int cifs_writepages(struct address_space *mapping,
2320 struct writeback_control *wbc)
2322 struct inode *inode = mapping->host;
2323 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2324 struct TCP_Server_Info *server;
2325 bool done = false, scanned = false, range_whole = false;
2327 struct cifs_writedata *wdata;
2328 struct cifsFileInfo *cfile = NULL;
2334 * If wsize is smaller than the page cache size, default to writing
2335 * one page at a time via cifs_writepage
2337 if (cifs_sb->ctx->wsize < PAGE_SIZE)
2338 return generic_writepages(mapping, wbc);
2341 if (wbc->range_cyclic) {
2342 index = mapping->writeback_index; /* Start from prev offset */
2345 index = wbc->range_start >> PAGE_SHIFT;
2346 end = wbc->range_end >> PAGE_SHIFT;
2347 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2351 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
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), FIND_WR_ANY, &cfile);
2366 /* in case of an error store it to return later */
2370 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->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;
2406 wdata->server = server;
2409 if (!wdata->cfile) {
2410 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2412 if (is_retryable_error(get_file_rc))
2417 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2419 for (i = 0; i < nr_pages; ++i)
2420 unlock_page(wdata->pages[i]);
2422 /* send failure -- clean up the mess */
2424 add_credits_and_wake_if(server, &wdata->credits, 0);
2425 for (i = 0; i < nr_pages; ++i) {
2426 if (is_retryable_error(rc))
2427 redirty_page_for_writepage(wbc,
2430 SetPageError(wdata->pages[i]);
2431 end_page_writeback(wdata->pages[i]);
2432 put_page(wdata->pages[i]);
2434 if (!is_retryable_error(rc))
2435 mapping_set_error(mapping, rc);
2437 kref_put(&wdata->refcount, cifs_writedata_release);
2439 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2440 index = saved_index;
2444 /* Return immediately if we received a signal during writing */
2445 if (is_interrupt_error(rc)) {
2450 if (rc != 0 && saved_rc == 0)
2453 wbc->nr_to_write -= nr_pages;
2454 if (wbc->nr_to_write <= 0)
2460 if (!scanned && !done) {
2462 * We hit the last page and there is more work to be done: wrap
2463 * back to the start of the file
2473 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2474 mapping->writeback_index = index;
2477 cifsFileInfo_put(cfile);
2483 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2489 /* BB add check for wbc flags */
2491 if (!PageUptodate(page))
2492 cifs_dbg(FYI, "ppw - page not up to date\n");
2495 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2497 * A writepage() implementation always needs to do either this,
2498 * or re-dirty the page with "redirty_page_for_writepage()" in
2499 * the case of a failure.
2501 * Just unlocking the page will cause the radix tree tag-bits
2502 * to fail to update with the state of the page correctly.
2504 set_page_writeback(page);
2506 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2507 if (is_retryable_error(rc)) {
2508 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2510 redirty_page_for_writepage(wbc, page);
2511 } else if (rc != 0) {
2513 mapping_set_error(page->mapping, rc);
2515 SetPageUptodate(page);
2517 end_page_writeback(page);
2523 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2525 int rc = cifs_writepage_locked(page, wbc);
2530 static int cifs_write_end(struct file *file, struct address_space *mapping,
2531 loff_t pos, unsigned len, unsigned copied,
2532 struct page *page, void *fsdata)
2535 struct inode *inode = mapping->host;
2536 struct cifsFileInfo *cfile = file->private_data;
2537 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2540 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2543 pid = current->tgid;
2545 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2548 if (PageChecked(page)) {
2550 SetPageUptodate(page);
2551 ClearPageChecked(page);
2552 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2553 SetPageUptodate(page);
2555 if (!PageUptodate(page)) {
2557 unsigned offset = pos & (PAGE_SIZE - 1);
2561 /* this is probably better than directly calling
2562 partialpage_write since in this function the file handle is
2563 known which we might as well leverage */
2564 /* BB check if anything else missing out of ppw
2565 such as updating last write time */
2566 page_data = kmap(page);
2567 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2568 /* if (rc < 0) should we set writebehind rc? */
2575 set_page_dirty(page);
2579 spin_lock(&inode->i_lock);
2580 if (pos > inode->i_size)
2581 i_size_write(inode, pos);
2582 spin_unlock(&inode->i_lock);
2591 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2596 struct cifs_tcon *tcon;
2597 struct TCP_Server_Info *server;
2598 struct cifsFileInfo *smbfile = file->private_data;
2599 struct inode *inode = file_inode(file);
2600 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2602 rc = file_write_and_wait_range(file, start, end);
2604 trace_cifs_fsync_err(inode->i_ino, rc);
2610 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2613 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2614 rc = cifs_zap_mapping(inode);
2616 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2617 rc = 0; /* don't care about it in fsync */
2621 tcon = tlink_tcon(smbfile->tlink);
2622 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2623 server = tcon->ses->server;
2624 if (server->ops->flush)
2625 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2634 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2638 struct cifs_tcon *tcon;
2639 struct TCP_Server_Info *server;
2640 struct cifsFileInfo *smbfile = file->private_data;
2641 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2643 rc = file_write_and_wait_range(file, start, end);
2645 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
2651 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2654 tcon = tlink_tcon(smbfile->tlink);
2655 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2656 server = tcon->ses->server;
2657 if (server->ops->flush)
2658 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2668 * As file closes, flush all cached write data for this inode checking
2669 * for write behind errors.
2671 int cifs_flush(struct file *file, fl_owner_t id)
2673 struct inode *inode = file_inode(file);
2676 if (file->f_mode & FMODE_WRITE)
2677 rc = filemap_write_and_wait(inode->i_mapping);
2679 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2681 trace_cifs_flush_err(inode->i_ino, rc);
2686 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2691 for (i = 0; i < num_pages; i++) {
2692 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2695 * save number of pages we have already allocated and
2696 * return with ENOMEM error
2705 for (i = 0; i < num_pages; i++)
2712 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2717 clen = min_t(const size_t, len, wsize);
2718 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
2727 cifs_uncached_writedata_release(struct kref *refcount)
2730 struct cifs_writedata *wdata = container_of(refcount,
2731 struct cifs_writedata, refcount);
2733 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
2734 for (i = 0; i < wdata->nr_pages; i++)
2735 put_page(wdata->pages[i]);
2736 cifs_writedata_release(refcount);
2739 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2742 cifs_uncached_writev_complete(struct work_struct *work)
2744 struct cifs_writedata *wdata = container_of(work,
2745 struct cifs_writedata, work);
2746 struct inode *inode = d_inode(wdata->cfile->dentry);
2747 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2749 spin_lock(&inode->i_lock);
2750 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2751 if (cifsi->server_eof > inode->i_size)
2752 i_size_write(inode, cifsi->server_eof);
2753 spin_unlock(&inode->i_lock);
2755 complete(&wdata->done);
2756 collect_uncached_write_data(wdata->ctx);
2757 /* the below call can possibly free the last ref to aio ctx */
2758 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2762 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2763 size_t *len, unsigned long *num_pages)
2765 size_t save_len, copied, bytes, cur_len = *len;
2766 unsigned long i, nr_pages = *num_pages;
2769 for (i = 0; i < nr_pages; i++) {
2770 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2771 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2774 * If we didn't copy as much as we expected, then that
2775 * may mean we trod into an unmapped area. Stop copying
2776 * at that point. On the next pass through the big
2777 * loop, we'll likely end up getting a zero-length
2778 * write and bailing out of it.
2783 cur_len = save_len - cur_len;
2787 * If we have no data to send, then that probably means that
2788 * the copy above failed altogether. That's most likely because
2789 * the address in the iovec was bogus. Return -EFAULT and let
2790 * the caller free anything we allocated and bail out.
2796 * i + 1 now represents the number of pages we actually used in
2797 * the copy phase above.
2804 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
2805 struct cifs_aio_ctx *ctx)
2808 struct cifs_credits credits;
2810 struct TCP_Server_Info *server = wdata->server;
2813 if (wdata->cfile->invalidHandle) {
2814 rc = cifs_reopen_file(wdata->cfile, false);
2823 * Wait for credits to resend this wdata.
2824 * Note: we are attempting to resend the whole wdata not in
2828 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
2833 if (wsize < wdata->bytes) {
2834 add_credits_and_wake_if(server, &credits, 0);
2837 } while (wsize < wdata->bytes);
2838 wdata->credits = credits;
2840 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
2843 if (wdata->cfile->invalidHandle)
2846 #ifdef CONFIG_CIFS_SMB_DIRECT
2848 wdata->mr->need_invalidate = true;
2849 smbd_deregister_mr(wdata->mr);
2853 rc = server->ops->async_writev(wdata,
2854 cifs_uncached_writedata_release);
2858 /* If the write was successfully sent, we are done */
2860 list_add_tail(&wdata->list, wdata_list);
2864 /* Roll back credits and retry if needed */
2865 add_credits_and_wake_if(server, &wdata->credits, 0);
2866 } while (rc == -EAGAIN);
2869 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
2874 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2875 struct cifsFileInfo *open_file,
2876 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2877 struct cifs_aio_ctx *ctx)
2881 unsigned long nr_pages, num_pages, i;
2882 struct cifs_writedata *wdata;
2883 struct iov_iter saved_from = *from;
2884 loff_t saved_offset = offset;
2886 struct TCP_Server_Info *server;
2887 struct page **pagevec;
2891 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2892 pid = open_file->pid;
2894 pid = current->tgid;
2896 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
2901 struct cifs_credits credits_on_stack;
2902 struct cifs_credits *credits = &credits_on_stack;
2904 if (open_file->invalidHandle) {
2905 rc = cifs_reopen_file(open_file, false);
2912 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2917 cur_len = min_t(const size_t, len, wsize);
2919 if (ctx->direct_io) {
2922 result = iov_iter_get_pages_alloc(
2923 from, &pagevec, cur_len, &start);
2926 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
2927 result, iov_iter_type(from),
2928 from->iov_offset, from->count);
2932 add_credits_and_wake_if(server, credits, 0);
2935 cur_len = (size_t)result;
2936 iov_iter_advance(from, cur_len);
2939 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
2941 wdata = cifs_writedata_direct_alloc(pagevec,
2942 cifs_uncached_writev_complete);
2945 add_credits_and_wake_if(server, credits, 0);
2950 wdata->page_offset = start;
2953 cur_len - (PAGE_SIZE - start) -
2954 (nr_pages - 2) * PAGE_SIZE :
2957 nr_pages = get_numpages(wsize, len, &cur_len);
2958 wdata = cifs_writedata_alloc(nr_pages,
2959 cifs_uncached_writev_complete);
2962 add_credits_and_wake_if(server, credits, 0);
2966 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2968 kvfree(wdata->pages);
2970 add_credits_and_wake_if(server, credits, 0);
2974 num_pages = nr_pages;
2975 rc = wdata_fill_from_iovec(
2976 wdata, from, &cur_len, &num_pages);
2978 for (i = 0; i < nr_pages; i++)
2979 put_page(wdata->pages[i]);
2980 kvfree(wdata->pages);
2982 add_credits_and_wake_if(server, credits, 0);
2987 * Bring nr_pages down to the number of pages we
2988 * actually used, and free any pages that we didn't use.
2990 for ( ; nr_pages > num_pages; nr_pages--)
2991 put_page(wdata->pages[nr_pages - 1]);
2993 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
2996 wdata->sync_mode = WB_SYNC_ALL;
2997 wdata->nr_pages = nr_pages;
2998 wdata->offset = (__u64)offset;
2999 wdata->cfile = cifsFileInfo_get(open_file);
3000 wdata->server = server;
3002 wdata->bytes = cur_len;
3003 wdata->pagesz = PAGE_SIZE;
3004 wdata->credits = credits_on_stack;
3006 kref_get(&ctx->refcount);
3008 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3011 if (wdata->cfile->invalidHandle)
3014 rc = server->ops->async_writev(wdata,
3015 cifs_uncached_writedata_release);
3019 add_credits_and_wake_if(server, &wdata->credits, 0);
3020 kref_put(&wdata->refcount,
3021 cifs_uncached_writedata_release);
3022 if (rc == -EAGAIN) {
3024 iov_iter_advance(from, offset - saved_offset);
3030 list_add_tail(&wdata->list, wdata_list);
3039 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3041 struct cifs_writedata *wdata, *tmp;
3042 struct cifs_tcon *tcon;
3043 struct cifs_sb_info *cifs_sb;
3044 struct dentry *dentry = ctx->cfile->dentry;
3047 tcon = tlink_tcon(ctx->cfile->tlink);
3048 cifs_sb = CIFS_SB(dentry->d_sb);
3050 mutex_lock(&ctx->aio_mutex);
3052 if (list_empty(&ctx->list)) {
3053 mutex_unlock(&ctx->aio_mutex);
3059 * Wait for and collect replies for any successful sends in order of
3060 * increasing offset. Once an error is hit, then return without waiting
3061 * for any more replies.
3064 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3066 if (!try_wait_for_completion(&wdata->done)) {
3067 mutex_unlock(&ctx->aio_mutex);
3074 ctx->total_len += wdata->bytes;
3076 /* resend call if it's a retryable error */
3077 if (rc == -EAGAIN) {
3078 struct list_head tmp_list;
3079 struct iov_iter tmp_from = ctx->iter;
3081 INIT_LIST_HEAD(&tmp_list);
3082 list_del_init(&wdata->list);
3085 rc = cifs_resend_wdata(
3086 wdata, &tmp_list, ctx);
3088 iov_iter_advance(&tmp_from,
3089 wdata->offset - ctx->pos);
3091 rc = cifs_write_from_iter(wdata->offset,
3092 wdata->bytes, &tmp_from,
3093 ctx->cfile, cifs_sb, &tmp_list,
3096 kref_put(&wdata->refcount,
3097 cifs_uncached_writedata_release);
3100 list_splice(&tmp_list, &ctx->list);
3104 list_del_init(&wdata->list);
3105 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3108 cifs_stats_bytes_written(tcon, ctx->total_len);
3109 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3111 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3113 mutex_unlock(&ctx->aio_mutex);
3115 if (ctx->iocb && ctx->iocb->ki_complete)
3116 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3118 complete(&ctx->done);
3121 static ssize_t __cifs_writev(
3122 struct kiocb *iocb, struct iov_iter *from, bool direct)
3124 struct file *file = iocb->ki_filp;
3125 ssize_t total_written = 0;
3126 struct cifsFileInfo *cfile;
3127 struct cifs_tcon *tcon;
3128 struct cifs_sb_info *cifs_sb;
3129 struct cifs_aio_ctx *ctx;
3130 struct iov_iter saved_from = *from;
3131 size_t len = iov_iter_count(from);
3135 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3136 * In this case, fall back to non-direct write function.
3137 * this could be improved by getting pages directly in ITER_KVEC
3139 if (direct && iov_iter_is_kvec(from)) {
3140 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3144 rc = generic_write_checks(iocb, from);
3148 cifs_sb = CIFS_FILE_SB(file);
3149 cfile = file->private_data;
3150 tcon = tlink_tcon(cfile->tlink);
3152 if (!tcon->ses->server->ops->async_writev)
3155 ctx = cifs_aio_ctx_alloc();
3159 ctx->cfile = cifsFileInfo_get(cfile);
3161 if (!is_sync_kiocb(iocb))
3164 ctx->pos = iocb->ki_pos;
3167 ctx->direct_io = true;
3171 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3173 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3178 /* grab a lock here due to read response handlers can access ctx */
3179 mutex_lock(&ctx->aio_mutex);
3181 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3182 cfile, cifs_sb, &ctx->list, ctx);
3185 * If at least one write was successfully sent, then discard any rc
3186 * value from the later writes. If the other write succeeds, then
3187 * we'll end up returning whatever was written. If it fails, then
3188 * we'll get a new rc value from that.
3190 if (!list_empty(&ctx->list))
3193 mutex_unlock(&ctx->aio_mutex);
3196 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3200 if (!is_sync_kiocb(iocb)) {
3201 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3202 return -EIOCBQUEUED;
3205 rc = wait_for_completion_killable(&ctx->done);
3207 mutex_lock(&ctx->aio_mutex);
3208 ctx->rc = rc = -EINTR;
3209 total_written = ctx->total_len;
3210 mutex_unlock(&ctx->aio_mutex);
3213 total_written = ctx->total_len;
3216 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3218 if (unlikely(!total_written))
3221 iocb->ki_pos += total_written;
3222 return total_written;
3225 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3227 return __cifs_writev(iocb, from, true);
3230 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3232 return __cifs_writev(iocb, from, false);
3236 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3238 struct file *file = iocb->ki_filp;
3239 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3240 struct inode *inode = file->f_mapping->host;
3241 struct cifsInodeInfo *cinode = CIFS_I(inode);
3242 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3247 * We need to hold the sem to be sure nobody modifies lock list
3248 * with a brlock that prevents writing.
3250 down_read(&cinode->lock_sem);
3252 rc = generic_write_checks(iocb, from);
3256 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3257 server->vals->exclusive_lock_type, 0,
3258 NULL, CIFS_WRITE_OP))
3259 rc = __generic_file_write_iter(iocb, from);
3263 up_read(&cinode->lock_sem);
3264 inode_unlock(inode);
3267 rc = generic_write_sync(iocb, rc);
3272 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3274 struct inode *inode = file_inode(iocb->ki_filp);
3275 struct cifsInodeInfo *cinode = CIFS_I(inode);
3276 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3277 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3278 iocb->ki_filp->private_data;
3279 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3282 written = cifs_get_writer(cinode);
3286 if (CIFS_CACHE_WRITE(cinode)) {
3287 if (cap_unix(tcon->ses) &&
3288 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3289 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3290 written = generic_file_write_iter(iocb, from);
3293 written = cifs_writev(iocb, from);
3297 * For non-oplocked files in strict cache mode we need to write the data
3298 * to the server exactly from the pos to pos+len-1 rather than flush all
3299 * affected pages because it may cause a error with mandatory locks on
3300 * these pages but not on the region from pos to ppos+len-1.
3302 written = cifs_user_writev(iocb, from);
3303 if (CIFS_CACHE_READ(cinode)) {
3305 * We have read level caching and we have just sent a write
3306 * request to the server thus making data in the cache stale.
3307 * Zap the cache and set oplock/lease level to NONE to avoid
3308 * reading stale data from the cache. All subsequent read
3309 * operations will read new data from the server.
3311 cifs_zap_mapping(inode);
3312 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3317 cifs_put_writer(cinode);
3321 static struct cifs_readdata *
3322 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3324 struct cifs_readdata *rdata;
3326 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3327 if (rdata != NULL) {
3328 rdata->pages = pages;
3329 kref_init(&rdata->refcount);
3330 INIT_LIST_HEAD(&rdata->list);
3331 init_completion(&rdata->done);
3332 INIT_WORK(&rdata->work, complete);
3338 static struct cifs_readdata *
3339 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3341 struct page **pages =
3342 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3343 struct cifs_readdata *ret = NULL;
3346 ret = cifs_readdata_direct_alloc(pages, complete);
3355 cifs_readdata_release(struct kref *refcount)
3357 struct cifs_readdata *rdata = container_of(refcount,
3358 struct cifs_readdata, refcount);
3359 #ifdef CONFIG_CIFS_SMB_DIRECT
3361 smbd_deregister_mr(rdata->mr);
3366 cifsFileInfo_put(rdata->cfile);
3368 kvfree(rdata->pages);
3373 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3379 for (i = 0; i < nr_pages; i++) {
3380 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3385 rdata->pages[i] = page;
3389 unsigned int nr_page_failed = i;
3391 for (i = 0; i < nr_page_failed; i++) {
3392 put_page(rdata->pages[i]);
3393 rdata->pages[i] = NULL;
3400 cifs_uncached_readdata_release(struct kref *refcount)
3402 struct cifs_readdata *rdata = container_of(refcount,
3403 struct cifs_readdata, refcount);
3406 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3407 for (i = 0; i < rdata->nr_pages; i++) {
3408 put_page(rdata->pages[i]);
3410 cifs_readdata_release(refcount);
3414 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3415 * @rdata: the readdata response with list of pages holding data
3416 * @iter: destination for our data
3418 * This function copies data from a list of pages in a readdata response into
3419 * an array of iovecs. It will first calculate where the data should go
3420 * based on the info in the readdata and then copy the data into that spot.
3423 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3425 size_t remaining = rdata->got_bytes;
3428 for (i = 0; i < rdata->nr_pages; i++) {
3429 struct page *page = rdata->pages[i];
3430 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3433 if (unlikely(iov_iter_is_pipe(iter))) {
3434 void *addr = kmap_atomic(page);
3436 written = copy_to_iter(addr, copy, iter);
3437 kunmap_atomic(addr);
3439 written = copy_page_to_iter(page, 0, copy, iter);
3440 remaining -= written;
3441 if (written < copy && iov_iter_count(iter) > 0)
3444 return remaining ? -EFAULT : 0;
3447 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3450 cifs_uncached_readv_complete(struct work_struct *work)
3452 struct cifs_readdata *rdata = container_of(work,
3453 struct cifs_readdata, work);
3455 complete(&rdata->done);
3456 collect_uncached_read_data(rdata->ctx);
3457 /* the below call can possibly free the last ref to aio ctx */
3458 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3462 uncached_fill_pages(struct TCP_Server_Info *server,
3463 struct cifs_readdata *rdata, struct iov_iter *iter,
3468 unsigned int nr_pages = rdata->nr_pages;
3469 unsigned int page_offset = rdata->page_offset;
3471 rdata->got_bytes = 0;
3472 rdata->tailsz = PAGE_SIZE;
3473 for (i = 0; i < nr_pages; i++) {
3474 struct page *page = rdata->pages[i];
3476 unsigned int segment_size = rdata->pagesz;
3479 segment_size -= page_offset;
3485 /* no need to hold page hostage */
3486 rdata->pages[i] = NULL;
3493 if (len >= segment_size)
3494 /* enough data to fill the page */
3497 rdata->tailsz = len;
3501 result = copy_page_from_iter(
3502 page, page_offset, n, iter);
3503 #ifdef CONFIG_CIFS_SMB_DIRECT
3508 result = cifs_read_page_from_socket(
3509 server, page, page_offset, n);
3513 rdata->got_bytes += result;
3516 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3517 rdata->got_bytes : result;
3521 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3522 struct cifs_readdata *rdata, unsigned int len)
3524 return uncached_fill_pages(server, rdata, NULL, len);
3528 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3529 struct cifs_readdata *rdata,
3530 struct iov_iter *iter)
3532 return uncached_fill_pages(server, rdata, iter, iter->count);
3535 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3536 struct list_head *rdata_list,
3537 struct cifs_aio_ctx *ctx)
3540 struct cifs_credits credits;
3542 struct TCP_Server_Info *server;
3544 /* XXX: should we pick a new channel here? */
3545 server = rdata->server;
3548 if (rdata->cfile->invalidHandle) {
3549 rc = cifs_reopen_file(rdata->cfile, true);
3557 * Wait for credits to resend this rdata.
3558 * Note: we are attempting to resend the whole rdata not in
3562 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3568 if (rsize < rdata->bytes) {
3569 add_credits_and_wake_if(server, &credits, 0);
3572 } while (rsize < rdata->bytes);
3573 rdata->credits = credits;
3575 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3577 if (rdata->cfile->invalidHandle)
3580 #ifdef CONFIG_CIFS_SMB_DIRECT
3582 rdata->mr->need_invalidate = true;
3583 smbd_deregister_mr(rdata->mr);
3587 rc = server->ops->async_readv(rdata);
3591 /* If the read was successfully sent, we are done */
3593 /* Add to aio pending list */
3594 list_add_tail(&rdata->list, rdata_list);
3598 /* Roll back credits and retry if needed */
3599 add_credits_and_wake_if(server, &rdata->credits, 0);
3600 } while (rc == -EAGAIN);
3603 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3608 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3609 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3610 struct cifs_aio_ctx *ctx)
3612 struct cifs_readdata *rdata;
3613 unsigned int npages, rsize;
3614 struct cifs_credits credits_on_stack;
3615 struct cifs_credits *credits = &credits_on_stack;
3619 struct TCP_Server_Info *server;
3620 struct page **pagevec;
3622 struct iov_iter direct_iov = ctx->iter;
3624 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3626 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3627 pid = open_file->pid;
3629 pid = current->tgid;
3632 iov_iter_advance(&direct_iov, offset - ctx->pos);
3635 if (open_file->invalidHandle) {
3636 rc = cifs_reopen_file(open_file, true);
3643 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
3648 cur_len = min_t(const size_t, len, rsize);
3650 if (ctx->direct_io) {
3653 result = iov_iter_get_pages_alloc(
3654 &direct_iov, &pagevec,
3658 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3659 result, iov_iter_type(&direct_iov),
3660 direct_iov.iov_offset,
3665 add_credits_and_wake_if(server, credits, 0);
3668 cur_len = (size_t)result;
3669 iov_iter_advance(&direct_iov, cur_len);
3671 rdata = cifs_readdata_direct_alloc(
3672 pagevec, cifs_uncached_readv_complete);
3674 add_credits_and_wake_if(server, credits, 0);
3679 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
3680 rdata->page_offset = start;
3681 rdata->tailsz = npages > 1 ?
3682 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
3687 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
3688 /* allocate a readdata struct */
3689 rdata = cifs_readdata_alloc(npages,
3690 cifs_uncached_readv_complete);
3692 add_credits_and_wake_if(server, credits, 0);
3697 rc = cifs_read_allocate_pages(rdata, npages);
3699 kvfree(rdata->pages);
3701 add_credits_and_wake_if(server, credits, 0);
3705 rdata->tailsz = PAGE_SIZE;
3708 rdata->server = server;
3709 rdata->cfile = cifsFileInfo_get(open_file);
3710 rdata->nr_pages = npages;
3711 rdata->offset = offset;
3712 rdata->bytes = cur_len;
3714 rdata->pagesz = PAGE_SIZE;
3715 rdata->read_into_pages = cifs_uncached_read_into_pages;
3716 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
3717 rdata->credits = credits_on_stack;
3719 kref_get(&ctx->refcount);
3721 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3724 if (rdata->cfile->invalidHandle)
3727 rc = server->ops->async_readv(rdata);
3731 add_credits_and_wake_if(server, &rdata->credits, 0);
3732 kref_put(&rdata->refcount,
3733 cifs_uncached_readdata_release);
3734 if (rc == -EAGAIN) {
3735 iov_iter_revert(&direct_iov, cur_len);
3741 list_add_tail(&rdata->list, rdata_list);
3750 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
3752 struct cifs_readdata *rdata, *tmp;
3753 struct iov_iter *to = &ctx->iter;
3754 struct cifs_sb_info *cifs_sb;
3757 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
3759 mutex_lock(&ctx->aio_mutex);
3761 if (list_empty(&ctx->list)) {
3762 mutex_unlock(&ctx->aio_mutex);
3767 /* the loop below should proceed in the order of increasing offsets */
3769 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
3771 if (!try_wait_for_completion(&rdata->done)) {
3772 mutex_unlock(&ctx->aio_mutex);
3776 if (rdata->result == -EAGAIN) {
3777 /* resend call if it's a retryable error */
3778 struct list_head tmp_list;
3779 unsigned int got_bytes = rdata->got_bytes;
3781 list_del_init(&rdata->list);
3782 INIT_LIST_HEAD(&tmp_list);
3785 * Got a part of data and then reconnect has
3786 * happened -- fill the buffer and continue
3789 if (got_bytes && got_bytes < rdata->bytes) {
3791 if (!ctx->direct_io)
3792 rc = cifs_readdata_to_iov(rdata, to);
3794 kref_put(&rdata->refcount,
3795 cifs_uncached_readdata_release);
3800 if (ctx->direct_io) {
3802 * Re-use rdata as this is a
3805 rc = cifs_resend_rdata(
3809 rc = cifs_send_async_read(
3810 rdata->offset + got_bytes,
3811 rdata->bytes - got_bytes,
3812 rdata->cfile, cifs_sb,
3815 kref_put(&rdata->refcount,
3816 cifs_uncached_readdata_release);
3819 list_splice(&tmp_list, &ctx->list);
3822 } else if (rdata->result)
3824 else if (!ctx->direct_io)
3825 rc = cifs_readdata_to_iov(rdata, to);
3827 /* if there was a short read -- discard anything left */
3828 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3831 ctx->total_len += rdata->got_bytes;
3833 list_del_init(&rdata->list);
3834 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3837 if (!ctx->direct_io)
3838 ctx->total_len = ctx->len - iov_iter_count(to);
3840 /* mask nodata case */
3844 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
3846 mutex_unlock(&ctx->aio_mutex);
3848 if (ctx->iocb && ctx->iocb->ki_complete)
3849 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3851 complete(&ctx->done);
3854 static ssize_t __cifs_readv(
3855 struct kiocb *iocb, struct iov_iter *to, bool direct)
3858 struct file *file = iocb->ki_filp;
3859 struct cifs_sb_info *cifs_sb;
3860 struct cifsFileInfo *cfile;
3861 struct cifs_tcon *tcon;
3862 ssize_t rc, total_read = 0;
3863 loff_t offset = iocb->ki_pos;
3864 struct cifs_aio_ctx *ctx;
3867 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
3868 * fall back to data copy read path
3869 * this could be improved by getting pages directly in ITER_KVEC
3871 if (direct && iov_iter_is_kvec(to)) {
3872 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
3876 len = iov_iter_count(to);
3880 cifs_sb = CIFS_FILE_SB(file);
3881 cfile = file->private_data;
3882 tcon = tlink_tcon(cfile->tlink);
3884 if (!tcon->ses->server->ops->async_readv)
3887 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3888 cifs_dbg(FYI, "attempting read on write only file instance\n");
3890 ctx = cifs_aio_ctx_alloc();
3894 ctx->cfile = cifsFileInfo_get(cfile);
3896 if (!is_sync_kiocb(iocb))
3899 if (iter_is_iovec(to))
3900 ctx->should_dirty = true;
3904 ctx->direct_io = true;
3908 rc = setup_aio_ctx_iter(ctx, to, READ);
3910 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3916 /* grab a lock here due to read response handlers can access ctx */
3917 mutex_lock(&ctx->aio_mutex);
3919 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
3921 /* if at least one read request send succeeded, then reset rc */
3922 if (!list_empty(&ctx->list))
3925 mutex_unlock(&ctx->aio_mutex);
3928 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3932 if (!is_sync_kiocb(iocb)) {
3933 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3934 return -EIOCBQUEUED;
3937 rc = wait_for_completion_killable(&ctx->done);
3939 mutex_lock(&ctx->aio_mutex);
3940 ctx->rc = rc = -EINTR;
3941 total_read = ctx->total_len;
3942 mutex_unlock(&ctx->aio_mutex);
3945 total_read = ctx->total_len;
3948 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3951 iocb->ki_pos += total_read;
3957 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
3959 return __cifs_readv(iocb, to, true);
3962 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
3964 return __cifs_readv(iocb, to, false);
3968 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
3970 struct inode *inode = file_inode(iocb->ki_filp);
3971 struct cifsInodeInfo *cinode = CIFS_I(inode);
3972 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3973 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3974 iocb->ki_filp->private_data;
3975 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3979 * In strict cache mode we need to read from the server all the time
3980 * if we don't have level II oplock because the server can delay mtime
3981 * change - so we can't make a decision about inode invalidating.
3982 * And we can also fail with pagereading if there are mandatory locks
3983 * on pages affected by this read but not on the region from pos to
3986 if (!CIFS_CACHE_READ(cinode))
3987 return cifs_user_readv(iocb, to);
3989 if (cap_unix(tcon->ses) &&
3990 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3991 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
3992 return generic_file_read_iter(iocb, to);
3995 * We need to hold the sem to be sure nobody modifies lock list
3996 * with a brlock that prevents reading.
3998 down_read(&cinode->lock_sem);
3999 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4000 tcon->ses->server->vals->shared_lock_type,
4001 0, NULL, CIFS_READ_OP))
4002 rc = generic_file_read_iter(iocb, to);
4003 up_read(&cinode->lock_sem);
4008 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4011 unsigned int bytes_read = 0;
4012 unsigned int total_read;
4013 unsigned int current_read_size;
4015 struct cifs_sb_info *cifs_sb;
4016 struct cifs_tcon *tcon;
4017 struct TCP_Server_Info *server;
4020 struct cifsFileInfo *open_file;
4021 struct cifs_io_parms io_parms = {0};
4022 int buf_type = CIFS_NO_BUFFER;
4026 cifs_sb = CIFS_FILE_SB(file);
4028 /* FIXME: set up handlers for larger reads and/or convert to async */
4029 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4031 if (file->private_data == NULL) {
4036 open_file = file->private_data;
4037 tcon = tlink_tcon(open_file->tlink);
4038 server = cifs_pick_channel(tcon->ses);
4040 if (!server->ops->sync_read) {
4045 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4046 pid = open_file->pid;
4048 pid = current->tgid;
4050 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4051 cifs_dbg(FYI, "attempting read on write only file instance\n");
4053 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4054 total_read += bytes_read, cur_offset += bytes_read) {
4056 current_read_size = min_t(uint, read_size - total_read,
4059 * For windows me and 9x we do not want to request more
4060 * than it negotiated since it will refuse the read
4063 if (!(tcon->ses->capabilities &
4064 tcon->ses->server->vals->cap_large_files)) {
4065 current_read_size = min_t(uint,
4066 current_read_size, CIFSMaxBufSize);
4068 if (open_file->invalidHandle) {
4069 rc = cifs_reopen_file(open_file, true);
4074 io_parms.tcon = tcon;
4075 io_parms.offset = *offset;
4076 io_parms.length = current_read_size;
4077 io_parms.server = server;
4078 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4079 &bytes_read, &cur_offset,
4081 } while (rc == -EAGAIN);
4083 if (rc || (bytes_read == 0)) {
4091 cifs_stats_bytes_read(tcon, total_read);
4092 *offset += bytes_read;
4100 * If the page is mmap'ed into a process' page tables, then we need to make
4101 * sure that it doesn't change while being written back.
4104 cifs_page_mkwrite(struct vm_fault *vmf)
4106 struct page *page = vmf->page;
4109 return VM_FAULT_LOCKED;
4112 static const struct vm_operations_struct cifs_file_vm_ops = {
4113 .fault = filemap_fault,
4114 .map_pages = filemap_map_pages,
4115 .page_mkwrite = cifs_page_mkwrite,
4118 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4121 struct inode *inode = file_inode(file);
4125 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4126 rc = cifs_zap_mapping(inode);
4128 rc = generic_file_mmap(file, vma);
4130 vma->vm_ops = &cifs_file_vm_ops;
4136 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4142 rc = cifs_revalidate_file(file);
4144 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4147 rc = generic_file_mmap(file, vma);
4149 vma->vm_ops = &cifs_file_vm_ops;
4156 cifs_readv_complete(struct work_struct *work)
4158 unsigned int i, got_bytes;
4159 struct cifs_readdata *rdata = container_of(work,
4160 struct cifs_readdata, work);
4162 got_bytes = rdata->got_bytes;
4163 for (i = 0; i < rdata->nr_pages; i++) {
4164 struct page *page = rdata->pages[i];
4166 lru_cache_add(page);
4168 if (rdata->result == 0 ||
4169 (rdata->result == -EAGAIN && got_bytes)) {
4170 flush_dcache_page(page);
4171 SetPageUptodate(page);
4176 if (rdata->result == 0 ||
4177 (rdata->result == -EAGAIN && got_bytes))
4178 cifs_readpage_to_fscache(rdata->mapping->host, page);
4180 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4183 rdata->pages[i] = NULL;
4185 kref_put(&rdata->refcount, cifs_readdata_release);
4189 readpages_fill_pages(struct TCP_Server_Info *server,
4190 struct cifs_readdata *rdata, struct iov_iter *iter,
4197 unsigned int nr_pages = rdata->nr_pages;
4198 unsigned int page_offset = rdata->page_offset;
4200 /* determine the eof that the server (probably) has */
4201 eof = CIFS_I(rdata->mapping->host)->server_eof;
4202 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4203 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4205 rdata->got_bytes = 0;
4206 rdata->tailsz = PAGE_SIZE;
4207 for (i = 0; i < nr_pages; i++) {
4208 struct page *page = rdata->pages[i];
4209 unsigned int to_read = rdata->pagesz;
4213 to_read -= page_offset;
4219 if (len >= to_read) {
4221 } else if (len > 0) {
4222 /* enough for partial page, fill and zero the rest */
4223 zero_user(page, len + page_offset, to_read - len);
4224 n = rdata->tailsz = len;
4226 } else if (page->index > eof_index) {
4228 * The VFS will not try to do readahead past the
4229 * i_size, but it's possible that we have outstanding
4230 * writes with gaps in the middle and the i_size hasn't
4231 * caught up yet. Populate those with zeroed out pages
4232 * to prevent the VFS from repeatedly attempting to
4233 * fill them until the writes are flushed.
4235 zero_user(page, 0, PAGE_SIZE);
4236 lru_cache_add(page);
4237 flush_dcache_page(page);
4238 SetPageUptodate(page);
4241 rdata->pages[i] = NULL;
4245 /* no need to hold page hostage */
4246 lru_cache_add(page);
4249 rdata->pages[i] = NULL;
4255 result = copy_page_from_iter(
4256 page, page_offset, n, iter);
4257 #ifdef CONFIG_CIFS_SMB_DIRECT
4262 result = cifs_read_page_from_socket(
4263 server, page, page_offset, n);
4267 rdata->got_bytes += result;
4270 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
4271 rdata->got_bytes : result;
4275 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4276 struct cifs_readdata *rdata, unsigned int len)
4278 return readpages_fill_pages(server, rdata, NULL, len);
4282 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4283 struct cifs_readdata *rdata,
4284 struct iov_iter *iter)
4286 return readpages_fill_pages(server, rdata, iter, iter->count);
4290 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4291 unsigned int rsize, struct list_head *tmplist,
4292 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4294 struct page *page, *tpage;
4295 unsigned int expected_index;
4297 gfp_t gfp = readahead_gfp_mask(mapping);
4299 INIT_LIST_HEAD(tmplist);
4301 page = lru_to_page(page_list);
4304 * Lock the page and put it in the cache. Since no one else
4305 * should have access to this page, we're safe to simply set
4306 * PG_locked without checking it first.
4308 __SetPageLocked(page);
4309 rc = add_to_page_cache_locked(page, mapping,
4312 /* give up if we can't stick it in the cache */
4314 __ClearPageLocked(page);
4318 /* move first page to the tmplist */
4319 *offset = (loff_t)page->index << PAGE_SHIFT;
4322 list_move_tail(&page->lru, tmplist);
4324 /* now try and add more pages onto the request */
4325 expected_index = page->index + 1;
4326 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4327 /* discontinuity ? */
4328 if (page->index != expected_index)
4331 /* would this page push the read over the rsize? */
4332 if (*bytes + PAGE_SIZE > rsize)
4335 __SetPageLocked(page);
4336 rc = add_to_page_cache_locked(page, mapping, page->index, gfp);
4338 __ClearPageLocked(page);
4341 list_move_tail(&page->lru, tmplist);
4342 (*bytes) += PAGE_SIZE;
4349 static int cifs_readpages(struct file *file, struct address_space *mapping,
4350 struct list_head *page_list, unsigned num_pages)
4354 struct list_head tmplist;
4355 struct cifsFileInfo *open_file = file->private_data;
4356 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4357 struct TCP_Server_Info *server;
4363 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4364 * immediately if the cookie is negative
4366 * After this point, every page in the list might have PG_fscache set,
4367 * so we will need to clean that up off of every page we don't use.
4369 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4376 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4377 pid = open_file->pid;
4379 pid = current->tgid;
4382 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4384 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4385 __func__, file, mapping, num_pages);
4388 * Start with the page at end of list and move it to private
4389 * list. Do the same with any following pages until we hit
4390 * the rsize limit, hit an index discontinuity, or run out of
4391 * pages. Issue the async read and then start the loop again
4392 * until the list is empty.
4394 * Note that list order is important. The page_list is in
4395 * the order of declining indexes. When we put the pages in
4396 * the rdata->pages, then we want them in increasing order.
4398 while (!list_empty(page_list) && !err) {
4399 unsigned int i, nr_pages, bytes, rsize;
4401 struct page *page, *tpage;
4402 struct cifs_readdata *rdata;
4403 struct cifs_credits credits_on_stack;
4404 struct cifs_credits *credits = &credits_on_stack;
4406 if (open_file->invalidHandle) {
4407 rc = cifs_reopen_file(open_file, true);
4414 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4420 * Give up immediately if rsize is too small to read an entire
4421 * page. The VFS will fall back to readpage. We should never
4422 * reach this point however since we set ra_pages to 0 when the
4423 * rsize is smaller than a cache page.
4425 if (unlikely(rsize < PAGE_SIZE)) {
4426 add_credits_and_wake_if(server, credits, 0);
4432 err = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4433 &nr_pages, &offset, &bytes);
4435 add_credits_and_wake_if(server, credits, 0);
4439 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4441 /* best to give up if we're out of mem */
4442 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4443 list_del(&page->lru);
4444 lru_cache_add(page);
4449 add_credits_and_wake_if(server, credits, 0);
4453 rdata->cfile = cifsFileInfo_get(open_file);
4454 rdata->server = server;
4455 rdata->mapping = mapping;
4456 rdata->offset = offset;
4457 rdata->bytes = bytes;
4459 rdata->pagesz = PAGE_SIZE;
4460 rdata->tailsz = PAGE_SIZE;
4461 rdata->read_into_pages = cifs_readpages_read_into_pages;
4462 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4463 rdata->credits = credits_on_stack;
4465 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4466 list_del(&page->lru);
4467 rdata->pages[rdata->nr_pages++] = page;
4470 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4473 if (rdata->cfile->invalidHandle)
4476 rc = server->ops->async_readv(rdata);
4480 add_credits_and_wake_if(server, &rdata->credits, 0);
4481 for (i = 0; i < rdata->nr_pages; i++) {
4482 page = rdata->pages[i];
4483 lru_cache_add(page);
4487 /* Fallback to the readpage in error/reconnect cases */
4488 kref_put(&rdata->refcount, cifs_readdata_release);
4492 kref_put(&rdata->refcount, cifs_readdata_release);
4495 /* Any pages that have been shown to fscache but didn't get added to
4496 * the pagecache must be uncached before they get returned to the
4499 cifs_fscache_readpages_cancel(mapping->host, page_list);
4505 * cifs_readpage_worker must be called with the page pinned
4507 static int cifs_readpage_worker(struct file *file, struct page *page,
4513 /* Is the page cached? */
4514 rc = cifs_readpage_from_fscache(file_inode(file), page);
4518 read_data = kmap(page);
4519 /* for reads over a certain size could initiate async read ahead */
4521 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4526 cifs_dbg(FYI, "Bytes read %d\n", rc);
4528 /* we do not want atime to be less than mtime, it broke some apps */
4529 file_inode(file)->i_atime = current_time(file_inode(file));
4530 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4531 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4533 file_inode(file)->i_atime = current_time(file_inode(file));
4536 memset(read_data + rc, 0, PAGE_SIZE - rc);
4538 flush_dcache_page(page);
4539 SetPageUptodate(page);
4541 /* send this page to the cache */
4542 cifs_readpage_to_fscache(file_inode(file), page);
4554 static int cifs_readpage(struct file *file, struct page *page)
4556 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
4562 if (file->private_data == NULL) {
4568 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4569 page, (int)offset, (int)offset);
4571 rc = cifs_readpage_worker(file, page, &offset);
4577 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4579 struct cifsFileInfo *open_file;
4581 spin_lock(&cifs_inode->open_file_lock);
4582 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4583 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4584 spin_unlock(&cifs_inode->open_file_lock);
4588 spin_unlock(&cifs_inode->open_file_lock);
4592 /* We do not want to update the file size from server for inodes
4593 open for write - to avoid races with writepage extending
4594 the file - in the future we could consider allowing
4595 refreshing the inode only on increases in the file size
4596 but this is tricky to do without racing with writebehind
4597 page caching in the current Linux kernel design */
4598 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4603 if (is_inode_writable(cifsInode)) {
4604 /* This inode is open for write at least once */
4605 struct cifs_sb_info *cifs_sb;
4607 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4608 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4609 /* since no page cache to corrupt on directio
4610 we can change size safely */
4614 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4622 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4623 loff_t pos, unsigned len, unsigned flags,
4624 struct page **pagep, void **fsdata)
4627 pgoff_t index = pos >> PAGE_SHIFT;
4628 loff_t offset = pos & (PAGE_SIZE - 1);
4629 loff_t page_start = pos & PAGE_MASK;
4634 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4637 page = grab_cache_page_write_begin(mapping, index, flags);
4643 if (PageUptodate(page))
4647 * If we write a full page it will be up to date, no need to read from
4648 * the server. If the write is short, we'll end up doing a sync write
4651 if (len == PAGE_SIZE)
4655 * optimize away the read when we have an oplock, and we're not
4656 * expecting to use any of the data we'd be reading in. That
4657 * is, when the page lies beyond the EOF, or straddles the EOF
4658 * and the write will cover all of the existing data.
4660 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4661 i_size = i_size_read(mapping->host);
4662 if (page_start >= i_size ||
4663 (offset == 0 && (pos + len) >= i_size)) {
4664 zero_user_segments(page, 0, offset,
4668 * PageChecked means that the parts of the page
4669 * to which we're not writing are considered up
4670 * to date. Once the data is copied to the
4671 * page, it can be set uptodate.
4673 SetPageChecked(page);
4678 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4680 * might as well read a page, it is fast enough. If we get
4681 * an error, we don't need to return it. cifs_write_end will
4682 * do a sync write instead since PG_uptodate isn't set.
4684 cifs_readpage_worker(file, page, &page_start);
4689 /* we could try using another file handle if there is one -
4690 but how would we lock it to prevent close of that handle
4691 racing with this read? In any case
4692 this will be written out by write_end so is fine */
4699 static int cifs_release_page(struct page *page, gfp_t gfp)
4701 if (PagePrivate(page))
4704 return cifs_fscache_release_page(page, gfp);
4707 static void cifs_invalidate_page(struct page *page, unsigned int offset,
4708 unsigned int length)
4710 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4712 if (offset == 0 && length == PAGE_SIZE)
4713 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4716 static int cifs_launder_page(struct page *page)
4719 loff_t range_start = page_offset(page);
4720 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
4721 struct writeback_control wbc = {
4722 .sync_mode = WB_SYNC_ALL,
4724 .range_start = range_start,
4725 .range_end = range_end,
4728 cifs_dbg(FYI, "Launder page: %p\n", page);
4730 if (clear_page_dirty_for_io(page))
4731 rc = cifs_writepage_locked(page, &wbc);
4733 cifs_fscache_invalidate_page(page, page->mapping->host);
4737 void cifs_oplock_break(struct work_struct *work)
4739 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4741 struct inode *inode = d_inode(cfile->dentry);
4742 struct cifsInodeInfo *cinode = CIFS_I(inode);
4743 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4744 struct TCP_Server_Info *server = tcon->ses->server;
4746 bool purge_cache = false;
4748 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4749 TASK_UNINTERRUPTIBLE);
4751 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4752 cfile->oplock_epoch, &purge_cache);
4754 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4755 cifs_has_mand_locks(cinode)) {
4756 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4761 if (inode && S_ISREG(inode->i_mode)) {
4762 if (CIFS_CACHE_READ(cinode))
4763 break_lease(inode, O_RDONLY);
4765 break_lease(inode, O_WRONLY);
4766 rc = filemap_fdatawrite(inode->i_mapping);
4767 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4768 rc = filemap_fdatawait(inode->i_mapping);
4769 mapping_set_error(inode->i_mapping, rc);
4770 cifs_zap_mapping(inode);
4772 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4773 if (CIFS_CACHE_WRITE(cinode))
4774 goto oplock_break_ack;
4777 rc = cifs_push_locks(cfile);
4779 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4783 * releasing stale oplock after recent reconnect of smb session using
4784 * a now incorrect file handle is not a data integrity issue but do
4785 * not bother sending an oplock release if session to server still is
4786 * disconnected since oplock already released by the server
4788 if (!cfile->oplock_break_cancelled) {
4789 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4791 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4793 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4794 cifs_done_oplock_break(cinode);
4798 * The presence of cifs_direct_io() in the address space ops vector
4799 * allowes open() O_DIRECT flags which would have failed otherwise.
4801 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4802 * so this method should never be called.
4804 * Direct IO is not yet supported in the cached mode.
4807 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4811 * Eventually need to support direct IO for non forcedirectio mounts
4816 static int cifs_swap_activate(struct swap_info_struct *sis,
4817 struct file *swap_file, sector_t *span)
4819 struct cifsFileInfo *cfile = swap_file->private_data;
4820 struct inode *inode = swap_file->f_mapping->host;
4821 unsigned long blocks;
4824 cifs_dbg(FYI, "swap activate\n");
4826 spin_lock(&inode->i_lock);
4827 blocks = inode->i_blocks;
4828 isize = inode->i_size;
4829 spin_unlock(&inode->i_lock);
4830 if (blocks*512 < isize) {
4831 pr_warn("swap activate: swapfile has holes\n");
4836 pr_warn_once("Swap support over SMB3 is experimental\n");
4839 * TODO: consider adding ACL (or documenting how) to prevent other
4840 * users (on this or other systems) from reading it
4844 /* TODO: add sk_set_memalloc(inet) or similar */
4847 cfile->swapfile = true;
4849 * TODO: Since file already open, we can't open with DENY_ALL here
4850 * but we could add call to grab a byte range lock to prevent others
4851 * from reading or writing the file
4857 static void cifs_swap_deactivate(struct file *file)
4859 struct cifsFileInfo *cfile = file->private_data;
4861 cifs_dbg(FYI, "swap deactivate\n");
4863 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
4866 cfile->swapfile = false;
4868 /* do we need to unpin (or unlock) the file */
4871 const struct address_space_operations cifs_addr_ops = {
4872 .readpage = cifs_readpage,
4873 .readpages = cifs_readpages,
4874 .writepage = cifs_writepage,
4875 .writepages = cifs_writepages,
4876 .write_begin = cifs_write_begin,
4877 .write_end = cifs_write_end,
4878 .set_page_dirty = __set_page_dirty_nobuffers,
4879 .releasepage = cifs_release_page,
4880 .direct_IO = cifs_direct_io,
4881 .invalidatepage = cifs_invalidate_page,
4882 .launder_page = cifs_launder_page,
4884 * TODO: investigate and if useful we could add an cifs_migratePage
4885 * helper (under an CONFIG_MIGRATION) in the future, and also
4886 * investigate and add an is_dirty_writeback helper if needed
4888 .swap_activate = cifs_swap_activate,
4889 .swap_deactivate = cifs_swap_deactivate,
4893 * cifs_readpages requires the server to support a buffer large enough to
4894 * contain the header plus one complete page of data. Otherwise, we need
4895 * to leave cifs_readpages out of the address space operations.
4897 const struct address_space_operations cifs_addr_ops_smallbuf = {
4898 .readpage = cifs_readpage,
4899 .writepage = cifs_writepage,
4900 .writepages = cifs_writepages,
4901 .write_begin = cifs_write_begin,
4902 .write_end = cifs_write_end,
4903 .set_page_dirty = __set_page_dirty_nobuffers,
4904 .releasepage = cifs_release_page,
4905 .invalidatepage = cifs_invalidate_page,
4906 .launder_page = cifs_launder_page,