]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/aufs/dentry.c
UBUNTU: SAUCE: Update aufs to 5.4.3 20200302
[mirror_ubuntu-focal-kernel.git] / fs / aufs / dentry.c
CommitLineData
a3a49a17
SF
1// SPDX-License-Identifier: GPL-2.0
2/*
e4a3f096 3 * Copyright (C) 2005-2020 Junjiro R. Okajima
a3a49a17
SF
4 *
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/*
20 * lookup and dentry operations
21 */
22
23#include <linux/iversion.h>
24#include <linux/namei.h>
25#include "aufs.h"
26
27/*
28 * returns positive/negative dentry, NULL or an error.
29 * NULL means whiteout-ed or not-found.
30 */
31static struct dentry*
32au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
33 aufs_bindex_t bindex, struct au_do_lookup_args *args)
34{
35 struct dentry *h_dentry;
36 struct inode *h_inode;
37 struct au_branch *br;
38 int wh_found, opq;
39 unsigned char wh_able;
40 const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
41 const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
42 IGNORE_PERM);
43
44 wh_found = 0;
45 br = au_sbr(dentry->d_sb, bindex);
46 wh_able = !!au_br_whable(br->br_perm);
47 if (wh_able)
48 wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
49 h_dentry = ERR_PTR(wh_found);
50 if (!wh_found)
51 goto real_lookup;
52 if (unlikely(wh_found < 0))
53 goto out;
54
55 /* We found a whiteout */
56 /* au_set_dbbot(dentry, bindex); */
57 au_set_dbwh(dentry, bindex);
58 if (!allow_neg)
59 return NULL; /* success */
60
61real_lookup:
62 if (!ignore_perm)
63 h_dentry = vfsub_lkup_one(args->name, h_parent);
64 else
65 h_dentry = au_sio_lkup_one(args->name, h_parent);
66 if (IS_ERR(h_dentry)) {
67 if (PTR_ERR(h_dentry) == -ENAMETOOLONG
68 && !allow_neg)
69 h_dentry = NULL;
70 goto out;
71 }
72
73 h_inode = d_inode(h_dentry);
74 if (d_is_negative(h_dentry)) {
75 if (!allow_neg)
76 goto out_neg;
77 } else if (wh_found
78 || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
79 goto out_neg;
80 else if (au_ftest_lkup(args->flags, DIRREN)
81 /* && h_inode */
82 && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
83 AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
84 (unsigned long long)h_inode->i_ino);
85 goto out_neg;
86 }
87
88 if (au_dbbot(dentry) <= bindex)
89 au_set_dbbot(dentry, bindex);
90 if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
91 au_set_dbtop(dentry, bindex);
92 au_set_h_dptr(dentry, bindex, h_dentry);
93
94 if (!d_is_dir(h_dentry)
95 || !wh_able
96 || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
97 goto out; /* success */
98
99 inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
100 opq = au_diropq_test(h_dentry);
101 inode_unlock_shared(h_inode);
102 if (opq > 0)
103 au_set_dbdiropq(dentry, bindex);
104 else if (unlikely(opq < 0)) {
105 au_set_h_dptr(dentry, bindex, NULL);
106 h_dentry = ERR_PTR(opq);
107 }
108 goto out;
109
110out_neg:
111 dput(h_dentry);
112 h_dentry = NULL;
113out:
114 return h_dentry;
115}
116
117static int au_test_shwh(struct super_block *sb, const struct qstr *name)
118{
119 if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
120 && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
121 return -EPERM;
122 return 0;
123}
124
125/*
126 * returns the number of lower positive dentries,
127 * otherwise an error.
128 * can be called at unlinking with @type is zero.
129 */
130int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
131 unsigned int flags)
132{
133 int npositive, err;
134 aufs_bindex_t bindex, btail, bdiropq;
135 unsigned char isdir, dirperm1, dirren;
136 struct au_do_lookup_args args = {
137 .flags = flags,
138 .name = &dentry->d_name
139 };
140 struct dentry *parent;
141 struct super_block *sb;
142
143 sb = dentry->d_sb;
144 err = au_test_shwh(sb, args.name);
145 if (unlikely(err))
146 goto out;
147
148 err = au_wh_name_alloc(&args.whname, args.name);
149 if (unlikely(err))
150 goto out;
151
152 isdir = !!d_is_dir(dentry);
153 dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
154 dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
155 if (dirren)
156 au_fset_lkup(args.flags, DIRREN);
157
158 npositive = 0;
159 parent = dget_parent(dentry);
160 btail = au_dbtaildir(parent);
161 for (bindex = btop; bindex <= btail; bindex++) {
162 struct dentry *h_parent, *h_dentry;
163 struct inode *h_inode, *h_dir;
164 struct au_branch *br;
165
166 h_dentry = au_h_dptr(dentry, bindex);
167 if (h_dentry) {
168 if (d_is_positive(h_dentry))
169 npositive++;
170 break;
171 }
172 h_parent = au_h_dptr(parent, bindex);
173 if (!h_parent || !d_is_dir(h_parent))
174 continue;
175
176 if (dirren) {
177 /* if the inum matches, then use the prepared name */
178 err = au_dr_lkup_name(&args, bindex);
179 if (unlikely(err))
180 goto out_parent;
181 }
182
183 h_dir = d_inode(h_parent);
184 inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
185 h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
186 inode_unlock_shared(h_dir);
187 err = PTR_ERR(h_dentry);
188 if (IS_ERR(h_dentry))
189 goto out_parent;
190 if (h_dentry)
191 au_fclr_lkup(args.flags, ALLOW_NEG);
192 if (dirperm1)
193 au_fset_lkup(args.flags, IGNORE_PERM);
194
195 if (au_dbwh(dentry) == bindex)
196 break;
197 if (!h_dentry)
198 continue;
199 if (d_is_negative(h_dentry))
200 continue;
201 h_inode = d_inode(h_dentry);
202 npositive++;
203 if (!args.type)
204 args.type = h_inode->i_mode & S_IFMT;
205 if (args.type != S_IFDIR)
206 break;
207 else if (isdir) {
208 /* the type of lower may be different */
209 bdiropq = au_dbdiropq(dentry);
210 if (bdiropq >= 0 && bdiropq <= bindex)
211 break;
212 }
213 br = au_sbr(sb, bindex);
214 if (dirren
215 && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
216 /*add_ent*/NULL)) {
217 /* prepare next name to lookup */
218 err = au_dr_lkup(&args, dentry, bindex);
219 if (unlikely(err))
220 goto out_parent;
221 }
222 }
223
224 if (npositive) {
225 AuLabel(positive);
226 au_update_dbtop(dentry);
227 }
228 err = npositive;
229 if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
230 && au_dbtop(dentry) < 0)) {
231 err = -EIO;
232 AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
233 dentry, err);
234 }
235
236out_parent:
237 dput(parent);
238 au_kfree_try_rcu(args.whname.name);
239 if (dirren)
240 au_dr_lkup_fin(&args);
241out:
242 return err;
243}
244
245struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
246{
247 struct dentry *dentry;
248 int wkq_err;
249
250 if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
251 dentry = vfsub_lkup_one(name, parent);
252 else {
253 struct vfsub_lkup_one_args args = {
254 .errp = &dentry,
255 .name = name,
256 .parent = parent
257 };
258
259 wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
260 if (unlikely(wkq_err))
261 dentry = ERR_PTR(wkq_err);
262 }
263
264 return dentry;
265}
266
267/*
268 * lookup @dentry on @bindex which should be negative.
269 */
270int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
271{
272 int err;
273 struct dentry *parent, *h_parent, *h_dentry;
274 struct au_branch *br;
275
276 parent = dget_parent(dentry);
277 h_parent = au_h_dptr(parent, bindex);
278 br = au_sbr(dentry->d_sb, bindex);
279 if (wh)
280 h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
281 else
282 h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
283 err = PTR_ERR(h_dentry);
284 if (IS_ERR(h_dentry))
285 goto out;
286 if (unlikely(d_is_positive(h_dentry))) {
287 err = -EIO;
288 AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
289 dput(h_dentry);
290 goto out;
291 }
292
293 err = 0;
294 if (bindex < au_dbtop(dentry))
295 au_set_dbtop(dentry, bindex);
296 if (au_dbbot(dentry) < bindex)
297 au_set_dbbot(dentry, bindex);
298 au_set_h_dptr(dentry, bindex, h_dentry);
299
300out:
301 dput(parent);
302 return err;
303}
304
305/* ---------------------------------------------------------------------- */
306
307/* subset of struct inode */
308struct au_iattr {
309 unsigned long i_ino;
310 /* unsigned int i_nlink; */
311 kuid_t i_uid;
312 kgid_t i_gid;
313 u64 i_version;
314/*
315 loff_t i_size;
316 blkcnt_t i_blocks;
317*/
318 umode_t i_mode;
319};
320
321static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
322{
323 ia->i_ino = h_inode->i_ino;
324 /* ia->i_nlink = h_inode->i_nlink; */
325 ia->i_uid = h_inode->i_uid;
326 ia->i_gid = h_inode->i_gid;
327 ia->i_version = inode_query_iversion(h_inode);
328/*
329 ia->i_size = h_inode->i_size;
330 ia->i_blocks = h_inode->i_blocks;
331*/
332 ia->i_mode = (h_inode->i_mode & S_IFMT);
333}
334
335static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
336{
337 return ia->i_ino != h_inode->i_ino
338 /* || ia->i_nlink != h_inode->i_nlink */
339 || !uid_eq(ia->i_uid, h_inode->i_uid)
340 || !gid_eq(ia->i_gid, h_inode->i_gid)
341 || !inode_eq_iversion(h_inode, ia->i_version)
342/*
343 || ia->i_size != h_inode->i_size
344 || ia->i_blocks != h_inode->i_blocks
345*/
346 || ia->i_mode != (h_inode->i_mode & S_IFMT);
347}
348
349static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
350 struct au_branch *br)
351{
352 int err;
353 struct au_iattr ia;
354 struct inode *h_inode;
355 struct dentry *h_d;
356 struct super_block *h_sb;
357
358 err = 0;
359 memset(&ia, -1, sizeof(ia));
360 h_sb = h_dentry->d_sb;
361 h_inode = NULL;
362 if (d_is_positive(h_dentry)) {
363 h_inode = d_inode(h_dentry);
364 au_iattr_save(&ia, h_inode);
365 } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
366 /* nfs d_revalidate may return 0 for negative dentry */
367 /* fuse d_revalidate always return 0 for negative dentry */
368 goto out;
369
370 /* main purpose is namei.c:cached_lookup() and d_revalidate */
371 h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
372 err = PTR_ERR(h_d);
373 if (IS_ERR(h_d))
374 goto out;
375
376 err = 0;
377 if (unlikely(h_d != h_dentry
378 || d_inode(h_d) != h_inode
379 || (h_inode && au_iattr_test(&ia, h_inode))))
380 err = au_busy_or_stale();
381 dput(h_d);
382
383out:
384 AuTraceErr(err);
385 return err;
386}
387
388int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
389 struct dentry *h_parent, struct au_branch *br)
390{
391 int err;
392
393 err = 0;
394 if (udba == AuOpt_UDBA_REVAL
395 && !au_test_fs_remote(h_dentry->d_sb)) {
396 IMustLock(h_dir);
397 err = (d_inode(h_dentry->d_parent) != h_dir);
398 } else if (udba != AuOpt_UDBA_NONE)
399 err = au_h_verify_dentry(h_dentry, h_parent, br);
400
401 return err;
402}
403
404/* ---------------------------------------------------------------------- */
405
406static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
407{
408 int err;
409 aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
410 struct au_hdentry tmp, *p, *q;
411 struct au_dinfo *dinfo;
412 struct super_block *sb;
413
414 DiMustWriteLock(dentry);
415
416 sb = dentry->d_sb;
417 dinfo = au_di(dentry);
418 bbot = dinfo->di_bbot;
419 bwh = dinfo->di_bwh;
420 bdiropq = dinfo->di_bdiropq;
421 bindex = dinfo->di_btop;
422 p = au_hdentry(dinfo, bindex);
423 for (; bindex <= bbot; bindex++, p++) {
424 if (!p->hd_dentry)
425 continue;
426
427 new_bindex = au_br_index(sb, p->hd_id);
428 if (new_bindex == bindex)
429 continue;
430
431 if (dinfo->di_bwh == bindex)
432 bwh = new_bindex;
433 if (dinfo->di_bdiropq == bindex)
434 bdiropq = new_bindex;
435 if (new_bindex < 0) {
436 au_hdput(p);
437 p->hd_dentry = NULL;
438 continue;
439 }
440
441 /* swap two lower dentries, and loop again */
442 q = au_hdentry(dinfo, new_bindex);
443 tmp = *q;
444 *q = *p;
445 *p = tmp;
446 if (tmp.hd_dentry) {
447 bindex--;
448 p--;
449 }
450 }
451
452 dinfo->di_bwh = -1;
453 if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
454 dinfo->di_bwh = bwh;
455
456 dinfo->di_bdiropq = -1;
457 if (bdiropq >= 0
458 && bdiropq <= au_sbbot(sb)
459 && au_sbr_whable(sb, bdiropq))
460 dinfo->di_bdiropq = bdiropq;
461
462 err = -EIO;
463 dinfo->di_btop = -1;
464 dinfo->di_bbot = -1;
465 bbot = au_dbbot(parent);
466 bindex = 0;
467 p = au_hdentry(dinfo, bindex);
468 for (; bindex <= bbot; bindex++, p++)
469 if (p->hd_dentry) {
470 dinfo->di_btop = bindex;
471 break;
472 }
473
474 if (dinfo->di_btop >= 0) {
475 bindex = bbot;
476 p = au_hdentry(dinfo, bindex);
477 for (; bindex >= 0; bindex--, p--)
478 if (p->hd_dentry) {
479 dinfo->di_bbot = bindex;
480 err = 0;
481 break;
482 }
483 }
484
485 return err;
486}
487
488static void au_do_hide(struct dentry *dentry)
489{
490 struct inode *inode;
491
492 if (d_really_is_positive(dentry)) {
493 inode = d_inode(dentry);
494 if (!d_is_dir(dentry)) {
495 if (inode->i_nlink && !d_unhashed(dentry))
496 drop_nlink(inode);
497 } else {
498 clear_nlink(inode);
499 /* stop next lookup */
500 inode->i_flags |= S_DEAD;
501 }
502 smp_mb(); /* necessary? */
503 }
504 d_drop(dentry);
505}
506
507static int au_hide_children(struct dentry *parent)
508{
509 int err, i, j, ndentry;
510 struct au_dcsub_pages dpages;
511 struct au_dpage *dpage;
512 struct dentry *dentry;
513
514 err = au_dpages_init(&dpages, GFP_NOFS);
515 if (unlikely(err))
516 goto out;
517 err = au_dcsub_pages(&dpages, parent, NULL, NULL);
518 if (unlikely(err))
519 goto out_dpages;
520
521 /* in reverse order */
522 for (i = dpages.ndpage - 1; i >= 0; i--) {
523 dpage = dpages.dpages + i;
524 ndentry = dpage->ndentry;
525 for (j = ndentry - 1; j >= 0; j--) {
526 dentry = dpage->dentries[j];
527 if (dentry != parent)
528 au_do_hide(dentry);
529 }
530 }
531
532out_dpages:
533 au_dpages_free(&dpages);
534out:
535 return err;
536}
537
538static void au_hide(struct dentry *dentry)
539{
540 int err;
541
542 AuDbgDentry(dentry);
543 if (d_is_dir(dentry)) {
544 /* shrink_dcache_parent(dentry); */
545 err = au_hide_children(dentry);
546 if (unlikely(err))
547 AuIOErr("%pd, failed hiding children, ignored %d\n",
548 dentry, err);
549 }
550 au_do_hide(dentry);
551}
552
553/*
554 * By adding a dirty branch, a cached dentry may be affected in various ways.
555 *
556 * a dirty branch is added
557 * - on the top of layers
558 * - in the middle of layers
559 * - to the bottom of layers
560 *
561 * on the added branch there exists
562 * - a whiteout
563 * - a diropq
564 * - a same named entry
565 * + exist
566 * * negative --> positive
567 * * positive --> positive
568 * - type is unchanged
569 * - type is changed
570 * + doesn't exist
571 * * negative --> negative
572 * * positive --> negative (rejected by au_br_del() for non-dir case)
573 * - none
574 */
575static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
576 struct au_dinfo *tmp)
577{
578 int err;
579 aufs_bindex_t bindex, bbot;
580 struct {
581 struct dentry *dentry;
582 struct inode *inode;
583 mode_t mode;
584 } orig_h, tmp_h = {
585 .dentry = NULL
586 };
587 struct au_hdentry *hd;
588 struct inode *inode, *h_inode;
589 struct dentry *h_dentry;
590
591 err = 0;
592 AuDebugOn(dinfo->di_btop < 0);
593 orig_h.mode = 0;
594 orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
595 orig_h.inode = NULL;
596 if (d_is_positive(orig_h.dentry)) {
597 orig_h.inode = d_inode(orig_h.dentry);
598 orig_h.mode = orig_h.inode->i_mode & S_IFMT;
599 }
600 if (tmp->di_btop >= 0) {
601 tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
602 if (d_is_positive(tmp_h.dentry)) {
603 tmp_h.inode = d_inode(tmp_h.dentry);
604 tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
605 }
606 }
607
608 inode = NULL;
609 if (d_really_is_positive(dentry))
610 inode = d_inode(dentry);
611 if (!orig_h.inode) {
612 AuDbg("negative originally\n");
613 if (inode) {
614 au_hide(dentry);
615 goto out;
616 }
617 AuDebugOn(inode);
618 AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
619 AuDebugOn(dinfo->di_bdiropq != -1);
620
621 if (!tmp_h.inode) {
622 AuDbg("negative --> negative\n");
623 /* should have only one negative lower */
624 if (tmp->di_btop >= 0
625 && tmp->di_btop < dinfo->di_btop) {
626 AuDebugOn(tmp->di_btop != tmp->di_bbot);
627 AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
628 au_set_h_dptr(dentry, dinfo->di_btop, NULL);
629 au_di_cp(dinfo, tmp);
630 hd = au_hdentry(tmp, tmp->di_btop);
631 au_set_h_dptr(dentry, tmp->di_btop,
632 dget(hd->hd_dentry));
633 }
634 au_dbg_verify_dinode(dentry);
635 } else {
636 AuDbg("negative --> positive\n");
637 /*
638 * similar to the behaviour of creating with bypassing
639 * aufs.
640 * unhash it in order to force an error in the
641 * succeeding create operation.
642 * we should not set S_DEAD here.
643 */
644 d_drop(dentry);
645 /* au_di_swap(tmp, dinfo); */
646 au_dbg_verify_dinode(dentry);
647 }
648 } else {
649 AuDbg("positive originally\n");
650 /* inode may be NULL */
651 AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
652 if (!tmp_h.inode) {
653 AuDbg("positive --> negative\n");
654 /* or bypassing aufs */
655 au_hide(dentry);
656 if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
657 dinfo->di_bwh = tmp->di_bwh;
658 if (inode)
659 err = au_refresh_hinode_self(inode);
660 au_dbg_verify_dinode(dentry);
661 } else if (orig_h.mode == tmp_h.mode) {
662 AuDbg("positive --> positive, same type\n");
663 if (!S_ISDIR(orig_h.mode)
664 && dinfo->di_btop > tmp->di_btop) {
665 /*
666 * similar to the behaviour of removing and
667 * creating.
668 */
669 au_hide(dentry);
670 if (inode)
671 err = au_refresh_hinode_self(inode);
672 au_dbg_verify_dinode(dentry);
673 } else {
674 /* fill empty slots */
675 if (dinfo->di_btop > tmp->di_btop)
676 dinfo->di_btop = tmp->di_btop;
677 if (dinfo->di_bbot < tmp->di_bbot)
678 dinfo->di_bbot = tmp->di_bbot;
679 dinfo->di_bwh = tmp->di_bwh;
680 dinfo->di_bdiropq = tmp->di_bdiropq;
681 bbot = dinfo->di_bbot;
682 bindex = tmp->di_btop;
683 hd = au_hdentry(tmp, bindex);
684 for (; bindex <= bbot; bindex++, hd++) {
685 if (au_h_dptr(dentry, bindex))
686 continue;
687 h_dentry = hd->hd_dentry;
688 if (!h_dentry)
689 continue;
690 AuDebugOn(d_is_negative(h_dentry));
691 h_inode = d_inode(h_dentry);
692 AuDebugOn(orig_h.mode
693 != (h_inode->i_mode
694 & S_IFMT));
695 au_set_h_dptr(dentry, bindex,
696 dget(h_dentry));
697 }
698 if (inode)
699 err = au_refresh_hinode(inode, dentry);
700 au_dbg_verify_dinode(dentry);
701 }
702 } else {
703 AuDbg("positive --> positive, different type\n");
704 /* similar to the behaviour of removing and creating */
705 au_hide(dentry);
706 if (inode)
707 err = au_refresh_hinode_self(inode);
708 au_dbg_verify_dinode(dentry);
709 }
710 }
711
712out:
713 return err;
714}
715
716void au_refresh_dop(struct dentry *dentry, int force_reval)
717{
718 const struct dentry_operations *dop
719 = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
720 static const unsigned int mask
721 = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
722
723 BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
724
725 if (dentry->d_op == dop)
726 return;
727
728 AuDbg("%pd\n", dentry);
729 spin_lock(&dentry->d_lock);
730 if (dop == &aufs_dop)
731 dentry->d_flags |= mask;
732 else
733 dentry->d_flags &= ~mask;
734 dentry->d_op = dop;
735 spin_unlock(&dentry->d_lock);
736}
737
738int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
739{
740 int err, ebrange, nbr;
741 unsigned int sigen;
742 struct au_dinfo *dinfo, *tmp;
743 struct super_block *sb;
744 struct inode *inode;
745
746 DiMustWriteLock(dentry);
747 AuDebugOn(IS_ROOT(dentry));
748 AuDebugOn(d_really_is_negative(parent));
749
750 sb = dentry->d_sb;
751 sigen = au_sigen(sb);
752 err = au_digen_test(parent, sigen);
753 if (unlikely(err))
754 goto out;
755
756 nbr = au_sbbot(sb) + 1;
757 dinfo = au_di(dentry);
758 err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
759 if (unlikely(err))
760 goto out;
761 ebrange = au_dbrange_test(dentry);
762 if (!ebrange)
763 ebrange = au_do_refresh_hdentry(dentry, parent);
764
765 if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
766 AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
767 if (d_really_is_positive(dentry)) {
768 inode = d_inode(dentry);
769 err = au_refresh_hinode_self(inode);
770 }
771 au_dbg_verify_dinode(dentry);
772 if (!err)
773 goto out_dgen; /* success */
774 goto out;
775 }
776
777 /* temporary dinfo */
778 AuDbgDentry(dentry);
779 err = -ENOMEM;
780 tmp = au_di_alloc(sb, AuLsc_DI_TMP);
781 if (unlikely(!tmp))
782 goto out;
783 au_di_swap(tmp, dinfo);
784 /* returns the number of positive dentries */
785 /*
786 * if current working dir is removed, it returns an error.
787 * but the dentry is legal.
788 */
789 err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
790 AuDbgDentry(dentry);
791 au_di_swap(tmp, dinfo);
792 if (err == -ENOENT)
793 err = 0;
794 if (err >= 0) {
795 /* compare/refresh by dinfo */
796 AuDbgDentry(dentry);
797 err = au_refresh_by_dinfo(dentry, dinfo, tmp);
798 au_dbg_verify_dinode(dentry);
799 AuTraceErr(err);
800 }
801 au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
802 au_rw_write_unlock(&tmp->di_rwsem);
803 au_di_free(tmp);
804 if (unlikely(err))
805 goto out;
806
807out_dgen:
808 au_update_digen(dentry);
809out:
810 if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
811 AuIOErr("failed refreshing %pd, %d\n", dentry, err);
812 AuDbgDentry(dentry);
813 }
814 AuTraceErr(err);
815 return err;
816}
817
818static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
819 struct dentry *dentry, aufs_bindex_t bindex)
820{
821 int err, valid;
822
823 err = 0;
824 if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
825 goto out;
826
827 AuDbg("b%d\n", bindex);
828 /*
829 * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
830 * due to whiteout and branch permission.
831 */
832 flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
833 | LOOKUP_FOLLOW | LOOKUP_EXCL);
834 /* it may return tri-state */
835 valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
836
837 if (unlikely(valid < 0))
838 err = valid;
839 else if (!valid)
840 err = -EINVAL;
841
842out:
843 AuTraceErr(err);
844 return err;
845}
846
847/* todo: remove this */
848static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
849 unsigned int flags, int do_udba, int dirren)
850{
851 int err;
852 umode_t mode, h_mode;
853 aufs_bindex_t bindex, btail, btop, ibs, ibe;
854 unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
855 struct inode *h_inode, *h_cached_inode;
856 struct dentry *h_dentry;
857 struct qstr *name, *h_name;
858
859 err = 0;
860 plus = 0;
861 mode = 0;
862 ibs = -1;
863 ibe = -1;
864 unhashed = !!d_unhashed(dentry);
865 is_root = !!IS_ROOT(dentry);
866 name = &dentry->d_name;
867 tmpfile = au_di(dentry)->di_tmpfile;
868
869 /*
870 * Theoretically, REVAL test should be unnecessary in case of
871 * {FS,I}NOTIFY.
872 * But {fs,i}notify doesn't fire some necessary events,
873 * IN_ATTRIB for atime/nlink/pageio
874 * Let's do REVAL test too.
875 */
876 if (do_udba && inode) {
877 mode = (inode->i_mode & S_IFMT);
878 plus = (inode->i_nlink > 0);
879 ibs = au_ibtop(inode);
880 ibe = au_ibbot(inode);
881 }
882
883 btop = au_dbtop(dentry);
884 btail = btop;
885 if (inode && S_ISDIR(inode->i_mode))
886 btail = au_dbtaildir(dentry);
887 for (bindex = btop; bindex <= btail; bindex++) {
888 h_dentry = au_h_dptr(dentry, bindex);
889 if (!h_dentry)
890 continue;
891
892 AuDbg("b%d, %pd\n", bindex, h_dentry);
893 h_nfs = !!au_test_nfs(h_dentry->d_sb);
894 spin_lock(&h_dentry->d_lock);
895 h_name = &h_dentry->d_name;
896 if (unlikely(do_udba
897 && !is_root
898 && ((!h_nfs
899 && (unhashed != !!d_unhashed(h_dentry)
900 || (!tmpfile && !dirren
901 && !au_qstreq(name, h_name))
902 ))
903 || (h_nfs
904 && !(flags & LOOKUP_OPEN)
905 && (h_dentry->d_flags
906 & DCACHE_NFSFS_RENAMED)))
907 )) {
908 int h_unhashed;
909
910 h_unhashed = d_unhashed(h_dentry);
911 spin_unlock(&h_dentry->d_lock);
912 AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
913 unhashed, h_unhashed, dentry, h_dentry);
914 goto err;
915 }
916 spin_unlock(&h_dentry->d_lock);
917
918 err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
919 if (unlikely(err))
920 /* do not goto err, to keep the errno */
921 break;
922
923 /* todo: plink too? */
924 if (!do_udba)
925 continue;
926
927 /* UDBA tests */
928 if (unlikely(!!inode != d_is_positive(h_dentry)))
929 goto err;
930
931 h_inode = NULL;
932 if (d_is_positive(h_dentry))
933 h_inode = d_inode(h_dentry);
934 h_plus = plus;
935 h_mode = mode;
936 h_cached_inode = h_inode;
937 if (h_inode) {
938 h_mode = (h_inode->i_mode & S_IFMT);
939 h_plus = (h_inode->i_nlink > 0);
940 }
941 if (inode && ibs <= bindex && bindex <= ibe)
942 h_cached_inode = au_h_iptr(inode, bindex);
943
944 if (!h_nfs) {
945 if (unlikely(plus != h_plus && !tmpfile))
946 goto err;
947 } else {
948 if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
949 && !is_root
950 && !IS_ROOT(h_dentry)
951 && unhashed != d_unhashed(h_dentry)))
952 goto err;
953 }
954 if (unlikely(mode != h_mode
955 || h_cached_inode != h_inode))
956 goto err;
957 continue;
958
959err:
960 err = -EINVAL;
961 break;
962 }
963
964 AuTraceErr(err);
965 return err;
966}
967
968/* todo: consolidate with do_refresh() and au_reval_for_attr() */
969static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
970{
971 int err;
972 struct dentry *parent;
973
974 if (!au_digen_test(dentry, sigen))
975 return 0;
976
977 parent = dget_parent(dentry);
978 di_read_lock_parent(parent, AuLock_IR);
979 AuDebugOn(au_digen_test(parent, sigen));
980 au_dbg_verify_gen(parent, sigen);
981 err = au_refresh_dentry(dentry, parent);
982 di_read_unlock(parent, AuLock_IR);
983 dput(parent);
984 AuTraceErr(err);
985 return err;
986}
987
988int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
989{
990 int err;
991 struct dentry *d, *parent;
992
993 if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
994 return simple_reval_dpath(dentry, sigen);
995
996 /* slow loop, keep it simple and stupid */
997 /* cf: au_cpup_dirs() */
998 err = 0;
999 parent = NULL;
1000 while (au_digen_test(dentry, sigen)) {
1001 d = dentry;
1002 while (1) {
1003 dput(parent);
1004 parent = dget_parent(d);
1005 if (!au_digen_test(parent, sigen))
1006 break;
1007 d = parent;
1008 }
1009
1010 if (d != dentry)
1011 di_write_lock_child2(d);
1012
1013 /* someone might update our dentry while we were sleeping */
1014 if (au_digen_test(d, sigen)) {
1015 /*
1016 * todo: consolidate with simple_reval_dpath(),
1017 * do_refresh() and au_reval_for_attr().
1018 */
1019 di_read_lock_parent(parent, AuLock_IR);
1020 err = au_refresh_dentry(d, parent);
1021 di_read_unlock(parent, AuLock_IR);
1022 }
1023
1024 if (d != dentry)
1025 di_write_unlock(d);
1026 dput(parent);
1027 if (unlikely(err))
1028 break;
1029 }
1030
1031 return err;
1032}
1033
1034/*
1035 * if valid returns 1, otherwise 0.
1036 */
1037static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1038{
1039 int valid, err;
1040 unsigned int sigen;
1041 unsigned char do_udba, dirren;
1042 struct super_block *sb;
1043 struct inode *inode;
1044
1045 /* todo: support rcu-walk? */
1046 if (flags & LOOKUP_RCU)
1047 return -ECHILD;
1048
1049 valid = 0;
1050 if (unlikely(!au_di(dentry)))
1051 goto out;
1052
1053 valid = 1;
1054 sb = dentry->d_sb;
1055 /*
1056 * todo: very ugly
1057 * i_mutex of parent dir may be held,
1058 * but we should not return 'invalid' due to busy.
1059 */
1060 err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
1061 if (unlikely(err)) {
1062 valid = err;
1063 AuTraceErr(err);
1064 goto out;
1065 }
1066 inode = NULL;
1067 if (d_really_is_positive(dentry))
1068 inode = d_inode(dentry);
1069 if (unlikely(inode && au_is_bad_inode(inode))) {
1070 err = -EINVAL;
1071 AuTraceErr(err);
1072 goto out_dgrade;
1073 }
1074 if (unlikely(au_dbrange_test(dentry))) {
1075 err = -EINVAL;
1076 AuTraceErr(err);
1077 goto out_dgrade;
1078 }
1079
1080 sigen = au_sigen(sb);
1081 if (au_digen_test(dentry, sigen)) {
1082 AuDebugOn(IS_ROOT(dentry));
1083 err = au_reval_dpath(dentry, sigen);
1084 if (unlikely(err)) {
1085 AuTraceErr(err);
1086 goto out_dgrade;
1087 }
1088 }
1089 di_downgrade_lock(dentry, AuLock_IR);
1090
1091 err = -EINVAL;
1092 if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
1093 && inode
1094 && !(inode->i_state && I_LINKABLE)
1095 && (IS_DEADDIR(inode) || !inode->i_nlink)) {
1096 AuTraceErr(err);
1097 goto out_inval;
1098 }
1099
1100 do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
1101 if (do_udba && inode) {
1102 aufs_bindex_t btop = au_ibtop(inode);
1103 struct inode *h_inode;
1104
1105 if (btop >= 0) {
1106 h_inode = au_h_iptr(inode, btop);
1107 if (h_inode && au_test_higen(inode, h_inode)) {
1108 AuTraceErr(err);
1109 goto out_inval;
1110 }
1111 }
1112 }
1113
1114 dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
1115 err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
1116 if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1117 err = -EIO;
1118 AuDbg("both of real entry and whiteout found, %p, err %d\n",
1119 dentry, err);
1120 }
1121 goto out_inval;
1122
1123out_dgrade:
1124 di_downgrade_lock(dentry, AuLock_IR);
1125out_inval:
1126 aufs_read_unlock(dentry, AuLock_IR);
1127 AuTraceErr(err);
1128 valid = !err;
1129out:
1130 if (!valid) {
1131 AuDbg("%pd invalid, %d\n", dentry, valid);
1132 d_drop(dentry);
1133 }
1134 return valid;
1135}
1136
1137static void aufs_d_release(struct dentry *dentry)
1138{
1139 if (au_di(dentry)) {
1140 au_di_fin(dentry);
1141 au_hn_di_reinit(dentry);
1142 }
1143}
1144
1145const struct dentry_operations aufs_dop = {
1146 .d_revalidate = aufs_d_revalidate,
1147 .d_weak_revalidate = aufs_d_revalidate,
1148 .d_release = aufs_d_release
1149};
1150
1151/* aufs_dop without d_revalidate */
1152const struct dentry_operations aufs_dop_noreval = {
1153 .d_release = aufs_d_release
1154};