]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/aufs/whout.c
UBUNTU: SAUCE: Import aufs driver
[mirror_ubuntu-bionic-kernel.git] / fs / aufs / whout.c
1 /*
2 * Copyright (C) 2005-2017 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 * whiteout for logical deletion and opaque directory
20 */
21
22 #include "aufs.h"
23
24 #define WH_MASK S_IRUGO
25
26 /*
27 * If a directory contains this file, then it is opaque. We start with the
28 * .wh. flag so that it is blocked by lookup.
29 */
30 static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
31 sizeof(AUFS_WH_DIROPQ) - 1);
32
33 /*
34 * generate whiteout name, which is NOT terminated by NULL.
35 * @name: original d_name.name
36 * @len: original d_name.len
37 * @wh: whiteout qstr
38 * returns zero when succeeds, otherwise error.
39 * succeeded value as wh->name should be freed by kfree().
40 */
41 int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
42 {
43 char *p;
44
45 if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
46 return -ENAMETOOLONG;
47
48 wh->len = name->len + AUFS_WH_PFX_LEN;
49 p = kmalloc(wh->len, GFP_NOFS);
50 wh->name = p;
51 if (p) {
52 memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
53 memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
54 /* smp_mb(); */
55 return 0;
56 }
57 return -ENOMEM;
58 }
59
60 /* ---------------------------------------------------------------------- */
61
62 /*
63 * test if the @wh_name exists under @h_parent.
64 * @try_sio specifies the necessary of super-io.
65 */
66 int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
67 {
68 int err;
69 struct dentry *wh_dentry;
70
71 if (!try_sio)
72 wh_dentry = vfsub_lkup_one(wh_name, h_parent);
73 else
74 wh_dentry = au_sio_lkup_one(wh_name, h_parent);
75 err = PTR_ERR(wh_dentry);
76 if (IS_ERR(wh_dentry)) {
77 if (err == -ENAMETOOLONG)
78 err = 0;
79 goto out;
80 }
81
82 err = 0;
83 if (d_is_negative(wh_dentry))
84 goto out_wh; /* success */
85
86 err = 1;
87 if (d_is_reg(wh_dentry))
88 goto out_wh; /* success */
89
90 err = -EIO;
91 AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
92 wh_dentry, d_inode(wh_dentry)->i_mode);
93
94 out_wh:
95 dput(wh_dentry);
96 out:
97 return err;
98 }
99
100 /*
101 * test if the @h_dentry sets opaque or not.
102 */
103 int au_diropq_test(struct dentry *h_dentry)
104 {
105 int err;
106 struct inode *h_dir;
107
108 h_dir = d_inode(h_dentry);
109 err = au_wh_test(h_dentry, &diropq_name,
110 au_test_h_perm_sio(h_dir, MAY_EXEC));
111 return err;
112 }
113
114 /*
115 * returns a negative dentry whose name is unique and temporary.
116 */
117 struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
118 struct qstr *prefix)
119 {
120 struct dentry *dentry;
121 int i;
122 char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
123 *name, *p;
124 /* strict atomic_t is unnecessary here */
125 static unsigned short cnt;
126 struct qstr qs;
127
128 BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
129
130 name = defname;
131 qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
132 if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
133 dentry = ERR_PTR(-ENAMETOOLONG);
134 if (unlikely(qs.len > NAME_MAX))
135 goto out;
136 dentry = ERR_PTR(-ENOMEM);
137 name = kmalloc(qs.len + 1, GFP_NOFS);
138 if (unlikely(!name))
139 goto out;
140 }
141
142 /* doubly whiteout-ed */
143 memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
144 p = name + AUFS_WH_PFX_LEN * 2;
145 memcpy(p, prefix->name, prefix->len);
146 p += prefix->len;
147 *p++ = '.';
148 AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
149
150 qs.name = name;
151 for (i = 0; i < 3; i++) {
152 sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
153 dentry = au_sio_lkup_one(&qs, h_parent);
154 if (IS_ERR(dentry) || d_is_negative(dentry))
155 goto out_name;
156 dput(dentry);
157 }
158 /* pr_warn("could not get random name\n"); */
159 dentry = ERR_PTR(-EEXIST);
160 AuDbg("%.*s\n", AuLNPair(&qs));
161 BUG();
162
163 out_name:
164 if (name != defname)
165 kfree(name);
166 out:
167 AuTraceErrPtr(dentry);
168 return dentry;
169 }
170
171 /*
172 * rename the @h_dentry on @br to the whiteouted temporary name.
173 */
174 int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
175 {
176 int err;
177 struct path h_path = {
178 .mnt = au_br_mnt(br)
179 };
180 struct inode *h_dir, *delegated;
181 struct dentry *h_parent;
182
183 h_parent = h_dentry->d_parent; /* dir inode is locked */
184 h_dir = d_inode(h_parent);
185 IMustLock(h_dir);
186
187 h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
188 err = PTR_ERR(h_path.dentry);
189 if (IS_ERR(h_path.dentry))
190 goto out;
191
192 /* under the same dir, no need to lock_rename() */
193 delegated = NULL;
194 err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
195 /*flags*/0);
196 AuTraceErr(err);
197 if (unlikely(err == -EWOULDBLOCK)) {
198 pr_warn("cannot retry for NFSv4 delegation"
199 " for an internal rename\n");
200 iput(delegated);
201 }
202 dput(h_path.dentry);
203
204 out:
205 AuTraceErr(err);
206 return err;
207 }
208
209 /* ---------------------------------------------------------------------- */
210 /*
211 * functions for removing a whiteout
212 */
213
214 static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
215 {
216 int err, force;
217 struct inode *delegated;
218
219 /*
220 * forces superio when the dir has a sticky bit.
221 * this may be a violation of unix fs semantics.
222 */
223 force = (h_dir->i_mode & S_ISVTX)
224 && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
225 delegated = NULL;
226 err = vfsub_unlink(h_dir, h_path, &delegated, force);
227 if (unlikely(err == -EWOULDBLOCK)) {
228 pr_warn("cannot retry for NFSv4 delegation"
229 " for an internal unlink\n");
230 iput(delegated);
231 }
232 return err;
233 }
234
235 int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
236 struct dentry *dentry)
237 {
238 int err;
239
240 err = do_unlink_wh(h_dir, h_path);
241 if (!err && dentry)
242 au_set_dbwh(dentry, -1);
243
244 return err;
245 }
246
247 static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
248 struct au_branch *br)
249 {
250 int err;
251 struct path h_path = {
252 .mnt = au_br_mnt(br)
253 };
254
255 err = 0;
256 h_path.dentry = vfsub_lkup_one(wh, h_parent);
257 if (IS_ERR(h_path.dentry))
258 err = PTR_ERR(h_path.dentry);
259 else {
260 if (d_is_reg(h_path.dentry))
261 err = do_unlink_wh(d_inode(h_parent), &h_path);
262 dput(h_path.dentry);
263 }
264
265 return err;
266 }
267
268 /* ---------------------------------------------------------------------- */
269 /*
270 * initialize/clean whiteout for a branch
271 */
272
273 static void au_wh_clean(struct inode *h_dir, struct path *whpath,
274 const int isdir)
275 {
276 int err;
277 struct inode *delegated;
278
279 if (d_is_negative(whpath->dentry))
280 return;
281
282 if (isdir)
283 err = vfsub_rmdir(h_dir, whpath);
284 else {
285 delegated = NULL;
286 err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
287 if (unlikely(err == -EWOULDBLOCK)) {
288 pr_warn("cannot retry for NFSv4 delegation"
289 " for an internal unlink\n");
290 iput(delegated);
291 }
292 }
293 if (unlikely(err))
294 pr_warn("failed removing %pd (%d), ignored.\n",
295 whpath->dentry, err);
296 }
297
298 static int test_linkable(struct dentry *h_root)
299 {
300 struct inode *h_dir = d_inode(h_root);
301
302 if (h_dir->i_op->link)
303 return 0;
304
305 pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
306 h_root, au_sbtype(h_root->d_sb));
307 return -ENOSYS;
308 }
309
310 /* todo: should this mkdir be done in /sbin/mount.aufs helper? */
311 static int au_whdir(struct inode *h_dir, struct path *path)
312 {
313 int err;
314
315 err = -EEXIST;
316 if (d_is_negative(path->dentry)) {
317 int mode = S_IRWXU;
318
319 if (au_test_nfs(path->dentry->d_sb))
320 mode |= S_IXUGO;
321 err = vfsub_mkdir(h_dir, path, mode);
322 } else if (d_is_dir(path->dentry))
323 err = 0;
324 else
325 pr_err("unknown %pd exists\n", path->dentry);
326
327 return err;
328 }
329
330 struct au_wh_base {
331 const struct qstr *name;
332 struct dentry *dentry;
333 };
334
335 static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
336 struct path *h_path)
337 {
338 h_path->dentry = base[AuBrWh_BASE].dentry;
339 au_wh_clean(h_dir, h_path, /*isdir*/0);
340 h_path->dentry = base[AuBrWh_PLINK].dentry;
341 au_wh_clean(h_dir, h_path, /*isdir*/1);
342 h_path->dentry = base[AuBrWh_ORPH].dentry;
343 au_wh_clean(h_dir, h_path, /*isdir*/1);
344 }
345
346 /*
347 * returns tri-state,
348 * minus: error, caller should print the message
349 * zero: succuess
350 * plus: error, caller should NOT print the message
351 */
352 static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
353 int do_plink, struct au_wh_base base[],
354 struct path *h_path)
355 {
356 int err;
357 struct inode *h_dir;
358
359 h_dir = d_inode(h_root);
360 h_path->dentry = base[AuBrWh_BASE].dentry;
361 au_wh_clean(h_dir, h_path, /*isdir*/0);
362 h_path->dentry = base[AuBrWh_PLINK].dentry;
363 if (do_plink) {
364 err = test_linkable(h_root);
365 if (unlikely(err)) {
366 err = 1;
367 goto out;
368 }
369
370 err = au_whdir(h_dir, h_path);
371 if (unlikely(err))
372 goto out;
373 wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
374 } else
375 au_wh_clean(h_dir, h_path, /*isdir*/1);
376 h_path->dentry = base[AuBrWh_ORPH].dentry;
377 err = au_whdir(h_dir, h_path);
378 if (unlikely(err))
379 goto out;
380 wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
381
382 out:
383 return err;
384 }
385
386 /*
387 * for the moment, aufs supports the branch filesystem which does not support
388 * link(2). testing on FAT which does not support i_op->setattr() fully either,
389 * copyup failed. finally, such filesystem will not be used as the writable
390 * branch.
391 *
392 * returns tri-state, see above.
393 */
394 static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
395 int do_plink, struct au_wh_base base[],
396 struct path *h_path)
397 {
398 int err;
399 struct inode *h_dir;
400
401 WbrWhMustWriteLock(wbr);
402
403 err = test_linkable(h_root);
404 if (unlikely(err)) {
405 err = 1;
406 goto out;
407 }
408
409 /*
410 * todo: should this create be done in /sbin/mount.aufs helper?
411 */
412 err = -EEXIST;
413 h_dir = d_inode(h_root);
414 if (d_is_negative(base[AuBrWh_BASE].dentry)) {
415 h_path->dentry = base[AuBrWh_BASE].dentry;
416 err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
417 } else if (d_is_reg(base[AuBrWh_BASE].dentry))
418 err = 0;
419 else
420 pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
421 if (unlikely(err))
422 goto out;
423
424 h_path->dentry = base[AuBrWh_PLINK].dentry;
425 if (do_plink) {
426 err = au_whdir(h_dir, h_path);
427 if (unlikely(err))
428 goto out;
429 wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
430 } else
431 au_wh_clean(h_dir, h_path, /*isdir*/1);
432 wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
433
434 h_path->dentry = base[AuBrWh_ORPH].dentry;
435 err = au_whdir(h_dir, h_path);
436 if (unlikely(err))
437 goto out;
438 wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
439
440 out:
441 return err;
442 }
443
444 /*
445 * initialize the whiteout base file/dir for @br.
446 */
447 int au_wh_init(struct au_branch *br, struct super_block *sb)
448 {
449 int err, i;
450 const unsigned char do_plink
451 = !!au_opt_test(au_mntflags(sb), PLINK);
452 struct inode *h_dir;
453 struct path path = br->br_path;
454 struct dentry *h_root = path.dentry;
455 struct au_wbr *wbr = br->br_wbr;
456 static const struct qstr base_name[] = {
457 [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
458 sizeof(AUFS_BASE_NAME) - 1),
459 [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
460 sizeof(AUFS_PLINKDIR_NAME) - 1),
461 [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
462 sizeof(AUFS_ORPHDIR_NAME) - 1)
463 };
464 struct au_wh_base base[] = {
465 [AuBrWh_BASE] = {
466 .name = base_name + AuBrWh_BASE,
467 .dentry = NULL
468 },
469 [AuBrWh_PLINK] = {
470 .name = base_name + AuBrWh_PLINK,
471 .dentry = NULL
472 },
473 [AuBrWh_ORPH] = {
474 .name = base_name + AuBrWh_ORPH,
475 .dentry = NULL
476 }
477 };
478
479 if (wbr)
480 WbrWhMustWriteLock(wbr);
481
482 for (i = 0; i < AuBrWh_Last; i++) {
483 /* doubly whiteouted */
484 struct dentry *d;
485
486 d = au_wh_lkup(h_root, (void *)base[i].name, br);
487 err = PTR_ERR(d);
488 if (IS_ERR(d))
489 goto out;
490
491 base[i].dentry = d;
492 AuDebugOn(wbr
493 && wbr->wbr_wh[i]
494 && wbr->wbr_wh[i] != base[i].dentry);
495 }
496
497 if (wbr)
498 for (i = 0; i < AuBrWh_Last; i++) {
499 dput(wbr->wbr_wh[i]);
500 wbr->wbr_wh[i] = NULL;
501 }
502
503 err = 0;
504 if (!au_br_writable(br->br_perm)) {
505 h_dir = d_inode(h_root);
506 au_wh_init_ro(h_dir, base, &path);
507 } else if (!au_br_wh_linkable(br->br_perm)) {
508 err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
509 if (err > 0)
510 goto out;
511 else if (err)
512 goto out_err;
513 } else {
514 err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
515 if (err > 0)
516 goto out;
517 else if (err)
518 goto out_err;
519 }
520 goto out; /* success */
521
522 out_err:
523 pr_err("an error(%d) on the writable branch %pd(%s)\n",
524 err, h_root, au_sbtype(h_root->d_sb));
525 out:
526 for (i = 0; i < AuBrWh_Last; i++)
527 dput(base[i].dentry);
528 return err;
529 }
530
531 /* ---------------------------------------------------------------------- */
532 /*
533 * whiteouts are all hard-linked usually.
534 * when its link count reaches a ceiling, we create a new whiteout base
535 * asynchronously.
536 */
537
538 struct reinit_br_wh {
539 struct super_block *sb;
540 struct au_branch *br;
541 };
542
543 static void reinit_br_wh(void *arg)
544 {
545 int err;
546 aufs_bindex_t bindex;
547 struct path h_path;
548 struct reinit_br_wh *a = arg;
549 struct au_wbr *wbr;
550 struct inode *dir, *delegated;
551 struct dentry *h_root;
552 struct au_hinode *hdir;
553
554 err = 0;
555 wbr = a->br->br_wbr;
556 /* big aufs lock */
557 si_noflush_write_lock(a->sb);
558 if (!au_br_writable(a->br->br_perm))
559 goto out;
560 bindex = au_br_index(a->sb, a->br->br_id);
561 if (unlikely(bindex < 0))
562 goto out;
563
564 di_read_lock_parent(a->sb->s_root, AuLock_IR);
565 dir = d_inode(a->sb->s_root);
566 hdir = au_hi(dir, bindex);
567 h_root = au_h_dptr(a->sb->s_root, bindex);
568 AuDebugOn(h_root != au_br_dentry(a->br));
569
570 au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
571 wbr_wh_write_lock(wbr);
572 err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
573 h_root, a->br);
574 if (!err) {
575 h_path.dentry = wbr->wbr_whbase;
576 h_path.mnt = au_br_mnt(a->br);
577 delegated = NULL;
578 err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
579 /*force*/0);
580 if (unlikely(err == -EWOULDBLOCK)) {
581 pr_warn("cannot retry for NFSv4 delegation"
582 " for an internal unlink\n");
583 iput(delegated);
584 }
585 } else {
586 pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
587 err = 0;
588 }
589 dput(wbr->wbr_whbase);
590 wbr->wbr_whbase = NULL;
591 if (!err)
592 err = au_wh_init(a->br, a->sb);
593 wbr_wh_write_unlock(wbr);
594 au_hn_inode_unlock(hdir);
595 di_read_unlock(a->sb->s_root, AuLock_IR);
596 if (!err)
597 au_fhsm_wrote(a->sb, bindex, /*force*/0);
598
599 out:
600 if (wbr)
601 atomic_dec(&wbr->wbr_wh_running);
602 au_br_put(a->br);
603 si_write_unlock(a->sb);
604 au_nwt_done(&au_sbi(a->sb)->si_nowait);
605 kfree(arg);
606 if (unlikely(err))
607 AuIOErr("err %d\n", err);
608 }
609
610 static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
611 {
612 int do_dec, wkq_err;
613 struct reinit_br_wh *arg;
614
615 do_dec = 1;
616 if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
617 goto out;
618
619 /* ignore ENOMEM */
620 arg = kmalloc(sizeof(*arg), GFP_NOFS);
621 if (arg) {
622 /*
623 * dec(wh_running), kfree(arg) and dec(br_count)
624 * in reinit function
625 */
626 arg->sb = sb;
627 arg->br = br;
628 au_br_get(br);
629 wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
630 if (unlikely(wkq_err)) {
631 atomic_dec(&br->br_wbr->wbr_wh_running);
632 au_br_put(br);
633 kfree(arg);
634 }
635 do_dec = 0;
636 }
637
638 out:
639 if (do_dec)
640 atomic_dec(&br->br_wbr->wbr_wh_running);
641 }
642
643 /* ---------------------------------------------------------------------- */
644
645 /*
646 * create the whiteout @wh.
647 */
648 static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
649 struct dentry *wh)
650 {
651 int err;
652 struct path h_path = {
653 .dentry = wh
654 };
655 struct au_branch *br;
656 struct au_wbr *wbr;
657 struct dentry *h_parent;
658 struct inode *h_dir, *delegated;
659
660 h_parent = wh->d_parent; /* dir inode is locked */
661 h_dir = d_inode(h_parent);
662 IMustLock(h_dir);
663
664 br = au_sbr(sb, bindex);
665 h_path.mnt = au_br_mnt(br);
666 wbr = br->br_wbr;
667 wbr_wh_read_lock(wbr);
668 if (wbr->wbr_whbase) {
669 delegated = NULL;
670 err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
671 if (unlikely(err == -EWOULDBLOCK)) {
672 pr_warn("cannot retry for NFSv4 delegation"
673 " for an internal link\n");
674 iput(delegated);
675 }
676 if (!err || err != -EMLINK)
677 goto out;
678
679 /* link count full. re-initialize br_whbase. */
680 kick_reinit_br_wh(sb, br);
681 }
682
683 /* return this error in this context */
684 err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
685 if (!err)
686 au_fhsm_wrote(sb, bindex, /*force*/0);
687
688 out:
689 wbr_wh_read_unlock(wbr);
690 return err;
691 }
692
693 /* ---------------------------------------------------------------------- */
694
695 /*
696 * create or remove the diropq.
697 */
698 static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
699 unsigned int flags)
700 {
701 struct dentry *opq_dentry, *h_dentry;
702 struct super_block *sb;
703 struct au_branch *br;
704 int err;
705
706 sb = dentry->d_sb;
707 br = au_sbr(sb, bindex);
708 h_dentry = au_h_dptr(dentry, bindex);
709 opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
710 if (IS_ERR(opq_dentry))
711 goto out;
712
713 if (au_ftest_diropq(flags, CREATE)) {
714 err = link_or_create_wh(sb, bindex, opq_dentry);
715 if (!err) {
716 au_set_dbdiropq(dentry, bindex);
717 goto out; /* success */
718 }
719 } else {
720 struct path tmp = {
721 .dentry = opq_dentry,
722 .mnt = au_br_mnt(br)
723 };
724 err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
725 if (!err)
726 au_set_dbdiropq(dentry, -1);
727 }
728 dput(opq_dentry);
729 opq_dentry = ERR_PTR(err);
730
731 out:
732 return opq_dentry;
733 }
734
735 struct do_diropq_args {
736 struct dentry **errp;
737 struct dentry *dentry;
738 aufs_bindex_t bindex;
739 unsigned int flags;
740 };
741
742 static void call_do_diropq(void *args)
743 {
744 struct do_diropq_args *a = args;
745 *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
746 }
747
748 struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
749 unsigned int flags)
750 {
751 struct dentry *diropq, *h_dentry;
752
753 h_dentry = au_h_dptr(dentry, bindex);
754 if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
755 diropq = do_diropq(dentry, bindex, flags);
756 else {
757 int wkq_err;
758 struct do_diropq_args args = {
759 .errp = &diropq,
760 .dentry = dentry,
761 .bindex = bindex,
762 .flags = flags
763 };
764
765 wkq_err = au_wkq_wait(call_do_diropq, &args);
766 if (unlikely(wkq_err))
767 diropq = ERR_PTR(wkq_err);
768 }
769
770 return diropq;
771 }
772
773 /* ---------------------------------------------------------------------- */
774
775 /*
776 * lookup whiteout dentry.
777 * @h_parent: lower parent dentry which must exist and be locked
778 * @base_name: name of dentry which will be whiteouted
779 * returns dentry for whiteout.
780 */
781 struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
782 struct au_branch *br)
783 {
784 int err;
785 struct qstr wh_name;
786 struct dentry *wh_dentry;
787
788 err = au_wh_name_alloc(&wh_name, base_name);
789 wh_dentry = ERR_PTR(err);
790 if (!err) {
791 wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
792 kfree(wh_name.name);
793 }
794 return wh_dentry;
795 }
796
797 /*
798 * link/create a whiteout for @dentry on @bindex.
799 */
800 struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
801 struct dentry *h_parent)
802 {
803 struct dentry *wh_dentry;
804 struct super_block *sb;
805 int err;
806
807 sb = dentry->d_sb;
808 wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
809 if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
810 err = link_or_create_wh(sb, bindex, wh_dentry);
811 if (!err) {
812 au_set_dbwh(dentry, bindex);
813 au_fhsm_wrote(sb, bindex, /*force*/0);
814 } else {
815 dput(wh_dentry);
816 wh_dentry = ERR_PTR(err);
817 }
818 }
819
820 return wh_dentry;
821 }
822
823 /* ---------------------------------------------------------------------- */
824
825 /* Delete all whiteouts in this directory on branch bindex. */
826 static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
827 aufs_bindex_t bindex, struct au_branch *br)
828 {
829 int err;
830 unsigned long ul, n;
831 struct qstr wh_name;
832 char *p;
833 struct hlist_head *head;
834 struct au_vdir_wh *pos;
835 struct au_vdir_destr *str;
836
837 err = -ENOMEM;
838 p = (void *)__get_free_page(GFP_NOFS);
839 wh_name.name = p;
840 if (unlikely(!wh_name.name))
841 goto out;
842
843 err = 0;
844 memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
845 p += AUFS_WH_PFX_LEN;
846 n = whlist->nh_num;
847 head = whlist->nh_head;
848 for (ul = 0; !err && ul < n; ul++, head++) {
849 hlist_for_each_entry(pos, head, wh_hash) {
850 if (pos->wh_bindex != bindex)
851 continue;
852
853 str = &pos->wh_str;
854 if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
855 memcpy(p, str->name, str->len);
856 wh_name.len = AUFS_WH_PFX_LEN + str->len;
857 err = unlink_wh_name(h_dentry, &wh_name, br);
858 if (!err)
859 continue;
860 break;
861 }
862 AuIOErr("whiteout name too long %.*s\n",
863 str->len, str->name);
864 err = -EIO;
865 break;
866 }
867 }
868 free_page((unsigned long)wh_name.name);
869
870 out:
871 return err;
872 }
873
874 struct del_wh_children_args {
875 int *errp;
876 struct dentry *h_dentry;
877 struct au_nhash *whlist;
878 aufs_bindex_t bindex;
879 struct au_branch *br;
880 };
881
882 static void call_del_wh_children(void *args)
883 {
884 struct del_wh_children_args *a = args;
885 *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
886 }
887
888 /* ---------------------------------------------------------------------- */
889
890 struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
891 {
892 struct au_whtmp_rmdir *whtmp;
893 int err;
894 unsigned int rdhash;
895
896 SiMustAnyLock(sb);
897
898 whtmp = kzalloc(sizeof(*whtmp), gfp);
899 if (unlikely(!whtmp)) {
900 whtmp = ERR_PTR(-ENOMEM);
901 goto out;
902 }
903
904 /* no estimation for dir size */
905 rdhash = au_sbi(sb)->si_rdhash;
906 if (!rdhash)
907 rdhash = AUFS_RDHASH_DEF;
908 err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
909 if (unlikely(err)) {
910 kfree(whtmp);
911 whtmp = ERR_PTR(err);
912 }
913
914 out:
915 return whtmp;
916 }
917
918 void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
919 {
920 if (whtmp->br)
921 au_br_put(whtmp->br);
922 dput(whtmp->wh_dentry);
923 iput(whtmp->dir);
924 au_nhash_wh_free(&whtmp->whlist);
925 kfree(whtmp);
926 }
927
928 /*
929 * rmdir the whiteouted temporary named dir @h_dentry.
930 * @whlist: whiteouted children.
931 */
932 int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
933 struct dentry *wh_dentry, struct au_nhash *whlist)
934 {
935 int err;
936 unsigned int h_nlink;
937 struct path h_tmp;
938 struct inode *wh_inode, *h_dir;
939 struct au_branch *br;
940
941 h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
942 IMustLock(h_dir);
943
944 br = au_sbr(dir->i_sb, bindex);
945 wh_inode = d_inode(wh_dentry);
946 inode_lock_nested(wh_inode, AuLsc_I_CHILD);
947
948 /*
949 * someone else might change some whiteouts while we were sleeping.
950 * it means this whlist may have an obsoleted entry.
951 */
952 if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
953 err = del_wh_children(wh_dentry, whlist, bindex, br);
954 else {
955 int wkq_err;
956 struct del_wh_children_args args = {
957 .errp = &err,
958 .h_dentry = wh_dentry,
959 .whlist = whlist,
960 .bindex = bindex,
961 .br = br
962 };
963
964 wkq_err = au_wkq_wait(call_del_wh_children, &args);
965 if (unlikely(wkq_err))
966 err = wkq_err;
967 }
968 inode_unlock(wh_inode);
969
970 if (!err) {
971 h_tmp.dentry = wh_dentry;
972 h_tmp.mnt = au_br_mnt(br);
973 h_nlink = h_dir->i_nlink;
974 err = vfsub_rmdir(h_dir, &h_tmp);
975 /* some fs doesn't change the parent nlink in some cases */
976 h_nlink -= h_dir->i_nlink;
977 }
978
979 if (!err) {
980 if (au_ibtop(dir) == bindex) {
981 /* todo: dir->i_mutex is necessary */
982 au_cpup_attr_timesizes(dir);
983 if (h_nlink)
984 vfsub_drop_nlink(dir);
985 }
986 return 0; /* success */
987 }
988
989 pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
990 return err;
991 }
992
993 static void call_rmdir_whtmp(void *args)
994 {
995 int err;
996 aufs_bindex_t bindex;
997 struct au_whtmp_rmdir *a = args;
998 struct super_block *sb;
999 struct dentry *h_parent;
1000 struct inode *h_dir;
1001 struct au_hinode *hdir;
1002
1003 /* rmdir by nfsd may cause deadlock with this i_mutex */
1004 /* inode_lock(a->dir); */
1005 err = -EROFS;
1006 sb = a->dir->i_sb;
1007 si_read_lock(sb, !AuLock_FLUSH);
1008 if (!au_br_writable(a->br->br_perm))
1009 goto out;
1010 bindex = au_br_index(sb, a->br->br_id);
1011 if (unlikely(bindex < 0))
1012 goto out;
1013
1014 err = -EIO;
1015 ii_write_lock_parent(a->dir);
1016 h_parent = dget_parent(a->wh_dentry);
1017 h_dir = d_inode(h_parent);
1018 hdir = au_hi(a->dir, bindex);
1019 err = vfsub_mnt_want_write(au_br_mnt(a->br));
1020 if (unlikely(err))
1021 goto out_mnt;
1022 au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1023 err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
1024 a->br);
1025 if (!err)
1026 err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
1027 au_hn_inode_unlock(hdir);
1028 vfsub_mnt_drop_write(au_br_mnt(a->br));
1029
1030 out_mnt:
1031 dput(h_parent);
1032 ii_write_unlock(a->dir);
1033 out:
1034 /* inode_unlock(a->dir); */
1035 au_whtmp_rmdir_free(a);
1036 si_read_unlock(sb);
1037 au_nwt_done(&au_sbi(sb)->si_nowait);
1038 if (unlikely(err))
1039 AuIOErr("err %d\n", err);
1040 }
1041
1042 void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
1043 struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
1044 {
1045 int wkq_err;
1046 struct super_block *sb;
1047
1048 IMustLock(dir);
1049
1050 /* all post-process will be done in do_rmdir_whtmp(). */
1051 sb = dir->i_sb;
1052 args->dir = au_igrab(dir);
1053 args->br = au_sbr(sb, bindex);
1054 au_br_get(args->br);
1055 args->wh_dentry = dget(wh_dentry);
1056 wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1057 if (unlikely(wkq_err)) {
1058 pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1059 au_whtmp_rmdir_free(args);
1060 }
1061 }