From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jeff Layton Subject: [PATCH v7 17/17] locks: make locks_mandatory_area check for file-private locks Date: Wed, 19 Mar 2014 16:46:01 -0400 Message-ID: <1395261961-10855-18-git-send-email-jlayton@redhat.com> References: <1395261961-10855-1-git-send-email-jlayton@redhat.com> Cc: linux-fsdevel@vger.kernel.org, bfields@fieldses.org To: viro@ZenIV.linux.org.uk Return-path: Received: from mail-qg0-f42.google.com ([209.85.192.42]:36698 "EHLO mail-qg0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755764AbaCSUqe (ORCPT ); Wed, 19 Mar 2014 16:46:34 -0400 Received: by mail-qg0-f42.google.com with SMTP id q107so27393173qgd.1 for ; Wed, 19 Mar 2014 13:46:33 -0700 (PDT) In-Reply-To: <1395261961-10855-1-git-send-email-jlayton@redhat.com> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: Allow locks_mandatory_area() to handle file-private locks correctly. If there is a file-private lock set on an open file and we're doing I/O via the same, then that should not cause anything to block. Handle this by first doing a non-blocking FL_ACCESS check for a file-private lock, and then fall back to checking for a classic POSIX lock (and possibly blocking). Note that this approach is subject to the same races that have always plagued mandatory locking on Linux. Reported-by: Trond Myklebust Signed-off-by: Jeff Layton --- fs/locks.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fs/locks.c b/fs/locks.c index 98bb44558488..4d2099d0cb39 100644 --- a/fs/locks.c +++ b/fs/locks.c @@ -1199,19 +1199,30 @@ int locks_mandatory_area(int read_write, struct inode *inode, { struct file_lock fl; int error; + bool sleep = false; locks_init_lock(&fl); - fl.fl_owner = current->files; fl.fl_pid = current->tgid; fl.fl_file = filp; fl.fl_flags = FL_POSIX | FL_ACCESS; if (filp && !(filp->f_flags & O_NONBLOCK)) - fl.fl_flags |= FL_SLEEP; + sleep = true; fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; fl.fl_start = offset; fl.fl_end = offset + count - 1; for (;;) { + if (filp) { + fl.fl_owner = (fl_owner_t)filp; + fl.fl_flags &= ~FL_SLEEP; + error = __posix_lock_file(inode, &fl, NULL); + if (!error) + break; + } + + if (sleep) + fl.fl_flags |= FL_SLEEP; + fl.fl_owner = current->files; error = __posix_lock_file(inode, &fl, NULL); if (error != FILE_LOCK_DEFERRED) break; -- 1.8.5.3