2 * Copyright (C) 2005-2013 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * copy-up functions, see wbr_policy.c for copy-down
23 #include <linux/fs_stack.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_ibstart(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, bend;
55 bindex = au_ibstart(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))
63 set_nlink(inode, h_inode->i_nlink);
66 * fewer nlink makes find(1) noisy, but larger nlink doesn't.
67 * it may includes whplink directory.
69 if (S_ISDIR(h_inode->i_mode)) {
70 bend = au_ibend(inode);
71 for (bindex++; bindex <= bend; bindex++) {
72 h_inode = au_h_iptr(inode, bindex);
74 au_add_nlink(inode, h_inode);
79 void au_cpup_attr_changeable(struct inode *inode)
81 struct inode *h_inode;
83 h_inode = au_h_iptr(inode, au_ibstart(inode));
84 inode->i_mode = h_inode->i_mode;
85 inode->i_uid = h_inode->i_uid;
86 inode->i_gid = h_inode->i_gid;
87 au_cpup_attr_timesizes(inode);
88 au_cpup_attr_flags(inode, h_inode->i_flags);
91 void au_cpup_igen(struct inode *inode, struct inode *h_inode)
93 struct au_iinfo *iinfo = au_ii(inode);
95 IiMustWriteLock(inode);
97 iinfo->ii_higen = h_inode->i_generation;
98 iinfo->ii_hsb1 = h_inode->i_sb;
101 void au_cpup_attr_all(struct inode *inode, int force)
103 struct inode *h_inode;
105 h_inode = au_h_iptr(inode, au_ibstart(inode));
106 au_cpup_attr_changeable(inode);
107 if (inode->i_nlink > 0)
108 au_cpup_attr_nlink(inode, force);
109 inode->i_rdev = h_inode->i_rdev;
110 inode->i_blkbits = h_inode->i_blkbits;
111 au_cpup_igen(inode, h_inode);
114 /* ---------------------------------------------------------------------- */
116 /* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
118 /* keep the timestamps of the parent dir when cpup */
119 void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
122 struct inode *h_inode;
124 dt->dt_dentry = dentry;
125 dt->dt_h_path = *h_path;
126 h_inode = h_path->dentry->d_inode;
127 dt->dt_atime = h_inode->i_atime;
128 dt->dt_mtime = h_inode->i_mtime;
132 void au_dtime_revert(struct au_dtime *dt)
137 attr.ia_atime = dt->dt_atime;
138 attr.ia_mtime = dt->dt_mtime;
139 attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
140 | ATTR_ATIME | ATTR_ATIME_SET;
142 err = vfsub_notify_change(&dt->dt_h_path, &attr);
144 pr_warn("restoring timestamps failed(%d). ignored\n", err);
147 /* ---------------------------------------------------------------------- */
149 /* internal use only */
150 struct au_cpup_reg_attr {
153 unsigned int iflags; /* inode->i_flags */
156 static noinline_for_stack
157 int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
158 struct au_cpup_reg_attr *h_src_attr)
163 struct inode *h_isrc, *h_idst;
166 h_path.dentry = au_h_dptr(dst, bindex);
167 h_idst = h_path.dentry->d_inode;
168 h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
169 h_isrc = h_src->d_inode;
170 ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
171 | ATTR_ATIME | ATTR_MTIME
172 | ATTR_ATIME_SET | ATTR_MTIME_SET;
173 if (h_src_attr && h_src_attr->valid) {
174 h_st = &h_src_attr->st;
175 ia.ia_uid = h_st->uid;
176 ia.ia_gid = h_st->gid;
177 ia.ia_atime = h_st->atime;
178 ia.ia_mtime = h_st->mtime;
179 if (h_idst->i_mode != h_st->mode
180 && !S_ISLNK(h_idst->i_mode)) {
181 ia.ia_valid |= ATTR_MODE;
182 ia.ia_mode = h_st->mode;
184 sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
185 au_cpup_attr_flags(h_idst, h_src_attr->iflags);
187 ia.ia_uid = h_isrc->i_uid;
188 ia.ia_gid = h_isrc->i_gid;
189 ia.ia_atime = h_isrc->i_atime;
190 ia.ia_mtime = h_isrc->i_mtime;
191 if (h_idst->i_mode != h_isrc->i_mode
192 && !S_ISLNK(h_idst->i_mode)) {
193 ia.ia_valid |= ATTR_MODE;
194 ia.ia_mode = h_isrc->i_mode;
196 sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
197 au_cpup_attr_flags(h_idst, h_isrc->i_flags);
199 err = vfsub_notify_change(&h_path, &ia);
201 /* is this nfs only? */
202 if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
203 ia.ia_valid = ATTR_FORCE | ATTR_MODE;
204 ia.ia_mode = h_isrc->i_mode;
205 err = vfsub_notify_change(&h_path, &ia);
211 /* ---------------------------------------------------------------------- */
213 static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
214 char *buf, unsigned long blksize)
217 size_t sz, rbytes, wbytes;
218 unsigned char all_zero;
221 /* reduce stack usage */
224 zp = page_address(ZERO_PAGE(0));
226 return -ENOMEM; /* possible? */
231 AuDbg("len %lld\n", len);
237 /* todo: signal_pending? */
238 while (!rbytes || err == -EAGAIN || err == -EINTR) {
239 rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
242 if (unlikely(err < 0))
246 if (len >= rbytes && rbytes == blksize)
247 all_zero = !memcmp(buf, zp, rbytes);
254 b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
256 /* todo: signal_pending? */
257 if (unlikely(err == -EAGAIN || err == -EINTR))
259 if (unlikely(err < 0))
264 if (unlikely(err < 0))
270 res = vfsub_llseek(dst, rbytes, SEEK_CUR);
272 if (unlikely(res < 0))
279 /* the last block may be a hole */
280 if (!err && all_zero) {
284 if (au_test_nfs(dst->f_dentry->d_sb)) {
285 /* nfs requires this step to make last hole */
286 /* is this only nfs? */
288 /* todo: signal_pending? */
289 err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
290 } while (err == -EAGAIN || err == -EINTR);
297 ia->ia_size = dst->f_pos;
298 ia->ia_valid = ATTR_SIZE | ATTR_FILE;
300 h_mtx = &dst->f_dentry->d_inode->i_mutex;
301 mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
302 err = vfsub_notify_change(&dst->f_path, ia);
310 int au_copy_file(struct file *dst, struct file *src, loff_t len)
313 unsigned long blksize;
314 unsigned char do_kfree;
318 blksize = dst->f_dentry->d_sb->s_blocksize;
319 if (!blksize || PAGE_SIZE < blksize)
321 AuDbg("blksize %lu\n", blksize);
322 do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
324 buf = kmalloc(blksize, GFP_NOFS);
326 buf = (void *)__get_free_page(GFP_NOFS);
331 AuDbg("copying a large file %lld\n", (long long)len);
335 err = au_do_copy_file(dst, src, len, buf, blksize);
339 free_page((unsigned long)buf);
346 * to support a sparse file which is opened with O_APPEND,
347 * we need to close the file.
349 static int au_cp_regular(struct au_cp_generic *cpg)
354 aufs_bindex_t bindex;
356 struct dentry *dentry;
358 void *label, *label_file;
362 .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
365 .label_file = &&out_src
369 .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
372 .label_file = &&out_dst
375 struct super_block *sb;
377 /* bsrc branch can be ro/rw. */
378 sb = cpg->dentry->d_sb;
380 for (i = 0; i < 2; i++, f++) {
381 f->dentry = au_h_dptr(cpg->dentry, f->bindex);
382 f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
384 err = PTR_ERR(f->file);
388 if (unlikely(!f->file->f_op))
392 /* try stopping to update while we copyup */
393 IMustLock(file[SRC].dentry->d_inode);
394 err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
397 fput(file[DST].file);
398 au_sbr_put(sb, file[DST].bindex);
400 fput(file[SRC].file);
401 au_sbr_put(sb, file[SRC].bindex);
406 static int au_do_cpup_regular(struct au_cp_generic *cpg,
407 struct au_cpup_reg_attr *h_src_attr)
411 struct dentry *h_src_dentry;
412 struct inode *h_src_inode;
413 struct vfsmount *h_src_mnt;
416 h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
417 l = i_size_read(h_src_inode);
418 if (cpg->len == -1 || l < cpg->len)
421 /* try stopping to update while we are referencing */
422 mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
423 au_pin_hdir_unlock(cpg->pin);
425 h_src_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
426 h_src_mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
427 h_src_attr->iflags = h_src_inode->i_flags;
428 err = vfs_getattr(h_src_mnt, h_src_dentry, &h_src_attr->st);
430 mutex_unlock(&h_src_inode->i_mutex);
433 h_src_attr->valid = 1;
434 err = au_cp_regular(cpg);
435 mutex_unlock(&h_src_inode->i_mutex);
436 rerr = au_pin_hdir_relock(cpg->pin);
445 static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
456 if (unlikely(!h_src->d_inode->i_op->readlink))
460 sym.k = __getname_gfp(GFP_NOFS);
461 if (unlikely(!sym.k))
464 /* unnecessary to support mmap_sem since symlink is not mmap-able */
467 symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
473 err = vfsub_symlink(h_dir, h_path, sym.k);
481 static noinline_for_stack
482 int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
483 struct au_cpup_reg_attr *h_src_attr)
487 unsigned int mnt_flags;
489 const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
492 struct dentry *h_src, *h_dst, *h_parent;
493 struct inode *h_inode, *h_dir;
494 struct super_block *sb;
496 /* bsrc branch can be ro/rw. */
497 h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
498 h_inode = h_src->d_inode;
499 AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
501 /* try stopping to be referenced while we are creating */
502 h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
503 if (au_ftest_cpup(cpg->flags, RENAME))
504 AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
506 h_parent = h_dst->d_parent; /* dir inode is locked */
507 h_dir = h_parent->d_inode;
509 AuDebugOn(h_parent != h_dst->d_parent);
511 sb = cpg->dentry->d_sb;
512 h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
514 h_path.dentry = h_parent;
515 au_dtime_store(&dt, dst_parent, &h_path);
517 h_path.dentry = h_dst;
520 mode = h_inode->i_mode;
521 switch (mode & S_IFMT) {
523 err = vfsub_create(h_dir, &h_path, mode | S_IWUSR);
525 err = au_do_cpup_regular(cpg, h_src_attr);
529 err = vfsub_mkdir(h_dir, &h_path, mode);
532 * strange behaviour from the users view,
533 * particularry setattr case
535 if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
536 au_cpup_attr_nlink(dst_parent->d_inode,
538 au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
542 err = au_do_cpup_symlink(&h_path, h_src, h_dir);
546 AuDebugOn(!capable(CAP_MKNOD));
550 err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
553 AuIOErr("Unknown inode type 0%o\n", mode);
557 mnt_flags = au_mntflags(sb);
558 if (!au_opt_test(mnt_flags, UDBA_NONE)
560 && au_opt_test(mnt_flags, XINO)
561 && h_inode->i_nlink == 1
562 /* todo: unnecessary? */
563 /* && cpg->dentry->d_inode->i_nlink == 1 */
564 && cpg->bdst < cpg->bsrc
565 && !au_ftest_cpup(cpg->flags, KEEPLINO))
566 au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
567 /* ignore this error */
570 au_dtime_revert(&dt);
574 static int au_do_ren_after_cpup(struct dentry *dentry, aufs_bindex_t bdst,
578 struct dentry *h_dentry, *h_parent;
581 h_dentry = dget(au_h_dptr(dentry, bdst));
582 au_set_h_dptr(dentry, bdst, NULL);
583 err = au_lkup_neg(dentry, bdst, /*wh*/0);
585 au_set_h_dptr(dentry, bdst, h_dentry);
589 h_path->dentry = dget(au_h_dptr(dentry, bdst));
590 au_set_h_dptr(dentry, bdst, h_dentry);
591 h_parent = h_dentry->d_parent; /* dir inode is locked */
592 h_dir = h_parent->d_inode;
594 AuDbg("%.*s %.*s\n", AuDLNPair(h_dentry), AuDLNPair(h_path->dentry));
595 err = vfsub_rename(h_dir, h_dentry, h_dir, h_path);
596 dput(h_path->dentry);
603 * copyup the @dentry from @bsrc to @bdst.
604 * the caller must set the both of lower dentries.
605 * @len is for truncating when it is -1 copyup the entire file.
606 * in link/rename cases, @dst_parent may be different from the real one.
608 static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
611 aufs_bindex_t old_ibstart;
612 unsigned char isdir, plink;
613 struct dentry *h_src, *h_dst, *h_parent;
614 struct inode *dst_inode, *h_dir, *inode;
615 struct super_block *sb;
616 struct au_branch *br;
617 /* to reuduce stack size */
621 struct au_cpup_reg_attr h_src_attr;
624 AuDebugOn(cpg->bsrc <= cpg->bdst);
627 a = kmalloc(sizeof(*a), GFP_NOFS);
630 a->h_src_attr.valid = 0;
632 sb = cpg->dentry->d_sb;
633 br = au_sbr(sb, cpg->bdst);
634 a->h_path.mnt = au_br_mnt(br);
635 h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
636 h_parent = h_dst->d_parent; /* dir inode is locked */
637 h_dir = h_parent->d_inode;
640 h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
641 inode = cpg->dentry->d_inode;
644 dst_parent = dget_parent(cpg->dentry);
648 plink = !!au_opt_test(au_mntflags(sb), PLINK);
649 dst_inode = au_h_iptr(inode, cpg->bdst);
651 if (unlikely(!plink)) {
653 AuIOErr("hi%lu(i%lu) exists on b%d "
654 "but plink is disabled\n",
655 dst_inode->i_ino, inode->i_ino, cpg->bdst);
659 if (dst_inode->i_nlink) {
660 const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
662 h_src = au_plink_lkup(inode, cpg->bdst);
663 err = PTR_ERR(h_src);
666 if (unlikely(!h_src->d_inode)) {
668 AuIOErr("i%lu exists on a upper branch "
669 "but not pseudo-linked\n",
676 a->h_path.dentry = h_parent;
677 au_dtime_store(&a->dt, dst_parent, &a->h_path);
680 a->h_path.dentry = h_dst;
681 err = vfsub_link(h_src, h_dir, &a->h_path);
682 if (!err && au_ftest_cpup(cpg->flags, RENAME))
683 err = au_do_ren_after_cpup
684 (cpg->dentry, cpg->bdst, &a->h_path);
686 au_dtime_revert(&a->dt);
690 /* todo: cpup_wh_file? */
692 au_update_ibrange(inode, /*do_put_zero*/1);
695 isdir = S_ISDIR(inode->i_mode);
696 old_ibstart = au_ibstart(inode);
697 err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
700 dst_inode = h_dst->d_inode;
701 mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
702 /* todo: necessary? */
703 /* au_pin_hdir_unlock(cpg->pin); */
705 err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
707 /* todo: necessary? */
708 /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
709 mutex_unlock(&dst_inode->i_mutex);
713 if (cpg->bdst < old_ibstart) {
714 if (S_ISREG(inode->i_mode)) {
715 err = au_dy_iaop(inode, cpg->bdst, dst_inode);
717 /* ignore an error */
718 /* au_pin_hdir_relock(cpg->pin); */
719 mutex_unlock(&dst_inode->i_mutex);
723 au_set_ibstart(inode, cpg->bdst);
725 au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
726 au_hi_flags(inode, isdir));
728 /* todo: necessary? */
729 /* err = au_pin_hdir_relock(cpg->pin); */
730 mutex_unlock(&dst_inode->i_mutex);
735 && h_src->d_inode->i_nlink > 1
737 au_plink_append(inode, cpg->bdst, h_dst);
739 if (au_ftest_cpup(cpg->flags, RENAME)) {
740 a->h_path.dentry = h_dst;
741 err = au_do_ren_after_cpup(cpg->dentry, cpg->bdst, &a->h_path);
744 goto out_parent; /* success */
748 a->h_path.dentry = h_parent;
749 au_dtime_store(&a->dt, dst_parent, &a->h_path);
750 a->h_path.dentry = h_dst;
752 if (h_dst->d_inode) {
754 rerr = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
756 rerr = vfsub_rmdir(h_dir, &a->h_path);
758 au_dtime_revert(&a->dt);
760 AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
771 struct au_cpup_single_args {
773 struct au_cp_generic *cpg;
774 struct dentry *dst_parent;
777 static void au_call_cpup_single(void *args)
779 struct au_cpup_single_args *a = args;
781 au_pin_hdir_acquire_nest(a->cpg->pin);
782 *a->errp = au_cpup_single(a->cpg, a->dst_parent);
783 au_pin_hdir_release(a->cpg->pin);
788 * prevent SIGXFSZ in copy-up.
789 * testing CAP_MKNOD is for generic fs,
790 * but CAP_FSETID is for xfs only, currently.
792 static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
795 struct super_block *sb;
799 sb = au_pinned_parent(pin)->d_sb;
801 && (!au_sbi(sb)->si_plink_maint_pid
802 || au_plink_maint(sb, AuLock_NOPLM))) {
803 switch (mode & S_IFMT) {
805 /* no condition about RLIMIT_FSIZE and the file size */
810 do_sio = !capable(CAP_MKNOD);
814 do_sio = ((mode & (S_ISUID | S_ISGID))
815 && !capable(CAP_FSETID));
816 /* this workaround may be removed in the future */
818 h_dir = au_pinned_h_dir(pin);
819 do_sio = h_dir->i_mode & S_ISVTX;
827 int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
830 struct dentry *h_dentry;
832 h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
833 if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
834 err = au_cpup_single(cpg, dst_parent);
836 struct au_cpup_single_args args = {
839 .dst_parent = dst_parent
841 wkq_err = au_wkq_wait(au_call_cpup_single, &args);
842 if (unlikely(wkq_err))
851 * copyup the @dentry from the first active lower branch to @bdst,
852 * using au_cpup_single().
854 static int au_cpup_simple(struct au_cp_generic *cpg)
857 unsigned int flags_orig;
858 aufs_bindex_t bsrc, bend;
859 struct dentry *dentry, *h_dentry;
861 dentry = cpg->dentry;
862 DiMustWriteLock(dentry);
864 bend = au_dbend(dentry);
866 for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
867 h_dentry = au_h_dptr(dentry, bsrc);
869 AuDebugOn(!h_dentry->d_inode);
873 AuDebugOn(bsrc > bend);
877 err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
879 flags_orig = cpg->flags;
880 au_fset_cpup(cpg->flags, RENAME);
881 err = au_cpup_single(cpg, NULL);
882 cpg->flags = flags_orig;
884 return 0; /* success */
887 au_set_h_dptr(dentry, cpg->bdst, NULL);
888 au_set_dbstart(dentry, cpg->bsrc);
894 struct au_cpup_simple_args {
896 struct au_cp_generic *cpg;
899 static void au_call_cpup_simple(void *args)
901 struct au_cpup_simple_args *a = args;
903 au_pin_hdir_acquire_nest(a->cpg->pin);
904 *a->errp = au_cpup_simple(a->cpg);
905 au_pin_hdir_release(a->cpg->pin);
908 int au_sio_cpup_simple(struct au_cp_generic *cpg)
911 struct dentry *dentry, *parent;
915 dentry = cpg->dentry;
917 if (au_ftest_cpup(cpg->flags, HOPEN)) {
918 AuDebugOn(cpg->bsrc < 0);
919 h_file = au_h_open_pre(dentry, cpg->bsrc);
920 err = PTR_ERR(h_file);
925 parent = dget_parent(dentry);
926 h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
927 if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
928 && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
929 err = au_cpup_simple(cpg);
931 struct au_cpup_simple_args args = {
935 wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
936 if (unlikely(wkq_err))
942 au_h_open_post(dentry, cpg->bsrc, h_file);
948 /* ---------------------------------------------------------------------- */
951 * copyup the deleted file for writing.
953 static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
957 unsigned int flags_orig;
958 aufs_bindex_t bsrc_orig;
959 struct dentry *h_d_dst, *h_d_start;
960 struct au_dinfo *dinfo;
961 struct au_hdentry *hdp;
963 dinfo = au_di(cpg->dentry);
964 AuRwMustWriteLock(&dinfo->di_rwsem);
966 bsrc_orig = cpg->bsrc;
967 cpg->bsrc = dinfo->di_bstart;
968 hdp = dinfo->di_hdentry;
969 h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
970 dinfo->di_bstart = cpg->bdst;
971 hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
974 h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
975 hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_dentry;
977 flags_orig = cpg->flags;
978 cpg->flags = !AuCpup_DTIME;
979 err = au_cpup_single(cpg, /*h_parent*/NULL);
980 cpg->flags = flags_orig;
983 err = au_reopen_nondir(file);
984 hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
986 hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
987 dinfo->di_bstart = cpg->bsrc;
988 cpg->bsrc = bsrc_orig;
993 static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
998 struct dentry *dentry, *parent, *h_parent, *wh_dentry;
999 struct au_branch *br;
1002 dentry = cpg->dentry;
1004 br = au_sbr(dentry->d_sb, bdst);
1005 parent = dget_parent(dentry);
1006 h_parent = au_h_dptr(parent, bdst);
1007 wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
1008 err = PTR_ERR(wh_dentry);
1009 if (IS_ERR(wh_dentry))
1012 h_path.dentry = h_parent;
1013 h_path.mnt = au_br_mnt(br);
1014 au_dtime_store(&dt, parent, &h_path);
1015 err = au_do_cpup_wh(cpg, wh_dentry, file);
1020 h_path.dentry = wh_dentry;
1021 if (!S_ISDIR(wh_dentry->d_inode->i_mode))
1022 err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
1024 err = vfsub_rmdir(h_parent->d_inode, &h_path);
1025 if (unlikely(err)) {
1026 AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
1027 AuDLNPair(wh_dentry), err);
1030 au_dtime_revert(&dt);
1031 au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
1040 struct au_cpup_wh_args {
1042 struct au_cp_generic *cpg;
1046 static void au_call_cpup_wh(void *args)
1048 struct au_cpup_wh_args *a = args;
1050 au_pin_hdir_acquire_nest(a->cpg->pin);
1051 *a->errp = au_cpup_wh(a->cpg, a->file);
1052 au_pin_hdir_release(a->cpg->pin);
1055 int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1059 struct dentry *dentry, *parent, *h_orph, *h_parent, *h_dentry;
1060 struct inode *dir, *h_dir, *h_tmpdir;
1062 struct au_pin wh_pin, *pin_orig;
1064 dentry = cpg->dentry;
1066 parent = dget_parent(dentry);
1067 dir = parent->d_inode;
1070 h_dir = au_igrab(au_h_iptr(dir, bdst));
1073 if (!h_dir->i_nlink) {
1074 wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
1075 h_orph = wbr->wbr_orph;
1077 h_parent = dget(au_h_dptr(parent, bdst));
1078 au_set_h_dptr(parent, bdst, dget(h_orph));
1079 h_tmpdir = h_orph->d_inode;
1080 au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
1083 h_dentry = au_hf_top(file)->f_dentry;
1085 h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
1086 mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
1087 /* todo: au_h_open_pre()? */
1089 pin_orig = cpg->pin;
1090 au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
1091 AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
1095 if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
1096 && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
1097 err = au_cpup_wh(cpg, file);
1099 struct au_cpup_wh_args args = {
1104 wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
1105 if (unlikely(wkq_err))
1110 mutex_unlock(&h_tmpdir->i_mutex);
1111 /* todo: au_h_open_post()? */
1112 au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1113 au_set_h_dptr(parent, bdst, h_parent);
1114 AuDebugOn(!pin_orig);
1115 cpg->pin = pin_orig;
1123 /* ---------------------------------------------------------------------- */
1126 * generic routine for both of copy-up and copy-down.
1128 /* cf. revalidate function in file.c */
1129 int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
1130 int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
1132 struct dentry *h_parent, void *arg),
1137 struct dentry *d, *parent, *h_parent, *real_parent;
1140 parent = dget_parent(dentry);
1141 if (IS_ROOT(parent))
1144 au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
1145 au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
1147 /* do not use au_dpage */
1148 real_parent = parent;
1151 parent = dget_parent(dentry);
1152 h_parent = au_h_dptr(parent, bdst);
1154 goto out; /* success */
1156 /* find top dir which is necessary to cpup */
1160 parent = dget_parent(d);
1161 di_read_lock_parent3(parent, !AuLock_IR);
1162 h_parent = au_h_dptr(parent, bdst);
1163 di_read_unlock(parent, !AuLock_IR);
1164 } while (!h_parent);
1166 if (d != real_parent)
1167 di_write_lock_child3(d);
1169 /* somebody else might create while we were sleeping */
1170 if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
1171 if (au_h_dptr(d, bdst))
1172 au_update_dbstart(d);
1174 au_pin_set_dentry(&pin, d);
1175 err = au_do_pin(&pin);
1177 err = cp(d, bdst, &pin, h_parent, arg);
1182 if (d != real_parent)
1193 static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
1195 struct dentry *h_parent __maybe_unused ,
1196 void *arg __maybe_unused)
1198 struct au_cp_generic cpg = {
1204 .flags = AuCpup_DTIME
1206 return au_sio_cpup_simple(&cpg);
1209 int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
1211 return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
1214 int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
1217 struct dentry *parent;
1220 parent = dget_parent(dentry);
1221 dir = parent->d_inode;
1223 if (au_h_iptr(dir, bdst))
1226 di_read_unlock(parent, AuLock_IR);
1227 di_write_lock_parent(parent);
1228 /* someone else might change our inode while we were sleeping */
1229 if (!au_h_iptr(dir, bdst))
1230 err = au_cpup_dirs(dentry, bdst);
1231 di_downgrade_lock(parent, AuLock_IR);