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