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 * 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
, 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
))
330 err
= aufs_read_lock(dentry
, AuLock_DW
| AuLock_FLUSH
| AuLock_GEN
);
334 parent
= dentry
->d_parent
; /* dir is locked */
335 di_write_lock_parent(parent
);
336 err
= au_lkup_dentry(dentry
, /*btop*/0, AuLkup_ALLOW_NEG
);
341 if (d_is_positive(dentry
))
344 args
.file
= get_empty_filp();
345 err
= PTR_ERR(args
.file
);
346 if (IS_ERR(args
.file
))
349 args
.file
->f_flags
= file
->f_flags
;
350 err
= au_aopen_or_create(dir
, dentry
, &args
);
352 AuDbgFile(args
.file
);
353 if (unlikely(err
< 0)) {
354 if (h_opened
& FILE_OPENED
)
361 /* some filesystems don't set FILE_CREATED while succeeded? */
362 *opened
|= FILE_CREATED
;
363 if (h_opened
& FILE_OPENED
)
364 aopen_node
.h_file
= args
.file
;
369 aopen
= &au_sbi(dir
->i_sb
)->si_aopen
;
370 au_sphl_add(&aopen_node
.hlist
, aopen
);
371 err
= finish_open(file
, dentry
, au_do_aopen
, opened
);
372 au_sphl_del(&aopen_node
.hlist
, aopen
);
375 if (aopen_node
.h_file
)
376 fput(aopen_node
.h_file
);
379 di_write_unlock(parent
);
380 aufs_read_unlock(dentry
, AuLock_DW
);
382 if (unlikely(err
< 0))
385 if (err
>= 0 && !(*opened
& FILE_CREATED
)) {
386 AuLabel(out_no_open
);
388 err
= finish_no_open(file
, dentry
);
391 AuDbg("%pd%s%s\n", dentry
,
392 (*opened
& FILE_CREATED
) ? " created" : "",
393 (*opened
& FILE_OPENED
) ? " opened" : "");
399 /* ---------------------------------------------------------------------- */
401 static int au_wr_dir_cpup(struct dentry
*dentry
, struct dentry
*parent
,
402 const unsigned char add_entry
, aufs_bindex_t bcpup
,
406 struct dentry
*h_parent
;
410 IMustLock(d_inode(parent
));
412 di_write_lock_parent(parent
);
415 if (!au_h_dptr(parent
, bcpup
)) {
417 err
= au_cpup_dirs(dentry
, bcpup
);
418 else if (btop
< bcpup
)
419 err
= au_cpdown_dirs(dentry
, bcpup
);
423 if (!err
&& add_entry
&& !au_ftest_wrdir(add_entry
, TMPFILE
)) {
424 h_parent
= au_h_dptr(parent
, bcpup
);
425 h_dir
= d_inode(h_parent
);
426 inode_lock_nested(h_dir
, AuLsc_I_PARENT
);
427 err
= au_lkup_neg(dentry
, bcpup
, /*wh*/0);
428 /* todo: no unlock here */
431 AuDbg("bcpup %d\n", bcpup
);
433 if (d_really_is_negative(dentry
))
434 au_set_h_dptr(dentry
, btop
, NULL
);
435 au_update_dbrange(dentry
, /*do_put_zero*/0);
440 di_write_unlock(parent
);
442 err
= bcpup
; /* success */
449 * decide the branch and the parent dir where we will create a new entry.
450 * returns new bindex or an error.
451 * copyup the parent dir if needed.
453 int au_wr_dir(struct dentry
*dentry
, struct dentry
*src_dentry
,
454 struct au_wr_dir_args
*args
)
458 aufs_bindex_t bcpup
, btop
, src_btop
;
459 const unsigned char add_entry
460 = au_ftest_wrdir(args
->flags
, ADD_ENTRY
)
461 | au_ftest_wrdir(args
->flags
, TMPFILE
);
462 struct super_block
*sb
;
463 struct dentry
*parent
;
464 struct au_sbinfo
*sbinfo
;
468 parent
= dget_parent(dentry
);
469 btop
= au_dbtop(dentry
);
471 if (args
->force_btgt
< 0) {
473 src_btop
= au_dbtop(src_dentry
);
476 } else if (add_entry
) {
478 if (au_ftest_wrdir(args
->flags
, ISDIR
))
479 au_fset_wbr(flags
, DIR);
480 err
= AuWbrCreate(sbinfo
, dentry
, flags
);
484 if (bcpup
< 0 || au_test_ro(sb
, bcpup
, d_inode(dentry
))) {
486 err
= AuWbrCopyup(sbinfo
, dentry
);
488 if (!IS_ROOT(dentry
)) {
489 di_read_lock_parent(parent
, !AuLock_IR
);
490 err
= AuWbrCopyup(sbinfo
, dentry
);
491 di_read_unlock(parent
, !AuLock_IR
);
493 err
= AuWbrCopyup(sbinfo
, dentry
);
496 if (unlikely(err
< 0))
500 bcpup
= args
->force_btgt
;
501 AuDebugOn(au_test_ro(sb
, bcpup
, d_inode(dentry
)));
504 AuDbg("btop %d, bcpup %d\n", btop
, bcpup
);
507 goto out
; /* success */
509 /* copyup the new parent into the branch we process */
510 err
= au_wr_dir_cpup(dentry
, parent
, add_entry
, bcpup
, btop
);
512 if (d_really_is_negative(dentry
)) {
513 au_set_h_dptr(dentry
, btop
, NULL
);
514 au_set_dbtop(dentry
, bcpup
);
515 au_set_dbbot(dentry
, bcpup
);
518 && !au_ftest_wrdir(args
->flags
, TMPFILE
)
519 && !au_h_dptr(dentry
, bcpup
));
527 /* ---------------------------------------------------------------------- */
529 void au_pin_hdir_unlock(struct au_pin
*p
)
532 au_hn_inode_unlock(p
->hdir
);
535 int au_pin_hdir_lock(struct au_pin
*p
)
543 /* even if an error happens later, keep this lock */
544 au_hn_inode_lock_nested(p
->hdir
, p
->lsc_hi
);
547 if (unlikely(p
->hdir
->hi_inode
!= d_inode(p
->h_parent
)))
552 err
= au_h_verify(p
->h_dentry
, p
->udba
, p
->hdir
->hi_inode
,
559 int au_pin_hdir_relock(struct au_pin
*p
)
563 struct dentry
*h_d
[] = {
568 err
= au_pin_hdir_lock(p
);
572 for (i
= 0; !err
&& i
< sizeof(h_d
)/sizeof(*h_d
); i
++) {
575 if (d_is_positive(h_d
[i
])) {
576 h_i
= d_inode(h_d
[i
]);
585 static void au_pin_hdir_set_owner(struct au_pin
*p
, struct task_struct
*task
)
587 #if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
588 p
->hdir
->hi_inode
->i_rwsem
.owner
= task
;
592 void au_pin_hdir_acquire_nest(struct au_pin
*p
)
595 rwsem_acquire_nest(&p
->hdir
->hi_inode
->i_rwsem
.dep_map
,
596 p
->lsc_hi
, 0, NULL
, _RET_IP_
);
597 au_pin_hdir_set_owner(p
, current
);
601 void au_pin_hdir_release(struct au_pin
*p
)
604 au_pin_hdir_set_owner(p
, p
->task
);
605 rwsem_release(&p
->hdir
->hi_inode
->i_rwsem
.dep_map
, 1, _RET_IP_
);
609 struct dentry
*au_pinned_h_parent(struct au_pin
*pin
)
611 if (pin
&& pin
->parent
)
612 return au_h_dptr(pin
->parent
, pin
->bindex
);
616 void au_unpin(struct au_pin
*p
)
619 au_pin_hdir_unlock(p
);
620 if (p
->h_mnt
&& au_ftest_pin(p
->flags
, MNT_WRITE
))
621 vfsub_mnt_drop_write(p
->h_mnt
);
625 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
626 di_read_unlock(p
->parent
, AuLock_IR
);
627 iput(p
->hdir
->hi_inode
);
632 /* do not clear p->task */
635 int au_do_pin(struct au_pin
*p
)
638 struct super_block
*sb
;
642 sb
= p
->dentry
->d_sb
;
643 p
->br
= au_sbr(sb
, p
->bindex
);
644 if (IS_ROOT(p
->dentry
)) {
645 if (au_ftest_pin(p
->flags
, MNT_WRITE
)) {
646 p
->h_mnt
= au_br_mnt(p
->br
);
647 err
= vfsub_mnt_want_write(p
->h_mnt
);
649 au_fclr_pin(p
->flags
, MNT_WRITE
);
657 if (p
->bindex
<= au_dbbot(p
->dentry
))
658 p
->h_dentry
= au_h_dptr(p
->dentry
, p
->bindex
);
660 p
->parent
= dget_parent(p
->dentry
);
661 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
662 di_read_lock(p
->parent
, AuLock_IR
, p
->lsc_di
);
665 p
->h_parent
= au_h_dptr(p
->parent
, p
->bindex
);
666 p
->hdir
= au_hi(d_inode(p
->parent
), p
->bindex
);
668 h_dir
= p
->hdir
->hi_inode
;
672 * if DI_LOCKED is not set, then p->parent may be different
673 * and h_parent can be NULL.
675 if (unlikely(!p
->hdir
|| !h_dir
|| !p
->h_parent
)) {
677 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
678 di_read_unlock(p
->parent
, AuLock_IR
);
684 if (au_ftest_pin(p
->flags
, MNT_WRITE
)) {
685 p
->h_mnt
= au_br_mnt(p
->br
);
686 err
= vfsub_mnt_want_write(p
->h_mnt
);
688 au_fclr_pin(p
->flags
, MNT_WRITE
);
689 if (!au_ftest_pin(p
->flags
, DI_LOCKED
))
690 di_read_unlock(p
->parent
, AuLock_IR
);
698 err
= au_pin_hdir_lock(p
);
700 goto out
; /* success */
705 pr_err("err %d\n", err
);
706 err
= au_busy_or_stale();
711 void au_pin_init(struct au_pin
*p
, struct dentry
*dentry
,
712 aufs_bindex_t bindex
, int lsc_di
, int lsc_hi
,
713 unsigned int udba
, unsigned char flags
)
732 int au_pin(struct au_pin
*pin
, struct dentry
*dentry
, aufs_bindex_t bindex
,
733 unsigned int udba
, unsigned char flags
)
735 au_pin_init(pin
, dentry
, bindex
, AuLsc_DI_PARENT
, AuLsc_I_PARENT2
,
737 return au_do_pin(pin
);
740 /* ---------------------------------------------------------------------- */
743 * ->setattr() and ->getattr() are called in various cases.
744 * chmod, stat: dentry is revalidated.
745 * fchmod, fstat: file and dentry are not revalidated, additionally they may be
747 * for ->setattr(), ia->ia_file is passed from ftruncate only.
749 /* todo: consolidate with do_refresh() and simple_reval_dpath() */
750 int au_reval_for_attr(struct dentry
*dentry
, unsigned int sigen
)
753 struct dentry
*parent
;
756 if (au_digen_test(dentry
, sigen
)) {
757 parent
= dget_parent(dentry
);
758 di_read_lock_parent(parent
, AuLock_IR
);
759 err
= au_refresh_dentry(dentry
, parent
);
760 di_read_unlock(parent
, AuLock_IR
);
768 int au_pin_and_icpup(struct dentry
*dentry
, struct iattr
*ia
,
769 struct au_icpup_args
*a
)
773 aufs_bindex_t btop
, ibtop
;
774 struct dentry
*hi_wh
, *parent
;
776 struct au_wr_dir_args wr_dir_args
= {
781 if (d_is_dir(dentry
))
782 au_fset_wrdir(wr_dir_args
.flags
, ISDIR
);
783 /* plink or hi_wh() case */
784 btop
= au_dbtop(dentry
);
785 inode
= d_inode(dentry
);
786 ibtop
= au_ibtop(inode
);
787 if (btop
!= ibtop
&& !au_test_ro(inode
->i_sb
, ibtop
, inode
))
788 wr_dir_args
.force_btgt
= ibtop
;
789 err
= au_wr_dir(dentry
, /*src_dentry*/NULL
, &wr_dir_args
);
790 if (unlikely(err
< 0))
794 au_fset_icpup(a
->flags
, DID_CPUP
);
797 a
->pin_flags
= AuPin_MNT_WRITE
;
799 if (!IS_ROOT(dentry
)) {
800 au_fset_pin(a
->pin_flags
, DI_LOCKED
);
801 parent
= dget_parent(dentry
);
802 di_write_lock_parent(parent
);
805 err
= au_pin(&a
->pin
, dentry
, a
->btgt
, a
->udba
, a
->pin_flags
);
810 a
->h_path
.dentry
= au_h_dptr(dentry
, btop
);
811 a
->h_inode
= d_inode(a
->h_path
.dentry
);
812 if (ia
&& (ia
->ia_valid
& ATTR_SIZE
)) {
813 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
814 if (ia
->ia_size
< i_size_read(a
->h_inode
))
816 inode_unlock(a
->h_inode
);
820 if (au_ftest_icpup(a
->flags
, DID_CPUP
) && d_unlinked(dentry
)) {
821 hi_wh
= au_hi_wh(inode
, a
->btgt
);
823 struct au_cp_generic cpg
= {
830 err
= au_sio_cpup_wh(&cpg
, /*file*/NULL
);
833 hi_wh
= au_hi_wh(inode
, a
->btgt
);
834 /* todo: revalidate hi_wh? */
839 au_pin_set_parent_lflag(&a
->pin
, /*lflag*/0);
840 di_downgrade_lock(parent
, AuLock_IR
);
844 if (!au_ftest_icpup(a
->flags
, DID_CPUP
))
845 goto out
; /* success */
847 if (!d_unhashed(dentry
)) {
848 struct au_cp_generic cpg
= {
854 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
856 err
= au_sio_cpup_simple(&cpg
);
858 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->btgt
);
860 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->btgt
);
862 a
->h_path
.dentry
= hi_wh
; /* do not dget here */
865 a
->h_inode
= d_inode(a
->h_path
.dentry
);
867 goto out
; /* success */
871 di_write_unlock(parent
);
876 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
880 static int aufs_setattr(struct dentry
*dentry
, struct iattr
*ia
)
883 struct inode
*inode
, *delegated
;
884 struct super_block
*sb
;
886 struct au_icpup_args
*a
;
888 inode
= d_inode(dentry
);
891 err
= setattr_prepare(dentry
, ia
);
896 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
900 if (ia
->ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
))
901 ia
->ia_valid
&= ~ATTR_MODE
;
905 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
909 if (ia
->ia_valid
& ATTR_FILE
) {
910 /* currently ftruncate(2) only */
911 AuDebugOn(!d_is_reg(dentry
));
913 err
= au_reval_and_lock_fdi(file
, au_reopen_nondir
, /*wlock*/1);
916 ia
->ia_file
= au_hf_top(file
);
917 a
->udba
= AuOpt_UDBA_NONE
;
919 /* fchmod() doesn't pass ia_file */
920 a
->udba
= au_opt_udba(sb
);
921 di_write_lock_child(dentry
);
922 /* no d_unlinked(), to set UDBA_NONE for root */
923 if (d_unhashed(dentry
))
924 a
->udba
= AuOpt_UDBA_NONE
;
925 if (a
->udba
!= AuOpt_UDBA_NONE
) {
926 AuDebugOn(IS_ROOT(dentry
));
927 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
933 err
= au_pin_and_icpup(dentry
, ia
, a
);
934 if (unlikely(err
< 0))
936 if (au_ftest_icpup(a
->flags
, DID_CPUP
)) {
938 ia
->ia_valid
&= ~ATTR_FILE
;
941 a
->h_path
.mnt
= au_sbr_mnt(sb
, a
->btgt
);
942 if ((ia
->ia_valid
& (ATTR_MODE
| ATTR_CTIME
))
943 == (ATTR_MODE
| ATTR_CTIME
)) {
944 err
= security_path_chmod(&a
->h_path
, ia
->ia_mode
);
947 } else if ((ia
->ia_valid
& (ATTR_UID
| ATTR_GID
))
948 && (ia
->ia_valid
& ATTR_CTIME
)) {
949 err
= security_path_chown(&a
->h_path
, ia
->ia_uid
, ia
->ia_gid
);
954 if (ia
->ia_valid
& ATTR_SIZE
) {
957 if (ia
->ia_size
< i_size_read(inode
))
959 truncate_setsize(inode
, ia
->ia_size
);
962 if (ia
->ia_valid
& ATTR_FILE
)
964 inode_unlock(a
->h_inode
);
965 err
= vfsub_trunc(&a
->h_path
, ia
->ia_size
, ia
->ia_valid
, f
);
966 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
970 err
= vfsub_notify_change(&a
->h_path
, ia
, &delegated
);
972 err
= break_deleg_wait(&delegated
);
980 * regardless aufs 'acl' option setting.
981 * why don't all acl-aware fs call this func from their ->setattr()?
983 if (!err
&& (ia
->ia_valid
& ATTR_MODE
))
984 err
= vfsub_acl_chmod(a
->h_inode
, ia
->ia_mode
);
986 au_cpup_attr_changeable(inode
);
989 inode_unlock(a
->h_inode
);
992 au_update_dbtop(dentry
);
994 di_write_unlock(dentry
);
996 fi_write_unlock(file
);
998 ia
->ia_valid
|= ATTR_FILE
;
1003 au_delayed_kfree(a
);
1009 #if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
1010 static int au_h_path_to_set_attr(struct dentry
*dentry
,
1011 struct au_icpup_args
*a
, struct path
*h_path
)
1014 struct super_block
*sb
;
1017 a
->udba
= au_opt_udba(sb
);
1018 /* no d_unlinked(), to set UDBA_NONE for root */
1019 if (d_unhashed(dentry
))
1020 a
->udba
= AuOpt_UDBA_NONE
;
1021 if (a
->udba
!= AuOpt_UDBA_NONE
) {
1022 AuDebugOn(IS_ROOT(dentry
));
1023 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
1027 err
= au_pin_and_icpup(dentry
, /*ia*/NULL
, a
);
1028 if (unlikely(err
< 0))
1031 h_path
->dentry
= a
->h_path
.dentry
;
1032 h_path
->mnt
= au_sbr_mnt(sb
, a
->btgt
);
1038 ssize_t
au_srxattr(struct dentry
*dentry
, struct inode
*inode
,
1039 struct au_srxattr
*arg
)
1043 struct super_block
*sb
;
1044 struct au_icpup_args
*a
;
1045 struct inode
*h_inode
;
1050 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
1055 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1059 h_path
.dentry
= NULL
; /* silence gcc */
1060 di_write_lock_child(dentry
);
1061 err
= au_h_path_to_set_attr(dentry
, a
, &h_path
);
1065 inode_unlock(a
->h_inode
);
1066 switch (arg
->type
) {
1068 AuDebugOn(d_is_negative(h_path
.dentry
));
1069 err
= vfsub_setxattr(h_path
.dentry
,
1070 arg
->u
.set
.name
, arg
->u
.set
.value
,
1071 arg
->u
.set
.size
, arg
->u
.set
.flags
);
1073 case AU_XATTR_REMOVE
:
1074 err
= vfsub_removexattr(h_path
.dentry
, arg
->u
.remove
.name
);
1078 h_inode
= d_inode(h_path
.dentry
);
1079 if (h_inode
->i_op
->set_acl
)
1080 err
= h_inode
->i_op
->set_acl(h_inode
,
1082 arg
->u
.acl_set
.type
);
1086 au_cpup_attr_timesizes(inode
);
1090 au_update_dbtop(dentry
);
1093 di_write_unlock(dentry
);
1096 au_delayed_kfree(a
);
1103 static void au_refresh_iattr(struct inode
*inode
, struct kstat
*st
,
1108 inode
->i_mode
= st
->mode
;
1109 /* don't i_[ug]id_write() here */
1110 inode
->i_uid
= st
->uid
;
1111 inode
->i_gid
= st
->gid
;
1112 inode
->i_atime
= st
->atime
;
1113 inode
->i_mtime
= st
->mtime
;
1114 inode
->i_ctime
= st
->ctime
;
1116 au_cpup_attr_nlink(inode
, /*force*/0);
1117 if (S_ISDIR(inode
->i_mode
)) {
1121 smp_mb(); /* for i_nlink */
1123 set_nlink(inode
, n
);
1126 spin_lock(&inode
->i_lock
);
1127 inode
->i_blocks
= st
->blocks
;
1128 i_size_write(inode
, st
->size
);
1129 spin_unlock(&inode
->i_lock
);
1133 * common routine for aufs_getattr() and aufs_getxattr().
1134 * returns zero or negative (an error).
1135 * @dentry will be read-locked in success.
1137 int au_h_path_getattr(struct dentry
*dentry
, int force
, struct path
*h_path
)
1140 unsigned int mnt_flags
, sigen
;
1141 unsigned char udba_none
;
1142 aufs_bindex_t bindex
;
1143 struct super_block
*sb
, *h_sb
;
1144 struct inode
*inode
;
1147 h_path
->dentry
= NULL
;
1151 mnt_flags
= au_mntflags(sb
);
1152 udba_none
= !!au_opt_test(mnt_flags
, UDBA_NONE
);
1154 /* support fstat(2) */
1155 if (!d_unlinked(dentry
) && !udba_none
) {
1156 sigen
= au_sigen(sb
);
1157 err
= au_digen_test(dentry
, sigen
);
1159 di_read_lock_child(dentry
, AuLock_IR
);
1160 err
= au_dbrange_test(dentry
);
1161 if (unlikely(err
)) {
1162 di_read_unlock(dentry
, AuLock_IR
);
1166 AuDebugOn(IS_ROOT(dentry
));
1167 di_write_lock_child(dentry
);
1168 err
= au_dbrange_test(dentry
);
1170 err
= au_reval_for_attr(dentry
, sigen
);
1172 di_downgrade_lock(dentry
, AuLock_IR
);
1174 di_write_unlock(dentry
);
1179 di_read_lock_child(dentry
, AuLock_IR
);
1181 inode
= d_inode(dentry
);
1182 bindex
= au_ibtop(inode
);
1183 h_path
->mnt
= au_sbr_mnt(sb
, bindex
);
1184 h_sb
= h_path
->mnt
->mnt_sb
;
1186 && !au_test_fs_bad_iattr(h_sb
)
1188 goto out
; /* success */
1190 if (au_dbtop(dentry
) == bindex
)
1191 h_path
->dentry
= au_h_dptr(dentry
, bindex
);
1192 else if (au_opt_test(mnt_flags
, PLINK
) && au_plink_test(inode
)) {
1193 h_path
->dentry
= au_plink_lkup(inode
, bindex
);
1194 if (IS_ERR(h_path
->dentry
))
1195 /* pretending success */
1196 h_path
->dentry
= NULL
;
1198 dput(h_path
->dentry
);
1205 static int aufs_getattr(struct vfsmount
*mnt __maybe_unused
,
1206 struct dentry
*dentry
, struct kstat
*st
)
1209 unsigned char positive
;
1211 struct inode
*inode
;
1212 struct super_block
*sb
;
1214 inode
= d_inode(dentry
);
1216 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1219 err
= au_h_path_getattr(dentry
, /*force*/0, &h_path
);
1222 if (unlikely(!h_path
.dentry
))
1223 /* illegally overlapped or something */
1224 goto out_fill
; /* pretending success */
1226 positive
= d_is_positive(h_path
.dentry
);
1228 err
= vfs_getattr(&h_path
, st
);
1231 au_refresh_iattr(inode
, st
,
1232 d_inode(h_path
.dentry
)->i_nlink
);
1233 goto out_fill
; /* success */
1239 generic_fillattr(inode
, st
);
1241 di_read_unlock(dentry
, AuLock_IR
);
1249 /* ---------------------------------------------------------------------- */
1251 static const char *aufs_get_link(struct dentry
*dentry
, struct inode
*inode
,
1252 struct delayed_call
*done
)
1255 struct dentry
*h_dentry
;
1256 struct inode
*h_inode
;
1258 aufs_bindex_t bindex
;
1260 ret
= NULL
; /* suppress a warning */
1265 err
= aufs_read_lock(dentry
, AuLock_IR
| AuLock_GEN
);
1269 err
= au_d_hashed_positive(dentry
);
1274 inode
= d_inode(dentry
);
1275 bindex
= au_ibtop(inode
);
1276 h_inode
= au_h_iptr(inode
, bindex
);
1277 if (unlikely(!h_inode
->i_op
->get_link
))
1282 if (au_dbtop(dentry
) <= bindex
) {
1283 h_dentry
= au_h_dptr(dentry
, bindex
);
1288 h_dentry
= d_find_any_alias(h_inode
);
1289 if (IS_ERR(h_dentry
)) {
1290 err
= PTR_ERR(h_dentry
);
1294 if (unlikely(!h_dentry
))
1298 AuDbg("%pf\n", h_inode
->i_op
->get_link
);
1299 AuDbgDentry(h_dentry
);
1300 ret
= h_inode
->i_op
->get_link(h_dentry
, h_inode
, done
);
1306 aufs_read_unlock(dentry
, AuLock_IR
);
1314 /* ---------------------------------------------------------------------- */
1316 static int au_is_special(struct inode
*inode
)
1318 return (inode
->i_mode
& (S_IFBLK
| S_IFCHR
| S_IFIFO
| S_IFSOCK
));
1321 static int aufs_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
1324 aufs_bindex_t bindex
;
1325 struct super_block
*sb
;
1326 struct inode
*h_inode
;
1327 struct vfsmount
*h_mnt
;
1330 WARN_ONCE((flags
& S_ATIME
) && !IS_NOATIME(inode
),
1331 "unexpected s_flags 0x%lx", sb
->s_flags
);
1333 /* mmap_sem might be acquired already, cf. aufs_mmap() */
1335 si_read_lock(sb
, AuLock_FLUSH
);
1336 ii_write_lock_child(inode
);
1340 bindex
= au_ibtop(inode
);
1341 h_inode
= au_h_iptr(inode
, bindex
);
1342 if (!au_test_ro(sb
, bindex
, inode
)) {
1343 h_mnt
= au_sbr_mnt(sb
, bindex
);
1344 err
= vfsub_mnt_want_write(h_mnt
);
1346 err
= vfsub_update_time(h_inode
, ts
, flags
);
1347 vfsub_mnt_drop_write(h_mnt
);
1349 } else if (au_is_special(h_inode
)) {
1351 * Never copy-up here.
1352 * These special files may already be opened and used for
1353 * communicating. If we copied it up, then the communication
1354 * would be corrupted.
1356 AuWarn1("timestamps for i%lu are ignored "
1357 "since it is on readonly branch (hi%lu).\n",
1358 inode
->i_ino
, h_inode
->i_ino
);
1359 } else if (flags
& ~S_ATIME
) {
1361 AuIOErr1("unexpected flags 0x%x\n", flags
);
1367 au_cpup_attr_timesizes(inode
);
1368 ii_write_unlock(inode
);
1372 if (!err
&& (flags
& S_VERSION
))
1373 inode_inc_iversion(inode
);
1378 /* ---------------------------------------------------------------------- */
1380 /* no getattr version will be set by module.c:aufs_init() */
1381 struct inode_operations aufs_iop_nogetattr
[AuIop_Last
],
1384 .permission
= aufs_permission
,
1385 #ifdef CONFIG_FS_POSIX_ACL
1386 .get_acl
= aufs_get_acl
,
1387 .set_acl
= aufs_set_acl
, /* unsupport for symlink? */
1390 .setattr
= aufs_setattr
,
1391 .getattr
= aufs_getattr
,
1393 #ifdef CONFIG_AUFS_XATTR
1394 .listxattr
= aufs_listxattr
,
1397 .readlink
= generic_readlink
,
1398 .get_link
= aufs_get_link
,
1400 /* .update_time = aufs_update_time */
1403 .create
= aufs_create
,
1404 .lookup
= aufs_lookup
,
1406 .unlink
= aufs_unlink
,
1407 .symlink
= aufs_symlink
,
1408 .mkdir
= aufs_mkdir
,
1409 .rmdir
= aufs_rmdir
,
1410 .mknod
= aufs_mknod
,
1411 .rename
= aufs_rename
,
1413 .permission
= aufs_permission
,
1414 #ifdef CONFIG_FS_POSIX_ACL
1415 .get_acl
= aufs_get_acl
,
1416 .set_acl
= aufs_set_acl
,
1419 .setattr
= aufs_setattr
,
1420 .getattr
= aufs_getattr
,
1422 #ifdef CONFIG_AUFS_XATTR
1423 .listxattr
= aufs_listxattr
,
1426 .update_time
= aufs_update_time
,
1427 .atomic_open
= aufs_atomic_open
,
1428 .tmpfile
= aufs_tmpfile
1431 .permission
= aufs_permission
,
1432 #ifdef CONFIG_FS_POSIX_ACL
1433 .get_acl
= aufs_get_acl
,
1434 .set_acl
= aufs_set_acl
,
1437 .setattr
= aufs_setattr
,
1438 .getattr
= aufs_getattr
,
1440 #ifdef CONFIG_AUFS_XATTR
1441 .listxattr
= aufs_listxattr
,
1444 .update_time
= aufs_update_time