From patchwork Fri Jun 29 20:55:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Paulo Alcantara (SUSE)" X-Patchwork-Id: 10497499 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2848C60325 for ; Fri, 29 Jun 2018 20:55:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18ECB291D2 for ; Fri, 29 Jun 2018 20:55:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D4FF296C6; Fri, 29 Jun 2018 20:55:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 665C829603 for ; Fri, 29 Jun 2018 20:55:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933489AbeF2Uzo (ORCPT ); Fri, 29 Jun 2018 16:55:44 -0400 Received: from mail.paulo.ac ([34.238.86.106]:34968 "EHLO mail.paulo.ac" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935367AbeF2Uzn (ORCPT ); Fri, 29 Jun 2018 16:55:43 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 7859BC78F24; Fri, 29 Jun 2018 20:55:42 +0000 (UTC) X-Virus-Scanned: amavisd-new at paulo.ac Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bKbYl7rRsRgH; Fri, 29 Jun 2018 20:55:38 +0000 (UTC) Received: from localhost.localdomain (189.27.149.191.dynamic.adsl.gvt.net.br [189.27.149.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id B721BC78F25; Fri, 29 Jun 2018 20:55:37 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac B721BC78F25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1530305738; bh=q45SIRsdgAJf7feiYVcI8U8lba8CIITN37rdHAe9rZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mgL6DhIsdLoFBj+PstPlwSfk+pXRNygHtCd2Cv+MTYMT36ZJ0BzVmrbwqADFsoGe2 bDgtSWAw/I5csqT/PnfXkksfcWe/M1kSoEqX8GG0tF/F9O7qNSVyzXAlU/4tsrrlfD 9OwnRVAMzjs7VBMgs+q0xugaVEF+c/Mlj4OSkn54= From: Paulo Alcantara To: linux-cifs@vger.kernel.org Cc: smfrench@gmail.com, Paulo Alcantara , Paulo Alcantara Subject: [PATCH 2/2] cifs: Handle OFD locks Date: Fri, 29 Jun 2018 17:55:00 -0300 Message-Id: <20180629205500.7950-2-paulo@paulo.ac> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180629205500.7950-1-paulo@paulo.ac> References: <20180629205500.7950-1-paulo@paulo.ac> Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Correctly check for OFD lock conflicts. Signed-off-by: Paulo Alcantara --- fs/cifs/cifsglob.h | 1 + fs/cifs/cifsproto.h | 4 -- fs/cifs/file.c | 93 ++++++++++++++++++++++++++++++--------------- 3 files changed, 63 insertions(+), 35 deletions(-) diff --git a/fs/cifs/cifsglob.h b/fs/cifs/cifsglob.h index a2962fd41c6f..a627559be72e 100644 --- a/fs/cifs/cifsglob.h +++ b/fs/cifs/cifsglob.h @@ -1093,6 +1093,7 @@ struct cifsLockInfo { __u64 length; __u32 pid; __u32 type; + unsigned int flags; /* file lock flags*/ }; /* diff --git a/fs/cifs/cifsproto.h b/fs/cifs/cifsproto.h index 1890f534c88b..870ff791a170 100644 --- a/fs/cifs/cifsproto.h +++ b/fs/cifs/cifsproto.h @@ -214,10 +214,6 @@ extern void cifs_umount(struct cifs_sb_info *); extern void cifs_mark_open_files_invalid(struct cifs_tcon *tcon); extern void cifs_reopen_persistent_handles(struct cifs_tcon *tcon); -extern bool cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, - __u64 length, __u8 type, - struct cifsLockInfo **conf_lock, - int rw_check); extern void cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink, struct cifs_pending_open *open); diff --git a/fs/cifs/file.c b/fs/cifs/file.c index 259167f32580..7c7c505fab3e 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -864,16 +864,17 @@ int cifs_closedir(struct inode *inode, struct file *file) } static struct cifsLockInfo * -cifs_lock_init(__u64 offset, __u64 length, __u8 type) +cifs_lock_init(struct file_lock *flock, __u64 length, __u8 type) { struct cifsLockInfo *lock = kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); if (!lock) return lock; - lock->offset = offset; + lock->offset = flock->fl_start; lock->length = length; lock->type = type; lock->pid = current->tgid; + lock->flags = flock->fl_flags; INIT_LIST_HEAD(&lock->blist); init_waitqueue_head(&lock->block_q); return lock; @@ -893,31 +894,56 @@ cifs_del_lock_waiters(struct cifsLockInfo *lock) #define CIFS_READ_OP 1 #define CIFS_WRITE_OP 2 -/* @rw_check : 0 - no op, 1 - read, 2 - write */ +static inline bool locks_overlap(struct cifsLockInfo *li, __u64 offset, + __u64 length) +{ + return offset + length > li->offset && + li->offset + li->length > offset; +} + static bool -cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, - __u64 length, __u8 type, struct cifsFileInfo *cfile, - struct cifsLockInfo **conf_lock, int rw_check) +locks_conflict(struct cifsFileInfo *cfile, struct cifsFileInfo *cur_cfile, + struct cifsLockInfo *li, __u8 type, unsigned int flags, + struct TCP_Server_Info *server, int rw_check) +{ + if (rw_check == CIFS_LOCK_OP) { + if (!server->ops->compare_fids(cfile, cur_cfile)) + return true; + if ((flags & FL_FLOCK) && current->tgid == li->pid) + return false; + if ((flags & FL_OFDLCK) && (li->flags & FL_OFDLCK)) + return false; + if (li->type & server->vals->exclusive_lock_type) + return true; + if (type & server->vals->exclusive_lock_type) + return true; + return false; + } + + if (server->ops->compare_fids(cfile, cur_cfile) && + current->tgid == li->pid && + (!(li->type & server->vals->shared_lock_type) || + rw_check != CIFS_WRITE_OP)) + return false; + + return true; +} + +static inline bool +__fid_lock_conflicts(struct cifs_fid_locks *fdlocks, __u64 offset, __u64 length, + __u8 type, unsigned int flags, struct cifsFileInfo *cfile, + struct cifsLockInfo **conf_lock, int rw_check) { struct cifsLockInfo *li; struct cifsFileInfo *cur_cfile = fdlocks->cfile; struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; list_for_each_entry(li, &fdlocks->locks, llist) { - if (offset + length <= li->offset || - offset >= li->offset + li->length) - continue; - if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && - server->ops->compare_fids(cfile, cur_cfile)) { - /* shared lock prevents write op through the same fid */ - if (!(li->type & server->vals->shared_lock_type) || - rw_check != CIFS_WRITE_OP) - continue; - } - if ((type & server->vals->shared_lock_type) && - ((server->ops->compare_fids(cfile, cur_cfile) && - current->tgid == li->pid) || type == li->type)) + if (!locks_overlap(li, offset, length) || + !locks_conflict(cfile, cur_cfile, li, type, flags, server, + rw_check)) continue; + if (conf_lock) *conf_lock = li; return true; @@ -925,18 +951,19 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, return false; } -bool +static bool cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, - __u8 type, struct cifsLockInfo **conf_lock, - int rw_check) + __u8 type, unsigned int flags, + struct cifsLockInfo **conf_lock, int rw_check) { bool rc = false; struct cifs_fid_locks *cur; struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); list_for_each_entry(cur, &cinode->llist, llist) { - rc = cifs_find_fid_lock_conflict(cur, offset, length, type, - cfile, conf_lock, rw_check); + rc = __fid_lock_conflicts(cur, offset, length, type, + flags, cfile, conf_lock, + rw_check); if (rc) break; } @@ -964,7 +991,8 @@ cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, down_read(&cinode->lock_sem); exist = cifs_find_lock_conflict(cfile, offset, length, type, - &conf_lock, CIFS_LOCK_OP); + flock->fl_flags, &conf_lock, + CIFS_LOCK_OP); if (exist) { flock->fl_start = conf_lock->offset; flock->fl_end = conf_lock->offset + conf_lock->length - 1; @@ -1011,7 +1039,8 @@ cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, down_write(&cinode->lock_sem); exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, - lock->type, &conf_lock, CIFS_LOCK_OP); + lock->flags, lock->type, &conf_lock, + CIFS_LOCK_OP); if (!exist && cinode->can_cache_brlcks) { list_add_tail(&lock->llist, &cfile->llist->locks); up_write(&cinode->lock_sem); @@ -1307,7 +1336,7 @@ static int cifs_read_flock(struct file_lock *flock, __u32 *type, bool *lock, bool *wait_flag, struct TCP_Server_Info *server) { unsigned int flags = FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | - FL_LEASE | FL_CLOSE; + FL_LEASE | FL_CLOSE | FL_OFDLCK; if (flock->fl_flags & ~flags) { cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags); @@ -1318,7 +1347,9 @@ static int cifs_read_flock(struct file_lock *flock, __u32 *type, bool *lock, if (flock->fl_flags & FL_POSIX) cifs_dbg(FYI, "Posix\n"); if (flock->fl_flags & FL_FLOCK) - cifs_dbg(FYI, "Flock\n"); + cifs_dbg(FYI, "File lock\n"); + if (flock->fl_flags & FL_OFDLCK) + cifs_dbg(FYI, "OFD lock\n"); if (flock->fl_flags & FL_ACCESS) cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); if (flock->fl_flags & FL_LEASE) @@ -1587,7 +1618,7 @@ static int cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, if (lock) { struct cifsLockInfo *lock; - lock = cifs_lock_init(flock->fl_start, length, type); + lock = cifs_lock_init(flock, length, type); if (!lock) return -ENOMEM; @@ -2814,7 +2845,7 @@ cifs_writev(struct kiocb *iocb, struct iov_iter *from) goto out; if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), - server->vals->exclusive_lock_type, NULL, + server->vals->exclusive_lock_type, 0, NULL, CIFS_WRITE_OP)) rc = __generic_file_write_iter(iocb, from); else @@ -3385,7 +3416,7 @@ cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) down_read(&cinode->lock_sem); if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to), tcon->ses->server->vals->shared_lock_type, - NULL, CIFS_READ_OP)) + 0, NULL, CIFS_READ_OP)) rc = generic_file_read_iter(iocb, to); up_read(&cinode->lock_sem); return rc;