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
);
892 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
896 if (ia
->ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
))
897 ia
->ia_valid
&= ~ATTR_MODE
;
901 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
905 if (ia
->ia_valid
& ATTR_FILE
) {
906 /* currently ftruncate(2) only */
907 AuDebugOn(!d_is_reg(dentry
));
909 err
= au_reval_and_lock_fdi(file
, au_reopen_nondir
, /*wlock*/1);
912 ia
->ia_file
= au_hf_top(file
);
913 a
->udba
= AuOpt_UDBA_NONE
;
915 /* fchmod() doesn't pass ia_file */
916 a
->udba
= au_opt_udba(sb
);
917 di_write_lock_child(dentry
);
918 /* no d_unlinked(), to set UDBA_NONE for root */
919 if (d_unhashed(dentry
))
920 a
->udba
= AuOpt_UDBA_NONE
;
921 if (a
->udba
!= AuOpt_UDBA_NONE
) {
922 AuDebugOn(IS_ROOT(dentry
));
923 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
929 err
= au_pin_and_icpup(dentry
, ia
, a
);
930 if (unlikely(err
< 0))
932 if (au_ftest_icpup(a
->flags
, DID_CPUP
)) {
934 ia
->ia_valid
&= ~ATTR_FILE
;
937 a
->h_path
.mnt
= au_sbr_mnt(sb
, a
->btgt
);
938 if ((ia
->ia_valid
& (ATTR_MODE
| ATTR_CTIME
))
939 == (ATTR_MODE
| ATTR_CTIME
)) {
940 err
= security_path_chmod(&a
->h_path
, ia
->ia_mode
);
943 } else if ((ia
->ia_valid
& (ATTR_UID
| ATTR_GID
))
944 && (ia
->ia_valid
& ATTR_CTIME
)) {
945 err
= security_path_chown(&a
->h_path
, ia
->ia_uid
, ia
->ia_gid
);
950 if (ia
->ia_valid
& ATTR_SIZE
) {
953 if (ia
->ia_size
< i_size_read(inode
))
955 truncate_setsize(inode
, ia
->ia_size
);
958 if (ia
->ia_valid
& ATTR_FILE
)
960 inode_unlock(a
->h_inode
);
961 err
= vfsub_trunc(&a
->h_path
, ia
->ia_size
, ia
->ia_valid
, f
);
962 inode_lock_nested(a
->h_inode
, AuLsc_I_CHILD
);
966 err
= vfsub_notify_change(&a
->h_path
, ia
, &delegated
);
968 err
= break_deleg_wait(&delegated
);
976 * regardless aufs 'acl' option setting.
977 * why don't all acl-aware fs call this func from their ->setattr()?
979 if (!err
&& (ia
->ia_valid
& ATTR_MODE
))
980 err
= vfsub_acl_chmod(a
->h_inode
, ia
->ia_mode
);
982 au_cpup_attr_changeable(inode
);
985 inode_unlock(a
->h_inode
);
988 au_update_dbtop(dentry
);
990 di_write_unlock(dentry
);
992 fi_write_unlock(file
);
994 ia
->ia_valid
|= ATTR_FILE
;
1005 #if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
1006 static int au_h_path_to_set_attr(struct dentry
*dentry
,
1007 struct au_icpup_args
*a
, struct path
*h_path
)
1010 struct super_block
*sb
;
1013 a
->udba
= au_opt_udba(sb
);
1014 /* no d_unlinked(), to set UDBA_NONE for root */
1015 if (d_unhashed(dentry
))
1016 a
->udba
= AuOpt_UDBA_NONE
;
1017 if (a
->udba
!= AuOpt_UDBA_NONE
) {
1018 AuDebugOn(IS_ROOT(dentry
));
1019 err
= au_reval_for_attr(dentry
, au_sigen(sb
));
1023 err
= au_pin_and_icpup(dentry
, /*ia*/NULL
, a
);
1024 if (unlikely(err
< 0))
1027 h_path
->dentry
= a
->h_path
.dentry
;
1028 h_path
->mnt
= au_sbr_mnt(sb
, a
->btgt
);
1034 ssize_t
au_srxattr(struct dentry
*dentry
, struct inode
*inode
,
1035 struct au_srxattr
*arg
)
1039 struct super_block
*sb
;
1040 struct au_icpup_args
*a
;
1041 struct inode
*h_inode
;
1046 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
1051 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1055 h_path
.dentry
= NULL
; /* silence gcc */
1056 di_write_lock_child(dentry
);
1057 err
= au_h_path_to_set_attr(dentry
, a
, &h_path
);
1061 inode_unlock(a
->h_inode
);
1062 switch (arg
->type
) {
1064 AuDebugOn(d_is_negative(h_path
.dentry
));
1065 err
= vfsub_setxattr(h_path
.dentry
,
1066 arg
->u
.set
.name
, arg
->u
.set
.value
,
1067 arg
->u
.set
.size
, arg
->u
.set
.flags
);
1069 case AU_XATTR_REMOVE
:
1070 err
= vfsub_removexattr(h_path
.dentry
, arg
->u
.remove
.name
);
1074 h_inode
= d_inode(h_path
.dentry
);
1075 if (h_inode
->i_op
->set_acl
)
1076 err
= h_inode
->i_op
->set_acl(h_inode
,
1078 arg
->u
.acl_set
.type
);
1082 au_cpup_attr_timesizes(inode
);
1086 au_update_dbtop(dentry
);
1089 di_write_unlock(dentry
);
1092 au_delayed_kfree(a
);
1099 static void au_refresh_iattr(struct inode
*inode
, struct kstat
*st
,
1104 inode
->i_mode
= st
->mode
;
1105 /* don't i_[ug]id_write() here */
1106 inode
->i_uid
= st
->uid
;
1107 inode
->i_gid
= st
->gid
;
1108 inode
->i_atime
= st
->atime
;
1109 inode
->i_mtime
= st
->mtime
;
1110 inode
->i_ctime
= st
->ctime
;
1112 au_cpup_attr_nlink(inode
, /*force*/0);
1113 if (S_ISDIR(inode
->i_mode
)) {
1117 smp_mb(); /* for i_nlink */
1119 set_nlink(inode
, n
);
1122 spin_lock(&inode
->i_lock
);
1123 inode
->i_blocks
= st
->blocks
;
1124 i_size_write(inode
, st
->size
);
1125 spin_unlock(&inode
->i_lock
);
1129 * common routine for aufs_getattr() and aufs_getxattr().
1130 * returns zero or negative (an error).
1131 * @dentry will be read-locked in success.
1133 int au_h_path_getattr(struct dentry
*dentry
, int force
, struct path
*h_path
)
1136 unsigned int mnt_flags
, sigen
;
1137 unsigned char udba_none
;
1138 aufs_bindex_t bindex
;
1139 struct super_block
*sb
, *h_sb
;
1140 struct inode
*inode
;
1143 h_path
->dentry
= NULL
;
1147 mnt_flags
= au_mntflags(sb
);
1148 udba_none
= !!au_opt_test(mnt_flags
, UDBA_NONE
);
1150 /* support fstat(2) */
1151 if (!d_unlinked(dentry
) && !udba_none
) {
1152 sigen
= au_sigen(sb
);
1153 err
= au_digen_test(dentry
, sigen
);
1155 di_read_lock_child(dentry
, AuLock_IR
);
1156 err
= au_dbrange_test(dentry
);
1157 if (unlikely(err
)) {
1158 di_read_unlock(dentry
, AuLock_IR
);
1162 AuDebugOn(IS_ROOT(dentry
));
1163 di_write_lock_child(dentry
);
1164 err
= au_dbrange_test(dentry
);
1166 err
= au_reval_for_attr(dentry
, sigen
);
1168 di_downgrade_lock(dentry
, AuLock_IR
);
1170 di_write_unlock(dentry
);
1175 di_read_lock_child(dentry
, AuLock_IR
);
1177 inode
= d_inode(dentry
);
1178 bindex
= au_ibtop(inode
);
1179 h_path
->mnt
= au_sbr_mnt(sb
, bindex
);
1180 h_sb
= h_path
->mnt
->mnt_sb
;
1182 && !au_test_fs_bad_iattr(h_sb
)
1184 goto out
; /* success */
1186 if (au_dbtop(dentry
) == bindex
)
1187 h_path
->dentry
= au_h_dptr(dentry
, bindex
);
1188 else if (au_opt_test(mnt_flags
, PLINK
) && au_plink_test(inode
)) {
1189 h_path
->dentry
= au_plink_lkup(inode
, bindex
);
1190 if (IS_ERR(h_path
->dentry
))
1191 /* pretending success */
1192 h_path
->dentry
= NULL
;
1194 dput(h_path
->dentry
);
1201 static int aufs_getattr(struct vfsmount
*mnt __maybe_unused
,
1202 struct dentry
*dentry
, struct kstat
*st
)
1205 unsigned char positive
;
1207 struct inode
*inode
;
1208 struct super_block
*sb
;
1210 inode
= d_inode(dentry
);
1212 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
1215 err
= au_h_path_getattr(dentry
, /*force*/0, &h_path
);
1218 if (unlikely(!h_path
.dentry
))
1219 /* illegally overlapped or something */
1220 goto out_fill
; /* pretending success */
1222 positive
= d_is_positive(h_path
.dentry
);
1224 err
= vfs_getattr(&h_path
, st
);
1227 au_refresh_iattr(inode
, st
,
1228 d_inode(h_path
.dentry
)->i_nlink
);
1229 goto out_fill
; /* success */
1235 generic_fillattr(inode
, st
);
1237 di_read_unlock(dentry
, AuLock_IR
);
1245 /* ---------------------------------------------------------------------- */
1247 static const char *aufs_get_link(struct dentry
*dentry
, struct inode
*inode
,
1248 struct delayed_call
*done
)
1251 struct dentry
*h_dentry
;
1252 struct inode
*h_inode
;
1254 aufs_bindex_t bindex
;
1256 ret
= NULL
; /* suppress a warning */
1261 err
= aufs_read_lock(dentry
, AuLock_IR
| AuLock_GEN
);
1265 err
= au_d_hashed_positive(dentry
);
1270 inode
= d_inode(dentry
);
1271 bindex
= au_ibtop(inode
);
1272 h_inode
= au_h_iptr(inode
, bindex
);
1273 if (unlikely(!h_inode
->i_op
->get_link
))
1278 if (au_dbtop(dentry
) <= bindex
) {
1279 h_dentry
= au_h_dptr(dentry
, bindex
);
1284 h_dentry
= d_find_any_alias(h_inode
);
1285 if (IS_ERR(h_dentry
)) {
1286 err
= PTR_ERR(h_dentry
);
1290 if (unlikely(!h_dentry
))
1294 AuDbg("%pf\n", h_inode
->i_op
->get_link
);
1295 AuDbgDentry(h_dentry
);
1296 ret
= h_inode
->i_op
->get_link(h_dentry
, h_inode
, done
);
1302 aufs_read_unlock(dentry
, AuLock_IR
);
1310 /* ---------------------------------------------------------------------- */
1312 static int au_is_special(struct inode
*inode
)
1314 return (inode
->i_mode
& (S_IFBLK
| S_IFCHR
| S_IFIFO
| S_IFSOCK
));
1317 static int aufs_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
1320 aufs_bindex_t bindex
;
1321 struct super_block
*sb
;
1322 struct inode
*h_inode
;
1323 struct vfsmount
*h_mnt
;
1326 WARN_ONCE((flags
& S_ATIME
) && !IS_NOATIME(inode
),
1327 "unexpected s_flags 0x%lx", sb
->s_flags
);
1329 /* mmap_sem might be acquired already, cf. aufs_mmap() */
1331 si_read_lock(sb
, AuLock_FLUSH
);
1332 ii_write_lock_child(inode
);
1336 bindex
= au_ibtop(inode
);
1337 h_inode
= au_h_iptr(inode
, bindex
);
1338 if (!au_test_ro(sb
, bindex
, inode
)) {
1339 h_mnt
= au_sbr_mnt(sb
, bindex
);
1340 err
= vfsub_mnt_want_write(h_mnt
);
1342 err
= vfsub_update_time(h_inode
, ts
, flags
);
1343 vfsub_mnt_drop_write(h_mnt
);
1345 } else if (au_is_special(h_inode
)) {
1347 * Never copy-up here.
1348 * These special files may already be opened and used for
1349 * communicating. If we copied it up, then the communication
1350 * would be corrupted.
1352 AuWarn1("timestamps for i%lu are ignored "
1353 "since it is on readonly branch (hi%lu).\n",
1354 inode
->i_ino
, h_inode
->i_ino
);
1355 } else if (flags
& ~S_ATIME
) {
1357 AuIOErr1("unexpected flags 0x%x\n", flags
);
1363 au_cpup_attr_timesizes(inode
);
1364 ii_write_unlock(inode
);
1368 if (!err
&& (flags
& S_VERSION
))
1369 inode_inc_iversion(inode
);
1374 /* ---------------------------------------------------------------------- */
1376 /* no getattr version will be set by module.c:aufs_init() */
1377 struct inode_operations aufs_iop_nogetattr
[AuIop_Last
],
1380 .permission
= aufs_permission
,
1381 #ifdef CONFIG_FS_POSIX_ACL
1382 .get_acl
= aufs_get_acl
,
1383 .set_acl
= aufs_set_acl
, /* unsupport for symlink? */
1386 .setattr
= aufs_setattr
,
1387 .getattr
= aufs_getattr
,
1389 #ifdef CONFIG_AUFS_XATTR
1390 .listxattr
= aufs_listxattr
,
1393 .readlink
= generic_readlink
,
1394 .get_link
= aufs_get_link
,
1396 /* .update_time = aufs_update_time */
1399 .create
= aufs_create
,
1400 .lookup
= aufs_lookup
,
1402 .unlink
= aufs_unlink
,
1403 .symlink
= aufs_symlink
,
1404 .mkdir
= aufs_mkdir
,
1405 .rmdir
= aufs_rmdir
,
1406 .mknod
= aufs_mknod
,
1407 .rename
= aufs_rename
,
1409 .permission
= aufs_permission
,
1410 #ifdef CONFIG_FS_POSIX_ACL
1411 .get_acl
= aufs_get_acl
,
1412 .set_acl
= aufs_set_acl
,
1415 .setattr
= aufs_setattr
,
1416 .getattr
= aufs_getattr
,
1418 #ifdef CONFIG_AUFS_XATTR
1419 .listxattr
= aufs_listxattr
,
1422 .update_time
= aufs_update_time
,
1423 .atomic_open
= aufs_atomic_open
,
1424 .tmpfile
= aufs_tmpfile
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