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 SB_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_bl_node hblist
;
256 struct file
*file
, *h_file
;
259 static int au_do_aopen(struct inode
*inode
, struct file
*file
)
261 struct hlist_bl_head
*aopen
;
262 struct hlist_bl_node
*pos
;
263 struct aopen_node
*node
;
264 struct au_do_open_args args
= {
266 .open
= au_do_open_nondir
269 aopen
= &au_sbi(inode
->i_sb
)->si_aopen
;
270 hlist_bl_lock(aopen
);
271 hlist_bl_for_each_entry(node
, pos
, aopen
, hblist
)
272 if (node
->file
== file
) {
273 args
.h_file
= node
->h_file
;
276 hlist_bl_unlock(aopen
);
277 /* AuDebugOn(!args.h_file); */
279 return au_do_open(file
, &args
);
282 static int aufs_atomic_open(struct inode
*dir
, struct dentry
*dentry
,
283 struct file
*file
, unsigned int open_flag
,
284 umode_t create_mode
, int *opened
)
286 int err
, unlocked
, h_opened
= *opened
;
287 unsigned int lkup_flags
;
288 struct dentry
*parent
, *d
;
289 struct hlist_bl_head
*aopen
;
290 struct vfsub_aopen_args args
= {
291 .open_flag
= open_flag
,
292 .create_mode
= create_mode
,
295 struct aopen_node aopen_node
= {
300 AuDbg("open_flag 0%o\n", open_flag
);
304 if (!au_di(dentry
)) {
305 lkup_flags
= LOOKUP_OPEN
;
306 if (open_flag
& O_CREAT
)
307 lkup_flags
|= LOOKUP_CREATE
;
308 d
= aufs_lookup(dir
, dentry
, lkup_flags
);
315 * obsoleted dentry found.
316 * another error will be returned later.
325 if (d_is_positive(dentry
)
326 || d_unhashed(dentry
)
327 || d_unlinked(dentry
)
328 || !(open_flag
& O_CREAT
))
332 err
= aufs_read_lock(dentry
, AuLock_DW
| AuLock_FLUSH
| AuLock_GEN
);
336 parent
= dentry
->d_parent
; /* dir is locked */
337 di_write_lock_parent(parent
);
338 err
= au_lkup_dentry(dentry
, /*btop*/0, AuLkup_ALLOW_NEG
);
343 if (d_is_positive(dentry
))
346 args
.file
= get_empty_filp();
347 err
= PTR_ERR(args
.file
);
348 if (IS_ERR(args
.file
))
351 args
.file
->f_flags
= file
->f_flags
;
352 err
= au_aopen_or_create(dir
, dentry
, &args
);
354 AuDbgFile(args
.file
);
355 if (unlikely(err
< 0)) {
356 if (h_opened
& FILE_OPENED
)
362 di_write_unlock(parent
);
363 di_write_unlock(dentry
);
366 /* some filesystems don't set FILE_CREATED while succeeded? */
367 *opened
|= FILE_CREATED
;
368 if (h_opened
& FILE_OPENED
)
369 aopen_node
.h_file
= args
.file
;
374 aopen
= &au_sbi(dir
->i_sb
)->si_aopen
;
375 au_hbl_add(&aopen_node
.hblist
, aopen
);
376 err
= finish_open(file
, dentry
, au_do_aopen
, opened
);
377 au_hbl_del(&aopen_node
.hblist
, aopen
);
380 if (aopen_node
.h_file
)
381 fput(aopen_node
.h_file
);
385 si_read_unlock(dentry
->d_sb
);
387 di_write_unlock(parent
);
388 aufs_read_unlock(dentry
, AuLock_DW
);
391 if (unlikely(err
< 0))
394 if (err
>= 0 && !(*opened
& FILE_CREATED
)) {
395 AuLabel(out_no_open
);
397 err
= finish_no_open(file
, dentry
);
400 AuDbg("%pd%s%s\n", dentry
,
401 (*opened
& FILE_CREATED
) ? " created" : "",
402 (*opened
& FILE_OPENED
) ? " opened" : "");
408 /* ---------------------------------------------------------------------- */
410 static int au_wr_dir_cpup(struct dentry
*dentry
, struct dentry
*parent
,
411 const unsigned char add_entry
, aufs_bindex_t bcpup
,
415 struct dentry
*h_parent
;
419 IMustLock(d_inode(parent
));
421 di_write_lock_parent(parent
);
424 if (!au_h_dptr(parent
, bcpup
)) {
426 err
= au_cpup_dirs(dentry
, bcpup
);
427 else if (btop
< bcpup
)
428 err
= au_cpdown_dirs(dentry
, bcpup
);
432 if (!err
&& add_entry
&& !au_ftest_wrdir(add_entry
, TMPFILE
)) {
433 h_parent
= au_h_dptr(parent
, bcpup
);
434 h_dir
= d_inode(h_parent
);
435 vfsub_inode_lock_shared_nested(h_dir
, AuLsc_I_PARENT
);
436 err
= au_lkup_neg(dentry
, bcpup
, /*wh*/0);
437 /* todo: no unlock here */
438 inode_unlock_shared(h_dir
);
440 AuDbg("bcpup %d\n", bcpup
);
442 if (d_really_is_negative(dentry
))
443 au_set_h_dptr(dentry
, btop
, NULL
);
444 au_update_dbrange(dentry
, /*do_put_zero*/0);
449 di_write_unlock(parent
);
451 err
= bcpup
; /* success */
458 * decide the branch and the parent dir where we will create a new entry.
459 * returns new bindex or an error.
460 * copyup the parent dir if needed.
462 int au_wr_dir(struct dentry
*dentry
, struct dentry
*src_dentry
,
463 struct au_wr_dir_args
*args
)
467 aufs_bindex_t bcpup
, btop
, src_btop
;
468 const unsigned char add_entry
469 = au_ftest_wrdir(args
->flags
, ADD_ENTRY
)
470 | au_ftest_wrdir(args
->flags
, TMPFILE
);
471 struct super_block
*sb
;
472 struct dentry
*parent
;
473 struct au_sbinfo
*sbinfo
;
477 parent
= dget_parent(dentry
);
478 btop
= au_dbtop(dentry
);
480 if (args
->force_btgt
< 0) {
482 src_btop
= au_dbtop(src_dentry
);
485 } else if (add_entry
) {
487 if (au_ftest_wrdir(args
->flags
, ISDIR
))
488 au_fset_wbr(flags
, DIR);
489 err
= AuWbrCreate(sbinfo
, dentry
, flags
);
493 if (bcpup
< 0 || au_test_ro(sb
, bcpup
, d_inode(dentry
))) {
495 err
= AuWbrCopyup(sbinfo
, dentry
);
497 if (!IS_ROOT(dentry
)) {
498 di_read_lock_parent(parent
, !AuLock_IR
);
499 err
= AuWbrCopyup(sbinfo
, dentry
);
500 di_read_unlock(parent
, !AuLock_IR
);
502 err
= AuWbrCopyup(sbinfo
, dentry
);
505 if (unlikely(err
< 0))
509 bcpup
= args
->force_btgt
;
510 AuDebugOn(au_test_ro(sb
, bcpup
, d_inode(dentry
)));
513 AuDbg("btop %d, bcpup %d\n", btop
, bcpup
);
516 goto out
; /* success */
518 /* copyup the new parent into the branch we process */
519 err
= au_wr_dir_cpup(dentry
, parent
, add_entry
, bcpup
, btop
);
521 if (d_really_is_negative(dentry
)) {
522 au_set_h_dptr(dentry
, btop
, NULL
);
523 au_set_dbtop(dentry
, bcpup
);
524 au_set_dbbot(dentry
, bcpup
);
527 && !au_ftest_wrdir(args
->flags
, TMPFILE
)
528 && !au_h_dptr(dentry
, bcpup
));
536 /* ---------------------------------------------------------------------- */
538 void au_pin_hdir_unlock(struct au_pin
*p
)
541 au_hn_inode_unlock(p
->hdir
);
544 int au_pin_hdir_lock(struct au_pin
*p
)
552 /* even if an error happens later, keep this lock */
553 au_hn_inode_lock_nested(p
->hdir
, p
->lsc_hi
);
556 if (unlikely(p
->hdir
->hi_inode
!= d_inode(p
->h_parent
)))
561 err
= au_h_verify(p
->h_dentry
, p
->udba
, p
->hdir
->hi_inode
,
568 int au_pin_hdir_relock(struct au_pin
*p
)
572 struct dentry
*h_d
[] = {
577 err
= au_pin_hdir_lock(p
);
581 for (i
= 0; !err
&& i
< sizeof(h_d
)/sizeof(*h_d
); i
++) {
584 if (d_is_positive(h_d
[i
])) {
585 h_i
= d_inode(h_d
[i
]);
594 static void au_pin_hdir_set_owner(struct au_pin
*p
, struct task_struct
*task
)
596 #if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
597 p
->hdir
->hi_inode
->i_rwsem
.owner
= task
;
601 void au_pin_hdir_acquire_nest(struct au_pin
*p
)
604 rwsem_acquire_nest(&p
->hdir
->hi_inode
->i_rwsem
.dep_map
,
605 p
->lsc_hi
, 0, NULL
, _RET_IP_
);
606 au_pin_hdir_set_owner(p
, current
);
610 void au_pin_hdir_release(struct au_pin
*p
)
613 au_pin_hdir_set_owner(p
, p
->task
);
614 rwsem_release(&p
->hdir
->hi_inode
->i_rwsem
.dep_map
, 1, _RET_IP_
);
618 struct dentry
*au_pinned_h_parent(struct au_pin
*pin
)
620 if (pin
&& pin
->parent
)
621 return au_h_dptr(pin
->parent
, pin
->bindex
);
625 void au_unpin(struct au_pin
*p
)
628 au_pin_hdir_unlock(p
);
629 if (p
->h_mnt
&& au_ftest_pin(p
->flags
, MNT_WRITE
))
630 vfsub_mnt_drop_write(p
->h_mnt
);
634 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
635 di_read_unlock(p
->parent
, AuLock_IR
);
636 iput(p
->hdir
->hi_inode
);
641 /* do not clear p->task */
644 int au_do_pin(struct au_pin
*p
)
647 struct super_block
*sb
;
651 sb
= p
->dentry
->d_sb
;
652 p
->br
= au_sbr(sb
, p
->bindex
);
653 if (IS_ROOT(p
->dentry
)) {
654 if (au_ftest_pin(p
->flags
, MNT_WRITE
)) {
655 p
->h_mnt
= au_br_mnt(p
->br
);
656 err
= vfsub_mnt_want_write(p
->h_mnt
);
658 au_fclr_pin(p
->flags
, MNT_WRITE
);
666 if (p
->bindex
<= au_dbbot(p
->dentry
))
667 p
->h_dentry
= au_h_dptr(p
->dentry
, p
->bindex
);
669 p
->parent
= dget_parent(p
->dentry
);
670 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
671 di_read_lock(p
->parent
, AuLock_IR
, p
->lsc_di
);
674 p
->h_parent
= au_h_dptr(p
->parent
, p
->bindex
);
675 p
->hdir
= au_hi(d_inode(p
->parent
), p
->bindex
);
677 h_dir
= p
->hdir
->hi_inode
;
681 * if DI_LOCKED is not set, then p->parent may be different
682 * and h_parent can be NULL.
684 if (unlikely(!p
->hdir
|| !h_dir
|| !p
->h_parent
)) {
686 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
687 di_read_unlock(p
->parent
, AuLock_IR
);
693 if (au_ftest_pin(p
->flags
, MNT_WRITE
)) {
694 p
->h_mnt
= au_br_mnt(p
->br
);
695 err
= vfsub_mnt_want_write(p
->h_mnt
);
697 au_fclr_pin(p
->flags
, MNT_WRITE
);
698 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
699 di_read_unlock(p
->parent
, AuLock_IR
);
707 err
= au_pin_hdir_lock(p
);
709 goto out
; /* success */
714 pr_err("err %d\n", err
);
715 err
= au_busy_or_stale();
720 void au_pin_init(struct au_pin
*p
, struct dentry
*dentry
,
721 aufs_bindex_t bindex
, int lsc_di
, int lsc_hi
,
722 unsigned int udba
, unsigned char flags
)
741 int au_pin(struct au_pin
*pin
, struct dentry
*dentry
, aufs_bindex_t bindex
,
742 unsigned int udba
, unsigned char flags
)
744 au_pin_init(pin
, dentry
, bindex
, AuLsc_DI_PARENT
, AuLsc_I_PARENT2
,
746 return au_do_pin(pin
);
749 /* ---------------------------------------------------------------------- */
752 * ->setattr() and ->getattr() are called in various cases.
753 * chmod, stat: dentry is revalidated.
754 * fchmod, fstat: file and dentry are not revalidated, additionally they may be
756 * for ->setattr(), ia->ia_file is passed from ftruncate only.
758 /* todo: consolidate with do_refresh() and simple_reval_dpath() */
759 int au_reval_for_attr(struct dentry
*dentry
, unsigned int sigen
)
762 struct dentry
*parent
;
765 if (au_digen_test(dentry
, sigen
)) {
766 parent
= dget_parent(dentry
);
767 di_read_lock_parent(parent
, AuLock_IR
);
768 err
= au_refresh_dentry(dentry
, parent
);
769 di_read_unlock(parent
, AuLock_IR
);
777 int au_pin_and_icpup(struct dentry
*dentry
, struct iattr
*ia
,
778 struct au_icpup_args
*a
)
782 aufs_bindex_t btop
, ibtop
;
783 struct dentry
*hi_wh
, *parent
;
785 struct au_wr_dir_args wr_dir_args
= {
790 if (d_is_dir(dentry
))
791 au_fset_wrdir(wr_dir_args
.flags
, ISDIR
);
792 /* plink or hi_wh() case */
793 btop
= au_dbtop(dentry
);
794 inode
= d_inode(dentry
);
795 ibtop
= au_ibtop(inode
);
796 if (btop
!= ibtop
&& !au_test_ro(inode
->i_sb
, ibtop
, inode
))
797 wr_dir_args
.force_btgt
= ibtop
;
798 err
= au_wr_dir(dentry
, /*src_dentry*/NULL
, &wr_dir_args
);
799 if (unlikely(err
< 0))
803 au_fset_icpup(a
->flags
, DID_CPUP
);
806 a
->pin_flags
= AuPin_MNT_WRITE
;
808 if (!IS_ROOT(dentry
)) {
809 au_fset_pin(a
->pin_flags
, DI_LOCKED
);
810 parent
= dget_parent(dentry
);
811 di_write_lock_parent(parent
);
814 err
= au_pin(&a
->pin
, dentry
, a
->btgt
, a
->udba
, a
->pin_flags
);
819 a
->h_path
.dentry
= au_h_dptr(dentry
, btop
);
820 a
->h_inode
= d_inode(a
->h_path
.dentry
);
821 if (ia
&& (ia
->ia_valid
& ATTR_SIZE
)) {
822 vfsub_inode_lock_shared_nested(a
->h_inode
, AuLsc_I_CHILD
);
823 if (ia
->ia_size
< i_size_read(a
->h_inode
))
825 inode_unlock_shared(a
->h_inode
);
829 if (au_ftest_icpup(a
->flags
, DID_CPUP
) && d_unlinked(dentry
)) {
830 hi_wh
= au_hi_wh(inode
, a
->btgt
);
832 struct au_cp_generic cpg
= {
839 err
= au_sio_cpup_wh(&cpg
, /*file*/NULL
);
842 hi_wh
= au_hi_wh(inode
, a
->btgt
);
843 /* todo: revalidate hi_wh? */
848 au_pin_set_parent_lflag(&a
->pin
, /*lflag*/0);
849 di_downgrade_lock(parent
, AuLock_IR
);
853 if (!au_ftest_icpup(a
->flags
, DID_CPUP
))
854 goto out
; /* success */
856 if (!d_unhashed(dentry
)) {
857 struct au_cp_generic cpg
= {
863 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
865 err
= au_sio_cpup_simple(&cpg
);
867 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->btgt
);
869 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->btgt
);
871 a
->h_path
.dentry
= hi_wh
; /* do not dget here */
874 a
->h_inode
= d_inode(a
->h_path
.dentry
);
876 goto out
; /* success */
880 di_write_unlock(parent
);
885 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
889 static int aufs_setattr(struct dentry
*dentry
, struct iattr
*ia
)
892 struct inode
*inode
, *delegated
;
893 struct super_block
*sb
;
895 struct au_icpup_args
*a
;
897 inode
= d_inode(dentry
);
900 err
= setattr_prepare(dentry
, ia
);
905 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
909 if (ia
->ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
))
910 ia
->ia_valid
&= ~ATTR_MODE
;
914 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
918 if (ia
->ia_valid
& ATTR_FILE
) {
919 /* currently ftruncate(2) only */
920 AuDebugOn(!d_is_reg(dentry
));
922 err
= au_reval_and_lock_fdi(file
, au_reopen_nondir
, /*wlock*/1,
926 ia
->ia_file
= au_hf_top(file
);
927 a
->udba
= AuOpt_UDBA_NONE
;
929 /* fchmod() doesn't pass ia_file */
930 a
->udba
= au_opt_udba(sb
);
931 di_write_lock_child(dentry
);
932 /* no d_unlinked(), to set UDBA_NONE for root */
933 if (d_unhashed(dentry
))
934 a
->udba
= AuOpt_UDBA_NONE
;
935 if (a
->udba
!= AuOpt_UDBA_NONE
) {
936 AuDebugOn(IS_ROOT(dentry
));
937 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
943 err
= au_pin_and_icpup(dentry
, ia
, a
);
944 if (unlikely(err
< 0))
946 if (au_ftest_icpup(a
->flags
, DID_CPUP
)) {
948 ia
->ia_valid
&= ~ATTR_FILE
;
951 a
->h_path
.mnt
= au_sbr_mnt(sb
, a
->btgt
);
952 if ((ia
->ia_valid
& (ATTR_MODE
| ATTR_CTIME
))
953 == (ATTR_MODE
| ATTR_CTIME
)) {
954 err
= security_path_chmod(&a
->h_path
, ia
->ia_mode
);
957 } else if ((ia
->ia_valid
& (ATTR_UID
| ATTR_GID
))
958 && (ia
->ia_valid
& ATTR_CTIME
)) {
959 err
= security_path_chown(&a
->h_path
, ia
->ia_uid
, ia
->ia_gid
);
964 if (ia
->ia_valid
& ATTR_SIZE
) {
967 if (ia
->ia_size
< i_size_read(inode
))
969 truncate_setsize(inode
, ia
->ia_size
);
972 if (ia
->ia_valid
& ATTR_FILE
)
974 inode_unlock(a
->h_inode
);
975 err
= vfsub_trunc(&a
->h_path
, ia
->ia_size
, ia
->ia_valid
, f
);
976 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
980 err
= vfsub_notify_change(&a
->h_path
, ia
, &delegated
);
982 err
= break_deleg_wait(&delegated
);
990 * regardless aufs 'acl' option setting.
991 * why don't all acl-aware fs call this func from their ->setattr()?
993 if (!err
&& (ia
->ia_valid
& ATTR_MODE
))
994 err
= vfsub_acl_chmod(a
->h_inode
, ia
->ia_mode
);
996 au_cpup_attr_changeable(inode
);
999 inode_unlock(a
->h_inode
);
1002 au_update_dbtop(dentry
);
1004 di_write_unlock(dentry
);
1006 fi_write_unlock(file
);
1008 ia
->ia_valid
|= ATTR_FILE
;
1019 #if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
1020 static int au_h_path_to_set_attr(struct dentry
*dentry
,
1021 struct au_icpup_args
*a
, struct path
*h_path
)
1024 struct super_block
*sb
;
1027 a
->udba
= au_opt_udba(sb
);
1028 /* no d_unlinked(), to set UDBA_NONE for root */
1029 if (d_unhashed(dentry
))
1030 a
->udba
= AuOpt_UDBA_NONE
;
1031 if (a
->udba
!= AuOpt_UDBA_NONE
) {
1032 AuDebugOn(IS_ROOT(dentry
));
1033 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
1037 err
= au_pin_and_icpup(dentry
, /*ia*/NULL
, a
);
1038 if (unlikely(err
< 0))
1041 h_path
->dentry
= a
->h_path
.dentry
;
1042 h_path
->mnt
= au_sbr_mnt(sb
, a
->btgt
);
1048 ssize_t
au_sxattr(struct dentry
*dentry
, struct inode
*inode
,
1049 struct au_sxattr
*arg
)
1053 struct super_block
*sb
;
1054 struct au_icpup_args
*a
;
1055 struct inode
*h_inode
;
1060 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
1065 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1069 h_path
.dentry
= NULL
; /* silence gcc */
1070 di_write_lock_child(dentry
);
1071 err
= au_h_path_to_set_attr(dentry
, a
, &h_path
);
1075 inode_unlock(a
->h_inode
);
1076 switch (arg
->type
) {
1078 AuDebugOn(d_is_negative(h_path
.dentry
));
1079 err
= vfsub_setxattr(h_path
.dentry
,
1080 arg
->u
.set
.name
, arg
->u
.set
.value
,
1081 arg
->u
.set
.size
, arg
->u
.set
.flags
);
1085 h_inode
= d_inode(h_path
.dentry
);
1086 if (h_inode
->i_op
->set_acl
)
1087 /* this will call posix_acl_update_mode */
1088 err
= h_inode
->i_op
->set_acl(h_inode
,
1090 arg
->u
.acl_set
.type
);
1094 au_cpup_attr_timesizes(inode
);
1098 au_update_dbtop(dentry
);
1101 di_write_unlock(dentry
);
1111 static void au_refresh_iattr(struct inode
*inode
, struct kstat
*st
,
1116 inode
->i_mode
= st
->mode
;
1117 /* don't i_[ug]id_write() here */
1118 inode
->i_uid
= st
->uid
;
1119 inode
->i_gid
= st
->gid
;
1120 inode
->i_atime
= st
->atime
;
1121 inode
->i_mtime
= st
->mtime
;
1122 inode
->i_ctime
= st
->ctime
;
1124 au_cpup_attr_nlink(inode
, /*force*/0);
1125 if (S_ISDIR(inode
->i_mode
)) {
1129 smp_mb(); /* for i_nlink */
1131 set_nlink(inode
, n
);
1134 spin_lock(&inode
->i_lock
);
1135 inode
->i_blocks
= st
->blocks
;
1136 i_size_write(inode
, st
->size
);
1137 spin_unlock(&inode
->i_lock
);
1141 * common routine for aufs_getattr() and au_getxattr().
1142 * returns zero or negative (an error).
1143 * @dentry will be read-locked in success.
1145 int au_h_path_getattr(struct dentry
*dentry
, int force
, struct path
*h_path
,
1149 unsigned int mnt_flags
, sigen
;
1150 unsigned char udba_none
;
1151 aufs_bindex_t bindex
;
1152 struct super_block
*sb
, *h_sb
;
1153 struct inode
*inode
;
1156 h_path
->dentry
= NULL
;
1160 mnt_flags
= au_mntflags(sb
);
1161 udba_none
= !!au_opt_test(mnt_flags
, UDBA_NONE
);
1163 if (unlikely(locked
))
1164 goto body
; /* skip locking dinfo */
1166 /* support fstat(2) */
1167 if (!d_unlinked(dentry
) && !udba_none
) {
1168 sigen
= au_sigen(sb
);
1169 err
= au_digen_test(dentry
, sigen
);
1171 di_read_lock_child(dentry
, AuLock_IR
);
1172 err
= au_dbrange_test(dentry
);
1173 if (unlikely(err
)) {
1174 di_read_unlock(dentry
, AuLock_IR
);
1178 AuDebugOn(IS_ROOT(dentry
));
1179 di_write_lock_child(dentry
);
1180 err
= au_dbrange_test(dentry
);
1182 err
= au_reval_for_attr(dentry
, sigen
);
1184 di_downgrade_lock(dentry
, AuLock_IR
);
1186 di_write_unlock(dentry
);
1191 di_read_lock_child(dentry
, AuLock_IR
);
1194 inode
= d_inode(dentry
);
1195 bindex
= au_ibtop(inode
);
1196 h_path
->mnt
= au_sbr_mnt(sb
, bindex
);
1197 h_sb
= h_path
->mnt
->mnt_sb
;
1199 && !au_test_fs_bad_iattr(h_sb
)
1201 goto out
; /* success */
1203 if (au_dbtop(dentry
) == bindex
)
1204 h_path
->dentry
= au_h_dptr(dentry
, bindex
);
1205 else if (au_opt_test(mnt_flags
, PLINK
) && au_plink_test(inode
)) {
1206 h_path
->dentry
= au_plink_lkup(inode
, bindex
);
1207 if (IS_ERR(h_path
->dentry
))
1208 /* pretending success */
1209 h_path
->dentry
= NULL
;
1211 dput(h_path
->dentry
);
1218 static int aufs_getattr(const struct path
*path
, struct kstat
*st
,
1219 u32 request
, unsigned int query
)
1222 unsigned char positive
;
1224 struct dentry
*dentry
;
1225 struct inode
*inode
;
1226 struct super_block
*sb
;
1228 dentry
= path
->dentry
;
1229 inode
= d_inode(dentry
);
1231 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1234 err
= au_h_path_getattr(dentry
, /*force*/0, &h_path
, /*locked*/0);
1237 if (unlikely(!h_path
.dentry
))
1238 /* illegally overlapped or something */
1239 goto out_fill
; /* pretending success */
1241 positive
= d_is_positive(h_path
.dentry
);
1243 /* no vfsub version */
1244 err
= vfs_getattr(&h_path
, st
, request
, query
);
1247 au_refresh_iattr(inode
, st
,
1248 d_inode(h_path
.dentry
)->i_nlink
);
1249 goto out_fill
; /* success */
1255 generic_fillattr(inode
, st
);
1257 di_read_unlock(dentry
, AuLock_IR
);
1265 /* ---------------------------------------------------------------------- */
1267 static const char *aufs_get_link(struct dentry
*dentry
, struct inode
*inode
,
1268 struct delayed_call
*done
)
1271 struct dentry
*h_dentry
;
1272 struct inode
*h_inode
;
1274 aufs_bindex_t bindex
;
1276 ret
= NULL
; /* suppress a warning */
1281 err
= aufs_read_lock(dentry
, AuLock_IR
| AuLock_GEN
);
1285 err
= au_d_hashed_positive(dentry
);
1290 inode
= d_inode(dentry
);
1291 bindex
= au_ibtop(inode
);
1292 h_inode
= au_h_iptr(inode
, bindex
);
1293 if (unlikely(!h_inode
->i_op
->get_link
))
1298 if (au_dbtop(dentry
) <= bindex
) {
1299 h_dentry
= au_h_dptr(dentry
, bindex
);
1304 h_dentry
= d_find_any_alias(h_inode
);
1305 if (IS_ERR(h_dentry
)) {
1306 err
= PTR_ERR(h_dentry
);
1310 if (unlikely(!h_dentry
))
1314 AuDbg("%pf\n", h_inode
->i_op
->get_link
);
1315 AuDbgDentry(h_dentry
);
1316 ret
= vfs_get_link(h_dentry
, done
);
1322 aufs_read_unlock(dentry
, AuLock_IR
);
1330 /* ---------------------------------------------------------------------- */
1332 static int au_is_special(struct inode
*inode
)
1334 return (inode
->i_mode
& (S_IFBLK
| S_IFCHR
| S_IFIFO
| S_IFSOCK
));
1337 static int aufs_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
1340 aufs_bindex_t bindex
;
1341 struct super_block
*sb
;
1342 struct inode
*h_inode
;
1343 struct vfsmount
*h_mnt
;
1346 WARN_ONCE((flags
& S_ATIME
) && !IS_NOATIME(inode
),
1347 "unexpected s_flags 0x%lx", sb
->s_flags
);
1349 /* mmap_sem might be acquired already, cf. aufs_mmap() */
1351 si_read_lock(sb
, AuLock_FLUSH
);
1352 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
);
1381 au_cpup_attr_timesizes(inode
);
1382 ii_write_unlock(inode
);
1386 if (!err
&& (flags
& S_VERSION
))
1387 inode_inc_iversion(inode
);
1392 /* ---------------------------------------------------------------------- */
1394 /* no getattr version will be set by module.c:aufs_init() */
1395 struct inode_operations aufs_iop_nogetattr
[AuIop_Last
],
1398 .permission
= aufs_permission
,
1399 #ifdef CONFIG_FS_POSIX_ACL
1400 .get_acl
= aufs_get_acl
,
1401 .set_acl
= aufs_set_acl
, /* unsupport for symlink? */
1404 .setattr
= aufs_setattr
,
1405 .getattr
= aufs_getattr
,
1407 #ifdef CONFIG_AUFS_XATTR
1408 .listxattr
= aufs_listxattr
,
1411 .get_link
= aufs_get_link
,
1413 /* .update_time = aufs_update_time */
1416 .create
= aufs_create
,
1417 .lookup
= aufs_lookup
,
1419 .unlink
= aufs_unlink
,
1420 .symlink
= aufs_symlink
,
1421 .mkdir
= aufs_mkdir
,
1422 .rmdir
= aufs_rmdir
,
1423 .mknod
= aufs_mknod
,
1424 .rename
= aufs_rename
,
1426 .permission
= aufs_permission
,
1427 #ifdef CONFIG_FS_POSIX_ACL
1428 .get_acl
= aufs_get_acl
,
1429 .set_acl
= aufs_set_acl
,
1432 .setattr
= aufs_setattr
,
1433 .getattr
= aufs_getattr
,
1435 #ifdef CONFIG_AUFS_XATTR
1436 .listxattr
= aufs_listxattr
,
1439 .update_time
= aufs_update_time
,
1440 .atomic_open
= aufs_atomic_open
,
1441 .tmpfile
= aufs_tmpfile
1444 .permission
= aufs_permission
,
1445 #ifdef CONFIG_FS_POSIX_ACL
1446 .get_acl
= aufs_get_acl
,
1447 .set_acl
= aufs_set_acl
,
1450 .setattr
= aufs_setattr
,
1451 .getattr
= aufs_getattr
,
1453 #ifdef CONFIG_AUFS_XATTR
1454 .listxattr
= aufs_listxattr
,
1457 .update_time
= aufs_update_time