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 * copy-up functions, see wbr_policy.c for copy-down
22 #include <linux/fs_stack.h>
24 #include <linux/task_work.h>
27 void au_cpup_attr_flags(struct inode
*dst
, unsigned int iflags
)
29 const unsigned int mask
= S_DEAD
| S_SWAPFILE
| S_PRIVATE
30 | S_NOATIME
| S_NOCMTIME
| S_AUTOMOUNT
;
32 BUILD_BUG_ON(sizeof(iflags
) != sizeof(dst
->i_flags
));
34 dst
->i_flags
|= iflags
& ~mask
;
35 if (au_test_fs_notime(dst
->i_sb
))
36 dst
->i_flags
|= S_NOATIME
| S_NOCMTIME
;
39 void au_cpup_attr_timesizes(struct inode
*inode
)
41 struct inode
*h_inode
;
43 h_inode
= au_h_iptr(inode
, au_ibtop(inode
));
44 fsstack_copy_attr_times(inode
, h_inode
);
45 fsstack_copy_inode_size(inode
, h_inode
);
48 void au_cpup_attr_nlink(struct inode
*inode
, int force
)
50 struct inode
*h_inode
;
51 struct super_block
*sb
;
52 aufs_bindex_t bindex
, bbot
;
55 bindex
= au_ibtop(inode
);
56 h_inode
= au_h_iptr(inode
, bindex
);
58 && !S_ISDIR(h_inode
->i_mode
)
59 && au_opt_test(au_mntflags(sb
), PLINK
)
60 && au_plink_test(inode
))
64 * 0 can happen in revalidating.
65 * h_inode->i_mutex may not be held here, but it is harmless since once
66 * i_nlink reaches 0, it will never become positive except O_TMPFILE
68 * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
69 * the incorrect link count.
71 set_nlink(inode
, h_inode
->i_nlink
);
74 * fewer nlink makes find(1) noisy, but larger nlink doesn't.
75 * it may includes whplink directory.
77 if (S_ISDIR(h_inode
->i_mode
)) {
78 bbot
= au_ibbot(inode
);
79 for (bindex
++; bindex
<= bbot
; bindex
++) {
80 h_inode
= au_h_iptr(inode
, bindex
);
82 au_add_nlink(inode
, h_inode
);
87 void au_cpup_attr_changeable(struct inode
*inode
)
89 struct inode
*h_inode
;
91 h_inode
= au_h_iptr(inode
, au_ibtop(inode
));
92 inode
->i_mode
= h_inode
->i_mode
;
93 inode
->i_uid
= h_inode
->i_uid
;
94 inode
->i_gid
= h_inode
->i_gid
;
95 au_cpup_attr_timesizes(inode
);
96 au_cpup_attr_flags(inode
, h_inode
->i_flags
);
99 void au_cpup_igen(struct inode
*inode
, struct inode
*h_inode
)
101 struct au_iinfo
*iinfo
= au_ii(inode
);
103 IiMustWriteLock(inode
);
105 iinfo
->ii_higen
= h_inode
->i_generation
;
106 iinfo
->ii_hsb1
= h_inode
->i_sb
;
109 void au_cpup_attr_all(struct inode
*inode
, int force
)
111 struct inode
*h_inode
;
113 h_inode
= au_h_iptr(inode
, au_ibtop(inode
));
114 au_cpup_attr_changeable(inode
);
115 if (inode
->i_nlink
> 0)
116 au_cpup_attr_nlink(inode
, force
);
117 inode
->i_rdev
= h_inode
->i_rdev
;
118 inode
->i_blkbits
= h_inode
->i_blkbits
;
119 au_cpup_igen(inode
, h_inode
);
122 /* ---------------------------------------------------------------------- */
124 /* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
126 /* keep the timestamps of the parent dir when cpup */
127 void au_dtime_store(struct au_dtime
*dt
, struct dentry
*dentry
,
130 struct inode
*h_inode
;
132 dt
->dt_dentry
= dentry
;
133 dt
->dt_h_path
= *h_path
;
134 h_inode
= d_inode(h_path
->dentry
);
135 dt
->dt_atime
= h_inode
->i_atime
;
136 dt
->dt_mtime
= h_inode
->i_mtime
;
140 void au_dtime_revert(struct au_dtime
*dt
)
145 attr
.ia_atime
= dt
->dt_atime
;
146 attr
.ia_mtime
= dt
->dt_mtime
;
147 attr
.ia_valid
= ATTR_FORCE
| ATTR_MTIME
| ATTR_MTIME_SET
148 | ATTR_ATIME
| ATTR_ATIME_SET
;
150 /* no delegation since this is a directory */
151 err
= vfsub_notify_change(&dt
->dt_h_path
, &attr
, /*delegated*/NULL
);
153 pr_warn("restoring timestamps failed(%d). ignored\n", err
);
156 /* ---------------------------------------------------------------------- */
158 /* internal use only */
159 struct au_cpup_reg_attr
{
162 unsigned int iflags
; /* inode->i_flags */
165 static noinline_for_stack
166 int cpup_iattr(struct dentry
*dst
, aufs_bindex_t bindex
, struct dentry
*h_src
,
167 struct au_cpup_reg_attr
*h_src_attr
)
169 int err
, sbits
, icex
;
170 unsigned int mnt_flags
;
171 unsigned char verbose
;
174 struct inode
*h_isrc
, *h_idst
;
176 struct au_branch
*br
;
178 h_path
.dentry
= au_h_dptr(dst
, bindex
);
179 h_idst
= d_inode(h_path
.dentry
);
180 br
= au_sbr(dst
->d_sb
, bindex
);
181 h_path
.mnt
= au_br_mnt(br
);
182 h_isrc
= d_inode(h_src
);
183 ia
.ia_valid
= ATTR_FORCE
| ATTR_UID
| ATTR_GID
184 | ATTR_ATIME
| ATTR_MTIME
185 | ATTR_ATIME_SET
| ATTR_MTIME_SET
;
186 if (h_src_attr
&& h_src_attr
->valid
) {
187 h_st
= &h_src_attr
->st
;
188 ia
.ia_uid
= h_st
->uid
;
189 ia
.ia_gid
= h_st
->gid
;
190 ia
.ia_atime
= h_st
->atime
;
191 ia
.ia_mtime
= h_st
->mtime
;
192 if (h_idst
->i_mode
!= h_st
->mode
193 && !S_ISLNK(h_idst
->i_mode
)) {
194 ia
.ia_valid
|= ATTR_MODE
;
195 ia
.ia_mode
= h_st
->mode
;
197 sbits
= !!(h_st
->mode
& (S_ISUID
| S_ISGID
));
198 au_cpup_attr_flags(h_idst
, h_src_attr
->iflags
);
200 ia
.ia_uid
= h_isrc
->i_uid
;
201 ia
.ia_gid
= h_isrc
->i_gid
;
202 ia
.ia_atime
= h_isrc
->i_atime
;
203 ia
.ia_mtime
= h_isrc
->i_mtime
;
204 if (h_idst
->i_mode
!= h_isrc
->i_mode
205 && !S_ISLNK(h_idst
->i_mode
)) {
206 ia
.ia_valid
|= ATTR_MODE
;
207 ia
.ia_mode
= h_isrc
->i_mode
;
209 sbits
= !!(h_isrc
->i_mode
& (S_ISUID
| S_ISGID
));
210 au_cpup_attr_flags(h_idst
, h_isrc
->i_flags
);
212 /* no delegation since it is just created */
213 err
= vfsub_notify_change(&h_path
, &ia
, /*delegated*/NULL
);
215 /* is this nfs only? */
216 if (!err
&& sbits
&& au_test_nfs(h_path
.dentry
->d_sb
)) {
217 ia
.ia_valid
= ATTR_FORCE
| ATTR_MODE
;
218 ia
.ia_mode
= h_isrc
->i_mode
;
219 err
= vfsub_notify_change(&h_path
, &ia
, /*delegated*/NULL
);
222 icex
= br
->br_perm
& AuBrAttr_ICEX
;
224 mnt_flags
= au_mntflags(dst
->d_sb
);
225 verbose
= !!au_opt_test(mnt_flags
, VERBOSE
);
226 err
= au_cpup_xattr(h_path
.dentry
, h_src
, icex
, verbose
);
232 /* ---------------------------------------------------------------------- */
234 static int au_do_copy_file(struct file
*dst
, struct file
*src
, loff_t len
,
235 char *buf
, unsigned long blksize
)
238 size_t sz
, rbytes
, wbytes
;
239 unsigned char all_zero
;
241 struct inode
*h_inode
;
242 /* reduce stack usage */
245 zp
= page_address(ZERO_PAGE(0));
247 return -ENOMEM
; /* possible? */
252 AuDbg("len %lld\n", len
);
258 /* todo: signal_pending? */
259 while (!rbytes
|| err
== -EAGAIN
|| err
== -EINTR
) {
260 rbytes
= vfsub_read_k(src
, buf
, sz
, &src
->f_pos
);
263 if (unlikely(err
< 0))
267 if (len
>= rbytes
&& rbytes
== blksize
)
268 all_zero
= !memcmp(buf
, zp
, rbytes
);
275 b
= vfsub_write_k(dst
, p
, wbytes
, &dst
->f_pos
);
277 /* todo: signal_pending? */
278 if (unlikely(err
== -EAGAIN
|| err
== -EINTR
))
280 if (unlikely(err
< 0))
285 if (unlikely(err
< 0))
291 res
= vfsub_llseek(dst
, rbytes
, SEEK_CUR
);
293 if (unlikely(res
< 0))
300 /* the last block may be a hole */
301 if (!err
&& all_zero
) {
305 if (au_test_nfs(dst
->f_path
.dentry
->d_sb
)) {
306 /* nfs requires this step to make last hole */
307 /* is this only nfs? */
309 /* todo: signal_pending? */
310 err
= vfsub_write_k(dst
, "\0", 1, &dst
->f_pos
);
311 } while (err
== -EAGAIN
|| err
== -EINTR
);
318 ia
->ia_size
= dst
->f_pos
;
319 ia
->ia_valid
= ATTR_SIZE
| ATTR_FILE
;
321 h_inode
= file_inode(dst
);
322 inode_lock_nested(h_inode
, AuLsc_I_CHILD2
);
323 /* no delegation since it is just created */
324 err
= vfsub_notify_change(&dst
->f_path
, ia
,
326 inode_unlock(h_inode
);
333 int au_copy_file(struct file
*dst
, struct file
*src
, loff_t len
)
336 unsigned long blksize
;
337 unsigned char do_kfree
;
341 blksize
= dst
->f_path
.dentry
->d_sb
->s_blocksize
;
342 if (!blksize
|| PAGE_SIZE
< blksize
)
344 AuDbg("blksize %lu\n", blksize
);
345 do_kfree
= (blksize
!= PAGE_SIZE
&& blksize
>= sizeof(struct iattr
*));
347 buf
= kmalloc(blksize
, GFP_NOFS
);
349 buf
= (void *)__get_free_page(GFP_NOFS
);
354 AuDbg("copying a large file %lld\n", (long long)len
);
358 err
= au_do_copy_file(dst
, src
, len
, buf
, blksize
);
360 au_delayed_kfree(buf
);
362 au_delayed_free_page((unsigned long)buf
);
368 static int au_do_copy(struct file
*dst
, struct file
*src
, loff_t len
)
371 struct super_block
*h_src_sb
;
372 struct inode
*h_src_inode
;
374 h_src_inode
= file_inode(src
);
375 h_src_sb
= h_src_inode
->i_sb
;
377 /* XFS acquires inode_lock */
378 if (!au_test_xfs(h_src_sb
))
379 err
= au_copy_file(dst
, src
, len
);
381 inode_unlock(h_src_inode
);
382 err
= au_copy_file(dst
, src
, len
);
383 inode_lock(h_src_inode
);
389 static int au_clone_or_copy(struct file
*dst
, struct file
*src
, loff_t len
)
392 struct super_block
*h_src_sb
;
393 struct inode
*h_src_inode
;
395 h_src_inode
= file_inode(src
);
396 h_src_sb
= h_src_inode
->i_sb
;
397 if (h_src_sb
!= file_inode(dst
)->i_sb
398 || !dst
->f_op
->clone_file_range
) {
399 err
= au_do_copy(dst
, src
, len
);
403 if (!au_test_nfs(h_src_sb
)) {
404 inode_unlock(h_src_inode
);
405 err
= vfsub_clone_file_range(src
, dst
, len
);
406 inode_lock(h_src_inode
);
408 err
= vfsub_clone_file_range(src
, dst
, len
);
409 /* older XFS has a condition in cloning */
410 if (unlikely(err
!= -EOPNOTSUPP
))
413 /* the backend fs on NFS may not support cloning */
414 err
= au_do_copy(dst
, src
, len
);
422 * to support a sparse file which is opened with O_APPEND,
423 * we need to close the file.
425 static int au_cp_regular(struct au_cp_generic
*cpg
)
430 aufs_bindex_t bindex
;
432 struct dentry
*dentry
;
439 .flags
= O_RDONLY
| O_NOATIME
| O_LARGEFILE
,
444 .flags
= O_WRONLY
| O_NOATIME
| O_LARGEFILE
,
445 .force_wr
= !!au_ftest_cpup(cpg
->flags
, RWDST
),
449 struct super_block
*sb
, *h_src_sb
;
450 struct inode
*h_src_inode
;
451 struct task_struct
*tsk
= current
;
453 /* bsrc branch can be ro/rw. */
454 sb
= cpg
->dentry
->d_sb
;
456 for (i
= 0; i
< 2; i
++, f
++) {
457 f
->dentry
= au_h_dptr(cpg
->dentry
, f
->bindex
);
458 f
->file
= au_h_open(cpg
->dentry
, f
->bindex
, f
->flags
,
459 /*file*/NULL
, f
->force_wr
);
460 err
= PTR_ERR(f
->file
);
465 /* try stopping to update while we copyup */
466 h_src_inode
= d_inode(file
[SRC
].dentry
);
467 h_src_sb
= h_src_inode
->i_sb
;
468 if (!au_test_nfs(h_src_sb
))
469 IMustLock(h_src_inode
);
470 err
= au_clone_or_copy(file
[DST
].file
, file
[SRC
].file
, cpg
->len
);
472 /* i wonder if we had O_NO_DELAY_FPUT flag */
473 if (tsk
->flags
& PF_KTHREAD
)
474 __fput_sync(file
[DST
].file
);
476 WARN(1, "%pD\nPlease report this warning to aufs-users ML",
478 fput(file
[DST
].file
);
481 * we have to call both since we don't know which place the file
485 flush_delayed_fput();
487 au_sbr_put(sb
, file
[DST
].bindex
);
490 fput(file
[SRC
].file
);
491 au_sbr_put(sb
, file
[SRC
].bindex
);
496 static int au_do_cpup_regular(struct au_cp_generic
*cpg
,
497 struct au_cpup_reg_attr
*h_src_attr
)
502 struct inode
*h_src_inode
, *h_dst_inode
;
505 h_src_inode
= au_h_iptr(d_inode(cpg
->dentry
), cpg
->bsrc
);
506 l
= i_size_read(h_src_inode
);
507 if (cpg
->len
== -1 || l
< cpg
->len
)
510 /* try stopping to update while we are referencing */
511 inode_lock_nested(h_src_inode
, AuLsc_I_CHILD
);
512 au_pin_hdir_unlock(cpg
->pin
);
514 h_path
.dentry
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
515 h_path
.mnt
= au_sbr_mnt(cpg
->dentry
->d_sb
, cpg
->bsrc
);
516 h_src_attr
->iflags
= h_src_inode
->i_flags
;
517 if (!au_test_nfs(h_src_inode
->i_sb
))
518 err
= vfs_getattr(&h_path
, &h_src_attr
->st
);
520 inode_unlock(h_src_inode
);
521 err
= vfs_getattr(&h_path
, &h_src_attr
->st
);
522 inode_lock_nested(h_src_inode
, AuLsc_I_CHILD
);
525 inode_unlock(h_src_inode
);
528 h_src_attr
->valid
= 1;
529 if (!au_test_nfs(h_src_inode
->i_sb
)) {
530 err
= au_cp_regular(cpg
);
531 inode_unlock(h_src_inode
);
533 inode_unlock(h_src_inode
);
534 err
= au_cp_regular(cpg
);
536 rerr
= au_pin_hdir_relock(cpg
->pin
);
540 if (!err
&& (h_src_inode
->i_state
& I_LINKABLE
)) {
541 h_path
.dentry
= au_h_dptr(cpg
->dentry
, cpg
->bdst
);
542 h_dst_inode
= d_inode(h_path
.dentry
);
543 spin_lock(&h_dst_inode
->i_lock
);
544 h_dst_inode
->i_state
|= I_LINKABLE
;
545 spin_unlock(&h_dst_inode
->i_lock
);
552 static int au_do_cpup_symlink(struct path
*h_path
, struct dentry
*h_src
,
563 sym
.k
= (void *)__get_free_page(GFP_NOFS
);
564 if (unlikely(!sym
.k
))
567 /* unnecessary to support mmap_sem since symlink is not mmap-able */
570 symlen
= vfs_readlink(h_src
, sym
.u
, PATH_MAX
);
576 err
= vfsub_symlink(h_dir
, h_path
, sym
.k
);
578 au_delayed_free_page((unsigned long)sym
.k
);
585 * regardless 'acl' option, reset all ACL.
586 * All ACL will be copied up later from the original entry on the lower branch.
588 static int au_reset_acl(struct inode
*h_dir
, struct path
*h_path
, umode_t mode
)
591 struct dentry
*h_dentry
;
592 struct inode
*h_inode
;
594 h_dentry
= h_path
->dentry
;
595 h_inode
= d_inode(h_dentry
);
596 /* forget_all_cached_acls(h_inode)); */
597 err
= vfsub_removexattr(h_dentry
, XATTR_NAME_POSIX_ACL_ACCESS
);
599 if (err
== -EOPNOTSUPP
)
602 err
= vfsub_acl_chmod(h_inode
, mode
);
608 static int au_do_cpup_dir(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
,
609 struct inode
*h_dir
, struct path
*h_path
)
612 struct inode
*dir
, *inode
;
614 err
= vfsub_removexattr(h_path
->dentry
, XATTR_NAME_POSIX_ACL_DEFAULT
);
616 if (err
== -EOPNOTSUPP
)
622 * strange behaviour from the users view,
623 * particularry setattr case
625 dir
= d_inode(dst_parent
);
626 if (au_ibtop(dir
) == cpg
->bdst
)
627 au_cpup_attr_nlink(dir
, /*force*/1);
628 inode
= d_inode(cpg
->dentry
);
629 au_cpup_attr_nlink(inode
, /*force*/1);
635 static noinline_for_stack
636 int cpup_entry(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
,
637 struct au_cpup_reg_attr
*h_src_attr
)
641 unsigned int mnt_flags
;
642 unsigned char isdir
, isreg
, force
;
643 const unsigned char do_dt
= !!au_ftest_cpup(cpg
->flags
, DTIME
);
646 struct dentry
*h_src
, *h_dst
, *h_parent
;
647 struct inode
*h_inode
, *h_dir
;
648 struct super_block
*sb
;
650 /* bsrc branch can be ro/rw. */
651 h_src
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
652 h_inode
= d_inode(h_src
);
653 AuDebugOn(h_inode
!= au_h_iptr(d_inode(cpg
->dentry
), cpg
->bsrc
));
655 /* try stopping to be referenced while we are creating */
656 h_dst
= au_h_dptr(cpg
->dentry
, cpg
->bdst
);
657 if (au_ftest_cpup(cpg
->flags
, RENAME
))
658 AuDebugOn(strncmp(h_dst
->d_name
.name
, AUFS_WH_PFX
,
660 h_parent
= h_dst
->d_parent
; /* dir inode is locked */
661 h_dir
= d_inode(h_parent
);
663 AuDebugOn(h_parent
!= h_dst
->d_parent
);
665 sb
= cpg
->dentry
->d_sb
;
666 h_path
.mnt
= au_sbr_mnt(sb
, cpg
->bdst
);
668 h_path
.dentry
= h_parent
;
669 au_dtime_store(&dt
, dst_parent
, &h_path
);
671 h_path
.dentry
= h_dst
;
675 mode
= h_inode
->i_mode
;
676 switch (mode
& S_IFMT
) {
679 err
= vfsub_create(h_dir
, &h_path
, S_IRUSR
| S_IWUSR
,
682 err
= au_do_cpup_regular(cpg
, h_src_attr
);
686 err
= vfsub_mkdir(h_dir
, &h_path
, mode
);
688 err
= au_do_cpup_dir(cpg
, dst_parent
, h_dir
, &h_path
);
691 err
= au_do_cpup_symlink(&h_path
, h_src
, h_dir
);
695 AuDebugOn(!capable(CAP_MKNOD
));
699 err
= vfsub_mknod(h_dir
, &h_path
, mode
, h_inode
->i_rdev
);
702 AuIOErr("Unknown inode type 0%o\n", mode
);
706 err
= au_reset_acl(h_dir
, &h_path
, mode
);
708 mnt_flags
= au_mntflags(sb
);
709 if (!au_opt_test(mnt_flags
, UDBA_NONE
)
711 && au_opt_test(mnt_flags
, XINO
)
712 && (h_inode
->i_nlink
== 1
713 || (h_inode
->i_state
& I_LINKABLE
))
714 /* todo: unnecessary? */
715 /* && d_inode(cpg->dentry)->i_nlink == 1 */
716 && cpg
->bdst
< cpg
->bsrc
717 && !au_ftest_cpup(cpg
->flags
, KEEPLINO
))
718 au_xino_write(sb
, cpg
->bsrc
, h_inode
->i_ino
, /*ino*/0);
719 /* ignore this error */
726 force
= !!i_size_read(h_inode
);
728 au_fhsm_wrote(sb
, cpg
->bdst
, force
);
732 au_dtime_revert(&dt
);
736 static int au_do_ren_after_cpup(struct au_cp_generic
*cpg
, struct path
*h_path
)
739 struct dentry
*dentry
, *h_dentry
, *h_parent
, *parent
;
743 dentry
= cpg
->dentry
;
745 h_dentry
= au_h_dptr(dentry
, bdst
);
746 if (!au_ftest_cpup(cpg
->flags
, OVERWRITE
)) {
748 au_set_h_dptr(dentry
, bdst
, NULL
);
749 err
= au_lkup_neg(dentry
, bdst
, /*wh*/0);
751 h_path
->dentry
= dget(au_h_dptr(dentry
, bdst
));
752 au_set_h_dptr(dentry
, bdst
, h_dentry
);
755 parent
= dget_parent(dentry
);
756 h_parent
= au_h_dptr(parent
, bdst
);
758 h_path
->dentry
= vfsub_lkup_one(&dentry
->d_name
, h_parent
);
759 if (IS_ERR(h_path
->dentry
))
760 err
= PTR_ERR(h_path
->dentry
);
765 h_parent
= h_dentry
->d_parent
; /* dir inode is locked */
766 h_dir
= d_inode(h_parent
);
768 AuDbg("%pd %pd\n", h_dentry
, h_path
->dentry
);
769 /* no delegation since it is just created */
770 err
= vfsub_rename(h_dir
, h_dentry
, h_dir
, h_path
, /*delegated*/NULL
,
772 dput(h_path
->dentry
);
779 * copyup the @dentry from @bsrc to @bdst.
780 * the caller must set the both of lower dentries.
781 * @len is for truncating when it is -1 copyup the entire file.
782 * in link/rename cases, @dst_parent may be different from the real one.
783 * basic->bsrc can be larger than basic->bdst.
784 * aufs doesn't touch the credential so
785 * security_inode_copy_up{,_xattr}() are unnecrssary.
787 static int au_cpup_single(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
)
790 aufs_bindex_t old_ibtop
;
791 unsigned char isdir
, plink
;
792 struct dentry
*h_src
, *h_dst
, *h_parent
;
793 struct inode
*dst_inode
, *h_dir
, *inode
, *delegated
, *src_inode
;
794 struct super_block
*sb
;
795 struct au_branch
*br
;
796 /* to reuduce stack size */
800 struct au_cpup_reg_attr h_src_attr
;
804 a
= kmalloc(sizeof(*a
), GFP_NOFS
);
807 a
->h_src_attr
.valid
= 0;
809 sb
= cpg
->dentry
->d_sb
;
810 br
= au_sbr(sb
, cpg
->bdst
);
811 a
->h_path
.mnt
= au_br_mnt(br
);
812 h_dst
= au_h_dptr(cpg
->dentry
, cpg
->bdst
);
813 h_parent
= h_dst
->d_parent
; /* dir inode is locked */
814 h_dir
= d_inode(h_parent
);
817 h_src
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
818 inode
= d_inode(cpg
->dentry
);
821 dst_parent
= dget_parent(cpg
->dentry
);
825 plink
= !!au_opt_test(au_mntflags(sb
), PLINK
);
826 dst_inode
= au_h_iptr(inode
, cpg
->bdst
);
828 if (unlikely(!plink
)) {
830 AuIOErr("hi%lu(i%lu) exists on b%d "
831 "but plink is disabled\n",
832 dst_inode
->i_ino
, inode
->i_ino
, cpg
->bdst
);
836 if (dst_inode
->i_nlink
) {
837 const int do_dt
= au_ftest_cpup(cpg
->flags
, DTIME
);
839 h_src
= au_plink_lkup(inode
, cpg
->bdst
);
840 err
= PTR_ERR(h_src
);
843 if (unlikely(d_is_negative(h_src
))) {
845 AuIOErr("i%lu exists on b%d "
846 "but not pseudo-linked\n",
847 inode
->i_ino
, cpg
->bdst
);
853 a
->h_path
.dentry
= h_parent
;
854 au_dtime_store(&a
->dt
, dst_parent
, &a
->h_path
);
857 a
->h_path
.dentry
= h_dst
;
859 err
= vfsub_link(h_src
, h_dir
, &a
->h_path
, &delegated
);
860 if (!err
&& au_ftest_cpup(cpg
->flags
, RENAME
))
861 err
= au_do_ren_after_cpup(cpg
, &a
->h_path
);
863 au_dtime_revert(&a
->dt
);
864 if (unlikely(err
== -EWOULDBLOCK
)) {
865 pr_warn("cannot retry for NFSv4 delegation"
866 " for an internal link\n");
872 /* todo: cpup_wh_file? */
874 au_update_ibrange(inode
, /*do_put_zero*/1);
877 isdir
= S_ISDIR(inode
->i_mode
);
878 old_ibtop
= au_ibtop(inode
);
879 err
= cpup_entry(cpg
, dst_parent
, &a
->h_src_attr
);
882 dst_inode
= d_inode(h_dst
);
883 inode_lock_nested(dst_inode
, AuLsc_I_CHILD2
);
884 /* todo: necessary? */
885 /* au_pin_hdir_unlock(cpg->pin); */
887 err
= cpup_iattr(cpg
->dentry
, cpg
->bdst
, h_src
, &a
->h_src_attr
);
889 /* todo: necessary? */
890 /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
891 inode_unlock(dst_inode
);
895 if (cpg
->bdst
< old_ibtop
) {
896 if (S_ISREG(inode
->i_mode
)) {
897 err
= au_dy_iaop(inode
, cpg
->bdst
, dst_inode
);
899 /* ignore an error */
900 /* au_pin_hdir_relock(cpg->pin); */
901 inode_unlock(dst_inode
);
905 au_set_ibtop(inode
, cpg
->bdst
);
907 au_set_ibbot(inode
, cpg
->bdst
);
908 au_set_h_iptr(inode
, cpg
->bdst
, au_igrab(dst_inode
),
909 au_hi_flags(inode
, isdir
));
911 /* todo: necessary? */
912 /* err = au_pin_hdir_relock(cpg->pin); */
913 inode_unlock(dst_inode
);
917 src_inode
= d_inode(h_src
);
919 && (src_inode
->i_nlink
> 1
920 || src_inode
->i_state
& I_LINKABLE
)
922 au_plink_append(inode
, cpg
->bdst
, h_dst
);
924 if (au_ftest_cpup(cpg
->flags
, RENAME
)) {
925 a
->h_path
.dentry
= h_dst
;
926 err
= au_do_ren_after_cpup(cpg
, &a
->h_path
);
929 goto out_parent
; /* success */
933 a
->h_path
.dentry
= h_parent
;
934 au_dtime_store(&a
->dt
, dst_parent
, &a
->h_path
);
935 a
->h_path
.dentry
= h_dst
;
937 if (d_is_positive(h_dst
)) {
939 /* no delegation since it is just created */
940 rerr
= vfsub_unlink(h_dir
, &a
->h_path
,
941 /*delegated*/NULL
, /*force*/0);
943 rerr
= vfsub_rmdir(h_dir
, &a
->h_path
);
945 au_dtime_revert(&a
->dt
);
947 AuIOErr("failed removing broken entry(%d, %d)\n", err
, rerr
);
958 struct au_cpup_single_args
{
960 struct au_cp_generic
*cpg
;
961 struct dentry
*dst_parent
;
964 static void au_call_cpup_single(void *args
)
966 struct au_cpup_single_args
*a
= args
;
968 au_pin_hdir_acquire_nest(a
->cpg
->pin
);
969 *a
->errp
= au_cpup_single(a
->cpg
, a
->dst_parent
);
970 au_pin_hdir_release(a
->cpg
->pin
);
975 * prevent SIGXFSZ in copy-up.
976 * testing CAP_MKNOD is for generic fs,
977 * but CAP_FSETID is for xfs only, currently.
979 static int au_cpup_sio_test(struct au_pin
*pin
, umode_t mode
)
982 struct super_block
*sb
;
986 sb
= au_pinned_parent(pin
)->d_sb
;
988 && (!au_sbi(sb
)->si_plink_maint_pid
989 || au_plink_maint(sb
, AuLock_NOPLM
))) {
990 switch (mode
& S_IFMT
) {
992 /* no condition about RLIMIT_FSIZE and the file size */
997 do_sio
= !capable(CAP_MKNOD
);
1001 do_sio
= ((mode
& (S_ISUID
| S_ISGID
))
1002 && !capable(CAP_FSETID
));
1003 /* this workaround may be removed in the future */
1005 h_dir
= au_pinned_h_dir(pin
);
1006 do_sio
= h_dir
->i_mode
& S_ISVTX
;
1013 #if 0 /* reserved */
1014 int au_sio_cpup_single(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
)
1017 struct dentry
*h_dentry
;
1019 h_dentry
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
1020 if (!au_cpup_sio_test(pin
, d_inode(h_dentry
)->i_mode
))
1021 err
= au_cpup_single(cpg
, dst_parent
);
1023 struct au_cpup_single_args args
= {
1026 .dst_parent
= dst_parent
1028 wkq_err
= au_wkq_wait(au_call_cpup_single
, &args
);
1029 if (unlikely(wkq_err
))
1038 * copyup the @dentry from the first active lower branch to @bdst,
1039 * using au_cpup_single().
1041 static int au_cpup_simple(struct au_cp_generic
*cpg
)
1044 unsigned int flags_orig
;
1045 struct dentry
*dentry
;
1047 AuDebugOn(cpg
->bsrc
< 0);
1049 dentry
= cpg
->dentry
;
1050 DiMustWriteLock(dentry
);
1052 err
= au_lkup_neg(dentry
, cpg
->bdst
, /*wh*/1);
1054 flags_orig
= cpg
->flags
;
1055 au_fset_cpup(cpg
->flags
, RENAME
);
1056 err
= au_cpup_single(cpg
, NULL
);
1057 cpg
->flags
= flags_orig
;
1059 return 0; /* success */
1062 au_set_h_dptr(dentry
, cpg
->bdst
, NULL
);
1063 au_set_dbtop(dentry
, cpg
->bsrc
);
1069 struct au_cpup_simple_args
{
1071 struct au_cp_generic
*cpg
;
1074 static void au_call_cpup_simple(void *args
)
1076 struct au_cpup_simple_args
*a
= args
;
1078 au_pin_hdir_acquire_nest(a
->cpg
->pin
);
1079 *a
->errp
= au_cpup_simple(a
->cpg
);
1080 au_pin_hdir_release(a
->cpg
->pin
);
1083 static int au_do_sio_cpup_simple(struct au_cp_generic
*cpg
)
1086 struct dentry
*dentry
, *parent
;
1087 struct file
*h_file
;
1088 struct inode
*h_dir
;
1090 dentry
= cpg
->dentry
;
1092 if (au_ftest_cpup(cpg
->flags
, HOPEN
)) {
1093 AuDebugOn(cpg
->bsrc
< 0);
1094 h_file
= au_h_open_pre(dentry
, cpg
->bsrc
, /*force_wr*/0);
1095 err
= PTR_ERR(h_file
);
1100 parent
= dget_parent(dentry
);
1101 h_dir
= au_h_iptr(d_inode(parent
), cpg
->bdst
);
1102 if (!au_test_h_perm_sio(h_dir
, MAY_EXEC
| MAY_WRITE
)
1103 && !au_cpup_sio_test(cpg
->pin
, d_inode(dentry
)->i_mode
))
1104 err
= au_cpup_simple(cpg
);
1106 struct au_cpup_simple_args args
= {
1110 wkq_err
= au_wkq_wait(au_call_cpup_simple
, &args
);
1111 if (unlikely(wkq_err
))
1117 au_h_open_post(dentry
, cpg
->bsrc
, h_file
);
1123 int au_sio_cpup_simple(struct au_cp_generic
*cpg
)
1125 aufs_bindex_t bsrc
, bbot
;
1126 struct dentry
*dentry
, *h_dentry
;
1128 if (cpg
->bsrc
< 0) {
1129 dentry
= cpg
->dentry
;
1130 bbot
= au_dbbot(dentry
);
1131 for (bsrc
= cpg
->bdst
+ 1; bsrc
<= bbot
; bsrc
++) {
1132 h_dentry
= au_h_dptr(dentry
, bsrc
);
1134 AuDebugOn(d_is_negative(h_dentry
));
1138 AuDebugOn(bsrc
> bbot
);
1141 AuDebugOn(cpg
->bsrc
<= cpg
->bdst
);
1142 return au_do_sio_cpup_simple(cpg
);
1145 int au_sio_cpdown_simple(struct au_cp_generic
*cpg
)
1147 AuDebugOn(cpg
->bdst
<= cpg
->bsrc
);
1148 return au_do_sio_cpup_simple(cpg
);
1151 /* ---------------------------------------------------------------------- */
1154 * copyup the deleted file for writing.
1156 static int au_do_cpup_wh(struct au_cp_generic
*cpg
, struct dentry
*wh_dentry
,
1160 unsigned int flags_orig
;
1161 aufs_bindex_t bsrc_orig
;
1162 struct au_dinfo
*dinfo
;
1164 struct au_hdentry
*hd
;
1165 struct dentry
*h_dentry
;
1168 dinfo
= au_di(cpg
->dentry
);
1169 AuRwMustWriteLock(&dinfo
->di_rwsem
);
1171 bsrc_orig
= cpg
->bsrc
;
1172 cpg
->bsrc
= dinfo
->di_btop
;
1173 hdst
.hd
= au_hdentry(dinfo
, cpg
->bdst
);
1174 hdst
.h_dentry
= hdst
.hd
->hd_dentry
;
1175 hdst
.hd
->hd_dentry
= wh_dentry
;
1176 dinfo
->di_btop
= cpg
->bdst
;
1178 hsrc
.h_dentry
= NULL
;
1180 hsrc
.hd
= au_hdentry(dinfo
, cpg
->bsrc
);
1181 hsrc
.h_dentry
= hsrc
.hd
->hd_dentry
;
1182 hsrc
.hd
->hd_dentry
= au_hf_top(file
)->f_path
.dentry
;
1184 flags_orig
= cpg
->flags
;
1185 cpg
->flags
= !AuCpup_DTIME
;
1186 err
= au_cpup_single(cpg
, /*h_parent*/NULL
);
1187 cpg
->flags
= flags_orig
;
1190 err
= au_reopen_nondir(file
);
1191 hsrc
.hd
->hd_dentry
= hsrc
.h_dentry
;
1193 hdst
.hd
->hd_dentry
= hdst
.h_dentry
;
1194 dinfo
->di_btop
= cpg
->bsrc
;
1195 cpg
->bsrc
= bsrc_orig
;
1200 static int au_cpup_wh(struct au_cp_generic
*cpg
, struct file
*file
)
1205 struct dentry
*dentry
, *parent
, *h_parent
, *wh_dentry
;
1206 struct au_branch
*br
;
1209 dentry
= cpg
->dentry
;
1211 br
= au_sbr(dentry
->d_sb
, bdst
);
1212 parent
= dget_parent(dentry
);
1213 h_parent
= au_h_dptr(parent
, bdst
);
1214 wh_dentry
= au_whtmp_lkup(h_parent
, br
, &dentry
->d_name
);
1215 err
= PTR_ERR(wh_dentry
);
1216 if (IS_ERR(wh_dentry
))
1219 h_path
.dentry
= h_parent
;
1220 h_path
.mnt
= au_br_mnt(br
);
1221 au_dtime_store(&dt
, parent
, &h_path
);
1222 err
= au_do_cpup_wh(cpg
, wh_dentry
, file
);
1227 h_path
.dentry
= wh_dentry
;
1228 if (!d_is_dir(wh_dentry
)) {
1229 /* no delegation since it is just created */
1230 err
= vfsub_unlink(d_inode(h_parent
), &h_path
,
1231 /*delegated*/NULL
, /*force*/0);
1233 err
= vfsub_rmdir(d_inode(h_parent
), &h_path
);
1234 if (unlikely(err
)) {
1235 AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
1239 au_dtime_revert(&dt
);
1240 au_set_hi_wh(d_inode(dentry
), bdst
, wh_dentry
);
1249 struct au_cpup_wh_args
{
1251 struct au_cp_generic
*cpg
;
1255 static void au_call_cpup_wh(void *args
)
1257 struct au_cpup_wh_args
*a
= args
;
1259 au_pin_hdir_acquire_nest(a
->cpg
->pin
);
1260 *a
->errp
= au_cpup_wh(a
->cpg
, a
->file
);
1261 au_pin_hdir_release(a
->cpg
->pin
);
1264 int au_sio_cpup_wh(struct au_cp_generic
*cpg
, struct file
*file
)
1268 struct dentry
*dentry
, *parent
, *h_orph
, *h_parent
;
1269 struct inode
*dir
, *h_dir
, *h_tmpdir
;
1271 struct au_pin wh_pin
, *pin_orig
;
1273 dentry
= cpg
->dentry
;
1275 parent
= dget_parent(dentry
);
1276 dir
= d_inode(parent
);
1279 h_dir
= au_igrab(au_h_iptr(dir
, bdst
));
1282 if (!h_dir
->i_nlink
) {
1283 wbr
= au_sbr(dentry
->d_sb
, bdst
)->br_wbr
;
1284 h_orph
= wbr
->wbr_orph
;
1286 h_parent
= dget(au_h_dptr(parent
, bdst
));
1287 au_set_h_dptr(parent
, bdst
, dget(h_orph
));
1288 h_tmpdir
= d_inode(h_orph
);
1289 au_set_h_iptr(dir
, bdst
, au_igrab(h_tmpdir
), /*flags*/0);
1291 inode_lock_nested(h_tmpdir
, AuLsc_I_PARENT3
);
1292 /* todo: au_h_open_pre()? */
1294 pin_orig
= cpg
->pin
;
1295 au_pin_init(&wh_pin
, dentry
, bdst
, AuLsc_DI_PARENT
,
1296 AuLsc_I_PARENT3
, cpg
->pin
->udba
, AuPin_DI_LOCKED
);
1300 if (!au_test_h_perm_sio(h_tmpdir
, MAY_EXEC
| MAY_WRITE
)
1301 && !au_cpup_sio_test(cpg
->pin
, d_inode(dentry
)->i_mode
))
1302 err
= au_cpup_wh(cpg
, file
);
1304 struct au_cpup_wh_args args
= {
1309 wkq_err
= au_wkq_wait(au_call_cpup_wh
, &args
);
1310 if (unlikely(wkq_err
))
1315 inode_unlock(h_tmpdir
);
1316 /* todo: au_h_open_post()? */
1317 au_set_h_iptr(dir
, bdst
, au_igrab(h_dir
), /*flags*/0);
1318 au_set_h_dptr(parent
, bdst
, h_parent
);
1319 AuDebugOn(!pin_orig
);
1320 cpg
->pin
= pin_orig
;
1328 /* ---------------------------------------------------------------------- */
1331 * generic routine for both of copy-up and copy-down.
1333 /* cf. revalidate function in file.c */
1334 int au_cp_dirs(struct dentry
*dentry
, aufs_bindex_t bdst
,
1335 int (*cp
)(struct dentry
*dentry
, aufs_bindex_t bdst
,
1337 struct dentry
*h_parent
, void *arg
),
1342 struct dentry
*d
, *parent
, *h_parent
, *real_parent
, *h_dentry
;
1345 parent
= dget_parent(dentry
);
1346 if (IS_ROOT(parent
))
1349 au_pin_init(&pin
, dentry
, bdst
, AuLsc_DI_PARENT2
, AuLsc_I_PARENT2
,
1350 au_opt_udba(dentry
->d_sb
), AuPin_MNT_WRITE
);
1352 /* do not use au_dpage */
1353 real_parent
= parent
;
1356 parent
= dget_parent(dentry
);
1357 h_parent
= au_h_dptr(parent
, bdst
);
1359 goto out
; /* success */
1361 /* find top dir which is necessary to cpup */
1365 parent
= dget_parent(d
);
1366 di_read_lock_parent3(parent
, !AuLock_IR
);
1367 h_parent
= au_h_dptr(parent
, bdst
);
1368 di_read_unlock(parent
, !AuLock_IR
);
1369 } while (!h_parent
);
1371 if (d
!= real_parent
)
1372 di_write_lock_child3(d
);
1374 /* somebody else might create while we were sleeping */
1375 h_dentry
= au_h_dptr(d
, bdst
);
1376 if (!h_dentry
|| d_is_negative(h_dentry
)) {
1380 au_pin_set_dentry(&pin
, d
);
1381 err
= au_do_pin(&pin
);
1383 err
= cp(d
, bdst
, &pin
, h_parent
, arg
);
1388 if (d
!= real_parent
)
1399 static int au_cpup_dir(struct dentry
*dentry
, aufs_bindex_t bdst
,
1401 struct dentry
*h_parent __maybe_unused
,
1402 void *arg __maybe_unused
)
1404 struct au_cp_generic cpg
= {
1410 .flags
= AuCpup_DTIME
1412 return au_sio_cpup_simple(&cpg
);
1415 int au_cpup_dirs(struct dentry
*dentry
, aufs_bindex_t bdst
)
1417 return au_cp_dirs(dentry
, bdst
, au_cpup_dir
, NULL
);
1420 int au_test_and_cpup_dirs(struct dentry
*dentry
, aufs_bindex_t bdst
)
1423 struct dentry
*parent
;
1426 parent
= dget_parent(dentry
);
1427 dir
= d_inode(parent
);
1429 if (au_h_iptr(dir
, bdst
))
1432 di_read_unlock(parent
, AuLock_IR
);
1433 di_write_lock_parent(parent
);
1434 /* someone else might change our inode while we were sleeping */
1435 if (!au_h_iptr(dir
, bdst
))
1436 err
= au_cpup_dirs(dentry
, bdst
);
1437 di_downgrade_lock(parent
, AuLock_IR
);