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