From: Jeff Mahoney <jeffm@suse.com>
Subject: vfs: add super_operations->get_inode_dev
References: bsc#927455
Patch-mainline: Never, Upstream submissions have been met with a request to use per-subvolume superblocks, which is unworkable in practice

There are many places where a dev_t:ino_t pair are passed to userspace
to uniquely describe an inode.  Some file systems, like btrfs, have
multiple inode namespace internally and use a separate dev_t to make the
distinction between them.

This patch adds a super_operations->get_inode_dev operation to allow
the file system to export those dev_ts to callers within the kernel
for further export to userspace.

Without this patch, things like audit and some perf and trace events
will not distinguish between subvolumes within a single btrfs filesystem.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
---
 arch/arc/kernel/troubleshoot.c             |    2 +-
 drivers/staging/lustre/lustre/llite/dir.c  |    2 +-
 drivers/staging/lustre/lustre/llite/file.c |    2 +-
 fs/locks.c                                 |    4 ++--
 fs/nfsd/nfs3xdr.c                          |    2 +-
 fs/nfsd/vfs.c                              |    4 ++--
 fs/notify/fdinfo.c                         |    4 ++--
 fs/proc/nommu.c                            |    2 +-
 fs/proc/task_mmu.c                         |    2 +-
 fs/proc/task_nommu.c                       |    2 +-
 fs/stat.c                                  |    2 +-
 include/linux/fs.h                         |    9 +++++++++
 include/trace/events/filemap.h             |    2 +-
 kernel/audit.c                             |    2 +-
 kernel/audit_watch.c                       |    4 ++--
 kernel/events/core.c                       |    2 +-
 mm/memory-failure.c                        |    2 +-
 security/tomoyo/condition.c                |    2 +-
 18 files changed, 30 insertions(+), 21 deletions(-)

