blob: 8349ed6b1412aa13e9f23d17fad91e66c9ec1915 [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:
Jan Engelhardt96de0e22007-10-19 23:21:04 +020018 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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>
Dave Hansen49e0d022008-02-15 14:37:32 -080040#include <linux/file.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080041#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
Al Viro6d59e7f2008-03-22 15:48:17 -040043#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046LIST_HEAD(super_blocks);
47DEFINE_SPINLOCK(sb_lock);
48
49/**
50 * alloc_super - create new superblock
Henrik Kretzschmarfe2bbc42006-09-06 00:03:41 -070051 * @type: filesystem type superblock should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
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);
Ken Chen0e0f4fc2007-10-16 23:30:38 -070069 INIT_LIST_HEAD(&s->s_more_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -070074 INIT_LIST_HEAD(&s->s_dentry_lru);
Arjan van de Venefaee192009-01-06 07:20:54 -080075 INIT_LIST_HEAD(&s->s_async_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080077 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070078 lockdep_set_class(&s->s_umount, &type->s_umount_key);
Ingo Molnarcf516242006-07-03 00:25:27 -070079 /*
80 * The locking rules for s_lock are up to the
81 * filesystem. For example ext3fs has different
82 * lock ordering than usbfs:
83 */
84 lockdep_set_class(&s->s_lock, &type->s_lock_key);
Peter Zijlstraada723d2009-02-18 14:48:30 -080085 /*
86 * sget() can have s_umount recursion.
87 *
88 * When it cannot find a suitable sb, it allocates a new
89 * one (this one), and tries again to find a suitable old
90 * one.
91 *
92 * In case that succeeds, it will acquire the s_umount
93 * lock of the old one. Since these are clearly distrinct
94 * locks, and this object isn't exposed yet, there's no
95 * risk of deadlocks.
96 *
97 * Annotate this by putting this lock in a different
98 * subclass.
99 */
100 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 s->s_count = S_BIAS;
102 atomic_set(&s->s_active, 1);
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800103 mutex_init(&s->s_vfs_rename_mutex);
Ingo Molnard3be9152006-03-23 03:00:29 -0800104 mutex_init(&s->s_dquot.dqio_mutex);
105 mutex_init(&s->s_dquot.dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 init_rwsem(&s->s_dquot.dqptr_sem);
107 init_waitqueue_head(&s->s_wait_unfrozen);
108 s->s_maxbytes = MAX_NON_LFS;
109 s->dq_op = sb_dquot_ops;
110 s->s_qcop = sb_quotactl_ops;
111 s->s_op = &default_op;
112 s->s_time_gran = 1000000000;
113 }
114out:
115 return s;
116}
117
118/**
119 * destroy_super - frees a superblock
120 * @s: superblock to free
121 *
122 * Frees a superblock.
123 */
124static inline void destroy_super(struct super_block *s)
125{
126 security_sb_free(s);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700127 kfree(s->s_subtype);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800128 kfree(s->s_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 kfree(s);
130}
131
132/* Superblock refcounting */
133
134/*
135 * Drop a superblock's refcount. Returns non-zero if the superblock was
136 * destroyed. The caller must hold sb_lock.
137 */
Adrian Bunk6b09ae62008-04-29 00:58:54 -0700138static int __put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int ret = 0;
141
142 if (!--sb->s_count) {
143 destroy_super(sb);
144 ret = 1;
145 }
146 return ret;
147}
148
149/*
150 * Drop a superblock's refcount.
151 * Returns non-zero if the superblock is about to be destroyed and
152 * at least is already removed from super_blocks list, so if we are
153 * making a loop through super blocks then we need to restart.
154 * The caller must hold sb_lock.
155 */
156int __put_super_and_need_restart(struct super_block *sb)
157{
158 /* check for race with generic_shutdown_super() */
159 if (list_empty(&sb->s_list)) {
160 /* super block is removed, need to restart... */
161 __put_super(sb);
162 return 1;
163 }
164 /* can't be the last, since s_list is still in use */
165 sb->s_count--;
166 BUG_ON(sb->s_count == 0);
167 return 0;
168}
169
170/**
171 * put_super - drop a temporary reference to superblock
172 * @sb: superblock in question
173 *
174 * Drops a temporary reference, frees superblock if there's no
175 * references left.
176 */
177static void put_super(struct super_block *sb)
178{
179 spin_lock(&sb_lock);
180 __put_super(sb);
181 spin_unlock(&sb_lock);
182}
183
184
185/**
186 * deactivate_super - drop an active reference to superblock
187 * @s: superblock to deactivate
188 *
189 * Drops an active reference to superblock, acquiring a temprory one if
190 * there is no active references left. In that case we lock superblock,
191 * tell fs driver to shut it down and drop the temporary reference we
192 * had just acquired.
193 */
194void deactivate_super(struct super_block *s)
195{
196 struct file_system_type *fs = s->s_type;
197 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
198 s->s_count -= S_BIAS-1;
199 spin_unlock(&sb_lock);
Jan Kara0ff5af82008-04-28 02:14:33 -0700200 DQUOT_OFF(s, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 down_write(&s->s_umount);
202 fs->kill_sb(s);
203 put_filesystem(fs);
204 put_super(s);
205 }
206}
207
208EXPORT_SYMBOL(deactivate_super);
209
210/**
211 * grab_super - acquire an active reference
212 * @s: reference we are trying to make active
213 *
214 * Tries to acquire an active reference. grab_super() is used when we
215 * had just found a superblock in super_blocks or fs_type->fs_supers
216 * and want to turn it into a full-blown active reference. grab_super()
217 * is called with sb_lock held and drops it. Returns 1 in case of
218 * success, 0 if we had failed (superblock contents was already dead or
219 * dying when grab_super() had been called).
220 */
Josh Triplett9c4dbee2006-09-29 01:59:29 -0700221static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 s->s_count++;
224 spin_unlock(&sb_lock);
225 down_write(&s->s_umount);
226 if (s->s_root) {
227 spin_lock(&sb_lock);
228 if (s->s_count > S_BIAS) {
229 atomic_inc(&s->s_active);
230 s->s_count--;
231 spin_unlock(&sb_lock);
232 return 1;
233 }
234 spin_unlock(&sb_lock);
235 }
236 up_write(&s->s_umount);
237 put_super(s);
238 yield();
239 return 0;
240}
241
David Howellscf9a2ae2006-08-29 19:05:54 +0100242/*
Al Viro914e2632006-10-18 13:55:46 -0400243 * Superblock locking. We really ought to get rid of these two.
244 */
245void lock_super(struct super_block * sb)
246{
247 get_fs_excl();
248 mutex_lock(&sb->s_lock);
249}
250
251void unlock_super(struct super_block * sb)
252{
253 put_fs_excl();
254 mutex_unlock(&sb->s_lock);
255}
256
257EXPORT_SYMBOL(lock_super);
258EXPORT_SYMBOL(unlock_super);
259
260/*
David Howellscf9a2ae2006-08-29 19:05:54 +0100261 * Write out and wait upon all dirty data associated with this
262 * superblock. Filesystem data as well as the underlying block
263 * device. Takes the superblock lock. Requires a second blkdev
264 * flush by the caller to complete the operation.
265 */
266void __fsync_super(struct super_block *sb)
267{
268 sync_inodes_sb(sb, 0);
269 DQUOT_SYNC(sb);
270 lock_super(sb);
271 if (sb->s_dirt && sb->s_op->write_super)
272 sb->s_op->write_super(sb);
273 unlock_super(sb);
274 if (sb->s_op->sync_fs)
275 sb->s_op->sync_fs(sb, 1);
276 sync_blockdev(sb->s_bdev);
277 sync_inodes_sb(sb, 1);
278}
279
280/*
281 * Write out and wait upon all dirty data associated with this
282 * superblock. Filesystem data as well as the underlying block
283 * device. Takes the superblock lock.
284 */
285int fsync_super(struct super_block *sb)
286{
287 __fsync_super(sb);
288 return sync_blockdev(sb->s_bdev);
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/**
292 * generic_shutdown_super - common helper for ->kill_sb()
293 * @sb: superblock to kill
294 *
295 * generic_shutdown_super() does all fs-independent work on superblock
296 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
297 * that need destruction out of superblock, call generic_shutdown_super()
298 * and release aforementioned objects. Note: dentries and inodes _are_
299 * taken care of and do not need specific handling.
David Howellsc636ebd2006-10-11 01:22:19 -0700300 *
301 * Upon calling this function, the filesystem may no longer alter or
302 * rearrange the set of dentries belonging to this super_block, nor may it
303 * change the attachments of dentries to inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305void generic_shutdown_super(struct super_block *sb)
306{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800307 const struct super_operations *sop = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Arjan van de Venefaee192009-01-06 07:20:54 -0800309
David Howellsc636ebd2006-10-11 01:22:19 -0700310 if (sb->s_root) {
311 shrink_dcache_for_umount(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 fsync_super(sb);
313 lock_super(sb);
314 sb->s_flags &= ~MS_ACTIVE;
Arjan van de Venefaee192009-01-06 07:20:54 -0800315
316 /*
317 * wait for asynchronous fs operations to finish before going further
318 */
Cornelia Huck766ccb92009-01-20 15:31:31 +0100319 async_synchronize_full_domain(&sb->s_async_list);
Arjan van de Venefaee192009-01-06 07:20:54 -0800320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* bad name - it should be evict_inodes() */
322 invalidate_inodes(sb);
323 lock_kernel();
324
325 if (sop->write_super && sb->s_dirt)
326 sop->write_super(sb);
327 if (sop->put_super)
328 sop->put_super(sb);
329
330 /* Forget any remaining inodes */
331 if (invalidate_inodes(sb)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800332 printk("VFS: Busy inodes after unmount of %s. "
333 "Self-destruct in 5 seconds. Have a nice day...\n",
334 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 unlock_kernel();
338 unlock_super(sb);
339 }
340 spin_lock(&sb_lock);
341 /* should be initialized for __put_super_and_need_restart() */
342 list_del_init(&sb->s_list);
343 list_del(&sb->s_instances);
344 spin_unlock(&sb_lock);
345 up_write(&sb->s_umount);
346}
347
348EXPORT_SYMBOL(generic_shutdown_super);
349
350/**
351 * sget - find or create a superblock
352 * @type: filesystem type superblock should belong to
353 * @test: comparison callback
354 * @set: setup callback
355 * @data: argument to each of them
356 */
357struct super_block *sget(struct file_system_type *type,
358 int (*test)(struct super_block *,void *),
359 int (*set)(struct super_block *,void *),
360 void *data)
361{
362 struct super_block *s = NULL;
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700363 struct super_block *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int err;
365
366retry:
367 spin_lock(&sb_lock);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700368 if (test) {
369 list_for_each_entry(old, &type->fs_supers, s_instances) {
370 if (!test(old, data))
371 continue;
372 if (!grab_super(old))
373 goto retry;
374 if (s)
375 destroy_super(s);
376 return old;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 if (!s) {
380 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700381 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!s)
383 return ERR_PTR(-ENOMEM);
384 goto retry;
385 }
386
387 err = set(s, data);
388 if (err) {
389 spin_unlock(&sb_lock);
390 destroy_super(s);
391 return ERR_PTR(err);
392 }
393 s->s_type = type;
394 strlcpy(s->s_id, type->name, sizeof(s->s_id));
395 list_add_tail(&s->s_list, &super_blocks);
396 list_add(&s->s_instances, &type->fs_supers);
397 spin_unlock(&sb_lock);
398 get_filesystem(type);
399 return s;
400}
401
402EXPORT_SYMBOL(sget);
403
404void drop_super(struct super_block *sb)
405{
406 up_read(&sb->s_umount);
407 put_super(sb);
408}
409
410EXPORT_SYMBOL(drop_super);
411
412static inline void write_super(struct super_block *sb)
413{
414 lock_super(sb);
415 if (sb->s_root && sb->s_dirt)
416 if (sb->s_op->write_super)
417 sb->s_op->write_super(sb);
418 unlock_super(sb);
419}
420
421/*
422 * Note: check the dirty flag before waiting, so we don't
423 * hold up the sync while mounting a device. (The newly
424 * mounted device won't need syncing.)
425 */
426void sync_supers(void)
427{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700428 struct super_block *sb;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700431restart:
432 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (sb->s_dirt) {
434 sb->s_count++;
435 spin_unlock(&sb_lock);
436 down_read(&sb->s_umount);
437 write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700438 up_read(&sb->s_umount);
439 spin_lock(&sb_lock);
440 if (__put_super_and_need_restart(sb))
441 goto restart;
442 }
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 spin_unlock(&sb_lock);
445}
446
447/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200448 * Call the ->sync_fs super_op against all filesystems which are r/w and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * which implement it.
450 *
451 * This operation is careful to avoid the livelock which could easily happen
452 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
453 * is used only here. We set it against all filesystems and then clear it as
454 * we sync them. So redirtied filesystems are skipped.
455 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200456 * But if process A is currently running sync_filesystems and then process B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
458 * flags again, which will cause process A to resync everything. Fix that with
459 * a local mutex.
460 *
461 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
462 */
463void sync_filesystems(int wait)
464{
465 struct super_block *sb;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800466 static DEFINE_MUTEX(mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800468 mutex_lock(&mutex); /* Could be down_interruptible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700470 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!sb->s_op->sync_fs)
472 continue;
473 if (sb->s_flags & MS_RDONLY)
474 continue;
475 sb->s_need_sync_fs = 1;
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478restart:
Kirill Korotaev618f0632005-06-23 00:09:54 -0700479 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!sb->s_need_sync_fs)
481 continue;
482 sb->s_need_sync_fs = 0;
483 if (sb->s_flags & MS_RDONLY)
484 continue; /* hm. Was remounted r/o meanwhile */
485 sb->s_count++;
486 spin_unlock(&sb_lock);
487 down_read(&sb->s_umount);
Cornelia Huck766ccb92009-01-20 15:31:31 +0100488 async_synchronize_full_domain(&sb->s_async_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (sb->s_root && (wait || sb->s_dirt))
490 sb->s_op->sync_fs(sb, wait);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700491 up_read(&sb->s_umount);
492 /* restart only when sb is no longer on the list */
493 spin_lock(&sb_lock);
494 if (__put_super_and_need_restart(sb))
495 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 spin_unlock(&sb_lock);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800498 mutex_unlock(&mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/**
502 * get_super - get the superblock of a device
503 * @bdev: device to get the superblock for
504 *
505 * Scans the superblock list and finds the superblock of the file system
506 * mounted on the device given. %NULL is returned if no match is found.
507 */
508
509struct super_block * get_super(struct block_device *bdev)
510{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700511 struct super_block *sb;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!bdev)
514 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700517rescan:
518 list_for_each_entry(sb, &super_blocks, s_list) {
519 if (sb->s_bdev == bdev) {
520 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700522 down_read(&sb->s_umount);
523 if (sb->s_root)
524 return sb;
525 up_read(&sb->s_umount);
526 /* restart only when sb is no longer on the list */
527 spin_lock(&sb_lock);
528 if (__put_super_and_need_restart(sb))
529 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
532 spin_unlock(&sb_lock);
533 return NULL;
534}
535
536EXPORT_SYMBOL(get_super);
537
538struct super_block * user_get_super(dev_t dev)
539{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700540 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700543rescan:
544 list_for_each_entry(sb, &super_blocks, s_list) {
545 if (sb->s_dev == dev) {
546 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700548 down_read(&sb->s_umount);
549 if (sb->s_root)
550 return sb;
551 up_read(&sb->s_umount);
552 /* restart only when sb is no longer on the list */
553 spin_lock(&sb_lock);
554 if (__put_super_and_need_restart(sb))
555 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 }
558 spin_unlock(&sb_lock);
559 return NULL;
560}
561
Heiko Carstens257ac262009-01-14 14:14:13 +0100562SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct super_block *s;
565 struct ustat tmp;
566 struct kstatfs sbuf;
567 int err = -EINVAL;
568
569 s = user_get_super(new_decode_dev(dev));
570 if (s == NULL)
571 goto out;
David Howells726c3342006-06-23 02:02:58 -0700572 err = vfs_statfs(s->s_root, &sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 drop_super(s);
574 if (err)
575 goto out;
576
577 memset(&tmp,0,sizeof(struct ustat));
578 tmp.f_tfree = sbuf.f_bfree;
579 tmp.f_tinode = sbuf.f_ffree;
580
581 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
582out:
583 return err;
584}
585
586/**
Randy Dunlapa6b91912008-03-19 17:01:00 -0700587 * mark_files_ro - mark all files read-only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * @sb: superblock in question
589 *
Randy Dunlapa6b91912008-03-19 17:01:00 -0700590 * All files are marked read-only. We don't care about pending
591 * delete files so this should be used in 'force' mode only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
593
594static void mark_files_ro(struct super_block *sb)
595{
596 struct file *f;
597
Dave Hansen49e0d022008-02-15 14:37:32 -0800598retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800600 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
Dave Hansen49e0d022008-02-15 14:37:32 -0800601 struct vfsmount *mnt;
602 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
603 continue;
604 if (!file_count(f))
605 continue;
606 if (!(f->f_mode & FMODE_WRITE))
607 continue;
608 f->f_mode &= ~FMODE_WRITE;
Dave Hansenad775f52008-02-15 14:38:01 -0800609 if (file_check_writeable(f) != 0)
610 continue;
611 file_release_write(f);
Dave Hansen49e0d022008-02-15 14:37:32 -0800612 mnt = mntget(f->f_path.mnt);
613 file_list_unlock();
614 /*
615 * This can sleep, so we can't hold
616 * the file_list_lock() spinlock.
617 */
618 mnt_drop_write(mnt);
619 mntput(mnt);
620 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 file_list_unlock();
623}
624
625/**
626 * do_remount_sb - asks filesystem to change mount options.
627 * @sb: superblock in question
628 * @flags: numeric part of options
629 * @data: the rest of options
630 * @force: whether or not to force the change
631 *
632 * Alters the mount options of a mounted file system.
633 */
634int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
635{
636 int retval;
Jan Kara0ff5af82008-04-28 02:14:33 -0700637 int remount_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David Howells93614012006-09-30 20:45:40 +0200639#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
641 return -EACCES;
David Howells93614012006-09-30 20:45:40 +0200642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (flags & MS_RDONLY)
644 acct_auto_close(sb);
645 shrink_dcache_sb(sb);
646 fsync_super(sb);
647
648 /* If we are remounting RDONLY and current sb is read/write,
649 make sure there are no rw files opened */
650 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
651 if (force)
652 mark_files_ro(sb);
653 else if (!fs_may_remount_ro(sb))
654 return -EBUSY;
Jan Kara0ff5af82008-04-28 02:14:33 -0700655 retval = DQUOT_OFF(sb, 1);
656 if (retval < 0 && retval != -ENOSYS)
657 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Jan Kara0ff5af82008-04-28 02:14:33 -0700659 remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (sb->s_op->remount_fs) {
662 lock_super(sb);
663 retval = sb->s_op->remount_fs(sb, &flags, data);
664 unlock_super(sb);
665 if (retval)
666 return retval;
667 }
668 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
Jan Kara0ff5af82008-04-28 02:14:33 -0700669 if (remount_rw)
670 DQUOT_ON_REMOUNT(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return 0;
672}
673
674static void do_emergency_remount(unsigned long foo)
675{
676 struct super_block *sb;
677
678 spin_lock(&sb_lock);
679 list_for_each_entry(sb, &super_blocks, s_list) {
680 sb->s_count++;
681 spin_unlock(&sb_lock);
682 down_read(&sb->s_umount);
683 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
684 /*
685 * ->remount_fs needs lock_kernel().
686 *
687 * What lock protects sb->s_flags??
688 */
689 lock_kernel();
690 do_remount_sb(sb, MS_RDONLY, NULL, 1);
691 unlock_kernel();
692 }
693 drop_super(sb);
694 spin_lock(&sb_lock);
695 }
696 spin_unlock(&sb_lock);
697 printk("Emergency Remount complete\n");
698}
699
700void emergency_remount(void)
701{
702 pdflush_operation(do_emergency_remount, 0);
703}
704
705/*
706 * Unnamed block devices are dummy devices used by virtual
707 * filesystems which don't use real block-devices. -- jrs
708 */
709
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400710static DEFINE_IDA(unnamed_dev_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
712
713int set_anon_super(struct super_block *s, void *data)
714{
715 int dev;
716 int error;
717
718 retry:
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400719 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -ENOMEM;
721 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400722 error = ida_get_new(&unnamed_dev_ida, &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 spin_unlock(&unnamed_dev_lock);
724 if (error == -EAGAIN)
725 /* We raced and lost with another CPU. */
726 goto retry;
727 else if (error)
728 return -EAGAIN;
729
730 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
731 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400732 ida_remove(&unnamed_dev_ida, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 spin_unlock(&unnamed_dev_lock);
734 return -EMFILE;
735 }
736 s->s_dev = MKDEV(0, dev & MINORMASK);
737 return 0;
738}
739
740EXPORT_SYMBOL(set_anon_super);
741
742void kill_anon_super(struct super_block *sb)
743{
744 int slot = MINOR(sb->s_dev);
745
746 generic_shutdown_super(sb);
747 spin_lock(&unnamed_dev_lock);
Alexey Dobriyanad76cbc2008-08-28 06:26:23 +0400748 ida_remove(&unnamed_dev_ida, slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_unlock(&unnamed_dev_lock);
750}
751
752EXPORT_SYMBOL(kill_anon_super);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754void kill_litter_super(struct super_block *sb)
755{
756 if (sb->s_root)
757 d_genocide(sb->s_root);
758 kill_anon_super(sb);
759}
760
761EXPORT_SYMBOL(kill_litter_super);
762
David Howells93614012006-09-30 20:45:40 +0200763#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764static int set_bdev_super(struct super_block *s, void *data)
765{
766 s->s_bdev = data;
767 s->s_dev = s->s_bdev->bd_dev;
768 return 0;
769}
770
771static int test_bdev_super(struct super_block *s, void *data)
772{
773 return (void *)s->s_bdev == data;
774}
775
David Howells454e2392006-06-23 02:02:57 -0700776int get_sb_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int flags, const char *dev_name, void *data,
David Howells454e2392006-06-23 02:02:57 -0700778 int (*fill_super)(struct super_block *, void *, int),
779 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 struct block_device *bdev;
782 struct super_block *s;
Al Viro30c40d22008-02-22 19:50:45 -0500783 fmode_t mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int error = 0;
785
Al Viro30c40d22008-02-22 19:50:45 -0500786 if (!(flags & MS_RDONLY))
787 mode |= FMODE_WRITE;
788
789 bdev = open_bdev_exclusive(dev_name, mode, fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (IS_ERR(bdev))
David Howells454e2392006-06-23 02:02:57 -0700791 return PTR_ERR(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /*
794 * once the super is inserted into the list by sget, s_umount
795 * will protect the lockfs code from trying to start a snapshot
796 * while we are mounting
797 */
David Chinnerf73ca1b2007-01-10 23:15:41 -0800798 down(&bdev->bd_mount_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
David Chinnerf73ca1b2007-01-10 23:15:41 -0800800 up(&bdev->bd_mount_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700802 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (s->s_root) {
805 if ((flags ^ s->s_flags) & MS_RDONLY) {
806 up_write(&s->s_umount);
807 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700808 error = -EBUSY;
809 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
David Howells454e2392006-06-23 02:02:57 -0700811
Al Viro30c40d22008-02-22 19:50:45 -0500812 close_bdev_exclusive(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 } else {
814 char b[BDEVNAME_SIZE];
815
816 s->s_flags = flags;
Al Viro30c40d22008-02-22 19:50:45 -0500817 s->s_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800819 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800820 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (error) {
822 up_write(&s->s_umount);
823 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700824 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800825 }
David Howells454e2392006-06-23 02:02:57 -0700826
827 s->s_flags |= MS_ACTIVE;
Theodore Ts'o87d8fe12009-01-03 09:47:09 -0500828 bdev->bd_super = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
David Howells454e2392006-06-23 02:02:57 -0700831 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
David Howells454e2392006-06-23 02:02:57 -0700833error_s:
834 error = PTR_ERR(s);
835error_bdev:
Al Viro30c40d22008-02-22 19:50:45 -0500836 close_bdev_exclusive(bdev, mode);
David Howells454e2392006-06-23 02:02:57 -0700837error:
838 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841EXPORT_SYMBOL(get_sb_bdev);
842
843void kill_block_super(struct super_block *sb)
844{
845 struct block_device *bdev = sb->s_bdev;
Al Viro30c40d22008-02-22 19:50:45 -0500846 fmode_t mode = sb->s_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Theodore Ts'o87d8fe12009-01-03 09:47:09 -0500848 bdev->bd_super = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 generic_shutdown_super(sb);
850 sync_blockdev(bdev);
Al Viro30c40d22008-02-22 19:50:45 -0500851 close_bdev_exclusive(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
David Howells454e2392006-06-23 02:02:57 -0700857int get_sb_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700859 int (*fill_super)(struct super_block *, void *, int),
860 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 int error;
863 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
864
865 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700866 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 s->s_flags = flags;
869
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800870 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (error) {
872 up_write(&s->s_umount);
873 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700874 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 s->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700877 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880EXPORT_SYMBOL(get_sb_nodev);
881
882static int compare_single(struct super_block *s, void *p)
883{
884 return 1;
885}
886
David Howells454e2392006-06-23 02:02:57 -0700887int get_sb_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700889 int (*fill_super)(struct super_block *, void *, int),
890 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct super_block *s;
893 int error;
894
895 s = sget(fs_type, compare_single, set_anon_super, NULL);
896 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700897 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!s->s_root) {
899 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800900 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (error) {
902 up_write(&s->s_umount);
903 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700904 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 s->s_flags |= MS_ACTIVE;
907 }
908 do_remount_sb(s, flags, data, 0);
David Howells454e2392006-06-23 02:02:57 -0700909 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912EXPORT_SYMBOL(get_sb_single);
913
914struct vfsmount *
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400915vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 char *secdata = NULL;
David Howells454e2392006-06-23 02:02:57 -0700919 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!type)
922 return ERR_PTR(-ENODEV);
923
David Howells454e2392006-06-23 02:02:57 -0700924 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 mnt = alloc_vfsmnt(name);
926 if (!mnt)
927 goto out;
928
Eric Parise0007522008-03-05 10:31:54 -0500929 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700931 if (!secdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto out_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Parise0007522008-03-05 10:31:54 -0500934 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700935 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
David Howells454e2392006-06-23 02:02:57 -0700939 error = type->get_sb(type, flags, name, data, mnt);
940 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto out_free_secdata;
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -0700942 BUG_ON(!mnt->mnt_sb);
David Howells454e2392006-06-23 02:02:57 -0700943
James Morris12204e22008-12-19 10:44:42 +1100944 error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (error)
946 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700947
948 mnt->mnt_mountpoint = mnt->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 mnt->mnt_parent = mnt;
David Howells454e2392006-06-23 02:02:57 -0700950 up_write(&mnt->mnt_sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -0700951 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return mnt;
953out_sb:
David Howells454e2392006-06-23 02:02:57 -0700954 dput(mnt->mnt_root);
955 up_write(&mnt->mnt_sb->s_umount);
956 deactivate_super(mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957out_free_secdata:
958 free_secdata(secdata);
959out_mnt:
960 free_vfsmnt(mnt);
961out:
David Howells454e2392006-06-23 02:02:57 -0700962 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400965EXPORT_SYMBOL_GPL(vfs_kern_mount);
966
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700967static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
968{
969 int err;
970 const char *subtype = strchr(fstype, '.');
971 if (subtype) {
972 subtype++;
973 err = -EINVAL;
974 if (!subtype[0])
975 goto err;
976 } else
977 subtype = "";
978
979 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
980 err = -ENOMEM;
981 if (!mnt->mnt_sb->s_subtype)
982 goto err;
983 return mnt;
984
985 err:
986 mntput(mnt);
987 return ERR_PTR(err);
988}
989
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400990struct vfsmount *
991do_kern_mount(const char *fstype, int flags, const char *name, void *data)
992{
993 struct file_system_type *type = get_fs_type(fstype);
994 struct vfsmount *mnt;
995 if (!type)
996 return ERR_PTR(-ENODEV);
997 mnt = vfs_kern_mount(type, flags, name, data);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700998 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
999 !mnt->mnt_sb->s_subtype)
1000 mnt = fs_set_subtype(mnt, fstype);
Trond Myklebustbb4a58b2006-06-09 09:34:15 -04001001 put_filesystem(type);
1002 return mnt;
1003}
Al Viro8a4e98d2008-02-24 01:43:03 -05001004EXPORT_SYMBOL_GPL(do_kern_mount);
Trond Myklebustbb4a58b2006-06-09 09:34:15 -04001005
Pavel Emelyanov8bf97252007-10-18 23:40:02 -07001006struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Pavel Emelyanov8bf97252007-10-18 23:40:02 -07001008 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Pavel Emelyanov8bf97252007-10-18 23:40:02 -07001011EXPORT_SYMBOL_GPL(kern_mount_data);