aufs: fix up for the new stable kernel
[pandora-kernel.git] / fs / aufs / branch.c
1 /*
2  * Copyright (C) 2005-2013 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * branch management
21  */
22
23 #include <linux/compat.h>
24 #include <linux/statfs.h>
25 #include "aufs.h"
26
27 /*
28  * free a single branch
29  */
30
31 /* prohibit rmdir to the root of the branch */
32 /* todo: another new flag? */
33 static void au_br_dflags_force(struct au_branch *br)
34 {
35         struct dentry *h_dentry;
36
37         h_dentry = au_br_dentry(br);
38         spin_lock(&h_dentry->d_lock);
39         br->br_dflags = h_dentry->d_flags & DCACHE_MOUNTED;
40         h_dentry->d_flags |= DCACHE_MOUNTED;
41         spin_unlock(&h_dentry->d_lock);
42 }
43
44 /* restore its d_flags */
45 static void au_br_dflags_restore(struct au_branch *br)
46 {
47         struct dentry *h_dentry;
48
49         if (br->br_dflags)
50                 return;
51
52         h_dentry = au_br_dentry(br);
53         spin_lock(&h_dentry->d_lock);
54         h_dentry->d_flags &= ~DCACHE_MOUNTED;
55         spin_unlock(&h_dentry->d_lock);
56 }
57
58 static void au_br_do_free(struct au_branch *br)
59 {
60         int i;
61         struct au_wbr *wbr;
62         struct au_dykey **key;
63
64         au_hnotify_fin_br(br);
65
66         if (br->br_xino.xi_file)
67                 fput(br->br_xino.xi_file);
68         mutex_destroy(&br->br_xino.xi_nondir_mtx);
69
70         AuDebugOn(atomic_read(&br->br_count));
71
72         wbr = br->br_wbr;
73         if (wbr) {
74                 for (i = 0; i < AuBrWh_Last; i++)
75                         dput(wbr->wbr_wh[i]);
76                 AuDebugOn(atomic_read(&wbr->wbr_wh_running));
77                 AuRwDestroy(&wbr->wbr_wh_rwsem);
78         }
79
80         key = br->br_dykey;
81         for (i = 0; i < AuBrDynOp; i++, key++)
82                 if (*key)
83                         au_dy_put(*key);
84                 else
85                         break;
86
87         au_br_dflags_restore(br);
88
89         /* recursive lock, s_umount of branch's */
90         lockdep_off();
91         path_put(&br->br_path);
92         lockdep_on();
93         kfree(wbr);
94         kfree(br);
95 }
96
97 /*
98  * frees all branches
99  */
100 void au_br_free(struct au_sbinfo *sbinfo)
101 {
102         aufs_bindex_t bmax;
103         struct au_branch **br;
104
105         AuRwMustWriteLock(&sbinfo->si_rwsem);
106
107         bmax = sbinfo->si_bend + 1;
108         br = sbinfo->si_branch;
109         while (bmax--)
110                 au_br_do_free(*br++);
111 }
112
113 /*
114  * find the index of a branch which is specified by @br_id.
115  */
116 int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
117 {
118         aufs_bindex_t bindex, bend;
119
120         bend = au_sbend(sb);
121         for (bindex = 0; bindex <= bend; bindex++)
122                 if (au_sbr_id(sb, bindex) == br_id)
123                         return bindex;
124         return -1;
125 }
126
127 /* ---------------------------------------------------------------------- */
128
129 /*
130  * add a branch
131  */
132
133 static int test_overlap(struct super_block *sb, struct dentry *h_adding,
134                         struct dentry *h_root)
135 {
136         if (unlikely(h_adding == h_root
137                      || au_test_loopback_overlap(sb, h_adding)))
138                 return 1;
139         if (h_adding->d_sb != h_root->d_sb)
140                 return 0;
141         return au_test_subdir(h_adding, h_root)
142                 || au_test_subdir(h_root, h_adding);
143 }
144
145 /*
146  * returns a newly allocated branch. @new_nbranch is a number of branches
147  * after adding a branch.
148  */
149 static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
150                                      int perm)
151 {
152         struct au_branch *add_branch;
153         struct dentry *root;
154         int err;
155
156         err = -ENOMEM;
157         root = sb->s_root;
158         add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
159         if (unlikely(!add_branch))
160                 goto out;
161
162         err = au_hnotify_init_br(add_branch, perm);
163         if (unlikely(err))
164                 goto out_br;
165
166         add_branch->br_wbr = NULL;
167         if (au_br_writable(perm)) {
168                 /* may be freed separately at changing the branch permission */
169                 add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
170                                              GFP_NOFS);
171                 if (unlikely(!add_branch->br_wbr))
172                         goto out_hnotify;
173         }
174
175         err = au_sbr_realloc(au_sbi(sb), new_nbranch);
176         if (!err)
177                 err = au_di_realloc(au_di(root), new_nbranch);
178         if (!err)
179                 err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
180         if (!err)
181                 return add_branch; /* success */
182
183         kfree(add_branch->br_wbr);
184
185 out_hnotify:
186         au_hnotify_fin_br(add_branch);
187 out_br:
188         kfree(add_branch);
189 out:
190         return ERR_PTR(err);
191 }
192
193 /*
194  * test if the branch permission is legal or not.
195  */
196 static int test_br(struct inode *inode, int brperm, char *path)
197 {
198         int err;
199
200         err = (au_br_writable(brperm) && IS_RDONLY(inode));
201         if (!err)
202                 goto out;
203
204         err = -EINVAL;
205         pr_err("write permission for readonly mount or inode, %s\n", path);
206
207 out:
208         return err;
209 }
210
211 /*
212  * returns:
213  * 0: success, the caller will add it
214  * plus: success, it is already unified, the caller should ignore it
215  * minus: error
216  */
217 static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
218 {
219         int err;
220         aufs_bindex_t bend, bindex;
221         struct dentry *root;
222         struct inode *inode, *h_inode;
223
224         root = sb->s_root;
225         bend = au_sbend(sb);
226         if (unlikely(bend >= 0
227                      && au_find_dbindex(root, add->path.dentry) >= 0)) {
228                 err = 1;
229                 if (!remount) {
230                         err = -EINVAL;
231                         pr_err("%s duplicated\n", add->pathname);
232                 }
233                 goto out;
234         }
235
236         err = -ENOSPC; /* -E2BIG; */
237         if (unlikely(AUFS_BRANCH_MAX <= add->bindex
238                      || AUFS_BRANCH_MAX - 1 <= bend)) {
239                 pr_err("number of branches exceeded %s\n", add->pathname);
240                 goto out;
241         }
242
243         err = -EDOM;
244         if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
245                 pr_err("bad index %d\n", add->bindex);
246                 goto out;
247         }
248
249         inode = add->path.dentry->d_inode;
250         err = -ENOENT;
251         if (unlikely(!inode->i_nlink)) {
252                 pr_err("no existence %s\n", add->pathname);
253                 goto out;
254         }
255
256         err = -EINVAL;
257         if (unlikely(inode->i_sb == sb)) {
258                 pr_err("%s must be outside\n", add->pathname);
259                 goto out;
260         }
261
262         if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
263                 pr_err("unsupported filesystem, %s (%s)\n",
264                        add->pathname, au_sbtype(inode->i_sb));
265                 goto out;
266         }
267
268         err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
269         if (unlikely(err))
270                 goto out;
271
272         if (bend < 0)
273                 return 0; /* success */
274
275         err = -EINVAL;
276         for (bindex = 0; bindex <= bend; 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_inode = au_h_dptr(root, 0)->d_inode;
286                 if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
287                     || h_inode->i_uid != inode->i_uid
288                     || h_inode->i_gid != inode->i_gid)
289                         pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
290                                 add->pathname,
291                                 inode->i_uid, inode->i_gid,
292                                 (inode->i_mode & S_IALLUGO),
293                                 h_inode->i_uid, h_inode->i_gid,
294                                 (h_inode->i_mode & S_IALLUGO));
295         }
296
297 out:
298         return err;
299 }
300
301 /*
302  * initialize or clean the whiteouts for an adding branch
303  */
304 static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
305                          int new_perm)
306 {
307         int err, old_perm;
308         aufs_bindex_t bindex;
309         struct mutex *h_mtx;
310         struct au_wbr *wbr;
311         struct au_hinode *hdir;
312
313         wbr = br->br_wbr;
314         old_perm = br->br_perm;
315         br->br_perm = new_perm;
316         hdir = NULL;
317         h_mtx = NULL;
318         bindex = au_br_index(sb, br->br_id);
319         if (0 <= bindex) {
320                 hdir = au_hi(sb->s_root->d_inode, bindex);
321                 au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
322         } else {
323                 h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
324                 mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
325         }
326         if (!wbr)
327                 err = au_wh_init(br, sb);
328         else {
329                 wbr_wh_write_lock(wbr);
330                 err = au_wh_init(br, sb);
331                 wbr_wh_write_unlock(wbr);
332         }
333         if (hdir)
334                 au_hn_imtx_unlock(hdir);
335         else
336                 mutex_unlock(h_mtx);
337         br->br_perm = old_perm;
338
339         if (!err && wbr && !au_br_writable(new_perm)) {
340                 kfree(wbr);
341                 br->br_wbr = NULL;
342         }
343
344         return err;
345 }
346
347 static 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         memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
357         atomic_set(&wbr->wbr_wh_running, 0);
358         wbr->wbr_bytes = 0;
359
360         /*
361          * a limit for rmdir/rename a dir
362          * cf. AUFS_MAX_NAMELEN in include/linux/aufs_type.h
363          */
364         err = vfs_statfs(&br->br_path, &kst);
365         if (unlikely(err))
366                 goto out;
367         err = -EINVAL;
368         if (kst.f_namelen >= NAME_MAX)
369                 err = au_br_init_wh(sb, br, perm);
370         else
371                 pr_err("%.*s(%s), unsupported namelen %ld\n",
372                        AuDLNPair(au_br_dentry(br)),
373                        au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
374
375 out:
376         return err;
377 }
378
379 /* intialize a new branch */
380 static int au_br_init(struct au_branch *br, struct super_block *sb,
381                       struct au_opt_add *add)
382 {
383         int err;
384
385         err = 0;
386         memset(&br->br_xino, 0, sizeof(br->br_xino));
387         mutex_init(&br->br_xino.xi_nondir_mtx);
388         br->br_perm = add->perm;
389         BUILD_BUG_ON(sizeof(br->br_dflags)
390                      != sizeof(br->br_path.dentry->d_flags));
391         br->br_dflags = DCACHE_MOUNTED;
392         br->br_path = add->path; /* set first, path_get() later */
393         spin_lock_init(&br->br_dykey_lock);
394         memset(br->br_dykey, 0, sizeof(br->br_dykey));
395         atomic_set(&br->br_count, 0);
396         atomic_set(&br->br_xino_running, 0);
397         br->br_id = au_new_br_id(sb);
398         AuDebugOn(br->br_id < 0);
399
400         if (au_br_writable(add->perm)) {
401                 err = au_wbr_init(br, sb, add->perm);
402                 if (unlikely(err))
403                         goto out_err;
404         }
405
406         if (au_opt_test(au_mntflags(sb), XINO)) {
407                 err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
408                                  au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
409                 if (unlikely(err)) {
410                         AuDebugOn(br->br_xino.xi_file);
411                         goto out_err;
412                 }
413         }
414
415         sysaufs_br_init(br);
416         path_get(&br->br_path);
417         goto out; /* success */
418
419 out_err:
420         memset(&br->br_path, 0, sizeof(br->br_path));
421 out:
422         return err;
423 }
424
425 static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
426                              struct au_branch *br, aufs_bindex_t bend,
427                              aufs_bindex_t amount)
428 {
429         struct au_branch **brp;
430
431         AuRwMustWriteLock(&sbinfo->si_rwsem);
432
433         brp = sbinfo->si_branch + bindex;
434         memmove(brp + 1, brp, sizeof(*brp) * amount);
435         *brp = br;
436         sbinfo->si_bend++;
437         if (unlikely(bend < 0))
438                 sbinfo->si_bend = 0;
439 }
440
441 static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
442                              aufs_bindex_t bend, aufs_bindex_t amount)
443 {
444         struct au_hdentry *hdp;
445
446         AuRwMustWriteLock(&dinfo->di_rwsem);
447
448         hdp = dinfo->di_hdentry + bindex;
449         memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
450         au_h_dentry_init(hdp);
451         dinfo->di_bend++;
452         if (unlikely(bend < 0))
453                 dinfo->di_bstart = 0;
454 }
455
456 static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
457                              aufs_bindex_t bend, aufs_bindex_t amount)
458 {
459         struct au_hinode *hip;
460
461         AuRwMustWriteLock(&iinfo->ii_rwsem);
462
463         hip = iinfo->ii_hinode + bindex;
464         memmove(hip + 1, hip, sizeof(*hip) * amount);
465         hip->hi_inode = NULL;
466         au_hn_init(hip);
467         iinfo->ii_bend++;
468         if (unlikely(bend < 0))
469                 iinfo->ii_bstart = 0;
470 }
471
472 static void au_br_do_add(struct super_block *sb, struct au_branch *br,
473                          aufs_bindex_t bindex)
474 {
475         struct dentry *root, *h_dentry;
476         struct inode *root_inode;
477         aufs_bindex_t bend, amount;
478
479         au_br_dflags_force(br);
480
481         root = sb->s_root;
482         root_inode = root->d_inode;
483         bend = au_sbend(sb);
484         amount = bend + 1 - bindex;
485         h_dentry = au_br_dentry(br);
486         au_sbilist_lock();
487         au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
488         au_br_do_add_hdp(au_di(root), bindex, bend, amount);
489         au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
490         au_set_h_dptr(root, bindex, dget(h_dentry));
491         au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
492                       /*flags*/0);
493         au_sbilist_unlock();
494 }
495
496 int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
497 {
498         int err;
499         aufs_bindex_t bend, add_bindex;
500         struct dentry *root, *h_dentry;
501         struct inode *root_inode;
502         struct au_branch *add_branch;
503
504         root = sb->s_root;
505         root_inode = root->d_inode;
506         IMustLock(root_inode);
507         err = test_add(sb, add, remount);
508         if (unlikely(err < 0))
509                 goto out;
510         if (err) {
511                 err = 0;
512                 goto out; /* success */
513         }
514
515         bend = au_sbend(sb);
516         add_branch = au_br_alloc(sb, bend + 2, add->perm);
517         err = PTR_ERR(add_branch);
518         if (IS_ERR(add_branch))
519                 goto out;
520
521         err = au_br_init(add_branch, sb, add);
522         if (unlikely(err)) {
523                 au_br_do_free(add_branch);
524                 goto out;
525         }
526
527         add_bindex = add->bindex;
528         if (!remount)
529                 au_br_do_add(sb, add_branch, add_bindex);
530         else {
531                 sysaufs_brs_del(sb, add_bindex);
532                 au_br_do_add(sb, add_branch, add_bindex);
533                 sysaufs_brs_add(sb, add_bindex);
534         }
535
536         h_dentry = add->path.dentry;
537         if (!add_bindex) {
538                 au_cpup_attr_all(root_inode, /*force*/1);
539                 sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
540         } else
541                 au_add_nlink(root_inode, h_dentry->d_inode);
542
543         /*
544          * this test/set prevents aufs from handling unnecesary notify events
545          * of xino files, in case of re-adding a writable branch which was
546          * once detached from aufs.
547          */
548         if (au_xino_brid(sb) < 0
549             && au_br_writable(add_branch->br_perm)
550             && !au_test_fs_bad_xino(h_dentry->d_sb)
551             && add_branch->br_xino.xi_file
552             && add_branch->br_xino.xi_file->f_dentry->d_parent == h_dentry)
553                 au_xino_brid_set(sb, add_branch->br_id);
554
555 out:
556         return err;
557 }
558
559 /* ---------------------------------------------------------------------- */
560
561 /*
562  * delete a branch
563  */
564
565 /* to show the line number, do not make it inlined function */
566 #define AuVerbose(do_info, fmt, ...) do { \
567         if (do_info) \
568                 pr_info(fmt, ##__VA_ARGS__); \
569 } while (0)
570
571 static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
572                          aufs_bindex_t bend)
573 {
574         return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
575 }
576
577 static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
578                          aufs_bindex_t bend)
579 {
580         return au_test_ibusy(dentry->d_inode, bstart, bend);
581 }
582
583 /*
584  * test if the branch is deletable or not.
585  */
586 static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
587                             unsigned int sigen, const unsigned int verbose)
588 {
589         int err, i, j, ndentry;
590         aufs_bindex_t bstart, bend;
591         struct au_dcsub_pages dpages;
592         struct au_dpage *dpage;
593         struct dentry *d;
594
595         err = au_dpages_init(&dpages, GFP_NOFS);
596         if (unlikely(err))
597                 goto out;
598         err = au_dcsub_pages(&dpages, root, NULL, NULL);
599         if (unlikely(err))
600                 goto out_dpages;
601
602         for (i = 0; !err && i < dpages.ndpage; i++) {
603                 dpage = dpages.dpages + i;
604                 ndentry = dpage->ndentry;
605                 for (j = 0; !err && j < ndentry; j++) {
606                         d = dpage->dentries[j];
607                         AuDebugOn(!d->d_count);
608                         if (!au_digen_test(d, sigen)) {
609                                 di_read_lock_child(d, AuLock_IR);
610                                 if (unlikely(au_dbrange_test(d))) {
611                                         di_read_unlock(d, AuLock_IR);
612                                         continue;
613                                 }
614                         } else {
615                                 di_write_lock_child(d);
616                                 if (unlikely(au_dbrange_test(d))) {
617                                         di_write_unlock(d);
618                                         continue;
619                                 }
620                                 err = au_reval_dpath(d, sigen);
621                                 if (!err)
622                                         di_downgrade_lock(d, AuLock_IR);
623                                 else {
624                                         di_write_unlock(d);
625                                         break;
626                                 }
627                         }
628
629                         /* AuDbgDentry(d); */
630                         bstart = au_dbstart(d);
631                         bend = au_dbend(d);
632                         if (bstart <= bindex
633                             && bindex <= bend
634                             && au_h_dptr(d, bindex)
635                             && au_test_dbusy(d, bstart, bend)) {
636                                 err = -EBUSY;
637                                 AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
638                                 AuDbgDentry(d);
639                         }
640                         di_read_unlock(d, AuLock_IR);
641                 }
642         }
643
644 out_dpages:
645         au_dpages_free(&dpages);
646 out:
647         return err;
648 }
649
650 static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
651                            unsigned int sigen, const unsigned int verbose)
652 {
653         int err;
654         unsigned long long max, ull;
655         struct inode *i, **array;
656         aufs_bindex_t bstart, bend;
657
658         array = au_iarray_alloc(sb, &max);
659         err = PTR_ERR(array);
660         if (IS_ERR(array))
661                 goto out;
662
663         err = 0;
664         AuDbg("b%d\n", bindex);
665         for (ull = 0; !err && ull < max; ull++) {
666                 i = array[ull];
667                 if (i->i_ino == AUFS_ROOT_INO)
668                         continue;
669
670                 /* AuDbgInode(i); */
671                 if (au_iigen(i, NULL) == sigen)
672                         ii_read_lock_child(i);
673                 else {
674                         ii_write_lock_child(i);
675                         err = au_refresh_hinode_self(i);
676                         au_iigen_dec(i);
677                         if (!err)
678                                 ii_downgrade_lock(i);
679                         else {
680                                 ii_write_unlock(i);
681                                 break;
682                         }
683                 }
684
685                 bstart = au_ibstart(i);
686                 bend = au_ibend(i);
687                 if (bstart <= bindex
688                     && bindex <= bend
689                     && au_h_iptr(i, bindex)
690                     && au_test_ibusy(i, bstart, bend)) {
691                         err = -EBUSY;
692                         AuVerbose(verbose, "busy i%lu\n", i->i_ino);
693                         AuDbgInode(i);
694                 }
695                 ii_read_unlock(i);
696         }
697         au_iarray_free(array, max);
698
699 out:
700         return err;
701 }
702
703 static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
704                               const unsigned int verbose)
705 {
706         int err;
707         unsigned int sigen;
708
709         sigen = au_sigen(root->d_sb);
710         DiMustNoWaiters(root);
711         IiMustNoWaiters(root->d_inode);
712         di_write_unlock(root);
713         err = test_dentry_busy(root, bindex, sigen, verbose);
714         if (!err)
715                 err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
716         di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
717
718         return err;
719 }
720
721 static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
722                              const aufs_bindex_t bindex,
723                              const aufs_bindex_t bend)
724 {
725         struct au_branch **brp, **p;
726
727         AuRwMustWriteLock(&sbinfo->si_rwsem);
728
729         brp = sbinfo->si_branch + bindex;
730         if (bindex < bend)
731                 memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
732         sbinfo->si_branch[0 + bend] = NULL;
733         sbinfo->si_bend--;
734
735         p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
736         if (p)
737                 sbinfo->si_branch = p;
738         /* harmless error */
739 }
740
741 static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
742                              const aufs_bindex_t bend)
743 {
744         struct au_hdentry *hdp, *p;
745
746         AuRwMustWriteLock(&dinfo->di_rwsem);
747
748         hdp = dinfo->di_hdentry;
749         if (bindex < bend)
750                 memmove(hdp + bindex, hdp + bindex + 1,
751                         sizeof(*hdp) * (bend - bindex));
752         hdp[0 + bend].hd_dentry = NULL;
753         dinfo->di_bend--;
754
755         p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
756         if (p)
757                 dinfo->di_hdentry = p;
758         /* harmless error */
759 }
760
761 static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
762                              const aufs_bindex_t bend)
763 {
764         struct au_hinode *hip, *p;
765
766         AuRwMustWriteLock(&iinfo->ii_rwsem);
767
768         hip = iinfo->ii_hinode + bindex;
769         if (bindex < bend)
770                 memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
771         iinfo->ii_hinode[0 + bend].hi_inode = NULL;
772         au_hn_init(iinfo->ii_hinode + bend);
773         iinfo->ii_bend--;
774
775         p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
776         if (p)
777                 iinfo->ii_hinode = p;
778         /* harmless error */
779 }
780
781 static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
782                          struct au_branch *br)
783 {
784         aufs_bindex_t bend;
785         struct au_sbinfo *sbinfo;
786         struct dentry *root, *h_root;
787         struct inode *inode, *h_inode;
788         struct au_hinode *hinode;
789
790         SiMustWriteLock(sb);
791
792         root = sb->s_root;
793         inode = root->d_inode;
794         sbinfo = au_sbi(sb);
795         bend = sbinfo->si_bend;
796
797         h_root = au_h_dptr(root, bindex);
798         hinode = au_hi(inode, bindex);
799         h_inode = au_igrab(hinode->hi_inode);
800         au_hiput(hinode);
801
802         au_sbilist_lock();
803         au_br_do_del_brp(sbinfo, bindex, bend);
804         au_br_do_del_hdp(au_di(root), bindex, bend);
805         au_br_do_del_hip(au_ii(inode), bindex, bend);
806         au_sbilist_unlock();
807
808         dput(h_root);
809         iput(h_inode);
810         au_br_do_free(br);
811 }
812
813 int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
814 {
815         int err, rerr, i;
816         unsigned int mnt_flags;
817         aufs_bindex_t bindex, bend, br_id;
818         unsigned char do_wh, verbose;
819         struct au_branch *br;
820         struct au_wbr *wbr;
821
822         err = 0;
823         bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
824         if (bindex < 0) {
825                 if (remount)
826                         goto out; /* success */
827                 err = -ENOENT;
828                 pr_err("%s no such branch\n", del->pathname);
829                 goto out;
830         }
831         AuDbg("bindex b%d\n", bindex);
832
833         err = -EBUSY;
834         mnt_flags = au_mntflags(sb);
835         verbose = !!au_opt_test(mnt_flags, VERBOSE);
836         bend = au_sbend(sb);
837         if (unlikely(!bend)) {
838                 AuVerbose(verbose, "no more branches left\n");
839                 goto out;
840         }
841         br = au_sbr(sb, bindex);
842         AuDebugOn(!path_equal(&br->br_path, &del->h_path));
843         i = atomic_read(&br->br_count);
844         if (unlikely(i)) {
845                 AuVerbose(verbose, "%d file(s) opened\n", i);
846                 goto out;
847         }
848
849         wbr = br->br_wbr;
850         do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
851         if (do_wh) {
852                 /* instead of WbrWhMustWriteLock(wbr) */
853                 SiMustWriteLock(sb);
854                 for (i = 0; i < AuBrWh_Last; i++) {
855                         dput(wbr->wbr_wh[i]);
856                         wbr->wbr_wh[i] = NULL;
857                 }
858         }
859
860         err = test_children_busy(sb->s_root, bindex, verbose);
861         if (unlikely(err)) {
862                 if (do_wh)
863                         goto out_wh;
864                 goto out;
865         }
866
867         err = 0;
868         br_id = br->br_id;
869         if (!remount)
870                 au_br_do_del(sb, bindex, br);
871         else {
872                 sysaufs_brs_del(sb, bindex);
873                 au_br_do_del(sb, bindex, br);
874                 sysaufs_brs_add(sb, bindex);
875         }
876
877         if (!bindex) {
878                 au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
879                 sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
880         } else
881                 au_sub_nlink(sb->s_root->d_inode, del->h_path.dentry->d_inode);
882         if (au_opt_test(mnt_flags, PLINK))
883                 au_plink_half_refresh(sb, br_id);
884
885         if (au_xino_brid(sb) == br_id)
886                 au_xino_brid_set(sb, -1);
887         goto out; /* success */
888
889 out_wh:
890         /* revert */
891         rerr = au_br_init_wh(sb, br, br->br_perm);
892         if (rerr)
893                 pr_warn("failed re-creating base whiteout, %s. (%d)\n",
894                         del->pathname, rerr);
895 out:
896         return err;
897 }
898
899 /* ---------------------------------------------------------------------- */
900
901 static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
902 {
903         int err;
904         aufs_bindex_t bstart, bend;
905         struct aufs_ibusy ibusy;
906         struct inode *inode, *h_inode;
907
908         err = -EPERM;
909         if (unlikely(!capable(CAP_SYS_ADMIN)))
910                 goto out;
911
912         err = copy_from_user(&ibusy, arg, sizeof(ibusy));
913         if (!err)
914                 err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
915         if (unlikely(err)) {
916                 err = -EFAULT;
917                 AuTraceErr(err);
918                 goto out;
919         }
920
921         err = -EINVAL;
922         si_read_lock(sb, AuLock_FLUSH);
923         if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
924                 goto out_unlock;
925
926         err = 0;
927         ibusy.h_ino = 0; /* invalid */
928         inode = ilookup(sb, ibusy.ino);
929         if (!inode
930             || inode->i_ino == AUFS_ROOT_INO
931             || is_bad_inode(inode))
932                 goto out_unlock;
933
934         ii_read_lock_child(inode);
935         bstart = au_ibstart(inode);
936         bend = au_ibend(inode);
937         if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
938                 h_inode = au_h_iptr(inode, ibusy.bindex);
939                 if (h_inode && au_test_ibusy(inode, bstart, bend))
940                         ibusy.h_ino = h_inode->i_ino;
941         }
942         ii_read_unlock(inode);
943         iput(inode);
944
945 out_unlock:
946         si_read_unlock(sb);
947         if (!err) {
948                 err = __put_user(ibusy.h_ino, &arg->h_ino);
949                 if (unlikely(err)) {
950                         err = -EFAULT;
951                         AuTraceErr(err);
952                 }
953         }
954 out:
955         return err;
956 }
957
958 long au_ibusy_ioctl(struct file *file, unsigned long arg)
959 {
960         return au_ibusy(file->f_dentry->d_sb, (void __user *)arg);
961 }
962
963 #ifdef CONFIG_COMPAT
964 long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
965 {
966         return au_ibusy(file->f_dentry->d_sb, compat_ptr(arg));
967 }
968 #endif
969
970 /* ---------------------------------------------------------------------- */
971
972 /*
973  * change a branch permission
974  */
975
976 static void au_warn_ima(void)
977 {
978 #ifdef CONFIG_IMA
979         /* since it doesn't support mark_files_ro() */
980         AuWarn1("RW -> RO makes IMA to produce wrong message\n");
981 #endif
982 }
983
984 static int do_need_sigen_inc(int a, int b)
985 {
986         return au_br_whable(a) && !au_br_whable(b);
987 }
988
989 static int need_sigen_inc(int old, int new)
990 {
991         return do_need_sigen_inc(old, new)
992                 || do_need_sigen_inc(new, old);
993 }
994
995 static unsigned long long au_farray_cb(void *a,
996                                        unsigned long long max __maybe_unused,
997                                        void *arg)
998 {
999         unsigned long long n;
1000         struct file **p, *f;
1001         struct super_block *sb = arg;
1002
1003         n = 0;
1004         p = a;
1005         lg_global_lock(files_lglock);
1006         do_file_list_for_each_entry(sb, f) {
1007                 if (au_fi(f)
1008                     && file_count(f)
1009                     && !special_file(f->f_dentry->d_inode->i_mode)) {
1010                         get_file(f);
1011                         *p++ = f;
1012                         n++;
1013                         AuDebugOn(n > max);
1014                 }
1015         } while_file_list_for_each_entry;
1016         lg_global_unlock(files_lglock);
1017
1018         return n;
1019 }
1020
1021 static struct file **au_farray_alloc(struct super_block *sb,
1022                                      unsigned long long *max)
1023 {
1024         *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
1025         return au_array_alloc(max, au_farray_cb, sb);
1026 }
1027
1028 static void au_farray_free(struct file **a, unsigned long long max)
1029 {
1030         unsigned long long ull;
1031
1032         for (ull = 0; ull < max; ull++)
1033                 if (a[ull])
1034                         fput(a[ull]);
1035         au_array_free(a);
1036 }
1037
1038 static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
1039 {
1040         int err, do_warn;
1041         unsigned int mnt_flags;
1042         unsigned long long ull, max;
1043         aufs_bindex_t br_id;
1044         unsigned char verbose;
1045         struct file *file, *hf, **array;
1046         struct inode *inode;
1047         struct au_hfile *hfile;
1048
1049         mnt_flags = au_mntflags(sb);
1050         verbose = !!au_opt_test(mnt_flags, VERBOSE);
1051
1052         array = au_farray_alloc(sb, &max);
1053         err = PTR_ERR(array);
1054         if (IS_ERR(array))
1055                 goto out;
1056
1057         do_warn = 0;
1058         br_id = au_sbr_id(sb, bindex);
1059         for (ull = 0; ull < max; ull++) {
1060                 file = array[ull];
1061
1062                 /* AuDbg("%.*s\n", AuDLNPair(file->f_dentry)); */
1063                 fi_read_lock(file);
1064                 if (unlikely(au_test_mmapped(file))) {
1065                         err = -EBUSY;
1066                         AuVerbose(verbose, "mmapped %.*s\n",
1067                                   AuDLNPair(file->f_dentry));
1068                         AuDbgFile(file);
1069                         FiMustNoWaiters(file);
1070                         fi_read_unlock(file);
1071                         goto out_array;
1072                 }
1073
1074                 inode = file->f_dentry->d_inode;
1075                 hfile = &au_fi(file)->fi_htop;
1076                 hf = hfile->hf_file;
1077                 if (!S_ISREG(inode->i_mode)
1078                     || !(file->f_mode & FMODE_WRITE)
1079                     || hfile->hf_br->br_id != br_id
1080                     || !(hf->f_mode & FMODE_WRITE))
1081                         array[ull] = NULL;
1082                 else {
1083                         do_warn = 1;
1084                         get_file(file);
1085                 }
1086
1087                 FiMustNoWaiters(file);
1088                 fi_read_unlock(file);
1089                 fput(file);
1090         }
1091
1092         err = 0;
1093         if (do_warn)
1094                 au_warn_ima();
1095
1096         for (ull = 0; ull < max; ull++) {
1097                 file = array[ull];
1098                 if (!file)
1099                         continue;
1100
1101                 /* todo: already flushed? */
1102                 /* cf. fs/super.c:mark_files_ro() */
1103                 /* fi_read_lock(file); */
1104                 hfile = &au_fi(file)->fi_htop;
1105                 hf = hfile->hf_file;
1106                 /* fi_read_unlock(file); */
1107                 spin_lock(&hf->f_lock);
1108                 hf->f_mode &= ~FMODE_WRITE;
1109                 spin_unlock(&hf->f_lock);
1110                 if (!file_check_writeable(hf)) {
1111                         file_release_write(hf);
1112                         mnt_drop_write(hf->f_vfsmnt);
1113                 }
1114         }
1115
1116 out_array:
1117         au_farray_free(array, max);
1118 out:
1119         AuTraceErr(err);
1120         return err;
1121 }
1122
1123 int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
1124               int *do_refresh)
1125 {
1126         int err, rerr;
1127         aufs_bindex_t bindex;
1128         struct dentry *root;
1129         struct au_branch *br;
1130
1131         root = sb->s_root;
1132         bindex = au_find_dbindex(root, mod->h_root);
1133         if (bindex < 0) {
1134                 if (remount)
1135                         return 0; /* success */
1136                 err = -ENOENT;
1137                 pr_err("%s no such branch\n", mod->path);
1138                 goto out;
1139         }
1140         AuDbg("bindex b%d\n", bindex);
1141
1142         err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
1143         if (unlikely(err))
1144                 goto out;
1145
1146         br = au_sbr(sb, bindex);
1147         AuDebugOn(mod->h_root != au_br_dentry(br));
1148         if (br->br_perm == mod->perm)
1149                 return 0; /* success */
1150
1151         if (au_br_writable(br->br_perm)) {
1152                 /* remove whiteout base */
1153                 err = au_br_init_wh(sb, br, mod->perm);
1154                 if (unlikely(err))
1155                         goto out;
1156
1157                 if (!au_br_writable(mod->perm)) {
1158                         /* rw --> ro, file might be mmapped */
1159                         DiMustNoWaiters(root);
1160                         IiMustNoWaiters(root->d_inode);
1161                         di_write_unlock(root);
1162                         err = au_br_mod_files_ro(sb, bindex);
1163                         /* aufs_write_lock() calls ..._child() */
1164                         di_write_lock_child(root);
1165
1166                         if (unlikely(err)) {
1167                                 rerr = -ENOMEM;
1168                                 br->br_wbr = kmalloc(sizeof(*br->br_wbr),
1169                                                      GFP_NOFS);
1170                                 if (br->br_wbr)
1171                                         rerr = au_wbr_init(br, sb, br->br_perm);
1172                                 if (unlikely(rerr)) {
1173                                         AuIOErr("nested error %d (%d)\n",
1174                                                 rerr, err);
1175                                         br->br_perm = mod->perm;
1176                                 }
1177                         }
1178                 }
1179         } else if (au_br_writable(mod->perm)) {
1180                 /* ro --> rw */
1181                 err = -ENOMEM;
1182                 br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
1183                 if (br->br_wbr) {
1184                         err = au_wbr_init(br, sb, mod->perm);
1185                         if (unlikely(err)) {
1186                                 kfree(br->br_wbr);
1187                                 br->br_wbr = NULL;
1188                         }
1189                 }
1190         }
1191
1192         if (!err) {
1193                 if ((br->br_perm & AuBrAttr_UNPIN)
1194                     && !(mod->perm & AuBrAttr_UNPIN))
1195                         au_br_dflags_force(br);
1196                 else if (!(br->br_perm & AuBrAttr_UNPIN)
1197                          && (mod->perm & AuBrAttr_UNPIN))
1198                         au_br_dflags_restore(br);
1199                 *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
1200                 br->br_perm = mod->perm;
1201         }
1202
1203 out:
1204         AuTraceErr(err);
1205         return err;
1206 }