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 * inode operations (add entry)
26 * final procedure of adding a new entry, except link(2).
27 * remove whiteout, instantiate, copyup the parent dir's times and size
29 * if it failed, re-create the removed whiteout.
31 static int epilog(struct inode *dir, aufs_bindex_t bindex,
32 struct dentry *wh_dentry, struct dentry *dentry)
37 struct inode *inode, *h_dir;
42 h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
44 AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
45 bwh = au_dbwh(dentry);
46 h_path.dentry = wh_dentry;
47 h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
48 err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
54 inode = au_new_inode(dentry, /*must_new*/1);
56 d_instantiate(dentry, inode);
57 dir = dentry->d_parent->d_inode; /* dir inode is locked */
59 if (au_ibstart(dir) == au_dbstart(dentry))
60 au_cpup_attr_timesizes(dir);
62 return 0; /* success */
70 /* dir inode is locked */
71 wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
74 AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
75 AuDLNPair(dentry), err, rerr);
84 static int au_d_may_add(struct dentry *dentry)
89 if (unlikely(d_unhashed(dentry)))
91 if (unlikely(dentry->d_inode))
97 * simple tests for the adding inode operations.
98 * following the checks in vfs, plus the parent-child relationship.
100 int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
101 struct dentry *h_parent, int isdir)
105 struct dentry *h_dentry;
106 struct inode *h_inode;
109 if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
112 h_dentry = au_h_dptr(dentry, bindex);
113 h_inode = h_dentry->d_inode;
114 if (!dentry->d_inode) {
116 if (unlikely(h_inode))
121 if (unlikely(!h_inode || !h_inode->i_nlink))
124 h_mode = h_inode->i_mode;
127 if (unlikely(S_ISDIR(h_mode)))
129 } else if (unlikely(!S_ISDIR(h_mode))) {
136 /* expected parent dir is locked */
137 if (unlikely(h_parent != h_dentry->d_parent))
146 * initial procedure of adding a new entry.
147 * prepare writable branch and the parent dir, lock it,
148 * and lookup whiteout for the new entry.
150 static struct dentry*
151 lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
152 struct dentry *src_dentry, struct au_pin *pin,
153 struct au_wr_dir_args *wr_dir_args)
155 struct dentry *wh_dentry, *h_parent;
156 struct super_block *sb;
157 struct au_branch *br;
162 AuDbg("%.*s\n", AuDLNPair(dentry));
164 err = au_wr_dir(dentry, src_dentry, wr_dir_args);
166 wh_dentry = ERR_PTR(err);
167 if (unlikely(err < 0))
171 udba = au_opt_udba(sb);
172 err = au_pin(pin, dentry, bcpup, udba,
173 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
174 wh_dentry = ERR_PTR(err);
178 h_parent = au_pinned_h_parent(pin);
179 if (udba != AuOpt_UDBA_NONE
180 && au_dbstart(dentry) == bcpup)
181 err = au_may_add(dentry, bcpup, h_parent,
182 au_ftest_wrdir(wr_dir_args->flags, ISDIR));
183 else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
185 wh_dentry = ERR_PTR(err);
189 br = au_sbr(sb, bcpup);
195 au_dtime_store(dt, au_pinned_parent(pin), &tmp);
199 if (bcpup != au_dbwh(dentry))
200 goto out; /* success */
202 wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
205 if (IS_ERR(wh_dentry))
211 /* ---------------------------------------------------------------------- */
213 enum { Mknod, Symlink, Creat };
219 struct nameidata *nd;
231 static int add_simple(struct inode *dir, struct dentry *dentry,
232 struct simple_arg *arg)
235 aufs_bindex_t bstart;
236 unsigned char created;
237 struct dentry *wh_dentry, *parent;
239 /* to reuduce stack size */
244 struct au_wr_dir_args wr_dir_args;
247 AuDbg("%.*s\n", AuDLNPair(dentry));
251 a = kmalloc(sizeof(*a), GFP_NOFS);
254 a->wr_dir_args.force_btgt = -1;
255 a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
257 parent = dentry->d_parent; /* dir inode is locked */
258 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
261 err = au_d_may_add(dentry);
264 di_write_lock_parent(parent);
265 wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
266 &a->pin, &a->wr_dir_args);
267 err = PTR_ERR(wh_dentry);
268 if (IS_ERR(wh_dentry))
271 bstart = au_dbstart(dentry);
272 a->h_path.dentry = au_h_dptr(dentry, bstart);
273 a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
274 h_dir = au_pinned_h_dir(&a->pin);
277 err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode);
280 err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
283 err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
291 err = epilog(dir, bstart, wh_dentry, dentry);
294 if (unlikely(created && err && a->h_path.dentry->d_inode)) {
296 rerr = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
298 AuIOErr("%.*s revert failure(%d, %d)\n",
299 AuDLNPair(dentry), err, rerr);
302 au_dtime_revert(&a->dt);
309 di_write_unlock(parent);
312 au_update_dbstart(dentry);
315 aufs_read_unlock(dentry, AuLock_DW);
322 int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
324 struct simple_arg arg = {
331 return add_simple(dir, dentry, &arg);
334 int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
336 struct simple_arg arg = {
338 .u.s.symname = symname
340 return add_simple(dir, dentry, &arg);
343 int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
344 struct nameidata *nd)
346 struct simple_arg arg = {
353 return add_simple(dir, dentry, &arg);
356 /* ---------------------------------------------------------------------- */
358 struct au_link_args {
359 aufs_bindex_t bdst, bsrc;
362 struct dentry *src_parent, *parent;
365 static int au_cpup_before_link(struct dentry *src_dentry,
366 struct au_link_args *a)
369 struct dentry *h_src_dentry;
370 struct au_cp_generic cpg = {
371 .dentry = src_dentry,
376 .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
379 di_read_lock_parent(a->src_parent, AuLock_IR);
380 err = au_test_and_cpup_dirs(src_dentry, a->bdst);
384 h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
385 err = au_pin(&a->pin, src_dentry, a->bdst,
386 au_opt_udba(src_dentry->d_sb),
387 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
391 err = au_sio_cpup_simple(&cpg);
395 di_read_unlock(a->src_parent, AuLock_IR);
399 static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
400 struct au_link_args *a)
405 struct dentry *h_src_dentry;
406 struct inode *h_inode, *inode;
407 struct super_block *sb;
412 sb = src_dentry->d_sb;
413 inode = src_dentry->d_inode;
414 if (au_ibstart(inode) <= a->bdst)
415 h_inode = au_h_iptr(inode, a->bdst);
416 if (!h_inode || !h_inode->i_nlink) {
417 /* copyup src_dentry as the name of dentry. */
418 bend = au_dbend(dentry);
420 au_set_dbend(dentry, a->bsrc);
421 au_set_h_dptr(dentry, a->bsrc,
422 dget(au_h_dptr(src_dentry, a->bsrc)));
423 dget(a->h_path.dentry);
424 au_set_h_dptr(dentry, a->bdst, NULL);
425 dentry->d_inode = src_dentry->d_inode; /* tmp */
426 h_file = au_h_open_pre(dentry, a->bsrc);
428 err = PTR_ERR(h_file);
430 struct au_cp_generic cpg = {
436 .flags = AuCpup_KEEPLINO
438 err = au_sio_cpup_simple(&cpg);
439 au_h_open_post(dentry, a->bsrc, h_file);
441 dput(a->h_path.dentry);
442 a->h_path.dentry = au_h_dptr(dentry, a->bdst);
444 au_set_h_dptr(dentry, a->bdst,
447 dentry->d_inode = NULL; /* restore */
448 au_set_h_dptr(dentry, a->bsrc, NULL);
449 au_set_dbend(dentry, bend);
451 /* the inode of src_dentry already exists on a.bdst branch */
452 h_src_dentry = d_find_alias(h_inode);
453 if (!h_src_dentry && au_plink_test(inode)) {
455 h_src_dentry = au_plink_lkup(inode, a->bdst);
456 err = PTR_ERR(h_src_dentry);
457 if (IS_ERR(h_src_dentry))
460 if (unlikely(!h_src_dentry->d_inode)) {
467 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
471 AuIOErr("no dentry found for hi%lu on b%d\n",
472 h_inode->i_ino, a->bdst);
478 au_plink_append(inode, a->bdst, a->h_path.dentry);
485 int aufs_link(struct dentry *src_dentry, struct inode *dir,
486 struct dentry *dentry)
490 struct au_link_args *a;
491 struct dentry *wh_dentry, *h_src_dentry;
493 struct super_block *sb;
494 struct au_wr_dir_args wr_dir_args = {
495 /* .force_btgt = -1, */
496 .flags = AuWrDir_ADD_ENTRY
500 inode = src_dentry->d_inode;
504 a = kzalloc(sizeof(*a), GFP_NOFS);
508 a->parent = dentry->d_parent; /* dir inode is locked */
509 err = aufs_read_and_write_lock2(dentry, src_dentry,
510 AuLock_NOPLM | AuLock_GEN);
513 err = au_d_hashed_positive(src_dentry);
516 err = au_d_may_add(dentry);
520 a->src_parent = dget_parent(src_dentry);
521 wr_dir_args.force_btgt = au_ibstart(inode);
523 di_write_lock_parent(a->parent);
524 wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
525 wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
527 err = PTR_ERR(wh_dentry);
528 if (IS_ERR(wh_dentry))
533 a->bdst = au_dbstart(dentry);
534 a->h_path.dentry = au_h_dptr(dentry, a->bdst);
535 a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
536 a->bsrc = au_ibstart(inode);
537 h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
539 a->bsrc = au_dbstart(src_dentry);
540 h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
541 AuDebugOn(!h_src_dentry);
542 } else if (IS_ERR(h_src_dentry))
545 if (au_opt_test(au_mntflags(sb), PLINK)) {
546 if (a->bdst < a->bsrc
547 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
548 err = au_cpup_or_link(src_dentry, dentry, a);
550 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
555 * copyup src_dentry to the branch we process,
556 * and then link(2) to it.
559 if (a->bdst < a->bsrc
560 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
562 di_write_unlock(a->parent);
563 err = au_cpup_before_link(src_dentry, a);
564 di_write_lock_parent(a->parent);
566 err = au_pin(&a->pin, dentry, a->bdst,
568 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
573 h_src_dentry = au_h_dptr(src_dentry, a->bdst);
575 if (h_src_dentry && h_src_dentry->d_inode)
576 err = vfsub_link(h_src_dentry,
577 au_pinned_h_dir(&a->pin),
585 a->h_path.dentry = wh_dentry;
586 err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
593 if (au_ibstart(dir) == au_dbstart(dentry))
594 au_cpup_attr_timesizes(dir);
596 inode->i_ctime = dir->i_ctime;
597 d_instantiate(dentry, au_igrab(inode));
598 if (d_unhashed(a->h_path.dentry))
599 /* some filesystem calls d_drop() */
601 goto out_unpin; /* success */
604 rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
605 if (unlikely(rerr)) {
606 AuIOErr("%.*s reverting failed(%d, %d)\n",
607 AuDLNPair(dentry), err, rerr);
610 au_dtime_revert(&dt);
616 di_write_unlock(a->parent);
620 au_update_dbstart(dentry);
623 aufs_read_and_write_unlock2(dentry, src_dentry);
631 int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
634 aufs_bindex_t bindex;
635 unsigned char diropq;
637 struct dentry *wh_dentry, *parent, *opq_dentry;
639 struct super_block *sb;
643 } *a; /* reduce the stack usage */
644 struct au_wr_dir_args wr_dir_args = {
646 .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
652 a = kmalloc(sizeof(*a), GFP_NOFS);
656 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
659 err = au_d_may_add(dentry);
663 parent = dentry->d_parent; /* dir inode is locked */
664 di_write_lock_parent(parent);
665 wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
666 &a->pin, &wr_dir_args);
667 err = PTR_ERR(wh_dentry);
668 if (IS_ERR(wh_dentry))
672 bindex = au_dbstart(dentry);
673 h_path.dentry = au_h_dptr(dentry, bindex);
674 h_path.mnt = au_sbr_mnt(sb, bindex);
675 err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
679 /* make the dir opaque */
681 h_mtx = &h_path.dentry->d_inode->i_mutex;
683 || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
684 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
685 opq_dentry = au_diropq_create(dentry, bindex);
687 err = PTR_ERR(opq_dentry);
688 if (IS_ERR(opq_dentry))
694 err = epilog(dir, bindex, wh_dentry, dentry);
697 goto out_unpin; /* success */
703 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
704 rerr = au_diropq_remove(dentry, bindex);
707 AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
708 AuDLNPair(dentry), err, rerr);
715 rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
717 AuIOErr("%.*s reverting dir failed(%d, %d)\n",
718 AuDLNPair(dentry), err, rerr);
721 au_dtime_revert(&a->dt);
726 di_write_unlock(parent);
729 au_update_dbstart(dentry);
732 aufs_read_unlock(dentry, AuLock_DW);