From patchwork Tue Jul 31 21:10:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Fasheh X-Patchwork-Id: 10551457 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 723FA139A for ; Tue, 31 Jul 2018 21:11:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 63F752B64A for ; Tue, 31 Jul 2018 21:11:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 568342B660; Tue, 31 Jul 2018 21:11:04 +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.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable 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 056752B666 for ; Tue, 31 Jul 2018 21:11:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732079AbeGaWxO (ORCPT ); Tue, 31 Jul 2018 18:53:14 -0400 Received: from mx2.suse.de ([195.135.220.15]:35784 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725886AbeGaWxO (ORCPT ); Tue, 31 Jul 2018 18:53:14 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id B9296AD48; Tue, 31 Jul 2018 21:10:59 +0000 (UTC) From: Mark Fasheh To: Al Viro , linux-fsdevel@vger.kernel.org Cc: linux-btrfs@vger.kernel.org, Andrew Morton , Amir Goldstein , linux-unionfs@vger.kernel.org, Jeff Mahoney , linux-nfs@vger.kernel.org, Mark Fasheh Subject: [PATCH 1/4] vfs: introduce function to map unique ino/dev pairs Date: Tue, 31 Jul 2018 14:10:42 -0700 Message-Id: <20180731211045.5671-2-mfasheh@suse.de> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180731211045.5671-1-mfasheh@suse.de> References: <20180731211045.5671-1-mfasheh@suse.de> 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 There are places in the VFS where we export an ino/dev pair to userspace. /proc/PID/maps is a good example - we directly expose inode->i_ino and inode->i_sb->s_dev to userspace there. Many filesystems don't put a unique value in inode->i_ino and instead rely on ->getattr to provide the real inode number to userspace. super->s_dev is similar - some filesystems expose a difference device from what's put in super->s_dev when queried via stat/statx. Ultimately this makes it impossible for a user (or software) to match one of those reported pairs to the right inode. We can fix this by adding a helper function, vfs_map_unique_ino_dev(), which will query the owning filesystem (via ->getattr) to get the correct ino/dev pair. Later patches will update those places which simply dump inode->i_ino and super->s_dev to use the helper. Signed-off-by: Mark Fasheh Signed-off-by: Mark Fasheh --- fs/stat.c | 23 +++++++++++++++++++++++ include/linux/fs.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/fs/stat.c b/fs/stat.c index f8e6fb2c3657..80ea42505219 100644 --- a/fs/stat.c +++ b/fs/stat.c @@ -84,6 +84,29 @@ int vfs_getattr_nosec(const struct path *path, struct kstat *stat, } EXPORT_SYMBOL(vfs_getattr_nosec); +void vfs_map_unique_ino_dev(struct dentry *dentry, u64 *ino, dev_t *dev) +{ + int ret; + struct path path; + struct kstat stat; + + path.mnt = NULL; + path.dentry = dentry; + /* ->dev is always returned, so we only need to specify ino here */ + ret = vfs_getattr_nosec(&path, &stat, STATX_INO, 0); + if (ret) { + struct inode *inode = d_inode(dentry); + /* Fallback to old behavior in case of getattr error */ + *ino = inode->i_ino; + *dev = inode->i_sb->s_dev; + return ret; + } + *ino = stat.ino; + *dev = stat.dev; + return 0; +} +EXPORT_SYMBOL(vfs_map_unique_ino_dev); + /* * vfs_getattr - Get the enhanced basic attributes of a file * @path: The file of interest diff --git a/include/linux/fs.h b/include/linux/fs.h index d78d146a98da..b80789472438 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3077,6 +3077,8 @@ extern void kfree_link(void *); extern void generic_fillattr(struct inode *, struct kstat *); extern int vfs_getattr_nosec(const struct path *, struct kstat *, u32, unsigned int); extern int vfs_getattr(const struct path *, struct kstat *, u32, unsigned int); +extern void vfs_map_unique_ino_dev(struct dentry *dentry, u64 *ino, dev_t *dev); + void __inode_add_bytes(struct inode *inode, loff_t bytes); void inode_add_bytes(struct inode *inode, loff_t bytes); void __inode_sub_bytes(struct inode *inode, loff_t bytes); From patchwork Tue Jul 31 21:10:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Fasheh X-Patchwork-Id: 10551459 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 2EFCD14E0 for ; Tue, 31 Jul 2018 21:11:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 218A52B426 for ; Tue, 31 Jul 2018 21:11:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 200DB2B611; Tue, 31 Jul 2018 21:11:07 +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.9 required=2.0 tests=BAYES_00,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 C1DF92B426 for ; Tue, 31 Jul 2018 21:11:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732269AbeGaWxR (ORCPT ); Tue, 31 Jul 2018 18:53:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:35820 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725886AbeGaWxQ (ORCPT ); Tue, 31 Jul 2018 18:53:16 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 43366ADF7; Tue, 31 Jul 2018 21:11:02 +0000 (UTC) From: Mark Fasheh To: Al Viro , linux-fsdevel@vger.kernel.org Cc: linux-btrfs@vger.kernel.org, Andrew Morton , Amir Goldstein , linux-unionfs@vger.kernel.org, Jeff Mahoney , linux-nfs@vger.kernel.org, Mark Fasheh Subject: [PATCH 2/4] nfs: check for NULL vfsmount in nfs_getattr Date: Tue, 31 Jul 2018 14:10:43 -0700 Message-Id: <20180731211045.5671-3-mfasheh@suse.de> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180731211045.5671-1-mfasheh@suse.de> References: <20180731211045.5671-1-mfasheh@suse.de> 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 ->getattr from inside the kernel won't always have a vfsmount, check for this. Signed-off-by: Mark Fasheh --- fs/nfs/inode.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c index b65aee481d13..7a3d90a7cc92 100644 --- a/fs/nfs/inode.c +++ b/fs/nfs/inode.c @@ -795,7 +795,9 @@ int nfs_getattr(const struct path *path, struct kstat *stat, } /* - * We may force a getattr if the user cares about atime. + * We may force a getattr if the user cares about atime. A + * NULL vfsmount means we are called from inside the kernel, + * where no atime is required. * * Note that we only have to check the vfsmount flags here: * - NFS always sets S_NOATIME by so checking it would give a @@ -803,7 +805,7 @@ int nfs_getattr(const struct path *path, struct kstat *stat, * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is * no point in checking those. */ - if ((path->mnt->mnt_flags & MNT_NOATIME) || + if (!path->mnt || (path->mnt->mnt_flags & MNT_NOATIME) || ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) request_mask &= ~STATX_ATIME; From patchwork Tue Jul 31 21:10:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Fasheh X-Patchwork-Id: 10551467 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 3ADE0174A for ; Tue, 31 Jul 2018 21:11:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 293602B66E for ; Tue, 31 Jul 2018 21:11:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 26E662B4B4; Tue, 31 Jul 2018 21:11:09 +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.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable 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 B2A232B666 for ; Tue, 31 Jul 2018 21:11:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732318AbeGaWxU (ORCPT ); Tue, 31 Jul 2018 18:53:20 -0400 Received: from mx2.suse.de ([195.135.220.15]:35844 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732203AbeGaWxU (ORCPT ); Tue, 31 Jul 2018 18:53:20 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 9A68DAFD5; Tue, 31 Jul 2018 21:11:04 +0000 (UTC) From: Mark Fasheh To: Al Viro , linux-fsdevel@vger.kernel.org Cc: linux-btrfs@vger.kernel.org, Andrew Morton , Amir Goldstein , linux-unionfs@vger.kernel.org, Jeff Mahoney , linux-nfs@vger.kernel.org, Mark Fasheh Subject: [PATCH 3/4] proc: use vfs helper to get ino/dev pairs for maps file Date: Tue, 31 Jul 2018 14:10:44 -0700 Message-Id: <20180731211045.5671-4-mfasheh@suse.de> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180731211045.5671-1-mfasheh@suse.de> References: <20180731211045.5671-1-mfasheh@suse.de> 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 Proc writes ino/dev into /proc/PID/maps which it gets from i_ino and s_dev. The problem with this is that i_ino and s_dev are not guaranteed to be unique - we can have duplicates in the maps file for otherwise distinct inodes. This breaks any software expecting them to be unique, including lsof(8). We can fix this by using vfs_map_unique_ino_dev() to map the unique ino/dev pair for us. Signed-off-by: Mark Fasheh --- fs/proc/nommu.c | 11 ++++------- fs/proc/task_mmu.c | 8 +++----- fs/proc/task_nommu.c | 8 +++----- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c index 3b63be64e436..56c12d02c5ad 100644 --- a/fs/proc/nommu.c +++ b/fs/proc/nommu.c @@ -36,7 +36,7 @@ */ static int nommu_region_show(struct seq_file *m, struct vm_region *region) { - unsigned long ino = 0; + u64 ino = 0; struct file *file; dev_t dev = 0; int flags; @@ -44,15 +44,12 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region) flags = region->vm_flags; file = region->vm_file; - if (file) { - struct inode *inode = file_inode(region->vm_file); - dev = inode->i_sb->s_dev; - ino = inode->i_ino; - } + if (file) + vfs_map_unique_ino_dev(file_dentry(file), &ino, &dev); { seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); seq_printf(m, - "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ", + "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %llu ", region->vm_start, region->vm_end, flags & VM_READ ? 'r' : '-', diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index dfd73a4616ce..e9cd33425191 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -276,7 +276,7 @@ static int is_stack(struct vm_area_struct *vma) static void show_vma_header_prefix(struct seq_file *m, unsigned long start, unsigned long end, vm_flags_t flags, unsigned long long pgoff, - dev_t dev, unsigned long ino) + dev_t dev, u64 ino) { seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); seq_put_hex_ll(m, NULL, start, 8); @@ -299,16 +299,14 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid) struct mm_struct *mm = vma->vm_mm; struct file *file = vma->vm_file; vm_flags_t flags = vma->vm_flags; - unsigned long ino = 0; + u64 ino = 0; unsigned long long pgoff = 0; unsigned long start, end; dev_t dev = 0; const char *name = NULL; if (file) { - struct inode *inode = file_inode(vma->vm_file); - dev = inode->i_sb->s_dev; - ino = inode->i_ino; + vfs_map_unique_ino_dev(file_dentry(file), &ino, &dev); pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; } diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c index 5b62f57bd9bc..1198e979ed3f 100644 --- a/fs/proc/task_nommu.c +++ b/fs/proc/task_nommu.c @@ -146,7 +146,7 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma, int is_pid) { struct mm_struct *mm = vma->vm_mm; - unsigned long ino = 0; + u64 ino = 0; struct file *file; dev_t dev = 0; int flags; @@ -156,15 +156,13 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma, file = vma->vm_file; if (file) { - struct inode *inode = file_inode(vma->vm_file); - dev = inode->i_sb->s_dev; - ino = inode->i_ino; + vfs_map_unique_inode_dev(file_dentry(file), &ino, &dev)); pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT; } seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); seq_printf(m, - "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ", + "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %llu ", vma->vm_start, vma->vm_end, flags & VM_READ ? 'r' : '-', From patchwork Tue Jul 31 21:10:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Fasheh X-Patchwork-Id: 10551475 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 AB0ED139A for ; Tue, 31 Jul 2018 21:11:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9CCDC2B66A for ; Tue, 31 Jul 2018 21:11:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9156A2B677; Tue, 31 Jul 2018 21:11:12 +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.9 required=2.0 tests=BAYES_00,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 2F2012B670 for ; Tue, 31 Jul 2018 21:11:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732345AbeGaWxW (ORCPT ); Tue, 31 Jul 2018 18:53:22 -0400 Received: from mx2.suse.de ([195.135.220.15]:35870 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725886AbeGaWxV (ORCPT ); Tue, 31 Jul 2018 18:53:21 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id EF41AAE4C; Tue, 31 Jul 2018 21:11:06 +0000 (UTC) From: Mark Fasheh To: Al Viro , linux-fsdevel@vger.kernel.org Cc: linux-btrfs@vger.kernel.org, Andrew Morton , Amir Goldstein , linux-unionfs@vger.kernel.org, Jeff Mahoney , linux-nfs@vger.kernel.org, Mark Fasheh Subject: [PATCH 4/4] locks: map correct ino/dev pairs when exporting to userspace Date: Tue, 31 Jul 2018 14:10:45 -0700 Message-Id: <20180731211045.5671-5-mfasheh@suse.de> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180731211045.5671-1-mfasheh@suse.de> References: <20180731211045.5671-1-mfasheh@suse.de> 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 /proc/locks does not always print the correct inode number/device pair. Update lock_get_status() to use vfs_map_unique_ino_dev() to get the real, unique values for userspace. Signed-off-by: Mark Fasheh --- fs/locks.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fs/locks.c b/fs/locks.c index db7b6917d9c5..3a012df87fd8 100644 --- a/fs/locks.c +++ b/fs/locks.c @@ -2621,6 +2621,7 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl, loff_t id, char *pfx) { struct inode *inode = NULL; + struct dentry *dentry; unsigned int fl_pid; struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info; @@ -2633,8 +2634,10 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl, if (fl_pid == 0) return; - if (fl->fl_file != NULL) + if (fl->fl_file != NULL) { inode = locks_inode(fl->fl_file); + dentry = file_dentry(fl->fl_file); + } seq_printf(f, "%lld:%s ", id, pfx); if (IS_POSIX(fl)) { @@ -2681,10 +2684,13 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl, : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ "); } if (inode) { + __u64 ino; + dev_t dev; + + vfs_map_unique_ino_dev(dentry, &ino, &dev); /* userspace relies on this representation of dev_t */ seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, - MAJOR(inode->i_sb->s_dev), - MINOR(inode->i_sb->s_dev), inode->i_ino); + MAJOR(dev), MINOR(dev), inode->i_ino); } else { seq_printf(f, "%d :0 ", fl_pid); }