89afca5055ab513db1bf7ae1821e282a5be25836
[GitHub/mt8127/android_kernel_alcatel_ttab.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/acct.h>
26 #include <linux/blkdev.h>
27 #include <linux/quotaops.h>
28 #include <linux/mount.h>
29 #include <linux/security.h>
30 #include <linux/writeback.h> /* for the emergency remount stuff */
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <linux/backing-dev.h>
34 #include "internal.h"
35
36
37 LIST_HEAD(super_blocks);
38 DEFINE_SPINLOCK(sb_lock);
39
40 /**
41 * alloc_super - create new superblock
42 * @type: filesystem type superblock should belong to
43 *
44 * Allocates and initializes a new &struct super_block. alloc_super()
45 * returns a pointer new superblock or %NULL if allocation had failed.
46 */
47 static struct super_block *alloc_super(struct file_system_type *type)
48 {
49 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
50 static const struct super_operations default_op;
51
52 if (s) {
53 if (security_sb_alloc(s)) {
54 kfree(s);
55 s = NULL;
56 goto out;
57 }
58 INIT_LIST_HEAD(&s->s_files);
59 INIT_LIST_HEAD(&s->s_instances);
60 INIT_HLIST_HEAD(&s->s_anon);
61 INIT_LIST_HEAD(&s->s_inodes);
62 INIT_LIST_HEAD(&s->s_dentry_lru);
63 init_rwsem(&s->s_umount);
64 mutex_init(&s->s_lock);
65 lockdep_set_class(&s->s_umount, &type->s_umount_key);
66 /*
67 * The locking rules for s_lock are up to the
68 * filesystem. For example ext3fs has different
69 * lock ordering than usbfs:
70 */
71 lockdep_set_class(&s->s_lock, &type->s_lock_key);
72 /*
73 * sget() can have s_umount recursion.
74 *
75 * When it cannot find a suitable sb, it allocates a new
76 * one (this one), and tries again to find a suitable old
77 * one.
78 *
79 * In case that succeeds, it will acquire the s_umount
80 * lock of the old one. Since these are clearly distrinct
81 * locks, and this object isn't exposed yet, there's no
82 * risk of deadlocks.
83 *
84 * Annotate this by putting this lock in a different
85 * subclass.
86 */
87 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
88 s->s_count = 1;
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->s_subtype);
115 kfree(s->s_options);
116 kfree(s);
117 }
118
119 /* Superblock refcounting */
120
121 /*
122 * Drop a superblock's refcount. The caller must hold sb_lock.
123 */
124 void __put_super(struct super_block *sb)
125 {
126 if (!--sb->s_count) {
127 list_del_init(&sb->s_list);
128 destroy_super(sb);
129 }
130 }
131
132 /**
133 * put_super - drop a temporary reference to superblock
134 * @sb: superblock in question
135 *
136 * Drops a temporary reference, frees superblock if there's no
137 * references left.
138 */
139 void put_super(struct super_block *sb)
140 {
141 spin_lock(&sb_lock);
142 __put_super(sb);
143 spin_unlock(&sb_lock);
144 }
145
146
147 /**
148 * deactivate_locked_super - drop an active reference to superblock
149 * @s: superblock to deactivate
150 *
151 * Drops an active reference to superblock, converting it into a temprory
152 * one if there is no other active references left. In that case we
153 * tell fs driver to shut it down and drop the temporary reference we
154 * had just acquired.
155 *
156 * Caller holds exclusive lock on superblock; that lock is released.
157 */
158 void deactivate_locked_super(struct super_block *s)
159 {
160 struct file_system_type *fs = s->s_type;
161 if (atomic_dec_and_test(&s->s_active)) {
162 vfs_dq_off(s, 0);
163 fs->kill_sb(s);
164 put_filesystem(fs);
165 put_super(s);
166 } else {
167 up_write(&s->s_umount);
168 }
169 }
170
171 EXPORT_SYMBOL(deactivate_locked_super);
172
173 /**
174 * deactivate_super - drop an active reference to superblock
175 * @s: superblock to deactivate
176 *
177 * Variant of deactivate_locked_super(), except that superblock is *not*
178 * locked by caller. If we are going to drop the final active reference,
179 * lock will be acquired prior to that.
180 */
181 void deactivate_super(struct super_block *s)
182 {
183 if (!atomic_add_unless(&s->s_active, -1, 1)) {
184 down_write(&s->s_umount);
185 deactivate_locked_super(s);
186 }
187 }
188
189 EXPORT_SYMBOL(deactivate_super);
190
191 /**
192 * grab_super - acquire an active reference
193 * @s: reference we are trying to make active
194 *
195 * Tries to acquire an active reference. grab_super() is used when we
196 * had just found a superblock in super_blocks or fs_type->fs_supers
197 * and want to turn it into a full-blown active reference. grab_super()
198 * is called with sb_lock held and drops it. Returns 1 in case of
199 * success, 0 if we had failed (superblock contents was already dead or
200 * dying when grab_super() had been called).
201 */
202 static int grab_super(struct super_block *s) __releases(sb_lock)
203 {
204 if (atomic_inc_not_zero(&s->s_active)) {
205 spin_unlock(&sb_lock);
206 return 1;
207 }
208 /* it's going away */
209 s->s_count++;
210 spin_unlock(&sb_lock);
211 /* wait for it to die */
212 down_write(&s->s_umount);
213 up_write(&s->s_umount);
214 put_super(s);
215 return 0;
216 }
217
218 /*
219 * Superblock locking. We really ought to get rid of these two.
220 */
221 void lock_super(struct super_block * sb)
222 {
223 get_fs_excl();
224 mutex_lock(&sb->s_lock);
225 }
226
227 void unlock_super(struct super_block * sb)
228 {
229 put_fs_excl();
230 mutex_unlock(&sb->s_lock);
231 }
232
233 EXPORT_SYMBOL(lock_super);
234 EXPORT_SYMBOL(unlock_super);
235
236 /**
237 * generic_shutdown_super - common helper for ->kill_sb()
238 * @sb: superblock to kill
239 *
240 * generic_shutdown_super() does all fs-independent work on superblock
241 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
242 * that need destruction out of superblock, call generic_shutdown_super()
243 * and release aforementioned objects. Note: dentries and inodes _are_
244 * taken care of and do not need specific handling.
245 *
246 * Upon calling this function, the filesystem may no longer alter or
247 * rearrange the set of dentries belonging to this super_block, nor may it
248 * change the attachments of dentries to inodes.
249 */
250 void generic_shutdown_super(struct super_block *sb)
251 {
252 const struct super_operations *sop = sb->s_op;
253
254
255 if (sb->s_root) {
256 shrink_dcache_for_umount(sb);
257 sync_filesystem(sb);
258 get_fs_excl();
259 sb->s_flags &= ~MS_ACTIVE;
260
261 /* bad name - it should be evict_inodes() */
262 invalidate_inodes(sb);
263
264 if (sop->put_super)
265 sop->put_super(sb);
266
267 /* Forget any remaining inodes */
268 if (invalidate_inodes(sb)) {
269 printk("VFS: Busy inodes after unmount of %s. "
270 "Self-destruct in 5 seconds. Have a nice day...\n",
271 sb->s_id);
272 }
273 put_fs_excl();
274 }
275 spin_lock(&sb_lock);
276 /* should be initialized for __put_super_and_need_restart() */
277 list_del_init(&sb->s_instances);
278 spin_unlock(&sb_lock);
279 up_write(&sb->s_umount);
280 }
281
282 EXPORT_SYMBOL(generic_shutdown_super);
283
284 /**
285 * sget - find or create a superblock
286 * @type: filesystem type superblock should belong to
287 * @test: comparison callback
288 * @set: setup callback
289 * @data: argument to each of them
290 */
291 struct super_block *sget(struct file_system_type *type,
292 int (*test)(struct super_block *,void *),
293 int (*set)(struct super_block *,void *),
294 void *data)
295 {
296 struct super_block *s = NULL;
297 struct super_block *old;
298 int err;
299
300 retry:
301 spin_lock(&sb_lock);
302 if (test) {
303 list_for_each_entry(old, &type->fs_supers, s_instances) {
304 if (!test(old, data))
305 continue;
306 if (!grab_super(old))
307 goto retry;
308 if (s) {
309 up_write(&s->s_umount);
310 destroy_super(s);
311 }
312 down_write(&old->s_umount);
313 return old;
314 }
315 }
316 if (!s) {
317 spin_unlock(&sb_lock);
318 s = alloc_super(type);
319 if (!s)
320 return ERR_PTR(-ENOMEM);
321 goto retry;
322 }
323
324 err = set(s, data);
325 if (err) {
326 spin_unlock(&sb_lock);
327 up_write(&s->s_umount);
328 destroy_super(s);
329 return ERR_PTR(err);
330 }
331 s->s_type = type;
332 strlcpy(s->s_id, type->name, sizeof(s->s_id));
333 list_add_tail(&s->s_list, &super_blocks);
334 list_add(&s->s_instances, &type->fs_supers);
335 spin_unlock(&sb_lock);
336 get_filesystem(type);
337 return s;
338 }
339
340 EXPORT_SYMBOL(sget);
341
342 void drop_super(struct super_block *sb)
343 {
344 up_read(&sb->s_umount);
345 put_super(sb);
346 }
347
348 EXPORT_SYMBOL(drop_super);
349
350 /**
351 * sync_supers - helper for periodic superblock writeback
352 *
353 * Call the write_super method if present on all dirty superblocks in
354 * the system. This is for the periodic writeback used by most older
355 * filesystems. For data integrity superblock writeback use
356 * sync_filesystems() instead.
357 *
358 * Note: check the dirty flag before waiting, so we don't
359 * hold up the sync while mounting a device. (The newly
360 * mounted device won't need syncing.)
361 */
362 void sync_supers(void)
363 {
364 struct super_block *sb, *n;
365
366 spin_lock(&sb_lock);
367 list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
368 if (list_empty(&sb->s_instances))
369 continue;
370 if (sb->s_op->write_super && sb->s_dirt) {
371 sb->s_count++;
372 spin_unlock(&sb_lock);
373
374 down_read(&sb->s_umount);
375 if (sb->s_root && sb->s_dirt)
376 sb->s_op->write_super(sb);
377 up_read(&sb->s_umount);
378
379 spin_lock(&sb_lock);
380 __put_super(sb);
381 }
382 }
383 spin_unlock(&sb_lock);
384 }
385
386 /**
387 * iterate_supers - call function for all active superblocks
388 * @f: function to call
389 * @arg: argument to pass to it
390 *
391 * Scans the superblock list and calls given function, passing it
392 * locked superblock and given argument.
393 */
394 void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
395 {
396 struct super_block *sb, *n;
397
398 spin_lock(&sb_lock);
399 list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
400 if (list_empty(&sb->s_instances))
401 continue;
402 sb->s_count++;
403 spin_unlock(&sb_lock);
404
405 down_read(&sb->s_umount);
406 if (sb->s_root)
407 f(sb, arg);
408 up_read(&sb->s_umount);
409
410 spin_lock(&sb_lock);
411 __put_super(sb);
412 }
413 spin_unlock(&sb_lock);
414 }
415
416 /**
417 * get_super - get the superblock of a device
418 * @bdev: device to get the superblock for
419 *
420 * Scans the superblock list and finds the superblock of the file system
421 * mounted on the device given. %NULL is returned if no match is found.
422 */
423
424 struct super_block *get_super(struct block_device *bdev)
425 {
426 struct super_block *sb;
427
428 if (!bdev)
429 return NULL;
430
431 spin_lock(&sb_lock);
432 rescan:
433 list_for_each_entry(sb, &super_blocks, s_list) {
434 if (list_empty(&sb->s_instances))
435 continue;
436 if (sb->s_bdev == bdev) {
437 sb->s_count++;
438 spin_unlock(&sb_lock);
439 down_read(&sb->s_umount);
440 /* still alive? */
441 if (sb->s_root)
442 return sb;
443 up_read(&sb->s_umount);
444 /* nope, got unmounted */
445 spin_lock(&sb_lock);
446 __put_super(sb);
447 goto rescan;
448 }
449 }
450 spin_unlock(&sb_lock);
451 return NULL;
452 }
453
454 EXPORT_SYMBOL(get_super);
455
456 /**
457 * get_active_super - get an active reference to the superblock of a device
458 * @bdev: device to get the superblock for
459 *
460 * Scans the superblock list and finds the superblock of the file system
461 * mounted on the device given. Returns the superblock with an active
462 * reference or %NULL if none was found.
463 */
464 struct super_block *get_active_super(struct block_device *bdev)
465 {
466 struct super_block *sb;
467
468 if (!bdev)
469 return NULL;
470
471 restart:
472 spin_lock(&sb_lock);
473 list_for_each_entry(sb, &super_blocks, s_list) {
474 if (list_empty(&sb->s_instances))
475 continue;
476 if (sb->s_bdev == bdev) {
477 if (grab_super(sb)) /* drops sb_lock */
478 return sb;
479 else
480 goto restart;
481 }
482 }
483 spin_unlock(&sb_lock);
484 return NULL;
485 }
486
487 struct super_block *user_get_super(dev_t dev)
488 {
489 struct super_block *sb;
490
491 spin_lock(&sb_lock);
492 rescan:
493 list_for_each_entry(sb, &super_blocks, s_list) {
494 if (list_empty(&sb->s_instances))
495 continue;
496 if (sb->s_dev == dev) {
497 sb->s_count++;
498 spin_unlock(&sb_lock);
499 down_read(&sb->s_umount);
500 /* still alive? */
501 if (sb->s_root)
502 return sb;
503 up_read(&sb->s_umount);
504 /* nope, got unmounted */
505 spin_lock(&sb_lock);
506 __put_super(sb);
507 goto rescan;
508 }
509 }
510 spin_unlock(&sb_lock);
511 return NULL;
512 }
513
514 /**
515 * do_remount_sb - asks filesystem to change mount options.
516 * @sb: superblock in question
517 * @flags: numeric part of options
518 * @data: the rest of options
519 * @force: whether or not to force the change
520 *
521 * Alters the mount options of a mounted file system.
522 */
523 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
524 {
525 int retval;
526 int remount_rw, remount_ro;
527
528 if (sb->s_frozen != SB_UNFROZEN)
529 return -EBUSY;
530
531 #ifdef CONFIG_BLOCK
532 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
533 return -EACCES;
534 #endif
535
536 if (flags & MS_RDONLY)
537 acct_auto_close(sb);
538 shrink_dcache_sb(sb);
539 sync_filesystem(sb);
540
541 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
542 remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
543
544 /* If we are remounting RDONLY and current sb is read/write,
545 make sure there are no rw files opened */
546 if (remount_ro) {
547 if (force)
548 mark_files_ro(sb);
549 else if (!fs_may_remount_ro(sb))
550 return -EBUSY;
551 retval = vfs_dq_off(sb, 1);
552 if (retval < 0 && retval != -ENOSYS)
553 return -EBUSY;
554 }
555
556 if (sb->s_op->remount_fs) {
557 retval = sb->s_op->remount_fs(sb, &flags, data);
558 if (retval)
559 return retval;
560 }
561 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
562 if (remount_rw)
563 vfs_dq_quota_on_remount(sb);
564 /*
565 * Some filesystems modify their metadata via some other path than the
566 * bdev buffer cache (eg. use a private mapping, or directories in
567 * pagecache, etc). Also file data modifications go via their own
568 * mappings. So If we try to mount readonly then copy the filesystem
569 * from bdev, we could get stale data, so invalidate it to give a best
570 * effort at coherency.
571 */
572 if (remount_ro && sb->s_bdev)
573 invalidate_bdev(sb->s_bdev);
574 return 0;
575 }
576
577 static void do_emergency_remount(struct work_struct *work)
578 {
579 struct super_block *sb, *n;
580
581 spin_lock(&sb_lock);
582 list_for_each_entry_safe(sb, n, &super_blocks, s_list) {
583 if (list_empty(&sb->s_instances))
584 continue;
585 sb->s_count++;
586 spin_unlock(&sb_lock);
587 down_write(&sb->s_umount);
588 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
589 /*
590 * What lock protects sb->s_flags??
591 */
592 do_remount_sb(sb, MS_RDONLY, NULL, 1);
593 }
594 up_write(&sb->s_umount);
595 spin_lock(&sb_lock);
596 __put_super(sb);
597 }
598 spin_unlock(&sb_lock);
599 kfree(work);
600 printk("Emergency Remount complete\n");
601 }
602
603 void emergency_remount(void)
604 {
605 struct work_struct *work;
606
607 work = kmalloc(sizeof(*work), GFP_ATOMIC);
608 if (work) {
609 INIT_WORK(work, do_emergency_remount);
610 schedule_work(work);
611 }
612 }
613
614 /*
615 * Unnamed block devices are dummy devices used by virtual
616 * filesystems which don't use real block-devices. -- jrs
617 */
618
619 static DEFINE_IDA(unnamed_dev_ida);
620 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
621 static int unnamed_dev_start = 0; /* don't bother trying below it */
622
623 int set_anon_super(struct super_block *s, void *data)
624 {
625 int dev;
626 int error;
627
628 retry:
629 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
630 return -ENOMEM;
631 spin_lock(&unnamed_dev_lock);
632 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
633 if (!error)
634 unnamed_dev_start = dev + 1;
635 spin_unlock(&unnamed_dev_lock);
636 if (error == -EAGAIN)
637 /* We raced and lost with another CPU. */
638 goto retry;
639 else if (error)
640 return -EAGAIN;
641
642 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
643 spin_lock(&unnamed_dev_lock);
644 ida_remove(&unnamed_dev_ida, dev);
645 if (unnamed_dev_start > dev)
646 unnamed_dev_start = dev;
647 spin_unlock(&unnamed_dev_lock);
648 return -EMFILE;
649 }
650 s->s_dev = MKDEV(0, dev & MINORMASK);
651 s->s_bdi = &noop_backing_dev_info;
652 return 0;
653 }
654
655 EXPORT_SYMBOL(set_anon_super);
656
657 void kill_anon_super(struct super_block *sb)
658 {
659 int slot = MINOR(sb->s_dev);
660
661 generic_shutdown_super(sb);
662 spin_lock(&unnamed_dev_lock);
663 ida_remove(&unnamed_dev_ida, slot);
664 if (slot < unnamed_dev_start)
665 unnamed_dev_start = slot;
666 spin_unlock(&unnamed_dev_lock);
667 }
668
669 EXPORT_SYMBOL(kill_anon_super);
670
671 void kill_litter_super(struct super_block *sb)
672 {
673 if (sb->s_root)
674 d_genocide(sb->s_root);
675 kill_anon_super(sb);
676 }
677
678 EXPORT_SYMBOL(kill_litter_super);
679
680 static int ns_test_super(struct super_block *sb, void *data)
681 {
682 return sb->s_fs_info == data;
683 }
684
685 static int ns_set_super(struct super_block *sb, void *data)
686 {
687 sb->s_fs_info = data;
688 return set_anon_super(sb, NULL);
689 }
690
691 int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
692 int (*fill_super)(struct super_block *, void *, int),
693 struct vfsmount *mnt)
694 {
695 struct super_block *sb;
696
697 sb = sget(fs_type, ns_test_super, ns_set_super, data);
698 if (IS_ERR(sb))
699 return PTR_ERR(sb);
700
701 if (!sb->s_root) {
702 int err;
703 sb->s_flags = flags;
704 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
705 if (err) {
706 deactivate_locked_super(sb);
707 return err;
708 }
709
710 sb->s_flags |= MS_ACTIVE;
711 }
712
713 simple_set_mnt(mnt, sb);
714 return 0;
715 }
716
717 EXPORT_SYMBOL(get_sb_ns);
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
725 /*
726 * We set the bdi here to the queue backing, file systems can
727 * overwrite this in ->fill_super()
728 */
729 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
730 return 0;
731 }
732
733 static int test_bdev_super(struct super_block *s, void *data)
734 {
735 return (void *)s->s_bdev == data;
736 }
737
738 int get_sb_bdev(struct file_system_type *fs_type,
739 int flags, const char *dev_name, void *data,
740 int (*fill_super)(struct super_block *, void *, int),
741 struct vfsmount *mnt)
742 {
743 struct block_device *bdev;
744 struct super_block *s;
745 fmode_t mode = FMODE_READ;
746 int error = 0;
747
748 if (!(flags & MS_RDONLY))
749 mode |= FMODE_WRITE;
750
751 bdev = open_bdev_exclusive(dev_name, mode, 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 mutex_lock(&bdev->bd_fsfreeze_mutex);
761 if (bdev->bd_fsfreeze_count > 0) {
762 mutex_unlock(&bdev->bd_fsfreeze_mutex);
763 error = -EBUSY;
764 goto error_bdev;
765 }
766 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
767 mutex_unlock(&bdev->bd_fsfreeze_mutex);
768 if (IS_ERR(s))
769 goto error_s;
770
771 if (s->s_root) {
772 if ((flags ^ s->s_flags) & MS_RDONLY) {
773 deactivate_locked_super(s);
774 error = -EBUSY;
775 goto error_bdev;
776 }
777
778 close_bdev_exclusive(bdev, mode);
779 } else {
780 char b[BDEVNAME_SIZE];
781
782 s->s_flags = flags;
783 s->s_mode = mode;
784 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
785 sb_set_blocksize(s, block_size(bdev));
786 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
787 if (error) {
788 deactivate_locked_super(s);
789 goto error;
790 }
791
792 s->s_flags |= MS_ACTIVE;
793 bdev->bd_super = s;
794 }
795
796 simple_set_mnt(mnt, s);
797 return 0;
798
799 error_s:
800 error = PTR_ERR(s);
801 error_bdev:
802 close_bdev_exclusive(bdev, mode);
803 error:
804 return error;
805 }
806
807 EXPORT_SYMBOL(get_sb_bdev);
808
809 void kill_block_super(struct super_block *sb)
810 {
811 struct block_device *bdev = sb->s_bdev;
812 fmode_t mode = sb->s_mode;
813
814 bdev->bd_super = NULL;
815 generic_shutdown_super(sb);
816 sync_blockdev(bdev);
817 close_bdev_exclusive(bdev, mode);
818 }
819
820 EXPORT_SYMBOL(kill_block_super);
821 #endif
822
823 int get_sb_nodev(struct file_system_type *fs_type,
824 int flags, void *data,
825 int (*fill_super)(struct super_block *, void *, int),
826 struct vfsmount *mnt)
827 {
828 int error;
829 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
830
831 if (IS_ERR(s))
832 return PTR_ERR(s);
833
834 s->s_flags = flags;
835
836 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
837 if (error) {
838 deactivate_locked_super(s);
839 return error;
840 }
841 s->s_flags |= MS_ACTIVE;
842 simple_set_mnt(mnt, s);
843 return 0;
844 }
845
846 EXPORT_SYMBOL(get_sb_nodev);
847
848 static int compare_single(struct super_block *s, void *p)
849 {
850 return 1;
851 }
852
853 int get_sb_single(struct file_system_type *fs_type,
854 int flags, void *data,
855 int (*fill_super)(struct super_block *, void *, int),
856 struct vfsmount *mnt)
857 {
858 struct super_block *s;
859 int error;
860
861 s = sget(fs_type, compare_single, set_anon_super, NULL);
862 if (IS_ERR(s))
863 return PTR_ERR(s);
864 if (!s->s_root) {
865 s->s_flags = flags;
866 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
867 if (error) {
868 deactivate_locked_super(s);
869 return error;
870 }
871 s->s_flags |= MS_ACTIVE;
872 } else {
873 do_remount_sb(s, flags, data, 0);
874 }
875 simple_set_mnt(mnt, s);
876 return 0;
877 }
878
879 EXPORT_SYMBOL(get_sb_single);
880
881 struct vfsmount *
882 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
883 {
884 struct vfsmount *mnt;
885 char *secdata = NULL;
886 int error;
887
888 if (!type)
889 return ERR_PTR(-ENODEV);
890
891 error = -ENOMEM;
892 mnt = alloc_vfsmnt(name);
893 if (!mnt)
894 goto out;
895
896 if (flags & MS_KERNMOUNT)
897 mnt->mnt_flags = MNT_INTERNAL;
898
899 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
900 secdata = alloc_secdata();
901 if (!secdata)
902 goto out_mnt;
903
904 error = security_sb_copy_data(data, secdata);
905 if (error)
906 goto out_free_secdata;
907 }
908
909 error = type->get_sb(type, flags, name, data, mnt);
910 if (error < 0)
911 goto out_free_secdata;
912 BUG_ON(!mnt->mnt_sb);
913 WARN_ON(!mnt->mnt_sb->s_bdi);
914
915 error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
916 if (error)
917 goto out_sb;
918
919 /*
920 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
921 * but s_maxbytes was an unsigned long long for many releases. Throw
922 * this warning for a little while to try and catch filesystems that
923 * violate this rule. This warning should be either removed or
924 * converted to a BUG() in 2.6.34.
925 */
926 WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
927 "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
928
929 mnt->mnt_mountpoint = mnt->mnt_root;
930 mnt->mnt_parent = mnt;
931 up_write(&mnt->mnt_sb->s_umount);
932 free_secdata(secdata);
933 return mnt;
934 out_sb:
935 dput(mnt->mnt_root);
936 deactivate_locked_super(mnt->mnt_sb);
937 out_free_secdata:
938 free_secdata(secdata);
939 out_mnt:
940 free_vfsmnt(mnt);
941 out:
942 return ERR_PTR(error);
943 }
944
945 EXPORT_SYMBOL_GPL(vfs_kern_mount);
946
947 /**
948 * freeze_super -- lock the filesystem and force it into a consistent state
949 * @super: the super to lock
950 *
951 * Syncs the super to make sure the filesystem is consistent and calls the fs's
952 * freeze_fs. Subsequent calls to this without first thawing the fs will return
953 * -EBUSY.
954 */
955 int freeze_super(struct super_block *sb)
956 {
957 int ret;
958
959 atomic_inc(&sb->s_active);
960 down_write(&sb->s_umount);
961 if (sb->s_frozen) {
962 deactivate_locked_super(sb);
963 return -EBUSY;
964 }
965
966 if (sb->s_flags & MS_RDONLY) {
967 sb->s_frozen = SB_FREEZE_TRANS;
968 smp_wmb();
969 up_write(&sb->s_umount);
970 return 0;
971 }
972
973 sb->s_frozen = SB_FREEZE_WRITE;
974 smp_wmb();
975
976 sync_filesystem(sb);
977
978 sb->s_frozen = SB_FREEZE_TRANS;
979 smp_wmb();
980
981 sync_blockdev(sb->s_bdev);
982 if (sb->s_op->freeze_fs) {
983 ret = sb->s_op->freeze_fs(sb);
984 if (ret) {
985 printk(KERN_ERR
986 "VFS:Filesystem freeze failed\n");
987 sb->s_frozen = SB_UNFROZEN;
988 deactivate_locked_super(sb);
989 return ret;
990 }
991 }
992 up_write(&sb->s_umount);
993 return 0;
994 }
995 EXPORT_SYMBOL(freeze_super);
996
997 /**
998 * thaw_super -- unlock filesystem
999 * @sb: the super to thaw
1000 *
1001 * Unlocks the filesystem and marks it writeable again after freeze_super().
1002 */
1003 int thaw_super(struct super_block *sb)
1004 {
1005 int error;
1006
1007 down_write(&sb->s_umount);
1008 if (sb->s_frozen == SB_UNFROZEN) {
1009 up_write(&sb->s_umount);
1010 return -EINVAL;
1011 }
1012
1013 if (sb->s_flags & MS_RDONLY)
1014 goto out;
1015
1016 if (sb->s_op->unfreeze_fs) {
1017 error = sb->s_op->unfreeze_fs(sb);
1018 if (error) {
1019 printk(KERN_ERR
1020 "VFS:Filesystem thaw failed\n");
1021 sb->s_frozen = SB_FREEZE_TRANS;
1022 up_write(&sb->s_umount);
1023 return error;
1024 }
1025 }
1026
1027 out:
1028 sb->s_frozen = SB_UNFROZEN;
1029 smp_wmb();
1030 wake_up(&sb->s_wait_unfrozen);
1031 deactivate_locked_super(sb);
1032
1033 return 0;
1034 }
1035 EXPORT_SYMBOL(thaw_super);
1036
1037 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1038 {
1039 int err;
1040 const char *subtype = strchr(fstype, '.');
1041 if (subtype) {
1042 subtype++;
1043 err = -EINVAL;
1044 if (!subtype[0])
1045 goto err;
1046 } else
1047 subtype = "";
1048
1049 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1050 err = -ENOMEM;
1051 if (!mnt->mnt_sb->s_subtype)
1052 goto err;
1053 return mnt;
1054
1055 err:
1056 mntput(mnt);
1057 return ERR_PTR(err);
1058 }
1059
1060 struct vfsmount *
1061 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1062 {
1063 struct file_system_type *type = get_fs_type(fstype);
1064 struct vfsmount *mnt;
1065 if (!type)
1066 return ERR_PTR(-ENODEV);
1067 mnt = vfs_kern_mount(type, flags, name, data);
1068 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1069 !mnt->mnt_sb->s_subtype)
1070 mnt = fs_set_subtype(mnt, fstype);
1071 put_filesystem(type);
1072 return mnt;
1073 }
1074 EXPORT_SYMBOL_GPL(do_kern_mount);
1075
1076 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
1077 {
1078 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
1079 }
1080
1081 EXPORT_SYMBOL_GPL(kern_mount_data);