From patchwork Thu Nov 15 18:45:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10684843 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 94A03109C for ; Thu, 15 Nov 2018 18:45:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8560B2CF57 for ; Thu, 15 Nov 2018 18:45:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7978A2CF59; Thu, 15 Nov 2018 18:45:59 +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 030D72CF57 for ; Thu, 15 Nov 2018 18:45:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388751AbeKPEyx (ORCPT ); Thu, 15 Nov 2018 23:54:53 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:38224 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726910AbeKPEyx (ORCPT ); Thu, 15 Nov 2018 23:54:53 -0500 Received: by mail-wm1-f65.google.com with SMTP id f2-v6so18891581wme.3; Thu, 15 Nov 2018 10:45:55 -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=R+R7qUgEBL1kLvOj6lsCZwS+aPA4gWgap7r6hYXnoD4=; b=K4vG6tS/IIF0/TXf1CQAFkNUYaHzgBisPc5tKWWvuMv1RfKVN7nEIULdP5st+FYOLN /mpv3F1ryIqB6ppkFigzzTD/jqYVD1LXgo7TT0CsA2LoZlduQaVoWJ/3SOKghCMiooMB 363t3/apaxGZsBVPsO7p/O+CvEv+MH90q8HQ6wiaK/RNUbgG7pbZ20TH5bXHlh0IaeTe GXoM1RYTJtG3FBACCD0865rfeuXZcix5UGz+iKbgbSxjZDkyM6yi+zuUUnPbdTaJ12SV UtZPF7FOrGnMxELjpQpBpBK1CTJDXobUZRmmD+B1gDAh1hq3XG1G9LRCX9ilkO26tzbA sZOw== 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=R+R7qUgEBL1kLvOj6lsCZwS+aPA4gWgap7r6hYXnoD4=; b=ULhYCEbqDta4WwpZVKA0cPyyvWcCyulb8DTfHaYLzQTlk5xzZ3lRzWilI8koiC/gUx mbZV7bh4oq+DIx81hmDPIXnIIF7b+u4gB1y7ijX4mgYGDOAXnheRnjWwTAqAhxntxeSu e0EoH43CLA1FOJUt5EA3dNNhI7YpyojvRVCDNz8dymLt+2crZadfC4ZcEdzXZIaFsQij lIj6Qh6EWseBo4mLPLTTrPVoFQLvPAyBEi3EsiPaaGzfi3edTajXMfY7/Mymaj/m2Kds BnnFat2saj7vrjSPgEMMNPGPdlo8eC0yNg5FS0FuLjM13zGAES/LXEAq16hr7vNnILfR J4KA== X-Gm-Message-State: AGRZ1gIeRpe0+Lclw2ko5AEy/T3xCg4GC6ndSEeoGjtfQwP92eMkrKKN SsFiuJgzVbT1tY17j3mKFbo= X-Google-Smtp-Source: AJdET5ddxkBBYQFiydijBKOWhQ1MMOjVWwtiDok6aBTDMeTPyt72QY2CoLKzke/02CJJZE2xaZNWBQ== X-Received: by 2002:a7b:c5cc:: with SMTP id n12mr108479wmk.149.1542307555057; Thu, 15 Nov 2018 10:45:55 -0800 (PST) Received: from localhost.localdomain ([37.46.46.74]) by smtp.gmail.com with ESMTPSA id s1sm13976327wro.9.2018.11.15.10.45.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Nov 2018 10:45:54 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH v2 3/9] fanotify: classify events that hold a file identifier Date: Thu, 15 Nov 2018 20:45:38 +0200 Message-Id: <20181115184544.30681-4-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181115184544.30681-1-amir73il@gmail.com> References: <20181115184544.30681-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..d6ed31134321 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..29f61060af66 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;