vfs: Make __fsync_super() a static function (version 4)
[firefly-linux-kernel-4.4.55.git] / fs / super.c
1 /*
2  *  linux/fs/super.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  super.c contains code to handle: - mount structures
7  *                                   - super-block tables
8  *                                   - filesystem drivers list
9  *                                   - mount system call
10  *                                   - umount system call
11  *                                   - ustat system call
12  *
13  * GK 2/5/95  -  Changed to support mounting the root fs via NFS
14  *
15  *  Added kerneld support: Jacques Gelinas and Bjorn Ekwall
16  *  Added change_root: Werner Almesberger & Hans Lermen, Feb '96
17  *  Added options to /proc/mounts:
18  *    Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
19  *  Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
20  *  Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
21  */
22
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/smp_lock.h>
27 #include <linux/acct.h>
28 #include <linux/blkdev.h>
29 #include <linux/quotaops.h>
30 #include <linux/namei.h>
31 #include <linux/buffer_head.h>          /* for fsync_super() */
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/syscalls.h>
35 #include <linux/vfs.h>
36 #include <linux/writeback.h>            /* for the emergency remount stuff */
37 #include <linux/idr.h>
38 #include <linux/kobject.h>
39 #include <linux/mutex.h>
40 #include <linux/file.h>
41 #include <asm/uaccess.h>
42 #include "internal.h"
43
44
45 LIST_HEAD(super_blocks);
46 DEFINE_SPINLOCK(sb_lock);
47
48 /**
49  *      alloc_super     -       create new superblock
50  *      @type:  filesystem type superblock should belong to
51  *
52  *      Allocates and initializes a new &struct super_block.  alloc_super()
53  *      returns a pointer new superblock or %NULL if allocation had failed.
54  */
55 static struct super_block *alloc_super(struct file_system_type *type)
56 {
57         struct super_block *s = kzalloc(sizeof(struct super_block),  GFP_USER);
58         static struct super_operations default_op;
59
60         if (s) {
61                 if (security_sb_alloc(s)) {
62                         kfree(s);
63                         s = NULL;
64                         goto out;
65                 }
66                 INIT_LIST_HEAD(&s->s_dirty);
67                 INIT_LIST_HEAD(&s->s_io);
68                 INIT_LIST_HEAD(&s->s_more_io);
69                 INIT_LIST_HEAD(&s->s_files);
70                 INIT_LIST_HEAD(&s->s_instances);
71                 INIT_HLIST_HEAD(&s->s_anon);
72                 INIT_LIST_HEAD(&s->s_inodes);
73                 INIT_LIST_HEAD(&s->s_dentry_lru);
74                 init_rwsem(&s->s_umount);
75                 mutex_init(&s->s_lock);
76                 lockdep_set_class(&s->s_umount, &type->s_umount_key);
77                 /*
78                  * The locking rules for s_lock are up to the
79                  * filesystem. For example ext3fs has different
80                  * lock ordering than usbfs:
81                  */
82                 lockdep_set_class(&s->s_lock, &type->s_lock_key);
83                 /*
84                  * sget() can have s_umount recursion.
85                  *
86                  * When it cannot find a suitable sb, it allocates a new
87                  * one (this one), and tries again to find a suitable old
88                  * one.
89                  *
90                  * In case that succeeds, it will acquire the s_umount
91                  * lock of the old one. Since these are clearly distrinct
92                  * locks, and this object isn't exposed yet, there's no
93                  * risk of deadlocks.
94                  *
95                  * Annotate this by putting this lock in a different
96                  * subclass.
97                  */
98                 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
99                 s->s_count = S_BIAS;
100                 atomic_set(&s->s_active, 1);
101                 mutex_init(&s->s_vfs_rename_mutex);
102                 mutex_init(&s->s_dquot.dqio_mutex);
103                 mutex_init(&s->s_dquot.dqonoff_mutex);
104                 init_rwsem(&s->s_dquot.dqptr_sem);
105                 init_waitqueue_head(&s->s_wait_unfrozen);
106                 s->s_maxbytes = MAX_NON_LFS;
107                 s->dq_op = sb_dquot_ops;
108                 s->s_qcop = sb_quotactl_ops;
109                 s->s_op = &default_op;
110                 s->s_time_gran = 1000000000;
111         }
112 out:
113         return s;
114 }
115
116 /**
117  *      destroy_super   -       frees a superblock
118  *      @s: superblock to free
119  *
120  *      Frees a superblock.
121  */
122 static inline void destroy_super(struct super_block *s)
123 {
124         security_sb_free(s);
125         kfree(s->s_subtype);
126         kfree(s->s_options);
127         kfree(s);
128 }
129
130 /* Superblock refcounting  */
131
132 /*
133  * Drop a superblock's refcount.  Returns non-zero if the superblock was
134  * destroyed.  The caller must hold sb_lock.
135  */
136 static int __put_super(struct super_block *sb)
137 {
138         int ret = 0;
139
140         if (!--sb->s_count) {
141                 destroy_super(sb);
142                 ret = 1;
143         }
144         return ret;
145 }
146
147 /*
148  * Drop a superblock's refcount.
149  * Returns non-zero if the superblock is about to be destroyed and
150  * at least is already removed from super_blocks list, so if we are
151  * making a loop through super blocks then we need to restart.
152  * The caller must hold sb_lock.
153  */
154 int __put_super_and_need_restart(struct super_block *sb)
155 {
156         /* check for race with generic_shutdown_super() */
157         if (list_empty(&sb->s_list)) {
158                 /* super block is removed, need to restart... */
159                 __put_super(sb);
160                 return 1;
161         }
162         /* can't be the last, since s_list is still in use */
163         sb->s_count--;
164         BUG_ON(sb->s_count == 0);
165         return 0;
166 }
167
168 /**
169  *      put_super       -       drop a temporary reference to superblock
170  *      @sb: superblock in question
171  *
172  *      Drops a temporary reference, frees superblock if there's no
173  *      references left.
174  */
175 static void put_super(struct super_block *sb)
176 {
177         spin_lock(&sb_lock);
178         __put_super(sb);
179         spin_unlock(&sb_lock);
180 }
181
182
183 /**
184  *      deactivate_super        -       drop an active reference to superblock
185  *      @s: superblock to deactivate
186  *
187  *      Drops an active reference to superblock, acquiring a temprory one if
188  *      there is no active references left.  In that case we lock superblock,
189  *      tell fs driver to shut it down and drop the temporary reference we
190  *      had just acquired.
191  */
192 void deactivate_super(struct super_block *s)
193 {
194         struct file_system_type *fs = s->s_type;
195         if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
196                 s->s_count -= S_BIAS-1;
197                 spin_unlock(&sb_lock);
198                 vfs_dq_off(s, 0);
199                 down_write(&s->s_umount);
200                 fs->kill_sb(s);
201                 put_filesystem(fs);
202                 put_super(s);
203         }
204 }
205
206 EXPORT_SYMBOL(deactivate_super);
207
208 /**
209  *      deactivate_locked_super -       drop an active reference to superblock
210  *      @s: superblock to deactivate
211  *
212  *      Equivalent of up_write(&s->s_umount); deactivate_super(s);, except that
213  *      it does not unlock it until it's all over.  As the result, it's safe to
214  *      use to dispose of new superblock on ->get_sb() failure exits - nobody
215  *      will see the sucker until it's all over.  Equivalent using up_write +
216  *      deactivate_super is safe for that purpose only if superblock is either
217  *      safe to use or has NULL ->s_root when we unlock.
218  */
219 void deactivate_locked_super(struct super_block *s)
220 {
221         struct file_system_type *fs = s->s_type;
222         if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
223                 s->s_count -= S_BIAS-1;
224                 spin_unlock(&sb_lock);
225                 vfs_dq_off(s, 0);
226                 fs->kill_sb(s);
227                 put_filesystem(fs);
228                 put_super(s);
229         } else {
230                 up_write(&s->s_umount);
231         }
232 }
233
234 EXPORT_SYMBOL(deactivate_locked_super);
235
236 /**
237  *      grab_super - acquire an active reference
238  *      @s: reference we are trying to make active
239  *
240  *      Tries to acquire an active reference.  grab_super() is used when we
241  *      had just found a superblock in super_blocks or fs_type->fs_supers
242  *      and want to turn it into a full-blown active reference.  grab_super()
243  *      is called with sb_lock held and drops it.  Returns 1 in case of
244  *      success, 0 if we had failed (superblock contents was already dead or
245  *      dying when grab_super() had been called).
246  */
247 static int grab_super(struct super_block *s) __releases(sb_lock)
248 {
249         s->s_count++;
250         spin_unlock(&sb_lock);
251         down_write(&s->s_umount);
252         if (s->s_root) {
253                 spin_lock(&sb_lock);
254                 if (s->s_count > S_BIAS) {
255                         atomic_inc(&s->s_active);
256                         s->s_count--;
257                         spin_unlock(&sb_lock);
258                         return 1;
259                 }
260                 spin_unlock(&sb_lock);
261         }
262         up_write(&s->s_umount);
263         put_super(s);
264         yield();
265         return 0;
266 }
267
268 /*
269  * Superblock locking.  We really ought to get rid of these two.
270  */
271 void lock_super(struct super_block * sb)
272 {
273         get_fs_excl();
274         mutex_lock(&sb->s_lock);
275 }
276
277 void unlock_super(struct super_block * sb)
278 {
279         put_fs_excl();
280         mutex_unlock(&sb->s_lock);
281 }
282
283 EXPORT_SYMBOL(lock_super);
284 EXPORT_SYMBOL(unlock_super);
285
286 /*
287  * Write out and wait upon all dirty data associated with this
288  * superblock.  Filesystem data as well as the underlying block
289  * device.  Takes the superblock lock.  Requires a second blkdev
290  * flush by the caller to complete the operation.
291  */
292 static int __fsync_super(struct super_block *sb)
293 {
294         sync_inodes_sb(sb, 0);
295         vfs_dq_sync(sb);
296         sync_inodes_sb(sb, 1);
297         lock_super(sb);
298         if (sb->s_dirt && sb->s_op->write_super)
299                 sb->s_op->write_super(sb);
300         unlock_super(sb);
301         if (sb->s_op->sync_fs)
302                 sb->s_op->sync_fs(sb, 1);
303         return sync_blockdev(sb->s_bdev);
304 }
305
306 /*
307  * Write out and wait upon all dirty data associated with this
308  * superblock.  Filesystem data as well as the underlying block
309  * device.  Takes the superblock lock.
310  */
311 int fsync_super(struct super_block *sb)
312 {
313         return __fsync_super(sb);
314 }
315 EXPORT_SYMBOL_GPL(fsync_super);
316
317 /**
318  *      generic_shutdown_super  -       common helper for ->kill_sb()
319  *      @sb: superblock to kill
320  *
321  *      generic_shutdown_super() does all fs-independent work on superblock
322  *      shutdown.  Typical ->kill_sb() should pick all fs-specific objects
323  *      that need destruction out of superblock, call generic_shutdown_super()
324  *      and release aforementioned objects.  Note: dentries and inodes _are_
325  *      taken care of and do not need specific handling.
326  *
327  *      Upon calling this function, the filesystem may no longer alter or
328  *      rearrange the set of dentries belonging to this super_block, nor may it
329  *      change the attachments of dentries to inodes.
330  */
331 void generic_shutdown_super(struct super_block *sb)
332 {
333         const struct super_operations *sop = sb->s_op;
334
335
336         if (sb->s_root) {
337                 shrink_dcache_for_umount(sb);
338                 fsync_super(sb);
339                 lock_super(sb);
340                 sb->s_flags &= ~MS_ACTIVE;
341
342                 /* bad name - it should be evict_inodes() */
343                 invalidate_inodes(sb);
344                 lock_kernel();
345
346                 if (sop->write_super && sb->s_dirt)
347                         sop->write_super(sb);
348                 if (sop->put_super)
349                         sop->put_super(sb);
350
351                 /* Forget any remaining inodes */
352                 if (invalidate_inodes(sb)) {
353                         printk("VFS: Busy inodes after unmount of %s. "
354                            "Self-destruct in 5 seconds.  Have a nice day...\n",
355                            sb->s_id);
356                 }
357
358                 unlock_kernel();
359                 unlock_super(sb);
360         }
361         spin_lock(&sb_lock);
362         /* should be initialized for __put_super_and_need_restart() */
363         list_del_init(&sb->s_list);
364         list_del(&sb->s_instances);
365         spin_unlock(&sb_lock);
366         up_write(&sb->s_umount);
367 }
368
369 EXPORT_SYMBOL(generic_shutdown_super);
370
371 /**
372  *      sget    -       find or create a superblock
373  *      @type:  filesystem type superblock should belong to
374  *      @test:  comparison callback
375  *      @set:   setup callback
376  *      @data:  argument to each of them
377  */
378 struct super_block *sget(struct file_system_type *type,
379                         int (*test)(struct super_block *,void *),
380                         int (*set)(struct super_block *,void *),
381                         void *data)
382 {
383         struct super_block *s = NULL;
384         struct super_block *old;
385         int err;
386
387 retry:
388         spin_lock(&sb_lock);
389         if (test) {
390                 list_for_each_entry(old, &type->fs_supers, s_instances) {
391                         if (!test(old, data))
392                                 continue;
393                         if (!grab_super(old))
394                                 goto retry;
395                         if (s) {
396                                 up_write(&s->s_umount);
397                                 destroy_super(s);
398                         }
399                         return old;
400                 }
401         }
402         if (!s) {
403                 spin_unlock(&sb_lock);
404                 s = alloc_super(type);
405                 if (!s)
406                         return ERR_PTR(-ENOMEM);
407                 goto retry;
408         }
409                 
410         err = set(s, data);
411         if (err) {
412                 spin_unlock(&sb_lock);
413                 up_write(&s->s_umount);
414                 destroy_super(s);
415                 return ERR_PTR(err);
416         }
417         s->s_type = type;
418         strlcpy(s->s_id, type->name, sizeof(s->s_id));
419         list_add_tail(&s->s_list, &super_blocks);
420         list_add(&s->s_instances, &type->fs_supers);
421         spin_unlock(&sb_lock);
422         get_filesystem(type);
423         return s;
424 }
425
426 EXPORT_SYMBOL(sget);
427
428 void drop_super(struct super_block *sb)
429 {
430         up_read(&sb->s_umount);
431         put_super(sb);
432 }
433
434 EXPORT_SYMBOL(drop_super);
435
436 static inline void write_super(struct super_block *sb)
437 {
438         lock_super(sb);
439         if (sb->s_root && sb->s_dirt)
440                 if (sb->s_op->write_super)
441                         sb->s_op->write_super(sb);
442         unlock_super(sb);
443 }
444
445 /*
446  * Note: check the dirty flag before waiting, so we don't
447  * hold up the sync while mounting a device. (The newly
448  * mounted device won't need syncing.)
449  */
450 void sync_supers(void)
451 {
452         struct super_block *sb;
453
454         spin_lock(&sb_lock);
455 restart:
456         list_for_each_entry(sb, &super_blocks, s_list) {
457                 if (sb->s_dirt) {
458                         sb->s_count++;
459                         spin_unlock(&sb_lock);
460                         down_read(&sb->s_umount);
461                         write_super(sb);
462                         up_read(&sb->s_umount);
463                         spin_lock(&sb_lock);
464                         if (__put_super_and_need_restart(sb))
465                                 goto restart;
466                 }
467         }
468         spin_unlock(&sb_lock);
469 }
470
471 /*
472  * Call the ->sync_fs super_op against all filesystems which are r/w and
473  * which implement it.
474  *
475  * This operation is careful to avoid the livelock which could easily happen
476  * if two or more filesystems are being continuously dirtied.  s_need_sync_fs
477  * is used only here.  We set it against all filesystems and then clear it as
478  * we sync them.  So redirtied filesystems are skipped.
479  *
480  * But if process A is currently running sync_filesystems and then process B
481  * calls sync_filesystems as well, process B will set all the s_need_sync_fs
482  * flags again, which will cause process A to resync everything.  Fix that with
483  * a local mutex.
484  *
485  * (Fabian) Avoid sync_fs with clean fs & wait mode 0
486  */
487 void sync_filesystems(int wait)
488 {
489         struct super_block *sb;
490         static DEFINE_MUTEX(mutex);
491
492         mutex_lock(&mutex);             /* Could be down_interruptible */
493         spin_lock(&sb_lock);
494         list_for_each_entry(sb, &super_blocks, s_list) {
495                 if (!sb->s_op->sync_fs)
496                         continue;
497                 if (sb->s_flags & MS_RDONLY)
498                         continue;
499                 sb->s_need_sync_fs = 1;
500         }
501
502 restart:
503         list_for_each_entry(sb, &super_blocks, s_list) {
504                 if (!sb->s_need_sync_fs)
505                         continue;
506                 sb->s_need_sync_fs = 0;
507                 if (sb->s_flags & MS_RDONLY)
508                         continue;       /* hm.  Was remounted r/o meanwhile */
509                 sb->s_count++;
510                 spin_unlock(&sb_lock);
511                 down_read(&sb->s_umount);
512                 if (sb->s_root)
513                         sb->s_op->sync_fs(sb, wait);
514                 up_read(&sb->s_umount);
515                 /* restart only when sb is no longer on the list */
516                 spin_lock(&sb_lock);
517                 if (__put_super_and_need_restart(sb))
518                         goto restart;
519         }
520         spin_unlock(&sb_lock);
521         mutex_unlock(&mutex);
522 }
523
524 #ifdef CONFIG_BLOCK
525 /*
526  *  Sync all block devices underlying some superblock
527  */
528 void sync_blockdevs(void)
529 {
530         struct super_block *sb;
531
532         spin_lock(&sb_lock);
533 restart:
534         list_for_each_entry(sb, &super_blocks, s_list) {
535                 if (!sb->s_bdev)
536                         continue;
537                 sb->s_count++;
538                 spin_unlock(&sb_lock);
539                 down_read(&sb->s_umount);
540                 if (sb->s_root)
541                         sync_blockdev(sb->s_bdev);
542                 up_read(&sb->s_umount);
543                 spin_lock(&sb_lock);
544                 if (__put_super_and_need_restart(sb))
545                         goto restart;
546         }
547         spin_unlock(&sb_lock);
548 }
549 #endif
550
551 /**
552  *      get_super - get the superblock of a device
553  *      @bdev: device to get the superblock for
554  *      
555  *      Scans the superblock list and finds the superblock of the file system
556  *      mounted on the device given. %NULL is returned if no match is found.
557  */
558
559 struct super_block * get_super(struct block_device *bdev)
560 {
561         struct super_block *sb;
562
563         if (!bdev)
564                 return NULL;
565
566         spin_lock(&sb_lock);
567 rescan:
568         list_for_each_entry(sb, &super_blocks, s_list) {
569                 if (sb->s_bdev == bdev) {
570                         sb->s_count++;
571                         spin_unlock(&sb_lock);
572                         down_read(&sb->s_umount);
573                         if (sb->s_root)
574                                 return sb;
575                         up_read(&sb->s_umount);
576                         /* restart only when sb is no longer on the list */
577                         spin_lock(&sb_lock);
578                         if (__put_super_and_need_restart(sb))
579                                 goto rescan;
580                 }
581         }
582         spin_unlock(&sb_lock);
583         return NULL;
584 }
585
586 EXPORT_SYMBOL(get_super);
587  
588 struct super_block * user_get_super(dev_t dev)
589 {
590         struct super_block *sb;
591
592         spin_lock(&sb_lock);
593 rescan:
594         list_for_each_entry(sb, &super_blocks, s_list) {
595                 if (sb->s_dev ==  dev) {
596                         sb->s_count++;
597                         spin_unlock(&sb_lock);
598                         down_read(&sb->s_umount);
599                         if (sb->s_root)
600                                 return sb;
601                         up_read(&sb->s_umount);
602                         /* restart only when sb is no longer on the list */
603                         spin_lock(&sb_lock);
604                         if (__put_super_and_need_restart(sb))
605                                 goto rescan;
606                 }
607         }
608         spin_unlock(&sb_lock);
609         return NULL;
610 }
611
612 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
613 {
614         struct super_block *s;
615         struct ustat tmp;
616         struct kstatfs sbuf;
617         int err = -EINVAL;
618
619         s = user_get_super(new_decode_dev(dev));
620         if (s == NULL)
621                 goto out;
622         err = vfs_statfs(s->s_root, &sbuf);
623         drop_super(s);
624         if (err)
625                 goto out;
626
627         memset(&tmp,0,sizeof(struct ustat));
628         tmp.f_tfree = sbuf.f_bfree;
629         tmp.f_tinode = sbuf.f_ffree;
630
631         err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
632 out:
633         return err;
634 }
635
636 /**
637  *      do_remount_sb - asks filesystem to change mount options.
638  *      @sb:    superblock in question
639  *      @flags: numeric part of options
640  *      @data:  the rest of options
641  *      @force: whether or not to force the change
642  *
643  *      Alters the mount options of a mounted file system.
644  */
645 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
646 {
647         int retval;
648         int remount_rw;
649         
650 #ifdef CONFIG_BLOCK
651         if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
652                 return -EACCES;
653 #endif
654         if (flags & MS_RDONLY)
655                 acct_auto_close(sb);
656         shrink_dcache_sb(sb);
657         fsync_super(sb);
658
659         /* If we are remounting RDONLY and current sb is read/write,
660            make sure there are no rw files opened */
661         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
662                 if (force)
663                         mark_files_ro(sb);
664                 else if (!fs_may_remount_ro(sb))
665                         return -EBUSY;
666                 retval = vfs_dq_off(sb, 1);
667                 if (retval < 0 && retval != -ENOSYS)
668                         return -EBUSY;
669         }
670         remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
671
672         if (sb->s_op->remount_fs) {
673                 lock_super(sb);
674                 retval = sb->s_op->remount_fs(sb, &flags, data);
675                 unlock_super(sb);
676                 if (retval)
677                         return retval;
678         }
679         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
680         if (remount_rw)
681                 vfs_dq_quota_on_remount(sb);
682         return 0;
683 }
684
685 static void do_emergency_remount(struct work_struct *work)
686 {
687         struct super_block *sb;
688
689         spin_lock(&sb_lock);
690         list_for_each_entry(sb, &super_blocks, s_list) {
691                 sb->s_count++;
692                 spin_unlock(&sb_lock);
693                 down_read(&sb->s_umount);
694                 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
695                         /*
696                          * ->remount_fs needs lock_kernel().
697                          *
698                          * What lock protects sb->s_flags??
699                          */
700                         lock_kernel();
701                         do_remount_sb(sb, MS_RDONLY, NULL, 1);
702                         unlock_kernel();
703                 }
704                 drop_super(sb);
705                 spin_lock(&sb_lock);
706         }
707         spin_unlock(&sb_lock);
708         kfree(work);
709         printk("Emergency Remount complete\n");
710 }
711
712 void emergency_remount(void)
713 {
714         struct work_struct *work;
715
716         work = kmalloc(sizeof(*work), GFP_ATOMIC);
717         if (work) {
718                 INIT_WORK(work, do_emergency_remount);
719                 schedule_work(work);
720         }
721 }
722
723 /*
724  * Unnamed block devices are dummy devices used by virtual
725  * filesystems which don't use real block-devices.  -- jrs
726  */
727
728 static DEFINE_IDA(unnamed_dev_ida);
729 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
730
731 int set_anon_super(struct super_block *s, void *data)
732 {
733         int dev;
734         int error;
735
736  retry:
737         if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
738                 return -ENOMEM;
739         spin_lock(&unnamed_dev_lock);
740         error = ida_get_new(&unnamed_dev_ida, &dev);
741         spin_unlock(&unnamed_dev_lock);
742         if (error == -EAGAIN)
743                 /* We raced and lost with another CPU. */
744                 goto retry;
745         else if (error)
746                 return -EAGAIN;
747
748         if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
749                 spin_lock(&unnamed_dev_lock);
750                 ida_remove(&unnamed_dev_ida, dev);
751                 spin_unlock(&unnamed_dev_lock);
752                 return -EMFILE;
753         }
754         s->s_dev = MKDEV(0, dev & MINORMASK);
755         return 0;
756 }
757
758 EXPORT_SYMBOL(set_anon_super);
759
760 void kill_anon_super(struct super_block *sb)
761 {
762         int slot = MINOR(sb->s_dev);
763
764         generic_shutdown_super(sb);
765         spin_lock(&unnamed_dev_lock);
766         ida_remove(&unnamed_dev_ida, slot);
767         spin_unlock(&unnamed_dev_lock);
768 }
769
770 EXPORT_SYMBOL(kill_anon_super);
771
772 void kill_litter_super(struct super_block *sb)
773 {
774         if (sb->s_root)
775                 d_genocide(sb->s_root);
776         kill_anon_super(sb);
777 }
778
779 EXPORT_SYMBOL(kill_litter_super);
780
781 static int ns_test_super(struct super_block *sb, void *data)
782 {
783         return sb->s_fs_info == data;
784 }
785
786 static int ns_set_super(struct super_block *sb, void *data)
787 {
788         sb->s_fs_info = data;
789         return set_anon_super(sb, NULL);
790 }
791
792 int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
793         int (*fill_super)(struct super_block *, void *, int),
794         struct vfsmount *mnt)
795 {
796         struct super_block *sb;
797
798         sb = sget(fs_type, ns_test_super, ns_set_super, data);
799         if (IS_ERR(sb))
800                 return PTR_ERR(sb);
801
802         if (!sb->s_root) {
803                 int err;
804                 sb->s_flags = flags;
805                 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
806                 if (err) {
807                         deactivate_locked_super(sb);
808                         return err;
809                 }
810
811                 sb->s_flags |= MS_ACTIVE;
812         }
813
814         simple_set_mnt(mnt, sb);
815         return 0;
816 }
817
818 EXPORT_SYMBOL(get_sb_ns);
819
820 #ifdef CONFIG_BLOCK
821 static int set_bdev_super(struct super_block *s, void *data)
822 {
823         s->s_bdev = data;
824         s->s_dev = s->s_bdev->bd_dev;
825         return 0;
826 }
827
828 static int test_bdev_super(struct super_block *s, void *data)
829 {
830         return (void *)s->s_bdev == data;
831 }
832
833 int get_sb_bdev(struct file_system_type *fs_type,
834         int flags, const char *dev_name, void *data,
835         int (*fill_super)(struct super_block *, void *, int),
836         struct vfsmount *mnt)
837 {
838         struct block_device *bdev;
839         struct super_block *s;
840         fmode_t mode = FMODE_READ;
841         int error = 0;
842
843         if (!(flags & MS_RDONLY))
844                 mode |= FMODE_WRITE;
845
846         bdev = open_bdev_exclusive(dev_name, mode, fs_type);
847         if (IS_ERR(bdev))
848                 return PTR_ERR(bdev);
849
850         /*
851          * once the super is inserted into the list by sget, s_umount
852          * will protect the lockfs code from trying to start a snapshot
853          * while we are mounting
854          */
855         down(&bdev->bd_mount_sem);
856         s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
857         up(&bdev->bd_mount_sem);
858         if (IS_ERR(s))
859                 goto error_s;
860
861         if (s->s_root) {
862                 if ((flags ^ s->s_flags) & MS_RDONLY) {
863                         deactivate_locked_super(s);
864                         error = -EBUSY;
865                         goto error_bdev;
866                 }
867
868                 close_bdev_exclusive(bdev, mode);
869         } else {
870                 char b[BDEVNAME_SIZE];
871
872                 s->s_flags = flags;
873                 s->s_mode = mode;
874                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
875                 sb_set_blocksize(s, block_size(bdev));
876                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
877                 if (error) {
878                         deactivate_locked_super(s);
879                         goto error;
880                 }
881
882                 s->s_flags |= MS_ACTIVE;
883                 bdev->bd_super = s;
884         }
885
886         simple_set_mnt(mnt, s);
887         return 0;
888
889 error_s:
890         error = PTR_ERR(s);
891 error_bdev:
892         close_bdev_exclusive(bdev, mode);
893 error:
894         return error;
895 }
896
897 EXPORT_SYMBOL(get_sb_bdev);
898
899 void kill_block_super(struct super_block *sb)
900 {
901         struct block_device *bdev = sb->s_bdev;
902         fmode_t mode = sb->s_mode;
903
904         bdev->bd_super = NULL;
905         generic_shutdown_super(sb);
906         sync_blockdev(bdev);
907         close_bdev_exclusive(bdev, mode);
908 }
909
910 EXPORT_SYMBOL(kill_block_super);
911 #endif
912
913 int get_sb_nodev(struct file_system_type *fs_type,
914         int flags, void *data,
915         int (*fill_super)(struct super_block *, void *, int),
916         struct vfsmount *mnt)
917 {
918         int error;
919         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
920
921         if (IS_ERR(s))
922                 return PTR_ERR(s);
923
924         s->s_flags = flags;
925
926         error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
927         if (error) {
928                 deactivate_locked_super(s);
929                 return error;
930         }
931         s->s_flags |= MS_ACTIVE;
932         simple_set_mnt(mnt, s);
933         return 0;
934 }
935
936 EXPORT_SYMBOL(get_sb_nodev);
937
938 static int compare_single(struct super_block *s, void *p)
939 {
940         return 1;
941 }
942
943 int get_sb_single(struct file_system_type *fs_type,
944         int flags, void *data,
945         int (*fill_super)(struct super_block *, void *, int),
946         struct vfsmount *mnt)
947 {
948         struct super_block *s;
949         int error;
950
951         s = sget(fs_type, compare_single, set_anon_super, NULL);
952         if (IS_ERR(s))
953                 return PTR_ERR(s);
954         if (!s->s_root) {
955                 s->s_flags = flags;
956                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
957                 if (error) {
958                         deactivate_locked_super(s);
959                         return error;
960                 }
961                 s->s_flags |= MS_ACTIVE;
962         }
963         do_remount_sb(s, flags, data, 0);
964         simple_set_mnt(mnt, s);
965         return 0;
966 }
967
968 EXPORT_SYMBOL(get_sb_single);
969
970 struct vfsmount *
971 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
972 {
973         struct vfsmount *mnt;
974         char *secdata = NULL;
975         int error;
976
977         if (!type)
978                 return ERR_PTR(-ENODEV);
979
980         error = -ENOMEM;
981         mnt = alloc_vfsmnt(name);
982         if (!mnt)
983                 goto out;
984
985         if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
986                 secdata = alloc_secdata();
987                 if (!secdata)
988                         goto out_mnt;
989
990                 error = security_sb_copy_data(data, secdata);
991                 if (error)
992                         goto out_free_secdata;
993         }
994
995         error = type->get_sb(type, flags, name, data, mnt);
996         if (error < 0)
997                 goto out_free_secdata;
998         BUG_ON(!mnt->mnt_sb);
999
1000         error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
1001         if (error)
1002                 goto out_sb;
1003
1004         mnt->mnt_mountpoint = mnt->mnt_root;
1005         mnt->mnt_parent = mnt;
1006         up_write(&mnt->mnt_sb->s_umount);
1007         free_secdata(secdata);
1008         return mnt;
1009 out_sb:
1010         dput(mnt->mnt_root);
1011         deactivate_locked_super(mnt->mnt_sb);
1012 out_free_secdata:
1013         free_secdata(secdata);
1014 out_mnt:
1015         free_vfsmnt(mnt);
1016 out:
1017         return ERR_PTR(error);
1018 }
1019
1020 EXPORT_SYMBOL_GPL(vfs_kern_mount);
1021
1022 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1023 {
1024         int err;
1025         const char *subtype = strchr(fstype, '.');
1026         if (subtype) {
1027                 subtype++;
1028                 err = -EINVAL;
1029                 if (!subtype[0])
1030                         goto err;
1031         } else
1032                 subtype = "";
1033
1034         mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1035         err = -ENOMEM;
1036         if (!mnt->mnt_sb->s_subtype)
1037                 goto err;
1038         return mnt;
1039
1040  err:
1041         mntput(mnt);
1042         return ERR_PTR(err);
1043 }
1044
1045 struct vfsmount *
1046 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1047 {
1048         struct file_system_type *type = get_fs_type(fstype);
1049         struct vfsmount *mnt;
1050         if (!type)
1051                 return ERR_PTR(-ENODEV);
1052         mnt = vfs_kern_mount(type, flags, name, data);
1053         if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1054             !mnt->mnt_sb->s_subtype)
1055                 mnt = fs_set_subtype(mnt, fstype);
1056         put_filesystem(type);
1057         return mnt;
1058 }
1059 EXPORT_SYMBOL_GPL(do_kern_mount);
1060
1061 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
1062 {
1063         return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
1064 }
1065
1066 EXPORT_SYMBOL_GPL(kern_mount_data);