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 * inode operations (except add/del/rename)
22 #include <linux/device_cgroup.h>
23 #include <linux/fs_stack.h>
24 #include <linux/namei.h>
25 #include <linux/security.h>
28 static int h_permission(struct inode
*h_inode
, int mask
,
29 struct path
*h_path
, int brperm
)
32 const unsigned char write_mask
= !!(mask
& (MAY_WRITE
| MAY_APPEND
));
35 if (write_mask
&& IS_IMMUTABLE(h_inode
))
39 if (((mask
& MAY_EXEC
)
40 && S_ISREG(h_inode
->i_mode
)
41 && (path_noexec(h_path
)
42 || !(h_inode
->i_mode
& S_IXUGO
))))
46 * - skip the lower fs test in the case of write to ro branch.
47 * - nfs dir permission write check is optimized, but a policy for
48 * link/rename requires a real check.
49 * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
50 * in this case, generic_permission() returns -EOPNOTSUPP.
52 if ((write_mask
&& !au_br_writable(brperm
))
53 || (au_test_nfs(h_inode
->i_sb
) && S_ISDIR(h_inode
->i_mode
)
54 && write_mask
&& !(mask
& MAY_READ
))
55 || !h_inode
->i_op
->permission
) {
56 /* AuLabel(generic_permission); */
57 /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
58 err
= generic_permission(h_inode
, mask
);
59 if (err
== -EOPNOTSUPP
&& au_test_nfs_noacl(h_inode
))
60 err
= h_inode
->i_op
->permission(h_inode
, mask
);
63 /* AuLabel(h_inode->permission); */
64 err
= h_inode
->i_op
->permission(h_inode
, mask
);
69 err
= devcgroup_inode_permission(h_inode
, mask
);
71 err
= security_inode_permission(h_inode
, mask
);
75 /* todo: do we need to call ima_path_check()? */
76 struct path h_path
= {
80 err
= ima_path_check(&h_path
,
81 mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
),
90 static int aufs_permission(struct inode
*inode
, int mask
)
93 aufs_bindex_t bindex
, bbot
;
94 const unsigned char isdir
= !!S_ISDIR(inode
->i_mode
),
95 write_mask
= !!(mask
& (MAY_WRITE
| MAY_APPEND
));
96 struct inode
*h_inode
;
97 struct super_block
*sb
;
100 /* todo: support rcu-walk? */
101 if (mask
& MAY_NOT_BLOCK
)
105 si_read_lock(sb
, AuLock_FLUSH
);
106 ii_read_lock_child(inode
);
108 err
= au_iigen_test(inode
, au_sigen(sb
));
115 || au_opt_test(au_mntflags(sb
), DIRPERM1
)) {
116 err
= au_busy_or_stale();
117 h_inode
= au_h_iptr(inode
, au_ibtop(inode
));
118 if (unlikely(!h_inode
119 || (h_inode
->i_mode
& S_IFMT
)
120 != (inode
->i_mode
& S_IFMT
)))
124 bindex
= au_ibtop(inode
);
125 br
= au_sbr(sb
, bindex
);
126 err
= h_permission(h_inode
, mask
, &br
->br_path
, br
->br_perm
);
129 && !special_file(h_inode
->i_mode
)) {
130 /* test whether the upper writable branch exists */
132 for (; bindex
>= 0; bindex
--)
133 if (!au_br_rdonly(au_sbr(sb
, bindex
))) {
141 /* non-write to dir */
143 bbot
= au_ibbot(inode
);
144 for (bindex
= au_ibtop(inode
); !err
&& bindex
<= bbot
; bindex
++) {
145 h_inode
= au_h_iptr(inode
, bindex
);
147 err
= au_busy_or_stale();
148 if (unlikely(!S_ISDIR(h_inode
->i_mode
)))
151 br
= au_sbr(sb
, bindex
);
152 err
= h_permission(h_inode
, mask
, &br
->br_path
,
158 ii_read_unlock(inode
);
163 /* ---------------------------------------------------------------------- */
165 static struct dentry
*aufs_lookup(struct inode
*dir
, struct dentry
*dentry
,
168 struct dentry
*ret
, *parent
;
170 struct super_block
*sb
;
175 /* todo: support rcu-walk? */
176 ret
= ERR_PTR(-ECHILD
);
177 if (flags
& LOOKUP_RCU
)
180 ret
= ERR_PTR(-ENAMETOOLONG
);
181 if (unlikely(dentry
->d_name
.len
> AUFS_MAX_NAMELEN
))
185 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
190 err
= au_di_init(dentry
);
196 npositive
= 0; /* suppress a warning */
197 parent
= dentry
->d_parent
; /* dir inode is locked */
198 di_read_lock_parent(parent
, AuLock_IR
);
199 err
= au_alive_dir(parent
);
201 err
= au_digen_test(parent
, au_sigen(sb
));
203 /* regardless LOOKUP_CREATE, always ALLOW_NEG */
204 npositive
= au_lkup_dentry(dentry
, au_dbtop(parent
),
208 di_read_unlock(parent
, AuLock_IR
);
210 if (unlikely(err
< 0))
214 inode
= au_new_inode(dentry
, /*must_new*/0);
223 atomic_inc(&inode
->i_count
);
224 ret
= d_splice_alias(inode
, dentry
);
226 if (unlikely(d_need_lookup(dentry
))) {
227 spin_lock(&dentry
->d_lock
);
228 dentry
->d_flags
&= ~DCACHE_NEED_LOOKUP
;
229 spin_unlock(&dentry
->d_lock
);
235 if (ret
&& ret
!= dentry
)
236 ii_write_unlock(inode
);
238 ii_write_unlock(inode
);
245 di_write_unlock(dentry
);
252 /* ---------------------------------------------------------------------- */
255 struct hlist_node hlist
;
256 struct file
*file
, *h_file
;
259 static int au_do_aopen(struct inode
*inode
, struct file
*file
)
261 struct au_sphlhead
*aopen
;
262 struct aopen_node
*node
;
263 struct au_do_open_args args
= {
265 .open
= au_do_open_nondir
268 aopen
= &au_sbi(inode
->i_sb
)->si_aopen
;
269 spin_lock(&aopen
->spin
);
270 hlist_for_each_entry(node
, &aopen
->head
, hlist
)
271 if (node
->file
== file
) {
272 args
.h_file
= node
->h_file
;
275 spin_unlock(&aopen
->spin
);
276 /* AuDebugOn(!args.h_file); */
278 return au_do_open(file
, &args
);
281 static int aufs_atomic_open(struct inode
*dir
, struct dentry
*dentry
,
282 struct file
*file
, unsigned int open_flag
,
283 umode_t create_mode
, int *opened
)
285 int err
, unlocked
, h_opened
= *opened
;
286 unsigned int lkup_flags
;
287 struct dentry
*parent
, *d
;
288 struct au_sphlhead
*aopen
;
289 struct vfsub_aopen_args args
= {
290 .open_flag
= open_flag
,
291 .create_mode
= create_mode
,
294 struct aopen_node aopen_node
= {
299 AuDbg("open_flag 0%o\n", open_flag
);
303 if (!au_di(dentry
)) {
304 lkup_flags
= LOOKUP_OPEN
;
305 if (open_flag
& O_CREAT
)
306 lkup_flags
|= LOOKUP_CREATE
;
307 d
= aufs_lookup(dir
, dentry
, lkup_flags
);
314 * obsoleted dentry found.
315 * another error will be returned later.
324 if (d_is_positive(dentry
)
325 || d_unhashed(dentry
)
326 || d_unlinked(dentry
)
327 || !(open_flag
& O_CREAT
))
331 err
= aufs_read_lock(dentry
, AuLock_DW
| AuLock_FLUSH
| AuLock_GEN
);
335 parent
= dentry
->d_parent
; /* dir is locked */
336 di_write_lock_parent(parent
);
337 err
= au_lkup_dentry(dentry
, /*btop*/0, AuLkup_ALLOW_NEG
);
342 if (d_is_positive(dentry
))
345 args
.file
= get_empty_filp();
346 err
= PTR_ERR(args
.file
);
347 if (IS_ERR(args
.file
))
350 args
.file
->f_flags
= file
->f_flags
;
351 err
= au_aopen_or_create(dir
, dentry
, &args
);
353 AuDbgFile(args
.file
);
354 if (unlikely(err
< 0)) {
355 if (h_opened
& FILE_OPENED
)
361 di_write_unlock(parent
);
362 di_write_unlock(dentry
);
365 /* some filesystems don't set FILE_CREATED while succeeded? */
366 *opened
|= FILE_CREATED
;
367 if (h_opened
& FILE_OPENED
)
368 aopen_node
.h_file
= args
.file
;
373 aopen
= &au_sbi(dir
->i_sb
)->si_aopen
;
374 au_sphl_add(&aopen_node
.hlist
, aopen
);
375 err
= finish_open(file
, dentry
, au_do_aopen
, opened
);
376 au_sphl_del(&aopen_node
.hlist
, aopen
);
379 if (aopen_node
.h_file
)
380 fput(aopen_node
.h_file
);
384 si_read_unlock(dentry
->d_sb
);
386 di_write_unlock(parent
);
387 aufs_read_unlock(dentry
, AuLock_DW
);
390 if (unlikely(err
< 0))
393 if (err
>= 0 && !(*opened
& FILE_CREATED
)) {
394 AuLabel(out_no_open
);
396 err
= finish_no_open(file
, dentry
);
399 AuDbg("%pd%s%s\n", dentry
,
400 (*opened
& FILE_CREATED
) ? " created" : "",
401 (*opened
& FILE_OPENED
) ? " opened" : "");
407 /* ---------------------------------------------------------------------- */
409 static int au_wr_dir_cpup(struct dentry
*dentry
, struct dentry
*parent
,
410 const unsigned char add_entry
, aufs_bindex_t bcpup
,
414 struct dentry
*h_parent
;
418 IMustLock(d_inode(parent
));
420 di_write_lock_parent(parent
);
423 if (!au_h_dptr(parent
, bcpup
)) {
425 err
= au_cpup_dirs(dentry
, bcpup
);
426 else if (btop
< bcpup
)
427 err
= au_cpdown_dirs(dentry
, bcpup
);
431 if (!err
&& add_entry
&& !au_ftest_wrdir(add_entry
, TMPFILE
)) {
432 h_parent
= au_h_dptr(parent
, bcpup
);
433 h_dir
= d_inode(h_parent
);
434 vfsub_inode_lock_shared_nested(h_dir
, AuLsc_I_PARENT
);
435 err
= au_lkup_neg(dentry
, bcpup
, /*wh*/0);
436 /* todo: no unlock here */
437 inode_unlock_shared(h_dir
);
439 AuDbg("bcpup %d\n", bcpup
);
441 if (d_really_is_negative(dentry
))
442 au_set_h_dptr(dentry
, btop
, NULL
);
443 au_update_dbrange(dentry
, /*do_put_zero*/0);
448 di_write_unlock(parent
);
450 err
= bcpup
; /* success */
457 * decide the branch and the parent dir where we will create a new entry.
458 * returns new bindex or an error.
459 * copyup the parent dir if needed.
461 int au_wr_dir(struct dentry
*dentry
, struct dentry
*src_dentry
,
462 struct au_wr_dir_args
*args
)
466 aufs_bindex_t bcpup
, btop
, src_btop
;
467 const unsigned char add_entry
468 = au_ftest_wrdir(args
->flags
, ADD_ENTRY
)
469 | au_ftest_wrdir(args
->flags
, TMPFILE
);
470 struct super_block
*sb
;
471 struct dentry
*parent
;
472 struct au_sbinfo
*sbinfo
;
476 parent
= dget_parent(dentry
);
477 btop
= au_dbtop(dentry
);
479 if (args
->force_btgt
< 0) {
481 src_btop
= au_dbtop(src_dentry
);
484 } else if (add_entry
) {
486 if (au_ftest_wrdir(args
->flags
, ISDIR
))
487 au_fset_wbr(flags
, DIR);
488 err
= AuWbrCreate(sbinfo
, dentry
, flags
);
492 if (bcpup
< 0 || au_test_ro(sb
, bcpup
, d_inode(dentry
))) {
494 err
= AuWbrCopyup(sbinfo
, dentry
);
496 if (!IS_ROOT(dentry
)) {
497 di_read_lock_parent(parent
, !AuLock_IR
);
498 err
= AuWbrCopyup(sbinfo
, dentry
);
499 di_read_unlock(parent
, !AuLock_IR
);
501 err
= AuWbrCopyup(sbinfo
, dentry
);
504 if (unlikely(err
< 0))
508 bcpup
= args
->force_btgt
;
509 AuDebugOn(au_test_ro(sb
, bcpup
, d_inode(dentry
)));
512 AuDbg("btop %d, bcpup %d\n", btop
, bcpup
);
515 goto out
; /* success */
517 /* copyup the new parent into the branch we process */
518 err
= au_wr_dir_cpup(dentry
, parent
, add_entry
, bcpup
, btop
);
520 if (d_really_is_negative(dentry
)) {
521 au_set_h_dptr(dentry
, btop
, NULL
);
522 au_set_dbtop(dentry
, bcpup
);
523 au_set_dbbot(dentry
, bcpup
);
526 && !au_ftest_wrdir(args
->flags
, TMPFILE
)
527 && !au_h_dptr(dentry
, bcpup
));
535 /* ---------------------------------------------------------------------- */
537 void au_pin_hdir_unlock(struct au_pin
*p
)
540 au_hn_inode_unlock(p
->hdir
);
543 int au_pin_hdir_lock(struct au_pin
*p
)
551 /* even if an error happens later, keep this lock */
552 au_hn_inode_lock_nested(p
->hdir
, p
->lsc_hi
);
555 if (unlikely(p
->hdir
->hi_inode
!= d_inode(p
->h_parent
)))
560 err
= au_h_verify(p
->h_dentry
, p
->udba
, p
->hdir
->hi_inode
,
567 int au_pin_hdir_relock(struct au_pin
*p
)
571 struct dentry
*h_d
[] = {
576 err
= au_pin_hdir_lock(p
);
580 for (i
= 0; !err
&& i
< sizeof(h_d
)/sizeof(*h_d
); i
++) {
583 if (d_is_positive(h_d
[i
])) {
584 h_i
= d_inode(h_d
[i
]);
593 static void au_pin_hdir_set_owner(struct au_pin
*p
, struct task_struct
*task
)
595 #if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
596 p
->hdir
->hi_inode
->i_rwsem
.owner
= task
;
600 void au_pin_hdir_acquire_nest(struct au_pin
*p
)
603 rwsem_acquire_nest(&p
->hdir
->hi_inode
->i_rwsem
.dep_map
,
604 p
->lsc_hi
, 0, NULL
, _RET_IP_
);
605 au_pin_hdir_set_owner(p
, current
);
609 void au_pin_hdir_release(struct au_pin
*p
)
612 au_pin_hdir_set_owner(p
, p
->task
);
613 rwsem_release(&p
->hdir
->hi_inode
->i_rwsem
.dep_map
, 1, _RET_IP_
);
617 struct dentry
*au_pinned_h_parent(struct au_pin
*pin
)
619 if (pin
&& pin
->parent
)
620 return au_h_dptr(pin
->parent
, pin
->bindex
);
624 void au_unpin(struct au_pin
*p
)
627 au_pin_hdir_unlock(p
);
628 if (p
->h_mnt
&& au_ftest_pin(p
->flags
, MNT_WRITE
))
629 vfsub_mnt_drop_write(p
->h_mnt
);
633 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
634 di_read_unlock(p
->parent
, AuLock_IR
);
635 iput(p
->hdir
->hi_inode
);
640 /* do not clear p->task */
643 int au_do_pin(struct au_pin
*p
)
646 struct super_block
*sb
;
650 sb
= p
->dentry
->d_sb
;
651 p
->br
= au_sbr(sb
, p
->bindex
);
652 if (IS_ROOT(p
->dentry
)) {
653 if (au_ftest_pin(p
->flags
, MNT_WRITE
)) {
654 p
->h_mnt
= au_br_mnt(p
->br
);
655 err
= vfsub_mnt_want_write(p
->h_mnt
);
657 au_fclr_pin(p
->flags
, MNT_WRITE
);
665 if (p
->bindex
<= au_dbbot(p
->dentry
))
666 p
->h_dentry
= au_h_dptr(p
->dentry
, p
->bindex
);
668 p
->parent
= dget_parent(p
->dentry
);
669 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
670 di_read_lock(p
->parent
, AuLock_IR
, p
->lsc_di
);
673 p
->h_parent
= au_h_dptr(p
->parent
, p
->bindex
);
674 p
->hdir
= au_hi(d_inode(p
->parent
), p
->bindex
);
676 h_dir
= p
->hdir
->hi_inode
;
680 * if DI_LOCKED is not set, then p->parent may be different
681 * and h_parent can be NULL.
683 if (unlikely(!p
->hdir
|| !h_dir
|| !p
->h_parent
)) {
685 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
686 di_read_unlock(p
->parent
, AuLock_IR
);
692 if (au_ftest_pin(p
->flags
, MNT_WRITE
)) {
693 p
->h_mnt
= au_br_mnt(p
->br
);
694 err
= vfsub_mnt_want_write(p
->h_mnt
);
696 au_fclr_pin(p
->flags
, MNT_WRITE
);
697 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
698 di_read_unlock(p
->parent
, AuLock_IR
);
706 err
= au_pin_hdir_lock(p
);
708 goto out
; /* success */
713 pr_err("err %d\n", err
);
714 err
= au_busy_or_stale();
719 void au_pin_init(struct au_pin
*p
, struct dentry
*dentry
,
720 aufs_bindex_t bindex
, int lsc_di
, int lsc_hi
,
721 unsigned int udba
, unsigned char flags
)
740 int au_pin(struct au_pin
*pin
, struct dentry
*dentry
, aufs_bindex_t bindex
,
741 unsigned int udba
, unsigned char flags
)
743 au_pin_init(pin
, dentry
, bindex
, AuLsc_DI_PARENT
, AuLsc_I_PARENT2
,
745 return au_do_pin(pin
);
748 /* ---------------------------------------------------------------------- */
751 * ->setattr() and ->getattr() are called in various cases.
752 * chmod, stat: dentry is revalidated.
753 * fchmod, fstat: file and dentry are not revalidated, additionally they may be
755 * for ->setattr(), ia->ia_file is passed from ftruncate only.
757 /* todo: consolidate with do_refresh() and simple_reval_dpath() */
758 int au_reval_for_attr(struct dentry
*dentry
, unsigned int sigen
)
761 struct dentry
*parent
;
764 if (au_digen_test(dentry
, sigen
)) {
765 parent
= dget_parent(dentry
);
766 di_read_lock_parent(parent
, AuLock_IR
);
767 err
= au_refresh_dentry(dentry
, parent
);
768 di_read_unlock(parent
, AuLock_IR
);
776 int au_pin_and_icpup(struct dentry
*dentry
, struct iattr
*ia
,
777 struct au_icpup_args
*a
)
781 aufs_bindex_t btop
, ibtop
;
782 struct dentry
*hi_wh
, *parent
;
784 struct au_wr_dir_args wr_dir_args
= {
789 if (d_is_dir(dentry
))
790 au_fset_wrdir(wr_dir_args
.flags
, ISDIR
);
791 /* plink or hi_wh() case */
792 btop
= au_dbtop(dentry
);
793 inode
= d_inode(dentry
);
794 ibtop
= au_ibtop(inode
);
795 if (btop
!= ibtop
&& !au_test_ro(inode
->i_sb
, ibtop
, inode
))
796 wr_dir_args
.force_btgt
= ibtop
;
797 err
= au_wr_dir(dentry
, /*src_dentry*/NULL
, &wr_dir_args
);
798 if (unlikely(err
< 0))
802 au_fset_icpup(a
->flags
, DID_CPUP
);
805 a
->pin_flags
= AuPin_MNT_WRITE
;
807 if (!IS_ROOT(dentry
)) {
808 au_fset_pin(a
->pin_flags
, DI_LOCKED
);
809 parent
= dget_parent(dentry
);
810 di_write_lock_parent(parent
);
813 err
= au_pin(&a
->pin
, dentry
, a
->btgt
, a
->udba
, a
->pin_flags
);
818 a
->h_path
.dentry
= au_h_dptr(dentry
, btop
);
819 a
->h_inode
= d_inode(a
->h_path
.dentry
);
820 if (ia
&& (ia
->ia_valid
& ATTR_SIZE
)) {
821 vfsub_inode_lock_shared_nested(a
->h_inode
, AuLsc_I_CHILD
);
822 if (ia
->ia_size
< i_size_read(a
->h_inode
))
824 inode_unlock_shared(a
->h_inode
);
828 if (au_ftest_icpup(a
->flags
, DID_CPUP
) && d_unlinked(dentry
)) {
829 hi_wh
= au_hi_wh(inode
, a
->btgt
);
831 struct au_cp_generic cpg
= {
838 err
= au_sio_cpup_wh(&cpg
, /*file*/NULL
);
841 hi_wh
= au_hi_wh(inode
, a
->btgt
);
842 /* todo: revalidate hi_wh? */
847 au_pin_set_parent_lflag(&a
->pin
, /*lflag*/0);
848 di_downgrade_lock(parent
, AuLock_IR
);
852 if (!au_ftest_icpup(a
->flags
, DID_CPUP
))
853 goto out
; /* success */
855 if (!d_unhashed(dentry
)) {
856 struct au_cp_generic cpg
= {
862 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
864 err
= au_sio_cpup_simple(&cpg
);
866 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->btgt
);
868 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->btgt
);
870 a
->h_path
.dentry
= hi_wh
; /* do not dget here */
873 a
->h_inode
= d_inode(a
->h_path
.dentry
);
875 goto out
; /* success */
879 di_write_unlock(parent
);
884 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
888 static int aufs_setattr(struct dentry
*dentry
, struct iattr
*ia
)
891 struct inode
*inode
, *delegated
;
892 struct super_block
*sb
;
894 struct au_icpup_args
*a
;
896 inode
= d_inode(dentry
);
899 err
= setattr_prepare(dentry
, ia
);
904 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
908 if (ia
->ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
))
909 ia
->ia_valid
&= ~ATTR_MODE
;
913 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
917 if (ia
->ia_valid
& ATTR_FILE
) {
918 /* currently ftruncate(2) only */
919 AuDebugOn(!d_is_reg(dentry
));
921 err
= au_reval_and_lock_fdi(file
, au_reopen_nondir
, /*wlock*/1,
925 ia
->ia_file
= au_hf_top(file
);
926 a
->udba
= AuOpt_UDBA_NONE
;
928 /* fchmod() doesn't pass ia_file */
929 a
->udba
= au_opt_udba(sb
);
930 di_write_lock_child(dentry
);
931 /* no d_unlinked(), to set UDBA_NONE for root */
932 if (d_unhashed(dentry
))
933 a
->udba
= AuOpt_UDBA_NONE
;
934 if (a
->udba
!= AuOpt_UDBA_NONE
) {
935 AuDebugOn(IS_ROOT(dentry
));
936 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
942 err
= au_pin_and_icpup(dentry
, ia
, a
);
943 if (unlikely(err
< 0))
945 if (au_ftest_icpup(a
->flags
, DID_CPUP
)) {
947 ia
->ia_valid
&= ~ATTR_FILE
;
950 a
->h_path
.mnt
= au_sbr_mnt(sb
, a
->btgt
);
951 if ((ia
->ia_valid
& (ATTR_MODE
| ATTR_CTIME
))
952 == (ATTR_MODE
| ATTR_CTIME
)) {
953 err
= security_path_chmod(&a
->h_path
, ia
->ia_mode
);
956 } else if ((ia
->ia_valid
& (ATTR_UID
| ATTR_GID
))
957 && (ia
->ia_valid
& ATTR_CTIME
)) {
958 err
= security_path_chown(&a
->h_path
, ia
->ia_uid
, ia
->ia_gid
);
963 if (ia
->ia_valid
& ATTR_SIZE
) {
966 if (ia
->ia_size
< i_size_read(inode
))
968 truncate_setsize(inode
, ia
->ia_size
);
971 if (ia
->ia_valid
& ATTR_FILE
)
973 inode_unlock(a
->h_inode
);
974 err
= vfsub_trunc(&a
->h_path
, ia
->ia_size
, ia
->ia_valid
, f
);
975 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
979 err
= vfsub_notify_change(&a
->h_path
, ia
, &delegated
);
981 err
= break_deleg_wait(&delegated
);
989 * regardless aufs 'acl' option setting.
990 * why don't all acl-aware fs call this func from their ->setattr()?
992 if (!err
&& (ia
->ia_valid
& ATTR_MODE
))
993 err
= vfsub_acl_chmod(a
->h_inode
, ia
->ia_mode
);
995 au_cpup_attr_changeable(inode
);
998 inode_unlock(a
->h_inode
);
1001 au_update_dbtop(dentry
);
1003 di_write_unlock(dentry
);
1005 fi_write_unlock(file
);
1007 ia
->ia_valid
|= ATTR_FILE
;
1018 #if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
1019 static int au_h_path_to_set_attr(struct dentry
*dentry
,
1020 struct au_icpup_args
*a
, struct path
*h_path
)
1023 struct super_block
*sb
;
1026 a
->udba
= au_opt_udba(sb
);
1027 /* no d_unlinked(), to set UDBA_NONE for root */
1028 if (d_unhashed(dentry
))
1029 a
->udba
= AuOpt_UDBA_NONE
;
1030 if (a
->udba
!= AuOpt_UDBA_NONE
) {
1031 AuDebugOn(IS_ROOT(dentry
));
1032 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
1036 err
= au_pin_and_icpup(dentry
, /*ia*/NULL
, a
);
1037 if (unlikely(err
< 0))
1040 h_path
->dentry
= a
->h_path
.dentry
;
1041 h_path
->mnt
= au_sbr_mnt(sb
, a
->btgt
);
1047 ssize_t
au_sxattr(struct dentry
*dentry
, struct inode
*inode
,
1048 struct au_sxattr
*arg
)
1052 struct super_block
*sb
;
1053 struct au_icpup_args
*a
;
1054 struct inode
*h_inode
;
1059 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
1064 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1068 h_path
.dentry
= NULL
; /* silence gcc */
1069 di_write_lock_child(dentry
);
1070 err
= au_h_path_to_set_attr(dentry
, a
, &h_path
);
1074 inode_unlock(a
->h_inode
);
1075 switch (arg
->type
) {
1077 AuDebugOn(d_is_negative(h_path
.dentry
));
1078 err
= vfsub_setxattr(h_path
.dentry
,
1079 arg
->u
.set
.name
, arg
->u
.set
.value
,
1080 arg
->u
.set
.size
, arg
->u
.set
.flags
);
1084 h_inode
= d_inode(h_path
.dentry
);
1085 if (h_inode
->i_op
->set_acl
)
1086 /* this will call posix_acl_update_mode */
1087 err
= h_inode
->i_op
->set_acl(h_inode
,
1089 arg
->u
.acl_set
.type
);
1093 au_cpup_attr_timesizes(inode
);
1097 au_update_dbtop(dentry
);
1100 di_write_unlock(dentry
);
1110 static void au_refresh_iattr(struct inode
*inode
, struct kstat
*st
,
1115 inode
->i_mode
= st
->mode
;
1116 /* don't i_[ug]id_write() here */
1117 inode
->i_uid
= st
->uid
;
1118 inode
->i_gid
= st
->gid
;
1119 inode
->i_atime
= st
->atime
;
1120 inode
->i_mtime
= st
->mtime
;
1121 inode
->i_ctime
= st
->ctime
;
1123 au_cpup_attr_nlink(inode
, /*force*/0);
1124 if (S_ISDIR(inode
->i_mode
)) {
1128 smp_mb(); /* for i_nlink */
1130 set_nlink(inode
, n
);
1133 spin_lock(&inode
->i_lock
);
1134 inode
->i_blocks
= st
->blocks
;
1135 i_size_write(inode
, st
->size
);
1136 spin_unlock(&inode
->i_lock
);
1140 * common routine for aufs_getattr() and au_getxattr().
1141 * returns zero or negative (an error).
1142 * @dentry will be read-locked in success.
1144 int au_h_path_getattr(struct dentry
*dentry
, int force
, struct path
*h_path
,
1148 unsigned int mnt_flags
, sigen
;
1149 unsigned char udba_none
;
1150 aufs_bindex_t bindex
;
1151 struct super_block
*sb
, *h_sb
;
1152 struct inode
*inode
;
1155 h_path
->dentry
= NULL
;
1159 mnt_flags
= au_mntflags(sb
);
1160 udba_none
= !!au_opt_test(mnt_flags
, UDBA_NONE
);
1162 if (unlikely(locked
))
1163 goto body
; /* skip locking dinfo */
1165 /* support fstat(2) */
1166 if (!d_unlinked(dentry
) && !udba_none
) {
1167 sigen
= au_sigen(sb
);
1168 err
= au_digen_test(dentry
, sigen
);
1170 di_read_lock_child(dentry
, AuLock_IR
);
1171 err
= au_dbrange_test(dentry
);
1172 if (unlikely(err
)) {
1173 di_read_unlock(dentry
, AuLock_IR
);
1177 AuDebugOn(IS_ROOT(dentry
));
1178 di_write_lock_child(dentry
);
1179 err
= au_dbrange_test(dentry
);
1181 err
= au_reval_for_attr(dentry
, sigen
);
1183 di_downgrade_lock(dentry
, AuLock_IR
);
1185 di_write_unlock(dentry
);
1190 di_read_lock_child(dentry
, AuLock_IR
);
1193 inode
= d_inode(dentry
);
1194 bindex
= au_ibtop(inode
);
1195 h_path
->mnt
= au_sbr_mnt(sb
, bindex
);
1196 h_sb
= h_path
->mnt
->mnt_sb
;
1198 && !au_test_fs_bad_iattr(h_sb
)
1200 goto out
; /* success */
1202 if (au_dbtop(dentry
) == bindex
)
1203 h_path
->dentry
= au_h_dptr(dentry
, bindex
);
1204 else if (au_opt_test(mnt_flags
, PLINK
) && au_plink_test(inode
)) {
1205 h_path
->dentry
= au_plink_lkup(inode
, bindex
);
1206 if (IS_ERR(h_path
->dentry
))
1207 /* pretending success */
1208 h_path
->dentry
= NULL
;
1210 dput(h_path
->dentry
);
1217 static int aufs_getattr(const struct path
*path
, struct kstat
*st
,
1218 u32 request
, unsigned int query
)
1221 unsigned char positive
;
1223 struct dentry
*dentry
;
1224 struct inode
*inode
;
1225 struct super_block
*sb
;
1227 dentry
= path
->dentry
;
1228 inode
= d_inode(dentry
);
1230 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1233 err
= au_h_path_getattr(dentry
, /*force*/0, &h_path
, /*locked*/0);
1236 if (unlikely(!h_path
.dentry
))
1237 /* illegally overlapped or something */
1238 goto out_fill
; /* pretending success */
1240 positive
= d_is_positive(h_path
.dentry
);
1242 /* no vfsub version */
1243 err
= vfs_getattr(&h_path
, st
, request
, query
);
1246 au_refresh_iattr(inode
, st
,
1247 d_inode(h_path
.dentry
)->i_nlink
);
1248 goto out_fill
; /* success */
1254 generic_fillattr(inode
, st
);
1256 di_read_unlock(dentry
, AuLock_IR
);
1264 /* ---------------------------------------------------------------------- */
1266 static const char *aufs_get_link(struct dentry
*dentry
, struct inode
*inode
,
1267 struct delayed_call
*done
)
1270 struct dentry
*h_dentry
;
1271 struct inode
*h_inode
;
1273 aufs_bindex_t bindex
;
1275 ret
= NULL
; /* suppress a warning */
1280 err
= aufs_read_lock(dentry
, AuLock_IR
| AuLock_GEN
);
1284 err
= au_d_hashed_positive(dentry
);
1289 inode
= d_inode(dentry
);
1290 bindex
= au_ibtop(inode
);
1291 h_inode
= au_h_iptr(inode
, bindex
);
1292 if (unlikely(!h_inode
->i_op
->get_link
))
1297 if (au_dbtop(dentry
) <= bindex
) {
1298 h_dentry
= au_h_dptr(dentry
, bindex
);
1303 h_dentry
= d_find_any_alias(h_inode
);
1304 if (IS_ERR(h_dentry
)) {
1305 err
= PTR_ERR(h_dentry
);
1309 if (unlikely(!h_dentry
))
1313 AuDbg("%pf\n", h_inode
->i_op
->get_link
);
1314 AuDbgDentry(h_dentry
);
1315 ret
= vfs_get_link(h_dentry
, done
);
1321 aufs_read_unlock(dentry
, AuLock_IR
);
1329 /* ---------------------------------------------------------------------- */
1331 static int au_is_special(struct inode
*inode
)
1333 return (inode
->i_mode
& (S_IFBLK
| S_IFCHR
| S_IFIFO
| S_IFSOCK
));
1336 static int aufs_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
1339 aufs_bindex_t bindex
;
1340 struct super_block
*sb
;
1341 struct inode
*h_inode
;
1342 struct vfsmount
*h_mnt
;
1345 WARN_ONCE((flags
& S_ATIME
) && !IS_NOATIME(inode
),
1346 "unexpected s_flags 0x%lx", sb
->s_flags
);
1348 /* mmap_sem might be acquired already, cf. aufs_mmap() */
1350 si_read_lock(sb
, AuLock_FLUSH
);
1351 ii_write_lock_child(inode
);
1355 bindex
= au_ibtop(inode
);
1356 h_inode
= au_h_iptr(inode
, bindex
);
1357 if (!au_test_ro(sb
, bindex
, inode
)) {
1358 h_mnt
= au_sbr_mnt(sb
, bindex
);
1359 err
= vfsub_mnt_want_write(h_mnt
);
1361 err
= vfsub_update_time(h_inode
, ts
, flags
);
1362 vfsub_mnt_drop_write(h_mnt
);
1364 } else if (au_is_special(h_inode
)) {
1366 * Never copy-up here.
1367 * These special files may already be opened and used for
1368 * communicating. If we copied it up, then the communication
1369 * would be corrupted.
1371 AuWarn1("timestamps for i%lu are ignored "
1372 "since it is on readonly branch (hi%lu).\n",
1373 inode
->i_ino
, h_inode
->i_ino
);
1374 } else if (flags
& ~S_ATIME
) {
1376 AuIOErr1("unexpected flags 0x%x\n", flags
);
1382 au_cpup_attr_timesizes(inode
);
1383 ii_write_unlock(inode
);
1387 if (!err
&& (flags
& S_VERSION
))
1388 inode_inc_iversion(inode
);
1393 /* ---------------------------------------------------------------------- */
1395 /* no getattr version will be set by module.c:aufs_init() */
1396 struct inode_operations aufs_iop_nogetattr
[AuIop_Last
],
1399 .permission
= aufs_permission
,
1400 #ifdef CONFIG_FS_POSIX_ACL
1401 .get_acl
= aufs_get_acl
,
1402 .set_acl
= aufs_set_acl
, /* unsupport for symlink? */
1405 .setattr
= aufs_setattr
,
1406 .getattr
= aufs_getattr
,
1408 #ifdef CONFIG_AUFS_XATTR
1409 .listxattr
= aufs_listxattr
,
1412 .get_link
= aufs_get_link
,
1414 /* .update_time = aufs_update_time */
1417 .create
= aufs_create
,
1418 .lookup
= aufs_lookup
,
1420 .unlink
= aufs_unlink
,
1421 .symlink
= aufs_symlink
,
1422 .mkdir
= aufs_mkdir
,
1423 .rmdir
= aufs_rmdir
,
1424 .mknod
= aufs_mknod
,
1425 .rename
= aufs_rename
,
1427 .permission
= aufs_permission
,
1428 #ifdef CONFIG_FS_POSIX_ACL
1429 .get_acl
= aufs_get_acl
,
1430 .set_acl
= aufs_set_acl
,
1433 .setattr
= aufs_setattr
,
1434 .getattr
= aufs_getattr
,
1436 #ifdef CONFIG_AUFS_XATTR
1437 .listxattr
= aufs_listxattr
,
1440 .update_time
= aufs_update_time
,
1441 .atomic_open
= aufs_atomic_open
,
1442 .tmpfile
= aufs_tmpfile
1445 .permission
= aufs_permission
,
1446 #ifdef CONFIG_FS_POSIX_ACL
1447 .get_acl
= aufs_get_acl
,
1448 .set_acl
= aufs_set_acl
,
1451 .setattr
= aufs_setattr
,
1452 .getattr
= aufs_getattr
,
1454 #ifdef CONFIG_AUFS_XATTR
1455 .listxattr
= aufs_listxattr
,
1458 .update_time
= aufs_update_time