aufs: debug, from aufs2.2-3.0
[zen-stable.git] / fs / aufs / i_op_add.c
blob8bf18f496e2deccae354a53065df388ce2bc46d2
1 /*
2 * Copyright (C) 2005-2011 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)
23 #include "aufs.h"
26 * final procedure of adding a new entry, except link(2).
27 * remove whiteout, instantiate, copyup the parent dir's times and size
28 * and update version.
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)
34 int err, rerr;
35 aufs_bindex_t bwh;
36 struct path h_path;
37 struct inode *inode, *h_dir;
38 struct dentry *wh;
40 bwh = -1;
41 if (wh_dentry) {
42 h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
43 IMustLock(h_dir);
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,
49 dentry);
50 if (unlikely(err))
51 goto out;
54 inode = au_new_inode(dentry, /*must_new*/1);
55 if (!IS_ERR(inode)) {
56 d_instantiate(dentry, inode);
57 dir = dentry->d_parent->d_inode; /* dir inode is locked */
58 IMustLock(dir);
59 if (au_ibstart(dir) == au_dbstart(dentry))
60 au_cpup_attr_timesizes(dir);
61 dir->i_version++;
62 return 0; /* success */
65 err = PTR_ERR(inode);
66 if (!wh_dentry)
67 goto out;
69 /* revert */
70 /* dir inode is locked */
71 wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
72 rerr = PTR_ERR(wh);
73 if (IS_ERR(wh)) {
74 AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
75 AuDLNPair(dentry), err, rerr);
76 err = -EIO;
77 } else
78 dput(wh);
80 out:
81 return err;
84 static int au_d_may_add(struct dentry *dentry)
86 int err;
88 err = 0;
89 if (unlikely(d_unhashed(dentry)))
90 err = -ENOENT;
91 if (unlikely(dentry->d_inode))
92 err = -EEXIST;
93 return err;
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)
103 int err;
104 umode_t h_mode;
105 struct dentry *h_dentry;
106 struct inode *h_inode;
108 err = -ENAMETOOLONG;
109 if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
110 goto out;
112 h_dentry = au_h_dptr(dentry, bindex);
113 h_inode = h_dentry->d_inode;
114 if (!dentry->d_inode) {
115 err = -EEXIST;
116 if (unlikely(h_inode))
117 goto out;
118 } else {
119 /* rename(2) case */
120 err = -EIO;
121 if (unlikely(!h_inode || !h_inode->i_nlink))
122 goto out;
124 h_mode = h_inode->i_mode;
125 if (!isdir) {
126 err = -EISDIR;
127 if (unlikely(S_ISDIR(h_mode)))
128 goto out;
129 } else if (unlikely(!S_ISDIR(h_mode))) {
130 err = -ENOTDIR;
131 goto out;
135 err = 0;
136 /* expected parent dir is locked */
137 if (unlikely(h_parent != h_dentry->d_parent))
138 err = -EIO;
140 out:
141 AuTraceErr(err);
142 return err;
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;
158 int err;
159 unsigned int udba;
160 aufs_bindex_t bcpup;
162 AuDbg("%.*s\n", AuDLNPair(dentry));
164 err = au_wr_dir(dentry, src_dentry, wr_dir_args);
165 bcpup = err;
166 wh_dentry = ERR_PTR(err);
167 if (unlikely(err < 0))
168 goto out;
170 sb = dentry->d_sb;
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);
175 if (unlikely(err))
176 goto out;
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))
184 err = -ENAMETOOLONG;
185 wh_dentry = ERR_PTR(err);
186 if (unlikely(err))
187 goto out_unpin;
189 br = au_sbr(sb, bcpup);
190 if (dt) {
191 struct path tmp = {
192 .dentry = h_parent,
193 .mnt = br->br_mnt
195 au_dtime_store(dt, au_pinned_parent(pin), &tmp);
198 wh_dentry = NULL;
199 if (bcpup != au_dbwh(dentry))
200 goto out; /* success */
202 wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
204 out_unpin:
205 if (IS_ERR(wh_dentry))
206 au_unpin(pin);
207 out:
208 return wh_dentry;
211 /* ---------------------------------------------------------------------- */
213 enum { Mknod, Symlink, Creat };
214 struct simple_arg {
215 int type;
216 union {
217 struct {
218 int mode;
219 struct nameidata *nd;
220 } c;
221 struct {
222 const char *symname;
223 } s;
224 struct {
225 int mode;
226 dev_t dev;
227 } m;
228 } u;
231 static int add_simple(struct inode *dir, struct dentry *dentry,
232 struct simple_arg *arg)
234 int err;
235 aufs_bindex_t bstart;
236 unsigned char created;
237 struct au_dtime dt;
238 struct au_pin pin;
239 struct path h_path;
240 struct dentry *wh_dentry, *parent;
241 struct inode *h_dir;
242 struct au_wr_dir_args wr_dir_args = {
243 .force_btgt = -1,
244 .flags = AuWrDir_ADD_ENTRY
247 AuDbg("%.*s\n", AuDLNPair(dentry));
248 IMustLock(dir);
250 parent = dentry->d_parent; /* dir inode is locked */
251 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
252 if (unlikely(err))
253 goto out;
254 err = au_d_may_add(dentry);
255 if (unlikely(err))
256 goto out_unlock;
257 di_write_lock_parent(parent);
258 wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL, &pin,
259 &wr_dir_args);
260 err = PTR_ERR(wh_dentry);
261 if (IS_ERR(wh_dentry))
262 goto out_parent;
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);
268 switch (arg->type) {
269 case Creat:
270 err = vfsub_create(h_dir, &h_path, arg->u.c.mode);
271 break;
272 case Symlink:
273 err = vfsub_symlink(h_dir, &h_path, arg->u.s.symname);
274 break;
275 case Mknod:
276 err = vfsub_mknod(h_dir, &h_path, arg->u.m.mode, arg->u.m.dev);
277 break;
278 default:
279 BUG();
281 created = !err;
282 if (!err)
283 err = epilog(dir, bstart, wh_dentry, dentry);
285 /* revert */
286 if (unlikely(created && err && h_path.dentry->d_inode)) {
287 int rerr;
288 rerr = vfsub_unlink(h_dir, &h_path, /*force*/0);
289 if (rerr) {
290 AuIOErr("%.*s revert failure(%d, %d)\n",
291 AuDLNPair(dentry), err, rerr);
292 err = -EIO;
294 au_dtime_revert(&dt);
297 au_unpin(&pin);
298 dput(wh_dentry);
300 out_parent:
301 di_write_unlock(parent);
302 out_unlock:
303 if (unlikely(err)) {
304 au_update_dbstart(dentry);
305 d_drop(dentry);
307 aufs_read_unlock(dentry, AuLock_DW);
308 out:
309 return err;
312 int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
314 struct simple_arg arg = {
315 .type = Mknod,
316 .u.m = {
317 .mode = mode,
318 .dev = dev
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 = {
327 .type = Symlink,
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 = {
337 .type = Creat,
338 .u.c = {
339 .mode = mode,
340 .nd = nd
343 return add_simple(dir, dentry, &arg);
346 /* ---------------------------------------------------------------------- */
348 struct au_link_args {
349 aufs_bindex_t bdst, bsrc;
350 struct au_pin pin;
351 struct path h_path;
352 struct dentry *src_parent, *parent;
355 static int au_cpup_before_link(struct dentry *src_dentry,
356 struct au_link_args *a)
358 int err;
359 struct dentry *h_src_dentry;
360 struct mutex *h_mtx;
361 struct file *h_file;
363 di_read_lock_parent(a->src_parent, AuLock_IR);
364 err = au_test_and_cpup_dirs(src_dentry, a->bdst);
365 if (unlikely(err))
366 goto out;
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);
373 if (unlikely(err))
374 goto out;
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);
379 h_file = NULL;
380 } else
381 err = au_sio_cpup_simple(src_dentry, a->bdst, a->bsrc,
382 AuCpup_DTIME /* | AuCpup_KEEPLINO */);
383 mutex_unlock(h_mtx);
384 au_h_open_post(src_dentry, a->bsrc, h_file);
385 au_unpin(&a->pin);
387 out:
388 di_read_unlock(a->src_parent, AuLock_IR);
389 return err;
392 static int au_cpup_or_link(struct dentry *src_dentry, struct au_link_args *a)
394 int err;
395 unsigned char plink;
396 struct inode *h_inode, *inode;
397 struct dentry *h_src_dentry;
398 struct super_block *sb;
399 struct file *h_file;
401 plink = 0;
402 h_inode = NULL;
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);
416 h_file = NULL;
417 } else
418 err = au_sio_cpup_single(src_dentry, a->bdst, a->bsrc,
419 -1, AuCpup_KEEPLINO,
420 a->parent);
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);
425 } else {
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)) {
429 plink = 1;
430 h_src_dentry = au_plink_lkup(inode, a->bdst);
431 err = PTR_ERR(h_src_dentry);
432 if (IS_ERR(h_src_dentry))
433 goto out;
435 if (unlikely(!h_src_dentry->d_inode)) {
436 dput(h_src_dentry);
437 h_src_dentry = NULL;
441 if (h_src_dentry) {
442 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
443 &a->h_path);
444 dput(h_src_dentry);
445 } else {
446 AuIOErr("no dentry found for hi%lu on b%d\n",
447 h_inode->i_ino, a->bdst);
448 err = -EIO;
452 if (!err && !plink)
453 au_plink_append(inode, a->bdst, a->h_path.dentry);
455 out:
456 AuTraceErr(err);
457 return err;
460 int aufs_link(struct dentry *src_dentry, struct inode *dir,
461 struct dentry *dentry)
463 int err, rerr;
464 struct au_dtime dt;
465 struct au_link_args *a;
466 struct dentry *wh_dentry, *h_src_dentry;
467 struct inode *inode;
468 struct super_block *sb;
469 struct au_wr_dir_args wr_dir_args = {
470 /* .force_btgt = -1, */
471 .flags = AuWrDir_ADD_ENTRY
474 IMustLock(dir);
475 inode = src_dentry->d_inode;
476 IMustLock(inode);
478 err = -ENOMEM;
479 a = kzalloc(sizeof(*a), GFP_NOFS);
480 if (unlikely(!a))
481 goto out;
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);
486 if (unlikely(err))
487 goto out_kfree;
488 err = au_d_hashed_positive(src_dentry);
489 if (unlikely(err))
490 goto out_unlock;
491 err = au_d_may_add(dentry);
492 if (unlikely(err))
493 goto out_unlock;
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,
501 &wr_dir_args);
502 err = PTR_ERR(wh_dentry);
503 if (IS_ERR(wh_dentry))
504 goto out_parent;
506 err = 0;
507 sb = dentry->d_sb;
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);
513 if (!h_src_dentry) {
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))
518 goto out_parent;
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);
524 else
525 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
526 &a->h_path);
527 dput(h_src_dentry);
528 } else {
530 * copyup src_dentry to the branch we process,
531 * and then link(2) to it.
533 dput(h_src_dentry);
534 if (a->bdst < a->bsrc
535 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
536 au_unpin(&a->pin);
537 di_write_unlock(a->parent);
538 err = au_cpup_before_link(src_dentry, a);
539 di_write_lock_parent(a->parent);
540 if (!err)
541 err = au_pin(&a->pin, dentry, a->bdst,
542 au_opt_udba(sb),
543 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
544 if (unlikely(err))
545 goto out_wh;
547 if (!err) {
548 h_src_dentry = au_h_dptr(src_dentry, a->bdst);
549 err = -ENOENT;
550 if (h_src_dentry && h_src_dentry->d_inode)
551 err = vfsub_link(h_src_dentry,
552 au_pinned_h_dir(&a->pin),
553 &a->h_path);
556 if (unlikely(err))
557 goto out_unpin;
559 if (wh_dentry) {
560 a->h_path.dentry = wh_dentry;
561 err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
562 dentry);
563 if (unlikely(err))
564 goto out_revert;
567 dir->i_version++;
568 if (au_ibstart(dir) == au_dbstart(dentry))
569 au_cpup_attr_timesizes(dir);
570 inc_nlink(inode);
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() */
575 d_drop(dentry);
576 goto out_unpin; /* success */
578 out_revert:
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);
583 err = -EIO;
585 au_dtime_revert(&dt);
586 out_unpin:
587 au_unpin(&a->pin);
588 out_wh:
589 dput(wh_dentry);
590 out_parent:
591 di_write_unlock(a->parent);
592 dput(a->src_parent);
593 out_unlock:
594 if (unlikely(err)) {
595 au_update_dbstart(dentry);
596 d_drop(dentry);
598 aufs_read_and_write_unlock2(dentry, src_dentry);
599 out_kfree:
600 kfree(a);
601 out:
602 return err;
605 int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
607 int err, rerr;
608 aufs_bindex_t bindex;
609 unsigned char diropq;
610 struct path h_path;
611 struct dentry *wh_dentry, *parent, *opq_dentry;
612 struct mutex *h_mtx;
613 struct super_block *sb;
614 struct {
615 struct au_pin pin;
616 struct au_dtime dt;
617 } *a; /* reduce the stack usage */
618 struct au_wr_dir_args wr_dir_args = {
619 .force_btgt = -1,
620 .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
623 IMustLock(dir);
625 err = -ENOMEM;
626 a = kmalloc(sizeof(*a), GFP_NOFS);
627 if (unlikely(!a))
628 goto out;
630 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
631 if (unlikely(err))
632 goto out_free;
633 err = au_d_may_add(dentry);
634 if (unlikely(err))
635 goto out_unlock;
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))
643 goto out_parent;
645 sb = dentry->d_sb;
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);
650 if (unlikely(err))
651 goto out_unpin;
653 /* make the dir opaque */
654 diropq = 0;
655 h_mtx = &h_path.dentry->d_inode->i_mutex;
656 if (wh_dentry
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);
660 mutex_unlock(h_mtx);
661 err = PTR_ERR(opq_dentry);
662 if (IS_ERR(opq_dentry))
663 goto out_dir;
664 dput(opq_dentry);
665 diropq = 1;
668 err = epilog(dir, bindex, wh_dentry, dentry);
669 if (!err) {
670 inc_nlink(dir);
671 goto out_unpin; /* success */
674 /* revert */
675 if (diropq) {
676 AuLabel(revert opq);
677 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
678 rerr = au_diropq_remove(dentry, bindex);
679 mutex_unlock(h_mtx);
680 if (rerr) {
681 AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
682 AuDLNPair(dentry), err, rerr);
683 err = -EIO;
687 out_dir:
688 AuLabel(revert dir);
689 rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
690 if (rerr) {
691 AuIOErr("%.*s reverting dir failed(%d, %d)\n",
692 AuDLNPair(dentry), err, rerr);
693 err = -EIO;
695 au_dtime_revert(&a->dt);
696 out_unpin:
697 au_unpin(&a->pin);
698 dput(wh_dentry);
699 out_parent:
700 di_write_unlock(parent);
701 out_unlock:
702 if (unlikely(err)) {
703 au_update_dbstart(dentry);
704 d_drop(dentry);
706 aufs_read_unlock(dentry, AuLock_DW);
707 out_free:
708 kfree(a);
709 out:
710 return err;