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
);
369 * to support a sparse file which is opened with O_APPEND,
370 * we need to close the file.
372 static int au_cp_regular(struct au_cp_generic
*cpg
)
377 aufs_bindex_t bindex
;
379 struct dentry
*dentry
;
386 .flags
= O_RDONLY
| O_NOATIME
| O_LARGEFILE
,
391 .flags
= O_WRONLY
| O_NOATIME
| O_LARGEFILE
,
392 .force_wr
= !!au_ftest_cpup(cpg
->flags
, RWDST
),
396 struct super_block
*sb
;
397 struct inode
*h_src_inode
;
398 struct task_struct
*tsk
= current
;
400 /* bsrc branch can be ro/rw. */
401 sb
= cpg
->dentry
->d_sb
;
403 for (i
= 0; i
< 2; i
++, f
++) {
404 f
->dentry
= au_h_dptr(cpg
->dentry
, f
->bindex
);
405 f
->file
= au_h_open(cpg
->dentry
, f
->bindex
, f
->flags
,
406 /*file*/NULL
, f
->force_wr
);
407 err
= PTR_ERR(f
->file
);
412 /* try stopping to update while we copyup */
413 h_src_inode
= d_inode(file
[SRC
].dentry
);
414 if (!au_test_nfs(h_src_inode
->i_sb
))
415 IMustLock(h_src_inode
);
416 err
= au_copy_file(file
[DST
].file
, file
[SRC
].file
, cpg
->len
);
418 /* i wonder if we had O_NO_DELAY_FPUT flag */
419 if (tsk
->flags
& PF_KTHREAD
)
420 __fput_sync(file
[DST
].file
);
422 WARN(1, "%pD\nPlease report this warning to aufs-users ML",
424 fput(file
[DST
].file
);
427 * we have to call both since we don't know which place the file
431 flush_delayed_fput();
433 au_sbr_put(sb
, file
[DST
].bindex
);
436 fput(file
[SRC
].file
);
437 au_sbr_put(sb
, file
[SRC
].bindex
);
442 static int au_do_cpup_regular(struct au_cp_generic
*cpg
,
443 struct au_cpup_reg_attr
*h_src_attr
)
448 struct inode
*h_src_inode
, *h_dst_inode
;
451 h_src_inode
= au_h_iptr(d_inode(cpg
->dentry
), cpg
->bsrc
);
452 l
= i_size_read(h_src_inode
);
453 if (cpg
->len
== -1 || l
< cpg
->len
)
456 /* try stopping to update while we are referencing */
457 inode_lock_nested(h_src_inode
, AuLsc_I_CHILD
);
458 au_pin_hdir_unlock(cpg
->pin
);
460 h_path
.dentry
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
461 h_path
.mnt
= au_sbr_mnt(cpg
->dentry
->d_sb
, cpg
->bsrc
);
462 h_src_attr
->iflags
= h_src_inode
->i_flags
;
463 if (!au_test_nfs(h_src_inode
->i_sb
))
464 err
= vfs_getattr(&h_path
, &h_src_attr
->st
);
466 inode_unlock(h_src_inode
);
467 err
= vfs_getattr(&h_path
, &h_src_attr
->st
);
468 inode_lock_nested(h_src_inode
, AuLsc_I_CHILD
);
471 inode_unlock(h_src_inode
);
474 h_src_attr
->valid
= 1;
475 if (!au_test_nfs(h_src_inode
->i_sb
)) {
476 err
= au_cp_regular(cpg
);
477 inode_unlock(h_src_inode
);
479 inode_unlock(h_src_inode
);
480 err
= au_cp_regular(cpg
);
482 rerr
= au_pin_hdir_relock(cpg
->pin
);
486 if (!err
&& (h_src_inode
->i_state
& I_LINKABLE
)) {
487 h_path
.dentry
= au_h_dptr(cpg
->dentry
, cpg
->bdst
);
488 h_dst_inode
= d_inode(h_path
.dentry
);
489 spin_lock(&h_dst_inode
->i_lock
);
490 h_dst_inode
->i_state
|= I_LINKABLE
;
491 spin_unlock(&h_dst_inode
->i_lock
);
498 static int au_do_cpup_symlink(struct path
*h_path
, struct dentry
*h_src
,
507 struct inode
*h_inode
= d_inode(h_src
);
508 const struct inode_operations
*h_iop
= h_inode
->i_op
;
511 if (unlikely(!h_iop
->readlink
))
515 sym
.k
= (void *)__get_free_page(GFP_NOFS
);
516 if (unlikely(!sym
.k
))
519 /* unnecessary to support mmap_sem since symlink is not mmap-able */
522 symlen
= h_iop
->readlink(h_src
, sym
.u
, PATH_MAX
);
528 err
= vfsub_symlink(h_dir
, h_path
, sym
.k
);
530 au_delayed_free_page((unsigned long)sym
.k
);
537 * regardless 'acl' option, reset all ACL.
538 * All ACL will be copied up later from the original entry on the lower branch.
540 static int au_reset_acl(struct inode
*h_dir
, struct path
*h_path
, umode_t mode
)
543 struct dentry
*h_dentry
;
544 struct inode
*h_inode
;
546 h_dentry
= h_path
->dentry
;
547 h_inode
= d_inode(h_dentry
);
548 /* forget_all_cached_acls(h_inode)); */
549 err
= vfsub_removexattr(h_dentry
, XATTR_NAME_POSIX_ACL_ACCESS
);
551 if (err
== -EOPNOTSUPP
)
554 err
= vfsub_acl_chmod(h_inode
, mode
);
560 static int au_do_cpup_dir(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
,
561 struct inode
*h_dir
, struct path
*h_path
)
564 struct inode
*dir
, *inode
;
566 err
= vfsub_removexattr(h_path
->dentry
, XATTR_NAME_POSIX_ACL_DEFAULT
);
568 if (err
== -EOPNOTSUPP
)
574 * strange behaviour from the users view,
575 * particularry setattr case
577 dir
= d_inode(dst_parent
);
578 if (au_ibtop(dir
) == cpg
->bdst
)
579 au_cpup_attr_nlink(dir
, /*force*/1);
580 inode
= d_inode(cpg
->dentry
);
581 au_cpup_attr_nlink(inode
, /*force*/1);
587 static noinline_for_stack
588 int cpup_entry(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
,
589 struct au_cpup_reg_attr
*h_src_attr
)
593 unsigned int mnt_flags
;
594 unsigned char isdir
, isreg
, force
;
595 const unsigned char do_dt
= !!au_ftest_cpup(cpg
->flags
, DTIME
);
598 struct dentry
*h_src
, *h_dst
, *h_parent
;
599 struct inode
*h_inode
, *h_dir
;
600 struct super_block
*sb
;
602 /* bsrc branch can be ro/rw. */
603 h_src
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
604 h_inode
= d_inode(h_src
);
605 AuDebugOn(h_inode
!= au_h_iptr(d_inode(cpg
->dentry
), cpg
->bsrc
));
607 /* try stopping to be referenced while we are creating */
608 h_dst
= au_h_dptr(cpg
->dentry
, cpg
->bdst
);
609 if (au_ftest_cpup(cpg
->flags
, RENAME
))
610 AuDebugOn(strncmp(h_dst
->d_name
.name
, AUFS_WH_PFX
,
612 h_parent
= h_dst
->d_parent
; /* dir inode is locked */
613 h_dir
= d_inode(h_parent
);
615 AuDebugOn(h_parent
!= h_dst
->d_parent
);
617 sb
= cpg
->dentry
->d_sb
;
618 h_path
.mnt
= au_sbr_mnt(sb
, cpg
->bdst
);
620 h_path
.dentry
= h_parent
;
621 au_dtime_store(&dt
, dst_parent
, &h_path
);
623 h_path
.dentry
= h_dst
;
627 mode
= h_inode
->i_mode
;
628 switch (mode
& S_IFMT
) {
631 err
= vfsub_create(h_dir
, &h_path
, S_IRUSR
| S_IWUSR
,
634 err
= au_do_cpup_regular(cpg
, h_src_attr
);
638 err
= vfsub_mkdir(h_dir
, &h_path
, mode
);
640 err
= au_do_cpup_dir(cpg
, dst_parent
, h_dir
, &h_path
);
643 err
= au_do_cpup_symlink(&h_path
, h_src
, h_dir
);
647 AuDebugOn(!capable(CAP_MKNOD
));
651 err
= vfsub_mknod(h_dir
, &h_path
, mode
, h_inode
->i_rdev
);
654 AuIOErr("Unknown inode type 0%o\n", mode
);
658 err
= au_reset_acl(h_dir
, &h_path
, mode
);
660 mnt_flags
= au_mntflags(sb
);
661 if (!au_opt_test(mnt_flags
, UDBA_NONE
)
663 && au_opt_test(mnt_flags
, XINO
)
664 && (h_inode
->i_nlink
== 1
665 || (h_inode
->i_state
& I_LINKABLE
))
666 /* todo: unnecessary? */
667 /* && d_inode(cpg->dentry)->i_nlink == 1 */
668 && cpg
->bdst
< cpg
->bsrc
669 && !au_ftest_cpup(cpg
->flags
, KEEPLINO
))
670 au_xino_write(sb
, cpg
->bsrc
, h_inode
->i_ino
, /*ino*/0);
671 /* ignore this error */
678 force
= !!i_size_read(h_inode
);
680 au_fhsm_wrote(sb
, cpg
->bdst
, force
);
684 au_dtime_revert(&dt
);
688 static int au_do_ren_after_cpup(struct au_cp_generic
*cpg
, struct path
*h_path
)
691 struct dentry
*dentry
, *h_dentry
, *h_parent
, *parent
;
695 dentry
= cpg
->dentry
;
697 h_dentry
= au_h_dptr(dentry
, bdst
);
698 if (!au_ftest_cpup(cpg
->flags
, OVERWRITE
)) {
700 au_set_h_dptr(dentry
, bdst
, NULL
);
701 err
= au_lkup_neg(dentry
, bdst
, /*wh*/0);
703 h_path
->dentry
= dget(au_h_dptr(dentry
, bdst
));
704 au_set_h_dptr(dentry
, bdst
, h_dentry
);
707 parent
= dget_parent(dentry
);
708 h_parent
= au_h_dptr(parent
, bdst
);
710 h_path
->dentry
= vfsub_lkup_one(&dentry
->d_name
, h_parent
);
711 if (IS_ERR(h_path
->dentry
))
712 err
= PTR_ERR(h_path
->dentry
);
717 h_parent
= h_dentry
->d_parent
; /* dir inode is locked */
718 h_dir
= d_inode(h_parent
);
720 AuDbg("%pd %pd\n", h_dentry
, h_path
->dentry
);
721 /* no delegation since it is just created */
722 err
= vfsub_rename(h_dir
, h_dentry
, h_dir
, h_path
, /*delegated*/NULL
);
723 dput(h_path
->dentry
);
730 * copyup the @dentry from @bsrc to @bdst.
731 * the caller must set the both of lower dentries.
732 * @len is for truncating when it is -1 copyup the entire file.
733 * in link/rename cases, @dst_parent may be different from the real one.
734 * basic->bsrc can be larger than basic->bdst.
736 static int au_cpup_single(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
)
739 aufs_bindex_t old_ibtop
;
740 unsigned char isdir
, plink
;
741 struct dentry
*h_src
, *h_dst
, *h_parent
;
742 struct inode
*dst_inode
, *h_dir
, *inode
, *delegated
, *src_inode
;
743 struct super_block
*sb
;
744 struct au_branch
*br
;
745 /* to reuduce stack size */
749 struct au_cpup_reg_attr h_src_attr
;
753 a
= kmalloc(sizeof(*a
), GFP_NOFS
);
756 a
->h_src_attr
.valid
= 0;
758 sb
= cpg
->dentry
->d_sb
;
759 br
= au_sbr(sb
, cpg
->bdst
);
760 a
->h_path
.mnt
= au_br_mnt(br
);
761 h_dst
= au_h_dptr(cpg
->dentry
, cpg
->bdst
);
762 h_parent
= h_dst
->d_parent
; /* dir inode is locked */
763 h_dir
= d_inode(h_parent
);
766 h_src
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
767 inode
= d_inode(cpg
->dentry
);
770 dst_parent
= dget_parent(cpg
->dentry
);
774 plink
= !!au_opt_test(au_mntflags(sb
), PLINK
);
775 dst_inode
= au_h_iptr(inode
, cpg
->bdst
);
777 if (unlikely(!plink
)) {
779 AuIOErr("hi%lu(i%lu) exists on b%d "
780 "but plink is disabled\n",
781 dst_inode
->i_ino
, inode
->i_ino
, cpg
->bdst
);
785 if (dst_inode
->i_nlink
) {
786 const int do_dt
= au_ftest_cpup(cpg
->flags
, DTIME
);
788 h_src
= au_plink_lkup(inode
, cpg
->bdst
);
789 err
= PTR_ERR(h_src
);
792 if (unlikely(d_is_negative(h_src
))) {
794 AuIOErr("i%lu exists on b%d "
795 "but not pseudo-linked\n",
796 inode
->i_ino
, cpg
->bdst
);
802 a
->h_path
.dentry
= h_parent
;
803 au_dtime_store(&a
->dt
, dst_parent
, &a
->h_path
);
806 a
->h_path
.dentry
= h_dst
;
808 err
= vfsub_link(h_src
, h_dir
, &a
->h_path
, &delegated
);
809 if (!err
&& au_ftest_cpup(cpg
->flags
, RENAME
))
810 err
= au_do_ren_after_cpup(cpg
, &a
->h_path
);
812 au_dtime_revert(&a
->dt
);
813 if (unlikely(err
== -EWOULDBLOCK
)) {
814 pr_warn("cannot retry for NFSv4 delegation"
815 " for an internal link\n");
821 /* todo: cpup_wh_file? */
823 au_update_ibrange(inode
, /*do_put_zero*/1);
826 isdir
= S_ISDIR(inode
->i_mode
);
827 old_ibtop
= au_ibtop(inode
);
828 err
= cpup_entry(cpg
, dst_parent
, &a
->h_src_attr
);
831 dst_inode
= d_inode(h_dst
);
832 inode_lock_nested(dst_inode
, AuLsc_I_CHILD2
);
833 /* todo: necessary? */
834 /* au_pin_hdir_unlock(cpg->pin); */
836 err
= cpup_iattr(cpg
->dentry
, cpg
->bdst
, h_src
, &a
->h_src_attr
);
838 /* todo: necessary? */
839 /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
840 inode_unlock(dst_inode
);
844 if (cpg
->bdst
< old_ibtop
) {
845 if (S_ISREG(inode
->i_mode
)) {
846 err
= au_dy_iaop(inode
, cpg
->bdst
, dst_inode
);
848 /* ignore an error */
849 /* au_pin_hdir_relock(cpg->pin); */
850 inode_unlock(dst_inode
);
854 au_set_ibtop(inode
, cpg
->bdst
);
856 au_set_ibbot(inode
, cpg
->bdst
);
857 au_set_h_iptr(inode
, cpg
->bdst
, au_igrab(dst_inode
),
858 au_hi_flags(inode
, isdir
));
860 /* todo: necessary? */
861 /* err = au_pin_hdir_relock(cpg->pin); */
862 inode_unlock(dst_inode
);
866 src_inode
= d_inode(h_src
);
868 && (src_inode
->i_nlink
> 1
869 || src_inode
->i_state
& I_LINKABLE
)
871 au_plink_append(inode
, cpg
->bdst
, h_dst
);
873 if (au_ftest_cpup(cpg
->flags
, RENAME
)) {
874 a
->h_path
.dentry
= h_dst
;
875 err
= au_do_ren_after_cpup(cpg
, &a
->h_path
);
878 goto out_parent
; /* success */
882 a
->h_path
.dentry
= h_parent
;
883 au_dtime_store(&a
->dt
, dst_parent
, &a
->h_path
);
884 a
->h_path
.dentry
= h_dst
;
886 if (d_is_positive(h_dst
)) {
888 /* no delegation since it is just created */
889 rerr
= vfsub_unlink(h_dir
, &a
->h_path
,
890 /*delegated*/NULL
, /*force*/0);
892 rerr
= vfsub_rmdir(h_dir
, &a
->h_path
);
894 au_dtime_revert(&a
->dt
);
896 AuIOErr("failed removing broken entry(%d, %d)\n", err
, rerr
);
907 struct au_cpup_single_args
{
909 struct au_cp_generic
*cpg
;
910 struct dentry
*dst_parent
;
913 static void au_call_cpup_single(void *args
)
915 struct au_cpup_single_args
*a
= args
;
917 au_pin_hdir_acquire_nest(a
->cpg
->pin
);
918 *a
->errp
= au_cpup_single(a
->cpg
, a
->dst_parent
);
919 au_pin_hdir_release(a
->cpg
->pin
);
924 * prevent SIGXFSZ in copy-up.
925 * testing CAP_MKNOD is for generic fs,
926 * but CAP_FSETID is for xfs only, currently.
928 static int au_cpup_sio_test(struct au_pin
*pin
, umode_t mode
)
931 struct super_block
*sb
;
935 sb
= au_pinned_parent(pin
)->d_sb
;
937 && (!au_sbi(sb
)->si_plink_maint_pid
938 || au_plink_maint(sb
, AuLock_NOPLM
))) {
939 switch (mode
& S_IFMT
) {
941 /* no condition about RLIMIT_FSIZE and the file size */
946 do_sio
= !capable(CAP_MKNOD
);
950 do_sio
= ((mode
& (S_ISUID
| S_ISGID
))
951 && !capable(CAP_FSETID
));
952 /* this workaround may be removed in the future */
954 h_dir
= au_pinned_h_dir(pin
);
955 do_sio
= h_dir
->i_mode
& S_ISVTX
;
963 int au_sio_cpup_single(struct au_cp_generic
*cpg
, struct dentry
*dst_parent
)
966 struct dentry
*h_dentry
;
968 h_dentry
= au_h_dptr(cpg
->dentry
, cpg
->bsrc
);
969 if (!au_cpup_sio_test(pin
, d_inode(h_dentry
)->i_mode
))
970 err
= au_cpup_single(cpg
, dst_parent
);
972 struct au_cpup_single_args args
= {
975 .dst_parent
= dst_parent
977 wkq_err
= au_wkq_wait(au_call_cpup_single
, &args
);
978 if (unlikely(wkq_err
))
987 * copyup the @dentry from the first active lower branch to @bdst,
988 * using au_cpup_single().
990 static int au_cpup_simple(struct au_cp_generic
*cpg
)
993 unsigned int flags_orig
;
994 struct dentry
*dentry
;
996 AuDebugOn(cpg
->bsrc
< 0);
998 dentry
= cpg
->dentry
;
999 DiMustWriteLock(dentry
);
1001 err
= au_lkup_neg(dentry
, cpg
->bdst
, /*wh*/1);
1003 flags_orig
= cpg
->flags
;
1004 au_fset_cpup(cpg
->flags
, RENAME
);
1005 err
= au_cpup_single(cpg
, NULL
);
1006 cpg
->flags
= flags_orig
;
1008 return 0; /* success */
1011 au_set_h_dptr(dentry
, cpg
->bdst
, NULL
);
1012 au_set_dbtop(dentry
, cpg
->bsrc
);
1018 struct au_cpup_simple_args
{
1020 struct au_cp_generic
*cpg
;
1023 static void au_call_cpup_simple(void *args
)
1025 struct au_cpup_simple_args
*a
= args
;
1027 au_pin_hdir_acquire_nest(a
->cpg
->pin
);
1028 *a
->errp
= au_cpup_simple(a
->cpg
);
1029 au_pin_hdir_release(a
->cpg
->pin
);
1032 static int au_do_sio_cpup_simple(struct au_cp_generic
*cpg
)
1035 struct dentry
*dentry
, *parent
;
1036 struct file
*h_file
;
1037 struct inode
*h_dir
;
1039 dentry
= cpg
->dentry
;
1041 if (au_ftest_cpup(cpg
->flags
, HOPEN
)) {
1042 AuDebugOn(cpg
->bsrc
< 0);
1043 h_file
= au_h_open_pre(dentry
, cpg
->bsrc
, /*force_wr*/0);
1044 err
= PTR_ERR(h_file
);
1049 parent
= dget_parent(dentry
);
1050 h_dir
= au_h_iptr(d_inode(parent
), cpg
->bdst
);
1051 if (!au_test_h_perm_sio(h_dir
, MAY_EXEC
| MAY_WRITE
)
1052 && !au_cpup_sio_test(cpg
->pin
, d_inode(dentry
)->i_mode
))
1053 err
= au_cpup_simple(cpg
);
1055 struct au_cpup_simple_args args
= {
1059 wkq_err
= au_wkq_wait(au_call_cpup_simple
, &args
);
1060 if (unlikely(wkq_err
))
1066 au_h_open_post(dentry
, cpg
->bsrc
, h_file
);
1072 int au_sio_cpup_simple(struct au_cp_generic
*cpg
)
1074 aufs_bindex_t bsrc
, bbot
;
1075 struct dentry
*dentry
, *h_dentry
;
1077 if (cpg
->bsrc
< 0) {
1078 dentry
= cpg
->dentry
;
1079 bbot
= au_dbbot(dentry
);
1080 for (bsrc
= cpg
->bdst
+ 1; bsrc
<= bbot
; bsrc
++) {
1081 h_dentry
= au_h_dptr(dentry
, bsrc
);
1083 AuDebugOn(d_is_negative(h_dentry
));
1087 AuDebugOn(bsrc
> bbot
);
1090 AuDebugOn(cpg
->bsrc
<= cpg
->bdst
);
1091 return au_do_sio_cpup_simple(cpg
);
1094 int au_sio_cpdown_simple(struct au_cp_generic
*cpg
)
1096 AuDebugOn(cpg
->bdst
<= cpg
->bsrc
);
1097 return au_do_sio_cpup_simple(cpg
);
1100 /* ---------------------------------------------------------------------- */
1103 * copyup the deleted file for writing.
1105 static int au_do_cpup_wh(struct au_cp_generic
*cpg
, struct dentry
*wh_dentry
,
1109 unsigned int flags_orig
;
1110 aufs_bindex_t bsrc_orig
;
1111 struct au_dinfo
*dinfo
;
1113 struct au_hdentry
*hd
;
1114 struct dentry
*h_dentry
;
1117 dinfo
= au_di(cpg
->dentry
);
1118 AuRwMustWriteLock(&dinfo
->di_rwsem
);
1120 bsrc_orig
= cpg
->bsrc
;
1121 cpg
->bsrc
= dinfo
->di_btop
;
1122 hdst
.hd
= au_hdentry(dinfo
, cpg
->bdst
);
1123 hdst
.h_dentry
= hdst
.hd
->hd_dentry
;
1124 hdst
.hd
->hd_dentry
= wh_dentry
;
1125 dinfo
->di_btop
= cpg
->bdst
;
1127 hsrc
.h_dentry
= NULL
;
1129 hsrc
.hd
= au_hdentry(dinfo
, cpg
->bsrc
);
1130 hsrc
.h_dentry
= hsrc
.hd
->hd_dentry
;
1131 hsrc
.hd
->hd_dentry
= au_hf_top(file
)->f_path
.dentry
;
1133 flags_orig
= cpg
->flags
;
1134 cpg
->flags
= !AuCpup_DTIME
;
1135 err
= au_cpup_single(cpg
, /*h_parent*/NULL
);
1136 cpg
->flags
= flags_orig
;
1139 err
= au_reopen_nondir(file
);
1140 hsrc
.hd
->hd_dentry
= hsrc
.h_dentry
;
1142 hdst
.hd
->hd_dentry
= hdst
.h_dentry
;
1143 dinfo
->di_btop
= cpg
->bsrc
;
1144 cpg
->bsrc
= bsrc_orig
;
1149 static int au_cpup_wh(struct au_cp_generic
*cpg
, struct file
*file
)
1154 struct dentry
*dentry
, *parent
, *h_parent
, *wh_dentry
;
1155 struct au_branch
*br
;
1158 dentry
= cpg
->dentry
;
1160 br
= au_sbr(dentry
->d_sb
, bdst
);
1161 parent
= dget_parent(dentry
);
1162 h_parent
= au_h_dptr(parent
, bdst
);
1163 wh_dentry
= au_whtmp_lkup(h_parent
, br
, &dentry
->d_name
);
1164 err
= PTR_ERR(wh_dentry
);
1165 if (IS_ERR(wh_dentry
))
1168 h_path
.dentry
= h_parent
;
1169 h_path
.mnt
= au_br_mnt(br
);
1170 au_dtime_store(&dt
, parent
, &h_path
);
1171 err
= au_do_cpup_wh(cpg
, wh_dentry
, file
);
1176 h_path
.dentry
= wh_dentry
;
1177 if (!d_is_dir(wh_dentry
)) {
1178 /* no delegation since it is just created */
1179 err
= vfsub_unlink(d_inode(h_parent
), &h_path
,
1180 /*delegated*/NULL
, /*force*/0);
1182 err
= vfsub_rmdir(d_inode(h_parent
), &h_path
);
1183 if (unlikely(err
)) {
1184 AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
1188 au_dtime_revert(&dt
);
1189 au_set_hi_wh(d_inode(dentry
), bdst
, wh_dentry
);
1198 struct au_cpup_wh_args
{
1200 struct au_cp_generic
*cpg
;
1204 static void au_call_cpup_wh(void *args
)
1206 struct au_cpup_wh_args
*a
= args
;
1208 au_pin_hdir_acquire_nest(a
->cpg
->pin
);
1209 *a
->errp
= au_cpup_wh(a
->cpg
, a
->file
);
1210 au_pin_hdir_release(a
->cpg
->pin
);
1213 int au_sio_cpup_wh(struct au_cp_generic
*cpg
, struct file
*file
)
1217 struct dentry
*dentry
, *parent
, *h_orph
, *h_parent
;
1218 struct inode
*dir
, *h_dir
, *h_tmpdir
;
1220 struct au_pin wh_pin
, *pin_orig
;
1222 dentry
= cpg
->dentry
;
1224 parent
= dget_parent(dentry
);
1225 dir
= d_inode(parent
);
1228 h_dir
= au_igrab(au_h_iptr(dir
, bdst
));
1231 if (!h_dir
->i_nlink
) {
1232 wbr
= au_sbr(dentry
->d_sb
, bdst
)->br_wbr
;
1233 h_orph
= wbr
->wbr_orph
;
1235 h_parent
= dget(au_h_dptr(parent
, bdst
));
1236 au_set_h_dptr(parent
, bdst
, dget(h_orph
));
1237 h_tmpdir
= d_inode(h_orph
);
1238 au_set_h_iptr(dir
, bdst
, au_igrab(h_tmpdir
), /*flags*/0);
1240 inode_lock_nested(h_tmpdir
, AuLsc_I_PARENT3
);
1241 /* todo: au_h_open_pre()? */
1243 pin_orig
= cpg
->pin
;
1244 au_pin_init(&wh_pin
, dentry
, bdst
, AuLsc_DI_PARENT
,
1245 AuLsc_I_PARENT3
, cpg
->pin
->udba
, AuPin_DI_LOCKED
);
1249 if (!au_test_h_perm_sio(h_tmpdir
, MAY_EXEC
| MAY_WRITE
)
1250 && !au_cpup_sio_test(cpg
->pin
, d_inode(dentry
)->i_mode
))
1251 err
= au_cpup_wh(cpg
, file
);
1253 struct au_cpup_wh_args args
= {
1258 wkq_err
= au_wkq_wait(au_call_cpup_wh
, &args
);
1259 if (unlikely(wkq_err
))
1264 inode_unlock(h_tmpdir
);
1265 /* todo: au_h_open_post()? */
1266 au_set_h_iptr(dir
, bdst
, au_igrab(h_dir
), /*flags*/0);
1267 au_set_h_dptr(parent
, bdst
, h_parent
);
1268 AuDebugOn(!pin_orig
);
1269 cpg
->pin
= pin_orig
;
1277 /* ---------------------------------------------------------------------- */
1280 * generic routine for both of copy-up and copy-down.
1282 /* cf. revalidate function in file.c */
1283 int au_cp_dirs(struct dentry
*dentry
, aufs_bindex_t bdst
,
1284 int (*cp
)(struct dentry
*dentry
, aufs_bindex_t bdst
,
1286 struct dentry
*h_parent
, void *arg
),
1291 struct dentry
*d
, *parent
, *h_parent
, *real_parent
, *h_dentry
;
1294 parent
= dget_parent(dentry
);
1295 if (IS_ROOT(parent
))
1298 au_pin_init(&pin
, dentry
, bdst
, AuLsc_DI_PARENT2
, AuLsc_I_PARENT2
,
1299 au_opt_udba(dentry
->d_sb
), AuPin_MNT_WRITE
);
1301 /* do not use au_dpage */
1302 real_parent
= parent
;
1305 parent
= dget_parent(dentry
);
1306 h_parent
= au_h_dptr(parent
, bdst
);
1308 goto out
; /* success */
1310 /* find top dir which is necessary to cpup */
1314 parent
= dget_parent(d
);
1315 di_read_lock_parent3(parent
, !AuLock_IR
);
1316 h_parent
= au_h_dptr(parent
, bdst
);
1317 di_read_unlock(parent
, !AuLock_IR
);
1318 } while (!h_parent
);
1320 if (d
!= real_parent
)
1321 di_write_lock_child3(d
);
1323 /* somebody else might create while we were sleeping */
1324 h_dentry
= au_h_dptr(d
, bdst
);
1325 if (!h_dentry
|| d_is_negative(h_dentry
)) {
1329 au_pin_set_dentry(&pin
, d
);
1330 err
= au_do_pin(&pin
);
1332 err
= cp(d
, bdst
, &pin
, h_parent
, arg
);
1337 if (d
!= real_parent
)
1348 static int au_cpup_dir(struct dentry
*dentry
, aufs_bindex_t bdst
,
1350 struct dentry
*h_parent __maybe_unused
,
1351 void *arg __maybe_unused
)
1353 struct au_cp_generic cpg
= {
1359 .flags
= AuCpup_DTIME
1361 return au_sio_cpup_simple(&cpg
);
1364 int au_cpup_dirs(struct dentry
*dentry
, aufs_bindex_t bdst
)
1366 return au_cp_dirs(dentry
, bdst
, au_cpup_dir
, NULL
);
1369 int au_test_and_cpup_dirs(struct dentry
*dentry
, aufs_bindex_t bdst
)
1372 struct dentry
*parent
;
1375 parent
= dget_parent(dentry
);
1376 dir
= d_inode(parent
);
1378 if (au_h_iptr(dir
, bdst
))
1381 di_read_unlock(parent
, AuLock_IR
);
1382 di_write_lock_parent(parent
);
1383 /* someone else might change our inode while we were sleeping */
1384 if (!au_h_iptr(dir
, bdst
))
1385 err
= au_cpup_dirs(dentry
, bdst
);
1386 di_downgrade_lock(parent
, AuLock_IR
);