--- a/arch/arc/kernel/troubleshoot.c
+++ b/arch/arc/kernel/troubleshoot.c
@@ -101,7 +101,7 @@ static void show_faulting_vma(unsigned l
 		if (file) {
 			nm = file_path(file, buf, PAGE_SIZE - 1);
 			inode = file_inode(vma->vm_file);
-			dev = inode->i_sb->s_dev;
+			dev = inode_get_dev(inode);
 			ino = inode->i_ino;
 		}
 		pr_info("    @off 0x%lx in [%s]\n"
--- a/drivers/staging/lustre/lustre/llite/dir.c
+++ b/drivers/staging/lustre/lustre/llite/dir.c
@@ -1500,7 +1500,7 @@ skip_lmm:
 			struct lov_user_mds_data *lmdp;
 			lstat_t st = { 0 };
 
-			st.st_dev     = inode->i_sb->s_dev;
+			st.st_dev     = inode_get_dev(inode);
 			st.st_mode    = body->mode;
 			st.st_nlink   = body->nlink;
 			st.st_uid     = body->uid;
--- a/drivers/staging/lustre/lustre/llite/file.c
+++ b/drivers/staging/lustre/lustre/llite/file.c
@@ -2993,7 +2993,7 @@ int ll_getattr(struct vfsmount *mnt, str
 	if (res)
 		return res;
 
-	stat->dev = inode->i_sb->s_dev;
+	stat->dev = inode_get_dev(inode);
 	if (ll_need_32bit_api(sbi))
 		stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
 	else
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -2600,8 +2600,8 @@ static void lock_get_status(struct seq_f
 	if (inode) {
 		/* 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(inode_get_dev(inode)),
+				MINOR(inode_get_dev(inode)), inode->i_ino);
 	} else {
 		seq_printf(f, "%d <none>:0 ", fl_pid);
 	}
--- a/fs/nfsd/nfs3xdr.c
+++ b/fs/nfsd/nfs3xdr.c
@@ -146,7 +146,7 @@ static __be32 *encode_fsid(__be32 *p, st
 	default:
 	case FSIDSOURCE_DEV:
 		p = xdr_encode_hyper(p, (u64)huge_encode_dev
-				     (d_inode(fhp->fh_dentry)->i_sb->s_dev));
+				     (inode_get_dev(d_inode(fhp->fh_dentry))));
 		break;
 	case FSIDSOURCE_FSID:
 		p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
--- a/fs/nfsd/vfs.c
+++ b/fs/nfsd/vfs.c
@@ -907,7 +907,7 @@ static int wait_for_concurrent_writes(st
 	int err = 0;
 
 	if (atomic_read(&inode->i_writecount) > 1
-	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
+	    || (last_ino == inode->i_ino && last_dev == inode_get_dev(inode))) {
 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
 		msleep(10);
 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
@@ -918,7 +918,7 @@ static int wait_for_concurrent_writes(st
 		err = vfs_fsync(file, 0);
 	}
 	last_ino = inode->i_ino;
-	last_dev = inode->i_sb->s_dev;
+	last_dev = inode_get_dev(inode);
 	return err;
 }
 
--- a/fs/notify/fdinfo.c
+++ b/fs/notify/fdinfo.c
@@ -91,7 +91,7 @@ static void inotify_fdinfo(struct seq_fi
 		 */
 		u32 mask = mark->mask & IN_ALL_EVENTS;
 		seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ",
-			   inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
+			   inode_mark->wd, inode->i_ino, inode_get_dev(inode),
 			   mask, mark->ignored_mask);
 		show_mark_fhandle(m, inode);
 		seq_putc(m, '\n');
@@ -124,7 +124,7 @@ static void fanotify_fdinfo(struct seq_f
 		if (!inode)
 			return;
 		seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
-			   inode->i_ino, inode->i_sb->s_dev,
+			   inode->i_ino, inode_get_dev(inode),
 			   mflags, mark->mask, mark->ignored_mask);
 		show_mark_fhandle(m, inode);
 		seq_putc(m, '\n');
--- a/fs/proc/nommu.c
+++ b/fs/proc/nommu.c
@@ -46,7 +46,7 @@ static int nommu_region_show(struct seq_
 
 	if (file) {
 		struct inode *inode = file_inode(region->vm_file);
-		dev = inode->i_sb->s_dev;
+		dev = inode_get_dev(inode);
 		ino = inode->i_ino;
 	}
 
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -288,7 +288,7 @@ show_map_vma(struct seq_file *m, struct
 
 	if (file) {
 		struct inode *inode = file_inode(vma->vm_file);
-		dev = inode->i_sb->s_dev;
+		dev = inode_get_dev(inode);
 		ino = inode->i_ino;
 		pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
 	}
--- a/fs/proc/task_nommu.c
+++ b/fs/proc/task_nommu.c
@@ -164,7 +164,7 @@ static int nommu_vma_show(struct seq_fil
 
 	if (file) {
 		struct inode *inode = file_inode(vma->vm_file);
-		dev = inode->i_sb->s_dev;
+		dev = inode_get_dev(inode);
 		ino = inode->i_ino;
 		pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
 	}
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -20,7 +20,7 @@
 
 void generic_fillattr(struct inode *inode, struct kstat *stat)
 {
-	stat->dev = inode->i_sb->s_dev;
+	stat->dev = inode_get_dev(inode);
 	stat->ino = inode->i_ino;
 	stat->mode = inode->i_mode;
 	stat->nlink = inode->i_nlink;
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1754,6 +1754,7 @@ struct super_operations {
 				  struct shrink_control *);
 	long (*free_cached_objects)(struct super_block *,
 				    struct shrink_control *);
+	dev_t (*get_inode_dev)(const struct inode *);
 };
 
 /*
@@ -3068,4 +3069,12 @@ static inline bool dir_relax(struct inod
 extern bool path_noexec(const struct path *path);
 extern void inode_nohighmem(struct inode *inode);
 
+static inline dev_t inode_get_dev(const struct inode *inode)
+{
+	if (inode->i_sb->s_op->get_inode_dev)
+		return inode->i_sb->s_op->get_inode_dev(inode);
+
+	return inode->i_sb->s_dev;
+}
+
 #endif /* _LINUX_FS_H */
--- a/include/trace/events/filemap.h
+++ b/include/trace/events/filemap.h
@@ -29,7 +29,7 @@ DECLARE_EVENT_CLASS(mm_filemap_op_page_c
 		__entry->i_ino = page->mapping->host->i_ino;
 		__entry->index = page->index;
 		if (page->mapping->host->i_sb)
-			__entry->s_dev = page->mapping->host->i_sb->s_dev;
+			__entry->s_dev = inode_get_dev(page->mapping->host);
 		else
 			__entry->s_dev = page->mapping->host->i_rdev;
 	),
--- a/kernel/audit.c
+++ b/kernel/audit.c
@@ -1725,7 +1725,7 @@ void audit_copy_inode(struct audit_names
 		      const struct inode *inode)
 {
 	name->ino   = inode->i_ino;
-	name->dev   = inode->i_sb->s_dev;
+	name->dev   = inode_get_dev(inode);
 	name->mode  = inode->i_mode;
 	name->uid   = inode->i_uid;
 	name->gid   = inode->i_gid;
--- a/kernel/audit_watch.c
+++ b/kernel/audit_watch.c
@@ -368,7 +368,7 @@ static int audit_get_nd(struct audit_wat
 	mutex_unlock(&d_backing_inode(parent->dentry)->i_mutex);
 	if (d_is_positive(d)) {
 		/* update watch filter fields */
-		watch->dev = d_backing_inode(d)->i_sb->s_dev;
+		watch->dev = inode_get_dev(d_backing_inode(d));
 		watch->ino = d_backing_inode(d)->i_ino;
 	}
 	dput(d);
@@ -498,7 +498,7 @@ static int audit_watch_handle_event(stru
 	};
 
 	if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
-		audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
+		audit_update_watch(parent, dname, inode_get_dev(inode), inode->i_ino, 0);
 	else if (mask & (FS_DELETE|FS_MOVED_FROM))
 		audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1);
 	else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6080,7 +6080,7 @@ static void perf_event_mmap_event(struct
 			goto cpy_name;
 		}
 		inode = file_inode(vma->vm_file);
-		dev = inode->i_sb->s_dev;
+		dev = inode_get_dev(inode);
 		ino = inode->i_ino;
 		gen = inode->i_generation;
 		maj = MAJOR(dev);
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -98,7 +98,7 @@ static int hwpoison_filter_dev(struct pa
 	if (mapping == NULL || mapping->host == NULL)
 		return -EINVAL;
 
-	dev = mapping->host->i_sb->s_dev;
+	dev = inode_get_dev(mapping->host);
 	if (hwpoison_filter_dev_major != ~0U &&
 	    hwpoison_filter_dev_major != MAJOR(dev))
 		return -EINVAL;
--- a/security/tomoyo/condition.c
+++ b/security/tomoyo/condition.c
@@ -721,7 +721,7 @@ void tomoyo_get_attributes(struct tomoyo
 			stat->gid  = inode->i_gid;
 			stat->ino  = inode->i_ino;
 			stat->mode = inode->i_mode;
-			stat->dev  = inode->i_sb->s_dev;
+			stat->dev  = inode_get_dev(inode);
 			stat->rdev = inode->i_rdev;
 			obj->stat_valid[i] = true;
 		}
