]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/aufs/vfsub.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005-2021 Junjiro R. Okajima
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * sub-routines for VFS
23 #include <linux/mnt_namespace.h>
24 #include <linux/namei.h>
25 #include <linux/nsproxy.h>
26 #include <linux/security.h>
27 #include <linux/splice.h>
30 #ifdef CONFIG_AUFS_BR_FUSE
31 int vfsub_test_mntns(struct vfsmount
*mnt
, struct super_block
*h_sb
)
33 if (!au_test_fuse(h_sb
) || !au_userns
)
36 return is_current_mnt_ns(mnt
) ? 0 : -EACCES
;
40 int vfsub_sync_filesystem(struct super_block
*h_sb
, int wait
)
45 down_read(&h_sb
->s_umount
);
46 err
= __sync_filesystem(h_sb
, wait
);
47 up_read(&h_sb
->s_umount
);
53 /* ---------------------------------------------------------------------- */
55 int vfsub_update_h_iattr(struct path
*h_path
, int *did
)
59 struct super_block
*h_sb
;
62 * Always needs h_path->mnt for LSM or FUSE branch.
64 AuDebugOn(!h_path
->mnt
);
66 /* for remote fs, leave work for its getattr or d_revalidate */
67 /* for bad i_attr fs, handle them in aufs_getattr() */
68 /* still some fs may acquire i_mutex. we need to skip them */
72 h_sb
= h_path
->dentry
->d_sb
;
73 *did
= (!au_test_fs_remote(h_sb
) && au_test_fs_refresh_iattr(h_sb
));
75 err
= vfsub_getattr(h_path
, &st
);
80 /* ---------------------------------------------------------------------- */
82 struct file
*vfsub_dentry_open(struct path
*path
, int flags
)
84 return dentry_open(path
, flags
/* | __FMODE_NONOTIFY */,
88 struct file
*vfsub_filp_open(const char *path
, int oflags
, int mode
)
93 file
= filp_open(path
,
94 oflags
/* | __FMODE_NONOTIFY */,
99 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
106 * Ideally this function should call VFS:do_last() in order to keep all its
107 * checkings. But it is very hard for aufs to regenerate several VFS internal
108 * structure such as nameidata. This is a second (or third) best approach.
109 * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
111 int vfsub_atomic_open(struct inode
*dir
, struct dentry
*dentry
,
112 struct vfsub_aopen_args
*args
)
115 struct au_branch
*br
= args
->br
;
116 struct file
*file
= args
->file
;
117 /* copied from linux/fs/namei.c:atomic_open() */
118 struct dentry
*const DENTRY_NOT_SET
= (void *)-1UL;
121 AuDebugOn(!dir
->i_op
->atomic_open
);
123 err
= au_br_test_oflag(args
->open_flag
, br
);
127 au_lcnt_inc(&br
->br_nfiles
);
128 file
->f_path
.dentry
= DENTRY_NOT_SET
;
129 file
->f_path
.mnt
= au_br_mnt(br
);
130 AuDbg("%ps\n", dir
->i_op
->atomic_open
);
131 err
= dir
->i_op
->atomic_open(dir
, dentry
, file
, args
->open_flag
,
133 if (unlikely(err
< 0)) {
134 au_lcnt_dec(&br
->br_nfiles
);
138 /* temporary workaround for nfsv4 branch */
139 if (au_test_nfs(dir
->i_sb
))
140 nfs_mark_for_revalidate(dir
);
142 if (file
->f_mode
& FMODE_CREATED
)
143 fsnotify_create(dir
, dentry
);
144 if (!(file
->f_mode
& FMODE_OPENED
)) {
145 au_lcnt_dec(&br
->br_nfiles
);
149 /* todo: call VFS:may_open() here */
150 /* todo: ima_file_check() too? */
151 if (!err
&& (args
->open_flag
& __FMODE_EXEC
))
152 err
= deny_write_access(file
);
156 au_lcnt_dec(&br
->br_nfiles
);
157 /* note that the file is created and still opened */
163 int vfsub_kern_path(const char *name
, unsigned int flags
, struct path
*path
)
167 err
= kern_path(name
, flags
, path
);
168 if (!err
&& d_is_positive(path
->dentry
))
169 vfsub_update_h_iattr(path
, /*did*/NULL
); /*ignore*/
173 struct dentry
*vfsub_lookup_one_len_unlocked(const char *name
,
174 struct path
*ppath
, int len
)
178 path
.dentry
= lookup_one_len_unlocked(name
, ppath
->dentry
, len
);
179 if (IS_ERR(path
.dentry
))
181 if (d_is_positive(path
.dentry
)) {
182 path
.mnt
= ppath
->mnt
;
183 vfsub_update_h_iattr(&path
, /*did*/NULL
); /*ignore*/
187 AuTraceErrPtr(path
.dentry
);
191 struct dentry
*vfsub_lookup_one_len(const char *name
, struct path
*ppath
,
196 /* VFS checks it too, but by WARN_ON_ONCE() */
197 IMustLock(d_inode(ppath
->dentry
));
199 path
.dentry
= lookup_one_len(name
, ppath
->dentry
, len
);
200 if (IS_ERR(path
.dentry
))
202 if (d_is_positive(path
.dentry
)) {
203 path
.mnt
= ppath
->mnt
;
204 vfsub_update_h_iattr(&path
, /*did*/NULL
); /*ignore*/
208 AuTraceErrPtr(path
.dentry
);
212 void vfsub_call_lkup_one(void *args
)
214 struct vfsub_lkup_one_args
*a
= args
;
215 *a
->errp
= vfsub_lkup_one(a
->name
, a
->ppath
);
218 /* ---------------------------------------------------------------------- */
220 struct dentry
*vfsub_lock_rename(struct dentry
*d1
, struct au_hinode
*hdir1
,
221 struct dentry
*d2
, struct au_hinode
*hdir2
)
226 d
= lock_rename(d1
, d2
);
228 au_hn_suspend(hdir1
);
230 au_hn_suspend(hdir2
);
235 void vfsub_unlock_rename(struct dentry
*d1
, struct au_hinode
*hdir1
,
236 struct dentry
*d2
, struct au_hinode
*hdir2
)
242 unlock_rename(d1
, d2
);
246 /* ---------------------------------------------------------------------- */
248 int vfsub_create(struct inode
*dir
, struct path
*path
, int mode
, bool want_excl
)
252 struct user_namespace
*userns
;
257 path
->dentry
= d
->d_parent
;
258 err
= security_path_mknod(path
, d
, mode
, 0);
262 userns
= mnt_user_ns(path
->mnt
);
265 err
= vfs_create(userns
, dir
, path
->dentry
, mode
, want_excl
);
268 struct path tmp
= *path
;
271 vfsub_update_h_iattr(&tmp
, &did
);
273 tmp
.dentry
= path
->dentry
->d_parent
;
274 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
283 int vfsub_symlink(struct inode
*dir
, struct path
*path
, const char *symname
)
287 struct user_namespace
*userns
;
292 path
->dentry
= d
->d_parent
;
293 err
= security_path_symlink(path
, d
, symname
);
297 userns
= mnt_user_ns(path
->mnt
);
300 err
= vfs_symlink(userns
, dir
, path
->dentry
, symname
);
303 struct path tmp
= *path
;
306 vfsub_update_h_iattr(&tmp
, &did
);
308 tmp
.dentry
= path
->dentry
->d_parent
;
309 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
318 int vfsub_mknod(struct inode
*dir
, struct path
*path
, int mode
, dev_t dev
)
322 struct user_namespace
*userns
;
327 path
->dentry
= d
->d_parent
;
328 err
= security_path_mknod(path
, d
, mode
, new_encode_dev(dev
));
332 userns
= mnt_user_ns(path
->mnt
);
335 err
= vfs_mknod(userns
, dir
, path
->dentry
, mode
, dev
);
338 struct path tmp
= *path
;
341 vfsub_update_h_iattr(&tmp
, &did
);
343 tmp
.dentry
= path
->dentry
->d_parent
;
344 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
353 static int au_test_nlink(struct inode
*inode
)
355 const unsigned int link_max
= UINT_MAX
>> 1; /* rough margin */
357 if (!au_test_fs_no_limit_nlink(inode
->i_sb
)
358 || inode
->i_nlink
< link_max
)
363 int vfsub_link(struct dentry
*src_dentry
, struct inode
*dir
, struct path
*path
,
364 struct inode
**delegated_inode
)
368 struct user_namespace
*userns
;
372 err
= au_test_nlink(d_inode(src_dentry
));
376 /* we don't call may_linkat() */
378 path
->dentry
= d
->d_parent
;
379 err
= security_path_link(src_dentry
, path
, d
);
383 userns
= mnt_user_ns(path
->mnt
);
386 err
= vfs_link(src_dentry
, userns
, dir
, path
->dentry
, delegated_inode
);
389 struct path tmp
= *path
;
392 /* fuse has different memory inode for the same inumber */
393 vfsub_update_h_iattr(&tmp
, &did
);
395 tmp
.dentry
= path
->dentry
->d_parent
;
396 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
397 tmp
.dentry
= src_dentry
;
398 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
407 int vfsub_rename(struct inode
*src_dir
, struct dentry
*src_dentry
,
408 struct inode
*dir
, struct path
*path
,
409 struct inode
**delegated_inode
, unsigned int flags
)
412 struct renamedata rd
;
422 path
->dentry
= d
->d_parent
;
423 tmp
.dentry
= src_dentry
->d_parent
;
424 err
= security_path_rename(&tmp
, src_dentry
, path
, d
, /*flags*/0);
429 rd
.old_mnt_userns
= mnt_user_ns(path
->mnt
);
430 rd
.old_dir
= src_dir
;
431 rd
.old_dentry
= src_dentry
;
432 rd
.new_mnt_userns
= rd
.old_mnt_userns
;
434 rd
.new_dentry
= path
->dentry
;
435 rd
.delegated_inode
= delegated_inode
;
438 err
= vfs_rename(&rd
);
443 tmp
.dentry
= d
->d_parent
;
444 vfsub_update_h_iattr(&tmp
, &did
);
446 tmp
.dentry
= src_dentry
;
447 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
448 tmp
.dentry
= src_dentry
->d_parent
;
449 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
458 int vfsub_mkdir(struct inode
*dir
, struct path
*path
, int mode
)
462 struct user_namespace
*userns
;
467 path
->dentry
= d
->d_parent
;
468 err
= security_path_mkdir(path
, d
, mode
);
472 userns
= mnt_user_ns(path
->mnt
);
475 err
= vfs_mkdir(userns
, dir
, path
->dentry
, mode
);
478 struct path tmp
= *path
;
481 vfsub_update_h_iattr(&tmp
, &did
);
483 tmp
.dentry
= path
->dentry
->d_parent
;
484 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
493 int vfsub_rmdir(struct inode
*dir
, struct path
*path
)
497 struct user_namespace
*userns
;
502 path
->dentry
= d
->d_parent
;
503 err
= security_path_rmdir(path
, d
);
507 userns
= mnt_user_ns(path
->mnt
);
510 err
= vfs_rmdir(userns
, dir
, path
->dentry
);
514 .dentry
= path
->dentry
->d_parent
,
518 vfsub_update_h_iattr(&tmp
, /*did*/NULL
); /*ignore*/
525 /* ---------------------------------------------------------------------- */
527 /* todo: support mmap_sem? */
528 ssize_t
vfsub_read_u(struct file
*file
, char __user
*ubuf
, size_t count
,
534 err
= vfs_read(file
, ubuf
, count
, ppos
);
537 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
541 ssize_t
vfsub_read_k(struct file
*file
, void *kbuf
, size_t count
,
547 err
= kernel_read(file
, kbuf
, count
, ppos
);
551 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
555 ssize_t
vfsub_write_u(struct file
*file
, const char __user
*ubuf
, size_t count
,
561 err
= vfs_write(file
, ubuf
, count
, ppos
);
564 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
568 ssize_t
vfsub_write_k(struct file
*file
, void *kbuf
, size_t count
, loff_t
*ppos
)
573 err
= kernel_write(file
, kbuf
, count
, ppos
);
576 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
580 int vfsub_flush(struct file
*file
, fl_owner_t id
)
585 if (file
->f_op
->flush
) {
586 if (!au_test_nfs(file
->f_path
.dentry
->d_sb
))
587 err
= file
->f_op
->flush(file
, id
);
590 err
= file
->f_op
->flush(file
, id
);
594 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
);
600 int vfsub_iterate_dir(struct file
*file
, struct dir_context
*ctx
)
604 AuDbg("%pD, ctx{%ps, %llu}\n", file
, ctx
->actor
, ctx
->pos
);
607 err
= iterate_dir(file
, ctx
);
610 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
615 long vfsub_splice_to(struct file
*in
, loff_t
*ppos
,
616 struct pipe_inode_info
*pipe
, size_t len
,
622 err
= do_splice_to(in
, ppos
, pipe
, len
, flags
);
626 vfsub_update_h_iattr(&in
->f_path
, /*did*/NULL
); /*ignore*/
630 long vfsub_splice_from(struct pipe_inode_info
*pipe
, struct file
*out
,
631 loff_t
*ppos
, size_t len
, unsigned int flags
)
636 err
= do_splice_from(pipe
, out
, ppos
, len
, flags
);
639 vfsub_update_h_iattr(&out
->f_path
, /*did*/NULL
); /*ignore*/
643 int vfsub_fsync(struct file
*file
, struct path
*path
, int datasync
)
647 /* file can be NULL */
649 err
= vfs_fsync(file
, datasync
);
654 path
= &file
->f_path
;
656 vfsub_update_h_iattr(path
, /*did*/NULL
); /*ignore*/
661 /* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
662 int vfsub_trunc(struct path
*h_path
, loff_t length
, unsigned int attr
,
666 struct inode
*h_inode
;
667 struct super_block
*h_sb
;
668 struct user_namespace
*h_userns
;
671 err
= vfsub_truncate(h_path
, length
);
675 h_inode
= d_inode(h_path
->dentry
);
676 h_sb
= h_inode
->i_sb
;
678 sb_start_write(h_sb
);
680 err
= security_path_truncate(h_path
);
682 h_userns
= mnt_user_ns(h_path
->mnt
);
684 err
= do_truncate(h_userns
, h_path
->dentry
, length
, attr
,
696 /* ---------------------------------------------------------------------- */
698 struct au_vfsub_mkdir_args
{
705 static void au_call_vfsub_mkdir(void *args
)
707 struct au_vfsub_mkdir_args
*a
= args
;
708 *a
->errp
= vfsub_mkdir(a
->dir
, a
->path
, a
->mode
);
711 int vfsub_sio_mkdir(struct inode
*dir
, struct path
*path
, int mode
)
713 int err
, do_sio
, wkq_err
;
714 struct user_namespace
*userns
;
716 userns
= mnt_user_ns(path
->mnt
);
717 do_sio
= au_test_h_perm_sio(userns
, dir
, MAY_EXEC
| MAY_WRITE
);
720 err
= vfsub_mkdir(dir
, path
, mode
);
723 struct au_vfsub_mkdir_args args
= {
729 wkq_err
= au_wkq_wait(au_call_vfsub_mkdir
, &args
);
730 if (unlikely(wkq_err
))
737 struct au_vfsub_rmdir_args
{
743 static void au_call_vfsub_rmdir(void *args
)
745 struct au_vfsub_rmdir_args
*a
= args
;
746 *a
->errp
= vfsub_rmdir(a
->dir
, a
->path
);
749 int vfsub_sio_rmdir(struct inode
*dir
, struct path
*path
)
751 int err
, do_sio
, wkq_err
;
752 struct user_namespace
*userns
;
754 userns
= mnt_user_ns(path
->mnt
);
755 do_sio
= au_test_h_perm_sio(userns
, dir
, MAY_EXEC
| MAY_WRITE
);
758 err
= vfsub_rmdir(dir
, path
);
761 struct au_vfsub_rmdir_args args
= {
766 wkq_err
= au_wkq_wait(au_call_vfsub_rmdir
, &args
);
767 if (unlikely(wkq_err
))
774 /* ---------------------------------------------------------------------- */
776 struct notify_change_args
{
780 struct inode
**delegated_inode
;
783 static void call_notify_change(void *args
)
785 struct notify_change_args
*a
= args
;
786 struct inode
*h_inode
;
787 struct user_namespace
*userns
;
789 h_inode
= d_inode(a
->path
->dentry
);
793 if (!IS_IMMUTABLE(h_inode
) && !IS_APPEND(h_inode
)) {
794 userns
= mnt_user_ns(a
->path
->mnt
);
796 *a
->errp
= notify_change(userns
, a
->path
->dentry
, a
->ia
,
800 vfsub_update_h_iattr(a
->path
, /*did*/NULL
); /*ignore*/
802 AuTraceErr(*a
->errp
);
805 int vfsub_notify_change(struct path
*path
, struct iattr
*ia
,
806 struct inode
**delegated_inode
)
809 struct notify_change_args args
= {
813 .delegated_inode
= delegated_inode
816 call_notify_change(&args
);
821 int vfsub_sio_notify_change(struct path
*path
, struct iattr
*ia
,
822 struct inode
**delegated_inode
)
825 struct notify_change_args args
= {
829 .delegated_inode
= delegated_inode
832 wkq_err
= au_wkq_wait(call_notify_change
, &args
);
833 if (unlikely(wkq_err
))
839 /* ---------------------------------------------------------------------- */
845 struct inode
**delegated_inode
;
848 static void call_unlink(void *args
)
850 struct unlink_args
*a
= args
;
851 struct dentry
*d
= a
->path
->dentry
;
852 struct inode
*h_inode
;
853 struct user_namespace
*userns
;
854 const int stop_sillyrename
= (au_test_nfs(d
->d_sb
)
855 && au_dcount(d
) == 1);
859 a
->path
->dentry
= d
->d_parent
;
860 *a
->errp
= security_path_unlink(a
->path
, d
);
862 if (unlikely(*a
->errp
))
865 if (!stop_sillyrename
)
868 if (d_is_positive(d
)) {
869 h_inode
= d_inode(d
);
873 userns
= mnt_user_ns(a
->path
->mnt
);
875 *a
->errp
= vfs_unlink(userns
, a
->dir
, d
, a
->delegated_inode
);
879 .dentry
= d
->d_parent
,
882 vfsub_update_h_iattr(&tmp
, /*did*/NULL
); /*ignore*/
885 if (!stop_sillyrename
)
890 AuTraceErr(*a
->errp
);
894 * @dir: must be locked.
895 * @dentry: target dentry.
897 int vfsub_unlink(struct inode
*dir
, struct path
*path
,
898 struct inode
**delegated_inode
, int force
)
901 struct unlink_args args
= {
905 .delegated_inode
= delegated_inode
913 wkq_err
= au_wkq_wait(call_unlink
, &args
);
914 if (unlikely(wkq_err
))