blob: 6d4e8174b6db4fd7548b94403aafdd49043966f9 [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 */
Ingo Molnarcf516242006-07-03 00:25:27 -070056static struct super_block *alloc_super(struct file_system_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
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);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070075 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -070076 /*
77 * The locking rules for s_lock are up to the
78 * filesystem. For example ext3fs has different
79 * lock ordering than usbfs:
80 */
81 lockdep_set_class(&s->s_lock, &type->s_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 down_write(&s->s_umount);
83 s->s_count = S_BIAS;
84 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -080085 mutex_init(&s->s_vfs_rename_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -080086 mutex_init(&s->s_dquot.dqio_mutex);
87 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 init_rwsem(&s->s_dquot.dqptr_sem);
89 init_waitqueue_head(&s->s_wait_unfrozen);
90 s->s_maxbytes = MAX_NON_LFS;
91 s->dq_op = sb_dquot_ops;
92 s->s_qcop = sb_quotactl_ops;
93 s->s_op = &default_op;
94 s->s_time_gran = 1000000000;
95 }
96out:
97 return s;
98}
99
100/**
101 * destroy_super - frees a superblock
102 * @s: superblock to free
103 *
104 * Frees a superblock.
105 */
106static inline void destroy_super(struct super_block *s)
107{
108 security_sb_free(s);
109 kfree(s);
110}
111
112/* Superblock refcounting */
113
114/*
115 * Drop a superblock's refcount. Returns non-zero if the superblock was
116 * destroyed. The caller must hold sb_lock.
117 */
118int __put_super(struct super_block *sb)
119{
120 int ret = 0;
121
122 if (!--sb->s_count) {
123 destroy_super(sb);
124 ret = 1;
125 }
126 return ret;
127}
128
129/*
130 * Drop a superblock's refcount.
131 * Returns non-zero if the superblock is about to be destroyed and
132 * at least is already removed from super_blocks list, so if we are
133 * making a loop through super blocks then we need to restart.
134 * The caller must hold sb_lock.
135 */
136int __put_super_and_need_restart(struct super_block *sb)
137{
138 /* check for race with generic_shutdown_super() */
139 if (list_empty(&sb->s_list)) {
140 /* super block is removed, need to restart... */
141 __put_super(sb);
142 return 1;
143 }
144 /* can't be the last, since s_list is still in use */
145 sb->s_count--;
146 BUG_ON(sb->s_count == 0);
147 return 0;
148}
149
150/**
151 * put_super - drop a temporary reference to superblock
152 * @sb: superblock in question
153 *
154 * Drops a temporary reference, frees superblock if there's no
155 * references left.
156 */
157static void put_super(struct super_block *sb)
158{
159 spin_lock(&sb_lock);
160 __put_super(sb);
161 spin_unlock(&sb_lock);
162}
163
164
165/**
166 * deactivate_super - drop an active reference to superblock
167 * @s: superblock to deactivate
168 *
169 * Drops an active reference to superblock, acquiring a temprory one if
170 * there is no active references left. In that case we lock superblock,
171 * tell fs driver to shut it down and drop the temporary reference we
172 * had just acquired.
173 */
174void deactivate_super(struct super_block *s)
175{
176 struct file_system_type *fs = s->s_type;
177 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
178 s->s_count -= S_BIAS-1;
179 spin_unlock(&sb_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500180 DQUOT_OFF(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 down_write(&s->s_umount);
182 fs->kill_sb(s);
183 put_filesystem(fs);
184 put_super(s);
185 }
186}
187
188EXPORT_SYMBOL(deactivate_super);
189
190/**
191 * grab_super - acquire an active reference
192 * @s: reference we are trying to make active
193 *
194 * Tries to acquire an active reference. grab_super() is used when we
195 * had just found a superblock in super_blocks or fs_type->fs_supers
196 * and want to turn it into a full-blown active reference. grab_super()
197 * is called with sb_lock held and drops it. Returns 1 in case of
198 * success, 0 if we had failed (superblock contents was already dead or
199 * dying when grab_super() had been called).
200 */
201static int grab_super(struct super_block *s)
202{
203 s->s_count++;
204 spin_unlock(&sb_lock);
205 down_write(&s->s_umount);
206 if (s->s_root) {
207 spin_lock(&sb_lock);
208 if (s->s_count > S_BIAS) {
209 atomic_inc(&s->s_active);
210 s->s_count--;
211 spin_unlock(&sb_lock);
212 return 1;
213 }
214 spin_unlock(&sb_lock);
215 }
216 up_write(&s->s_umount);
217 put_super(s);
218 yield();
219 return 0;
220}
221
222/**
223 * generic_shutdown_super - common helper for ->kill_sb()
224 * @sb: superblock to kill
225 *
226 * generic_shutdown_super() does all fs-independent work on superblock
227 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
228 * that need destruction out of superblock, call generic_shutdown_super()
229 * and release aforementioned objects. Note: dentries and inodes _are_
230 * taken care of and do not need specific handling.
231 */
232void generic_shutdown_super(struct super_block *sb)
233{
234 struct dentry *root = sb->s_root;
235 struct super_operations *sop = sb->s_op;
236
237 if (root) {
238 sb->s_root = NULL;
239 shrink_dcache_parent(root);
David Howells454e2392006-06-23 02:02:57 -0700240 shrink_dcache_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 dput(root);
242 fsync_super(sb);
243 lock_super(sb);
244 sb->s_flags &= ~MS_ACTIVE;
245 /* bad name - it should be evict_inodes() */
246 invalidate_inodes(sb);
247 lock_kernel();
248
249 if (sop->write_super && sb->s_dirt)
250 sop->write_super(sb);
251 if (sop->put_super)
252 sop->put_super(sb);
253
254 /* Forget any remaining inodes */
255 if (invalidate_inodes(sb)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800256 printk("VFS: Busy inodes after unmount of %s. "
257 "Self-destruct in 5 seconds. Have a nice day...\n",
258 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
261 unlock_kernel();
262 unlock_super(sb);
263 }
264 spin_lock(&sb_lock);
265 /* should be initialized for __put_super_and_need_restart() */
266 list_del_init(&sb->s_list);
267 list_del(&sb->s_instances);
268 spin_unlock(&sb_lock);
269 up_write(&sb->s_umount);
270}
271
272EXPORT_SYMBOL(generic_shutdown_super);
273
274/**
275 * sget - find or create a superblock
276 * @type: filesystem type superblock should belong to
277 * @test: comparison callback
278 * @set: setup callback
279 * @data: argument to each of them
280 */
281struct super_block *sget(struct file_system_type *type,
282 int (*test)(struct super_block *,void *),
283 int (*set)(struct super_block *,void *),
284 void *data)
285{
286 struct super_block *s = NULL;
287 struct list_head *p;
288 int err;
289
290retry:
291 spin_lock(&sb_lock);
292 if (test) list_for_each(p, &type->fs_supers) {
293 struct super_block *old;
294 old = list_entry(p, struct super_block, s_instances);
295 if (!test(old, data))
296 continue;
297 if (!grab_super(old))
298 goto retry;
299 if (s)
300 destroy_super(s);
301 return old;
302 }
303 if (!s) {
304 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700305 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!s)
307 return ERR_PTR(-ENOMEM);
308 goto retry;
309 }
310
311 err = set(s, data);
312 if (err) {
313 spin_unlock(&sb_lock);
314 destroy_super(s);
315 return ERR_PTR(err);
316 }
317 s->s_type = type;
318 strlcpy(s->s_id, type->name, sizeof(s->s_id));
319 list_add_tail(&s->s_list, &super_blocks);
320 list_add(&s->s_instances, &type->fs_supers);
321 spin_unlock(&sb_lock);
322 get_filesystem(type);
323 return s;
324}
325
326EXPORT_SYMBOL(sget);
327
328void drop_super(struct super_block *sb)
329{
330 up_read(&sb->s_umount);
331 put_super(sb);
332}
333
334EXPORT_SYMBOL(drop_super);
335
336static inline void write_super(struct super_block *sb)
337{
338 lock_super(sb);
339 if (sb->s_root && sb->s_dirt)
340 if (sb->s_op->write_super)
341 sb->s_op->write_super(sb);
342 unlock_super(sb);
343}
344
345/*
346 * Note: check the dirty flag before waiting, so we don't
347 * hold up the sync while mounting a device. (The newly
348 * mounted device won't need syncing.)
349 */
350void sync_supers(void)
351{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700352 struct super_block *sb;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700355restart:
356 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (sb->s_dirt) {
358 sb->s_count++;
359 spin_unlock(&sb_lock);
360 down_read(&sb->s_umount);
361 write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700362 up_read(&sb->s_umount);
363 spin_lock(&sb_lock);
364 if (__put_super_and_need_restart(sb))
365 goto restart;
366 }
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_unlock(&sb_lock);
369}
370
371/*
372 * Call the ->sync_fs super_op against all filesytems which are r/w and
373 * which implement it.
374 *
375 * This operation is careful to avoid the livelock which could easily happen
376 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
377 * is used only here. We set it against all filesystems and then clear it as
378 * we sync them. So redirtied filesystems are skipped.
379 *
380 * But if process A is currently running sync_filesytems and then process B
381 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
382 * flags again, which will cause process A to resync everything. Fix that with
383 * a local mutex.
384 *
385 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
386 */
387void sync_filesystems(int wait)
388{
389 struct super_block *sb;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800390 static DEFINE_MUTEX(mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800392 mutex_lock(&mutex); /* Could be down_interruptible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700394 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!sb->s_op->sync_fs)
396 continue;
397 if (sb->s_flags & MS_RDONLY)
398 continue;
399 sb->s_need_sync_fs = 1;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402restart:
Kirill Korotaev618f0632005-06-23 00:09:54 -0700403 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!sb->s_need_sync_fs)
405 continue;
406 sb->s_need_sync_fs = 0;
407 if (sb->s_flags & MS_RDONLY)
408 continue; /* hm. Was remounted r/o meanwhile */
409 sb->s_count++;
410 spin_unlock(&sb_lock);
411 down_read(&sb->s_umount);
412 if (sb->s_root && (wait || sb->s_dirt))
413 sb->s_op->sync_fs(sb, wait);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700414 up_read(&sb->s_umount);
415 /* restart only when sb is no longer on the list */
416 spin_lock(&sb_lock);
417 if (__put_super_and_need_restart(sb))
418 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 spin_unlock(&sb_lock);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800421 mutex_unlock(&mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/**
425 * get_super - get the superblock of a device
426 * @bdev: device to get the superblock for
427 *
428 * Scans the superblock list and finds the superblock of the file system
429 * mounted on the device given. %NULL is returned if no match is found.
430 */
431
432struct super_block * get_super(struct block_device *bdev)
433{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700434 struct super_block *sb;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!bdev)
437 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700440rescan:
441 list_for_each_entry(sb, &super_blocks, s_list) {
442 if (sb->s_bdev == bdev) {
443 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700445 down_read(&sb->s_umount);
446 if (sb->s_root)
447 return sb;
448 up_read(&sb->s_umount);
449 /* restart only when sb is no longer on the list */
450 spin_lock(&sb_lock);
451 if (__put_super_and_need_restart(sb))
452 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
455 spin_unlock(&sb_lock);
456 return NULL;
457}
458
459EXPORT_SYMBOL(get_super);
460
461struct super_block * user_get_super(dev_t dev)
462{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700463 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700466rescan:
467 list_for_each_entry(sb, &super_blocks, s_list) {
468 if (sb->s_dev == dev) {
469 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700471 down_read(&sb->s_umount);
472 if (sb->s_root)
473 return sb;
474 up_read(&sb->s_umount);
475 /* restart only when sb is no longer on the list */
476 spin_lock(&sb_lock);
477 if (__put_super_and_need_restart(sb))
478 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 }
481 spin_unlock(&sb_lock);
482 return NULL;
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
486{
487 struct super_block *s;
488 struct ustat tmp;
489 struct kstatfs sbuf;
490 int err = -EINVAL;
491
492 s = user_get_super(new_decode_dev(dev));
493 if (s == NULL)
494 goto out;
David Howells726c3342006-06-23 02:02:58 -0700495 err = vfs_statfs(s->s_root, &sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 drop_super(s);
497 if (err)
498 goto out;
499
500 memset(&tmp,0,sizeof(struct ustat));
501 tmp.f_tfree = sbuf.f_bfree;
502 tmp.f_tinode = sbuf.f_ffree;
503
504 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
505out:
506 return err;
507}
508
509/**
510 * mark_files_ro
511 * @sb: superblock in question
512 *
513 * All files are marked read/only. We don't care about pending
514 * delete files so this should be used in 'force' mode only
515 */
516
517static void mark_files_ro(struct super_block *sb)
518{
519 struct file *f;
520
521 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800522 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
524 f->f_mode &= ~FMODE_WRITE;
525 }
526 file_list_unlock();
527}
528
529/**
530 * do_remount_sb - asks filesystem to change mount options.
531 * @sb: superblock in question
532 * @flags: numeric part of options
533 * @data: the rest of options
534 * @force: whether or not to force the change
535 *
536 * Alters the mount options of a mounted file system.
537 */
538int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
539{
540 int retval;
541
542 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
543 return -EACCES;
544 if (flags & MS_RDONLY)
545 acct_auto_close(sb);
546 shrink_dcache_sb(sb);
547 fsync_super(sb);
548
549 /* If we are remounting RDONLY and current sb is read/write,
550 make sure there are no rw files opened */
551 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
552 if (force)
553 mark_files_ro(sb);
554 else if (!fs_may_remount_ro(sb))
555 return -EBUSY;
556 }
557
558 if (sb->s_op->remount_fs) {
559 lock_super(sb);
560 retval = sb->s_op->remount_fs(sb, &flags, data);
561 unlock_super(sb);
562 if (retval)
563 return retval;
564 }
565 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
566 return 0;
567}
568
569static void do_emergency_remount(unsigned long foo)
570{
571 struct super_block *sb;
572
573 spin_lock(&sb_lock);
574 list_for_each_entry(sb, &super_blocks, s_list) {
575 sb->s_count++;
576 spin_unlock(&sb_lock);
577 down_read(&sb->s_umount);
578 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
579 /*
580 * ->remount_fs needs lock_kernel().
581 *
582 * What lock protects sb->s_flags??
583 */
584 lock_kernel();
585 do_remount_sb(sb, MS_RDONLY, NULL, 1);
586 unlock_kernel();
587 }
588 drop_super(sb);
589 spin_lock(&sb_lock);
590 }
591 spin_unlock(&sb_lock);
592 printk("Emergency Remount complete\n");
593}
594
595void emergency_remount(void)
596{
597 pdflush_operation(do_emergency_remount, 0);
598}
599
600/*
601 * Unnamed block devices are dummy devices used by virtual
602 * filesystems which don't use real block-devices. -- jrs
603 */
604
605static struct idr unnamed_dev_idr;
606static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
607
608int set_anon_super(struct super_block *s, void *data)
609{
610 int dev;
611 int error;
612
613 retry:
614 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
615 return -ENOMEM;
616 spin_lock(&unnamed_dev_lock);
617 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
618 spin_unlock(&unnamed_dev_lock);
619 if (error == -EAGAIN)
620 /* We raced and lost with another CPU. */
621 goto retry;
622 else if (error)
623 return -EAGAIN;
624
625 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
626 spin_lock(&unnamed_dev_lock);
627 idr_remove(&unnamed_dev_idr, dev);
628 spin_unlock(&unnamed_dev_lock);
629 return -EMFILE;
630 }
631 s->s_dev = MKDEV(0, dev & MINORMASK);
632 return 0;
633}
634
635EXPORT_SYMBOL(set_anon_super);
636
637void kill_anon_super(struct super_block *sb)
638{
639 int slot = MINOR(sb->s_dev);
640
641 generic_shutdown_super(sb);
642 spin_lock(&unnamed_dev_lock);
643 idr_remove(&unnamed_dev_idr, slot);
644 spin_unlock(&unnamed_dev_lock);
645}
646
647EXPORT_SYMBOL(kill_anon_super);
648
649void __init unnamed_dev_init(void)
650{
651 idr_init(&unnamed_dev_idr);
652}
653
654void kill_litter_super(struct super_block *sb)
655{
656 if (sb->s_root)
657 d_genocide(sb->s_root);
658 kill_anon_super(sb);
659}
660
661EXPORT_SYMBOL(kill_litter_super);
662
663static int set_bdev_super(struct super_block *s, void *data)
664{
665 s->s_bdev = data;
666 s->s_dev = s->s_bdev->bd_dev;
667 return 0;
668}
669
670static int test_bdev_super(struct super_block *s, void *data)
671{
672 return (void *)s->s_bdev == data;
673}
674
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800675static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
676{
677 if (bdev->bd_disk) {
678 if (bdev->bd_part)
679 kobject_uevent(&bdev->bd_part->kobj, action);
680 else
681 kobject_uevent(&bdev->bd_disk->kobj, action);
682 }
683}
684
David Howells454e2392006-06-23 02:02:57 -0700685int get_sb_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int flags, const char *dev_name, void *data,
David Howells454e2392006-06-23 02:02:57 -0700687 int (*fill_super)(struct super_block *, void *, int),
688 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct block_device *bdev;
691 struct super_block *s;
692 int error = 0;
693
694 bdev = open_bdev_excl(dev_name, flags, fs_type);
695 if (IS_ERR(bdev))
David Howells454e2392006-06-23 02:02:57 -0700696 return PTR_ERR(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /*
699 * once the super is inserted into the list by sget, s_umount
700 * will protect the lockfs code from trying to start a snapshot
701 * while we are mounting
702 */
Arjan van de Venc039e312006-03-23 03:00:28 -0800703 mutex_lock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800705 mutex_unlock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700707 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (s->s_root) {
710 if ((flags ^ s->s_flags) & MS_RDONLY) {
711 up_write(&s->s_umount);
712 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700713 error = -EBUSY;
714 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
David Howells454e2392006-06-23 02:02:57 -0700716
717 close_bdev_excl(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
719 char b[BDEVNAME_SIZE];
720
721 s->s_flags = flags;
722 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800723 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800724 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (error) {
726 up_write(&s->s_umount);
727 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700728 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800729 }
David Howells454e2392006-06-23 02:02:57 -0700730
731 s->s_flags |= MS_ACTIVE;
732 bdev_uevent(bdev, KOBJ_MOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
David Howells454e2392006-06-23 02:02:57 -0700735 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
David Howells454e2392006-06-23 02:02:57 -0700737error_s:
738 error = PTR_ERR(s);
739error_bdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 close_bdev_excl(bdev);
David Howells454e2392006-06-23 02:02:57 -0700741error:
742 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745EXPORT_SYMBOL(get_sb_bdev);
746
747void kill_block_super(struct super_block *sb)
748{
749 struct block_device *bdev = sb->s_bdev;
750
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800751 bdev_uevent(bdev, KOBJ_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 generic_shutdown_super(sb);
753 sync_blockdev(bdev);
754 close_bdev_excl(bdev);
755}
756
757EXPORT_SYMBOL(kill_block_super);
758
David Howells454e2392006-06-23 02:02:57 -0700759int get_sb_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700761 int (*fill_super)(struct super_block *, void *, int),
762 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 int error;
765 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
766
767 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700768 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 s->s_flags = flags;
771
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800772 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (error) {
774 up_write(&s->s_umount);
775 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700776 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 s->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700779 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782EXPORT_SYMBOL(get_sb_nodev);
783
784static int compare_single(struct super_block *s, void *p)
785{
786 return 1;
787}
788
David Howells454e2392006-06-23 02:02:57 -0700789int get_sb_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700791 int (*fill_super)(struct super_block *, void *, int),
792 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct super_block *s;
795 int error;
796
797 s = sget(fs_type, compare_single, set_anon_super, NULL);
798 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700799 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!s->s_root) {
801 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800802 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (error) {
804 up_write(&s->s_umount);
805 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700806 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 s->s_flags |= MS_ACTIVE;
809 }
810 do_remount_sb(s, flags, data, 0);
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_single);
815
816struct vfsmount *
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400817vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 char *secdata = NULL;
David Howells454e2392006-06-23 02:02:57 -0700821 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (!type)
824 return ERR_PTR(-ENODEV);
825
David Howells454e2392006-06-23 02:02:57 -0700826 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 mnt = alloc_vfsmnt(name);
828 if (!mnt)
829 goto out;
830
831 if (data) {
832 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700833 if (!secdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto out_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 error = security_sb_copy_data(type, data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700837 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
David Howells454e2392006-06-23 02:02:57 -0700841 error = type->get_sb(type, flags, name, data, mnt);
842 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 goto out_free_secdata;
David Howells454e2392006-06-23 02:02:57 -0700844
845 error = security_sb_kern_mount(mnt->mnt_sb, secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (error)
847 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700848
849 mnt->mnt_mountpoint = mnt->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 mnt->mnt_parent = mnt;
David Howells454e2392006-06-23 02:02:57 -0700851 up_write(&mnt->mnt_sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -0700852 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return mnt;
854out_sb:
David Howells454e2392006-06-23 02:02:57 -0700855 dput(mnt->mnt_root);
856 up_write(&mnt->mnt_sb->s_umount);
857 deactivate_super(mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858out_free_secdata:
859 free_secdata(secdata);
860out_mnt:
861 free_vfsmnt(mnt);
862out:
David Howells454e2392006-06-23 02:02:57 -0700863 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400866EXPORT_SYMBOL_GPL(vfs_kern_mount);
867
868struct vfsmount *
869do_kern_mount(const char *fstype, int flags, const char *name, void *data)
870{
871 struct file_system_type *type = get_fs_type(fstype);
872 struct vfsmount *mnt;
873 if (!type)
874 return ERR_PTR(-ENODEV);
875 mnt = vfs_kern_mount(type, flags, name, data);
876 put_filesystem(type);
877 return mnt;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880struct vfsmount *kern_mount(struct file_system_type *type)
881{
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400882 return vfs_kern_mount(type, 0, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885EXPORT_SYMBOL(kern_mount);