]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/aufs/vfsub.c
2 * Copyright (C) 2005-2017 Junjiro R. Okajima
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * sub-routines for VFS
22 #include <linux/mnt_namespace.h>
23 #include <linux/namei.h>
24 #include <linux/nsproxy.h>
25 #include <linux/security.h>
26 #include <linux/splice.h>
29 #ifdef CONFIG_AUFS_BR_FUSE
30 int vfsub_test_mntns(struct vfsmount
*mnt
, struct super_block
*h_sb
)
32 if (!au_test_fuse(h_sb
) || !au_userns
)
35 return is_current_mnt_ns(mnt
) ? 0 : -EACCES
;
39 int vfsub_sync_filesystem(struct super_block
*h_sb
, int wait
)
44 down_read(&h_sb
->s_umount
);
45 err
= __sync_filesystem(h_sb
, wait
);
46 up_read(&h_sb
->s_umount
);
52 /* ---------------------------------------------------------------------- */
54 int vfsub_update_h_iattr(struct path
*h_path
, int *did
)
58 struct super_block
*h_sb
;
60 /* for remote fs, leave work for its getattr or d_revalidate */
61 /* for bad i_attr fs, handle them in aufs_getattr() */
62 /* still some fs may acquire i_mutex. we need to skip them */
66 h_sb
= h_path
->dentry
->d_sb
;
67 *did
= (!au_test_fs_remote(h_sb
) && au_test_fs_refresh_iattr(h_sb
));
69 err
= vfsub_getattr(h_path
, &st
);
74 /* ---------------------------------------------------------------------- */
76 struct file
*vfsub_dentry_open(struct path
*path
, int flags
)
80 file
= dentry_open(path
, flags
/* | __FMODE_NONOTIFY */,
82 if (!IS_ERR_OR_NULL(file
)
83 && (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) == FMODE_READ
)
84 i_readcount_inc(d_inode(path
->dentry
));
89 struct file
*vfsub_filp_open(const char *path
, int oflags
, int mode
)
94 file
= filp_open(path
,
95 oflags
/* | __FMODE_NONOTIFY */,
100 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
107 * Ideally this function should call VFS:do_last() in order to keep all its
108 * checkings. But it is very hard for aufs to regenerate several VFS internal
109 * structure such as nameidata. This is a second (or third) best approach.
110 * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
112 int vfsub_atomic_open(struct inode
*dir
, struct dentry
*dentry
,
113 struct vfsub_aopen_args
*args
, struct au_branch
*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 args
->file
->f_path
.dentry
= DENTRY_NOT_SET
;
128 args
->file
->f_path
.mnt
= au_br_mnt(br
);
129 err
= dir
->i_op
->atomic_open(dir
, dentry
, file
, args
->open_flag
,
130 args
->create_mode
, args
->opened
);
132 /* some filesystems don't set FILE_CREATED while succeeded? */
133 if (*args
->opened
& FILE_CREATED
)
134 fsnotify_create(dir
, dentry
);
140 /* todo: call VFS:may_open() here */
141 err
= open_check_o_direct(file
);
142 /* todo: ima_file_check() too? */
143 if (!err
&& (args
->open_flag
& __FMODE_EXEC
))
144 err
= deny_write_access(file
);
146 /* note that the file is created and still opened */
157 int vfsub_kern_path(const char *name
, unsigned int flags
, struct path
*path
)
161 err
= kern_path(name
, flags
, path
);
162 if (!err
&& d_is_positive(path
->dentry
))
163 vfsub_update_h_iattr(path
, /*did*/NULL
); /*ignore*/
167 struct dentry
*vfsub_lookup_one_len_unlocked(const char *name
,
168 struct dentry
*parent
, int len
)
174 path
.dentry
= lookup_one_len_unlocked(name
, parent
, len
);
175 if (IS_ERR(path
.dentry
))
177 if (d_is_positive(path
.dentry
))
178 vfsub_update_h_iattr(&path
, /*did*/NULL
); /*ignore*/
181 AuTraceErrPtr(path
.dentry
);
185 struct dentry
*vfsub_lookup_one_len(const char *name
, struct dentry
*parent
,
192 /* VFS checks it too, but by WARN_ON_ONCE() */
193 IMustLock(d_inode(parent
));
195 path
.dentry
= lookup_one_len(name
, parent
, len
);
196 if (IS_ERR(path
.dentry
))
198 if (d_is_positive(path
.dentry
))
199 vfsub_update_h_iattr(&path
, /*did*/NULL
); /*ignore*/
202 AuTraceErrPtr(path
.dentry
);
206 void vfsub_call_lkup_one(void *args
)
208 struct vfsub_lkup_one_args
*a
= args
;
209 *a
->errp
= vfsub_lkup_one(a
->name
, a
->parent
);
212 /* ---------------------------------------------------------------------- */
214 struct dentry
*vfsub_lock_rename(struct dentry
*d1
, struct au_hinode
*hdir1
,
215 struct dentry
*d2
, struct au_hinode
*hdir2
)
220 d
= lock_rename(d1
, d2
);
222 au_hn_suspend(hdir1
);
224 au_hn_suspend(hdir2
);
229 void vfsub_unlock_rename(struct dentry
*d1
, struct au_hinode
*hdir1
,
230 struct dentry
*d2
, struct au_hinode
*hdir2
)
236 unlock_rename(d1
, d2
);
240 /* ---------------------------------------------------------------------- */
242 int vfsub_create(struct inode
*dir
, struct path
*path
, int mode
, bool want_excl
)
250 path
->dentry
= d
->d_parent
;
251 err
= security_path_mknod(path
, d
, mode
, 0);
257 err
= vfs_create(dir
, path
->dentry
, mode
, want_excl
);
260 struct path tmp
= *path
;
263 vfsub_update_h_iattr(&tmp
, &did
);
265 tmp
.dentry
= path
->dentry
->d_parent
;
266 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
275 int vfsub_symlink(struct inode
*dir
, struct path
*path
, const char *symname
)
283 path
->dentry
= d
->d_parent
;
284 err
= security_path_symlink(path
, d
, symname
);
290 err
= vfs_symlink(dir
, path
->dentry
, symname
);
293 struct path tmp
= *path
;
296 vfsub_update_h_iattr(&tmp
, &did
);
298 tmp
.dentry
= path
->dentry
->d_parent
;
299 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
308 int vfsub_mknod(struct inode
*dir
, struct path
*path
, int mode
, dev_t dev
)
316 path
->dentry
= d
->d_parent
;
317 err
= security_path_mknod(path
, d
, mode
, new_encode_dev(dev
));
323 err
= vfs_mknod(dir
, path
->dentry
, mode
, dev
);
326 struct path tmp
= *path
;
329 vfsub_update_h_iattr(&tmp
, &did
);
331 tmp
.dentry
= path
->dentry
->d_parent
;
332 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
341 static int au_test_nlink(struct inode
*inode
)
343 const unsigned int link_max
= UINT_MAX
>> 1; /* rough margin */
345 if (!au_test_fs_no_limit_nlink(inode
->i_sb
)
346 || inode
->i_nlink
< link_max
)
351 int vfsub_link(struct dentry
*src_dentry
, struct inode
*dir
, struct path
*path
,
352 struct inode
**delegated_inode
)
359 err
= au_test_nlink(d_inode(src_dentry
));
363 /* we don't call may_linkat() */
365 path
->dentry
= d
->d_parent
;
366 err
= security_path_link(src_dentry
, path
, d
);
372 err
= vfs_link(src_dentry
, dir
, path
->dentry
, delegated_inode
);
375 struct path tmp
= *path
;
378 /* fuse has different memory inode for the same inumber */
379 vfsub_update_h_iattr(&tmp
, &did
);
381 tmp
.dentry
= path
->dentry
->d_parent
;
382 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
383 tmp
.dentry
= src_dentry
;
384 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
393 int vfsub_rename(struct inode
*src_dir
, struct dentry
*src_dentry
,
394 struct inode
*dir
, struct path
*path
,
395 struct inode
**delegated_inode
, unsigned int flags
)
407 path
->dentry
= d
->d_parent
;
408 tmp
.dentry
= src_dentry
->d_parent
;
409 err
= security_path_rename(&tmp
, src_dentry
, path
, d
, /*flags*/0);
415 err
= vfs_rename(src_dir
, src_dentry
, dir
, path
->dentry
,
416 delegated_inode
, flags
);
421 tmp
.dentry
= d
->d_parent
;
422 vfsub_update_h_iattr(&tmp
, &did
);
424 tmp
.dentry
= src_dentry
;
425 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
426 tmp
.dentry
= src_dentry
->d_parent
;
427 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
436 int vfsub_mkdir(struct inode
*dir
, struct path
*path
, int mode
)
444 path
->dentry
= d
->d_parent
;
445 err
= security_path_mkdir(path
, d
, mode
);
451 err
= vfs_mkdir(dir
, path
->dentry
, mode
);
454 struct path tmp
= *path
;
457 vfsub_update_h_iattr(&tmp
, &did
);
459 tmp
.dentry
= path
->dentry
->d_parent
;
460 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
469 int vfsub_rmdir(struct inode
*dir
, struct path
*path
)
477 path
->dentry
= d
->d_parent
;
478 err
= security_path_rmdir(path
, d
);
484 err
= vfs_rmdir(dir
, path
->dentry
);
488 .dentry
= path
->dentry
->d_parent
,
492 vfsub_update_h_iattr(&tmp
, /*did*/NULL
); /*ignore*/
499 /* ---------------------------------------------------------------------- */
501 /* todo: support mmap_sem? */
502 ssize_t
vfsub_read_u(struct file
*file
, char __user
*ubuf
, size_t count
,
508 err
= vfs_read(file
, ubuf
, count
, ppos
);
511 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
515 /* todo: kernel_read()? */
516 ssize_t
vfsub_read_k(struct file
*file
, void *kbuf
, size_t count
,
529 err
= vfsub_read_u(file
, buf
.u
, count
, ppos
);
534 ssize_t
vfsub_write_u(struct file
*file
, const char __user
*ubuf
, size_t count
,
540 err
= vfs_write(file
, ubuf
, count
, ppos
);
543 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
547 ssize_t
vfsub_write_k(struct file
*file
, void *kbuf
, size_t count
, loff_t
*ppos
)
553 const char __user
*u
;
559 err
= vfsub_write_u(file
, buf
.u
, count
, ppos
);
564 int vfsub_flush(struct file
*file
, fl_owner_t id
)
569 if (file
->f_op
->flush
) {
570 if (!au_test_nfs(file
->f_path
.dentry
->d_sb
))
571 err
= file
->f_op
->flush(file
, id
);
574 err
= file
->f_op
->flush(file
, id
);
578 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
);
584 int vfsub_iterate_dir(struct file
*file
, struct dir_context
*ctx
)
588 AuDbg("%pD, ctx{%pf, %llu}\n", file
, ctx
->actor
, ctx
->pos
);
591 err
= iterate_dir(file
, ctx
);
594 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
599 long vfsub_splice_to(struct file
*in
, loff_t
*ppos
,
600 struct pipe_inode_info
*pipe
, size_t len
,
606 err
= do_splice_to(in
, ppos
, pipe
, len
, flags
);
610 vfsub_update_h_iattr(&in
->f_path
, /*did*/NULL
); /*ignore*/
614 long vfsub_splice_from(struct pipe_inode_info
*pipe
, struct file
*out
,
615 loff_t
*ppos
, size_t len
, unsigned int flags
)
620 err
= do_splice_from(pipe
, out
, ppos
, len
, flags
);
623 vfsub_update_h_iattr(&out
->f_path
, /*did*/NULL
); /*ignore*/
627 int vfsub_fsync(struct file
*file
, struct path
*path
, int datasync
)
631 /* file can be NULL */
633 err
= vfs_fsync(file
, datasync
);
638 path
= &file
->f_path
;
640 vfsub_update_h_iattr(path
, /*did*/NULL
); /*ignore*/
645 /* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
646 int vfsub_trunc(struct path
*h_path
, loff_t length
, unsigned int attr
,
650 struct inode
*h_inode
;
651 struct super_block
*h_sb
;
654 err
= vfsub_truncate(h_path
, length
);
658 h_inode
= d_inode(h_path
->dentry
);
659 h_sb
= h_inode
->i_sb
;
661 sb_start_write(h_sb
);
663 err
= locks_verify_truncate(h_inode
, h_file
, length
);
665 err
= security_path_truncate(h_path
);
668 err
= do_truncate(h_path
->dentry
, length
, attr
, h_file
);
679 /* ---------------------------------------------------------------------- */
681 struct au_vfsub_mkdir_args
{
688 static void au_call_vfsub_mkdir(void *args
)
690 struct au_vfsub_mkdir_args
*a
= args
;
691 *a
->errp
= vfsub_mkdir(a
->dir
, a
->path
, a
->mode
);
694 int vfsub_sio_mkdir(struct inode
*dir
, struct path
*path
, int mode
)
696 int err
, do_sio
, wkq_err
;
698 do_sio
= au_test_h_perm_sio(dir
, MAY_EXEC
| MAY_WRITE
);
701 err
= vfsub_mkdir(dir
, path
, mode
);
704 struct au_vfsub_mkdir_args args
= {
710 wkq_err
= au_wkq_wait(au_call_vfsub_mkdir
, &args
);
711 if (unlikely(wkq_err
))
718 struct au_vfsub_rmdir_args
{
724 static void au_call_vfsub_rmdir(void *args
)
726 struct au_vfsub_rmdir_args
*a
= args
;
727 *a
->errp
= vfsub_rmdir(a
->dir
, a
->path
);
730 int vfsub_sio_rmdir(struct inode
*dir
, struct path
*path
)
732 int err
, do_sio
, wkq_err
;
734 do_sio
= au_test_h_perm_sio(dir
, MAY_EXEC
| MAY_WRITE
);
737 err
= vfsub_rmdir(dir
, path
);
740 struct au_vfsub_rmdir_args args
= {
745 wkq_err
= au_wkq_wait(au_call_vfsub_rmdir
, &args
);
746 if (unlikely(wkq_err
))
753 /* ---------------------------------------------------------------------- */
755 struct notify_change_args
{
759 struct inode
**delegated_inode
;
762 static void call_notify_change(void *args
)
764 struct notify_change_args
*a
= args
;
765 struct inode
*h_inode
;
767 h_inode
= d_inode(a
->path
->dentry
);
771 if (!IS_IMMUTABLE(h_inode
) && !IS_APPEND(h_inode
)) {
773 *a
->errp
= notify_change(a
->path
->dentry
, a
->ia
,
777 vfsub_update_h_iattr(a
->path
, /*did*/NULL
); /*ignore*/
779 AuTraceErr(*a
->errp
);
782 int vfsub_notify_change(struct path
*path
, struct iattr
*ia
,
783 struct inode
**delegated_inode
)
786 struct notify_change_args args
= {
790 .delegated_inode
= delegated_inode
793 call_notify_change(&args
);
798 int vfsub_sio_notify_change(struct path
*path
, struct iattr
*ia
,
799 struct inode
**delegated_inode
)
802 struct notify_change_args args
= {
806 .delegated_inode
= delegated_inode
809 wkq_err
= au_wkq_wait(call_notify_change
, &args
);
810 if (unlikely(wkq_err
))
816 /* ---------------------------------------------------------------------- */
822 struct inode
**delegated_inode
;
825 static void call_unlink(void *args
)
827 struct unlink_args
*a
= args
;
828 struct dentry
*d
= a
->path
->dentry
;
829 struct inode
*h_inode
;
830 const int stop_sillyrename
= (au_test_nfs(d
->d_sb
)
831 && au_dcount(d
) == 1);
835 a
->path
->dentry
= d
->d_parent
;
836 *a
->errp
= security_path_unlink(a
->path
, d
);
838 if (unlikely(*a
->errp
))
841 if (!stop_sillyrename
)
844 if (d_is_positive(d
)) {
845 h_inode
= d_inode(d
);
850 *a
->errp
= vfs_unlink(a
->dir
, d
, a
->delegated_inode
);
854 .dentry
= d
->d_parent
,
857 vfsub_update_h_iattr(&tmp
, /*did*/NULL
); /*ignore*/
860 if (!stop_sillyrename
)
865 AuTraceErr(*a
->errp
);
869 * @dir: must be locked.
870 * @dentry: target dentry.
872 int vfsub_unlink(struct inode
*dir
, struct path
*path
,
873 struct inode
**delegated_inode
, int force
)
876 struct unlink_args args
= {
880 .delegated_inode
= delegated_inode
888 wkq_err
= au_wkq_wait(call_unlink
, &args
);
889 if (unlikely(wkq_err
))