]>
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 | * branch management | |
20 | */ | |
21 | ||
22 | #include <linux/compat.h> | |
23 | #include <linux/statfs.h> | |
24 | #include "aufs.h" | |
25 | ||
26 | /* | |
27 | * free a single branch | |
28 | */ | |
29 | static void au_br_do_free(struct au_branch *br) | |
30 | { | |
31 | int i; | |
32 | struct au_wbr *wbr; | |
33 | struct au_dykey **key; | |
34 | ||
35 | au_hnotify_fin_br(br); | |
36 | ||
37 | if (br->br_xino.xi_file) | |
38 | fput(br->br_xino.xi_file); | |
39 | for (i = br->br_xino.xi_nondir.total - 1; i >= 0; i--) | |
40 | AuDebugOn(br->br_xino.xi_nondir.array[i]); | |
41 | kfree(br->br_xino.xi_nondir.array); | |
42 | ||
43 | AuDebugOn(au_br_count(br)); | |
44 | au_br_count_fin(br); | |
45 | ||
46 | wbr = br->br_wbr; | |
47 | if (wbr) { | |
48 | for (i = 0; i < AuBrWh_Last; i++) | |
49 | dput(wbr->wbr_wh[i]); | |
50 | AuDebugOn(atomic_read(&wbr->wbr_wh_running)); | |
51 | AuRwDestroy(&wbr->wbr_wh_rwsem); | |
52 | } | |
53 | ||
54 | if (br->br_fhsm) { | |
55 | au_br_fhsm_fin(br->br_fhsm); | |
56 | kfree(br->br_fhsm); | |
57 | } | |
58 | ||
59 | key = br->br_dykey; | |
60 | for (i = 0; i < AuBrDynOp; i++, key++) | |
61 | if (*key) | |
62 | au_dy_put(*key); | |
63 | else | |
64 | break; | |
65 | ||
66 | /* recursive lock, s_umount of branch's */ | |
67 | lockdep_off(); | |
68 | path_put(&br->br_path); | |
69 | lockdep_on(); | |
70 | kfree(wbr); | |
71 | kfree(br); | |
72 | } | |
73 | ||
74 | /* | |
75 | * frees all branches | |
76 | */ | |
77 | void au_br_free(struct au_sbinfo *sbinfo) | |
78 | { | |
79 | aufs_bindex_t bmax; | |
80 | struct au_branch **br; | |
81 | ||
82 | AuRwMustWriteLock(&sbinfo->si_rwsem); | |
83 | ||
84 | bmax = sbinfo->si_bbot + 1; | |
85 | br = sbinfo->si_branch; | |
86 | while (bmax--) | |
87 | au_br_do_free(*br++); | |
88 | } | |
89 | ||
90 | /* | |
91 | * find the index of a branch which is specified by @br_id. | |
92 | */ | |
93 | int au_br_index(struct super_block *sb, aufs_bindex_t br_id) | |
94 | { | |
95 | aufs_bindex_t bindex, bbot; | |
96 | ||
97 | bbot = au_sbbot(sb); | |
98 | for (bindex = 0; bindex <= bbot; bindex++) | |
99 | if (au_sbr_id(sb, bindex) == br_id) | |
100 | return bindex; | |
101 | return -1; | |
102 | } | |
103 | ||
104 | /* ---------------------------------------------------------------------- */ | |
105 | ||
106 | /* | |
107 | * add a branch | |
108 | */ | |
109 | ||
110 | static int test_overlap(struct super_block *sb, struct dentry *h_adding, | |
111 | struct dentry *h_root) | |
112 | { | |
113 | if (unlikely(h_adding == h_root | |
114 | || au_test_loopback_overlap(sb, h_adding))) | |
115 | return 1; | |
116 | if (h_adding->d_sb != h_root->d_sb) | |
117 | return 0; | |
118 | return au_test_subdir(h_adding, h_root) | |
119 | || au_test_subdir(h_root, h_adding); | |
120 | } | |
121 | ||
122 | /* | |
123 | * returns a newly allocated branch. @new_nbranch is a number of branches | |
124 | * after adding a branch. | |
125 | */ | |
126 | static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch, | |
127 | int perm) | |
128 | { | |
129 | struct au_branch *add_branch; | |
130 | struct dentry *root; | |
131 | struct inode *inode; | |
132 | int err; | |
133 | ||
134 | err = -ENOMEM; | |
135 | add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS); | |
136 | if (unlikely(!add_branch)) | |
137 | goto out; | |
138 | add_branch->br_xino.xi_nondir.total = 8; /* initial size */ | |
139 | add_branch->br_xino.xi_nondir.array | |
140 | = kcalloc(sizeof(ino_t), add_branch->br_xino.xi_nondir.total, | |
141 | GFP_NOFS); | |
142 | if (unlikely(!add_branch->br_xino.xi_nondir.array)) | |
143 | goto out_br; | |
144 | ||
145 | err = au_hnotify_init_br(add_branch, perm); | |
146 | if (unlikely(err)) | |
147 | goto out_xinondir; | |
148 | ||
149 | if (au_br_writable(perm)) { | |
150 | /* may be freed separately at changing the branch permission */ | |
151 | add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr), | |
152 | GFP_NOFS); | |
153 | if (unlikely(!add_branch->br_wbr)) | |
154 | goto out_hnotify; | |
155 | } | |
156 | ||
157 | if (au_br_fhsm(perm)) { | |
158 | err = au_fhsm_br_alloc(add_branch); | |
159 | if (unlikely(err)) | |
160 | goto out_wbr; | |
161 | } | |
162 | ||
163 | root = sb->s_root; | |
164 | err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0); | |
165 | if (!err) | |
166 | err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0); | |
167 | if (!err) { | |
168 | inode = d_inode(root); | |
169 | err = au_hinode_realloc(au_ii(inode), new_nbranch, | |
170 | /*may_shrink*/0); | |
171 | } | |
172 | if (!err) | |
173 | return add_branch; /* success */ | |
174 | ||
175 | out_wbr: | |
176 | kfree(add_branch->br_wbr); | |
177 | out_hnotify: | |
178 | au_hnotify_fin_br(add_branch); | |
179 | out_xinondir: | |
180 | kfree(add_branch->br_xino.xi_nondir.array); | |
181 | out_br: | |
182 | kfree(add_branch); | |
183 | out: | |
184 | return ERR_PTR(err); | |
185 | } | |
186 | ||
187 | /* | |
188 | * test if the branch permission is legal or not. | |
189 | */ | |
190 | static int test_br(struct inode *inode, int brperm, char *path) | |
191 | { | |
192 | int err; | |
193 | ||
194 | err = (au_br_writable(brperm) && IS_RDONLY(inode)); | |
195 | if (!err) | |
196 | goto out; | |
197 | ||
198 | err = -EINVAL; | |
199 | pr_err("write permission for readonly mount or inode, %s\n", path); | |
200 | ||
201 | out: | |
202 | return err; | |
203 | } | |
204 | ||
205 | /* | |
206 | * returns: | |
207 | * 0: success, the caller will add it | |
208 | * plus: success, it is already unified, the caller should ignore it | |
209 | * minus: error | |
210 | */ | |
211 | static int test_add(struct super_block *sb, struct au_opt_add *add, int remount) | |
212 | { | |
213 | int err; | |
214 | aufs_bindex_t bbot, bindex; | |
215 | struct dentry *root, *h_dentry; | |
216 | struct inode *inode, *h_inode; | |
217 | ||
218 | root = sb->s_root; | |
219 | bbot = au_sbbot(sb); | |
220 | if (unlikely(bbot >= 0 | |
221 | && au_find_dbindex(root, add->path.dentry) >= 0)) { | |
222 | err = 1; | |
223 | if (!remount) { | |
224 | err = -EINVAL; | |
225 | pr_err("%s duplicated\n", add->pathname); | |
226 | } | |
227 | goto out; | |
228 | } | |
229 | ||
230 | err = -ENOSPC; /* -E2BIG; */ | |
231 | if (unlikely(AUFS_BRANCH_MAX <= add->bindex | |
232 | || AUFS_BRANCH_MAX - 1 <= bbot)) { | |
233 | pr_err("number of branches exceeded %s\n", add->pathname); | |
234 | goto out; | |
235 | } | |
236 | ||
237 | err = -EDOM; | |
238 | if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) { | |
239 | pr_err("bad index %d\n", add->bindex); | |
240 | goto out; | |
241 | } | |
242 | ||
243 | inode = d_inode(add->path.dentry); | |
244 | err = -ENOENT; | |
245 | if (unlikely(!inode->i_nlink)) { | |
246 | pr_err("no existence %s\n", add->pathname); | |
247 | goto out; | |
248 | } | |
249 | ||
250 | err = -EINVAL; | |
251 | if (unlikely(inode->i_sb == sb)) { | |
252 | pr_err("%s must be outside\n", add->pathname); | |
253 | goto out; | |
254 | } | |
255 | ||
256 | if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) { | |
257 | pr_err("unsupported filesystem, %s (%s)\n", | |
258 | add->pathname, au_sbtype(inode->i_sb)); | |
259 | goto out; | |
260 | } | |
261 | ||
262 | if (unlikely(inode->i_sb->s_stack_depth)) { | |
263 | pr_err("already stacked, %s (%s)\n", | |
264 | add->pathname, au_sbtype(inode->i_sb)); | |
265 | goto out; | |
266 | } | |
267 | ||
268 | err = test_br(d_inode(add->path.dentry), add->perm, add->pathname); | |
269 | if (unlikely(err)) | |
270 | goto out; | |
271 | ||
272 | if (bbot < 0) | |
273 | return 0; /* success */ | |
274 | ||
275 | err = -EINVAL; | |
276 | for (bindex = 0; bindex <= bbot; bindex++) | |
277 | if (unlikely(test_overlap(sb, add->path.dentry, | |
278 | au_h_dptr(root, bindex)))) { | |
279 | pr_err("%s is overlapped\n", add->pathname); | |
280 | goto out; | |
281 | } | |
282 | ||
283 | err = 0; | |
284 | if (au_opt_test(au_mntflags(sb), WARN_PERM)) { | |
285 | h_dentry = au_h_dptr(root, 0); | |
286 | h_inode = d_inode(h_dentry); | |
287 | if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO) | |
288 | || !uid_eq(h_inode->i_uid, inode->i_uid) | |
289 | || !gid_eq(h_inode->i_gid, inode->i_gid)) | |
290 | pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n", | |
291 | add->pathname, | |
292 | i_uid_read(inode), i_gid_read(inode), | |
293 | (inode->i_mode & S_IALLUGO), | |
294 | i_uid_read(h_inode), i_gid_read(h_inode), | |
295 | (h_inode->i_mode & S_IALLUGO)); | |
296 | } | |
297 | ||
298 | out: | |
299 | return err; | |
300 | } | |
301 | ||
302 | /* | |
303 | * initialize or clean the whiteouts for an adding branch | |
304 | */ | |
305 | static int au_br_init_wh(struct super_block *sb, struct au_branch *br, | |
306 | int new_perm) | |
307 | { | |
308 | int err, old_perm; | |
309 | aufs_bindex_t bindex; | |
310 | struct inode *h_inode; | |
311 | struct au_wbr *wbr; | |
312 | struct au_hinode *hdir; | |
313 | struct dentry *h_dentry; | |
314 | ||
315 | err = vfsub_mnt_want_write(au_br_mnt(br)); | |
316 | if (unlikely(err)) | |
317 | goto out; | |
318 | ||
319 | wbr = br->br_wbr; | |
320 | old_perm = br->br_perm; | |
321 | br->br_perm = new_perm; | |
322 | hdir = NULL; | |
323 | h_inode = NULL; | |
324 | bindex = au_br_index(sb, br->br_id); | |
325 | if (0 <= bindex) { | |
326 | hdir = au_hi(d_inode(sb->s_root), bindex); | |
327 | au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT); | |
328 | } else { | |
329 | h_dentry = au_br_dentry(br); | |
330 | h_inode = d_inode(h_dentry); | |
331 | inode_lock_nested(h_inode, AuLsc_I_PARENT); | |
332 | } | |
333 | if (!wbr) | |
334 | err = au_wh_init(br, sb); | |
335 | else { | |
336 | wbr_wh_write_lock(wbr); | |
337 | err = au_wh_init(br, sb); | |
338 | wbr_wh_write_unlock(wbr); | |
339 | } | |
340 | if (hdir) | |
341 | au_hn_inode_unlock(hdir); | |
342 | else | |
343 | inode_unlock(h_inode); | |
344 | vfsub_mnt_drop_write(au_br_mnt(br)); | |
345 | br->br_perm = old_perm; | |
346 | ||
347 | if (!err && wbr && !au_br_writable(new_perm)) { | |
348 | kfree(wbr); | |
349 | br->br_wbr = NULL; | |
350 | } | |
351 | ||
352 | out: | |
353 | return err; | |
354 | } | |
355 | ||
356 | static int au_wbr_init(struct au_branch *br, struct super_block *sb, | |
357 | int perm) | |
358 | { | |
359 | int err; | |
360 | struct kstatfs kst; | |
361 | struct au_wbr *wbr; | |
362 | ||
363 | wbr = br->br_wbr; | |
364 | au_rw_init(&wbr->wbr_wh_rwsem); | |
365 | atomic_set(&wbr->wbr_wh_running, 0); | |
366 | ||
367 | /* | |
368 | * a limit for rmdir/rename a dir | |
369 | * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h | |
370 | */ | |
371 | err = vfs_statfs(&br->br_path, &kst); | |
372 | if (unlikely(err)) | |
373 | goto out; | |
374 | err = -EINVAL; | |
375 | if (kst.f_namelen >= NAME_MAX) | |
376 | err = au_br_init_wh(sb, br, perm); | |
377 | else | |
378 | pr_err("%pd(%s), unsupported namelen %ld\n", | |
379 | au_br_dentry(br), | |
380 | au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen); | |
381 | ||
382 | out: | |
383 | return err; | |
384 | } | |
385 | ||
386 | /* initialize a new branch */ | |
387 | static int au_br_init(struct au_branch *br, struct super_block *sb, | |
388 | struct au_opt_add *add) | |
389 | { | |
390 | int err; | |
391 | struct inode *h_inode; | |
392 | ||
393 | err = 0; | |
394 | spin_lock_init(&br->br_xino.xi_nondir.spin); | |
395 | init_waitqueue_head(&br->br_xino.xi_nondir.wqh); | |
396 | br->br_perm = add->perm; | |
397 | br->br_path = add->path; /* set first, path_get() later */ | |
398 | spin_lock_init(&br->br_dykey_lock); | |
399 | au_br_count_init(br); | |
400 | atomic_set(&br->br_xino_running, 0); | |
401 | br->br_id = au_new_br_id(sb); | |
402 | AuDebugOn(br->br_id < 0); | |
403 | ||
404 | if (au_br_writable(add->perm)) { | |
405 | err = au_wbr_init(br, sb, add->perm); | |
406 | if (unlikely(err)) | |
407 | goto out_err; | |
408 | } | |
409 | ||
410 | if (au_opt_test(au_mntflags(sb), XINO)) { | |
411 | h_inode = d_inode(add->path.dentry); | |
412 | err = au_xino_br(sb, br, h_inode->i_ino, | |
413 | au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1); | |
414 | if (unlikely(err)) { | |
415 | AuDebugOn(br->br_xino.xi_file); | |
416 | goto out_err; | |
417 | } | |
418 | } | |
419 | ||
420 | sysaufs_br_init(br); | |
421 | path_get(&br->br_path); | |
422 | goto out; /* success */ | |
423 | ||
424 | out_err: | |
425 | memset(&br->br_path, 0, sizeof(br->br_path)); | |
426 | out: | |
427 | return err; | |
428 | } | |
429 | ||
430 | static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex, | |
431 | struct au_branch *br, aufs_bindex_t bbot, | |
432 | aufs_bindex_t amount) | |
433 | { | |
434 | struct au_branch **brp; | |
435 | ||
436 | AuRwMustWriteLock(&sbinfo->si_rwsem); | |
437 | ||
438 | brp = sbinfo->si_branch + bindex; | |
439 | memmove(brp + 1, brp, sizeof(*brp) * amount); | |
440 | *brp = br; | |
441 | sbinfo->si_bbot++; | |
442 | if (unlikely(bbot < 0)) | |
443 | sbinfo->si_bbot = 0; | |
444 | } | |
445 | ||
446 | static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex, | |
447 | aufs_bindex_t bbot, aufs_bindex_t amount) | |
448 | { | |
449 | struct au_hdentry *hdp; | |
450 | ||
451 | AuRwMustWriteLock(&dinfo->di_rwsem); | |
452 | ||
453 | hdp = au_hdentry(dinfo, bindex); | |
454 | memmove(hdp + 1, hdp, sizeof(*hdp) * amount); | |
455 | au_h_dentry_init(hdp); | |
456 | dinfo->di_bbot++; | |
457 | if (unlikely(bbot < 0)) | |
458 | dinfo->di_btop = 0; | |
459 | } | |
460 | ||
461 | static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex, | |
462 | aufs_bindex_t bbot, aufs_bindex_t amount) | |
463 | { | |
464 | struct au_hinode *hip; | |
465 | ||
466 | AuRwMustWriteLock(&iinfo->ii_rwsem); | |
467 | ||
468 | hip = au_hinode(iinfo, bindex); | |
469 | memmove(hip + 1, hip, sizeof(*hip) * amount); | |
470 | au_hinode_init(hip); | |
471 | iinfo->ii_bbot++; | |
472 | if (unlikely(bbot < 0)) | |
473 | iinfo->ii_btop = 0; | |
474 | } | |
475 | ||
476 | static void au_br_do_add(struct super_block *sb, struct au_branch *br, | |
477 | aufs_bindex_t bindex) | |
478 | { | |
479 | struct dentry *root, *h_dentry; | |
480 | struct inode *root_inode, *h_inode; | |
481 | aufs_bindex_t bbot, amount; | |
482 | ||
483 | root = sb->s_root; | |
484 | root_inode = d_inode(root); | |
485 | bbot = au_sbbot(sb); | |
486 | amount = bbot + 1 - bindex; | |
487 | h_dentry = au_br_dentry(br); | |
488 | au_sbilist_lock(); | |
489 | au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount); | |
490 | au_br_do_add_hdp(au_di(root), bindex, bbot, amount); | |
491 | au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount); | |
492 | au_set_h_dptr(root, bindex, dget(h_dentry)); | |
493 | h_inode = d_inode(h_dentry); | |
494 | au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0); | |
495 | au_sbilist_unlock(); | |
496 | } | |
497 | ||
498 | int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount) | |
499 | { | |
500 | int err; | |
501 | aufs_bindex_t bbot, add_bindex; | |
502 | struct dentry *root, *h_dentry; | |
503 | struct inode *root_inode; | |
504 | struct au_branch *add_branch; | |
505 | ||
506 | root = sb->s_root; | |
507 | root_inode = d_inode(root); | |
508 | IMustLock(root_inode); | |
509 | IiMustWriteLock(root_inode); | |
510 | err = test_add(sb, add, remount); | |
511 | if (unlikely(err < 0)) | |
512 | goto out; | |
513 | if (err) { | |
514 | err = 0; | |
515 | goto out; /* success */ | |
516 | } | |
517 | ||
518 | bbot = au_sbbot(sb); | |
519 | add_branch = au_br_alloc(sb, bbot + 2, add->perm); | |
520 | err = PTR_ERR(add_branch); | |
521 | if (IS_ERR(add_branch)) | |
522 | goto out; | |
523 | ||
524 | err = au_br_init(add_branch, sb, add); | |
525 | if (unlikely(err)) { | |
526 | au_br_do_free(add_branch); | |
527 | goto out; | |
528 | } | |
529 | ||
530 | add_bindex = add->bindex; | |
531 | if (!remount) | |
532 | au_br_do_add(sb, add_branch, add_bindex); | |
533 | else { | |
534 | sysaufs_brs_del(sb, add_bindex); | |
535 | au_br_do_add(sb, add_branch, add_bindex); | |
536 | sysaufs_brs_add(sb, add_bindex); | |
537 | } | |
538 | ||
539 | h_dentry = add->path.dentry; | |
540 | if (!add_bindex) { | |
541 | au_cpup_attr_all(root_inode, /*force*/1); | |
542 | sb->s_maxbytes = h_dentry->d_sb->s_maxbytes; | |
543 | } else | |
544 | au_add_nlink(root_inode, d_inode(h_dentry)); | |
545 | ||
546 | /* | |
547 | * this test/set prevents aufs from handling unnecesary notify events | |
548 | * of xino files, in case of re-adding a writable branch which was | |
549 | * once detached from aufs. | |
550 | */ | |
551 | if (au_xino_brid(sb) < 0 | |
552 | && au_br_writable(add_branch->br_perm) | |
553 | && !au_test_fs_bad_xino(h_dentry->d_sb) | |
554 | && add_branch->br_xino.xi_file | |
555 | && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry) | |
556 | au_xino_brid_set(sb, add_branch->br_id); | |
557 | ||
558 | out: | |
559 | return err; | |
560 | } | |
561 | ||
562 | /* ---------------------------------------------------------------------- */ | |
563 | ||
564 | static unsigned long long au_farray_cb(struct super_block *sb, void *a, | |
565 | unsigned long long max __maybe_unused, | |
566 | void *arg) | |
567 | { | |
568 | unsigned long long n; | |
569 | struct file **p, *f; | |
570 | struct au_sphlhead *files; | |
571 | struct au_finfo *finfo; | |
572 | ||
573 | n = 0; | |
574 | p = a; | |
575 | files = &au_sbi(sb)->si_files; | |
576 | spin_lock(&files->spin); | |
577 | hlist_for_each_entry(finfo, &files->head, fi_hlist) { | |
578 | f = finfo->fi_file; | |
579 | if (file_count(f) | |
580 | && !special_file(file_inode(f)->i_mode)) { | |
581 | get_file(f); | |
582 | *p++ = f; | |
583 | n++; | |
584 | AuDebugOn(n > max); | |
585 | } | |
586 | } | |
587 | spin_unlock(&files->spin); | |
588 | ||
589 | return n; | |
590 | } | |
591 | ||
592 | static struct file **au_farray_alloc(struct super_block *sb, | |
593 | unsigned long long *max) | |
594 | { | |
595 | *max = au_nfiles(sb); | |
596 | return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL); | |
597 | } | |
598 | ||
599 | static void au_farray_free(struct file **a, unsigned long long max) | |
600 | { | |
601 | unsigned long long ull; | |
602 | ||
603 | for (ull = 0; ull < max; ull++) | |
604 | if (a[ull]) | |
605 | fput(a[ull]); | |
606 | kvfree(a); | |
607 | } | |
608 | ||
609 | /* ---------------------------------------------------------------------- */ | |
610 | ||
611 | /* | |
612 | * delete a branch | |
613 | */ | |
614 | ||
615 | /* to show the line number, do not make it inlined function */ | |
616 | #define AuVerbose(do_info, fmt, ...) do { \ | |
617 | if (do_info) \ | |
618 | pr_info(fmt, ##__VA_ARGS__); \ | |
619 | } while (0) | |
620 | ||
621 | static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop, | |
622 | aufs_bindex_t bbot) | |
623 | { | |
624 | return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot; | |
625 | } | |
626 | ||
627 | static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop, | |
628 | aufs_bindex_t bbot) | |
629 | { | |
630 | return au_test_ibusy(d_inode(dentry), btop, bbot); | |
631 | } | |
632 | ||
633 | /* | |
634 | * test if the branch is deletable or not. | |
635 | */ | |
636 | static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex, | |
637 | unsigned int sigen, const unsigned int verbose) | |
638 | { | |
639 | int err, i, j, ndentry; | |
640 | aufs_bindex_t btop, bbot; | |
641 | struct au_dcsub_pages dpages; | |
642 | struct au_dpage *dpage; | |
643 | struct dentry *d; | |
644 | ||
645 | err = au_dpages_init(&dpages, GFP_NOFS); | |
646 | if (unlikely(err)) | |
647 | goto out; | |
648 | err = au_dcsub_pages(&dpages, root, NULL, NULL); | |
649 | if (unlikely(err)) | |
650 | goto out_dpages; | |
651 | ||
652 | for (i = 0; !err && i < dpages.ndpage; i++) { | |
653 | dpage = dpages.dpages + i; | |
654 | ndentry = dpage->ndentry; | |
655 | for (j = 0; !err && j < ndentry; j++) { | |
656 | d = dpage->dentries[j]; | |
657 | AuDebugOn(au_dcount(d) <= 0); | |
658 | if (!au_digen_test(d, sigen)) { | |
659 | di_read_lock_child(d, AuLock_IR); | |
660 | if (unlikely(au_dbrange_test(d))) { | |
661 | di_read_unlock(d, AuLock_IR); | |
662 | continue; | |
663 | } | |
664 | } else { | |
665 | di_write_lock_child(d); | |
666 | if (unlikely(au_dbrange_test(d))) { | |
667 | di_write_unlock(d); | |
668 | continue; | |
669 | } | |
670 | err = au_reval_dpath(d, sigen); | |
671 | if (!err) | |
672 | di_downgrade_lock(d, AuLock_IR); | |
673 | else { | |
674 | di_write_unlock(d); | |
675 | break; | |
676 | } | |
677 | } | |
678 | ||
679 | /* AuDbgDentry(d); */ | |
680 | btop = au_dbtop(d); | |
681 | bbot = au_dbbot(d); | |
682 | if (btop <= bindex | |
683 | && bindex <= bbot | |
684 | && au_h_dptr(d, bindex) | |
685 | && au_test_dbusy(d, btop, bbot)) { | |
686 | err = -EBUSY; | |
687 | AuVerbose(verbose, "busy %pd\n", d); | |
688 | AuDbgDentry(d); | |
689 | } | |
690 | di_read_unlock(d, AuLock_IR); | |
691 | } | |
692 | } | |
693 | ||
694 | out_dpages: | |
695 | au_dpages_free(&dpages); | |
696 | out: | |
697 | return err; | |
698 | } | |
699 | ||
700 | static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex, | |
701 | unsigned int sigen, const unsigned int verbose) | |
702 | { | |
703 | int err; | |
704 | unsigned long long max, ull; | |
705 | struct inode *i, **array; | |
706 | aufs_bindex_t btop, bbot; | |
707 | ||
708 | array = au_iarray_alloc(sb, &max); | |
709 | err = PTR_ERR(array); | |
710 | if (IS_ERR(array)) | |
711 | goto out; | |
712 | ||
713 | err = 0; | |
714 | AuDbg("b%d\n", bindex); | |
715 | for (ull = 0; !err && ull < max; ull++) { | |
716 | i = array[ull]; | |
717 | if (unlikely(!i)) | |
718 | break; | |
719 | if (i->i_ino == AUFS_ROOT_INO) | |
720 | continue; | |
721 | ||
722 | /* AuDbgInode(i); */ | |
723 | if (au_iigen(i, NULL) == sigen) | |
724 | ii_read_lock_child(i); | |
725 | else { | |
726 | ii_write_lock_child(i); | |
727 | err = au_refresh_hinode_self(i); | |
728 | au_iigen_dec(i); | |
729 | if (!err) | |
730 | ii_downgrade_lock(i); | |
731 | else { | |
732 | ii_write_unlock(i); | |
733 | break; | |
734 | } | |
735 | } | |
736 | ||
737 | btop = au_ibtop(i); | |
738 | bbot = au_ibbot(i); | |
739 | if (btop <= bindex | |
740 | && bindex <= bbot | |
741 | && au_h_iptr(i, bindex) | |
742 | && au_test_ibusy(i, btop, bbot)) { | |
743 | err = -EBUSY; | |
744 | AuVerbose(verbose, "busy i%lu\n", i->i_ino); | |
745 | AuDbgInode(i); | |
746 | } | |
747 | ii_read_unlock(i); | |
748 | } | |
749 | au_iarray_free(array, max); | |
750 | ||
751 | out: | |
752 | return err; | |
753 | } | |
754 | ||
755 | static int test_children_busy(struct dentry *root, aufs_bindex_t bindex, | |
756 | const unsigned int verbose) | |
757 | { | |
758 | int err; | |
759 | unsigned int sigen; | |
760 | ||
761 | sigen = au_sigen(root->d_sb); | |
762 | DiMustNoWaiters(root); | |
763 | IiMustNoWaiters(d_inode(root)); | |
764 | di_write_unlock(root); | |
765 | err = test_dentry_busy(root, bindex, sigen, verbose); | |
766 | if (!err) | |
767 | err = test_inode_busy(root->d_sb, bindex, sigen, verbose); | |
768 | di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */ | |
769 | ||
770 | return err; | |
771 | } | |
772 | ||
773 | static int test_dir_busy(struct file *file, aufs_bindex_t br_id, | |
774 | struct file **to_free, int *idx) | |
775 | { | |
776 | int err; | |
777 | unsigned char matched, root; | |
778 | aufs_bindex_t bindex, bbot; | |
779 | struct au_fidir *fidir; | |
780 | struct au_hfile *hfile; | |
781 | ||
782 | err = 0; | |
783 | root = IS_ROOT(file->f_path.dentry); | |
784 | if (root) { | |
785 | get_file(file); | |
786 | to_free[*idx] = file; | |
787 | (*idx)++; | |
788 | goto out; | |
789 | } | |
790 | ||
791 | matched = 0; | |
792 | fidir = au_fi(file)->fi_hdir; | |
793 | AuDebugOn(!fidir); | |
794 | bbot = au_fbbot_dir(file); | |
795 | for (bindex = au_fbtop(file); bindex <= bbot; bindex++) { | |
796 | hfile = fidir->fd_hfile + bindex; | |
797 | if (!hfile->hf_file) | |
798 | continue; | |
799 | ||
800 | if (hfile->hf_br->br_id == br_id) { | |
801 | matched = 1; | |
802 | break; | |
803 | } | |
804 | } | |
805 | if (matched) | |
806 | err = -EBUSY; | |
807 | ||
808 | out: | |
809 | return err; | |
810 | } | |
811 | ||
812 | static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id, | |
813 | struct file **to_free, int opened) | |
814 | { | |
815 | int err, idx; | |
816 | unsigned long long ull, max; | |
817 | aufs_bindex_t btop; | |
818 | struct file *file, **array; | |
819 | struct dentry *root; | |
820 | struct au_hfile *hfile; | |
821 | ||
822 | array = au_farray_alloc(sb, &max); | |
823 | err = PTR_ERR(array); | |
824 | if (IS_ERR(array)) | |
825 | goto out; | |
826 | ||
827 | err = 0; | |
828 | idx = 0; | |
829 | root = sb->s_root; | |
830 | di_write_unlock(root); | |
831 | for (ull = 0; ull < max; ull++) { | |
832 | file = array[ull]; | |
833 | if (unlikely(!file)) | |
834 | break; | |
835 | ||
836 | /* AuDbg("%pD\n", file); */ | |
837 | fi_read_lock(file); | |
838 | btop = au_fbtop(file); | |
839 | if (!d_is_dir(file->f_path.dentry)) { | |
840 | hfile = &au_fi(file)->fi_htop; | |
841 | if (hfile->hf_br->br_id == br_id) | |
842 | err = -EBUSY; | |
843 | } else | |
844 | err = test_dir_busy(file, br_id, to_free, &idx); | |
845 | fi_read_unlock(file); | |
846 | if (unlikely(err)) | |
847 | break; | |
848 | } | |
849 | di_write_lock_child(root); | |
850 | au_farray_free(array, max); | |
851 | AuDebugOn(idx > opened); | |
852 | ||
853 | out: | |
854 | return err; | |
855 | } | |
856 | ||
857 | static void br_del_file(struct file **to_free, unsigned long long opened, | |
858 | aufs_bindex_t br_id) | |
859 | { | |
860 | unsigned long long ull; | |
861 | aufs_bindex_t bindex, btop, bbot, bfound; | |
862 | struct file *file; | |
863 | struct au_fidir *fidir; | |
864 | struct au_hfile *hfile; | |
865 | ||
866 | for (ull = 0; ull < opened; ull++) { | |
867 | file = to_free[ull]; | |
868 | if (unlikely(!file)) | |
869 | break; | |
870 | ||
871 | /* AuDbg("%pD\n", file); */ | |
872 | AuDebugOn(!d_is_dir(file->f_path.dentry)); | |
873 | bfound = -1; | |
874 | fidir = au_fi(file)->fi_hdir; | |
875 | AuDebugOn(!fidir); | |
876 | fi_write_lock(file); | |
877 | btop = au_fbtop(file); | |
878 | bbot = au_fbbot_dir(file); | |
879 | for (bindex = btop; bindex <= bbot; bindex++) { | |
880 | hfile = fidir->fd_hfile + bindex; | |
881 | if (!hfile->hf_file) | |
882 | continue; | |
883 | ||
884 | if (hfile->hf_br->br_id == br_id) { | |
885 | bfound = bindex; | |
886 | break; | |
887 | } | |
888 | } | |
889 | AuDebugOn(bfound < 0); | |
890 | au_set_h_fptr(file, bfound, NULL); | |
891 | if (bfound == btop) { | |
892 | for (btop++; btop <= bbot; btop++) | |
893 | if (au_hf_dir(file, btop)) { | |
894 | au_set_fbtop(file, btop); | |
895 | break; | |
896 | } | |
897 | } | |
898 | fi_write_unlock(file); | |
899 | } | |
900 | } | |
901 | ||
902 | static void au_br_do_del_brp(struct au_sbinfo *sbinfo, | |
903 | const aufs_bindex_t bindex, | |
904 | const aufs_bindex_t bbot) | |
905 | { | |
906 | struct au_branch **brp, **p; | |
907 | ||
908 | AuRwMustWriteLock(&sbinfo->si_rwsem); | |
909 | ||
910 | brp = sbinfo->si_branch + bindex; | |
911 | if (bindex < bbot) | |
912 | memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex)); | |
913 | sbinfo->si_branch[0 + bbot] = NULL; | |
914 | sbinfo->si_bbot--; | |
915 | ||
916 | p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST, | |
917 | /*may_shrink*/1); | |
918 | if (p) | |
919 | sbinfo->si_branch = p; | |
920 | /* harmless error */ | |
921 | } | |
922 | ||
923 | static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex, | |
924 | const aufs_bindex_t bbot) | |
925 | { | |
926 | struct au_hdentry *hdp, *p; | |
927 | ||
928 | AuRwMustWriteLock(&dinfo->di_rwsem); | |
929 | ||
930 | hdp = au_hdentry(dinfo, bindex); | |
931 | if (bindex < bbot) | |
932 | memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex)); | |
933 | /* au_h_dentry_init(au_hdentry(dinfo, bbot); */ | |
934 | dinfo->di_bbot--; | |
935 | ||
936 | p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST, | |
937 | /*may_shrink*/1); | |
938 | if (p) | |
939 | dinfo->di_hdentry = p; | |
940 | /* harmless error */ | |
941 | } | |
942 | ||
943 | static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex, | |
944 | const aufs_bindex_t bbot) | |
945 | { | |
946 | struct au_hinode *hip, *p; | |
947 | ||
948 | AuRwMustWriteLock(&iinfo->ii_rwsem); | |
949 | ||
950 | hip = au_hinode(iinfo, bindex); | |
951 | if (bindex < bbot) | |
952 | memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex)); | |
953 | /* au_hinode_init(au_hinode(iinfo, bbot)); */ | |
954 | iinfo->ii_bbot--; | |
955 | ||
956 | p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST, | |
957 | /*may_shrink*/1); | |
958 | if (p) | |
959 | iinfo->ii_hinode = p; | |
960 | /* harmless error */ | |
961 | } | |
962 | ||
963 | static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex, | |
964 | struct au_branch *br) | |
965 | { | |
966 | aufs_bindex_t bbot; | |
967 | struct au_sbinfo *sbinfo; | |
968 | struct dentry *root, *h_root; | |
969 | struct inode *inode, *h_inode; | |
970 | struct au_hinode *hinode; | |
971 | ||
972 | SiMustWriteLock(sb); | |
973 | ||
974 | root = sb->s_root; | |
975 | inode = d_inode(root); | |
976 | sbinfo = au_sbi(sb); | |
977 | bbot = sbinfo->si_bbot; | |
978 | ||
979 | h_root = au_h_dptr(root, bindex); | |
980 | hinode = au_hi(inode, bindex); | |
981 | h_inode = au_igrab(hinode->hi_inode); | |
982 | au_hiput(hinode); | |
983 | ||
984 | au_sbilist_lock(); | |
985 | au_br_do_del_brp(sbinfo, bindex, bbot); | |
986 | au_br_do_del_hdp(au_di(root), bindex, bbot); | |
987 | au_br_do_del_hip(au_ii(inode), bindex, bbot); | |
988 | au_sbilist_unlock(); | |
989 | ||
990 | dput(h_root); | |
991 | iput(h_inode); | |
992 | au_br_do_free(br); | |
993 | } | |
994 | ||
995 | static unsigned long long empty_cb(struct super_block *sb, void *array, | |
996 | unsigned long long max, void *arg) | |
997 | { | |
998 | return max; | |
999 | } | |
1000 | ||
1001 | int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount) | |
1002 | { | |
1003 | int err, rerr, i; | |
1004 | unsigned long long opened; | |
1005 | unsigned int mnt_flags; | |
1006 | aufs_bindex_t bindex, bbot, br_id; | |
1007 | unsigned char do_wh, verbose; | |
1008 | struct au_branch *br; | |
1009 | struct au_wbr *wbr; | |
1010 | struct dentry *root; | |
1011 | struct file **to_free; | |
1012 | ||
1013 | err = 0; | |
1014 | opened = 0; | |
1015 | to_free = NULL; | |
1016 | root = sb->s_root; | |
1017 | bindex = au_find_dbindex(root, del->h_path.dentry); | |
1018 | if (bindex < 0) { | |
1019 | if (remount) | |
1020 | goto out; /* success */ | |
1021 | err = -ENOENT; | |
1022 | pr_err("%s no such branch\n", del->pathname); | |
1023 | goto out; | |
1024 | } | |
1025 | AuDbg("bindex b%d\n", bindex); | |
1026 | ||
1027 | err = -EBUSY; | |
1028 | mnt_flags = au_mntflags(sb); | |
1029 | verbose = !!au_opt_test(mnt_flags, VERBOSE); | |
1030 | bbot = au_sbbot(sb); | |
1031 | if (unlikely(!bbot)) { | |
1032 | AuVerbose(verbose, "no more branches left\n"); | |
1033 | goto out; | |
1034 | } | |
1035 | br = au_sbr(sb, bindex); | |
1036 | AuDebugOn(!path_equal(&br->br_path, &del->h_path)); | |
1037 | ||
1038 | br_id = br->br_id; | |
1039 | opened = au_br_count(br); | |
1040 | if (unlikely(opened)) { | |
1041 | to_free = au_array_alloc(&opened, empty_cb, sb, NULL); | |
1042 | err = PTR_ERR(to_free); | |
1043 | if (IS_ERR(to_free)) | |
1044 | goto out; | |
1045 | ||
1046 | err = test_file_busy(sb, br_id, to_free, opened); | |
1047 | if (unlikely(err)) { | |
1048 | AuVerbose(verbose, "%llu file(s) opened\n", opened); | |
1049 | goto out; | |
1050 | } | |
1051 | } | |
1052 | ||
1053 | wbr = br->br_wbr; | |
1054 | do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph); | |
1055 | if (do_wh) { | |
1056 | /* instead of WbrWhMustWriteLock(wbr) */ | |
1057 | SiMustWriteLock(sb); | |
1058 | for (i = 0; i < AuBrWh_Last; i++) { | |
1059 | dput(wbr->wbr_wh[i]); | |
1060 | wbr->wbr_wh[i] = NULL; | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | err = test_children_busy(root, bindex, verbose); | |
1065 | if (unlikely(err)) { | |
1066 | if (do_wh) | |
1067 | goto out_wh; | |
1068 | goto out; | |
1069 | } | |
1070 | ||
1071 | err = 0; | |
1072 | if (to_free) { | |
1073 | /* | |
1074 | * now we confirmed the branch is deletable. | |
1075 | * let's free the remaining opened dirs on the branch. | |
1076 | */ | |
1077 | di_write_unlock(root); | |
1078 | br_del_file(to_free, opened, br_id); | |
1079 | di_write_lock_child(root); | |
1080 | } | |
1081 | ||
1082 | if (!remount) | |
1083 | au_br_do_del(sb, bindex, br); | |
1084 | else { | |
1085 | sysaufs_brs_del(sb, bindex); | |
1086 | au_br_do_del(sb, bindex, br); | |
1087 | sysaufs_brs_add(sb, bindex); | |
1088 | } | |
1089 | ||
1090 | if (!bindex) { | |
1091 | au_cpup_attr_all(d_inode(root), /*force*/1); | |
1092 | sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes; | |
1093 | } else | |
1094 | au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry)); | |
1095 | if (au_opt_test(mnt_flags, PLINK)) | |
1096 | au_plink_half_refresh(sb, br_id); | |
1097 | ||
1098 | if (au_xino_brid(sb) == br_id) | |
1099 | au_xino_brid_set(sb, -1); | |
1100 | goto out; /* success */ | |
1101 | ||
1102 | out_wh: | |
1103 | /* revert */ | |
1104 | rerr = au_br_init_wh(sb, br, br->br_perm); | |
1105 | if (rerr) | |
1106 | pr_warn("failed re-creating base whiteout, %s. (%d)\n", | |
1107 | del->pathname, rerr); | |
1108 | out: | |
1109 | if (to_free) | |
1110 | au_farray_free(to_free, opened); | |
1111 | return err; | |
1112 | } | |
1113 | ||
1114 | /* ---------------------------------------------------------------------- */ | |
1115 | ||
1116 | static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg) | |
1117 | { | |
1118 | int err; | |
1119 | aufs_bindex_t btop, bbot; | |
1120 | struct aufs_ibusy ibusy; | |
1121 | struct inode *inode, *h_inode; | |
1122 | ||
1123 | err = -EPERM; | |
1124 | if (unlikely(!capable(CAP_SYS_ADMIN))) | |
1125 | goto out; | |
1126 | ||
1127 | err = copy_from_user(&ibusy, arg, sizeof(ibusy)); | |
1128 | if (!err) | |
1129 | err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino)); | |
1130 | if (unlikely(err)) { | |
1131 | err = -EFAULT; | |
1132 | AuTraceErr(err); | |
1133 | goto out; | |
1134 | } | |
1135 | ||
1136 | err = -EINVAL; | |
1137 | si_read_lock(sb, AuLock_FLUSH); | |
1138 | if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb))) | |
1139 | goto out_unlock; | |
1140 | ||
1141 | err = 0; | |
1142 | ibusy.h_ino = 0; /* invalid */ | |
1143 | inode = ilookup(sb, ibusy.ino); | |
1144 | if (!inode | |
1145 | || inode->i_ino == AUFS_ROOT_INO | |
1146 | || au_is_bad_inode(inode)) | |
1147 | goto out_unlock; | |
1148 | ||
1149 | ii_read_lock_child(inode); | |
1150 | btop = au_ibtop(inode); | |
1151 | bbot = au_ibbot(inode); | |
1152 | if (btop <= ibusy.bindex && ibusy.bindex <= bbot) { | |
1153 | h_inode = au_h_iptr(inode, ibusy.bindex); | |
1154 | if (h_inode && au_test_ibusy(inode, btop, bbot)) | |
1155 | ibusy.h_ino = h_inode->i_ino; | |
1156 | } | |
1157 | ii_read_unlock(inode); | |
1158 | iput(inode); | |
1159 | ||
1160 | out_unlock: | |
1161 | si_read_unlock(sb); | |
1162 | if (!err) { | |
1163 | err = __put_user(ibusy.h_ino, &arg->h_ino); | |
1164 | if (unlikely(err)) { | |
1165 | err = -EFAULT; | |
1166 | AuTraceErr(err); | |
1167 | } | |
1168 | } | |
1169 | out: | |
1170 | return err; | |
1171 | } | |
1172 | ||
1173 | long au_ibusy_ioctl(struct file *file, unsigned long arg) | |
1174 | { | |
1175 | return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg); | |
1176 | } | |
1177 | ||
1178 | #ifdef CONFIG_COMPAT | |
1179 | long au_ibusy_compat_ioctl(struct file *file, unsigned long arg) | |
1180 | { | |
1181 | return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg)); | |
1182 | } | |
1183 | #endif | |
1184 | ||
1185 | /* ---------------------------------------------------------------------- */ | |
1186 | ||
1187 | /* | |
1188 | * change a branch permission | |
1189 | */ | |
1190 | ||
1191 | static void au_warn_ima(void) | |
1192 | { | |
1193 | #ifdef CONFIG_IMA | |
1194 | /* since it doesn't support mark_files_ro() */ | |
1195 | AuWarn1("RW -> RO makes IMA to produce wrong message\n"); | |
1196 | #endif | |
1197 | } | |
1198 | ||
1199 | static int do_need_sigen_inc(int a, int b) | |
1200 | { | |
1201 | return au_br_whable(a) && !au_br_whable(b); | |
1202 | } | |
1203 | ||
1204 | static int need_sigen_inc(int old, int new) | |
1205 | { | |
1206 | return do_need_sigen_inc(old, new) | |
1207 | || do_need_sigen_inc(new, old); | |
1208 | } | |
1209 | ||
1210 | static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex) | |
1211 | { | |
1212 | int err, do_warn; | |
1213 | unsigned int mnt_flags; | |
1214 | unsigned long long ull, max; | |
1215 | aufs_bindex_t br_id; | |
1216 | unsigned char verbose, writer; | |
1217 | struct file *file, *hf, **array; | |
1218 | struct au_hfile *hfile; | |
1219 | ||
1220 | mnt_flags = au_mntflags(sb); | |
1221 | verbose = !!au_opt_test(mnt_flags, VERBOSE); | |
1222 | ||
1223 | array = au_farray_alloc(sb, &max); | |
1224 | err = PTR_ERR(array); | |
1225 | if (IS_ERR(array)) | |
1226 | goto out; | |
1227 | ||
1228 | do_warn = 0; | |
1229 | br_id = au_sbr_id(sb, bindex); | |
1230 | for (ull = 0; ull < max; ull++) { | |
1231 | file = array[ull]; | |
1232 | if (unlikely(!file)) | |
1233 | break; | |
1234 | ||
1235 | /* AuDbg("%pD\n", file); */ | |
1236 | fi_read_lock(file); | |
1237 | if (unlikely(au_test_mmapped(file))) { | |
1238 | err = -EBUSY; | |
1239 | AuVerbose(verbose, "mmapped %pD\n", file); | |
1240 | AuDbgFile(file); | |
1241 | FiMustNoWaiters(file); | |
1242 | fi_read_unlock(file); | |
1243 | goto out_array; | |
1244 | } | |
1245 | ||
1246 | hfile = &au_fi(file)->fi_htop; | |
1247 | hf = hfile->hf_file; | |
1248 | if (!d_is_reg(file->f_path.dentry) | |
1249 | || !(file->f_mode & FMODE_WRITE) | |
1250 | || hfile->hf_br->br_id != br_id | |
1251 | || !(hf->f_mode & FMODE_WRITE)) | |
1252 | array[ull] = NULL; | |
1253 | else { | |
1254 | do_warn = 1; | |
1255 | get_file(file); | |
1256 | } | |
1257 | ||
1258 | FiMustNoWaiters(file); | |
1259 | fi_read_unlock(file); | |
1260 | fput(file); | |
1261 | } | |
1262 | ||
1263 | err = 0; | |
1264 | if (do_warn) | |
1265 | au_warn_ima(); | |
1266 | ||
1267 | for (ull = 0; ull < max; ull++) { | |
1268 | file = array[ull]; | |
1269 | if (!file) | |
1270 | continue; | |
1271 | ||
1272 | /* todo: already flushed? */ | |
1273 | /* | |
1274 | * fs/super.c:mark_files_ro() is gone, but aufs keeps its | |
1275 | * approach which resets f_mode and calls mnt_drop_write() and | |
1276 | * file_release_write() for each file, because the branch | |
1277 | * attribute in aufs world is totally different from the native | |
1278 | * fs rw/ro mode. | |
1279 | */ | |
1280 | /* fi_read_lock(file); */ | |
1281 | hfile = &au_fi(file)->fi_htop; | |
1282 | hf = hfile->hf_file; | |
1283 | /* fi_read_unlock(file); */ | |
1284 | spin_lock(&hf->f_lock); | |
1285 | writer = !!(hf->f_mode & FMODE_WRITER); | |
1286 | hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER); | |
1287 | spin_unlock(&hf->f_lock); | |
1288 | if (writer) { | |
1289 | put_write_access(file_inode(hf)); | |
1290 | __mnt_drop_write(hf->f_path.mnt); | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | out_array: | |
1295 | au_farray_free(array, max); | |
1296 | out: | |
1297 | AuTraceErr(err); | |
1298 | return err; | |
1299 | } | |
1300 | ||
1301 | int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount, | |
1302 | int *do_refresh) | |
1303 | { | |
1304 | int err, rerr; | |
1305 | aufs_bindex_t bindex; | |
1306 | struct dentry *root; | |
1307 | struct au_branch *br; | |
1308 | struct au_br_fhsm *bf; | |
1309 | ||
1310 | root = sb->s_root; | |
1311 | bindex = au_find_dbindex(root, mod->h_root); | |
1312 | if (bindex < 0) { | |
1313 | if (remount) | |
1314 | return 0; /* success */ | |
1315 | err = -ENOENT; | |
1316 | pr_err("%s no such branch\n", mod->path); | |
1317 | goto out; | |
1318 | } | |
1319 | AuDbg("bindex b%d\n", bindex); | |
1320 | ||
1321 | err = test_br(d_inode(mod->h_root), mod->perm, mod->path); | |
1322 | if (unlikely(err)) | |
1323 | goto out; | |
1324 | ||
1325 | br = au_sbr(sb, bindex); | |
1326 | AuDebugOn(mod->h_root != au_br_dentry(br)); | |
1327 | if (br->br_perm == mod->perm) | |
1328 | return 0; /* success */ | |
1329 | ||
1330 | /* pre-allocate for non-fhsm --> fhsm */ | |
1331 | bf = NULL; | |
1332 | if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) { | |
1333 | err = au_fhsm_br_alloc(br); | |
1334 | if (unlikely(err)) | |
1335 | goto out; | |
1336 | bf = br->br_fhsm; | |
1337 | br->br_fhsm = NULL; | |
1338 | } | |
1339 | ||
1340 | if (au_br_writable(br->br_perm)) { | |
1341 | /* remove whiteout base */ | |
1342 | err = au_br_init_wh(sb, br, mod->perm); | |
1343 | if (unlikely(err)) | |
1344 | goto out_bf; | |
1345 | ||
1346 | if (!au_br_writable(mod->perm)) { | |
1347 | /* rw --> ro, file might be mmapped */ | |
1348 | DiMustNoWaiters(root); | |
1349 | IiMustNoWaiters(d_inode(root)); | |
1350 | di_write_unlock(root); | |
1351 | err = au_br_mod_files_ro(sb, bindex); | |
1352 | /* aufs_write_lock() calls ..._child() */ | |
1353 | di_write_lock_child(root); | |
1354 | ||
1355 | if (unlikely(err)) { | |
1356 | rerr = -ENOMEM; | |
1357 | br->br_wbr = kzalloc(sizeof(*br->br_wbr), | |
1358 | GFP_NOFS); | |
1359 | if (br->br_wbr) | |
1360 | rerr = au_wbr_init(br, sb, br->br_perm); | |
1361 | if (unlikely(rerr)) { | |
1362 | AuIOErr("nested error %d (%d)\n", | |
1363 | rerr, err); | |
1364 | br->br_perm = mod->perm; | |
1365 | } | |
1366 | } | |
1367 | } | |
1368 | } else if (au_br_writable(mod->perm)) { | |
1369 | /* ro --> rw */ | |
1370 | err = -ENOMEM; | |
1371 | br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS); | |
1372 | if (br->br_wbr) { | |
1373 | err = au_wbr_init(br, sb, mod->perm); | |
1374 | if (unlikely(err)) { | |
1375 | kfree(br->br_wbr); | |
1376 | br->br_wbr = NULL; | |
1377 | } | |
1378 | } | |
1379 | } | |
1380 | if (unlikely(err)) | |
1381 | goto out_bf; | |
1382 | ||
1383 | if (au_br_fhsm(br->br_perm)) { | |
1384 | if (!au_br_fhsm(mod->perm)) { | |
1385 | /* fhsm --> non-fhsm */ | |
1386 | au_br_fhsm_fin(br->br_fhsm); | |
1387 | kfree(br->br_fhsm); | |
1388 | br->br_fhsm = NULL; | |
1389 | } | |
1390 | } else if (au_br_fhsm(mod->perm)) | |
1391 | /* non-fhsm --> fhsm */ | |
1392 | br->br_fhsm = bf; | |
1393 | ||
1394 | *do_refresh |= need_sigen_inc(br->br_perm, mod->perm); | |
1395 | br->br_perm = mod->perm; | |
1396 | goto out; /* success */ | |
1397 | ||
1398 | out_bf: | |
1399 | kfree(bf); | |
1400 | out: | |
1401 | AuTraceErr(err); | |
1402 | return err; | |
1403 | } | |
1404 | ||
1405 | /* ---------------------------------------------------------------------- */ | |
1406 | ||
1407 | int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs) | |
1408 | { | |
1409 | int err; | |
1410 | struct kstatfs kstfs; | |
1411 | ||
1412 | err = vfs_statfs(&br->br_path, &kstfs); | |
1413 | if (!err) { | |
1414 | stfs->f_blocks = kstfs.f_blocks; | |
1415 | stfs->f_bavail = kstfs.f_bavail; | |
1416 | stfs->f_files = kstfs.f_files; | |
1417 | stfs->f_ffree = kstfs.f_ffree; | |
1418 | } | |
1419 | ||
1420 | return err; | |
1421 | } |