Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

fs: create helper file_user_path() for user displayed mapped file path

Overlayfs uses backing files with "fake" overlayfs f_path and "real"
underlying f_inode, in order to use underlying inode aops for mapped
files and to display the overlayfs path in /proc/<pid>/maps.

In preparation for storing the overlayfs "fake" path instead of the
underlying "real" path in struct backing_file, define a noop helper
file_user_path() that returns f_path for now.

Use the new helper in procfs and kernel logs whenever a path of a
mapped file is displayed to users.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Link: https://lore.kernel.org/r/20231009153712.1566422-3-amir73il@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>

authored by

Amir Goldstein and committed by
Christian Brauner
08582d67 83bc1d29

+24 -8
+4 -2
arch/arc/kernel/troubleshoot.c
··· 90 90 */ 91 91 if (vma) { 92 92 char buf[ARC_PATH_MAX]; 93 - char *nm = "?"; 93 + char *nm = "anon"; 94 94 95 95 if (vma->vm_file) { 96 - nm = file_path(vma->vm_file, buf, ARC_PATH_MAX-1); 96 + /* XXX: can we use %pD below and get rid of buf? */ 97 + nm = d_path(file_user_path(vma->vm_file), buf, 98 + ARC_PATH_MAX-1); 97 99 if (IS_ERR(nm)) 98 100 nm = "?"; 99 101 }
+1 -1
fs/proc/base.c
··· 2218 2218 rc = -ENOENT; 2219 2219 vma = find_exact_vma(mm, vm_start, vm_end); 2220 2220 if (vma && vma->vm_file) { 2221 - *path = vma->vm_file->f_path; 2221 + *path = *file_user_path(vma->vm_file); 2222 2222 path_get(path); 2223 2223 rc = 0; 2224 2224 }
+1 -1
fs/proc/nommu.c
··· 58 58 59 59 if (file) { 60 60 seq_pad(m, ' '); 61 - seq_file_path(m, file, ""); 61 + seq_path(m, file_user_path(file), ""); 62 62 } 63 63 64 64 seq_putc(m, '\n');
+2 -2
fs/proc/task_mmu.c
··· 296 296 if (anon_name) 297 297 seq_printf(m, "[anon_shmem:%s]", anon_name->name); 298 298 else 299 - seq_file_path(m, file, "\n"); 299 + seq_path(m, file_user_path(file), "\n"); 300 300 goto done; 301 301 } 302 302 ··· 1967 1967 1968 1968 if (file) { 1969 1969 seq_puts(m, " file="); 1970 - seq_file_path(m, file, "\n\t= "); 1970 + seq_path(m, file_user_path(file), "\n\t= "); 1971 1971 } else if (vma_is_initial_heap(vma)) { 1972 1972 seq_puts(m, " heap"); 1973 1973 } else if (vma_is_initial_stack(vma)) {
+1 -1
fs/proc/task_nommu.c
··· 157 157 158 158 if (file) { 159 159 seq_pad(m, ' '); 160 - seq_file_path(m, file, ""); 160 + seq_path(m, file_user_path(file), ""); 161 161 } else if (mm && vma_is_initial_stack(vma)) { 162 162 seq_pad(m, ' '); 163 163 seq_puts(m, "[stack]");
+14
include/linux/fs.h
··· 2513 2513 return &f->f_path; 2514 2514 } 2515 2515 2516 + /* 2517 + * file_user_path - get the path to display for memory mapped file 2518 + * 2519 + * When mmapping a file on a stackable filesystem (e.g., overlayfs), the file 2520 + * stored in ->vm_file is a backing file whose f_inode is on the underlying 2521 + * filesystem. When the mapped file path is displayed to user (e.g. via 2522 + * /proc/<pid>/maps), this helper should be used to get the path to display 2523 + * to the user, which is the path of the fd that user has requested to map. 2524 + */ 2525 + static inline const struct path *file_user_path(struct file *f) 2526 + { 2527 + return &f->f_path; 2528 + } 2529 + 2516 2530 static inline struct file *file_clone_open(struct file *file) 2517 2531 { 2518 2532 return dentry_open(&file->f_path, file->f_flags, file->f_cred);
+1 -1
kernel/trace/trace_output.c
··· 404 404 vmstart = vma->vm_start; 405 405 } 406 406 if (file) { 407 - ret = trace_seq_path(s, &file->f_path); 407 + ret = trace_seq_path(s, file_user_path(file)); 408 408 if (ret) 409 409 trace_seq_printf(s, "[+0x%lx]", 410 410 ip - vmstart);