ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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/config.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/smp_lock.h>
28 #include <linux/acct.h>
29 #include <linux/blkdev.h>
30 #include <linux/quotaops.h>
31 #include <linux/namei.h>
32 #include <linux/buffer_head.h>          /* for fsync_super() */
33 #include <linux/mount.h>
34 #include <linux/security.h>
35 #include <linux/vfs.h>
36 #include <linux/writeback.h>            /* for the emergency remount stuff */
37 #include <linux/idr.h>
38 #include <asm/uaccess.h>
39
40
41 void get_filesystem(struct file_system_type *fs);
42 void put_filesystem(struct file_system_type *fs);
43 struct file_system_type *get_fs_type(const char *name);
44
45 LIST_HEAD(super_blocks);
46 spinlock_t sb_lock = SPIN_LOCK_UNLOCKED;
47
48 /**
49  *      alloc_super     -       create new superblock
50  *
51  *      Allocates and initializes a new &struct super_block.  alloc_super()
52  *      returns a pointer new superblock or %NULL if allocation had failed.
53  */
54 static struct super_block *alloc_super(void)
55 {
56         struct super_block *s = kmalloc(sizeof(struct super_block),  GFP_USER);
57         static struct super_operations default_op;
58
59         if (s) {
60                 memset(s, 0, sizeof(struct super_block));
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_files);
69                 INIT_LIST_HEAD(&s->s_instances);
70                 INIT_HLIST_HEAD(&s->s_anon);
71                 init_rwsem(&s->s_umount);
72                 sema_init(&s->s_lock, 1);
73                 down_write(&s->s_umount);
74                 s->s_count = S_BIAS;
75                 atomic_set(&s->s_active, 1);
76                 sema_init(&s->s_vfs_rename_sem,1);
77                 sema_init(&s->s_dquot.dqio_sem, 1);
78                 sema_init(&s->s_dquot.dqonoff_sem, 1);
79                 init_rwsem(&s->s_dquot.dqptr_sem);
80                 init_waitqueue_head(&s->s_wait_unfrozen);
81                 s->s_maxbytes = MAX_NON_LFS;
82                 s->dq_op = sb_dquot_ops;
83                 s->s_qcop = sb_quotactl_ops;
84                 s->s_op = &default_op;
85         }
86 out:
87         return s;
88 }
89
90 /**
91  *      destroy_super   -       frees a superblock
92  *      @s: superblock to free
93  *
94  *      Frees a superblock.
95  */
96 static inline void destroy_super(struct super_block *s)
97 {
98         security_sb_free(s);
99         kfree(s);
100 }
101
102 /* Superblock refcounting  */
103
104 /**
105  *      put_super       -       drop a temporary reference to superblock
106  *      @s: superblock in question
107  *
108  *      Drops a temporary reference, frees superblock if there's no
109  *      references left.
110  */
111 static inline void put_super(struct super_block *s)
112 {
113         spin_lock(&sb_lock);
114         if (!--s->s_count)
115                 destroy_super(s);
116         spin_unlock(&sb_lock);
117 }
118
119 /**
120  *      deactivate_super        -       drop an active reference to superblock
121  *      @s: superblock to deactivate
122  *
123  *      Drops an active reference to superblock, acquiring a temprory one if
124  *      there is no active references left.  In that case we lock superblock,
125  *      tell fs driver to shut it down and drop the temporary reference we
126  *      had just acquired.
127  */
128 void deactivate_super(struct super_block *s)
129 {
130         struct file_system_type *fs = s->s_type;
131         if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
132                 s->s_count -= S_BIAS-1;
133                 spin_unlock(&sb_lock);
134                 down_write(&s->s_umount);
135                 fs->kill_sb(s);
136                 put_filesystem(fs);
137                 put_super(s);
138         }
139 }
140
141 EXPORT_SYMBOL(deactivate_super);
142
143 /**
144  *      grab_super - acquire an active reference
145  *      @s: reference we are trying to make active
146  *
147  *      Tries to acquire an active reference.  grab_super() is used when we
148  *      had just found a superblock in super_blocks or fs_type->fs_supers
149  *      and want to turn it into a full-blown active reference.  grab_super()
150  *      is called with sb_lock held and drops it.  Returns 1 in case of
151  *      success, 0 if we had failed (superblock contents was already dead or
152  *      dying when grab_super() had been called).
153  */
154 static int grab_super(struct super_block *s)
155 {
156         s->s_count++;
157         spin_unlock(&sb_lock);
158         down_write(&s->s_umount);
159         if (s->s_root) {
160                 spin_lock(&sb_lock);
161                 if (s->s_count > S_BIAS) {
162                         atomic_inc(&s->s_active);
163                         s->s_count--;
164                         spin_unlock(&sb_lock);
165                         return 1;
166                 }
167                 spin_unlock(&sb_lock);
168         }
169         up_write(&s->s_umount);
170         put_super(s);
171         yield();
172         return 0;
173 }
174
175 /**
176  *      generic_shutdown_super  -       common helper for ->kill_sb()
177  *      @sb: superblock to kill
178  *
179  *      generic_shutdown_super() does all fs-independent work on superblock
180  *      shutdown.  Typical ->kill_sb() should pick all fs-specific objects
181  *      that need destruction out of superblock, call generic_shutdown_super()
182  *      and release aforementioned objects.  Note: dentries and inodes _are_
183  *      taken care of and do not need specific handling.
184  */
185 void generic_shutdown_super(struct super_block *sb)
186 {
187         struct dentry *root = sb->s_root;
188         struct super_operations *sop = sb->s_op;
189
190         if (root) {
191                 sb->s_root = NULL;
192                 shrink_dcache_parent(root);
193                 shrink_dcache_anon(&sb->s_anon);
194                 dput(root);
195                 fsync_super(sb);
196                 lock_super(sb);
197                 lock_kernel();
198                 sb->s_flags &= ~MS_ACTIVE;
199                 /* bad name - it should be evict_inodes() */
200                 invalidate_inodes(sb);
201
202                 if (sop->write_super && sb->s_dirt)
203                         sop->write_super(sb);
204                 if (sop->put_super)
205                         sop->put_super(sb);
206
207                 /* Forget any remaining inodes */
208                 if (invalidate_inodes(sb)) {
209                         printk("VFS: Busy inodes after unmount. "
210                            "Self-destruct in 5 seconds.  Have a nice day...\n");
211                 }
212
213                 unlock_kernel();
214                 unlock_super(sb);
215         }
216         spin_lock(&sb_lock);
217         list_del(&sb->s_list);
218         list_del(&sb->s_instances);
219         spin_unlock(&sb_lock);
220         up_write(&sb->s_umount);
221 }
222
223 EXPORT_SYMBOL(generic_shutdown_super);
224
225 /**
226  *      sget    -       find or create a superblock
227  *      @type:  filesystem type superblock should belong to
228  *      @test:  comparison callback
229  *      @set:   setup callback
230  *      @data:  argument to each of them
231  */
232 struct super_block *sget(struct file_system_type *type,
233                         int (*test)(struct super_block *,void *),
234                         int (*set)(struct super_block *,void *),
235                         void *data)
236 {
237         struct super_block *s = NULL;
238         struct list_head *p;
239         int err;
240
241 retry:
242         spin_lock(&sb_lock);
243         if (test) list_for_each(p, &type->fs_supers) {
244                 struct super_block *old;
245                 old = list_entry(p, struct super_block, s_instances);
246                 if (!test(old, data))
247                         continue;
248                 if (!grab_super(old))
249                         goto retry;
250                 if (s)
251                         destroy_super(s);
252                 return old;
253         }
254         if (!s) {
255                 spin_unlock(&sb_lock);
256                 s = alloc_super();
257                 if (!s)
258                         return ERR_PTR(-ENOMEM);
259                 goto retry;
260         }
261                 
262         err = set(s, data);
263         if (err) {
264                 spin_unlock(&sb_lock);
265                 destroy_super(s);
266                 return ERR_PTR(err);
267         }
268         s->s_type = type;
269         list_add(&s->s_list, super_blocks.prev);
270         list_add(&s->s_instances, &type->fs_supers);
271         spin_unlock(&sb_lock);
272         get_filesystem(type);
273         return s;
274 }
275
276 EXPORT_SYMBOL(sget);
277
278 void drop_super(struct super_block *sb)
279 {
280         up_read(&sb->s_umount);
281         put_super(sb);
282 }
283
284 EXPORT_SYMBOL(drop_super);
285
286 static inline void write_super(struct super_block *sb)
287 {
288         lock_super(sb);
289         if (sb->s_root && sb->s_dirt)
290                 if (sb->s_op->write_super)
291                         sb->s_op->write_super(sb);
292         unlock_super(sb);
293 }
294
295 /*
296  * Note: check the dirty flag before waiting, so we don't
297  * hold up the sync while mounting a device. (The newly
298  * mounted device won't need syncing.)
299  */
300 void sync_supers(void)
301 {
302         struct super_block * sb;
303 restart:
304         spin_lock(&sb_lock);
305         sb = sb_entry(super_blocks.next);
306         while (sb != sb_entry(&super_blocks))
307                 if (sb->s_dirt) {
308                         sb->s_count++;
309                         spin_unlock(&sb_lock);
310                         down_read(&sb->s_umount);
311                         write_super(sb);
312                         drop_super(sb);
313                         goto restart;
314                 } else
315                         sb = sb_entry(sb->s_list.next);
316         spin_unlock(&sb_lock);
317 }
318
319 /*
320  * Call the ->sync_fs super_op against all filesytems which are r/w and
321  * which implement it.
322  *
323  * This operation is careful to avoid the livelock which could easily happen
324  * if two or more filesystems are being continuously dirtied.  s_need_sync_fs
325  * is used only here.  We set it against all filesystems and then clear it as
326  * we sync them.  So redirtied filesystems are skipped.
327  *
328  * But if process A is currently running sync_filesytems and then process B
329  * calls sync_filesystems as well, process B will set all the s_need_sync_fs
330  * flags again, which will cause process A to resync everything.  Fix that with
331  * a local mutex.
332  *
333  * (Fabian) Avoid sync_fs with clean fs & wait mode 0
334  */
335 void sync_filesystems(int wait)
336 {
337         struct super_block *sb;
338         static DECLARE_MUTEX(mutex);
339
340         down(&mutex);           /* Could be down_interruptible */
341         spin_lock(&sb_lock);
342         for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
343                         sb = sb_entry(sb->s_list.next)) {
344                 if (!sb->s_op->sync_fs)
345                         continue;
346                 if (sb->s_flags & MS_RDONLY)
347                         continue;
348                 sb->s_need_sync_fs = 1;
349         }
350         spin_unlock(&sb_lock);
351
352 restart:
353         spin_lock(&sb_lock);
354         for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
355                         sb = sb_entry(sb->s_list.next)) {
356                 if (!sb->s_need_sync_fs)
357                         continue;
358                 sb->s_need_sync_fs = 0;
359                 if (sb->s_flags & MS_RDONLY)
360                         continue;       /* hm.  Was remounted r/o meanwhile */
361                 sb->s_count++;
362                 spin_unlock(&sb_lock);
363                 down_read(&sb->s_umount);
364                 if (sb->s_root && (wait || sb->s_dirt))
365                         sb->s_op->sync_fs(sb, wait);
366                 drop_super(sb);
367                 goto restart;
368         }
369         spin_unlock(&sb_lock);
370         up(&mutex);
371 }
372
373 /**
374  *      get_super - get the superblock of a device
375  *      @bdev: device to get the superblock for
376  *      
377  *      Scans the superblock list and finds the superblock of the file system
378  *      mounted on the device given. %NULL is returned if no match is found.
379  */
380
381 struct super_block * get_super(struct block_device *bdev)
382 {
383         struct list_head *p;
384         if (!bdev)
385                 return NULL;
386 rescan:
387         spin_lock(&sb_lock);
388         list_for_each(p, &super_blocks) {
389                 struct super_block *s = sb_entry(p);
390                 if (s->s_bdev == bdev) {
391                         s->s_count++;
392                         spin_unlock(&sb_lock);
393                         down_read(&s->s_umount);
394                         if (s->s_root)
395                                 return s;
396                         drop_super(s);
397                         goto rescan;
398                 }
399         }
400         spin_unlock(&sb_lock);
401         return NULL;
402 }
403
404 EXPORT_SYMBOL(get_super);
405  
406 struct super_block * user_get_super(dev_t dev)
407 {
408         struct list_head *p;
409
410 rescan:
411         spin_lock(&sb_lock);
412         list_for_each(p, &super_blocks) {
413                 struct super_block *s = sb_entry(p);
414                 if (s->s_dev ==  dev) {
415                         s->s_count++;
416                         spin_unlock(&sb_lock);
417                         down_read(&s->s_umount);
418                         if (s->s_root)
419                                 return s;
420                         drop_super(s);
421                         goto rescan;
422                 }
423         }
424         spin_unlock(&sb_lock);
425         return NULL;
426 }
427
428 EXPORT_SYMBOL(user_get_super);
429
430 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
431 {
432         struct super_block *s;
433         struct ustat tmp;
434         struct kstatfs sbuf;
435         int err = -EINVAL;
436
437         s = user_get_super(new_decode_dev(dev));
438         if (s == NULL)
439                 goto out;
440         err = vfs_statfs(s, &sbuf);
441         drop_super(s);
442         if (err)
443                 goto out;
444
445         memset(&tmp,0,sizeof(struct ustat));
446         tmp.f_tfree = sbuf.f_bfree;
447         tmp.f_tinode = sbuf.f_ffree;
448
449         err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
450 out:
451         return err;
452 }
453
454 /**
455  *      mark_files_ro
456  *      @sb: superblock in question
457  *
458  *      All files are marked read/only.  We don't care about pending
459  *      delete files so this should be used in 'force' mode only
460  */
461
462 static void mark_files_ro(struct super_block *sb)
463 {
464         struct file *f;
465
466         file_list_lock();
467         list_for_each_entry(f, &sb->s_files, f_list) {
468                 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
469                         f->f_mode &= ~FMODE_WRITE;
470         }
471         file_list_unlock();
472 }
473
474 /**
475  *      do_remount_sb - asks filesystem to change mount options.
476  *      @sb:    superblock in question
477  *      @flags: numeric part of options
478  *      @data:  the rest of options
479  *      @force: whether or not to force the change
480  *
481  *      Alters the mount options of a mounted file system.
482  */
483 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
484 {
485         int retval;
486         
487         if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
488                 return -EACCES;
489         if (flags & MS_RDONLY)
490                 acct_auto_close(sb);
491         shrink_dcache_sb(sb);
492         fsync_super(sb);
493
494         /* If we are remounting RDONLY and current sb is read/write,
495            make sure there are no rw files opened */
496         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
497                 if (force)
498                         mark_files_ro(sb);
499                 else if (!fs_may_remount_ro(sb))
500                         return -EBUSY;
501         }
502
503         if (sb->s_op->remount_fs) {
504                 lock_super(sb);
505                 retval = sb->s_op->remount_fs(sb, &flags, data);
506                 unlock_super(sb);
507                 if (retval)
508                         return retval;
509         }
510         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
511         return 0;
512 }
513
514 static void do_emergency_remount(unsigned long foo)
515 {
516         struct super_block *sb;
517
518         spin_lock(&sb_lock);
519         list_for_each_entry(sb, &super_blocks, s_list) {
520                 sb->s_count++;
521                 spin_unlock(&sb_lock);
522                 down_read(&sb->s_umount);
523                 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
524                         /*
525                          * ->remount_fs needs lock_kernel().
526                          *
527                          * What lock protects sb->s_flags??
528                          */
529                         lock_kernel();
530                         do_remount_sb(sb, MS_RDONLY, NULL, 1);
531                         unlock_kernel();
532                 }
533                 drop_super(sb);
534                 spin_lock(&sb_lock);
535         }
536         spin_unlock(&sb_lock);
537         printk("Emergency Remount complete\n");
538 }
539
540 void emergency_remount(void)
541 {
542         pdflush_operation(do_emergency_remount, 0);
543 }
544
545 /*
546  * Unnamed block devices are dummy devices used by virtual
547  * filesystems which don't use real block-devices.  -- jrs
548  */
549
550 static struct idr unnamed_dev_idr;
551 static spinlock_t unnamed_dev_lock = SPIN_LOCK_UNLOCKED;/* protects the above */
552
553 int set_anon_super(struct super_block *s, void *data)
554 {
555         int dev;
556
557         spin_lock(&unnamed_dev_lock);
558         if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0) {
559                 spin_unlock(&unnamed_dev_lock);
560                 return -ENOMEM;
561         }
562         dev = idr_get_new(&unnamed_dev_idr, NULL);
563         spin_unlock(&unnamed_dev_lock);
564
565         if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
566                 spin_lock(&unnamed_dev_lock);
567                 idr_remove(&unnamed_dev_idr, dev);
568                 spin_unlock(&unnamed_dev_lock);
569                 return -EMFILE;
570         }
571         s->s_dev = MKDEV(0, dev & MINORMASK);
572         return 0;
573 }
574
575 EXPORT_SYMBOL(set_anon_super);
576
577 void kill_anon_super(struct super_block *sb)
578 {
579         int slot = MINOR(sb->s_dev);
580
581         generic_shutdown_super(sb);
582         spin_lock(&unnamed_dev_lock);
583         idr_remove(&unnamed_dev_idr, slot);
584         spin_unlock(&unnamed_dev_lock);
585 }
586
587 EXPORT_SYMBOL(kill_anon_super);
588
589 void __init unnamed_dev_init(void)
590 {
591         idr_init(&unnamed_dev_idr);
592 }
593
594 void kill_litter_super(struct super_block *sb)
595 {
596         if (sb->s_root)
597                 d_genocide(sb->s_root);
598         kill_anon_super(sb);
599 }
600
601 EXPORT_SYMBOL(kill_litter_super);
602
603 static int set_bdev_super(struct super_block *s, void *data)
604 {
605         s->s_bdev = data;
606         s->s_dev = s->s_bdev->bd_dev;
607         return 0;
608 }
609
610 static int test_bdev_super(struct super_block *s, void *data)
611 {
612         return (void *)s->s_bdev == data;
613 }
614
615 struct super_block *get_sb_bdev(struct file_system_type *fs_type,
616         int flags, const char *dev_name, void *data,
617         int (*fill_super)(struct super_block *, void *, int))
618 {
619         struct block_device *bdev;
620         struct super_block *s;
621         int error = 0;
622
623         bdev = open_bdev_excl(dev_name, flags, fs_type);
624         if (IS_ERR(bdev))
625                 return (struct super_block *)bdev;
626
627         /*
628          * once the super is inserted into the list by sget, s_umount
629          * will protect the lockfs code from trying to start a snapshot
630          * while we are mounting
631          */
632         down(&bdev->bd_mount_sem);
633         s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
634         up(&bdev->bd_mount_sem);
635         if (IS_ERR(s))
636                 goto out;
637
638         if (s->s_root) {
639                 if ((flags ^ s->s_flags) & MS_RDONLY) {
640                         up_write(&s->s_umount);
641                         deactivate_super(s);
642                         s = ERR_PTR(-EBUSY);
643                 }
644                 goto out;
645         } else {
646                 char b[BDEVNAME_SIZE];
647
648                 s->s_flags = flags;
649                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
650                 s->s_old_blocksize = block_size(bdev);
651                 sb_set_blocksize(s, s->s_old_blocksize);
652                 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
653                 if (error) {
654                         up_write(&s->s_umount);
655                         deactivate_super(s);
656                         s = ERR_PTR(error);
657                 } else
658                         s->s_flags |= MS_ACTIVE;
659         }
660
661         return s;
662
663 out:
664         close_bdev_excl(bdev);
665         return s;
666 }
667
668 EXPORT_SYMBOL(get_sb_bdev);
669
670 void kill_block_super(struct super_block *sb)
671 {
672         struct block_device *bdev = sb->s_bdev;
673         generic_shutdown_super(sb);
674         set_blocksize(bdev, sb->s_old_blocksize);
675         close_bdev_excl(bdev);
676 }
677
678 EXPORT_SYMBOL(kill_block_super);
679
680 struct super_block *get_sb_nodev(struct file_system_type *fs_type,
681         int flags, void *data,
682         int (*fill_super)(struct super_block *, void *, int))
683 {
684         int error;
685         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
686
687         if (IS_ERR(s))
688                 return s;
689
690         s->s_flags = flags;
691
692         error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
693         if (error) {
694                 up_write(&s->s_umount);
695                 deactivate_super(s);
696                 return ERR_PTR(error);
697         }
698         s->s_flags |= MS_ACTIVE;
699         return s;
700 }
701
702 EXPORT_SYMBOL(get_sb_nodev);
703
704 static int compare_single(struct super_block *s, void *p)
705 {
706         return 1;
707 }
708
709 struct super_block *get_sb_single(struct file_system_type *fs_type,
710         int flags, void *data,
711         int (*fill_super)(struct super_block *, void *, int))
712 {
713         struct super_block *s;
714         int error;
715
716         s = sget(fs_type, compare_single, set_anon_super, NULL);
717         if (IS_ERR(s))
718                 return s;
719         if (!s->s_root) {
720                 s->s_flags = flags;
721                 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
722                 if (error) {
723                         up_write(&s->s_umount);
724                         deactivate_super(s);
725                         return ERR_PTR(error);
726                 }
727                 s->s_flags |= MS_ACTIVE;
728         }
729         do_remount_sb(s, flags, data, 0);
730         return s;
731 }
732
733 EXPORT_SYMBOL(get_sb_single);
734
735 struct vfsmount *
736 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
737 {
738         struct file_system_type *type = get_fs_type(fstype);
739         struct super_block *sb = ERR_PTR(-ENOMEM);
740         struct vfsmount *mnt;
741         int error;
742         char *secdata = NULL;
743
744         if (!type)
745                 return ERR_PTR(-ENODEV);
746
747         mnt = alloc_vfsmnt(name);
748         if (!mnt)
749                 goto out;
750
751         if (data) {
752                 secdata = alloc_secdata();
753                 if (!secdata) {
754                         sb = ERR_PTR(-ENOMEM);
755                         goto out_mnt;
756                 }
757
758                 error = security_sb_copy_data(type, data, secdata);
759                 if (error) {
760                         sb = ERR_PTR(error);
761                         goto out_free_secdata;
762                 }
763         }
764
765         sb = type->get_sb(type, flags, name, data);
766         if (IS_ERR(sb))
767                 goto out_free_secdata;
768         error = security_sb_kern_mount(sb, secdata);
769         if (error)
770                 goto out_sb;
771         mnt->mnt_sb = sb;
772         mnt->mnt_root = dget(sb->s_root);
773         mnt->mnt_mountpoint = sb->s_root;
774         mnt->mnt_parent = mnt;
775         up_write(&sb->s_umount);
776         put_filesystem(type);
777         return mnt;
778 out_sb:
779         up_write(&sb->s_umount);
780         deactivate_super(sb);
781         sb = ERR_PTR(error);
782 out_free_secdata:
783         free_secdata(secdata);
784 out_mnt:
785         free_vfsmnt(mnt);
786 out:
787         put_filesystem(type);
788         return (struct vfsmount *)sb;
789 }
790
791 struct vfsmount *kern_mount(struct file_system_type *type)
792 {
793         return do_kern_mount(type->name, 0, type->name, NULL);
794 }
795
796 EXPORT_SYMBOL(kern_mount);