From patchwork Sun Nov 25 13:43:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10696835 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 32CB21869 for ; Sun, 25 Nov 2018 13:44:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 21EDE2916A for ; Sun, 25 Nov 2018 13:44:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1694B291C0; Sun, 25 Nov 2018 13:44:20 +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 A9D2E2919A for ; Sun, 25 Nov 2018 13:44:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726369AbeKZAfY (ORCPT ); Sun, 25 Nov 2018 19:35:24 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:42849 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726312AbeKZAfY (ORCPT ); Sun, 25 Nov 2018 19:35:24 -0500 Received: by mail-wr1-f65.google.com with SMTP id q18so16214486wrx.9 for ; Sun, 25 Nov 2018 05:44:17 -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=n4PhYNAtzRKzzpR6l94qEPtjl4jLgerps8x8J1pUkxg=; b=i8AFJgpoYrmuVz1xsOPT6alQdQRF/BzLeiqBXpced81CaWpg6sApdgcahaRUzRQD4B gdChpyxj7WIL9n0JQD6NcLZun5aeCuVYCelKor2o/wszEae6Y4gQiF9v+PmP5Qaqre85 GfsDOXhv6fq4dwABvMDAPOHmKtlM8iNGVUzwhnmB4dHzRkE5XzH1MvYfz4jkzkubP8si 91EvzzPc2zRtN4GCUwYzyo75OlBPzyfPB+YFNmynq5PCjxRc3yDRIcYIKX7na/tK1Ta6 HjhJgNe6de1MSstDIZxB3ObbjuVsPiMR9gtLIkl8gUMnwvepJc3qZnMO/QVApRdKkuMN 1+cg== 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=n4PhYNAtzRKzzpR6l94qEPtjl4jLgerps8x8J1pUkxg=; b=eSKj4m8lQdsRFBnX5JGhlOiyslEd2eB8soce03NS/c+IBvgDAyXERz6ZvKRzBsQGhG jUx29mHg0mI+1GgjXWsLlYqyiUrlkKrCjSqJOjWzbp0zRsrKj6Aayfo6PJQBP0f1asoi QHztT/sx2NFYVrEWXc8PhEhgl6AdIe3LsSJ/Of7ggfTVmesf0fw+HpA3VENihWONKUeZ VlrREnrAfPnmoMzrfasjGQ82djOklG1W7ZNqQuzoVlI5srepN6+5ukfL2NVEYFm0UCWx SH390q2jTaWvcejJbvSES5JXHIUDp/K+NfTvK07fH1M3slV8oUdc4Kl75sSSwnwnmCD/ TgQQ== X-Gm-Message-State: AA+aEWap5z0/tR++ZOoepApYG92hFgjnHjUOEoB5aQv1exMVYyXvSxLM 0PfdwM+Blz4ic9z4Pnac6xY= X-Google-Smtp-Source: AFSGD/UtlemALcF/KHEeVjHw1fO+jNZeFwEUFMXrcCN9uyyi4K/VDBu6ryNBk7oZWUsFLtjptbw/rw== X-Received: by 2002:adf:ebd0:: with SMTP id v16mr14256249wrn.109.1543153456453; Sun, 25 Nov 2018 05:44:16 -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.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Nov 2018 05:44:15 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 06/13] fanotify: encode file identifier for FAN_REPORT_FID Date: Sun, 25 Nov 2018 15:43:45 +0200 Message-Id: <20181125134352.21499-7-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 Allocate and encode event->info.fid for a group with FAN_REPORT_FID. Treat failure to allocate fid buffer as failure to allocate event. Treat failure to encode fid by printing a warning but queueing the event without the fid information. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 59 +++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 8192d4b1db21..844b748f0b74 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include "fanotify.h" @@ -146,11 +148,55 @@ static u32 fanotify_group_event_mask(struct fsnotify_iter_info *iter_info, ~marks_ignored_mask; } +static struct fanotify_event_fid *fanotify_alloc_fid(const struct path *path, + gfp_t gfp) +{ + struct fanotify_event_fid *fid = NULL; + int dwords, bytes = 0; + struct kstatfs stat; + int err, type; + + dwords = 0; + err = -ENOENT; + type = exportfs_encode_fh(path->dentry, NULL, &dwords, 0); + if (!dwords) + goto out_err; + + err = vfs_statfs(path, &stat); + if (err) + goto out_err; + + /* Treat failure to allocate fid as failure to allocate event */ + bytes = dwords << 2; + fid = kmalloc(FANOTIFY_FID_LEN(bytes), gfp); + if (!fid) + return NULL; + + type = exportfs_encode_fh(path->dentry, (struct fid *)fid->f_handle, + &dwords, 0); + err = -EINVAL; + if (type == FILEID_INVALID || bytes != dwords << 2) + goto out_err; + + fid->handle_bytes = bytes; + fid->handle_type = type; + fid->fsid = stat.f_fsid; + + return fid; + +out_err: + pr_warn_ratelimited("fanotify: failed to encode fid of %pd2 (bytes=%d, err=%i)\n", + path->dentry, bytes, err); + kfree(fid); + return ERR_PTR(err); +} + struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, struct inode *inode, u32 mask, const struct path *path) { struct fanotify_event *event = NULL; + struct fanotify_event_fid *fid = NULL; gfp_t gfp = GFP_KERNEL_ACCOUNT; /* @@ -164,6 +210,16 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, /* Whoever is interested in the event, pays for the allocation. */ memalloc_use_memcg(group->memcg); + if (path && FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { + fid = fanotify_alloc_fid(path, gfp); + /* Treat failure to allocate fid as failure to allocate event */ + if (!fid) + goto out; + /* Report the event without a file identifier on encode error */ + if (IS_ERR(fid)) + fid = NULL; + } + if (fanotify_is_perm_event(mask)) { struct fanotify_perm_event *pevent; @@ -184,8 +240,7 @@ init: __maybe_unused else event->pid = get_pid(task_tgid(current)); if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) { - /* TODO: allocate buffer and encode file handle */ - fanotify_set_fid(event, NULL); + fanotify_set_fid(event, fid); } else if (path) { event->path = *path; path_get(&event->path);