From patchwork Mon Dec 3 12:15:44 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Shilovsky X-Patchwork-Id: 1832851 Return-Path: X-Original-To: patchwork-cifs-client@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 448003FCA5 for ; Mon, 3 Dec 2012 12:16:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754459Ab2LCMP5 (ORCPT ); Mon, 3 Dec 2012 07:15:57 -0500 Received: from mail-lb0-f174.google.com ([209.85.217.174]:53622 "EHLO mail-lb0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751091Ab2LCMPz (ORCPT ); Mon, 3 Dec 2012 07:15:55 -0500 Received: by mail-lb0-f174.google.com with SMTP id gi11so2275826lbb.19 for ; Mon, 03 Dec 2012 04:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=O18FdzTn99dLHi6Polbypj+X/8xIO+yPJw7ADcEOc+0=; b=y6Tjvw4eA1MfI7U1me9/bsbHnbP2JUzdx1LhjyzBOJgaV4CbW/LlMLr86MA1krAixx NCBZPYNx9iYI4v3Tl3iwSEpveUqsGzjActFhMoS+FRJsuoB8S8U/o0WYIvy/fvJxn7Ao P3gOLJMAQvA/iZYn8kZNl05uSg3j/exRItGj9dIDE60raSWp69KKCSTKnM8w84AyQtAD XMgE3N5RiHVRmxH3BVzdl0Dm/84LmeE5kU16G/cjJy+03KYm/O4Wm/hH1Ppp+GlGQTDi LKI90cwKS5IzP4nT6ukLS++CjqYWf9GqukF8jdTxLoqW8Uf6Wr3SY8qH4ZzG+oX0RGVh /9kA== Received: by 10.152.102.234 with SMTP id fr10mr9341543lab.28.1354536953883; Mon, 03 Dec 2012 04:15:53 -0800 (PST) Received: from localhost.localdomain ([79.126.69.151]) by mx.google.com with ESMTPS id z9sm5301445lby.8.2012.12.03.04.15.51 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 03 Dec 2012 04:15:52 -0800 (PST) From: Pavel Shilovsky To: linux-cifs@vger.kernel.org Subject: [PATCH v3 6/6] CIFS: Fix write after setting a read lock for read oplock files Date: Mon, 3 Dec 2012 16:15:44 +0400 Message-Id: <1354536945-9593-1-git-send-email-piastry@etersoft.ru> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1354112625-30315-7-git-send-email-piastry@etersoft.ru> References: <1354112625-30315-7-git-send-email-piastry@etersoft.ru> Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by checking for brlocks, populating the page cache without marking affected pages dirty and directly writing to the server. Also remove CONFIG_CIFS_SMB2 ifdefs because it's suitable for both CIFS and SMB2 protocols. Signed-off-by: Pavel Shilovsky --- fs/cifs/cifsfs.c | 1 + fs/cifs/cifsglob.h | 1 + fs/cifs/file.c | 82 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/fs/cifs/cifsfs.c b/fs/cifs/cifsfs.c index c6e32f2..210f0af 100644 --- a/fs/cifs/cifsfs.c +++ b/fs/cifs/cifsfs.c @@ -229,6 +229,7 @@ cifs_alloc_inode(struct super_block *sb) cifs_set_oplock_level(cifs_inode, 0); cifs_inode->delete_pending = false; cifs_inode->invalid_mapping = false; + cifs_inode->leave_pages_clean = false; cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */ cifs_inode->server_eof = 0; cifs_inode->uniqueid = 0; diff --git a/fs/cifs/cifsglob.h b/fs/cifs/cifsglob.h index 2ca6f7d..426a2bf 100644 --- a/fs/cifs/cifsglob.h +++ b/fs/cifs/cifsglob.h @@ -1037,6 +1037,7 @@ struct cifsInodeInfo { bool clientCanCacheAll; /* read and writebehind oplock */ bool delete_pending; /* DELETE_ON_CLOSE is set */ bool invalid_mapping; /* pagecache is invalid */ + bool leave_pages_clean; /* protected by i_mutex, not set pages dirty */ unsigned long time; /* jiffies of last update of inode */ u64 server_eof; /* current file size on server -- protected by i_lock */ u64 uniqueid; /* server inode number */ diff --git a/fs/cifs/file.c b/fs/cifs/file.c index e2fabc9..1bc6e67 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -2109,7 +2109,15 @@ static int cifs_write_end(struct file *file, struct address_space *mapping, } else { rc = copied; pos += copied; - set_page_dirty(page); + /* + * When we use strict cache mode and cifs_strict_writev was run + * with level II oplock (indicated by leave_pages_clean field of + * CIFS_I(inode)), we can leave pages clean and let + * cifs_strict_writev send a new data to the server itself. + */ + if (!CIFS_I(inode)->leave_pages_clean || + !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)) + set_page_dirty(page); } if (rc > 0) { @@ -2460,8 +2468,8 @@ ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov, } static ssize_t -cifs_writev(struct kiocb *iocb, const struct iovec *iov, - unsigned long nr_segs, loff_t pos) +cifs_pagecache_writev(struct kiocb *iocb, const struct iovec *iov, + unsigned long nr_segs, loff_t pos, bool cache_all) { struct file *file = iocb->ki_filp; struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; @@ -2483,8 +2491,12 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov, server->vals->exclusive_lock_type, NULL, CIFS_WRITE_OP)) { mutex_lock(&inode->i_mutex); + if (!cache_all) + cinode->leave_pages_clean = true; rc = __generic_file_aio_write(iocb, iov, nr_segs, - &iocb->ki_pos); + &iocb->ki_pos); + if (!cache_all) + cinode->leave_pages_clean = false; mutex_unlock(&inode->i_mutex); } @@ -2511,25 +2523,38 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, struct cifsFileInfo *cfile = (struct cifsFileInfo *) iocb->ki_filp->private_data; struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); + ssize_t written; -#ifdef CONFIG_CIFS_SMB2 - /* - * If we have an oplock for read and want to write a data to the file - * we need to store it in the page cache and then push it to the server - * to be sure the next read will get a valid data. - */ - if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) { - ssize_t written; - int rc; - - written = generic_file_aio_write(iocb, iov, nr_segs, pos); - rc = filemap_fdatawrite(inode->i_mapping); - if (rc) - return (ssize_t)rc; - - return written; + /* we have a read oplock - need to store a data in the page cache */ + if (cinode->clientCanCacheRead) { + /* + * We need to store clientCanCacheAll here to prevent race + * conditions - this value can be changed during an execution + * of generic_file_aio_write. For CIFS it can be changed from + * true to false only, but for SMB2 it can be changed both from + * true to false and vice versa. So, we can end up with a data + * stored in the cache, not marked dirty and not sent to the + * server if this value changes its state from false to true + * after cifs_write_end. + */ + bool cache_all = cinode->clientCanCacheAll; + + if (cap_unix(tcon->ses) && + ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0) && + (CIFS_UNIX_FCNTL_CAP & le64_to_cpu( + tcon->fsUnixInfo.Capability))) + written = generic_file_aio_write(iocb, iov, nr_segs, + pos); + else + written = cifs_pagecache_writev(iocb, iov, nr_segs, pos, + cache_all); + /* + * Errors occured during writing or we have read+write oplock - + * no need to flush to the server. + */ + if (written < 0 || cache_all) + return written; } -#endif /* * For non-oplocked files in strict cache mode we need to write the data @@ -2537,16 +2562,11 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, * affected pages because it may cause a error with mandatory locks on * these pages but not on the region from pos to ppos+len-1. */ - - if (!cinode->clientCanCacheAll) - return cifs_user_writev(iocb, iov, nr_segs, pos); - - if (cap_unix(tcon->ses) && - (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && - ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) - return generic_file_aio_write(iocb, iov, nr_segs, pos); - - return cifs_writev(iocb, iov, nr_segs, pos); + written = cifs_user_writev(iocb, iov, nr_segs, pos); + /* need to restore pos if errors occured */ + if (written < 0) + iocb->ki_pos = pos; + return written; } static struct cifs_readdata *