blob: 9b780c42d845ca57cc40faa24814533a80ef29d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41
42
43void get_filesystem(struct file_system_type *fs);
44void put_filesystem(struct file_system_type *fs);
45struct file_system_type *get_fs_type(const char *name);
46
47LIST_HEAD(super_blocks);
48DEFINE_SPINLOCK(sb_lock);
49
50/**
51 * alloc_super - create new superblock
52 *
53 * Allocates and initializes a new &struct super_block. alloc_super()
54 * returns a pointer new superblock or %NULL if allocation had failed.
55 */
56static struct super_block *alloc_super(void)
57{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -080058 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 static struct super_operations default_op;
60
61 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (security_sb_alloc(s)) {
63 kfree(s);
64 s = NULL;
65 goto out;
66 }
67 INIT_LIST_HEAD(&s->s_dirty);
68 INIT_LIST_HEAD(&s->s_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_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080074 mutex_init(&s->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 down_write(&s->s_umount);
76 s->s_count = S_BIAS;
77 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -080078 mutex_init(&s->s_vfs_rename_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -080079 mutex_init(&s->s_dquot.dqio_mutex);
80 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 init_rwsem(&s->s_dquot.dqptr_sem);
82 init_waitqueue_head(&s->s_wait_unfrozen);
83 s->s_maxbytes = MAX_NON_LFS;
84 s->dq_op = sb_dquot_ops;
85 s->s_qcop = sb_quotactl_ops;
86 s->s_op = &default_op;
87 s->s_time_gran = 1000000000;
88 }
89out:
90 return s;
91}
92
93/**
94 * destroy_super - frees a superblock
95 * @s: superblock to free
96 *
97 * Frees a superblock.
98 */
99static inline void destroy_super(struct super_block *s)
100{
101 security_sb_free(s);
102 kfree(s);
103}
104
105/* Superblock refcounting */
106
107/*
108 * Drop a superblock's refcount. Returns non-zero if the superblock was
109 * destroyed. The caller must hold sb_lock.
110 */
111int __put_super(struct super_block *sb)
112{
113 int ret = 0;
114
115 if (!--sb->s_count) {
116 destroy_super(sb);
117 ret = 1;
118 }
119 return ret;
120}
121
122/*
123 * Drop a superblock's refcount.
124 * Returns non-zero if the superblock is about to be destroyed and
125 * at least is already removed from super_blocks list, so if we are
126 * making a loop through super blocks then we need to restart.
127 * The caller must hold sb_lock.
128 */
129int __put_super_and_need_restart(struct super_block *sb)
130{
131 /* check for race with generic_shutdown_super() */
132 if (list_empty(&sb->s_list)) {
133 /* super block is removed, need to restart... */
134 __put_super(sb);
135 return 1;
136 }
137 /* can't be the last, since s_list is still in use */
138 sb->s_count--;
139 BUG_ON(sb->s_count == 0);
140 return 0;
141}
142
143/**
144 * put_super - drop a temporary reference to superblock
145 * @sb: superblock in question
146 *
147 * Drops a temporary reference, frees superblock if there's no
148 * references left.
149 */
150static void put_super(struct super_block *sb)
151{
152 spin_lock(&sb_lock);
153 __put_super(sb);
154 spin_unlock(&sb_lock);
155}
156
157
158/**
159 * deactivate_super - drop an active reference to superblock
160 * @s: superblock to deactivate
161 *
162 * Drops an active reference to superblock, acquiring a temprory one if
163 * there is no active references left. In that case we lock superblock,
164 * tell fs driver to shut it down and drop the temporary reference we
165 * had just acquired.
166 */
167void deactivate_super(struct super_block *s)
168{
169 struct file_system_type *fs = s->s_type;
170 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
171 s->s_count -= S_BIAS-1;
172 spin_unlock(&sb_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500173 DQUOT_OFF(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 down_write(&s->s_umount);
175 fs->kill_sb(s);
176 put_filesystem(fs);
177 put_super(s);
178 }
179}
180
181EXPORT_SYMBOL(deactivate_super);
182
183/**
184 * grab_super - acquire an active reference
185 * @s: reference we are trying to make active
186 *
187 * Tries to acquire an active reference. grab_super() is used when we
188 * had just found a superblock in super_blocks or fs_type->fs_supers
189 * and want to turn it into a full-blown active reference. grab_super()
190 * is called with sb_lock held and drops it. Returns 1 in case of
191 * success, 0 if we had failed (superblock contents was already dead or
192 * dying when grab_super() had been called).
193 */
194static int grab_super(struct super_block *s)
195{
196 s->s_count++;
197 spin_unlock(&sb_lock);
198 down_write(&s->s_umount);
199 if (s->s_root) {
200 spin_lock(&sb_lock);
201 if (s->s_count > S_BIAS) {
202 atomic_inc(&s->s_active);
203 s->s_count--;
204 spin_unlock(&sb_lock);
205 return 1;
206 }
207 spin_unlock(&sb_lock);
208 }
209 up_write(&s->s_umount);
210 put_super(s);
211 yield();
212 return 0;
213}
214
215/**
216 * generic_shutdown_super - common helper for ->kill_sb()
217 * @sb: superblock to kill
218 *
219 * generic_shutdown_super() does all fs-independent work on superblock
220 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
221 * that need destruction out of superblock, call generic_shutdown_super()
222 * and release aforementioned objects. Note: dentries and inodes _are_
223 * taken care of and do not need specific handling.
224 */
225void generic_shutdown_super(struct super_block *sb)
226{
227 struct dentry *root = sb->s_root;
228 struct super_operations *sop = sb->s_op;
229
230 if (root) {
231 sb->s_root = NULL;
232 shrink_dcache_parent(root);
David Howells454e2392006-06-23 02:02:57 -0700233 shrink_dcache_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dput(root);
235 fsync_super(sb);
236 lock_super(sb);
237 sb->s_flags &= ~MS_ACTIVE;
238 /* bad name - it should be evict_inodes() */
239 invalidate_inodes(sb);
240 lock_kernel();
241
242 if (sop->write_super && sb->s_dirt)
243 sop->write_super(sb);
244 if (sop->put_super)
245 sop->put_super(sb);
246
247 /* Forget any remaining inodes */
248 if (invalidate_inodes(sb)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800249 printk("VFS: Busy inodes after unmount of %s. "
250 "Self-destruct in 5 seconds. Have a nice day...\n",
251 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 unlock_kernel();
255 unlock_super(sb);
256 }
257 spin_lock(&sb_lock);
258 /* should be initialized for __put_super_and_need_restart() */
259 list_del_init(&sb->s_list);
260 list_del(&sb->s_instances);
261 spin_unlock(&sb_lock);
262 up_write(&sb->s_umount);
263}
264
265EXPORT_SYMBOL(generic_shutdown_super);
266
267/**
268 * sget - find or create a superblock
269 * @type: filesystem type superblock should belong to
270 * @test: comparison callback
271 * @set: setup callback
272 * @data: argument to each of them
273 */
274struct super_block *sget(struct file_system_type *type,
275 int (*test)(struct super_block *,void *),
276 int (*set)(struct super_block *,void *),
277 void *data)
278{
279 struct super_block *s = NULL;
280 struct list_head *p;
281 int err;
282
283retry:
284 spin_lock(&sb_lock);
285 if (test) list_for_each(p, &type->fs_supers) {
286 struct super_block *old;
287 old = list_entry(p, struct super_block, s_instances);
288 if (!test(old, data))
289 continue;
290 if (!grab_super(old))
291 goto retry;
292 if (s)
293 destroy_super(s);
294 return old;
295 }
296 if (!s) {
297 spin_unlock(&sb_lock);
298 s = alloc_super();
299 if (!s)
300 return ERR_PTR(-ENOMEM);
301 goto retry;
302 }
303
304 err = set(s, data);
305 if (err) {
306 spin_unlock(&sb_lock);
307 destroy_super(s);
308 return ERR_PTR(err);
309 }
310 s->s_type = type;
311 strlcpy(s->s_id, type->name, sizeof(s->s_id));
312 list_add_tail(&s->s_list, &super_blocks);
313 list_add(&s->s_instances, &type->fs_supers);
314 spin_unlock(&sb_lock);
315 get_filesystem(type);
316 return s;
317}
318
319EXPORT_SYMBOL(sget);
320
321void drop_super(struct super_block *sb)
322{
323 up_read(&sb->s_umount);
324 put_super(sb);
325}
326
327EXPORT_SYMBOL(drop_super);
328
329static inline void write_super(struct super_block *sb)
330{
331 lock_super(sb);
332 if (sb->s_root && sb->s_dirt)
333 if (sb->s_op->write_super)
334 sb->s_op->write_super(sb);
335 unlock_super(sb);
336}
337
338/*
339 * Note: check the dirty flag before waiting, so we don't
340 * hold up the sync while mounting a device. (The newly
341 * mounted device won't need syncing.)
342 */
343void sync_supers(void)
344{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700345 struct super_block *sb;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700348restart:
349 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (sb->s_dirt) {
351 sb->s_count++;
352 spin_unlock(&sb_lock);
353 down_read(&sb->s_umount);
354 write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700355 up_read(&sb->s_umount);
356 spin_lock(&sb_lock);
357 if (__put_super_and_need_restart(sb))
358 goto restart;
359 }
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock(&sb_lock);
362}
363
364/*
365 * Call the ->sync_fs super_op against all filesytems which are r/w and
366 * which implement it.
367 *
368 * This operation is careful to avoid the livelock which could easily happen
369 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
370 * is used only here. We set it against all filesystems and then clear it as
371 * we sync them. So redirtied filesystems are skipped.
372 *
373 * But if process A is currently running sync_filesytems and then process B
374 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
375 * flags again, which will cause process A to resync everything. Fix that with
376 * a local mutex.
377 *
378 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
379 */
380void sync_filesystems(int wait)
381{
382 struct super_block *sb;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800383 static DEFINE_MUTEX(mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800385 mutex_lock(&mutex); /* Could be down_interruptible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700387 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!sb->s_op->sync_fs)
389 continue;
390 if (sb->s_flags & MS_RDONLY)
391 continue;
392 sb->s_need_sync_fs = 1;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395restart:
Kirill Korotaev618f0632005-06-23 00:09:54 -0700396 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!sb->s_need_sync_fs)
398 continue;
399 sb->s_need_sync_fs = 0;
400 if (sb->s_flags & MS_RDONLY)
401 continue; /* hm. Was remounted r/o meanwhile */
402 sb->s_count++;
403 spin_unlock(&sb_lock);
404 down_read(&sb->s_umount);
405 if (sb->s_root && (wait || sb->s_dirt))
406 sb->s_op->sync_fs(sb, wait);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700407 up_read(&sb->s_umount);
408 /* restart only when sb is no longer on the list */
409 spin_lock(&sb_lock);
410 if (__put_super_and_need_restart(sb))
411 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 spin_unlock(&sb_lock);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800414 mutex_unlock(&mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/**
418 * get_super - get the superblock of a device
419 * @bdev: device to get the superblock for
420 *
421 * Scans the superblock list and finds the superblock of the file system
422 * mounted on the device given. %NULL is returned if no match is found.
423 */
424
425struct super_block * get_super(struct block_device *bdev)
426{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700427 struct super_block *sb;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (!bdev)
430 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700433rescan:
434 list_for_each_entry(sb, &super_blocks, s_list) {
435 if (sb->s_bdev == bdev) {
436 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700438 down_read(&sb->s_umount);
439 if (sb->s_root)
440 return sb;
441 up_read(&sb->s_umount);
442 /* restart only when sb is no longer on the list */
443 spin_lock(&sb_lock);
444 if (__put_super_and_need_restart(sb))
445 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 }
448 spin_unlock(&sb_lock);
449 return NULL;
450}
451
452EXPORT_SYMBOL(get_super);
453
454struct super_block * user_get_super(dev_t dev)
455{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700456 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700459rescan:
460 list_for_each_entry(sb, &super_blocks, s_list) {
461 if (sb->s_dev == dev) {
462 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700464 down_read(&sb->s_umount);
465 if (sb->s_root)
466 return sb;
467 up_read(&sb->s_umount);
468 /* restart only when sb is no longer on the list */
469 spin_lock(&sb_lock);
470 if (__put_super_and_need_restart(sb))
471 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 }
474 spin_unlock(&sb_lock);
475 return NULL;
476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
479{
480 struct super_block *s;
481 struct ustat tmp;
482 struct kstatfs sbuf;
483 int err = -EINVAL;
484
485 s = user_get_super(new_decode_dev(dev));
486 if (s == NULL)
487 goto out;
David Howells726c3342006-06-23 02:02:58 -0700488 err = vfs_statfs(s->s_root, &sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 drop_super(s);
490 if (err)
491 goto out;
492
493 memset(&tmp,0,sizeof(struct ustat));
494 tmp.f_tfree = sbuf.f_bfree;
495 tmp.f_tinode = sbuf.f_ffree;
496
497 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
498out:
499 return err;
500}
501
502/**
503 * mark_files_ro
504 * @sb: superblock in question
505 *
506 * All files are marked read/only. We don't care about pending
507 * delete files so this should be used in 'force' mode only
508 */
509
510static void mark_files_ro(struct super_block *sb)
511{
512 struct file *f;
513
514 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800515 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
517 f->f_mode &= ~FMODE_WRITE;
518 }
519 file_list_unlock();
520}
521
522/**
523 * do_remount_sb - asks filesystem to change mount options.
524 * @sb: superblock in question
525 * @flags: numeric part of options
526 * @data: the rest of options
527 * @force: whether or not to force the change
528 *
529 * Alters the mount options of a mounted file system.
530 */
531int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
532{
533 int retval;
534
535 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
536 return -EACCES;
537 if (flags & MS_RDONLY)
538 acct_auto_close(sb);
539 shrink_dcache_sb(sb);
540 fsync_super(sb);
541
542 /* If we are remounting RDONLY and current sb is read/write,
543 make sure there are no rw files opened */
544 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
545 if (force)
546 mark_files_ro(sb);
547 else if (!fs_may_remount_ro(sb))
548 return -EBUSY;
549 }
550
551 if (sb->s_op->remount_fs) {
552 lock_super(sb);
553 retval = sb->s_op->remount_fs(sb, &flags, data);
554 unlock_super(sb);
555 if (retval)
556 return retval;
557 }
558 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
559 return 0;
560}
561
562static void do_emergency_remount(unsigned long foo)
563{
564 struct super_block *sb;
565
566 spin_lock(&sb_lock);
567 list_for_each_entry(sb, &super_blocks, s_list) {
568 sb->s_count++;
569 spin_unlock(&sb_lock);
570 down_read(&sb->s_umount);
571 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
572 /*
573 * ->remount_fs needs lock_kernel().
574 *
575 * What lock protects sb->s_flags??
576 */
577 lock_kernel();
578 do_remount_sb(sb, MS_RDONLY, NULL, 1);
579 unlock_kernel();
580 }
581 drop_super(sb);
582 spin_lock(&sb_lock);
583 }
584 spin_unlock(&sb_lock);
585 printk("Emergency Remount complete\n");
586}
587
588void emergency_remount(void)
589{
590 pdflush_operation(do_emergency_remount, 0);
591}
592
593/*
594 * Unnamed block devices are dummy devices used by virtual
595 * filesystems which don't use real block-devices. -- jrs
596 */
597
598static struct idr unnamed_dev_idr;
599static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
600
601int set_anon_super(struct super_block *s, void *data)
602{
603 int dev;
604 int error;
605
606 retry:
607 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
608 return -ENOMEM;
609 spin_lock(&unnamed_dev_lock);
610 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
611 spin_unlock(&unnamed_dev_lock);
612 if (error == -EAGAIN)
613 /* We raced and lost with another CPU. */
614 goto retry;
615 else if (error)
616 return -EAGAIN;
617
618 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
619 spin_lock(&unnamed_dev_lock);
620 idr_remove(&unnamed_dev_idr, dev);
621 spin_unlock(&unnamed_dev_lock);
622 return -EMFILE;
623 }
624 s->s_dev = MKDEV(0, dev & MINORMASK);
625 return 0;
626}
627
628EXPORT_SYMBOL(set_anon_super);
629
630void kill_anon_super(struct super_block *sb)
631{
632 int slot = MINOR(sb->s_dev);
633
634 generic_shutdown_super(sb);
635 spin_lock(&unnamed_dev_lock);
636 idr_remove(&unnamed_dev_idr, slot);
637 spin_unlock(&unnamed_dev_lock);
638}
639
640EXPORT_SYMBOL(kill_anon_super);
641
642void __init unnamed_dev_init(void)
643{
644 idr_init(&unnamed_dev_idr);
645}
646
647void kill_litter_super(struct super_block *sb)
648{
649 if (sb->s_root)
650 d_genocide(sb->s_root);
651 kill_anon_super(sb);
652}
653
654EXPORT_SYMBOL(kill_litter_super);
655
656static int set_bdev_super(struct super_block *s, void *data)
657{
658 s->s_bdev = data;
659 s->s_dev = s->s_bdev->bd_dev;
660 return 0;
661}
662
663static int test_bdev_super(struct super_block *s, void *data)
664{
665 return (void *)s->s_bdev == data;
666}
667
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800668static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
669{
670 if (bdev->bd_disk) {
671 if (bdev->bd_part)
672 kobject_uevent(&bdev->bd_part->kobj, action);
673 else
674 kobject_uevent(&bdev->bd_disk->kobj, action);
675 }
676}
677
David Howells454e2392006-06-23 02:02:57 -0700678int get_sb_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int flags, const char *dev_name, void *data,
David Howells454e2392006-06-23 02:02:57 -0700680 int (*fill_super)(struct super_block *, void *, int),
681 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct block_device *bdev;
684 struct super_block *s;
685 int error = 0;
686
687 bdev = open_bdev_excl(dev_name, flags, fs_type);
688 if (IS_ERR(bdev))
David Howells454e2392006-06-23 02:02:57 -0700689 return PTR_ERR(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /*
692 * once the super is inserted into the list by sget, s_umount
693 * will protect the lockfs code from trying to start a snapshot
694 * while we are mounting
695 */
Arjan van de Venc039e312006-03-23 03:00:28 -0800696 mutex_lock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800698 mutex_unlock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700700 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (s->s_root) {
703 if ((flags ^ s->s_flags) & MS_RDONLY) {
704 up_write(&s->s_umount);
705 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700706 error = -EBUSY;
707 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
David Howells454e2392006-06-23 02:02:57 -0700709
710 close_bdev_excl(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } else {
712 char b[BDEVNAME_SIZE];
713
714 s->s_flags = flags;
715 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800716 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800717 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (error) {
719 up_write(&s->s_umount);
720 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700721 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800722 }
David Howells454e2392006-06-23 02:02:57 -0700723
724 s->s_flags |= MS_ACTIVE;
725 bdev_uevent(bdev, KOBJ_MOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
David Howells454e2392006-06-23 02:02:57 -0700728 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
David Howells454e2392006-06-23 02:02:57 -0700730error_s:
731 error = PTR_ERR(s);
732error_bdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 close_bdev_excl(bdev);
David Howells454e2392006-06-23 02:02:57 -0700734error:
735 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738EXPORT_SYMBOL(get_sb_bdev);
739
740void kill_block_super(struct super_block *sb)
741{
742 struct block_device *bdev = sb->s_bdev;
743
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800744 bdev_uevent(bdev, KOBJ_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 generic_shutdown_super(sb);
746 sync_blockdev(bdev);
747 close_bdev_excl(bdev);
748}
749
750EXPORT_SYMBOL(kill_block_super);
751
David Howells454e2392006-06-23 02:02:57 -0700752int get_sb_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700754 int (*fill_super)(struct super_block *, void *, int),
755 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 int error;
758 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
759
760 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700761 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 s->s_flags = flags;
764
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800765 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (error) {
767 up_write(&s->s_umount);
768 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700769 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 s->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700772 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775EXPORT_SYMBOL(get_sb_nodev);
776
777static int compare_single(struct super_block *s, void *p)
778{
779 return 1;
780}
781
David Howells454e2392006-06-23 02:02:57 -0700782int get_sb_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700784 int (*fill_super)(struct super_block *, void *, int),
785 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct super_block *s;
788 int error;
789
790 s = sget(fs_type, compare_single, set_anon_super, NULL);
791 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700792 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!s->s_root) {
794 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800795 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (error) {
797 up_write(&s->s_umount);
798 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700799 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 s->s_flags |= MS_ACTIVE;
802 }
803 do_remount_sb(s, flags, data, 0);
David Howells454e2392006-06-23 02:02:57 -0700804 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807EXPORT_SYMBOL(get_sb_single);
808
809struct vfsmount *
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400810vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 char *secdata = NULL;
David Howells454e2392006-06-23 02:02:57 -0700814 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (!type)
817 return ERR_PTR(-ENODEV);
818
David Howells454e2392006-06-23 02:02:57 -0700819 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 mnt = alloc_vfsmnt(name);
821 if (!mnt)
822 goto out;
823
824 if (data) {
825 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700826 if (!secdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto out_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 error = security_sb_copy_data(type, data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700830 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
David Howells454e2392006-06-23 02:02:57 -0700834 error = type->get_sb(type, flags, name, data, mnt);
835 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto out_free_secdata;
David Howells454e2392006-06-23 02:02:57 -0700837
838 error = security_sb_kern_mount(mnt->mnt_sb, secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (error)
840 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700841
842 mnt->mnt_mountpoint = mnt->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 mnt->mnt_parent = mnt;
David Howells454e2392006-06-23 02:02:57 -0700844 up_write(&mnt->mnt_sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -0700845 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return mnt;
847out_sb:
David Howells454e2392006-06-23 02:02:57 -0700848 dput(mnt->mnt_root);
849 up_write(&mnt->mnt_sb->s_umount);
850 deactivate_super(mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851out_free_secdata:
852 free_secdata(secdata);
853out_mnt:
854 free_vfsmnt(mnt);
855out:
David Howells454e2392006-06-23 02:02:57 -0700856 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400859EXPORT_SYMBOL_GPL(vfs_kern_mount);
860
861struct vfsmount *
862do_kern_mount(const char *fstype, int flags, const char *name, void *data)
863{
864 struct file_system_type *type = get_fs_type(fstype);
865 struct vfsmount *mnt;
866 if (!type)
867 return ERR_PTR(-ENODEV);
868 mnt = vfs_kern_mount(type, flags, name, data);
869 put_filesystem(type);
870 return mnt;
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873struct vfsmount *kern_mount(struct file_system_type *type)
874{
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400875 return vfs_kern_mount(type, 0, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878EXPORT_SYMBOL(kern_mount);