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