blob: 15671cd048b146b8315fb11012cddfd796ce82c4 [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
Henrik Kretzschmarfe2bbc42006-09-06 00:03:41 -070052 * @type: filesystem type superblock should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 *
54 * Allocates and initializes a new &struct super_block. alloc_super()
55 * returns a pointer new superblock or %NULL if allocation had failed.
56 */
Ingo Molnarcf516242006-07-03 00:25:27 -070057static struct super_block *alloc_super(struct file_system_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -080059 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 static struct super_operations default_op;
61
62 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (security_sb_alloc(s)) {
64 kfree(s);
65 s = NULL;
66 goto out;
67 }
68 INIT_LIST_HEAD(&s->s_dirty);
69 INIT_LIST_HEAD(&s->s_io);
70 INIT_LIST_HEAD(&s->s_files);
71 INIT_LIST_HEAD(&s->s_instances);
72 INIT_HLIST_HEAD(&s->s_anon);
73 INIT_LIST_HEAD(&s->s_inodes);
74 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080075 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070076 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -070077 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 down_write(&s->s_umount);
84 s->s_count = S_BIAS;
85 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -080086 mutex_init(&s->s_vfs_rename_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -080087 mutex_init(&s->s_dquot.dqio_mutex);
88 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 init_rwsem(&s->s_dquot.dqptr_sem);
90 init_waitqueue_head(&s->s_wait_unfrozen);
91 s->s_maxbytes = MAX_NON_LFS;
92 s->dq_op = sb_dquot_ops;
93 s->s_qcop = sb_quotactl_ops;
94 s->s_op = &default_op;
95 s->s_time_gran = 1000000000;
96 }
97out:
98 return s;
99}
100
101/**
102 * destroy_super - frees a superblock
103 * @s: superblock to free
104 *
105 * Frees a superblock.
106 */
107static inline void destroy_super(struct super_block *s)
108{
109 security_sb_free(s);
110 kfree(s);
111}
112
113/* Superblock refcounting */
114
115/*
116 * Drop a superblock's refcount. Returns non-zero if the superblock was
117 * destroyed. The caller must hold sb_lock.
118 */
119int __put_super(struct super_block *sb)
120{
121 int ret = 0;
122
123 if (!--sb->s_count) {
124 destroy_super(sb);
125 ret = 1;
126 }
127 return ret;
128}
129
130/*
131 * Drop a superblock's refcount.
132 * Returns non-zero if the superblock is about to be destroyed and
133 * at least is already removed from super_blocks list, so if we are
134 * making a loop through super blocks then we need to restart.
135 * The caller must hold sb_lock.
136 */
137int __put_super_and_need_restart(struct super_block *sb)
138{
139 /* check for race with generic_shutdown_super() */
140 if (list_empty(&sb->s_list)) {
141 /* super block is removed, need to restart... */
142 __put_super(sb);
143 return 1;
144 }
145 /* can't be the last, since s_list is still in use */
146 sb->s_count--;
147 BUG_ON(sb->s_count == 0);
148 return 0;
149}
150
151/**
152 * put_super - drop a temporary reference to superblock
153 * @sb: superblock in question
154 *
155 * Drops a temporary reference, frees superblock if there's no
156 * references left.
157 */
158static void put_super(struct super_block *sb)
159{
160 spin_lock(&sb_lock);
161 __put_super(sb);
162 spin_unlock(&sb_lock);
163}
164
165
166/**
167 * deactivate_super - drop an active reference to superblock
168 * @s: superblock to deactivate
169 *
170 * Drops an active reference to superblock, acquiring a temprory one if
171 * there is no active references left. In that case we lock superblock,
172 * tell fs driver to shut it down and drop the temporary reference we
173 * had just acquired.
174 */
175void deactivate_super(struct super_block *s)
176{
177 struct file_system_type *fs = s->s_type;
178 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
179 s->s_count -= S_BIAS-1;
180 spin_unlock(&sb_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500181 DQUOT_OFF(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 down_write(&s->s_umount);
183 fs->kill_sb(s);
184 put_filesystem(fs);
185 put_super(s);
186 }
187}
188
189EXPORT_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 */
Josh Triplett9c4dbee2006-09-29 01:59:29 -0700202static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 s->s_count++;
205 spin_unlock(&sb_lock);
206 down_write(&s->s_umount);
207 if (s->s_root) {
208 spin_lock(&sb_lock);
209 if (s->s_count > S_BIAS) {
210 atomic_inc(&s->s_active);
211 s->s_count--;
212 spin_unlock(&sb_lock);
213 return 1;
214 }
215 spin_unlock(&sb_lock);
216 }
217 up_write(&s->s_umount);
218 put_super(s);
219 yield();
220 return 0;
221}
222
David Howellscf9a2ae2006-08-29 19:05:54 +0100223/*
224 * Write out and wait upon all dirty data associated with this
225 * superblock. Filesystem data as well as the underlying block
226 * device. Takes the superblock lock. Requires a second blkdev
227 * flush by the caller to complete the operation.
228 */
229void __fsync_super(struct super_block *sb)
230{
231 sync_inodes_sb(sb, 0);
232 DQUOT_SYNC(sb);
233 lock_super(sb);
234 if (sb->s_dirt && sb->s_op->write_super)
235 sb->s_op->write_super(sb);
236 unlock_super(sb);
237 if (sb->s_op->sync_fs)
238 sb->s_op->sync_fs(sb, 1);
239 sync_blockdev(sb->s_bdev);
240 sync_inodes_sb(sb, 1);
241}
242
243/*
244 * Write out and wait upon all dirty data associated with this
245 * superblock. Filesystem data as well as the underlying block
246 * device. Takes the superblock lock.
247 */
248int fsync_super(struct super_block *sb)
249{
250 __fsync_super(sb);
251 return sync_blockdev(sb->s_bdev);
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
255 * generic_shutdown_super - common helper for ->kill_sb()
256 * @sb: superblock to kill
257 *
258 * generic_shutdown_super() does all fs-independent work on superblock
259 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
260 * that need destruction out of superblock, call generic_shutdown_super()
261 * and release aforementioned objects. Note: dentries and inodes _are_
262 * taken care of and do not need specific handling.
263 */
264void generic_shutdown_super(struct super_block *sb)
265{
266 struct dentry *root = sb->s_root;
267 struct super_operations *sop = sb->s_op;
268
269 if (root) {
270 sb->s_root = NULL;
271 shrink_dcache_parent(root);
David Howells454e2392006-06-23 02:02:57 -0700272 shrink_dcache_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dput(root);
274 fsync_super(sb);
275 lock_super(sb);
276 sb->s_flags &= ~MS_ACTIVE;
277 /* bad name - it should be evict_inodes() */
278 invalidate_inodes(sb);
279 lock_kernel();
280
281 if (sop->write_super && sb->s_dirt)
282 sop->write_super(sb);
283 if (sop->put_super)
284 sop->put_super(sb);
285
286 /* Forget any remaining inodes */
287 if (invalidate_inodes(sb)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800288 printk("VFS: Busy inodes after unmount of %s. "
289 "Self-destruct in 5 seconds. Have a nice day...\n",
290 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293 unlock_kernel();
294 unlock_super(sb);
295 }
296 spin_lock(&sb_lock);
297 /* should be initialized for __put_super_and_need_restart() */
298 list_del_init(&sb->s_list);
299 list_del(&sb->s_instances);
300 spin_unlock(&sb_lock);
301 up_write(&sb->s_umount);
302}
303
304EXPORT_SYMBOL(generic_shutdown_super);
305
306/**
307 * sget - find or create a superblock
308 * @type: filesystem type superblock should belong to
309 * @test: comparison callback
310 * @set: setup callback
311 * @data: argument to each of them
312 */
313struct super_block *sget(struct file_system_type *type,
314 int (*test)(struct super_block *,void *),
315 int (*set)(struct super_block *,void *),
316 void *data)
317{
318 struct super_block *s = NULL;
319 struct list_head *p;
320 int err;
321
322retry:
323 spin_lock(&sb_lock);
324 if (test) list_for_each(p, &type->fs_supers) {
325 struct super_block *old;
326 old = list_entry(p, struct super_block, s_instances);
327 if (!test(old, data))
328 continue;
329 if (!grab_super(old))
330 goto retry;
331 if (s)
332 destroy_super(s);
333 return old;
334 }
335 if (!s) {
336 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700337 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!s)
339 return ERR_PTR(-ENOMEM);
340 goto retry;
341 }
342
343 err = set(s, data);
344 if (err) {
345 spin_unlock(&sb_lock);
346 destroy_super(s);
347 return ERR_PTR(err);
348 }
349 s->s_type = type;
350 strlcpy(s->s_id, type->name, sizeof(s->s_id));
351 list_add_tail(&s->s_list, &super_blocks);
352 list_add(&s->s_instances, &type->fs_supers);
353 spin_unlock(&sb_lock);
354 get_filesystem(type);
355 return s;
356}
357
358EXPORT_SYMBOL(sget);
359
360void drop_super(struct super_block *sb)
361{
362 up_read(&sb->s_umount);
363 put_super(sb);
364}
365
366EXPORT_SYMBOL(drop_super);
367
368static inline void write_super(struct super_block *sb)
369{
370 lock_super(sb);
371 if (sb->s_root && sb->s_dirt)
372 if (sb->s_op->write_super)
373 sb->s_op->write_super(sb);
374 unlock_super(sb);
375}
376
377/*
378 * Note: check the dirty flag before waiting, so we don't
379 * hold up the sync while mounting a device. (The newly
380 * mounted device won't need syncing.)
381 */
382void sync_supers(void)
383{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700384 struct super_block *sb;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700387restart:
388 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (sb->s_dirt) {
390 sb->s_count++;
391 spin_unlock(&sb_lock);
392 down_read(&sb->s_umount);
393 write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700394 up_read(&sb->s_umount);
395 spin_lock(&sb_lock);
396 if (__put_super_and_need_restart(sb))
397 goto restart;
398 }
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 spin_unlock(&sb_lock);
401}
402
403/*
404 * Call the ->sync_fs super_op against all filesytems which are r/w and
405 * which implement it.
406 *
407 * This operation is careful to avoid the livelock which could easily happen
408 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
409 * is used only here. We set it against all filesystems and then clear it as
410 * we sync them. So redirtied filesystems are skipped.
411 *
412 * But if process A is currently running sync_filesytems and then process B
413 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
414 * flags again, which will cause process A to resync everything. Fix that with
415 * a local mutex.
416 *
417 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
418 */
419void sync_filesystems(int wait)
420{
421 struct super_block *sb;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800422 static DEFINE_MUTEX(mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800424 mutex_lock(&mutex); /* Could be down_interruptible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700426 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!sb->s_op->sync_fs)
428 continue;
429 if (sb->s_flags & MS_RDONLY)
430 continue;
431 sb->s_need_sync_fs = 1;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434restart:
Kirill Korotaev618f0632005-06-23 00:09:54 -0700435 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!sb->s_need_sync_fs)
437 continue;
438 sb->s_need_sync_fs = 0;
439 if (sb->s_flags & MS_RDONLY)
440 continue; /* hm. Was remounted r/o meanwhile */
441 sb->s_count++;
442 spin_unlock(&sb_lock);
443 down_read(&sb->s_umount);
444 if (sb->s_root && (wait || sb->s_dirt))
445 sb->s_op->sync_fs(sb, wait);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700446 up_read(&sb->s_umount);
447 /* restart only when sb is no longer on the list */
448 spin_lock(&sb_lock);
449 if (__put_super_and_need_restart(sb))
450 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 spin_unlock(&sb_lock);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800453 mutex_unlock(&mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456/**
457 * get_super - get 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. %NULL is returned if no match is found.
462 */
463
464struct super_block * get_super(struct block_device *bdev)
465{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700466 struct super_block *sb;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!bdev)
469 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700472rescan:
473 list_for_each_entry(sb, &super_blocks, s_list) {
474 if (sb->s_bdev == bdev) {
475 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700477 down_read(&sb->s_umount);
478 if (sb->s_root)
479 return sb;
480 up_read(&sb->s_umount);
481 /* restart only when sb is no longer on the list */
482 spin_lock(&sb_lock);
483 if (__put_super_and_need_restart(sb))
484 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 }
487 spin_unlock(&sb_lock);
488 return NULL;
489}
490
491EXPORT_SYMBOL(get_super);
492
493struct super_block * user_get_super(dev_t dev)
494{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700495 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700498rescan:
499 list_for_each_entry(sb, &super_blocks, s_list) {
500 if (sb->s_dev == dev) {
501 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700503 down_read(&sb->s_umount);
504 if (sb->s_root)
505 return sb;
506 up_read(&sb->s_umount);
507 /* restart only when sb is no longer on the list */
508 spin_lock(&sb_lock);
509 if (__put_super_and_need_restart(sb))
510 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 }
513 spin_unlock(&sb_lock);
514 return NULL;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
518{
519 struct super_block *s;
520 struct ustat tmp;
521 struct kstatfs sbuf;
522 int err = -EINVAL;
523
524 s = user_get_super(new_decode_dev(dev));
525 if (s == NULL)
526 goto out;
David Howells726c3342006-06-23 02:02:58 -0700527 err = vfs_statfs(s->s_root, &sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 drop_super(s);
529 if (err)
530 goto out;
531
532 memset(&tmp,0,sizeof(struct ustat));
533 tmp.f_tfree = sbuf.f_bfree;
534 tmp.f_tinode = sbuf.f_ffree;
535
536 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
537out:
538 return err;
539}
540
541/**
542 * mark_files_ro
543 * @sb: superblock in question
544 *
545 * All files are marked read/only. We don't care about pending
546 * delete files so this should be used in 'force' mode only
547 */
548
549static void mark_files_ro(struct super_block *sb)
550{
551 struct file *f;
552
553 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800554 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
556 f->f_mode &= ~FMODE_WRITE;
557 }
558 file_list_unlock();
559}
560
561/**
562 * do_remount_sb - asks filesystem to change mount options.
563 * @sb: superblock in question
564 * @flags: numeric part of options
565 * @data: the rest of options
566 * @force: whether or not to force the change
567 *
568 * Alters the mount options of a mounted file system.
569 */
570int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
571{
572 int retval;
573
574 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
575 return -EACCES;
576 if (flags & MS_RDONLY)
577 acct_auto_close(sb);
578 shrink_dcache_sb(sb);
579 fsync_super(sb);
580
581 /* If we are remounting RDONLY and current sb is read/write,
582 make sure there are no rw files opened */
583 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
584 if (force)
585 mark_files_ro(sb);
586 else if (!fs_may_remount_ro(sb))
587 return -EBUSY;
588 }
589
590 if (sb->s_op->remount_fs) {
591 lock_super(sb);
592 retval = sb->s_op->remount_fs(sb, &flags, data);
593 unlock_super(sb);
594 if (retval)
595 return retval;
596 }
597 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
598 return 0;
599}
600
601static void do_emergency_remount(unsigned long foo)
602{
603 struct super_block *sb;
604
605 spin_lock(&sb_lock);
606 list_for_each_entry(sb, &super_blocks, s_list) {
607 sb->s_count++;
608 spin_unlock(&sb_lock);
609 down_read(&sb->s_umount);
610 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
611 /*
612 * ->remount_fs needs lock_kernel().
613 *
614 * What lock protects sb->s_flags??
615 */
616 lock_kernel();
617 do_remount_sb(sb, MS_RDONLY, NULL, 1);
618 unlock_kernel();
619 }
620 drop_super(sb);
621 spin_lock(&sb_lock);
622 }
623 spin_unlock(&sb_lock);
624 printk("Emergency Remount complete\n");
625}
626
627void emergency_remount(void)
628{
629 pdflush_operation(do_emergency_remount, 0);
630}
631
632/*
633 * Unnamed block devices are dummy devices used by virtual
634 * filesystems which don't use real block-devices. -- jrs
635 */
636
637static struct idr unnamed_dev_idr;
638static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
639
640int set_anon_super(struct super_block *s, void *data)
641{
642 int dev;
643 int error;
644
645 retry:
646 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
647 return -ENOMEM;
648 spin_lock(&unnamed_dev_lock);
649 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
650 spin_unlock(&unnamed_dev_lock);
651 if (error == -EAGAIN)
652 /* We raced and lost with another CPU. */
653 goto retry;
654 else if (error)
655 return -EAGAIN;
656
657 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
658 spin_lock(&unnamed_dev_lock);
659 idr_remove(&unnamed_dev_idr, dev);
660 spin_unlock(&unnamed_dev_lock);
661 return -EMFILE;
662 }
663 s->s_dev = MKDEV(0, dev & MINORMASK);
664 return 0;
665}
666
667EXPORT_SYMBOL(set_anon_super);
668
669void kill_anon_super(struct super_block *sb)
670{
671 int slot = MINOR(sb->s_dev);
672
673 generic_shutdown_super(sb);
674 spin_lock(&unnamed_dev_lock);
675 idr_remove(&unnamed_dev_idr, slot);
676 spin_unlock(&unnamed_dev_lock);
677}
678
679EXPORT_SYMBOL(kill_anon_super);
680
681void __init unnamed_dev_init(void)
682{
683 idr_init(&unnamed_dev_idr);
684}
685
686void kill_litter_super(struct super_block *sb)
687{
688 if (sb->s_root)
689 d_genocide(sb->s_root);
690 kill_anon_super(sb);
691}
692
693EXPORT_SYMBOL(kill_litter_super);
694
695static int set_bdev_super(struct super_block *s, void *data)
696{
697 s->s_bdev = data;
698 s->s_dev = s->s_bdev->bd_dev;
699 return 0;
700}
701
702static int test_bdev_super(struct super_block *s, void *data)
703{
704 return (void *)s->s_bdev == data;
705}
706
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800707static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
708{
709 if (bdev->bd_disk) {
710 if (bdev->bd_part)
711 kobject_uevent(&bdev->bd_part->kobj, action);
712 else
713 kobject_uevent(&bdev->bd_disk->kobj, action);
714 }
715}
716
David Howells454e2392006-06-23 02:02:57 -0700717int get_sb_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int flags, const char *dev_name, void *data,
David Howells454e2392006-06-23 02:02:57 -0700719 int (*fill_super)(struct super_block *, void *, int),
720 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct block_device *bdev;
723 struct super_block *s;
724 int error = 0;
725
726 bdev = open_bdev_excl(dev_name, flags, fs_type);
727 if (IS_ERR(bdev))
David Howells454e2392006-06-23 02:02:57 -0700728 return PTR_ERR(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /*
731 * once the super is inserted into the list by sget, s_umount
732 * will protect the lockfs code from trying to start a snapshot
733 * while we are mounting
734 */
Arjan van de Venc039e312006-03-23 03:00:28 -0800735 mutex_lock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800737 mutex_unlock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700739 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (s->s_root) {
742 if ((flags ^ s->s_flags) & MS_RDONLY) {
743 up_write(&s->s_umount);
744 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700745 error = -EBUSY;
746 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
David Howells454e2392006-06-23 02:02:57 -0700748
749 close_bdev_excl(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 } else {
751 char b[BDEVNAME_SIZE];
752
753 s->s_flags = flags;
754 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800755 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800756 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (error) {
758 up_write(&s->s_umount);
759 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700760 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800761 }
David Howells454e2392006-06-23 02:02:57 -0700762
763 s->s_flags |= MS_ACTIVE;
764 bdev_uevent(bdev, KOBJ_MOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
David Howells454e2392006-06-23 02:02:57 -0700767 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
David Howells454e2392006-06-23 02:02:57 -0700769error_s:
770 error = PTR_ERR(s);
771error_bdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 close_bdev_excl(bdev);
David Howells454e2392006-06-23 02:02:57 -0700773error:
774 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777EXPORT_SYMBOL(get_sb_bdev);
778
779void kill_block_super(struct super_block *sb)
780{
781 struct block_device *bdev = sb->s_bdev;
782
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800783 bdev_uevent(bdev, KOBJ_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 generic_shutdown_super(sb);
785 sync_blockdev(bdev);
786 close_bdev_excl(bdev);
787}
788
789EXPORT_SYMBOL(kill_block_super);
790
David Howells454e2392006-06-23 02:02:57 -0700791int get_sb_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700793 int (*fill_super)(struct super_block *, void *, int),
794 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 int error;
797 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
798
799 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700800 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 s->s_flags = flags;
803
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800804 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (error) {
806 up_write(&s->s_umount);
807 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700808 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810 s->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700811 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814EXPORT_SYMBOL(get_sb_nodev);
815
816static int compare_single(struct super_block *s, void *p)
817{
818 return 1;
819}
820
David Howells454e2392006-06-23 02:02:57 -0700821int get_sb_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700823 int (*fill_super)(struct super_block *, void *, int),
824 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 struct super_block *s;
827 int error;
828
829 s = sget(fs_type, compare_single, set_anon_super, NULL);
830 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700831 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!s->s_root) {
833 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800834 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (error) {
836 up_write(&s->s_umount);
837 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700838 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 s->s_flags |= MS_ACTIVE;
841 }
842 do_remount_sb(s, flags, data, 0);
David Howells454e2392006-06-23 02:02:57 -0700843 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846EXPORT_SYMBOL(get_sb_single);
847
848struct vfsmount *
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400849vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 char *secdata = NULL;
David Howells454e2392006-06-23 02:02:57 -0700853 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (!type)
856 return ERR_PTR(-ENODEV);
857
David Howells454e2392006-06-23 02:02:57 -0700858 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 mnt = alloc_vfsmnt(name);
860 if (!mnt)
861 goto out;
862
863 if (data) {
864 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700865 if (!secdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto out_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 error = security_sb_copy_data(type, data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700869 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
David Howells454e2392006-06-23 02:02:57 -0700873 error = type->get_sb(type, flags, name, data, mnt);
874 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 goto out_free_secdata;
David Howells454e2392006-06-23 02:02:57 -0700876
877 error = security_sb_kern_mount(mnt->mnt_sb, secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (error)
879 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700880
881 mnt->mnt_mountpoint = mnt->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 mnt->mnt_parent = mnt;
David Howells454e2392006-06-23 02:02:57 -0700883 up_write(&mnt->mnt_sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -0700884 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return mnt;
886out_sb:
David Howells454e2392006-06-23 02:02:57 -0700887 dput(mnt->mnt_root);
888 up_write(&mnt->mnt_sb->s_umount);
889 deactivate_super(mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890out_free_secdata:
891 free_secdata(secdata);
892out_mnt:
893 free_vfsmnt(mnt);
894out:
David Howells454e2392006-06-23 02:02:57 -0700895 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400898EXPORT_SYMBOL_GPL(vfs_kern_mount);
899
900struct vfsmount *
901do_kern_mount(const char *fstype, int flags, const char *name, void *data)
902{
903 struct file_system_type *type = get_fs_type(fstype);
904 struct vfsmount *mnt;
905 if (!type)
906 return ERR_PTR(-ENODEV);
907 mnt = vfs_kern_mount(type, flags, name, data);
908 put_filesystem(type);
909 return mnt;
910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912struct vfsmount *kern_mount(struct file_system_type *type)
913{
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400914 return vfs_kern_mount(type, 0, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917EXPORT_SYMBOL(kern_mount);