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