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