2 * Copyright (C) 2005-2017 Junjiro R. Okajima
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * inode operations (add entry)
25 * final procedure of adding a new entry, except link(2).
26 * remove whiteout, instantiate, copyup the parent dir's times and size
28 * if it failed, re-create the removed whiteout.
30 static int epilog(struct inode
*dir
, aufs_bindex_t bindex
,
31 struct dentry
*wh_dentry
, struct dentry
*dentry
)
36 struct super_block
*sb
;
37 struct inode
*inode
, *h_dir
;
43 h_dir
= d_inode(wh_dentry
->d_parent
); /* dir inode is locked */
45 AuDebugOn(au_h_iptr(dir
, bindex
) != h_dir
);
46 bwh
= au_dbwh(dentry
);
47 h_path
.dentry
= wh_dentry
;
48 h_path
.mnt
= au_sbr_mnt(sb
, bindex
);
49 err
= au_wh_unlink_dentry(au_h_iptr(dir
, bindex
), &h_path
,
55 inode
= au_new_inode(dentry
, /*must_new*/1);
57 d_instantiate(dentry
, inode
);
58 dir
= d_inode(dentry
->d_parent
); /* dir inode is locked */
60 au_dir_ts(dir
, bindex
);
62 au_fhsm_wrote(sb
, bindex
, /*force*/0);
63 return 0; /* success */
71 /* dir inode is locked */
72 wh
= au_wh_create(dentry
, bwh
, wh_dentry
->d_parent
);
75 AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
85 static int au_d_may_add(struct dentry
*dentry
)
90 if (unlikely(d_unhashed(dentry
)))
92 if (unlikely(d_really_is_positive(dentry
)))
98 * simple tests for the adding inode operations.
99 * following the checks in vfs, plus the parent-child relationship.
101 int au_may_add(struct dentry
*dentry
, aufs_bindex_t bindex
,
102 struct dentry
*h_parent
, int isdir
)
106 struct dentry
*h_dentry
;
107 struct inode
*h_inode
;
110 if (unlikely(dentry
->d_name
.len
> AUFS_MAX_NAMELEN
))
113 h_dentry
= au_h_dptr(dentry
, bindex
);
114 if (d_really_is_negative(dentry
)) {
116 if (unlikely(d_is_positive(h_dentry
)))
121 if (unlikely(d_is_negative(h_dentry
)))
123 h_inode
= d_inode(h_dentry
);
124 if (unlikely(!h_inode
->i_nlink
))
127 h_mode
= h_inode
->i_mode
;
130 if (unlikely(S_ISDIR(h_mode
)))
132 } else if (unlikely(!S_ISDIR(h_mode
))) {
139 /* expected parent dir is locked */
140 if (unlikely(h_parent
!= h_dentry
->d_parent
))
149 * initial procedure of adding a new entry.
150 * prepare writable branch and the parent dir, lock it,
151 * and lookup whiteout for the new entry.
153 static struct dentry
*
154 lock_hdir_lkup_wh(struct dentry
*dentry
, struct au_dtime
*dt
,
155 struct dentry
*src_dentry
, struct au_pin
*pin
,
156 struct au_wr_dir_args
*wr_dir_args
)
158 struct dentry
*wh_dentry
, *h_parent
;
159 struct super_block
*sb
;
160 struct au_branch
*br
;
165 AuDbg("%pd\n", dentry
);
167 err
= au_wr_dir(dentry
, src_dentry
, wr_dir_args
);
169 wh_dentry
= ERR_PTR(err
);
170 if (unlikely(err
< 0))
174 udba
= au_opt_udba(sb
);
175 err
= au_pin(pin
, dentry
, bcpup
, udba
,
176 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
177 wh_dentry
= ERR_PTR(err
);
181 h_parent
= au_pinned_h_parent(pin
);
182 if (udba
!= AuOpt_UDBA_NONE
183 && au_dbtop(dentry
) == bcpup
)
184 err
= au_may_add(dentry
, bcpup
, h_parent
,
185 au_ftest_wrdir(wr_dir_args
->flags
, ISDIR
));
186 else if (unlikely(dentry
->d_name
.len
> AUFS_MAX_NAMELEN
))
188 wh_dentry
= ERR_PTR(err
);
192 br
= au_sbr(sb
, bcpup
);
198 au_dtime_store(dt
, au_pinned_parent(pin
), &tmp
);
202 if (bcpup
!= au_dbwh(dentry
))
203 goto out
; /* success */
206 * ENAMETOOLONG here means that if we allowed create such name, then it
207 * would not be able to removed in the future. So we don't allow such
208 * name here and we don't handle ENAMETOOLONG differently here.
210 wh_dentry
= au_wh_lkup(h_parent
, &dentry
->d_name
, br
);
213 if (IS_ERR(wh_dentry
))
219 /* ---------------------------------------------------------------------- */
221 enum { Mknod
, Symlink
, Creat
};
229 struct vfsub_aopen_args
*aopen
;
241 static int add_simple(struct inode
*dir
, struct dentry
*dentry
,
242 struct simple_arg
*arg
)
246 unsigned char created
;
247 const unsigned char try_aopen
248 = (arg
->type
== Creat
&& arg
->u
.c
.try_aopen
);
249 struct dentry
*wh_dentry
, *parent
;
251 struct super_block
*sb
;
252 struct au_branch
*br
;
253 /* to reuduce stack size */
258 struct au_wr_dir_args wr_dir_args
;
261 AuDbg("%pd\n", dentry
);
265 a
= kmalloc(sizeof(*a
), GFP_NOFS
);
268 a
->wr_dir_args
.force_btgt
= -1;
269 a
->wr_dir_args
.flags
= AuWrDir_ADD_ENTRY
;
271 parent
= dentry
->d_parent
; /* dir inode is locked */
273 err
= aufs_read_lock(dentry
, AuLock_DW
| AuLock_GEN
);
277 err
= au_d_may_add(dentry
);
281 di_write_lock_parent(parent
);
282 wh_dentry
= lock_hdir_lkup_wh(dentry
, &a
->dt
, /*src_dentry*/NULL
,
283 &a
->pin
, &a
->wr_dir_args
);
284 err
= PTR_ERR(wh_dentry
);
285 if (IS_ERR(wh_dentry
))
288 btop
= au_dbtop(dentry
);
290 br
= au_sbr(sb
, btop
);
291 a
->h_path
.dentry
= au_h_dptr(dentry
, btop
);
292 a
->h_path
.mnt
= au_br_mnt(br
);
293 h_dir
= au_pinned_h_dir(&a
->pin
);
297 if (!try_aopen
|| !h_dir
->i_op
->atomic_open
)
298 err
= vfsub_create(h_dir
, &a
->h_path
, arg
->u
.c
.mode
,
301 err
= vfsub_atomic_open(h_dir
, a
->h_path
.dentry
,
305 err
= vfsub_symlink(h_dir
, &a
->h_path
, arg
->u
.s
.symname
);
308 err
= vfsub_mknod(h_dir
, &a
->h_path
, arg
->u
.m
.mode
,
316 err
= epilog(dir
, btop
, wh_dentry
, dentry
);
319 if (unlikely(created
&& err
&& d_is_positive(a
->h_path
.dentry
))) {
320 /* no delegation since it is just created */
321 rerr
= vfsub_unlink(h_dir
, &a
->h_path
, /*delegated*/NULL
,
324 AuIOErr("%pd revert failure(%d, %d)\n",
328 au_dtime_revert(&a
->dt
);
331 if (!err
&& try_aopen
&& !h_dir
->i_op
->atomic_open
)
332 *arg
->u
.c
.aopen
->opened
|= FILE_CREATED
;
339 di_write_unlock(parent
);
342 au_update_dbtop(dentry
);
346 aufs_read_unlock(dentry
, AuLock_DW
);
353 int aufs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
356 struct simple_arg arg
= {
363 return add_simple(dir
, dentry
, &arg
);
366 int aufs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
368 struct simple_arg arg
= {
370 .u
.s
.symname
= symname
372 return add_simple(dir
, dentry
, &arg
);
375 int aufs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
378 struct simple_arg arg
= {
382 .want_excl
= want_excl
385 return add_simple(dir
, dentry
, &arg
);
388 int au_aopen_or_create(struct inode
*dir
, struct dentry
*dentry
,
389 struct vfsub_aopen_args
*aopen_args
)
391 struct simple_arg arg
= {
394 .mode
= aopen_args
->create_mode
,
395 .want_excl
= aopen_args
->open_flag
& O_EXCL
,
400 return add_simple(dir
, dentry
, &arg
);
403 int aufs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
406 aufs_bindex_t bindex
;
407 struct super_block
*sb
;
408 struct dentry
*parent
, *h_parent
, *h_dentry
;
409 struct inode
*h_dir
, *inode
;
410 struct vfsmount
*h_mnt
;
411 struct au_wr_dir_args wr_dir_args
= {
413 .flags
= AuWrDir_TMPFILE
416 /* copy-up may happen */
420 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
424 err
= au_di_init(dentry
);
429 parent
= d_find_any_alias(dir
);
431 di_write_lock_parent(parent
);
432 if (unlikely(d_inode(parent
) != dir
))
435 err
= au_digen_test(parent
, au_sigen(sb
));
439 bindex
= au_dbtop(parent
);
440 au_set_dbtop(dentry
, bindex
);
441 au_set_dbbot(dentry
, bindex
);
442 err
= au_wr_dir(dentry
, /*src_dentry*/NULL
, &wr_dir_args
);
444 if (unlikely(err
< 0))
448 h_dir
= au_h_iptr(dir
, bindex
);
449 if (unlikely(!h_dir
->i_op
->tmpfile
))
452 h_mnt
= au_sbr_mnt(sb
, bindex
);
453 err
= vfsub_mnt_want_write(h_mnt
);
457 h_parent
= au_h_dptr(parent
, bindex
);
458 h_dentry
= vfs_tmpfile(h_parent
, mode
, /*open_flag*/0);
459 if (IS_ERR(h_dentry
)) {
460 err
= PTR_ERR(h_dentry
);
464 au_set_dbtop(dentry
, bindex
);
465 au_set_dbbot(dentry
, bindex
);
466 au_set_h_dptr(dentry
, bindex
, dget(h_dentry
));
467 inode
= au_new_inode(dentry
, /*must_new*/1);
469 err
= PTR_ERR(inode
);
470 au_set_h_dptr(dentry
, bindex
, NULL
);
471 au_set_dbtop(dentry
, -1);
472 au_set_dbbot(dentry
, -1);
476 d_tmpfile(dentry
, inode
);
477 au_di(dentry
)->di_tmpfile
= 1;
479 /* update without i_mutex */
480 if (au_ibtop(dir
) == au_dbtop(dentry
))
481 au_cpup_attr_timesizes(dir
);
486 vfsub_mnt_drop_write(h_mnt
);
488 di_write_unlock(parent
);
490 di_write_unlock(dentry
);
493 dentry
->d_fsdata
= NULL
;
502 /* ---------------------------------------------------------------------- */
504 struct au_link_args
{
505 aufs_bindex_t bdst
, bsrc
;
508 struct dentry
*src_parent
, *parent
;
511 static int au_cpup_before_link(struct dentry
*src_dentry
,
512 struct au_link_args
*a
)
515 struct dentry
*h_src_dentry
;
516 struct au_cp_generic cpg
= {
517 .dentry
= src_dentry
,
522 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
/* | AuCpup_KEEPLINO */
525 di_read_lock_parent(a
->src_parent
, AuLock_IR
);
526 err
= au_test_and_cpup_dirs(src_dentry
, a
->bdst
);
530 h_src_dentry
= au_h_dptr(src_dentry
, a
->bsrc
);
531 err
= au_pin(&a
->pin
, src_dentry
, a
->bdst
,
532 au_opt_udba(src_dentry
->d_sb
),
533 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
537 err
= au_sio_cpup_simple(&cpg
);
541 di_read_unlock(a
->src_parent
, AuLock_IR
);
545 static int au_cpup_or_link(struct dentry
*src_dentry
, struct dentry
*dentry
,
546 struct au_link_args
*a
)
551 struct dentry
*h_src_dentry
;
552 struct inode
*h_inode
, *inode
, *delegated
;
553 struct super_block
*sb
;
558 sb
= src_dentry
->d_sb
;
559 inode
= d_inode(src_dentry
);
560 if (au_ibtop(inode
) <= a
->bdst
)
561 h_inode
= au_h_iptr(inode
, a
->bdst
);
562 if (!h_inode
|| !h_inode
->i_nlink
) {
563 /* copyup src_dentry as the name of dentry. */
564 bbot
= au_dbbot(dentry
);
566 au_set_dbbot(dentry
, a
->bsrc
);
567 au_set_h_dptr(dentry
, a
->bsrc
,
568 dget(au_h_dptr(src_dentry
, a
->bsrc
)));
569 dget(a
->h_path
.dentry
);
570 au_set_h_dptr(dentry
, a
->bdst
, NULL
);
571 AuDbg("temporary d_inode...\n");
572 spin_lock(&dentry
->d_lock
);
573 dentry
->d_inode
= d_inode(src_dentry
); /* tmp */
574 spin_unlock(&dentry
->d_lock
);
575 h_file
= au_h_open_pre(dentry
, a
->bsrc
, /*force_wr*/0);
577 err
= PTR_ERR(h_file
);
579 struct au_cp_generic cpg
= {
585 .flags
= AuCpup_KEEPLINO
587 err
= au_sio_cpup_simple(&cpg
);
588 au_h_open_post(dentry
, a
->bsrc
, h_file
);
590 dput(a
->h_path
.dentry
);
591 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->bdst
);
593 au_set_h_dptr(dentry
, a
->bdst
,
596 spin_lock(&dentry
->d_lock
);
597 dentry
->d_inode
= NULL
; /* restore */
598 spin_unlock(&dentry
->d_lock
);
599 AuDbg("temporary d_inode...done\n");
600 au_set_h_dptr(dentry
, a
->bsrc
, NULL
);
601 au_set_dbbot(dentry
, bbot
);
603 /* the inode of src_dentry already exists on a.bdst branch */
604 h_src_dentry
= d_find_alias(h_inode
);
605 if (!h_src_dentry
&& au_plink_test(inode
)) {
607 h_src_dentry
= au_plink_lkup(inode
, a
->bdst
);
608 err
= PTR_ERR(h_src_dentry
);
609 if (IS_ERR(h_src_dentry
))
612 if (unlikely(d_is_negative(h_src_dentry
))) {
620 err
= vfsub_link(h_src_dentry
, au_pinned_h_dir(&a
->pin
),
621 &a
->h_path
, &delegated
);
622 if (unlikely(err
== -EWOULDBLOCK
)) {
623 pr_warn("cannot retry for NFSv4 delegation"
624 " for an internal link\n");
629 AuIOErr("no dentry found for hi%lu on b%d\n",
630 h_inode
->i_ino
, a
->bdst
);
636 au_plink_append(inode
, a
->bdst
, a
->h_path
.dentry
);
643 int aufs_link(struct dentry
*src_dentry
, struct inode
*dir
,
644 struct dentry
*dentry
)
648 struct au_link_args
*a
;
649 struct dentry
*wh_dentry
, *h_src_dentry
;
650 struct inode
*inode
, *delegated
;
651 struct super_block
*sb
;
652 struct au_wr_dir_args wr_dir_args
= {
653 /* .force_btgt = -1, */
654 .flags
= AuWrDir_ADD_ENTRY
658 inode
= d_inode(src_dentry
);
662 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
666 a
->parent
= dentry
->d_parent
; /* dir inode is locked */
667 err
= aufs_read_and_write_lock2(dentry
, src_dentry
,
668 AuLock_NOPLM
| AuLock_GEN
);
671 err
= au_d_linkable(src_dentry
);
674 err
= au_d_may_add(dentry
);
678 a
->src_parent
= dget_parent(src_dentry
);
679 wr_dir_args
.force_btgt
= au_ibtop(inode
);
681 di_write_lock_parent(a
->parent
);
682 wr_dir_args
.force_btgt
= au_wbr(dentry
, wr_dir_args
.force_btgt
);
683 wh_dentry
= lock_hdir_lkup_wh(dentry
, &dt
, src_dentry
, &a
->pin
,
685 err
= PTR_ERR(wh_dentry
);
686 if (IS_ERR(wh_dentry
))
691 a
->bdst
= au_dbtop(dentry
);
692 a
->h_path
.dentry
= au_h_dptr(dentry
, a
->bdst
);
693 a
->h_path
.mnt
= au_sbr_mnt(sb
, a
->bdst
);
694 a
->bsrc
= au_ibtop(inode
);
695 h_src_dentry
= au_h_d_alias(src_dentry
, a
->bsrc
);
696 if (!h_src_dentry
&& au_di(src_dentry
)->di_tmpfile
)
697 h_src_dentry
= dget(au_hi_wh(inode
, a
->bsrc
));
699 a
->bsrc
= au_dbtop(src_dentry
);
700 h_src_dentry
= au_h_d_alias(src_dentry
, a
->bsrc
);
701 AuDebugOn(!h_src_dentry
);
702 } else if (IS_ERR(h_src_dentry
)) {
703 err
= PTR_ERR(h_src_dentry
);
708 * aufs doesn't touch the credential so
709 * security_dentry_create_files_as() is unnecrssary.
711 if (au_opt_test(au_mntflags(sb
), PLINK
)) {
712 if (a
->bdst
< a
->bsrc
713 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
714 err
= au_cpup_or_link(src_dentry
, dentry
, a
);
717 err
= vfsub_link(h_src_dentry
, au_pinned_h_dir(&a
->pin
),
718 &a
->h_path
, &delegated
);
719 if (unlikely(err
== -EWOULDBLOCK
)) {
720 pr_warn("cannot retry for NFSv4 delegation"
721 " for an internal link\n");
728 * copyup src_dentry to the branch we process,
729 * and then link(2) to it.
732 if (a
->bdst
< a
->bsrc
733 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
735 di_write_unlock(a
->parent
);
736 err
= au_cpup_before_link(src_dentry
, a
);
737 di_write_lock_parent(a
->parent
);
739 err
= au_pin(&a
->pin
, dentry
, a
->bdst
,
741 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
746 h_src_dentry
= au_h_dptr(src_dentry
, a
->bdst
);
748 if (h_src_dentry
&& d_is_positive(h_src_dentry
)) {
750 err
= vfsub_link(h_src_dentry
,
751 au_pinned_h_dir(&a
->pin
),
752 &a
->h_path
, &delegated
);
753 if (unlikely(err
== -EWOULDBLOCK
)) {
754 pr_warn("cannot retry"
755 " for NFSv4 delegation"
756 " for an internal link\n");
766 a
->h_path
.dentry
= wh_dentry
;
767 err
= au_wh_unlink_dentry(au_pinned_h_dir(&a
->pin
), &a
->h_path
,
773 au_dir_ts(dir
, a
->bdst
);
776 inode
->i_ctime
= dir
->i_ctime
;
777 d_instantiate(dentry
, au_igrab(inode
));
778 if (d_unhashed(a
->h_path
.dentry
))
779 /* some filesystem calls d_drop() */
781 /* some filesystems consume an inode even hardlink */
782 au_fhsm_wrote(sb
, a
->bdst
, /*force*/0);
783 goto out_unpin
; /* success */
786 /* no delegation since it is just created */
787 rerr
= vfsub_unlink(au_pinned_h_dir(&a
->pin
), &a
->h_path
,
788 /*delegated*/NULL
, /*force*/0);
789 if (unlikely(rerr
)) {
790 AuIOErr("%pd reverting failed(%d, %d)\n", dentry
, err
, rerr
);
793 au_dtime_revert(&dt
);
799 di_write_unlock(a
->parent
);
803 au_update_dbtop(dentry
);
806 aufs_read_and_write_unlock2(dentry
, src_dentry
);
814 int aufs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
817 aufs_bindex_t bindex
;
818 unsigned char diropq
;
820 struct dentry
*wh_dentry
, *parent
, *opq_dentry
;
821 struct inode
*h_inode
;
822 struct super_block
*sb
;
826 } *a
; /* reduce the stack usage */
827 struct au_wr_dir_args wr_dir_args
= {
829 .flags
= AuWrDir_ADD_ENTRY
| AuWrDir_ISDIR
835 a
= kmalloc(sizeof(*a
), GFP_NOFS
);
839 err
= aufs_read_lock(dentry
, AuLock_DW
| AuLock_GEN
);
842 err
= au_d_may_add(dentry
);
846 parent
= dentry
->d_parent
; /* dir inode is locked */
847 di_write_lock_parent(parent
);
848 wh_dentry
= lock_hdir_lkup_wh(dentry
, &a
->dt
, /*src_dentry*/NULL
,
849 &a
->pin
, &wr_dir_args
);
850 err
= PTR_ERR(wh_dentry
);
851 if (IS_ERR(wh_dentry
))
855 bindex
= au_dbtop(dentry
);
856 h_path
.dentry
= au_h_dptr(dentry
, bindex
);
857 h_path
.mnt
= au_sbr_mnt(sb
, bindex
);
858 err
= vfsub_mkdir(au_pinned_h_dir(&a
->pin
), &h_path
, mode
);
862 /* make the dir opaque */
864 h_inode
= d_inode(h_path
.dentry
);
866 || au_opt_test(au_mntflags(sb
), ALWAYS_DIROPQ
)) {
867 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
868 opq_dentry
= au_diropq_create(dentry
, bindex
);
869 inode_unlock(h_inode
);
870 err
= PTR_ERR(opq_dentry
);
871 if (IS_ERR(opq_dentry
))
877 err
= epilog(dir
, bindex
, wh_dentry
, dentry
);
880 goto out_unpin
; /* success */
886 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
887 rerr
= au_diropq_remove(dentry
, bindex
);
888 inode_unlock(h_inode
);
890 AuIOErr("%pd reverting diropq failed(%d, %d)\n",
898 rerr
= vfsub_rmdir(au_pinned_h_dir(&a
->pin
), &h_path
);
900 AuIOErr("%pd reverting dir failed(%d, %d)\n",
904 au_dtime_revert(&a
->dt
);
909 di_write_unlock(parent
);
912 au_update_dbtop(dentry
);
915 aufs_read_unlock(dentry
, AuLock_DW
);