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 * 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;
240 struct dentry *wh_dentry, *parent;
242 struct au_wr_dir_args wr_dir_args = {
244 .flags = AuWrDir_ADD_ENTRY
247 AuDbg("%.*s\n", AuDLNPair(dentry));
250 parent = dentry->d_parent; /* dir inode is locked */
251 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
254 err = au_d_may_add(dentry);
257 di_write_lock_parent(parent);
258 wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL, &pin,
260 err = PTR_ERR(wh_dentry);
261 if (IS_ERR(wh_dentry))
264 bstart = au_dbstart(dentry);
265 h_path.dentry = au_h_dptr(dentry, bstart);
266 h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
267 h_dir = au_pinned_h_dir(&pin);
270 err = vfsub_create(h_dir, &h_path, arg->u.c.mode);
273 err = vfsub_symlink(h_dir, &h_path, arg->u.s.symname);
276 err = vfsub_mknod(h_dir, &h_path, arg->u.m.mode, arg->u.m.dev);
283 err = epilog(dir, bstart, wh_dentry, dentry);
286 if (unlikely(created && err && h_path.dentry->d_inode)) {
288 rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
290 AuIOErr("%.*s revert failure(%d, %d)\n",
291 AuDLNPair(dentry), err, rerr);
294 au_dtime_revert(&dt);
301 di_write_unlock(parent);
304 au_update_dbstart(dentry);
307 aufs_read_unlock(dentry, AuLock_DW);
312 int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
314 struct simple_arg arg = {
321 return add_simple(dir, dentry, &arg);
324 int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
326 struct simple_arg arg = {
328 .u.s.symname = symname
330 return add_simple(dir, dentry, &arg);
333 int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
334 struct nameidata *nd)
336 struct simple_arg arg = {
343 return add_simple(dir, dentry, &arg);
346 /* ---------------------------------------------------------------------- */
348 struct au_link_args {
349 aufs_bindex_t bdst, bsrc;
352 struct dentry *src_parent, *parent;
355 static int au_cpup_before_link(struct dentry *src_dentry,
356 struct au_link_args *a)
359 struct dentry *h_src_dentry;
363 di_read_lock_parent(a->src_parent, AuLock_IR);
364 err = au_test_and_cpup_dirs(src_dentry, a->bdst);
368 h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
369 h_mtx = &h_src_dentry->d_inode->i_mutex;
370 err = au_pin(&a->pin, src_dentry, a->bdst,
371 au_opt_udba(src_dentry->d_sb),
372 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
375 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
376 h_file = au_h_open_pre(src_dentry, a->bsrc);
377 if (IS_ERR(h_file)) {
378 err = PTR_ERR(h_file);
381 err = au_sio_cpup_simple(src_dentry, a->bdst, -1,
382 AuCpup_DTIME /* | AuCpup_KEEPLINO */);
384 au_h_open_post(src_dentry, a->bsrc, h_file);
388 di_read_unlock(a->src_parent, AuLock_IR);
392 static int au_cpup_or_link(struct dentry *src_dentry, struct au_link_args *a)
396 struct inode *h_inode, *inode;
397 struct dentry *h_src_dentry;
398 struct super_block *sb;
403 sb = src_dentry->d_sb;
404 inode = src_dentry->d_inode;
405 if (au_ibstart(inode) <= a->bdst)
406 h_inode = au_h_iptr(inode, a->bdst);
407 if (!h_inode || !h_inode->i_nlink) {
408 /* copyup src_dentry as the name of dentry. */
409 au_set_dbstart(src_dentry, a->bdst);
410 au_set_h_dptr(src_dentry, a->bdst, dget(a->h_path.dentry));
411 h_inode = au_h_dptr(src_dentry, a->bsrc)->d_inode;
412 mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
413 h_file = au_h_open_pre(src_dentry, a->bsrc);
414 if (IS_ERR(h_file)) {
415 err = PTR_ERR(h_file);
418 err = au_sio_cpup_single(src_dentry, a->bdst, a->bsrc,
421 mutex_unlock(&h_inode->i_mutex);
422 au_h_open_post(src_dentry, a->bsrc, h_file);
423 au_set_h_dptr(src_dentry, a->bdst, NULL);
424 au_set_dbstart(src_dentry, a->bsrc);
426 /* the inode of src_dentry already exists on a.bdst branch */
427 h_src_dentry = d_find_alias(h_inode);
428 if (!h_src_dentry && au_plink_test(inode)) {
430 h_src_dentry = au_plink_lkup(inode, a->bdst);
431 err = PTR_ERR(h_src_dentry);
432 if (IS_ERR(h_src_dentry))
435 if (unlikely(!h_src_dentry->d_inode)) {
442 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
446 AuIOErr("no dentry found for hi%lu on b%d\n",
447 h_inode->i_ino, a->bdst);
453 au_plink_append(inode, a->bdst, a->h_path.dentry);
460 int aufs_link(struct dentry *src_dentry, struct inode *dir,
461 struct dentry *dentry)
465 struct au_link_args *a;
466 struct dentry *wh_dentry, *h_src_dentry;
468 struct super_block *sb;
469 struct au_wr_dir_args wr_dir_args = {
470 /* .force_btgt = -1, */
471 .flags = AuWrDir_ADD_ENTRY
475 inode = src_dentry->d_inode;
479 a = kzalloc(sizeof(*a), GFP_NOFS);
483 a->parent = dentry->d_parent; /* dir inode is locked */
484 err = aufs_read_and_write_lock2(dentry, src_dentry,
485 AuLock_NOPLM | AuLock_GEN);
488 err = au_d_hashed_positive(src_dentry);
491 err = au_d_may_add(dentry);
495 a->src_parent = dget_parent(src_dentry);
496 wr_dir_args.force_btgt = au_ibstart(inode);
498 di_write_lock_parent(a->parent);
499 wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
500 wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
502 err = PTR_ERR(wh_dentry);
503 if (IS_ERR(wh_dentry))
508 a->bdst = au_dbstart(dentry);
509 a->h_path.dentry = au_h_dptr(dentry, a->bdst);
510 a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
511 a->bsrc = au_ibstart(inode);
512 h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
514 a->bsrc = au_dbstart(src_dentry);
515 h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
516 AuDebugOn(!h_src_dentry);
517 } else if (IS_ERR(h_src_dentry))
520 if (au_opt_test(au_mntflags(sb), PLINK)) {
521 if (a->bdst < a->bsrc
522 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
523 err = au_cpup_or_link(src_dentry, a);
525 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
530 * copyup src_dentry to the branch we process,
531 * and then link(2) to it.
534 if (a->bdst < a->bsrc
535 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
537 di_write_unlock(a->parent);
538 err = au_cpup_before_link(src_dentry, a);
539 di_write_lock_parent(a->parent);
541 err = au_pin(&a->pin, dentry, a->bdst,
543 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
548 h_src_dentry = au_h_dptr(src_dentry, a->bdst);
550 if (h_src_dentry && h_src_dentry->d_inode)
551 err = vfsub_link(h_src_dentry,
552 au_pinned_h_dir(&a->pin),
560 a->h_path.dentry = wh_dentry;
561 err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
568 if (au_ibstart(dir) == au_dbstart(dentry))
569 au_cpup_attr_timesizes(dir);
571 inode->i_ctime = dir->i_ctime;
572 d_instantiate(dentry, au_igrab(inode));
573 if (d_unhashed(a->h_path.dentry))
574 /* some filesystem calls d_drop() */
576 goto out_unpin; /* success */
579 rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
580 if (unlikely(rerr)) {
581 AuIOErr("%.*s reverting failed(%d, %d)\n",
582 AuDLNPair(dentry), err, rerr);
585 au_dtime_revert(&dt);
591 di_write_unlock(a->parent);
595 au_update_dbstart(dentry);
598 aufs_read_and_write_unlock2(dentry, src_dentry);
605 int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
608 aufs_bindex_t bindex;
609 unsigned char diropq;
611 struct dentry *wh_dentry, *parent, *opq_dentry;
613 struct super_block *sb;
617 } *a; /* reduce the stack usage */
618 struct au_wr_dir_args wr_dir_args = {
620 .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
626 a = kmalloc(sizeof(*a), GFP_NOFS);
630 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
633 err = au_d_may_add(dentry);
637 parent = dentry->d_parent; /* dir inode is locked */
638 di_write_lock_parent(parent);
639 wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
640 &a->pin, &wr_dir_args);
641 err = PTR_ERR(wh_dentry);
642 if (IS_ERR(wh_dentry))
646 bindex = au_dbstart(dentry);
647 h_path.dentry = au_h_dptr(dentry, bindex);
648 h_path.mnt = au_sbr_mnt(sb, bindex);
649 err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
653 /* make the dir opaque */
655 h_mtx = &h_path.dentry->d_inode->i_mutex;
657 || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
658 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
659 opq_dentry = au_diropq_create(dentry, bindex);
661 err = PTR_ERR(opq_dentry);
662 if (IS_ERR(opq_dentry))
668 err = epilog(dir, bindex, wh_dentry, dentry);
671 goto out_unpin; /* success */
677 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
678 rerr = au_diropq_remove(dentry, bindex);
681 AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
682 AuDLNPair(dentry), err, rerr);
689 rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
691 AuIOErr("%.*s reverting dir failed(%d, %d)\n",
692 AuDLNPair(dentry), err, rerr);
695 au_dtime_revert(&a->dt);
700 di_write_unlock(parent);
703 au_update_dbstart(dentry);
706 aufs_read_unlock(dentry, AuLock_DW);