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