blob: 5a4fe8be462ad998d0f32cdf270f710d210d6326 [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);
Ingo Molnarcf516242006-07-03 00:25:27 -070075 /*
76 * The locking rules for s_lock are up to the
77 * filesystem. For example ext3fs has different
78 * lock ordering than usbfs:
79 */
80 lockdep_set_class(&s->s_lock, &type->s_lock_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 down_write(&s->s_umount);
82 s->s_count = S_BIAS;
83 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -080084 mutex_init(&s->s_vfs_rename_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -080085 mutex_init(&s->s_dquot.dqio_mutex);
86 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 init_rwsem(&s->s_dquot.dqptr_sem);
88 init_waitqueue_head(&s->s_wait_unfrozen);
89 s->s_maxbytes = MAX_NON_LFS;
90 s->dq_op = sb_dquot_ops;
91 s->s_qcop = sb_quotactl_ops;
92 s->s_op = &default_op;
93 s->s_time_gran = 1000000000;
94 }
95out:
96 return s;
97}
98
99/**
100 * destroy_super - frees a superblock
101 * @s: superblock to free
102 *
103 * Frees a superblock.
104 */
105static inline void destroy_super(struct super_block *s)
106{
107 security_sb_free(s);
108 kfree(s);
109}
110
111/* Superblock refcounting */
112
113/*
114 * Drop a superblock's refcount. Returns non-zero if the superblock was
115 * destroyed. The caller must hold sb_lock.
116 */
117int __put_super(struct super_block *sb)
118{
119 int ret = 0;
120
121 if (!--sb->s_count) {
122 destroy_super(sb);
123 ret = 1;
124 }
125 return ret;
126}
127
128/*
129 * Drop a superblock's refcount.
130 * Returns non-zero if the superblock is about to be destroyed and
131 * at least is already removed from super_blocks list, so if we are
132 * making a loop through super blocks then we need to restart.
133 * The caller must hold sb_lock.
134 */
135int __put_super_and_need_restart(struct super_block *sb)
136{
137 /* check for race with generic_shutdown_super() */
138 if (list_empty(&sb->s_list)) {
139 /* super block is removed, need to restart... */
140 __put_super(sb);
141 return 1;
142 }
143 /* can't be the last, since s_list is still in use */
144 sb->s_count--;
145 BUG_ON(sb->s_count == 0);
146 return 0;
147}
148
149/**
150 * put_super - drop a temporary reference to superblock
151 * @sb: superblock in question
152 *
153 * Drops a temporary reference, frees superblock if there's no
154 * references left.
155 */
156static void put_super(struct super_block *sb)
157{
158 spin_lock(&sb_lock);
159 __put_super(sb);
160 spin_unlock(&sb_lock);
161}
162
163
164/**
165 * deactivate_super - drop an active reference to superblock
166 * @s: superblock to deactivate
167 *
168 * Drops an active reference to superblock, acquiring a temprory one if
169 * there is no active references left. In that case we lock superblock,
170 * tell fs driver to shut it down and drop the temporary reference we
171 * had just acquired.
172 */
173void deactivate_super(struct super_block *s)
174{
175 struct file_system_type *fs = s->s_type;
176 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
177 s->s_count -= S_BIAS-1;
178 spin_unlock(&sb_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500179 DQUOT_OFF(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 down_write(&s->s_umount);
181 fs->kill_sb(s);
182 put_filesystem(fs);
183 put_super(s);
184 }
185}
186
187EXPORT_SYMBOL(deactivate_super);
188
189/**
190 * grab_super - acquire an active reference
191 * @s: reference we are trying to make active
192 *
193 * Tries to acquire an active reference. grab_super() is used when we
194 * had just found a superblock in super_blocks or fs_type->fs_supers
195 * and want to turn it into a full-blown active reference. grab_super()
196 * is called with sb_lock held and drops it. Returns 1 in case of
197 * success, 0 if we had failed (superblock contents was already dead or
198 * dying when grab_super() had been called).
199 */
200static int grab_super(struct super_block *s)
201{
202 s->s_count++;
203 spin_unlock(&sb_lock);
204 down_write(&s->s_umount);
205 if (s->s_root) {
206 spin_lock(&sb_lock);
207 if (s->s_count > S_BIAS) {
208 atomic_inc(&s->s_active);
209 s->s_count--;
210 spin_unlock(&sb_lock);
211 return 1;
212 }
213 spin_unlock(&sb_lock);
214 }
215 up_write(&s->s_umount);
216 put_super(s);
217 yield();
218 return 0;
219}
220
221/**
222 * generic_shutdown_super - common helper for ->kill_sb()
223 * @sb: superblock to kill
224 *
225 * generic_shutdown_super() does all fs-independent work on superblock
226 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
227 * that need destruction out of superblock, call generic_shutdown_super()
228 * and release aforementioned objects. Note: dentries and inodes _are_
229 * taken care of and do not need specific handling.
230 */
231void generic_shutdown_super(struct super_block *sb)
232{
233 struct dentry *root = sb->s_root;
234 struct super_operations *sop = sb->s_op;
235
236 if (root) {
237 sb->s_root = NULL;
238 shrink_dcache_parent(root);
David Howells454e2392006-06-23 02:02:57 -0700239 shrink_dcache_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 dput(root);
241 fsync_super(sb);
242 lock_super(sb);
243 sb->s_flags &= ~MS_ACTIVE;
244 /* bad name - it should be evict_inodes() */
245 invalidate_inodes(sb);
246 lock_kernel();
247
248 if (sop->write_super && sb->s_dirt)
249 sop->write_super(sb);
250 if (sop->put_super)
251 sop->put_super(sb);
252
253 /* Forget any remaining inodes */
254 if (invalidate_inodes(sb)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800255 printk("VFS: Busy inodes after unmount of %s. "
256 "Self-destruct in 5 seconds. Have a nice day...\n",
257 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
260 unlock_kernel();
261 unlock_super(sb);
262 }
263 spin_lock(&sb_lock);
264 /* should be initialized for __put_super_and_need_restart() */
265 list_del_init(&sb->s_list);
266 list_del(&sb->s_instances);
267 spin_unlock(&sb_lock);
268 up_write(&sb->s_umount);
269}
270
271EXPORT_SYMBOL(generic_shutdown_super);
272
273/**
274 * sget - find or create a superblock
275 * @type: filesystem type superblock should belong to
276 * @test: comparison callback
277 * @set: setup callback
278 * @data: argument to each of them
279 */
280struct super_block *sget(struct file_system_type *type,
281 int (*test)(struct super_block *,void *),
282 int (*set)(struct super_block *,void *),
283 void *data)
284{
285 struct super_block *s = NULL;
286 struct list_head *p;
287 int err;
288
289retry:
290 spin_lock(&sb_lock);
291 if (test) list_for_each(p, &type->fs_supers) {
292 struct super_block *old;
293 old = list_entry(p, struct super_block, s_instances);
294 if (!test(old, data))
295 continue;
296 if (!grab_super(old))
297 goto retry;
298 if (s)
299 destroy_super(s);
300 return old;
301 }
302 if (!s) {
303 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700304 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!s)
306 return ERR_PTR(-ENOMEM);
307 goto retry;
308 }
309
310 err = set(s, data);
311 if (err) {
312 spin_unlock(&sb_lock);
313 destroy_super(s);
314 return ERR_PTR(err);
315 }
316 s->s_type = type;
317 strlcpy(s->s_id, type->name, sizeof(s->s_id));
318 list_add_tail(&s->s_list, &super_blocks);
319 list_add(&s->s_instances, &type->fs_supers);
320 spin_unlock(&sb_lock);
321 get_filesystem(type);
322 return s;
323}
324
325EXPORT_SYMBOL(sget);
326
327void drop_super(struct super_block *sb)
328{
329 up_read(&sb->s_umount);
330 put_super(sb);
331}
332
333EXPORT_SYMBOL(drop_super);
334
335static inline void write_super(struct super_block *sb)
336{
337 lock_super(sb);
338 if (sb->s_root && sb->s_dirt)
339 if (sb->s_op->write_super)
340 sb->s_op->write_super(sb);
341 unlock_super(sb);
342}
343
344/*
345 * Note: check the dirty flag before waiting, so we don't
346 * hold up the sync while mounting a device. (The newly
347 * mounted device won't need syncing.)
348 */
349void sync_supers(void)
350{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700351 struct super_block *sb;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700354restart:
355 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (sb->s_dirt) {
357 sb->s_count++;
358 spin_unlock(&sb_lock);
359 down_read(&sb->s_umount);
360 write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700361 up_read(&sb->s_umount);
362 spin_lock(&sb_lock);
363 if (__put_super_and_need_restart(sb))
364 goto restart;
365 }
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 spin_unlock(&sb_lock);
368}
369
370/*
371 * Call the ->sync_fs super_op against all filesytems which are r/w and
372 * which implement it.
373 *
374 * This operation is careful to avoid the livelock which could easily happen
375 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
376 * is used only here. We set it against all filesystems and then clear it as
377 * we sync them. So redirtied filesystems are skipped.
378 *
379 * But if process A is currently running sync_filesytems and then process B
380 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
381 * flags again, which will cause process A to resync everything. Fix that with
382 * a local mutex.
383 *
384 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
385 */
386void sync_filesystems(int wait)
387{
388 struct super_block *sb;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800389 static DEFINE_MUTEX(mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800391 mutex_lock(&mutex); /* Could be down_interruptible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700393 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!sb->s_op->sync_fs)
395 continue;
396 if (sb->s_flags & MS_RDONLY)
397 continue;
398 sb->s_need_sync_fs = 1;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401restart:
Kirill Korotaev618f0632005-06-23 00:09:54 -0700402 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!sb->s_need_sync_fs)
404 continue;
405 sb->s_need_sync_fs = 0;
406 if (sb->s_flags & MS_RDONLY)
407 continue; /* hm. Was remounted r/o meanwhile */
408 sb->s_count++;
409 spin_unlock(&sb_lock);
410 down_read(&sb->s_umount);
411 if (sb->s_root && (wait || sb->s_dirt))
412 sb->s_op->sync_fs(sb, wait);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700413 up_read(&sb->s_umount);
414 /* restart only when sb is no longer on the list */
415 spin_lock(&sb_lock);
416 if (__put_super_and_need_restart(sb))
417 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 spin_unlock(&sb_lock);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800420 mutex_unlock(&mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/**
424 * get_super - get the superblock of a device
425 * @bdev: device to get the superblock for
426 *
427 * Scans the superblock list and finds the superblock of the file system
428 * mounted on the device given. %NULL is returned if no match is found.
429 */
430
431struct super_block * get_super(struct block_device *bdev)
432{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700433 struct super_block *sb;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!bdev)
436 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700439rescan:
440 list_for_each_entry(sb, &super_blocks, s_list) {
441 if (sb->s_bdev == bdev) {
442 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700444 down_read(&sb->s_umount);
445 if (sb->s_root)
446 return sb;
447 up_read(&sb->s_umount);
448 /* restart only when sb is no longer on the list */
449 spin_lock(&sb_lock);
450 if (__put_super_and_need_restart(sb))
451 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 }
454 spin_unlock(&sb_lock);
455 return NULL;
456}
457
458EXPORT_SYMBOL(get_super);
459
460struct super_block * user_get_super(dev_t dev)
461{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700462 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700465rescan:
466 list_for_each_entry(sb, &super_blocks, s_list) {
467 if (sb->s_dev == dev) {
468 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700470 down_read(&sb->s_umount);
471 if (sb->s_root)
472 return sb;
473 up_read(&sb->s_umount);
474 /* restart only when sb is no longer on the list */
475 spin_lock(&sb_lock);
476 if (__put_super_and_need_restart(sb))
477 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 }
480 spin_unlock(&sb_lock);
481 return NULL;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
485{
486 struct super_block *s;
487 struct ustat tmp;
488 struct kstatfs sbuf;
489 int err = -EINVAL;
490
491 s = user_get_super(new_decode_dev(dev));
492 if (s == NULL)
493 goto out;
David Howells726c3342006-06-23 02:02:58 -0700494 err = vfs_statfs(s->s_root, &sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 drop_super(s);
496 if (err)
497 goto out;
498
499 memset(&tmp,0,sizeof(struct ustat));
500 tmp.f_tfree = sbuf.f_bfree;
501 tmp.f_tinode = sbuf.f_ffree;
502
503 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
504out:
505 return err;
506}
507
508/**
509 * mark_files_ro
510 * @sb: superblock in question
511 *
512 * All files are marked read/only. We don't care about pending
513 * delete files so this should be used in 'force' mode only
514 */
515
516static void mark_files_ro(struct super_block *sb)
517{
518 struct file *f;
519
520 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800521 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
523 f->f_mode &= ~FMODE_WRITE;
524 }
525 file_list_unlock();
526}
527
528/**
529 * do_remount_sb - asks filesystem to change mount options.
530 * @sb: superblock in question
531 * @flags: numeric part of options
532 * @data: the rest of options
533 * @force: whether or not to force the change
534 *
535 * Alters the mount options of a mounted file system.
536 */
537int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
538{
539 int retval;
540
541 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
542 return -EACCES;
543 if (flags & MS_RDONLY)
544 acct_auto_close(sb);
545 shrink_dcache_sb(sb);
546 fsync_super(sb);
547
548 /* If we are remounting RDONLY and current sb is read/write,
549 make sure there are no rw files opened */
550 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
551 if (force)
552 mark_files_ro(sb);
553 else if (!fs_may_remount_ro(sb))
554 return -EBUSY;
555 }
556
557 if (sb->s_op->remount_fs) {
558 lock_super(sb);
559 retval = sb->s_op->remount_fs(sb, &flags, data);
560 unlock_super(sb);
561 if (retval)
562 return retval;
563 }
564 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
565 return 0;
566}
567
568static void do_emergency_remount(unsigned long foo)
569{
570 struct super_block *sb;
571
572 spin_lock(&sb_lock);
573 list_for_each_entry(sb, &super_blocks, s_list) {
574 sb->s_count++;
575 spin_unlock(&sb_lock);
576 down_read(&sb->s_umount);
577 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
578 /*
579 * ->remount_fs needs lock_kernel().
580 *
581 * What lock protects sb->s_flags??
582 */
583 lock_kernel();
584 do_remount_sb(sb, MS_RDONLY, NULL, 1);
585 unlock_kernel();
586 }
587 drop_super(sb);
588 spin_lock(&sb_lock);
589 }
590 spin_unlock(&sb_lock);
591 printk("Emergency Remount complete\n");
592}
593
594void emergency_remount(void)
595{
596 pdflush_operation(do_emergency_remount, 0);
597}
598
599/*
600 * Unnamed block devices are dummy devices used by virtual
601 * filesystems which don't use real block-devices. -- jrs
602 */
603
604static struct idr unnamed_dev_idr;
605static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
606
607int set_anon_super(struct super_block *s, void *data)
608{
609 int dev;
610 int error;
611
612 retry:
613 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
614 return -ENOMEM;
615 spin_lock(&unnamed_dev_lock);
616 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
617 spin_unlock(&unnamed_dev_lock);
618 if (error == -EAGAIN)
619 /* We raced and lost with another CPU. */
620 goto retry;
621 else if (error)
622 return -EAGAIN;
623
624 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
625 spin_lock(&unnamed_dev_lock);
626 idr_remove(&unnamed_dev_idr, dev);
627 spin_unlock(&unnamed_dev_lock);
628 return -EMFILE;
629 }
630 s->s_dev = MKDEV(0, dev & MINORMASK);
631 return 0;
632}
633
634EXPORT_SYMBOL(set_anon_super);
635
636void kill_anon_super(struct super_block *sb)
637{
638 int slot = MINOR(sb->s_dev);
639
640 generic_shutdown_super(sb);
641 spin_lock(&unnamed_dev_lock);
642 idr_remove(&unnamed_dev_idr, slot);
643 spin_unlock(&unnamed_dev_lock);
644}
645
646EXPORT_SYMBOL(kill_anon_super);
647
648void __init unnamed_dev_init(void)
649{
650 idr_init(&unnamed_dev_idr);
651}
652
653void kill_litter_super(struct super_block *sb)
654{
655 if (sb->s_root)
656 d_genocide(sb->s_root);
657 kill_anon_super(sb);
658}
659
660EXPORT_SYMBOL(kill_litter_super);
661
662static int set_bdev_super(struct super_block *s, void *data)
663{
664 s->s_bdev = data;
665 s->s_dev = s->s_bdev->bd_dev;
666 return 0;
667}
668
669static int test_bdev_super(struct super_block *s, void *data)
670{
671 return (void *)s->s_bdev == data;
672}
673
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800674static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
675{
676 if (bdev->bd_disk) {
677 if (bdev->bd_part)
678 kobject_uevent(&bdev->bd_part->kobj, action);
679 else
680 kobject_uevent(&bdev->bd_disk->kobj, action);
681 }
682}
683
David Howells454e2392006-06-23 02:02:57 -0700684int get_sb_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int flags, const char *dev_name, void *data,
David Howells454e2392006-06-23 02:02:57 -0700686 int (*fill_super)(struct super_block *, void *, int),
687 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct block_device *bdev;
690 struct super_block *s;
691 int error = 0;
692
693 bdev = open_bdev_excl(dev_name, flags, fs_type);
694 if (IS_ERR(bdev))
David Howells454e2392006-06-23 02:02:57 -0700695 return PTR_ERR(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /*
698 * once the super is inserted into the list by sget, s_umount
699 * will protect the lockfs code from trying to start a snapshot
700 * while we are mounting
701 */
Arjan van de Venc039e312006-03-23 03:00:28 -0800702 mutex_lock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800704 mutex_unlock(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700706 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (s->s_root) {
709 if ((flags ^ s->s_flags) & MS_RDONLY) {
710 up_write(&s->s_umount);
711 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700712 error = -EBUSY;
713 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
David Howells454e2392006-06-23 02:02:57 -0700715
716 close_bdev_excl(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 } else {
718 char b[BDEVNAME_SIZE];
719
720 s->s_flags = flags;
721 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800722 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800723 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (error) {
725 up_write(&s->s_umount);
726 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700727 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800728 }
David Howells454e2392006-06-23 02:02:57 -0700729
730 s->s_flags |= MS_ACTIVE;
731 bdev_uevent(bdev, KOBJ_MOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
David Howells454e2392006-06-23 02:02:57 -0700734 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
David Howells454e2392006-06-23 02:02:57 -0700736error_s:
737 error = PTR_ERR(s);
738error_bdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 close_bdev_excl(bdev);
David Howells454e2392006-06-23 02:02:57 -0700740error:
741 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744EXPORT_SYMBOL(get_sb_bdev);
745
746void kill_block_super(struct super_block *sb)
747{
748 struct block_device *bdev = sb->s_bdev;
749
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800750 bdev_uevent(bdev, KOBJ_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 generic_shutdown_super(sb);
752 sync_blockdev(bdev);
753 close_bdev_excl(bdev);
754}
755
756EXPORT_SYMBOL(kill_block_super);
757
David Howells454e2392006-06-23 02:02:57 -0700758int get_sb_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700760 int (*fill_super)(struct super_block *, void *, int),
761 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 int error;
764 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
765
766 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700767 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 s->s_flags = flags;
770
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800771 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (error) {
773 up_write(&s->s_umount);
774 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700775 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 s->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700778 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781EXPORT_SYMBOL(get_sb_nodev);
782
783static int compare_single(struct super_block *s, void *p)
784{
785 return 1;
786}
787
David Howells454e2392006-06-23 02:02:57 -0700788int get_sb_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700790 int (*fill_super)(struct super_block *, void *, int),
791 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct super_block *s;
794 int error;
795
796 s = sget(fs_type, compare_single, set_anon_super, NULL);
797 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700798 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!s->s_root) {
800 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800801 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (error) {
803 up_write(&s->s_umount);
804 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700805 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807 s->s_flags |= MS_ACTIVE;
808 }
809 do_remount_sb(s, flags, data, 0);
David Howells454e2392006-06-23 02:02:57 -0700810 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813EXPORT_SYMBOL(get_sb_single);
814
815struct vfsmount *
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400816vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 char *secdata = NULL;
David Howells454e2392006-06-23 02:02:57 -0700820 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (!type)
823 return ERR_PTR(-ENODEV);
824
David Howells454e2392006-06-23 02:02:57 -0700825 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 mnt = alloc_vfsmnt(name);
827 if (!mnt)
828 goto out;
829
830 if (data) {
831 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700832 if (!secdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto out_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 error = security_sb_copy_data(type, data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700836 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
David Howells454e2392006-06-23 02:02:57 -0700840 error = type->get_sb(type, flags, name, data, mnt);
841 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto out_free_secdata;
David Howells454e2392006-06-23 02:02:57 -0700843
844 error = security_sb_kern_mount(mnt->mnt_sb, secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (error)
846 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700847
848 mnt->mnt_mountpoint = mnt->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 mnt->mnt_parent = mnt;
David Howells454e2392006-06-23 02:02:57 -0700850 up_write(&mnt->mnt_sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -0700851 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return mnt;
853out_sb:
David Howells454e2392006-06-23 02:02:57 -0700854 dput(mnt->mnt_root);
855 up_write(&mnt->mnt_sb->s_umount);
856 deactivate_super(mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857out_free_secdata:
858 free_secdata(secdata);
859out_mnt:
860 free_vfsmnt(mnt);
861out:
David Howells454e2392006-06-23 02:02:57 -0700862 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400865EXPORT_SYMBOL_GPL(vfs_kern_mount);
866
867struct vfsmount *
868do_kern_mount(const char *fstype, int flags, const char *name, void *data)
869{
870 struct file_system_type *type = get_fs_type(fstype);
871 struct vfsmount *mnt;
872 if (!type)
873 return ERR_PTR(-ENODEV);
874 mnt = vfs_kern_mount(type, flags, name, data);
875 put_filesystem(type);
876 return mnt;
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879struct vfsmount *kern_mount(struct file_system_type *type)
880{
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400881 return vfs_kern_mount(type, 0, type->name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884EXPORT_SYMBOL(kern_mount);