2 * Copyright (C) 2005-2013 Junjiro R. Okajima
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.
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.
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
23 #include <linux/compat.h>
24 #include <linux/statfs.h>
28 * free a single branch
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)
35 struct dentry *h_dentry;
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);
44 /* restore its d_flags */
45 static void au_br_dflags_restore(struct au_branch *br)
47 struct dentry *h_dentry;
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);
58 static void au_br_do_free(struct au_branch *br)
62 struct au_dykey **key;
64 au_hnotify_fin_br(br);
66 if (br->br_xino.xi_file)
67 fput(br->br_xino.xi_file);
68 mutex_destroy(&br->br_xino.xi_nondir_mtx);
70 AuDebugOn(atomic_read(&br->br_count));
74 for (i = 0; i < AuBrWh_Last; i++)
76 AuDebugOn(atomic_read(&wbr->wbr_wh_running));
77 AuRwDestroy(&wbr->wbr_wh_rwsem);
81 for (i = 0; i < AuBrDynOp; i++, key++)
87 au_br_dflags_restore(br);
89 /* recursive lock, s_umount of branch's */
91 path_put(&br->br_path);
100 void au_br_free(struct au_sbinfo *sbinfo)
103 struct au_branch **br;
105 AuRwMustWriteLock(&sbinfo->si_rwsem);
107 bmax = sbinfo->si_bend + 1;
108 br = sbinfo->si_branch;
110 au_br_do_free(*br++);
114 * find the index of a branch which is specified by @br_id.
116 int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
118 aufs_bindex_t bindex, bend;
121 for (bindex = 0; bindex <= bend; bindex++)
122 if (au_sbr_id(sb, bindex) == br_id)
127 /* ---------------------------------------------------------------------- */
133 static int test_overlap(struct super_block *sb, struct dentry *h_adding,
134 struct dentry *h_root)
136 if (unlikely(h_adding == h_root
137 || au_test_loopback_overlap(sb, h_adding)))
139 if (h_adding->d_sb != h_root->d_sb)
141 return au_test_subdir(h_adding, h_root)
142 || au_test_subdir(h_root, h_adding);
146 * returns a newly allocated branch. @new_nbranch is a number of branches
147 * after adding a branch.
149 static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
152 struct au_branch *add_branch;
158 add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
159 if (unlikely(!add_branch))
162 err = au_hnotify_init_br(add_branch, perm);
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),
171 if (unlikely(!add_branch->br_wbr))
175 err = au_sbr_realloc(au_sbi(sb), new_nbranch);
177 err = au_di_realloc(au_di(root), new_nbranch);
179 err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
181 return add_branch; /* success */
183 kfree(add_branch->br_wbr);
186 au_hnotify_fin_br(add_branch);
194 * test if the branch permission is legal or not.
196 static int test_br(struct inode *inode, int brperm, char *path)
200 err = (au_br_writable(brperm) && IS_RDONLY(inode));
205 pr_err("write permission for readonly mount or inode, %s\n", path);
213 * 0: success, the caller will add it
214 * plus: success, it is already unified, the caller should ignore it
217 static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
220 aufs_bindex_t bend, bindex;
222 struct inode *inode, *h_inode;
226 if (unlikely(bend >= 0
227 && au_find_dbindex(root, add->path.dentry) >= 0)) {
231 pr_err("%s duplicated\n", add->pathname);
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);
244 if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
245 pr_err("bad index %d\n", add->bindex);
249 inode = add->path.dentry->d_inode;
251 if (unlikely(!inode->i_nlink)) {
252 pr_err("no existence %s\n", add->pathname);
257 if (unlikely(inode->i_sb == sb)) {
258 pr_err("%s must be outside\n", add->pathname);
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));
268 err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
273 return 0; /* success */
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);
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",
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));
302 * initialize or clean the whiteouts for an adding branch
304 static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
308 aufs_bindex_t bindex;
311 struct au_hinode *hdir;
314 old_perm = br->br_perm;
315 br->br_perm = new_perm;
318 bindex = au_br_index(sb, br->br_id);
320 hdir = au_hi(sb->s_root->d_inode, bindex);
321 au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
323 h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
324 mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
327 err = au_wh_init(br, sb);
329 wbr_wh_write_lock(wbr);
330 err = au_wh_init(br, sb);
331 wbr_wh_write_unlock(wbr);
334 au_hn_imtx_unlock(hdir);
337 br->br_perm = old_perm;
339 if (!err && wbr && !au_br_writable(new_perm)) {
347 static int au_wbr_init(struct au_branch *br, struct super_block *sb,
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);
361 * a limit for rmdir/rename a dir
362 * cf. AUFS_MAX_NAMELEN in include/linux/aufs_type.h
364 err = vfs_statfs(&br->br_path, &kst);
368 if (kst.f_namelen >= NAME_MAX)
369 err = au_br_init_wh(sb, br, perm);
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);
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)
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);
400 if (au_br_writable(add->perm)) {
401 err = au_wbr_init(br, sb, add->perm);
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);
410 AuDebugOn(br->br_xino.xi_file);
416 path_get(&br->br_path);
417 goto out; /* success */
420 memset(&br->br_path, 0, sizeof(br->br_path));
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)
429 struct au_branch **brp;
431 AuRwMustWriteLock(&sbinfo->si_rwsem);
433 brp = sbinfo->si_branch + bindex;
434 memmove(brp + 1, brp, sizeof(*brp) * amount);
437 if (unlikely(bend < 0))
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)
444 struct au_hdentry *hdp;
446 AuRwMustWriteLock(&dinfo->di_rwsem);
448 hdp = dinfo->di_hdentry + bindex;
449 memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
450 au_h_dentry_init(hdp);
452 if (unlikely(bend < 0))
453 dinfo->di_bstart = 0;
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)
459 struct au_hinode *hip;
461 AuRwMustWriteLock(&iinfo->ii_rwsem);
463 hip = iinfo->ii_hinode + bindex;
464 memmove(hip + 1, hip, sizeof(*hip) * amount);
465 hip->hi_inode = NULL;
468 if (unlikely(bend < 0))
469 iinfo->ii_bstart = 0;
472 static void au_br_do_add(struct super_block *sb, struct au_branch *br,
473 aufs_bindex_t bindex)
475 struct dentry *root, *h_dentry;
476 struct inode *root_inode;
477 aufs_bindex_t bend, amount;
479 au_br_dflags_force(br);
482 root_inode = root->d_inode;
484 amount = bend + 1 - bindex;
485 h_dentry = au_br_dentry(br);
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),
496 int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
499 aufs_bindex_t bend, add_bindex;
500 struct dentry *root, *h_dentry;
501 struct inode *root_inode;
502 struct au_branch *add_branch;
505 root_inode = root->d_inode;
506 IMustLock(root_inode);
507 err = test_add(sb, add, remount);
508 if (unlikely(err < 0))
512 goto out; /* success */
516 add_branch = au_br_alloc(sb, bend + 2, add->perm);
517 err = PTR_ERR(add_branch);
518 if (IS_ERR(add_branch))
521 err = au_br_init(add_branch, sb, add);
523 au_br_do_free(add_branch);
527 add_bindex = add->bindex;
529 au_br_do_add(sb, add_branch, add_bindex);
531 sysaufs_brs_del(sb, add_bindex);
532 au_br_do_add(sb, add_branch, add_bindex);
533 sysaufs_brs_add(sb, add_bindex);
536 h_dentry = add->path.dentry;
538 au_cpup_attr_all(root_inode, /*force*/1);
539 sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
541 au_add_nlink(root_inode, h_dentry->d_inode);
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.
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);
559 /* ---------------------------------------------------------------------- */
565 /* to show the line number, do not make it inlined function */
566 #define AuVerbose(do_info, fmt, ...) do { \
568 pr_info(fmt, ##__VA_ARGS__); \
571 static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
574 return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
577 static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
580 return au_test_ibusy(dentry->d_inode, bstart, bend);
584 * test if the branch is deletable or not.
586 static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
587 unsigned int sigen, const unsigned int verbose)
589 int err, i, j, ndentry;
590 aufs_bindex_t bstart, bend;
591 struct au_dcsub_pages dpages;
592 struct au_dpage *dpage;
595 err = au_dpages_init(&dpages, GFP_NOFS);
598 err = au_dcsub_pages(&dpages, root, NULL, NULL);
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);
615 di_write_lock_child(d);
616 if (unlikely(au_dbrange_test(d))) {
620 err = au_reval_dpath(d, sigen);
622 di_downgrade_lock(d, AuLock_IR);
629 /* AuDbgDentry(d); */
630 bstart = au_dbstart(d);
634 && au_h_dptr(d, bindex)
635 && au_test_dbusy(d, bstart, bend)) {
637 AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
640 di_read_unlock(d, AuLock_IR);
645 au_dpages_free(&dpages);
650 static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
651 unsigned int sigen, const unsigned int verbose)
654 unsigned long long max, ull;
655 struct inode *i, **array;
656 aufs_bindex_t bstart, bend;
658 array = au_iarray_alloc(sb, &max);
659 err = PTR_ERR(array);
664 AuDbg("b%d\n", bindex);
665 for (ull = 0; !err && ull < max; ull++) {
667 if (i->i_ino == AUFS_ROOT_INO)
671 if (au_iigen(i, NULL) == sigen)
672 ii_read_lock_child(i);
674 ii_write_lock_child(i);
675 err = au_refresh_hinode_self(i);
678 ii_downgrade_lock(i);
685 bstart = au_ibstart(i);
689 && au_h_iptr(i, bindex)
690 && au_test_ibusy(i, bstart, bend)) {
692 AuVerbose(verbose, "busy i%lu\n", i->i_ino);
697 au_iarray_free(array, max);
703 static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
704 const unsigned int verbose)
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);
715 err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
716 di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
721 static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
722 const aufs_bindex_t bindex,
723 const aufs_bindex_t bend)
725 struct au_branch **brp, **p;
727 AuRwMustWriteLock(&sbinfo->si_rwsem);
729 brp = sbinfo->si_branch + bindex;
731 memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
732 sbinfo->si_branch[0 + bend] = NULL;
735 p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
737 sbinfo->si_branch = p;
741 static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
742 const aufs_bindex_t bend)
744 struct au_hdentry *hdp, *p;
746 AuRwMustWriteLock(&dinfo->di_rwsem);
748 hdp = dinfo->di_hdentry;
750 memmove(hdp + bindex, hdp + bindex + 1,
751 sizeof(*hdp) * (bend - bindex));
752 hdp[0 + bend].hd_dentry = NULL;
755 p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
757 dinfo->di_hdentry = p;
761 static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
762 const aufs_bindex_t bend)
764 struct au_hinode *hip, *p;
766 AuRwMustWriteLock(&iinfo->ii_rwsem);
768 hip = iinfo->ii_hinode + bindex;
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);
775 p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
777 iinfo->ii_hinode = p;
781 static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
782 struct au_branch *br)
785 struct au_sbinfo *sbinfo;
786 struct dentry *root, *h_root;
787 struct inode *inode, *h_inode;
788 struct au_hinode *hinode;
793 inode = root->d_inode;
795 bend = sbinfo->si_bend;
797 h_root = au_h_dptr(root, bindex);
798 hinode = au_hi(inode, bindex);
799 h_inode = au_igrab(hinode->hi_inode);
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);
813 int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
816 unsigned int mnt_flags;
817 aufs_bindex_t bindex, bend, br_id;
818 unsigned char do_wh, verbose;
819 struct au_branch *br;
823 bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
826 goto out; /* success */
828 pr_err("%s no such branch\n", del->pathname);
831 AuDbg("bindex b%d\n", bindex);
834 mnt_flags = au_mntflags(sb);
835 verbose = !!au_opt_test(mnt_flags, VERBOSE);
837 if (unlikely(!bend)) {
838 AuVerbose(verbose, "no more branches left\n");
841 br = au_sbr(sb, bindex);
842 AuDebugOn(!path_equal(&br->br_path, &del->h_path));
843 i = atomic_read(&br->br_count);
845 AuVerbose(verbose, "%d file(s) opened\n", i);
850 do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
852 /* instead of WbrWhMustWriteLock(wbr) */
854 for (i = 0; i < AuBrWh_Last; i++) {
855 dput(wbr->wbr_wh[i]);
856 wbr->wbr_wh[i] = NULL;
860 err = test_children_busy(sb->s_root, bindex, verbose);
870 au_br_do_del(sb, bindex, br);
872 sysaufs_brs_del(sb, bindex);
873 au_br_do_del(sb, bindex, br);
874 sysaufs_brs_add(sb, bindex);
878 au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
879 sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
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);
885 if (au_xino_brid(sb) == br_id)
886 au_xino_brid_set(sb, -1);
887 goto out; /* success */
891 rerr = au_br_init_wh(sb, br, br->br_perm);
893 pr_warn("failed re-creating base whiteout, %s. (%d)\n",
894 del->pathname, rerr);
899 /* ---------------------------------------------------------------------- */
901 static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
904 aufs_bindex_t bstart, bend;
905 struct aufs_ibusy ibusy;
906 struct inode *inode, *h_inode;
909 if (unlikely(!capable(CAP_SYS_ADMIN)))
912 err = copy_from_user(&ibusy, arg, sizeof(ibusy));
914 err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
922 si_read_lock(sb, AuLock_FLUSH);
923 if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
927 ibusy.h_ino = 0; /* invalid */
928 inode = ilookup(sb, ibusy.ino);
930 || inode->i_ino == AUFS_ROOT_INO
931 || is_bad_inode(inode))
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;
942 ii_read_unlock(inode);
948 err = __put_user(ibusy.h_ino, &arg->h_ino);
958 long au_ibusy_ioctl(struct file *file, unsigned long arg)
960 return au_ibusy(file->f_dentry->d_sb, (void __user *)arg);
964 long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
966 return au_ibusy(file->f_dentry->d_sb, compat_ptr(arg));
970 /* ---------------------------------------------------------------------- */
973 * change a branch permission
976 static void au_warn_ima(void)
979 /* since it doesn't support mark_files_ro() */
980 AuWarn1("RW -> RO makes IMA to produce wrong message\n");
984 static int do_need_sigen_inc(int a, int b)
986 return au_br_whable(a) && !au_br_whable(b);
989 static int need_sigen_inc(int old, int new)
991 return do_need_sigen_inc(old, new)
992 || do_need_sigen_inc(new, old);
995 static unsigned long long au_farray_cb(void *a,
996 unsigned long long max __maybe_unused,
999 unsigned long long n;
1000 struct file **p, *f;
1001 struct super_block *sb = arg;
1005 lg_global_lock(files_lglock);
1006 do_file_list_for_each_entry(sb, f) {
1009 && !special_file(f->f_dentry->d_inode->i_mode)) {
1015 } while_file_list_for_each_entry;
1016 lg_global_unlock(files_lglock);
1021 static struct file **au_farray_alloc(struct super_block *sb,
1022 unsigned long long *max)
1024 *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
1025 return au_array_alloc(max, au_farray_cb, sb);
1028 static void au_farray_free(struct file **a, unsigned long long max)
1030 unsigned long long ull;
1032 for (ull = 0; ull < max; ull++)
1038 static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
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;
1049 mnt_flags = au_mntflags(sb);
1050 verbose = !!au_opt_test(mnt_flags, VERBOSE);
1052 array = au_farray_alloc(sb, &max);
1053 err = PTR_ERR(array);
1058 br_id = au_sbr_id(sb, bindex);
1059 for (ull = 0; ull < max; ull++) {
1062 /* AuDbg("%.*s\n", AuDLNPair(file->f_dentry)); */
1064 if (unlikely(au_test_mmapped(file))) {
1066 AuVerbose(verbose, "mmapped %.*s\n",
1067 AuDLNPair(file->f_dentry));
1069 FiMustNoWaiters(file);
1070 fi_read_unlock(file);
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))
1087 FiMustNoWaiters(file);
1088 fi_read_unlock(file);
1096 for (ull = 0; ull < max; ull++) {
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);
1117 au_farray_free(array, max);
1123 int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
1127 aufs_bindex_t bindex;
1128 struct dentry *root;
1129 struct au_branch *br;
1132 bindex = au_find_dbindex(root, mod->h_root);
1135 return 0; /* success */
1137 pr_err("%s no such branch\n", mod->path);
1140 AuDbg("bindex b%d\n", bindex);
1142 err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
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 */
1151 if (au_br_writable(br->br_perm)) {
1152 /* remove whiteout base */
1153 err = au_br_init_wh(sb, br, mod->perm);
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);
1166 if (unlikely(err)) {
1168 br->br_wbr = kmalloc(sizeof(*br->br_wbr),
1171 rerr = au_wbr_init(br, sb, br->br_perm);
1172 if (unlikely(rerr)) {
1173 AuIOErr("nested error %d (%d)\n",
1175 br->br_perm = mod->perm;
1179 } else if (au_br_writable(mod->perm)) {
1182 br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
1184 err = au_wbr_init(br, sb, mod->perm);
1185 if (unlikely(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;