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