From patchwork Sun Nov 25 13:43:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10696831 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B2E235A4 for ; Sun, 25 Nov 2018 13:44:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A31002916A for ; Sun, 25 Nov 2018 13:44:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9768F291C0; Sun, 25 Nov 2018 13:44:18 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI 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 174002916A for ; Sun, 25 Nov 2018 13:44:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726317AbeKZAfX (ORCPT ); Sun, 25 Nov 2018 19:35:23 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:53036 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726115AbeKZAfX (ORCPT ); Sun, 25 Nov 2018 19:35:23 -0500 Received: by mail-wm1-f68.google.com with SMTP id r11-v6so15576615wmb.2 for ; Sun, 25 Nov 2018 05:44:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8zaOzN3LzKPuGnGsKDBnuwT7+QfZYDQLfjPNUThOzNc=; b=b4jSEke9PFTnF1BjInLEG0iH0adrsk0SJjhX6Td1/o5h60rAf1IG83oyi98CsyRZCX AkHAI21mLF3bUN4iWeJiNvAJknpx7FtTNGnJBhH0F+FxF+LFz0cC5cS36sDOdc1hlNT8 bReB3q+ALsxbuCH9dDQtGF9pmbclo4OngRB3cSw+VWbbvfRT/xhmlPtYykxEtmaCFZY1 3BaQBcd398WuOQHzJccJ+5DP7/pkj9CJUPlJGQGUk14ieUCoqNCMn1xzu3aaeThxtZZB I4tKPqKdqc2tHo1AcZjrPgcR8Y1PKffmXe+9LXWL5EvS7IBWNiKOKSaWqoPW6mrHBnUs LyIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8zaOzN3LzKPuGnGsKDBnuwT7+QfZYDQLfjPNUThOzNc=; b=PZ5qYg1E7tLzXnoYwfp6e3SY/43tMMUW/LlkL5Nrp0uhXoGquxIf0oe8PpBsIvSz54 ZWZkFrRDD8K+GM4kvql00OeHreSEG3ygDNB+KMh/kUVmDtSEJHT/DAIGO/ZgWbT7YfjG SvwUJ7o/RqcrDCuT8p4oy3nNkZGbvoPFQU+uh3wXMnqxy0fAgQ9CTCklua1NfmL0nAUu LKG9yJySI/6LnS0yLYYDhrOe2C1qdJJxtA8jUyBo9KxGtntW2xVi1555bpq7bXHfB/hm eaZI+e4P3xqxzu+HGq9YhGcmlPFHOgsoow+gwftF1Dn8XKiCwwDLL2hhNXEB9GWdveyR d1vA== X-Gm-Message-State: AGRZ1gKRxb13fSWKihykSTApWvqnLo0nORkpH41z5jWE2uaZxRPQQB1l SvKYJtYLWvcq8iGu+QXGvgU= X-Google-Smtp-Source: AFSGD/UaRkJ5cSr7fxyeB1KuuWACUimntsPRNJDZGNSKUSkfBLe3FubsYlYRiQF9nkpdAUWTx3x8ig== X-Received: by 2002:a1c:3c1:: with SMTP id 184-v6mr19749854wmd.90.1543153455204; Sun, 25 Nov 2018 05:44:15 -0800 (PST) Received: from localhost.localdomain (bzq-166-168-31-246.red.bezeqint.net. [31.168.166.246]) by smtp.gmail.com with ESMTPSA id 125sm17354274wml.35.2018.11.25.05.44.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Nov 2018 05:44:14 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 05/13] fanotify: classify events that hold a file identifier Date: Sun, 25 Nov 2018 15:43:44 +0200 Message-Id: <20181125134352.21499-6-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181125134352.21499-1-amir73il@gmail.com> References: <20181125134352.21499-1-amir73il@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With group flag FAN_REPORT_FID, we do not store event->path. Instead, we will store the file handle and fsid in event->info.fid. Because event->info and event->path share the same union space, set bit 0 of event->info.flags, so we know how to free and compare the event objects. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 35 ++++++++++++++++++++++++------ fs/notify/fanotify/fanotify.h | 27 +++++++++++++++++++++++ fs/notify/fanotify/fanotify_user.c | 3 +++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 59d093923c97..8192d4b1db21 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -25,11 +25,16 @@ static bool should_merge(struct fsnotify_event *old_fsn, old = FANOTIFY_E(old_fsn); new = FANOTIFY_E(new_fsn); - if (old_fsn->inode == new_fsn->inode && old->pid == new->pid && - old->path.mnt == new->path.mnt && - old->path.dentry == new->path.dentry) - return true; - return false; + if (old_fsn->inode != new_fsn->inode || old->pid != new->pid) + return false; + + if (FANOTIFY_HAS_FID(new)) { + return FANOTIFY_HAS_FID(old) && + fanotify_fid_equal(old->info.fid, new->info.fid); + } else { + return old->path.mnt == new->path.mnt && + old->path.dentry == new->path.dentry; + } } /* and the list better be locked by something too! */ @@ -178,7 +183,10 @@ init: __maybe_unused event->pid = get_pid(task_pid(current)); else event->pid = get_pid(task_tgid(current)); - if (path && !FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { + if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { + /* TODO: allocate buffer and encode file handle */ + fanotify_set_fid(event, NULL); + } else if (path) { event->path = *path; path_get(&event->path); } else { @@ -277,8 +285,21 @@ static void fanotify_free_event(struct fsnotify_event *fsn_event) { struct fanotify_event *event; + /* + * event->path and event->info are a union. Make sure that + * event->info.flags overlaps with path.dentry pointer, so it is safe + * to use bit 0 to classify the event info type (FANOTIFY_HAS_FID). + */ + BUILD_BUG_ON(offsetof(struct fanotify_event, path.dentry) != + offsetof(struct fanotify_event, info.flags)); + BUILD_BUG_ON(!__builtin_types_compatible_p(typeof(event->info.flags), + unsigned long)); + event = FANOTIFY_E(fsn_event); - path_put(&event->path); + if (FANOTIFY_HAS_FID(event)) + kfree(event->info.fid); + else + path_put(&event->path); put_pid(event->pid); if (fanotify_is_perm_event(fsn_event->mask)) { kmem_cache_free(fanotify_perm_event_cachep, diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 2e4fca30afda..a79dcbd41702 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -13,8 +13,20 @@ extern struct kmem_cache *fanotify_perm_event_cachep; struct fanotify_info { struct fanotify_event_fid *fid; + unsigned long flags; }; +static inline bool fanotify_fid_equal(const struct fanotify_event_fid *fid1, + const struct fanotify_event_fid *fid2) +{ + if (fid1 == fid2) + return true; + + return fid1 && fid2 && fid1->handle_bytes == fid2->handle_bytes && + !memcmp((void *)fid1, (void *)fid2, + FANOTIFY_FID_LEN(fid1->handle_bytes)); +} + /* * Structure for normal fanotify events. It gets allocated in * fanotify_handle_event() and freed when the information is retrieved by @@ -38,6 +50,21 @@ struct fanotify_event { struct pid *pid; }; +/* + * Bit 0 of info.flags is set when event has fid information. + * event->info shares the same union address with event->path, so this helps + * us tell if event has fid or path. + */ +#define __FANOTIFY_FID 1UL +#define FANOTIFY_HAS_FID(event) ((event)->info.flags & __FANOTIFY_FID) + +static inline void fanotify_set_fid(struct fanotify_event *event, + struct fanotify_event_fid *fid) +{ + event->info.fid = fid; + event->info.flags = fid ? __FANOTIFY_FID : 0; +} + /* * Structure for permission fanotify events. It gets allocated and freed in * fanotify_handle_event() since we wait there for user response. When the diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 93e1aa2a389f..47e8bf3bcd28 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -81,6 +81,9 @@ static int create_fd(struct fsnotify_group *group, pr_debug("%s: group=%p event=%p\n", __func__, group, event); + if (WARN_ON_ONCE(FANOTIFY_HAS_FID(event))) + return -EBADF; + client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); if (client_fd < 0) return client_fd;