]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/aufs/i_op_add.c
UBUNTU: SAUCE: Revert "audit: fix auditd/kernel connection state tracking"
[mirror_ubuntu-zesty-kernel.git] / fs / aufs / i_op_add.c
CommitLineData
e14748e8
SF
1/*
2 * Copyright (C) 2005-2016 Junjiro R. Okajima
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18/*
19 * inode operations (add entry)
20 */
21
22#include "aufs.h"
23
24/*
25 * final procedure of adding a new entry, except link(2).
26 * remove whiteout, instantiate, copyup the parent dir's times and size
27 * and update version.
28 * if it failed, re-create the removed whiteout.
29 */
30static int epilog(struct inode *dir, aufs_bindex_t bindex,
31 struct dentry *wh_dentry, struct dentry *dentry)
32{
33 int err, rerr;
34 aufs_bindex_t bwh;
35 struct path h_path;
36 struct super_block *sb;
37 struct inode *inode, *h_dir;
38 struct dentry *wh;
39
40 bwh = -1;
41 sb = dir->i_sb;
42 if (wh_dentry) {
43 h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
44 IMustLock(h_dir);
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,
50 dentry);
51 if (unlikely(err))
52 goto out;
53 }
54
55 inode = au_new_inode(dentry, /*must_new*/1);
56 if (!IS_ERR(inode)) {
57 d_instantiate(dentry, inode);
58 dir = d_inode(dentry->d_parent); /* dir inode is locked */
59 IMustLock(dir);
60 au_dir_ts(dir, bindex);
61 dir->i_version++;
62 au_fhsm_wrote(sb, bindex, /*force*/0);
63 return 0; /* success */
64 }
65
66 err = PTR_ERR(inode);
67 if (!wh_dentry)
68 goto out;
69
70 /* revert */
71 /* dir inode is locked */
72 wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
73 rerr = PTR_ERR(wh);
74 if (IS_ERR(wh)) {
75 AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
76 dentry, err, rerr);
77 err = -EIO;
78 } else
79 dput(wh);
80
81out:
82 return err;
83}
84
85static int au_d_may_add(struct dentry *dentry)
86{
87 int err;
88
89 err = 0;
90 if (unlikely(d_unhashed(dentry)))
91 err = -ENOENT;
92 if (unlikely(d_really_is_positive(dentry)))
93 err = -EEXIST;
94 return err;
95}
96
97/*
98 * simple tests for the adding inode operations.
99 * following the checks in vfs, plus the parent-child relationship.
100 */
101int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
102 struct dentry *h_parent, int isdir)
103{
104 int err;
105 umode_t h_mode;
106 struct dentry *h_dentry;
107 struct inode *h_inode;
108
109 err = -ENAMETOOLONG;
110 if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
111 goto out;
112
113 h_dentry = au_h_dptr(dentry, bindex);
114 if (d_really_is_negative(dentry)) {
115 err = -EEXIST;
116 if (unlikely(d_is_positive(h_dentry)))
117 goto out;
118 } else {
119 /* rename(2) case */
120 err = -EIO;
121 if (unlikely(d_is_negative(h_dentry)))
122 goto out;
123 h_inode = d_inode(h_dentry);
124 if (unlikely(!h_inode->i_nlink))
125 goto out;
126
127 h_mode = h_inode->i_mode;
128 if (!isdir) {
129 err = -EISDIR;
130 if (unlikely(S_ISDIR(h_mode)))
131 goto out;
132 } else if (unlikely(!S_ISDIR(h_mode))) {
133 err = -ENOTDIR;
134 goto out;
135 }
136 }
137
138 err = 0;
139 /* expected parent dir is locked */
140 if (unlikely(h_parent != h_dentry->d_parent))
141 err = -EIO;
142
143out:
144 AuTraceErr(err);
145 return err;
146}
147
148/*
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.
152 */
153static struct dentry*
154lock_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)
157{
158 struct dentry *wh_dentry, *h_parent;
159 struct super_block *sb;
160 struct au_branch *br;
161 int err;
162 unsigned int udba;
163 aufs_bindex_t bcpup;
164
165 AuDbg("%pd\n", dentry);
166
167 err = au_wr_dir(dentry, src_dentry, wr_dir_args);
168 bcpup = err;
169 wh_dentry = ERR_PTR(err);
170 if (unlikely(err < 0))
171 goto out;
172
173 sb = dentry->d_sb;
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);
178 if (unlikely(err))
179 goto out;
180
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))
187 err = -ENAMETOOLONG;
188 wh_dentry = ERR_PTR(err);
189 if (unlikely(err))
190 goto out_unpin;
191
192 br = au_sbr(sb, bcpup);
193 if (dt) {
194 struct path tmp = {
195 .dentry = h_parent,
196 .mnt = au_br_mnt(br)
197 };
198 au_dtime_store(dt, au_pinned_parent(pin), &tmp);
199 }
200
201 wh_dentry = NULL;
202 if (bcpup != au_dbwh(dentry))
203 goto out; /* success */
204
205 /*
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.
209 */
210 wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
211
212out_unpin:
213 if (IS_ERR(wh_dentry))
214 au_unpin(pin);
215out:
216 return wh_dentry;
217}
218
219/* ---------------------------------------------------------------------- */
220
221enum { Mknod, Symlink, Creat };
222struct simple_arg {
223 int type;
224 union {
225 struct {
226 umode_t mode;
227 bool want_excl;
228 bool try_aopen;
229 struct vfsub_aopen_args *aopen;
230 } c;
231 struct {
232 const char *symname;
233 } s;
234 struct {
235 umode_t mode;
236 dev_t dev;
237 } m;
238 } u;
239};
240
241static int add_simple(struct inode *dir, struct dentry *dentry,
242 struct simple_arg *arg)
243{
244 int err, rerr;
245 aufs_bindex_t btop;
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;
250 struct inode *h_dir;
251 struct super_block *sb;
252 struct au_branch *br;
253 /* to reuduce stack size */
254 struct {
255 struct au_dtime dt;
256 struct au_pin pin;
257 struct path h_path;
258 struct au_wr_dir_args wr_dir_args;
259 } *a;
260
261 AuDbg("%pd\n", dentry);
262 IMustLock(dir);
263
264 err = -ENOMEM;
265 a = kmalloc(sizeof(*a), GFP_NOFS);
266 if (unlikely(!a))
267 goto out;
268 a->wr_dir_args.force_btgt = -1;
269 a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
270
271 parent = dentry->d_parent; /* dir inode is locked */
272 if (!try_aopen) {
273 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
274 if (unlikely(err))
275 goto out_free;
276 }
277 err = au_d_may_add(dentry);
278 if (unlikely(err))
279 goto out_unlock;
280 if (!try_aopen)
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))
286 goto out_parent;
287
288 btop = au_dbtop(dentry);
289 sb = dentry->d_sb;
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);
294 switch (arg->type) {
295 case Creat:
296 err = 0;
297 if (!try_aopen || !h_dir->i_op->atomic_open)
298 err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
299 arg->u.c.want_excl);
300 else
301 err = vfsub_atomic_open(h_dir, a->h_path.dentry,
302 arg->u.c.aopen, br);
303 break;
304 case Symlink:
305 err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
306 break;
307 case Mknod:
308 err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
309 arg->u.m.dev);
310 break;
311 default:
312 BUG();
313 }
314 created = !err;
315 if (!err)
316 err = epilog(dir, btop, wh_dentry, dentry);
317
318 /* revert */
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,
322 /*force*/0);
323 if (rerr) {
324 AuIOErr("%pd revert failure(%d, %d)\n",
325 dentry, err, rerr);
326 err = -EIO;
327 }
328 au_dtime_revert(&a->dt);
329 }
330
331 if (!err && try_aopen && !h_dir->i_op->atomic_open)
332 *arg->u.c.aopen->opened |= FILE_CREATED;
333
334 au_unpin(&a->pin);
335 dput(wh_dentry);
336
337out_parent:
338 if (!try_aopen)
339 di_write_unlock(parent);
340out_unlock:
341 if (unlikely(err)) {
342 au_update_dbtop(dentry);
343 d_drop(dentry);
344 }
345 if (!try_aopen)
346 aufs_read_unlock(dentry, AuLock_DW);
347out_free:
348 au_delayed_kfree(a);
349out:
350 return err;
351}
352
353int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
354 dev_t dev)
355{
356 struct simple_arg arg = {
357 .type = Mknod,
358 .u.m = {
359 .mode = mode,
360 .dev = dev
361 }
362 };
363 return add_simple(dir, dentry, &arg);
364}
365
366int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
367{
368 struct simple_arg arg = {
369 .type = Symlink,
370 .u.s.symname = symname
371 };
372 return add_simple(dir, dentry, &arg);
373}
374
375int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
376 bool want_excl)
377{
378 struct simple_arg arg = {
379 .type = Creat,
380 .u.c = {
381 .mode = mode,
382 .want_excl = want_excl
383 }
384 };
385 return add_simple(dir, dentry, &arg);
386}
387
388int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
389 struct vfsub_aopen_args *aopen_args)
390{
391 struct simple_arg arg = {
392 .type = Creat,
393 .u.c = {
394 .mode = aopen_args->create_mode,
395 .want_excl = aopen_args->open_flag & O_EXCL,
396 .try_aopen = true,
397 .aopen = aopen_args
398 }
399 };
400 return add_simple(dir, dentry, &arg);
401}
402
403int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
404{
405 int err;
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 = {
412 .force_btgt = -1,
413 .flags = AuWrDir_TMPFILE
414 };
415
416 /* copy-up may happen */
417 inode_lock(dir);
418
419 sb = dir->i_sb;
420 err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
421 if (unlikely(err))
422 goto out;
423
424 err = au_di_init(dentry);
425 if (unlikely(err))
426 goto out_si;
427
428 err = -EBUSY;
429 parent = d_find_any_alias(dir);
430 AuDebugOn(!parent);
431 di_write_lock_parent(parent);
432 if (unlikely(d_inode(parent) != dir))
433 goto out_parent;
434
435 err = au_digen_test(parent, au_sigen(sb));
436 if (unlikely(err))
437 goto out_parent;
438
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);
443 bindex = err;
444 if (unlikely(err < 0))
445 goto out_parent;
446
447 err = -EOPNOTSUPP;
448 h_dir = au_h_iptr(dir, bindex);
449 if (unlikely(!h_dir->i_op->tmpfile))
450 goto out_parent;
451
452 h_mnt = au_sbr_mnt(sb, bindex);
453 err = vfsub_mnt_want_write(h_mnt);
454 if (unlikely(err))
455 goto out_parent;
456
457 h_parent = au_h_dptr(parent, bindex);
458 err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
459 if (unlikely(err))
460 goto out_mnt;
461
462 err = -ENOMEM;
463 h_dentry = d_alloc(h_parent, &dentry->d_name);
464 if (unlikely(!h_dentry))
465 goto out_mnt;
466
467 err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
468 if (unlikely(err))
469 goto out_dentry;
470
471 au_set_dbtop(dentry, bindex);
472 au_set_dbbot(dentry, bindex);
473 au_set_h_dptr(dentry, bindex, dget(h_dentry));
474 inode = au_new_inode(dentry, /*must_new*/1);
475 if (IS_ERR(inode)) {
476 err = PTR_ERR(inode);
477 au_set_h_dptr(dentry, bindex, NULL);
478 au_set_dbtop(dentry, -1);
479 au_set_dbbot(dentry, -1);
480 } else {
481 if (!inode->i_nlink)
482 set_nlink(inode, 1);
483 d_tmpfile(dentry, inode);
484 au_di(dentry)->di_tmpfile = 1;
485
486 /* update without i_mutex */
487 if (au_ibtop(dir) == au_dbtop(dentry))
488 au_cpup_attr_timesizes(dir);
489 }
490
491out_dentry:
492 dput(h_dentry);
493out_mnt:
494 vfsub_mnt_drop_write(h_mnt);
495out_parent:
496 di_write_unlock(parent);
497 dput(parent);
498 di_write_unlock(dentry);
499 if (unlikely(err)) {
500 au_di_fin(dentry);
501 dentry->d_fsdata = NULL;
502 }
503out_si:
504 si_read_unlock(sb);
505out:
506 inode_unlock(dir);
507 return err;
508}
509
510/* ---------------------------------------------------------------------- */
511
512struct au_link_args {
513 aufs_bindex_t bdst, bsrc;
514 struct au_pin pin;
515 struct path h_path;
516 struct dentry *src_parent, *parent;
517};
518
519static int au_cpup_before_link(struct dentry *src_dentry,
520 struct au_link_args *a)
521{
522 int err;
523 struct dentry *h_src_dentry;
524 struct au_cp_generic cpg = {
525 .dentry = src_dentry,
526 .bdst = a->bdst,
527 .bsrc = a->bsrc,
528 .len = -1,
529 .pin = &a->pin,
530 .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
531 };
532
533 di_read_lock_parent(a->src_parent, AuLock_IR);
534 err = au_test_and_cpup_dirs(src_dentry, a->bdst);
535 if (unlikely(err))
536 goto out;
537
538 h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
539 err = au_pin(&a->pin, src_dentry, a->bdst,
540 au_opt_udba(src_dentry->d_sb),
541 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
542 if (unlikely(err))
543 goto out;
544
545 err = au_sio_cpup_simple(&cpg);
546 au_unpin(&a->pin);
547
548out:
549 di_read_unlock(a->src_parent, AuLock_IR);
550 return err;
551}
552
553static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
554 struct au_link_args *a)
555{
556 int err;
557 unsigned char plink;
558 aufs_bindex_t bbot;
559 struct dentry *h_src_dentry;
560 struct inode *h_inode, *inode, *delegated;
561 struct super_block *sb;
562 struct file *h_file;
563
564 plink = 0;
565 h_inode = NULL;
566 sb = src_dentry->d_sb;
567 inode = d_inode(src_dentry);
568 if (au_ibtop(inode) <= a->bdst)
569 h_inode = au_h_iptr(inode, a->bdst);
570 if (!h_inode || !h_inode->i_nlink) {
571 /* copyup src_dentry as the name of dentry. */
572 bbot = au_dbbot(dentry);
573 if (bbot < a->bsrc)
574 au_set_dbbot(dentry, a->bsrc);
575 au_set_h_dptr(dentry, a->bsrc,
576 dget(au_h_dptr(src_dentry, a->bsrc)));
577 dget(a->h_path.dentry);
578 au_set_h_dptr(dentry, a->bdst, NULL);
579 AuDbg("temporary d_inode...\n");
580 spin_lock(&dentry->d_lock);
581 dentry->d_inode = d_inode(src_dentry); /* tmp */
582 spin_unlock(&dentry->d_lock);
583 h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
584 if (IS_ERR(h_file))
585 err = PTR_ERR(h_file);
586 else {
587 struct au_cp_generic cpg = {
588 .dentry = dentry,
589 .bdst = a->bdst,
590 .bsrc = -1,
591 .len = -1,
592 .pin = &a->pin,
593 .flags = AuCpup_KEEPLINO
594 };
595 err = au_sio_cpup_simple(&cpg);
596 au_h_open_post(dentry, a->bsrc, h_file);
597 if (!err) {
598 dput(a->h_path.dentry);
599 a->h_path.dentry = au_h_dptr(dentry, a->bdst);
600 } else
601 au_set_h_dptr(dentry, a->bdst,
602 a->h_path.dentry);
603 }
604 spin_lock(&dentry->d_lock);
605 dentry->d_inode = NULL; /* restore */
606 spin_unlock(&dentry->d_lock);
607 AuDbg("temporary d_inode...done\n");
608 au_set_h_dptr(dentry, a->bsrc, NULL);
609 au_set_dbbot(dentry, bbot);
610 } else {
611 /* the inode of src_dentry already exists on a.bdst branch */
612 h_src_dentry = d_find_alias(h_inode);
613 if (!h_src_dentry && au_plink_test(inode)) {
614 plink = 1;
615 h_src_dentry = au_plink_lkup(inode, a->bdst);
616 err = PTR_ERR(h_src_dentry);
617 if (IS_ERR(h_src_dentry))
618 goto out;
619
620 if (unlikely(d_is_negative(h_src_dentry))) {
621 dput(h_src_dentry);
622 h_src_dentry = NULL;
623 }
624
625 }
626 if (h_src_dentry) {
627 delegated = NULL;
628 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
629 &a->h_path, &delegated);
630 if (unlikely(err == -EWOULDBLOCK)) {
631 pr_warn("cannot retry for NFSv4 delegation"
632 " for an internal link\n");
633 iput(delegated);
634 }
635 dput(h_src_dentry);
636 } else {
637 AuIOErr("no dentry found for hi%lu on b%d\n",
638 h_inode->i_ino, a->bdst);
639 err = -EIO;
640 }
641 }
642
643 if (!err && !plink)
644 au_plink_append(inode, a->bdst, a->h_path.dentry);
645
646out:
647 AuTraceErr(err);
648 return err;
649}
650
651int aufs_link(struct dentry *src_dentry, struct inode *dir,
652 struct dentry *dentry)
653{
654 int err, rerr;
655 struct au_dtime dt;
656 struct au_link_args *a;
657 struct dentry *wh_dentry, *h_src_dentry;
658 struct inode *inode, *delegated;
659 struct super_block *sb;
660 struct au_wr_dir_args wr_dir_args = {
661 /* .force_btgt = -1, */
662 .flags = AuWrDir_ADD_ENTRY
663 };
664
665 IMustLock(dir);
666 inode = d_inode(src_dentry);
667 IMustLock(inode);
668
669 err = -ENOMEM;
670 a = kzalloc(sizeof(*a), GFP_NOFS);
671 if (unlikely(!a))
672 goto out;
673
674 a->parent = dentry->d_parent; /* dir inode is locked */
675 err = aufs_read_and_write_lock2(dentry, src_dentry,
676 AuLock_NOPLM | AuLock_GEN);
677 if (unlikely(err))
678 goto out_kfree;
679 err = au_d_linkable(src_dentry);
680 if (unlikely(err))
681 goto out_unlock;
682 err = au_d_may_add(dentry);
683 if (unlikely(err))
684 goto out_unlock;
685
686 a->src_parent = dget_parent(src_dentry);
687 wr_dir_args.force_btgt = au_ibtop(inode);
688
689 di_write_lock_parent(a->parent);
690 wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
691 wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
692 &wr_dir_args);
693 err = PTR_ERR(wh_dentry);
694 if (IS_ERR(wh_dentry))
695 goto out_parent;
696
697 err = 0;
698 sb = dentry->d_sb;
699 a->bdst = au_dbtop(dentry);
700 a->h_path.dentry = au_h_dptr(dentry, a->bdst);
701 a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
702 a->bsrc = au_ibtop(inode);
703 h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
704 if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
705 h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
706 if (!h_src_dentry) {
707 a->bsrc = au_dbtop(src_dentry);
708 h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
709 AuDebugOn(!h_src_dentry);
710 } else if (IS_ERR(h_src_dentry)) {
711 err = PTR_ERR(h_src_dentry);
712 goto out_parent;
713 }
714
715 /*
716 * aufs doesn't touch the credential so
717 * security_dentry_create_files_as() is unnecrssary.
718 */
719 if (au_opt_test(au_mntflags(sb), PLINK)) {
720 if (a->bdst < a->bsrc
721 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
722 err = au_cpup_or_link(src_dentry, dentry, a);
723 else {
724 delegated = NULL;
725 err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
726 &a->h_path, &delegated);
727 if (unlikely(err == -EWOULDBLOCK)) {
728 pr_warn("cannot retry for NFSv4 delegation"
729 " for an internal link\n");
730 iput(delegated);
731 }
732 }
733 dput(h_src_dentry);
734 } else {
735 /*
736 * copyup src_dentry to the branch we process,
737 * and then link(2) to it.
738 */
739 dput(h_src_dentry);
740 if (a->bdst < a->bsrc
741 /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
742 au_unpin(&a->pin);
743 di_write_unlock(a->parent);
744 err = au_cpup_before_link(src_dentry, a);
745 di_write_lock_parent(a->parent);
746 if (!err)
747 err = au_pin(&a->pin, dentry, a->bdst,
748 au_opt_udba(sb),
749 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
750 if (unlikely(err))
751 goto out_wh;
752 }
753 if (!err) {
754 h_src_dentry = au_h_dptr(src_dentry, a->bdst);
755 err = -ENOENT;
756 if (h_src_dentry && d_is_positive(h_src_dentry)) {
757 delegated = NULL;
758 err = vfsub_link(h_src_dentry,
759 au_pinned_h_dir(&a->pin),
760 &a->h_path, &delegated);
761 if (unlikely(err == -EWOULDBLOCK)) {
762 pr_warn("cannot retry"
763 " for NFSv4 delegation"
764 " for an internal link\n");
765 iput(delegated);
766 }
767 }
768 }
769 }
770 if (unlikely(err))
771 goto out_unpin;
772
773 if (wh_dentry) {
774 a->h_path.dentry = wh_dentry;
775 err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
776 dentry);
777 if (unlikely(err))
778 goto out_revert;
779 }
780
781 au_dir_ts(dir, a->bdst);
782 dir->i_version++;
783 inc_nlink(inode);
784 inode->i_ctime = dir->i_ctime;
785 d_instantiate(dentry, au_igrab(inode));
786 if (d_unhashed(a->h_path.dentry))
787 /* some filesystem calls d_drop() */
788 d_drop(dentry);
789 /* some filesystems consume an inode even hardlink */
790 au_fhsm_wrote(sb, a->bdst, /*force*/0);
791 goto out_unpin; /* success */
792
793out_revert:
794 /* no delegation since it is just created */
795 rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
796 /*delegated*/NULL, /*force*/0);
797 if (unlikely(rerr)) {
798 AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
799 err = -EIO;
800 }
801 au_dtime_revert(&dt);
802out_unpin:
803 au_unpin(&a->pin);
804out_wh:
805 dput(wh_dentry);
806out_parent:
807 di_write_unlock(a->parent);
808 dput(a->src_parent);
809out_unlock:
810 if (unlikely(err)) {
811 au_update_dbtop(dentry);
812 d_drop(dentry);
813 }
814 aufs_read_and_write_unlock2(dentry, src_dentry);
815out_kfree:
816 au_delayed_kfree(a);
817out:
818 AuTraceErr(err);
819 return err;
820}
821
822int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
823{
824 int err, rerr;
825 aufs_bindex_t bindex;
826 unsigned char diropq;
827 struct path h_path;
828 struct dentry *wh_dentry, *parent, *opq_dentry;
829 struct inode *h_inode;
830 struct super_block *sb;
831 struct {
832 struct au_pin pin;
833 struct au_dtime dt;
834 } *a; /* reduce the stack usage */
835 struct au_wr_dir_args wr_dir_args = {
836 .force_btgt = -1,
837 .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
838 };
839
840 IMustLock(dir);
841
842 err = -ENOMEM;
843 a = kmalloc(sizeof(*a), GFP_NOFS);
844 if (unlikely(!a))
845 goto out;
846
847 err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
848 if (unlikely(err))
849 goto out_free;
850 err = au_d_may_add(dentry);
851 if (unlikely(err))
852 goto out_unlock;
853
854 parent = dentry->d_parent; /* dir inode is locked */
855 di_write_lock_parent(parent);
856 wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
857 &a->pin, &wr_dir_args);
858 err = PTR_ERR(wh_dentry);
859 if (IS_ERR(wh_dentry))
860 goto out_parent;
861
862 sb = dentry->d_sb;
863 bindex = au_dbtop(dentry);
864 h_path.dentry = au_h_dptr(dentry, bindex);
865 h_path.mnt = au_sbr_mnt(sb, bindex);
866 err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
867 if (unlikely(err))
868 goto out_unpin;
869
870 /* make the dir opaque */
871 diropq = 0;
872 h_inode = d_inode(h_path.dentry);
873 if (wh_dentry
874 || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
875 inode_lock_nested(h_inode, AuLsc_I_CHILD);
876 opq_dentry = au_diropq_create(dentry, bindex);
877 inode_unlock(h_inode);
878 err = PTR_ERR(opq_dentry);
879 if (IS_ERR(opq_dentry))
880 goto out_dir;
881 dput(opq_dentry);
882 diropq = 1;
883 }
884
885 err = epilog(dir, bindex, wh_dentry, dentry);
886 if (!err) {
887 inc_nlink(dir);
888 goto out_unpin; /* success */
889 }
890
891 /* revert */
892 if (diropq) {
893 AuLabel(revert opq);
894 inode_lock_nested(h_inode, AuLsc_I_CHILD);
895 rerr = au_diropq_remove(dentry, bindex);
896 inode_unlock(h_inode);
897 if (rerr) {
898 AuIOErr("%pd reverting diropq failed(%d, %d)\n",
899 dentry, err, rerr);
900 err = -EIO;
901 }
902 }
903
904out_dir:
905 AuLabel(revert dir);
906 rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
907 if (rerr) {
908 AuIOErr("%pd reverting dir failed(%d, %d)\n",
909 dentry, err, rerr);
910 err = -EIO;
911 }
912 au_dtime_revert(&a->dt);
913out_unpin:
914 au_unpin(&a->pin);
915 dput(wh_dentry);
916out_parent:
917 di_write_unlock(parent);
918out_unlock:
919 if (unlikely(err)) {
920 au_update_dbtop(dentry);
921 d_drop(dentry);
922 }
923 aufs_read_unlock(dentry, AuLock_DW);
924out_free:
925 au_delayed_kfree(a);
926out:
927 return err;
928}