]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/aufs/vfsub.c
2 * Copyright (C) 2005-2016 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/namei.h>
23 #include <linux/nsproxy.h>
24 #include <linux/security.h>
25 #include <linux/splice.h>
26 #include "../fs/mount.h"
29 #ifdef CONFIG_AUFS_BR_FUSE
30 int vfsub_test_mntns(struct vfsmount
*mnt
, struct super_block
*h_sb
)
34 if (!au_test_fuse(h_sb
) || !au_userns
)
37 ns
= current
->nsproxy
;
38 /* no {get,put}_nsproxy(ns) */
39 return real_mount(mnt
)->mnt_ns
== ns
->mnt_ns
? 0 : -EACCES
;
43 /* ---------------------------------------------------------------------- */
45 int vfsub_update_h_iattr(struct path
*h_path
, int *did
)
49 struct super_block
*h_sb
;
51 /* for remote fs, leave work for its getattr or d_revalidate */
52 /* for bad i_attr fs, handle them in aufs_getattr() */
53 /* still some fs may acquire i_mutex. we need to skip them */
57 h_sb
= h_path
->dentry
->d_sb
;
58 *did
= (!au_test_fs_remote(h_sb
) && au_test_fs_refresh_iattr(h_sb
));
60 err
= vfs_getattr(h_path
, &st
);
65 /* ---------------------------------------------------------------------- */
67 struct file
*vfsub_dentry_open(struct path
*path
, int flags
)
71 file
= dentry_open(path
, flags
/* | __FMODE_NONOTIFY */,
73 if (!IS_ERR_OR_NULL(file
)
74 && (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) == FMODE_READ
)
75 i_readcount_inc(d_inode(path
->dentry
));
80 struct file
*vfsub_filp_open(const char *path
, int oflags
, int mode
)
85 file
= filp_open(path
,
86 oflags
/* | __FMODE_NONOTIFY */,
91 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
98 * Ideally this function should call VFS:do_last() in order to keep all its
99 * checkings. But it is very hard for aufs to regenerate several VFS internal
100 * structure such as nameidata. This is a second (or third) best approach.
101 * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
103 int vfsub_atomic_open(struct inode
*dir
, struct dentry
*dentry
,
104 struct vfsub_aopen_args
*args
, struct au_branch
*br
)
107 struct file
*file
= args
->file
;
108 /* copied from linux/fs/namei.c:atomic_open() */
109 struct dentry
*const DENTRY_NOT_SET
= (void *)-1UL;
112 AuDebugOn(!dir
->i_op
->atomic_open
);
114 err
= au_br_test_oflag(args
->open_flag
, br
);
118 args
->file
->f_path
.dentry
= DENTRY_NOT_SET
;
119 args
->file
->f_path
.mnt
= au_br_mnt(br
);
120 err
= dir
->i_op
->atomic_open(dir
, dentry
, file
, args
->open_flag
,
121 args
->create_mode
, args
->opened
);
123 /* some filesystems don't set FILE_CREATED while succeeded? */
124 if (*args
->opened
& FILE_CREATED
)
125 fsnotify_create(dir
, dentry
);
131 /* todo: call VFS:may_open() here */
132 err
= open_check_o_direct(file
);
133 /* todo: ima_file_check() too? */
134 if (!err
&& (args
->open_flag
& __FMODE_EXEC
))
135 err
= deny_write_access(file
);
137 /* note that the file is created and still opened */
148 int vfsub_kern_path(const char *name
, unsigned int flags
, struct path
*path
)
152 err
= kern_path(name
, flags
, path
);
153 if (!err
&& d_is_positive(path
->dentry
))
154 vfsub_update_h_iattr(path
, /*did*/NULL
); /*ignore*/
158 struct dentry
*vfsub_lookup_one_len_unlocked(const char *name
,
159 struct dentry
*parent
, int len
)
165 path
.dentry
= lookup_one_len_unlocked(name
, parent
, len
);
166 if (IS_ERR(path
.dentry
))
168 if (d_is_positive(path
.dentry
))
169 vfsub_update_h_iattr(&path
, /*did*/NULL
); /*ignore*/
172 AuTraceErrPtr(path
.dentry
);
176 struct dentry
*vfsub_lookup_one_len(const char *name
, struct dentry
*parent
,
183 /* VFS checks it too, but by WARN_ON_ONCE() */
184 IMustLock(d_inode(parent
));
186 path
.dentry
= lookup_one_len(name
, parent
, len
);
187 if (IS_ERR(path
.dentry
))
189 if (d_is_positive(path
.dentry
))
190 vfsub_update_h_iattr(&path
, /*did*/NULL
); /*ignore*/
193 AuTraceErrPtr(path
.dentry
);
197 void vfsub_call_lkup_one(void *args
)
199 struct vfsub_lkup_one_args
*a
= args
;
200 *a
->errp
= vfsub_lkup_one(a
->name
, a
->parent
);
203 /* ---------------------------------------------------------------------- */
205 struct dentry
*vfsub_lock_rename(struct dentry
*d1
, struct au_hinode
*hdir1
,
206 struct dentry
*d2
, struct au_hinode
*hdir2
)
211 d
= lock_rename(d1
, d2
);
213 au_hn_suspend(hdir1
);
215 au_hn_suspend(hdir2
);
220 void vfsub_unlock_rename(struct dentry
*d1
, struct au_hinode
*hdir1
,
221 struct dentry
*d2
, struct au_hinode
*hdir2
)
227 unlock_rename(d1
, d2
);
231 /* ---------------------------------------------------------------------- */
233 int vfsub_create(struct inode
*dir
, struct path
*path
, int mode
, bool want_excl
)
241 path
->dentry
= d
->d_parent
;
242 err
= security_path_mknod(path
, d
, mode
, 0);
248 err
= vfs_create(dir
, path
->dentry
, mode
, want_excl
);
251 struct path tmp
= *path
;
254 vfsub_update_h_iattr(&tmp
, &did
);
256 tmp
.dentry
= path
->dentry
->d_parent
;
257 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
266 int vfsub_symlink(struct inode
*dir
, struct path
*path
, const char *symname
)
274 path
->dentry
= d
->d_parent
;
275 err
= security_path_symlink(path
, d
, symname
);
281 err
= vfs_symlink(dir
, path
->dentry
, symname
);
284 struct path tmp
= *path
;
287 vfsub_update_h_iattr(&tmp
, &did
);
289 tmp
.dentry
= path
->dentry
->d_parent
;
290 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
299 int vfsub_mknod(struct inode
*dir
, struct path
*path
, int mode
, dev_t dev
)
307 path
->dentry
= d
->d_parent
;
308 err
= security_path_mknod(path
, d
, mode
, new_encode_dev(dev
));
314 err
= vfs_mknod(dir
, path
->dentry
, mode
, dev
);
317 struct path tmp
= *path
;
320 vfsub_update_h_iattr(&tmp
, &did
);
322 tmp
.dentry
= path
->dentry
->d_parent
;
323 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
332 static int au_test_nlink(struct inode
*inode
)
334 const unsigned int link_max
= UINT_MAX
>> 1; /* rough margin */
336 if (!au_test_fs_no_limit_nlink(inode
->i_sb
)
337 || inode
->i_nlink
< link_max
)
342 int vfsub_link(struct dentry
*src_dentry
, struct inode
*dir
, struct path
*path
,
343 struct inode
**delegated_inode
)
350 err
= au_test_nlink(d_inode(src_dentry
));
354 /* we don't call may_linkat() */
356 path
->dentry
= d
->d_parent
;
357 err
= security_path_link(src_dentry
, path
, d
);
363 err
= vfs_link(src_dentry
, dir
, path
->dentry
, delegated_inode
);
366 struct path tmp
= *path
;
369 /* fuse has different memory inode for the same inumber */
370 vfsub_update_h_iattr(&tmp
, &did
);
372 tmp
.dentry
= path
->dentry
->d_parent
;
373 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
374 tmp
.dentry
= src_dentry
;
375 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
384 int vfsub_rename(struct inode
*src_dir
, struct dentry
*src_dentry
,
385 struct inode
*dir
, struct path
*path
,
386 struct inode
**delegated_inode
)
398 path
->dentry
= d
->d_parent
;
399 tmp
.dentry
= src_dentry
->d_parent
;
400 err
= security_path_rename(&tmp
, src_dentry
, path
, d
, /*flags*/0);
406 err
= vfs_rename(src_dir
, src_dentry
, dir
, path
->dentry
,
407 delegated_inode
, /*flags*/0);
412 tmp
.dentry
= d
->d_parent
;
413 vfsub_update_h_iattr(&tmp
, &did
);
415 tmp
.dentry
= src_dentry
;
416 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
417 tmp
.dentry
= src_dentry
->d_parent
;
418 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
427 int vfsub_mkdir(struct inode
*dir
, struct path
*path
, int mode
)
435 path
->dentry
= d
->d_parent
;
436 err
= security_path_mkdir(path
, d
, mode
);
442 err
= vfs_mkdir(dir
, path
->dentry
, mode
);
445 struct path tmp
= *path
;
448 vfsub_update_h_iattr(&tmp
, &did
);
450 tmp
.dentry
= path
->dentry
->d_parent
;
451 vfsub_update_h_iattr(&tmp
, /*did*/NULL
);
460 int vfsub_rmdir(struct inode
*dir
, struct path
*path
)
468 path
->dentry
= d
->d_parent
;
469 err
= security_path_rmdir(path
, d
);
475 err
= vfs_rmdir(dir
, path
->dentry
);
479 .dentry
= path
->dentry
->d_parent
,
483 vfsub_update_h_iattr(&tmp
, /*did*/NULL
); /*ignore*/
490 /* ---------------------------------------------------------------------- */
492 /* todo: support mmap_sem? */
493 ssize_t
vfsub_read_u(struct file
*file
, char __user
*ubuf
, size_t count
,
499 err
= vfs_read(file
, ubuf
, count
, ppos
);
502 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
506 /* todo: kernel_read()? */
507 ssize_t
vfsub_read_k(struct file
*file
, void *kbuf
, size_t count
,
520 err
= vfsub_read_u(file
, buf
.u
, count
, ppos
);
525 ssize_t
vfsub_write_u(struct file
*file
, const char __user
*ubuf
, size_t count
,
531 err
= vfs_write(file
, ubuf
, count
, ppos
);
534 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
538 ssize_t
vfsub_write_k(struct file
*file
, void *kbuf
, size_t count
, loff_t
*ppos
)
544 const char __user
*u
;
550 err
= vfsub_write_u(file
, buf
.u
, count
, ppos
);
555 int vfsub_flush(struct file
*file
, fl_owner_t id
)
560 if (file
->f_op
->flush
) {
561 if (!au_test_nfs(file
->f_path
.dentry
->d_sb
))
562 err
= file
->f_op
->flush(file
, id
);
565 err
= file
->f_op
->flush(file
, id
);
569 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
);
575 int vfsub_iterate_dir(struct file
*file
, struct dir_context
*ctx
)
579 AuDbg("%pD, ctx{%pf, %llu}\n", file
, ctx
->actor
, ctx
->pos
);
582 err
= iterate_dir(file
, ctx
);
585 vfsub_update_h_iattr(&file
->f_path
, /*did*/NULL
); /*ignore*/
589 long vfsub_splice_to(struct file
*in
, loff_t
*ppos
,
590 struct pipe_inode_info
*pipe
, size_t len
,
596 err
= do_splice_to(in
, ppos
, pipe
, len
, flags
);
600 vfsub_update_h_iattr(&in
->f_path
, /*did*/NULL
); /*ignore*/
604 long vfsub_splice_from(struct pipe_inode_info
*pipe
, struct file
*out
,
605 loff_t
*ppos
, size_t len
, unsigned int flags
)
610 err
= do_splice_from(pipe
, out
, ppos
, len
, flags
);
613 vfsub_update_h_iattr(&out
->f_path
, /*did*/NULL
); /*ignore*/
617 int vfsub_fsync(struct file
*file
, struct path
*path
, int datasync
)
621 /* file can be NULL */
623 err
= vfs_fsync(file
, datasync
);
628 path
= &file
->f_path
;
630 vfsub_update_h_iattr(path
, /*did*/NULL
); /*ignore*/
635 /* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
636 int vfsub_trunc(struct path
*h_path
, loff_t length
, unsigned int attr
,
640 struct inode
*h_inode
;
641 struct super_block
*h_sb
;
644 err
= vfsub_truncate(h_path
, length
);
648 h_inode
= d_inode(h_path
->dentry
);
649 h_sb
= h_inode
->i_sb
;
651 sb_start_write(h_sb
);
653 err
= locks_verify_truncate(h_inode
, h_file
, length
);
655 err
= security_path_truncate(h_path
);
658 err
= do_truncate(h_path
->dentry
, length
, attr
, h_file
);
669 /* ---------------------------------------------------------------------- */
671 struct au_vfsub_mkdir_args
{
678 static void au_call_vfsub_mkdir(void *args
)
680 struct au_vfsub_mkdir_args
*a
= args
;
681 *a
->errp
= vfsub_mkdir(a
->dir
, a
->path
, a
->mode
);
684 int vfsub_sio_mkdir(struct inode
*dir
, struct path
*path
, int mode
)
686 int err
, do_sio
, wkq_err
;
688 do_sio
= au_test_h_perm_sio(dir
, MAY_EXEC
| MAY_WRITE
);
691 err
= vfsub_mkdir(dir
, path
, mode
);
694 struct au_vfsub_mkdir_args args
= {
700 wkq_err
= au_wkq_wait(au_call_vfsub_mkdir
, &args
);
701 if (unlikely(wkq_err
))
708 struct au_vfsub_rmdir_args
{
714 static void au_call_vfsub_rmdir(void *args
)
716 struct au_vfsub_rmdir_args
*a
= args
;
717 *a
->errp
= vfsub_rmdir(a
->dir
, a
->path
);
720 int vfsub_sio_rmdir(struct inode
*dir
, struct path
*path
)
722 int err
, do_sio
, wkq_err
;
724 do_sio
= au_test_h_perm_sio(dir
, MAY_EXEC
| MAY_WRITE
);
727 err
= vfsub_rmdir(dir
, path
);
730 struct au_vfsub_rmdir_args args
= {
735 wkq_err
= au_wkq_wait(au_call_vfsub_rmdir
, &args
);
736 if (unlikely(wkq_err
))
743 /* ---------------------------------------------------------------------- */
745 struct notify_change_args
{
749 struct inode
**delegated_inode
;
752 static void call_notify_change(void *args
)
754 struct notify_change_args
*a
= args
;
755 struct inode
*h_inode
;
757 h_inode
= d_inode(a
->path
->dentry
);
761 if (!IS_IMMUTABLE(h_inode
) && !IS_APPEND(h_inode
)) {
763 *a
->errp
= notify_change(a
->path
->dentry
, a
->ia
,
767 vfsub_update_h_iattr(a
->path
, /*did*/NULL
); /*ignore*/
769 AuTraceErr(*a
->errp
);
772 int vfsub_notify_change(struct path
*path
, struct iattr
*ia
,
773 struct inode
**delegated_inode
)
776 struct notify_change_args args
= {
780 .delegated_inode
= delegated_inode
783 call_notify_change(&args
);
788 int vfsub_sio_notify_change(struct path
*path
, struct iattr
*ia
,
789 struct inode
**delegated_inode
)
792 struct notify_change_args args
= {
796 .delegated_inode
= delegated_inode
799 wkq_err
= au_wkq_wait(call_notify_change
, &args
);
800 if (unlikely(wkq_err
))
806 /* ---------------------------------------------------------------------- */
812 struct inode
**delegated_inode
;
815 static void call_unlink(void *args
)
817 struct unlink_args
*a
= args
;
818 struct dentry
*d
= a
->path
->dentry
;
819 struct inode
*h_inode
;
820 const int stop_sillyrename
= (au_test_nfs(d
->d_sb
)
821 && au_dcount(d
) == 1);
825 a
->path
->dentry
= d
->d_parent
;
826 *a
->errp
= security_path_unlink(a
->path
, d
);
828 if (unlikely(*a
->errp
))
831 if (!stop_sillyrename
)
834 if (d_is_positive(d
)) {
835 h_inode
= d_inode(d
);
840 *a
->errp
= vfs_unlink(a
->dir
, d
, a
->delegated_inode
);
844 .dentry
= d
->d_parent
,
847 vfsub_update_h_iattr(&tmp
, /*did*/NULL
); /*ignore*/
850 if (!stop_sillyrename
)
855 AuTraceErr(*a
->errp
);
859 * @dir: must be locked.
860 * @dentry: target dentry.
862 int vfsub_unlink(struct inode
*dir
, struct path
*path
,
863 struct inode
**delegated_inode
, int force
)
866 struct unlink_args args
= {
870 .delegated_inode
= delegated_inode
878 wkq_err
= au_wkq_wait(call_unlink
, &args
);
879 if (unlikely(wkq_err
))