vfs: Make sys_sync() use fsync_super() (version 4)
[safe/jmp/linux-2.6] / 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  * Do the filesystem syncing work. For simple filesystems sync_inodes_sb(sb, 0)
288  * just dirties buffers with inodes so we have to submit IO for these buffers
289  * via __sync_blockdev(). This also speeds up the wait == 1 case since in that
290  * case write_inode() functions do sync_dirty_buffer() and thus effectively
291  * write one block at a time.
292  */
293 static int __fsync_super(struct super_block *sb, int wait)
294 {
295         vfs_dq_sync(sb);
296         sync_inodes_sb(sb, wait);
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, wait);
303         return __sync_blockdev(sb->s_bdev, wait);
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         int ret;
314
315         ret = __fsync_super(sb, 0);
316         if (ret < 0)
317                 return ret;
318         return __fsync_super(sb, 1);
319 }
320 EXPORT_SYMBOL_GPL(fsync_super);
321
322 /**
323  *      generic_shutdown_super  -       common helper for ->kill_sb()
324  *      @sb: superblock to kill
325  *
326  *      generic_shutdown_super() does all fs-independent work on superblock
327  *      shutdown.  Typical ->kill_sb() should pick all fs-specific objects
328  *      that need destruction out of superblock, call generic_shutdown_super()
329  *      and release aforementioned objects.  Note: dentries and inodes _are_
330  *      taken care of and do not need specific handling.
331  *
332  *      Upon calling this function, the filesystem may no longer alter or
333  *      rearrange the set of dentries belonging to this super_block, nor may it
334  *      change the attachments of dentries to inodes.
335  */
336 void generic_shutdown_super(struct super_block *sb)
337 {
338         const struct super_operations *sop = sb->s_op;
339
340
341         if (sb->s_root) {
342                 shrink_dcache_for_umount(sb);
343                 fsync_super(sb);
344                 lock_super(sb);
345                 sb->s_flags &= ~MS_ACTIVE;
346
347                 /* bad name - it should be evict_inodes() */
348                 invalidate_inodes(sb);
349                 lock_kernel();
350
351                 if (sop->write_super && sb->s_dirt)
352                         sop->write_super(sb);
353                 if (sop->put_super)
354                         sop->put_super(sb);
355
356                 /* Forget any remaining inodes */
357                 if (invalidate_inodes(sb)) {
358                         printk("VFS: Busy inodes after unmount of %s. "
359                            "Self-destruct in 5 seconds.  Have a nice day...\n",
360                            sb->s_id);
361                 }
362
363                 unlock_kernel();
364                 unlock_super(sb);
365         }
366         spin_lock(&sb_lock);
367         /* should be initialized for __put_super_and_need_restart() */
368         list_del_init(&sb->s_list);
369         list_del(&sb->s_instances);
370         spin_unlock(&sb_lock);
371         up_write(&sb->s_umount);
372 }
373
374 EXPORT_SYMBOL(generic_shutdown_super);
375
376 /**
377  *      sget    -       find or create a superblock
378  *      @type:  filesystem type superblock should belong to
379  *      @test:  comparison callback
380  *      @set:   setup callback
381  *      @data:  argument to each of them
382  */
383 struct super_block *sget(struct file_system_type *type,
384                         int (*test)(struct super_block *,void *),
385                         int (*set)(struct super_block *,void *),
386                         void *data)
387 {
388         struct super_block *s = NULL;
389         struct super_block *old;
390         int err;
391
392 retry:
393         spin_lock(&sb_lock);
394         if (test) {
395                 list_for_each_entry(old, &type->fs_supers, s_instances) {
396                         if (!test(old, data))
397                                 continue;
398                         if (!grab_super(old))
399                                 goto retry;
400                         if (s) {
401                                 up_write(&s->s_umount);
402                                 destroy_super(s);
403                         }
404                         return old;
405                 }
406         }
407         if (!s) {
408                 spin_unlock(&sb_lock);
409                 s = alloc_super(type);
410                 if (!s)
411                         return ERR_PTR(-ENOMEM);
412                 goto retry;
413         }
414                 
415         err = set(s, data);
416         if (err) {
417                 spin_unlock(&sb_lock);
418                 up_write(&s->s_umount);
419                 destroy_super(s);
420                 return ERR_PTR(err);
421         }
422         s->s_type = type;
423         strlcpy(s->s_id, type->name, sizeof(s->s_id));
424         list_add_tail(&s->s_list, &super_blocks);
425         list_add(&s->s_instances, &type->fs_supers);
426         spin_unlock(&sb_lock);
427         get_filesystem(type);
428         return s;
429 }
430
431 EXPORT_SYMBOL(sget);
432
433 void drop_super(struct super_block *sb)
434 {
435         up_read(&sb->s_umount);
436         put_super(sb);
437 }
438
439 EXPORT_SYMBOL(drop_super);
440
441 static inline void write_super(struct super_block *sb)
442 {
443         lock_super(sb);
444         if (sb->s_root && sb->s_dirt)
445                 if (sb->s_op->write_super)
446                         sb->s_op->write_super(sb);
447         unlock_super(sb);
448 }
449
450 /*
451  * Note: check the dirty flag before waiting, so we don't
452  * hold up the sync while mounting a device. (The newly
453  * mounted device won't need syncing.)
454  */
455 void sync_supers(void)
456 {
457         struct super_block *sb;
458
459         spin_lock(&sb_lock);
460 restart:
461         list_for_each_entry(sb, &super_blocks, s_list) {
462                 if (sb->s_dirt) {
463                         sb->s_count++;
464                         spin_unlock(&sb_lock);
465                         down_read(&sb->s_umount);
466                         write_super(sb);
467                         up_read(&sb->s_umount);
468                         spin_lock(&sb_lock);
469                         if (__put_super_and_need_restart(sb))
470                                 goto restart;
471                 }
472         }
473         spin_unlock(&sb_lock);
474 }
475
476 /*
477  * Sync all the data for all the filesystems (called by sys_sync() and
478  * emergency sync)
479  *
480  * This operation is careful to avoid the livelock which could easily happen
481  * if two or more filesystems are being continuously dirtied.  s_need_sync
482  * is used only here.  We set it against all filesystems and then clear it as
483  * we sync them.  So redirtied filesystems are skipped.
484  *
485  * But if process A is currently running sync_filesystems and then process B
486  * calls sync_filesystems as well, process B will set all the s_need_sync
487  * flags again, which will cause process A to resync everything.  Fix that with
488  * a local mutex.
489  */
490 void sync_filesystems(int wait)
491 {
492         struct super_block *sb;
493         static DEFINE_MUTEX(mutex);
494
495         mutex_lock(&mutex);             /* Could be down_interruptible */
496         spin_lock(&sb_lock);
497         list_for_each_entry(sb, &super_blocks, s_list) {
498                 if (sb->s_flags & MS_RDONLY)
499                         continue;
500                 sb->s_need_sync = 1;
501         }
502
503 restart:
504         list_for_each_entry(sb, &super_blocks, s_list) {
505                 if (!sb->s_need_sync)
506                         continue;
507                 sb->s_need_sync = 0;
508                 if (sb->s_flags & MS_RDONLY)
509                         continue;       /* hm.  Was remounted r/o meanwhile */
510                 sb->s_count++;
511                 spin_unlock(&sb_lock);
512                 down_read(&sb->s_umount);
513                 if (sb->s_root)
514                         __fsync_super(sb, wait);
515                 up_read(&sb->s_umount);
516                 /* restart only when sb is no longer on the list */
517                 spin_lock(&sb_lock);
518                 if (__put_super_and_need_restart(sb))
519                         goto restart;
520         }
521         spin_unlock(&sb_lock);
522         mutex_unlock(&mutex);
523 }
524
525 /**
526  *      get_super - get the superblock of a device
527  *      @bdev: device to get the superblock for
528  *      
529  *      Scans the superblock list and finds the superblock of the file system
530  *      mounted on the device given. %NULL is returned if no match is found.
531  */
532
533 struct super_block * get_super(struct block_device *bdev)
534 {
535         struct super_block *sb;
536
537         if (!bdev)
538                 return NULL;
539
540         spin_lock(&sb_lock);
541 rescan:
542         list_for_each_entry(sb, &super_blocks, s_list) {
543                 if (sb->s_bdev == bdev) {
544                         sb->s_count++;
545                         spin_unlock(&sb_lock);
546                         down_read(&sb->s_umount);
547                         if (sb->s_root)
548                                 return sb;
549                         up_read(&sb->s_umount);
550                         /* restart only when sb is no longer on the list */
551                         spin_lock(&sb_lock);
552                         if (__put_super_and_need_restart(sb))
553                                 goto rescan;
554                 }
555         }
556         spin_unlock(&sb_lock);
557         return NULL;
558 }
559
560 EXPORT_SYMBOL(get_super);
561  
562 struct super_block * user_get_super(dev_t dev)
563 {
564         struct super_block *sb;
565
566         spin_lock(&sb_lock);
567 rescan:
568         list_for_each_entry(sb, &super_blocks, s_list) {
569                 if (sb->s_dev ==  dev) {
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 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
587 {
588         struct super_block *s;
589         struct ustat tmp;
590         struct kstatfs sbuf;
591         int err = -EINVAL;
592
593         s = user_get_super(new_decode_dev(dev));
594         if (s == NULL)
595                 goto out;
596         err = vfs_statfs(s->s_root, &sbuf);
597         drop_super(s);
598         if (err)
599                 goto out;
600
601         memset(&tmp,0,sizeof(struct ustat));
602         tmp.f_tfree = sbuf.f_bfree;
603         tmp.f_tinode = sbuf.f_ffree;
604
605         err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
606 out:
607         return err;
608 }
609
610 /**
611  *      do_remount_sb - asks filesystem to change mount options.
612  *      @sb:    superblock in question
613  *      @flags: numeric part of options
614  *      @data:  the rest of options
615  *      @force: whether or not to force the change
616  *
617  *      Alters the mount options of a mounted file system.
618  */
619 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
620 {
621         int retval;
622         int remount_rw;
623         
624 #ifdef CONFIG_BLOCK
625         if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
626                 return -EACCES;
627 #endif
628         if (flags & MS_RDONLY)
629                 acct_auto_close(sb);
630         shrink_dcache_sb(sb);
631         fsync_super(sb);
632
633         /* If we are remounting RDONLY and current sb is read/write,
634            make sure there are no rw files opened */
635         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
636                 if (force)
637                         mark_files_ro(sb);
638                 else if (!fs_may_remount_ro(sb))
639                         return -EBUSY;
640                 retval = vfs_dq_off(sb, 1);
641                 if (retval < 0 && retval != -ENOSYS)
642                         return -EBUSY;
643         }
644         remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
645
646         if (sb->s_op->remount_fs) {
647                 lock_super(sb);
648                 retval = sb->s_op->remount_fs(sb, &flags, data);
649                 unlock_super(sb);
650                 if (retval)
651                         return retval;
652         }
653         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
654         if (remount_rw)
655                 vfs_dq_quota_on_remount(sb);
656         return 0;
657 }
658
659 static void do_emergency_remount(struct work_struct *work)
660 {
661         struct super_block *sb;
662
663         spin_lock(&sb_lock);
664         list_for_each_entry(sb, &super_blocks, s_list) {
665                 sb->s_count++;
666                 spin_unlock(&sb_lock);
667                 down_read(&sb->s_umount);
668                 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
669                         /*
670                          * ->remount_fs needs lock_kernel().
671                          *
672                          * What lock protects sb->s_flags??
673                          */
674                         lock_kernel();
675                         do_remount_sb(sb, MS_RDONLY, NULL, 1);
676                         unlock_kernel();
677                 }
678                 drop_super(sb);
679                 spin_lock(&sb_lock);
680         }
681         spin_unlock(&sb_lock);
682         kfree(work);
683         printk("Emergency Remount complete\n");
684 }
685
686 void emergency_remount(void)
687 {
688         struct work_struct *work;
689
690         work = kmalloc(sizeof(*work), GFP_ATOMIC);
691         if (work) {
692                 INIT_WORK(work, do_emergency_remount);
693                 schedule_work(work);
694         }
695 }
696
697 /*
698  * Unnamed block devices are dummy devices used by virtual
699  * filesystems which don't use real block-devices.  -- jrs
700  */
701
702 static DEFINE_IDA(unnamed_dev_ida);
703 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
704
705 int set_anon_super(struct super_block *s, void *data)
706 {
707         int dev;
708         int error;
709
710  retry:
711         if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
712                 return -ENOMEM;
713         spin_lock(&unnamed_dev_lock);
714         error = ida_get_new(&unnamed_dev_ida, &dev);
715         spin_unlock(&unnamed_dev_lock);
716         if (error == -EAGAIN)
717                 /* We raced and lost with another CPU. */
718                 goto retry;
719         else if (error)
720                 return -EAGAIN;
721
722         if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
723                 spin_lock(&unnamed_dev_lock);
724                 ida_remove(&unnamed_dev_ida, dev);
725                 spin_unlock(&unnamed_dev_lock);
726                 return -EMFILE;
727         }
728         s->s_dev = MKDEV(0, dev & MINORMASK);
729         return 0;
730 }
731
732 EXPORT_SYMBOL(set_anon_super);
733
734 void kill_anon_super(struct super_block *sb)
735 {
736         int slot = MINOR(sb->s_dev);
737
738         generic_shutdown_super(sb);
739         spin_lock(&unnamed_dev_lock);
740         ida_remove(&unnamed_dev_ida, slot);
741         spin_unlock(&unnamed_dev_lock);
742 }
743
744 EXPORT_SYMBOL(kill_anon_super);
745
746 void kill_litter_super(struct super_block *sb)
747 {
748         if (sb->s_root)
749                 d_genocide(sb->s_root);
750         kill_anon_super(sb);
751 }
752
753 EXPORT_SYMBOL(kill_litter_super);
754
755 static int ns_test_super(struct super_block *sb, void *data)
756 {
757         return sb->s_fs_info == data;
758 }
759
760 static int ns_set_super(struct super_block *sb, void *data)
761 {
762         sb->s_fs_info = data;
763         return set_anon_super(sb, NULL);
764 }
765
766 int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
767         int (*fill_super)(struct super_block *, void *, int),
768         struct vfsmount *mnt)
769 {
770         struct super_block *sb;
771
772         sb = sget(fs_type, ns_test_super, ns_set_super, data);
773         if (IS_ERR(sb))
774                 return PTR_ERR(sb);
775
776         if (!sb->s_root) {
777                 int err;
778                 sb->s_flags = flags;
779                 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
780                 if (err) {
781                         deactivate_locked_super(sb);
782                         return err;
783                 }
784
785                 sb->s_flags |= MS_ACTIVE;
786         }
787
788         simple_set_mnt(mnt, sb);
789         return 0;
790 }
791
792 EXPORT_SYMBOL(get_sb_ns);
793
794 #ifdef CONFIG_BLOCK
795 static int set_bdev_super(struct super_block *s, void *data)
796 {
797         s->s_bdev = data;
798         s->s_dev = s->s_bdev->bd_dev;
799         return 0;
800 }
801
802 static int test_bdev_super(struct super_block *s, void *data)
803 {
804         return (void *)s->s_bdev == data;
805 }
806
807 int get_sb_bdev(struct file_system_type *fs_type,
808         int flags, const char *dev_name, void *data,
809         int (*fill_super)(struct super_block *, void *, int),
810         struct vfsmount *mnt)
811 {
812         struct block_device *bdev;
813         struct super_block *s;
814         fmode_t mode = FMODE_READ;
815         int error = 0;
816
817         if (!(flags & MS_RDONLY))
818                 mode |= FMODE_WRITE;
819
820         bdev = open_bdev_exclusive(dev_name, mode, fs_type);
821         if (IS_ERR(bdev))
822                 return PTR_ERR(bdev);
823
824         /*
825          * once the super is inserted into the list by sget, s_umount
826          * will protect the lockfs code from trying to start a snapshot
827          * while we are mounting
828          */
829         down(&bdev->bd_mount_sem);
830         s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
831         up(&bdev->bd_mount_sem);
832         if (IS_ERR(s))
833                 goto error_s;
834
835         if (s->s_root) {
836                 if ((flags ^ s->s_flags) & MS_RDONLY) {
837                         deactivate_locked_super(s);
838                         error = -EBUSY;
839                         goto error_bdev;
840                 }
841
842                 close_bdev_exclusive(bdev, mode);
843         } else {
844                 char b[BDEVNAME_SIZE];
845
846                 s->s_flags = flags;
847                 s->s_mode = mode;
848                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
849                 sb_set_blocksize(s, block_size(bdev));
850                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
851                 if (error) {
852                         deactivate_locked_super(s);
853                         goto error;
854                 }
855
856                 s->s_flags |= MS_ACTIVE;
857                 bdev->bd_super = s;
858         }
859
860         simple_set_mnt(mnt, s);
861         return 0;
862
863 error_s:
864         error = PTR_ERR(s);
865 error_bdev:
866         close_bdev_exclusive(bdev, mode);
867 error:
868         return error;
869 }
870
871 EXPORT_SYMBOL(get_sb_bdev);
872
873 void kill_block_super(struct super_block *sb)
874 {
875         struct block_device *bdev = sb->s_bdev;
876         fmode_t mode = sb->s_mode;
877
878         bdev->bd_super = NULL;
879         generic_shutdown_super(sb);
880         sync_blockdev(bdev);
881         close_bdev_exclusive(bdev, mode);
882 }
883
884 EXPORT_SYMBOL(kill_block_super);
885 #endif
886
887 int get_sb_nodev(struct file_system_type *fs_type,
888         int flags, void *data,
889         int (*fill_super)(struct super_block *, void *, int),
890         struct vfsmount *mnt)
891 {
892         int error;
893         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
894
895         if (IS_ERR(s))
896                 return PTR_ERR(s);
897
898         s->s_flags = flags;
899
900         error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
901         if (error) {
902                 deactivate_locked_super(s);
903                 return error;
904         }
905         s->s_flags |= MS_ACTIVE;
906         simple_set_mnt(mnt, s);
907         return 0;
908 }
909
910 EXPORT_SYMBOL(get_sb_nodev);
911
912 static int compare_single(struct super_block *s, void *p)
913 {
914         return 1;
915 }
916
917 int get_sb_single(struct file_system_type *fs_type,
918         int flags, void *data,
919         int (*fill_super)(struct super_block *, void *, int),
920         struct vfsmount *mnt)
921 {
922         struct super_block *s;
923         int error;
924
925         s = sget(fs_type, compare_single, set_anon_super, NULL);
926         if (IS_ERR(s))
927                 return PTR_ERR(s);
928         if (!s->s_root) {
929                 s->s_flags = flags;
930                 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
931                 if (error) {
932                         deactivate_locked_super(s);
933                         return error;
934                 }
935                 s->s_flags |= MS_ACTIVE;
936         }
937         do_remount_sb(s, flags, data, 0);
938         simple_set_mnt(mnt, s);
939         return 0;
940 }
941
942 EXPORT_SYMBOL(get_sb_single);
943
944 struct vfsmount *
945 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
946 {
947         struct vfsmount *mnt;
948         char *secdata = NULL;
949         int error;
950
951         if (!type)
952                 return ERR_PTR(-ENODEV);
953
954         error = -ENOMEM;
955         mnt = alloc_vfsmnt(name);
956         if (!mnt)
957                 goto out;
958
959         if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
960                 secdata = alloc_secdata();
961                 if (!secdata)
962                         goto out_mnt;
963
964                 error = security_sb_copy_data(data, secdata);
965                 if (error)
966                         goto out_free_secdata;
967         }
968
969         error = type->get_sb(type, flags, name, data, mnt);
970         if (error < 0)
971                 goto out_free_secdata;
972         BUG_ON(!mnt->mnt_sb);
973
974         error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
975         if (error)
976                 goto out_sb;
977
978         mnt->mnt_mountpoint = mnt->mnt_root;
979         mnt->mnt_parent = mnt;
980         up_write(&mnt->mnt_sb->s_umount);
981         free_secdata(secdata);
982         return mnt;
983 out_sb:
984         dput(mnt->mnt_root);
985         deactivate_locked_super(mnt->mnt_sb);
986 out_free_secdata:
987         free_secdata(secdata);
988 out_mnt:
989         free_vfsmnt(mnt);
990 out:
991         return ERR_PTR(error);
992 }
993
994 EXPORT_SYMBOL_GPL(vfs_kern_mount);
995
996 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
997 {
998         int err;
999         const char *subtype = strchr(fstype, '.');
1000         if (subtype) {
1001                 subtype++;
1002                 err = -EINVAL;
1003                 if (!subtype[0])
1004                         goto err;
1005         } else
1006                 subtype = "";
1007
1008         mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1009         err = -ENOMEM;
1010         if (!mnt->mnt_sb->s_subtype)
1011                 goto err;
1012         return mnt;
1013
1014  err:
1015         mntput(mnt);
1016         return ERR_PTR(err);
1017 }
1018
1019 struct vfsmount *
1020 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1021 {
1022         struct file_system_type *type = get_fs_type(fstype);
1023         struct vfsmount *mnt;
1024         if (!type)
1025                 return ERR_PTR(-ENODEV);
1026         mnt = vfs_kern_mount(type, flags, name, data);
1027         if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1028             !mnt->mnt_sb->s_subtype)
1029                 mnt = fs_set_subtype(mnt, fstype);
1030         put_filesystem(type);
1031         return mnt;
1032 }
1033 EXPORT_SYMBOL_GPL(do_kern_mount);
1034
1035 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
1036 {
1037         return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
1038 }
1039
1040 EXPORT_SYMBOL_GPL(kern_mount_data);