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