2 * Copyright (C) 2005-2012 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, struct inode *src)
29 const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
30 | S_NOATIME | S_NOCMTIME;
32 dst->i_flags |= src->i_flags & ~mask;
33 if (au_test_fs_notime(dst->i_sb))
34 dst->i_flags |= S_NOATIME | S_NOCMTIME;
37 void au_cpup_attr_timesizes(struct inode *inode)
39 struct inode *h_inode;
41 h_inode = au_h_iptr(inode, au_ibstart(inode));
42 fsstack_copy_attr_times(inode, h_inode);
43 fsstack_copy_inode_size(inode, h_inode);
46 void au_cpup_attr_nlink(struct inode *inode, int force)
48 struct inode *h_inode;
49 struct super_block *sb;
50 aufs_bindex_t bindex, bend;
53 bindex = au_ibstart(inode);
54 h_inode = au_h_iptr(inode, bindex);
56 && !S_ISDIR(h_inode->i_mode)
57 && au_opt_test(au_mntflags(sb), PLINK)
58 && au_plink_test(inode))
61 set_nlink(inode, h_inode->i_nlink);
64 * fewer nlink makes find(1) noisy, but larger nlink doesn't.
65 * it may includes whplink directory.
67 if (S_ISDIR(h_inode->i_mode)) {
68 bend = au_ibend(inode);
69 for (bindex++; bindex <= bend; bindex++) {
70 h_inode = au_h_iptr(inode, bindex);
72 au_add_nlink(inode, h_inode);
77 void au_cpup_attr_changeable(struct inode *inode)
79 struct inode *h_inode;
81 h_inode = au_h_iptr(inode, au_ibstart(inode));
82 inode->i_mode = h_inode->i_mode;
83 inode->i_uid = h_inode->i_uid;
84 inode->i_gid = h_inode->i_gid;
85 au_cpup_attr_timesizes(inode);
86 au_cpup_attr_flags(inode, h_inode);
89 void au_cpup_igen(struct inode *inode, struct inode *h_inode)
91 struct au_iinfo *iinfo = au_ii(inode);
93 IiMustWriteLock(inode);
95 iinfo->ii_higen = h_inode->i_generation;
96 iinfo->ii_hsb1 = h_inode->i_sb;
99 void au_cpup_attr_all(struct inode *inode, int force)
101 struct inode *h_inode;
103 h_inode = au_h_iptr(inode, au_ibstart(inode));
104 au_cpup_attr_changeable(inode);
105 if (inode->i_nlink > 0)
106 au_cpup_attr_nlink(inode, force);
107 inode->i_rdev = h_inode->i_rdev;
108 inode->i_blkbits = h_inode->i_blkbits;
109 au_cpup_igen(inode, h_inode);
112 /* ---------------------------------------------------------------------- */
114 /* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
116 /* keep the timestamps of the parent dir when cpup */
117 void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
120 struct inode *h_inode;
122 dt->dt_dentry = dentry;
123 dt->dt_h_path = *h_path;
124 h_inode = h_path->dentry->d_inode;
125 dt->dt_atime = h_inode->i_atime;
126 dt->dt_mtime = h_inode->i_mtime;
130 void au_dtime_revert(struct au_dtime *dt)
135 attr.ia_atime = dt->dt_atime;
136 attr.ia_mtime = dt->dt_mtime;
137 attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
138 | ATTR_ATIME | ATTR_ATIME_SET;
140 err = vfsub_notify_change(&dt->dt_h_path, &attr);
142 pr_warning("restoring timestamps failed(%d). ignored\n", err);
145 /* ---------------------------------------------------------------------- */
147 static noinline_for_stack
148 int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src)
153 struct inode *h_isrc, *h_idst;
155 h_path.dentry = au_h_dptr(dst, bindex);
156 h_idst = h_path.dentry->d_inode;
157 h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
158 h_isrc = h_src->d_inode;
159 ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
160 | ATTR_ATIME | ATTR_MTIME
161 | ATTR_ATIME_SET | ATTR_MTIME_SET;
162 ia.ia_uid = h_isrc->i_uid;
163 ia.ia_gid = h_isrc->i_gid;
164 ia.ia_atime = h_isrc->i_atime;
165 ia.ia_mtime = h_isrc->i_mtime;
166 if (h_idst->i_mode != h_isrc->i_mode
167 && !S_ISLNK(h_idst->i_mode)) {
168 ia.ia_valid |= ATTR_MODE;
169 ia.ia_mode = h_isrc->i_mode;
171 sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
172 au_cpup_attr_flags(h_idst, h_isrc);
173 err = vfsub_notify_change(&h_path, &ia);
175 /* is this nfs only? */
176 if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
177 ia.ia_valid = ATTR_FORCE | ATTR_MODE;
178 ia.ia_mode = h_isrc->i_mode;
179 err = vfsub_notify_change(&h_path, &ia);
185 /* ---------------------------------------------------------------------- */
187 static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
188 char *buf, unsigned long blksize)
191 size_t sz, rbytes, wbytes;
192 unsigned char all_zero;
195 /* reduce stack usage */
198 zp = page_address(ZERO_PAGE(0));
200 return -ENOMEM; /* possible? */
205 AuDbg("len %lld\n", len);
211 /* todo: signal_pending? */
212 while (!rbytes || err == -EAGAIN || err == -EINTR) {
213 rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
216 if (unlikely(err < 0))
220 if (len >= rbytes && rbytes == blksize)
221 all_zero = !memcmp(buf, zp, rbytes);
228 b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
230 /* todo: signal_pending? */
231 if (unlikely(err == -EAGAIN || err == -EINTR))
233 if (unlikely(err < 0))
242 res = vfsub_llseek(dst, rbytes, SEEK_CUR);
244 if (unlikely(res < 0))
251 /* the last block may be a hole */
252 if (!err && all_zero) {
256 if (au_test_nfs(dst->f_dentry->d_sb)) {
257 /* nfs requires this step to make last hole */
258 /* is this only nfs? */
260 /* todo: signal_pending? */
261 err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
262 } while (err == -EAGAIN || err == -EINTR);
269 ia->ia_size = dst->f_pos;
270 ia->ia_valid = ATTR_SIZE | ATTR_FILE;
272 h_mtx = &dst->f_dentry->d_inode->i_mutex;
273 mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
274 err = vfsub_notify_change(&dst->f_path, ia);
282 int au_copy_file(struct file *dst, struct file *src, loff_t len)
285 unsigned long blksize;
286 unsigned char do_kfree;
290 blksize = dst->f_dentry->d_sb->s_blocksize;
291 if (!blksize || PAGE_SIZE < blksize)
293 AuDbg("blksize %lu\n", blksize);
294 do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
296 buf = kmalloc(blksize, GFP_NOFS);
298 buf = (void *)__get_free_page(GFP_NOFS);
303 AuDbg("copying a large file %lld\n", (long long)len);
307 err = au_do_copy_file(dst, src, len, buf, blksize);
311 free_page((unsigned long)buf);
318 * to support a sparse file which is opened with O_APPEND,
319 * we need to close the file.
321 static int au_cp_regular(struct dentry *dentry, aufs_bindex_t bdst,
322 aufs_bindex_t bsrc, loff_t len)
327 aufs_bindex_t bindex;
329 struct dentry *dentry;
331 void *label, *label_file;
335 .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
338 .label_file = &&out_src
342 .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
345 .label_file = &&out_dst
348 struct super_block *sb;
350 /* bsrc branch can be ro/rw. */
353 for (i = 0; i < 2; i++, f++) {
354 f->dentry = au_h_dptr(dentry, f->bindex);
355 f->file = au_h_open(dentry, f->bindex, f->flags, /*file*/NULL);
356 err = PTR_ERR(f->file);
360 if (unlikely(!f->file->f_op))
364 /* try stopping to update while we copyup */
365 IMustLock(file[SRC].dentry->d_inode);
366 err = au_copy_file(file[DST].file, file[SRC].file, len);
369 fput(file[DST].file);
370 au_sbr_put(sb, file[DST].bindex);
372 fput(file[SRC].file);
373 au_sbr_put(sb, file[SRC].bindex);
378 static int au_do_cpup_regular(struct dentry *dentry, aufs_bindex_t bdst,
379 aufs_bindex_t bsrc, loff_t len,
380 struct inode *h_dir, struct path *h_path)
386 l = i_size_read(au_h_iptr(dentry->d_inode, bsrc));
387 if (len == -1 || l < len)
390 err = au_cp_regular(dentry, bdst, bsrc, len);
392 goto out; /* success */
394 rerr = vfsub_unlink(h_dir, h_path, /*force*/0);
396 AuIOErr("failed unlinking cpup-ed %.*s(%d, %d)\n",
397 AuDLNPair(h_path->dentry), err, rerr);
405 static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
416 if (unlikely(!h_src->d_inode->i_op->readlink))
420 sym.k = __getname_gfp(GFP_NOFS);
421 if (unlikely(!sym.k))
424 /* unnecessary to support mmap_sem since symlink is not mmap-able */
427 symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
433 err = vfsub_symlink(h_dir, h_path, sym.k);
441 /* return with the lower dst inode is locked */
442 static noinline_for_stack
443 int cpup_entry(struct dentry *dentry, aufs_bindex_t bdst,
444 aufs_bindex_t bsrc, loff_t len, unsigned int flags,
445 struct dentry *dst_parent)
449 unsigned int mnt_flags;
451 const unsigned char do_dt = !!au_ftest_cpup(flags, DTIME);
454 struct dentry *h_src, *h_dst, *h_parent;
455 struct inode *h_inode, *h_dir;
456 struct super_block *sb;
458 /* bsrc branch can be ro/rw. */
459 h_src = au_h_dptr(dentry, bsrc);
460 h_inode = h_src->d_inode;
461 AuDebugOn(h_inode != au_h_iptr(dentry->d_inode, bsrc));
463 /* try stopping to be referenced while we are creating */
464 h_dst = au_h_dptr(dentry, bdst);
465 h_parent = h_dst->d_parent; /* dir inode is locked */
466 h_dir = h_parent->d_inode;
468 AuDebugOn(h_parent != h_dst->d_parent);
471 h_path.mnt = au_sbr_mnt(sb, bdst);
473 h_path.dentry = h_parent;
474 au_dtime_store(&dt, dst_parent, &h_path);
476 h_path.dentry = h_dst;
479 mode = h_inode->i_mode;
480 switch (mode & S_IFMT) {
482 /* try stopping to update while we are referencing */
484 err = vfsub_create(h_dir, &h_path, mode | S_IWUSR);
486 err = au_do_cpup_regular
487 (dentry, bdst, bsrc, len,
488 au_h_iptr(dst_parent->d_inode, bdst), &h_path);
492 err = vfsub_mkdir(h_dir, &h_path, mode);
495 * strange behaviour from the users view,
496 * particularry setattr case
498 if (au_ibstart(dst_parent->d_inode) == bdst)
499 au_cpup_attr_nlink(dst_parent->d_inode,
501 au_cpup_attr_nlink(dentry->d_inode, /*force*/1);
505 err = au_do_cpup_symlink(&h_path, h_src, h_dir);
509 AuDebugOn(!capable(CAP_MKNOD));
513 err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
516 AuIOErr("Unknown inode type 0%o\n", mode);
520 mnt_flags = au_mntflags(sb);
521 if (!au_opt_test(mnt_flags, UDBA_NONE)
523 && au_opt_test(mnt_flags, XINO)
524 && h_inode->i_nlink == 1
525 /* todo: unnecessary? */
526 /* && dentry->d_inode->i_nlink == 1 */
528 && !au_ftest_cpup(flags, KEEPLINO))
529 au_xino_write(sb, bsrc, h_inode->i_ino, /*ino*/0);
530 /* ignore this error */
533 au_dtime_revert(&dt);
538 * copyup the @dentry from @bsrc to @bdst.
539 * the caller must set the both of lower dentries.
540 * @len is for truncating when it is -1 copyup the entire file.
541 * in link/rename cases, @dst_parent may be different from the real one.
543 static int au_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
544 aufs_bindex_t bsrc, loff_t len, unsigned int flags,
545 struct dentry *dst_parent)
548 aufs_bindex_t old_ibstart;
549 unsigned char isdir, plink;
552 struct dentry *h_src, *h_dst, *h_parent;
553 struct inode *dst_inode, *h_dir, *inode;
554 struct super_block *sb;
556 AuDebugOn(bsrc <= bdst);
559 h_path.mnt = au_sbr_mnt(sb, bdst);
560 h_dst = au_h_dptr(dentry, bdst);
561 h_parent = h_dst->d_parent; /* dir inode is locked */
562 h_dir = h_parent->d_inode;
565 h_src = au_h_dptr(dentry, bsrc);
566 inode = dentry->d_inode;
569 dst_parent = dget_parent(dentry);
573 plink = !!au_opt_test(au_mntflags(sb), PLINK);
574 dst_inode = au_h_iptr(inode, bdst);
576 if (unlikely(!plink)) {
578 AuIOErr("hi%lu(i%lu) exists on b%d "
579 "but plink is disabled\n",
580 dst_inode->i_ino, inode->i_ino, bdst);
584 if (dst_inode->i_nlink) {
585 const int do_dt = au_ftest_cpup(flags, DTIME);
587 h_src = au_plink_lkup(inode, bdst);
588 err = PTR_ERR(h_src);
591 if (unlikely(!h_src->d_inode)) {
593 AuIOErr("i%lu exists on a upper branch "
594 "but not pseudo-linked\n",
601 h_path.dentry = h_parent;
602 au_dtime_store(&dt, dst_parent, &h_path);
604 h_path.dentry = h_dst;
605 err = vfsub_link(h_src, h_dir, &h_path);
607 au_dtime_revert(&dt);
611 /* todo: cpup_wh_file? */
613 au_update_ibrange(inode, /*do_put_zero*/1);
616 old_ibstart = au_ibstart(inode);
617 err = cpup_entry(dentry, bdst, bsrc, len, flags, dst_parent);
620 dst_inode = h_dst->d_inode;
621 mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
623 err = cpup_iattr(dentry, bdst, h_src);
624 isdir = S_ISDIR(dst_inode->i_mode);
626 if (bdst < old_ibstart) {
627 if (S_ISREG(inode->i_mode)) {
628 err = au_dy_iaop(inode, bdst, dst_inode);
632 au_set_ibstart(inode, bdst);
634 au_set_h_iptr(inode, bdst, au_igrab(dst_inode),
635 au_hi_flags(inode, isdir));
636 mutex_unlock(&dst_inode->i_mutex);
638 && h_src->d_inode->i_nlink > 1
640 au_plink_append(inode, bdst, h_dst);
641 goto out; /* success */
646 h_path.dentry = h_parent;
647 mutex_unlock(&dst_inode->i_mutex);
648 au_dtime_store(&dt, dst_parent, &h_path);
649 h_path.dentry = h_dst;
651 rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
653 rerr = vfsub_rmdir(h_dir, &h_path);
654 au_dtime_revert(&dt);
656 AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
665 struct au_cpup_single_args {
667 struct dentry *dentry;
668 aufs_bindex_t bdst, bsrc;
671 struct dentry *dst_parent;
674 static void au_call_cpup_single(void *args)
676 struct au_cpup_single_args *a = args;
677 *a->errp = au_cpup_single(a->dentry, a->bdst, a->bsrc, a->len,
678 a->flags, a->dst_parent);
682 * prevent SIGXFSZ in copy-up.
683 * testing CAP_MKNOD is for generic fs,
684 * but CAP_FSETID is for xfs only, currently.
686 static int au_cpup_sio_test(struct super_block *sb, umode_t mode)
692 && (!au_sbi(sb)->si_plink_maint_pid
693 || au_plink_maint(sb, AuLock_NOPLM))) {
694 switch (mode & S_IFMT) {
696 /* no condition about RLIMIT_FSIZE and the file size */
701 do_sio = !capable(CAP_MKNOD);
705 do_sio = ((mode & (S_ISUID | S_ISGID))
706 && !capable(CAP_FSETID));
712 int au_sio_cpup_single(struct dentry *dentry, aufs_bindex_t bdst,
713 aufs_bindex_t bsrc, loff_t len, unsigned int flags,
714 struct dentry *dst_parent)
717 struct dentry *h_dentry;
719 h_dentry = au_h_dptr(dentry, bsrc);
720 if (!au_cpup_sio_test(dentry->d_sb, h_dentry->d_inode->i_mode))
721 err = au_cpup_single(dentry, bdst, bsrc, len, flags,
724 struct au_cpup_single_args args = {
731 .dst_parent = dst_parent
733 wkq_err = au_wkq_wait(au_call_cpup_single, &args);
734 if (unlikely(wkq_err))
742 * copyup the @dentry from the first active lower branch to @bdst,
743 * using au_cpup_single().
745 static int au_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
749 aufs_bindex_t bsrc, bend;
751 bend = au_dbend(dentry);
752 for (bsrc = bdst + 1; bsrc <= bend; bsrc++)
753 if (au_h_dptr(dentry, bsrc))
756 err = au_lkup_neg(dentry, bdst);
758 err = au_cpup_single(dentry, bdst, bsrc, len, flags, NULL);
760 return 0; /* success */
763 au_set_h_dptr(dentry, bdst, NULL);
764 au_set_dbstart(dentry, bsrc);
770 struct au_cpup_simple_args {
772 struct dentry *dentry;
778 static void au_call_cpup_simple(void *args)
780 struct au_cpup_simple_args *a = args;
781 *a->errp = au_cpup_simple(a->dentry, a->bdst, a->len, a->flags);
784 int au_sio_cpup_simple(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
788 struct dentry *parent;
791 parent = dget_parent(dentry);
792 h_dir = au_h_iptr(parent->d_inode, bdst);
793 if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
794 && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
795 err = au_cpup_simple(dentry, bdst, len, flags);
797 struct au_cpup_simple_args args = {
804 wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
805 if (unlikely(wkq_err))
813 /* ---------------------------------------------------------------------- */
816 * copyup the deleted file for writing.
818 static int au_do_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst,
819 struct dentry *wh_dentry, struct file *file,
823 aufs_bindex_t bstart;
824 struct au_dinfo *dinfo;
825 struct dentry *h_d_dst, *h_d_start;
826 struct au_hdentry *hdp;
828 dinfo = au_di(dentry);
829 AuRwMustWriteLock(&dinfo->di_rwsem);
831 bstart = dinfo->di_bstart;
832 hdp = dinfo->di_hdentry;
833 h_d_dst = hdp[0 + bdst].hd_dentry;
834 dinfo->di_bstart = bdst;
835 hdp[0 + bdst].hd_dentry = wh_dentry;
837 h_d_start = hdp[0 + bstart].hd_dentry;
838 hdp[0 + bstart].hd_dentry = au_hf_top(file)->f_dentry;
840 err = au_cpup_single(dentry, bdst, bstart, len, !AuCpup_DTIME,
844 err = au_reopen_nondir(file);
845 hdp[0 + bstart].hd_dentry = h_d_start;
847 hdp[0 + bdst].hd_dentry = h_d_dst;
848 dinfo->di_bstart = bstart;
853 static int au_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
858 struct dentry *parent, *h_parent, *wh_dentry;
859 struct au_branch *br;
862 br = au_sbr(dentry->d_sb, bdst);
863 parent = dget_parent(dentry);
864 h_parent = au_h_dptr(parent, bdst);
865 wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
866 err = PTR_ERR(wh_dentry);
867 if (IS_ERR(wh_dentry))
870 h_path.dentry = h_parent;
871 h_path.mnt = br->br_mnt;
872 au_dtime_store(&dt, parent, &h_path);
873 err = au_do_cpup_wh(dentry, bdst, wh_dentry, file, len);
878 h_path.dentry = wh_dentry;
879 if (!S_ISDIR(wh_dentry->d_inode->i_mode))
880 err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
882 err = vfsub_rmdir(h_parent->d_inode, &h_path);
884 AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
885 AuDLNPair(wh_dentry), err);
888 au_dtime_revert(&dt);
889 au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
898 struct au_cpup_wh_args {
900 struct dentry *dentry;
906 static void au_call_cpup_wh(void *args)
908 struct au_cpup_wh_args *a = args;
909 *a->errp = au_cpup_wh(a->dentry, a->bdst, a->len, a->file);
912 int au_sio_cpup_wh(struct dentry *dentry, aufs_bindex_t bdst, loff_t len,
916 struct dentry *parent, *h_orph, *h_parent, *h_dentry;
917 struct inode *dir, *h_dir, *h_tmpdir, *h_inode;
920 parent = dget_parent(dentry);
921 dir = parent->d_inode;
924 h_dir = au_igrab(au_h_iptr(dir, bdst));
926 if (!h_dir->i_nlink) {
927 wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
928 h_orph = wbr->wbr_orph;
930 h_parent = dget(au_h_dptr(parent, bdst));
931 au_set_h_dptr(parent, bdst, dget(h_orph));
932 h_tmpdir = h_orph->d_inode;
933 au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
935 /* this temporary unlock is safe */
937 h_dentry = au_hf_top(file)->f_dentry;
939 h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
940 h_inode = h_dentry->d_inode;
942 mutex_unlock(&h_inode->i_mutex);
943 mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
944 mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
945 /* todo: au_h_open_pre()? */
948 if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
949 && !au_cpup_sio_test(dentry->d_sb, dentry->d_inode->i_mode))
950 err = au_cpup_wh(dentry, bdst, len, file);
952 struct au_cpup_wh_args args = {
959 wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
960 if (unlikely(wkq_err))
965 mutex_unlock(&h_tmpdir->i_mutex);
966 /* todo: au_h_open_post()? */
967 au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
968 au_set_h_dptr(parent, bdst, h_parent);
976 /* ---------------------------------------------------------------------- */
979 * generic routine for both of copy-up and copy-down.
981 /* cf. revalidate function in file.c */
982 int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
983 int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
984 struct dentry *h_parent, void *arg),
989 struct dentry *d, *parent, *h_parent, *real_parent;
992 parent = dget_parent(dentry);
996 au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
997 au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
999 /* do not use au_dpage */
1000 real_parent = parent;
1003 parent = dget_parent(dentry);
1004 h_parent = au_h_dptr(parent, bdst);
1006 goto out; /* success */
1008 /* find top dir which is necessary to cpup */
1012 parent = dget_parent(d);
1013 di_read_lock_parent3(parent, !AuLock_IR);
1014 h_parent = au_h_dptr(parent, bdst);
1015 di_read_unlock(parent, !AuLock_IR);
1016 } while (!h_parent);
1018 if (d != real_parent)
1019 di_write_lock_child3(d);
1021 /* somebody else might create while we were sleeping */
1022 if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
1023 if (au_h_dptr(d, bdst))
1024 au_update_dbstart(d);
1026 au_pin_set_dentry(&pin, d);
1027 err = au_do_pin(&pin);
1029 err = cp(d, bdst, h_parent, arg);
1034 if (d != real_parent)
1045 static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
1046 struct dentry *h_parent __maybe_unused ,
1047 void *arg __maybe_unused)
1049 return au_sio_cpup_simple(dentry, bdst, -1, AuCpup_DTIME);
1052 int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
1054 return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
1057 int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
1060 struct dentry *parent;
1063 parent = dget_parent(dentry);
1064 dir = parent->d_inode;
1066 if (au_h_iptr(dir, bdst))
1069 di_read_unlock(parent, AuLock_IR);
1070 di_write_lock_parent(parent);
1071 /* someone else might change our inode while we were sleeping */
1072 if (!au_h_iptr(dir, bdst))
1073 err = au_cpup_dirs(dentry, bdst);
1074 di_downgrade_lock(parent, AuLock_IR);