blob: 1f8f05ede437067cadcfb00b77baa47bd09e9a5e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
42
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044LIST_HEAD(super_blocks);
45DEFINE_SPINLOCK(sb_lock);
46
47/**
48 * alloc_super - create new superblock
Henrik Kretzschmarfe2bbc42006-09-06 00:03:41 -070049 * @type: filesystem type superblock should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 *
51 * Allocates and initializes a new &struct super_block. alloc_super()
52 * returns a pointer new superblock or %NULL if allocation had failed.
53 */
Ingo Molnarcf516242006-07-03 00:25:27 -070054static struct super_block *alloc_super(struct file_system_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -080056 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 static struct super_operations default_op;
58
59 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (security_sb_alloc(s)) {
61 kfree(s);
62 s = NULL;
63 goto out;
64 }
65 INIT_LIST_HEAD(&s->s_dirty);
66 INIT_LIST_HEAD(&s->s_io);
Ken Chen0e0f4fc2007-10-16 23:30:38 -070067 INIT_LIST_HEAD(&s->s_more_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 INIT_LIST_HEAD(&s->s_files);
69 INIT_LIST_HEAD(&s->s_instances);
70 INIT_HLIST_HEAD(&s->s_anon);
71 INIT_LIST_HEAD(&s->s_inodes);
72 init_rwsem(&s->s_umount);
Ingo Molnar7892f2f2006-01-09 15:59:25 -080073 mutex_init(&s->s_lock);
Arjan van de Ven897c6ff2006-07-03 00:25:28 -070074 lockdep_set_class(&s->s_umount, &type->s_umount_key);
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);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700108 kfree(s->s_subtype);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800109 kfree(s->s_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 kfree(s);
111}
112
113/* Superblock refcounting */
114
115/*
116 * Drop a superblock's refcount. Returns non-zero if the superblock was
117 * destroyed. The caller must hold sb_lock.
118 */
119int __put_super(struct super_block *sb)
120{
121 int ret = 0;
122
123 if (!--sb->s_count) {
124 destroy_super(sb);
125 ret = 1;
126 }
127 return ret;
128}
129
130/*
131 * Drop a superblock's refcount.
132 * Returns non-zero if the superblock is about to be destroyed and
133 * at least is already removed from super_blocks list, so if we are
134 * making a loop through super blocks then we need to restart.
135 * The caller must hold sb_lock.
136 */
137int __put_super_and_need_restart(struct super_block *sb)
138{
139 /* check for race with generic_shutdown_super() */
140 if (list_empty(&sb->s_list)) {
141 /* super block is removed, need to restart... */
142 __put_super(sb);
143 return 1;
144 }
145 /* can't be the last, since s_list is still in use */
146 sb->s_count--;
147 BUG_ON(sb->s_count == 0);
148 return 0;
149}
150
151/**
152 * put_super - drop a temporary reference to superblock
153 * @sb: superblock in question
154 *
155 * Drops a temporary reference, frees superblock if there's no
156 * references left.
157 */
158static void put_super(struct super_block *sb)
159{
160 spin_lock(&sb_lock);
161 __put_super(sb);
162 spin_unlock(&sb_lock);
163}
164
165
166/**
167 * deactivate_super - drop an active reference to superblock
168 * @s: superblock to deactivate
169 *
170 * Drops an active reference to superblock, acquiring a temprory one if
171 * there is no active references left. In that case we lock superblock,
172 * tell fs driver to shut it down and drop the temporary reference we
173 * had just acquired.
174 */
175void deactivate_super(struct super_block *s)
176{
177 struct file_system_type *fs = s->s_type;
178 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
179 s->s_count -= S_BIAS-1;
180 spin_unlock(&sb_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500181 DQUOT_OFF(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 down_write(&s->s_umount);
183 fs->kill_sb(s);
184 put_filesystem(fs);
185 put_super(s);
186 }
187}
188
189EXPORT_SYMBOL(deactivate_super);
190
191/**
192 * grab_super - acquire an active reference
193 * @s: reference we are trying to make active
194 *
195 * Tries to acquire an active reference. grab_super() is used when we
196 * had just found a superblock in super_blocks or fs_type->fs_supers
197 * and want to turn it into a full-blown active reference. grab_super()
198 * is called with sb_lock held and drops it. Returns 1 in case of
199 * success, 0 if we had failed (superblock contents was already dead or
200 * dying when grab_super() had been called).
201 */
Josh Triplett9c4dbee2006-09-29 01:59:29 -0700202static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 s->s_count++;
205 spin_unlock(&sb_lock);
206 down_write(&s->s_umount);
207 if (s->s_root) {
208 spin_lock(&sb_lock);
209 if (s->s_count > S_BIAS) {
210 atomic_inc(&s->s_active);
211 s->s_count--;
212 spin_unlock(&sb_lock);
213 return 1;
214 }
215 spin_unlock(&sb_lock);
216 }
217 up_write(&s->s_umount);
218 put_super(s);
219 yield();
220 return 0;
221}
222
David Howellscf9a2ae2006-08-29 19:05:54 +0100223/*
Al Viro914e2632006-10-18 13:55:46 -0400224 * Superblock locking. We really ought to get rid of these two.
225 */
226void lock_super(struct super_block * sb)
227{
228 get_fs_excl();
229 mutex_lock(&sb->s_lock);
230}
231
232void unlock_super(struct super_block * sb)
233{
234 put_fs_excl();
235 mutex_unlock(&sb->s_lock);
236}
237
238EXPORT_SYMBOL(lock_super);
239EXPORT_SYMBOL(unlock_super);
240
241/*
David Howellscf9a2ae2006-08-29 19:05:54 +0100242 * Write out and wait upon all dirty data associated with this
243 * superblock. Filesystem data as well as the underlying block
244 * device. Takes the superblock lock. Requires a second blkdev
245 * flush by the caller to complete the operation.
246 */
247void __fsync_super(struct super_block *sb)
248{
249 sync_inodes_sb(sb, 0);
250 DQUOT_SYNC(sb);
251 lock_super(sb);
252 if (sb->s_dirt && sb->s_op->write_super)
253 sb->s_op->write_super(sb);
254 unlock_super(sb);
255 if (sb->s_op->sync_fs)
256 sb->s_op->sync_fs(sb, 1);
257 sync_blockdev(sb->s_bdev);
258 sync_inodes_sb(sb, 1);
259}
260
261/*
262 * Write out and wait upon all dirty data associated with this
263 * superblock. Filesystem data as well as the underlying block
264 * device. Takes the superblock lock.
265 */
266int fsync_super(struct super_block *sb)
267{
268 __fsync_super(sb);
269 return sync_blockdev(sb->s_bdev);
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/**
273 * generic_shutdown_super - common helper for ->kill_sb()
274 * @sb: superblock to kill
275 *
276 * generic_shutdown_super() does all fs-independent work on superblock
277 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
278 * that need destruction out of superblock, call generic_shutdown_super()
279 * and release aforementioned objects. Note: dentries and inodes _are_
280 * taken care of and do not need specific handling.
David Howellsc636ebd2006-10-11 01:22:19 -0700281 *
282 * Upon calling this function, the filesystem may no longer alter or
283 * rearrange the set of dentries belonging to this super_block, nor may it
284 * change the attachments of dentries to inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286void generic_shutdown_super(struct super_block *sb)
287{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800288 const struct super_operations *sop = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
David Howellsc636ebd2006-10-11 01:22:19 -0700290 if (sb->s_root) {
291 shrink_dcache_for_umount(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 fsync_super(sb);
293 lock_super(sb);
294 sb->s_flags &= ~MS_ACTIVE;
295 /* bad name - it should be evict_inodes() */
296 invalidate_inodes(sb);
297 lock_kernel();
298
299 if (sop->write_super && sb->s_dirt)
300 sop->write_super(sb);
301 if (sop->put_super)
302 sop->put_super(sb);
303
304 /* Forget any remaining inodes */
305 if (invalidate_inodes(sb)) {
Dave Jones7b4fe292006-02-07 12:58:48 -0800306 printk("VFS: Busy inodes after unmount of %s. "
307 "Self-destruct in 5 seconds. Have a nice day...\n",
308 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 unlock_kernel();
312 unlock_super(sb);
313 }
314 spin_lock(&sb_lock);
315 /* should be initialized for __put_super_and_need_restart() */
316 list_del_init(&sb->s_list);
317 list_del(&sb->s_instances);
318 spin_unlock(&sb_lock);
319 up_write(&sb->s_umount);
320}
321
322EXPORT_SYMBOL(generic_shutdown_super);
323
324/**
325 * sget - find or create a superblock
326 * @type: filesystem type superblock should belong to
327 * @test: comparison callback
328 * @set: setup callback
329 * @data: argument to each of them
330 */
331struct super_block *sget(struct file_system_type *type,
332 int (*test)(struct super_block *,void *),
333 int (*set)(struct super_block *,void *),
334 void *data)
335{
336 struct super_block *s = NULL;
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700337 struct super_block *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err;
339
340retry:
341 spin_lock(&sb_lock);
Matthias Kaehlcked4730122007-10-18 23:39:57 -0700342 if (test) {
343 list_for_each_entry(old, &type->fs_supers, s_instances) {
344 if (!test(old, data))
345 continue;
346 if (!grab_super(old))
347 goto retry;
348 if (s)
349 destroy_super(s);
350 return old;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 if (!s) {
354 spin_unlock(&sb_lock);
Ingo Molnarcf516242006-07-03 00:25:27 -0700355 s = alloc_super(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!s)
357 return ERR_PTR(-ENOMEM);
358 goto retry;
359 }
360
361 err = set(s, data);
362 if (err) {
363 spin_unlock(&sb_lock);
364 destroy_super(s);
365 return ERR_PTR(err);
366 }
367 s->s_type = type;
368 strlcpy(s->s_id, type->name, sizeof(s->s_id));
369 list_add_tail(&s->s_list, &super_blocks);
370 list_add(&s->s_instances, &type->fs_supers);
371 spin_unlock(&sb_lock);
372 get_filesystem(type);
373 return s;
374}
375
376EXPORT_SYMBOL(sget);
377
378void drop_super(struct super_block *sb)
379{
380 up_read(&sb->s_umount);
381 put_super(sb);
382}
383
384EXPORT_SYMBOL(drop_super);
385
386static inline void write_super(struct super_block *sb)
387{
388 lock_super(sb);
389 if (sb->s_root && sb->s_dirt)
390 if (sb->s_op->write_super)
391 sb->s_op->write_super(sb);
392 unlock_super(sb);
393}
394
395/*
396 * Note: check the dirty flag before waiting, so we don't
397 * hold up the sync while mounting a device. (The newly
398 * mounted device won't need syncing.)
399 */
400void sync_supers(void)
401{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700402 struct super_block *sb;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700405restart:
406 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (sb->s_dirt) {
408 sb->s_count++;
409 spin_unlock(&sb_lock);
410 down_read(&sb->s_umount);
411 write_super(sb);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700412 up_read(&sb->s_umount);
413 spin_lock(&sb_lock);
414 if (__put_super_and_need_restart(sb))
415 goto restart;
416 }
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 spin_unlock(&sb_lock);
419}
420
421/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200422 * Call the ->sync_fs super_op against all filesystems which are r/w and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * which implement it.
424 *
425 * This operation is careful to avoid the livelock which could easily happen
426 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
427 * is used only here. We set it against all filesystems and then clear it as
428 * we sync them. So redirtied filesystems are skipped.
429 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200430 * But if process A is currently running sync_filesystems and then process B
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
432 * flags again, which will cause process A to resync everything. Fix that with
433 * a local mutex.
434 *
435 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
436 */
437void sync_filesystems(int wait)
438{
439 struct super_block *sb;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800440 static DEFINE_MUTEX(mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800442 mutex_lock(&mutex); /* Could be down_interruptible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700444 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (!sb->s_op->sync_fs)
446 continue;
447 if (sb->s_flags & MS_RDONLY)
448 continue;
449 sb->s_need_sync_fs = 1;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452restart:
Kirill Korotaev618f0632005-06-23 00:09:54 -0700453 list_for_each_entry(sb, &super_blocks, s_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!sb->s_need_sync_fs)
455 continue;
456 sb->s_need_sync_fs = 0;
457 if (sb->s_flags & MS_RDONLY)
458 continue; /* hm. Was remounted r/o meanwhile */
459 sb->s_count++;
460 spin_unlock(&sb_lock);
461 down_read(&sb->s_umount);
462 if (sb->s_root && (wait || sb->s_dirt))
463 sb->s_op->sync_fs(sb, wait);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700464 up_read(&sb->s_umount);
465 /* restart only when sb is no longer on the list */
466 spin_lock(&sb_lock);
467 if (__put_super_and_need_restart(sb))
468 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 spin_unlock(&sb_lock);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800471 mutex_unlock(&mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474/**
475 * get_super - get the superblock of a device
476 * @bdev: device to get the superblock for
477 *
478 * Scans the superblock list and finds the superblock of the file system
479 * mounted on the device given. %NULL is returned if no match is found.
480 */
481
482struct super_block * get_super(struct block_device *bdev)
483{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700484 struct super_block *sb;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!bdev)
487 return NULL;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700490rescan:
491 list_for_each_entry(sb, &super_blocks, s_list) {
492 if (sb->s_bdev == bdev) {
493 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700495 down_read(&sb->s_umount);
496 if (sb->s_root)
497 return sb;
498 up_read(&sb->s_umount);
499 /* restart only when sb is no longer on the list */
500 spin_lock(&sb_lock);
501 if (__put_super_and_need_restart(sb))
502 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
505 spin_unlock(&sb_lock);
506 return NULL;
507}
508
509EXPORT_SYMBOL(get_super);
510
511struct super_block * user_get_super(dev_t dev)
512{
Kirill Korotaev618f0632005-06-23 00:09:54 -0700513 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 spin_lock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700516rescan:
517 list_for_each_entry(sb, &super_blocks, s_list) {
518 if (sb->s_dev == dev) {
519 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spin_unlock(&sb_lock);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700521 down_read(&sb->s_umount);
522 if (sb->s_root)
523 return sb;
524 up_read(&sb->s_umount);
525 /* restart only when sb is no longer on the list */
526 spin_lock(&sb_lock);
527 if (__put_super_and_need_restart(sb))
528 goto rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 }
531 spin_unlock(&sb_lock);
532 return NULL;
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
536{
537 struct super_block *s;
538 struct ustat tmp;
539 struct kstatfs sbuf;
540 int err = -EINVAL;
541
542 s = user_get_super(new_decode_dev(dev));
543 if (s == NULL)
544 goto out;
David Howells726c3342006-06-23 02:02:58 -0700545 err = vfs_statfs(s->s_root, &sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 drop_super(s);
547 if (err)
548 goto out;
549
550 memset(&tmp,0,sizeof(struct ustat));
551 tmp.f_tfree = sbuf.f_bfree;
552 tmp.f_tinode = sbuf.f_ffree;
553
554 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
555out:
556 return err;
557}
558
559/**
Randy Dunlapa6b91912008-03-19 17:01:00 -0700560 * mark_files_ro - mark all files read-only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * @sb: superblock in question
562 *
Randy Dunlapa6b91912008-03-19 17:01:00 -0700563 * All files are marked read-only. We don't care about pending
564 * delete files so this should be used in 'force' mode only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566
567static void mark_files_ro(struct super_block *sb)
568{
569 struct file *f;
570
Dave Hansen49e0d022008-02-15 14:37:32 -0800571retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 file_list_lock();
Eric Dumazet2f512012005-10-30 15:02:16 -0800573 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
Dave Hansen49e0d022008-02-15 14:37:32 -0800574 struct vfsmount *mnt;
575 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
576 continue;
577 if (!file_count(f))
578 continue;
579 if (!(f->f_mode & FMODE_WRITE))
580 continue;
581 f->f_mode &= ~FMODE_WRITE;
Dave Hansenad775f52008-02-15 14:38:01 -0800582 if (file_check_writeable(f) != 0)
583 continue;
584 file_release_write(f);
Dave Hansen49e0d022008-02-15 14:37:32 -0800585 mnt = mntget(f->f_path.mnt);
586 file_list_unlock();
587 /*
588 * This can sleep, so we can't hold
589 * the file_list_lock() spinlock.
590 */
591 mnt_drop_write(mnt);
592 mntput(mnt);
593 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 file_list_unlock();
596}
597
598/**
599 * do_remount_sb - asks filesystem to change mount options.
600 * @sb: superblock in question
601 * @flags: numeric part of options
602 * @data: the rest of options
603 * @force: whether or not to force the change
604 *
605 * Alters the mount options of a mounted file system.
606 */
607int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
608{
609 int retval;
610
David Howells93614012006-09-30 20:45:40 +0200611#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
613 return -EACCES;
David Howells93614012006-09-30 20:45:40 +0200614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (flags & MS_RDONLY)
616 acct_auto_close(sb);
617 shrink_dcache_sb(sb);
618 fsync_super(sb);
619
620 /* If we are remounting RDONLY and current sb is read/write,
621 make sure there are no rw files opened */
622 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
623 if (force)
624 mark_files_ro(sb);
625 else if (!fs_may_remount_ro(sb))
626 return -EBUSY;
Jan Kara66191dc2008-02-08 04:22:13 -0800627 DQUOT_OFF(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 if (sb->s_op->remount_fs) {
631 lock_super(sb);
632 retval = sb->s_op->remount_fs(sb, &flags, data);
633 unlock_super(sb);
634 if (retval)
635 return retval;
636 }
637 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
638 return 0;
639}
640
641static void do_emergency_remount(unsigned long foo)
642{
643 struct super_block *sb;
644
645 spin_lock(&sb_lock);
646 list_for_each_entry(sb, &super_blocks, s_list) {
647 sb->s_count++;
648 spin_unlock(&sb_lock);
649 down_read(&sb->s_umount);
650 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
651 /*
652 * ->remount_fs needs lock_kernel().
653 *
654 * What lock protects sb->s_flags??
655 */
656 lock_kernel();
657 do_remount_sb(sb, MS_RDONLY, NULL, 1);
658 unlock_kernel();
659 }
660 drop_super(sb);
661 spin_lock(&sb_lock);
662 }
663 spin_unlock(&sb_lock);
664 printk("Emergency Remount complete\n");
665}
666
667void emergency_remount(void)
668{
669 pdflush_operation(do_emergency_remount, 0);
670}
671
672/*
673 * Unnamed block devices are dummy devices used by virtual
674 * filesystems which don't use real block-devices. -- jrs
675 */
676
677static struct idr unnamed_dev_idr;
678static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
679
680int set_anon_super(struct super_block *s, void *data)
681{
682 int dev;
683 int error;
684
685 retry:
686 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
687 return -ENOMEM;
688 spin_lock(&unnamed_dev_lock);
689 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
690 spin_unlock(&unnamed_dev_lock);
691 if (error == -EAGAIN)
692 /* We raced and lost with another CPU. */
693 goto retry;
694 else if (error)
695 return -EAGAIN;
696
697 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
698 spin_lock(&unnamed_dev_lock);
699 idr_remove(&unnamed_dev_idr, dev);
700 spin_unlock(&unnamed_dev_lock);
701 return -EMFILE;
702 }
703 s->s_dev = MKDEV(0, dev & MINORMASK);
704 return 0;
705}
706
707EXPORT_SYMBOL(set_anon_super);
708
709void kill_anon_super(struct super_block *sb)
710{
711 int slot = MINOR(sb->s_dev);
712
713 generic_shutdown_super(sb);
714 spin_lock(&unnamed_dev_lock);
715 idr_remove(&unnamed_dev_idr, slot);
716 spin_unlock(&unnamed_dev_lock);
717}
718
719EXPORT_SYMBOL(kill_anon_super);
720
721void __init unnamed_dev_init(void)
722{
723 idr_init(&unnamed_dev_idr);
724}
725
726void kill_litter_super(struct super_block *sb)
727{
728 if (sb->s_root)
729 d_genocide(sb->s_root);
730 kill_anon_super(sb);
731}
732
733EXPORT_SYMBOL(kill_litter_super);
734
David Howells93614012006-09-30 20:45:40 +0200735#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736static int set_bdev_super(struct super_block *s, void *data)
737{
738 s->s_bdev = data;
739 s->s_dev = s->s_bdev->bd_dev;
740 return 0;
741}
742
743static int test_bdev_super(struct super_block *s, void *data)
744{
745 return (void *)s->s_bdev == data;
746}
747
David Howells454e2392006-06-23 02:02:57 -0700748int get_sb_bdev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int flags, const char *dev_name, void *data,
David Howells454e2392006-06-23 02:02:57 -0700750 int (*fill_super)(struct super_block *, void *, int),
751 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct block_device *bdev;
754 struct super_block *s;
755 int error = 0;
756
757 bdev = open_bdev_excl(dev_name, flags, fs_type);
758 if (IS_ERR(bdev))
David Howells454e2392006-06-23 02:02:57 -0700759 return PTR_ERR(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /*
762 * once the super is inserted into the list by sget, s_umount
763 * will protect the lockfs code from trying to start a snapshot
764 * while we are mounting
765 */
David Chinnerf73ca1b2007-01-10 23:15:41 -0800766 down(&bdev->bd_mount_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
David Chinnerf73ca1b2007-01-10 23:15:41 -0800768 up(&bdev->bd_mount_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700770 goto error_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (s->s_root) {
773 if ((flags ^ s->s_flags) & MS_RDONLY) {
774 up_write(&s->s_umount);
775 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700776 error = -EBUSY;
777 goto error_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
David Howells454e2392006-06-23 02:02:57 -0700779
780 close_bdev_excl(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else {
782 char b[BDEVNAME_SIZE];
783
784 s->s_flags = flags;
785 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Pekka Enberge78c9a02006-01-08 01:03:39 -0800786 sb_set_blocksize(s, block_size(bdev));
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800787 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (error) {
789 up_write(&s->s_umount);
790 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700791 goto error;
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -0800792 }
David Howells454e2392006-06-23 02:02:57 -0700793
794 s->s_flags |= MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
David Howells454e2392006-06-23 02:02:57 -0700797 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
David Howells454e2392006-06-23 02:02:57 -0700799error_s:
800 error = PTR_ERR(s);
801error_bdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 close_bdev_excl(bdev);
David Howells454e2392006-06-23 02:02:57 -0700803error:
804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807EXPORT_SYMBOL(get_sb_bdev);
808
809void kill_block_super(struct super_block *sb)
810{
811 struct block_device *bdev = sb->s_bdev;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 generic_shutdown_super(sb);
814 sync_blockdev(bdev);
815 close_bdev_excl(bdev);
816}
817
818EXPORT_SYMBOL(kill_block_super);
David Howells93614012006-09-30 20:45:40 +0200819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
David Howells454e2392006-06-23 02:02:57 -0700821int get_sb_nodev(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700823 int (*fill_super)(struct super_block *, void *, int),
824 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 int error;
827 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
828
829 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700830 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 s->s_flags = flags;
833
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800834 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (error) {
836 up_write(&s->s_umount);
837 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700838 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 s->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700841 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844EXPORT_SYMBOL(get_sb_nodev);
845
846static int compare_single(struct super_block *s, void *p)
847{
848 return 1;
849}
850
David Howells454e2392006-06-23 02:02:57 -0700851int get_sb_single(struct file_system_type *fs_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int flags, void *data,
David Howells454e2392006-06-23 02:02:57 -0700853 int (*fill_super)(struct super_block *, void *, int),
854 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct super_block *s;
857 int error;
858
859 s = sget(fs_type, compare_single, set_anon_super, NULL);
860 if (IS_ERR(s))
David Howells454e2392006-06-23 02:02:57 -0700861 return PTR_ERR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!s->s_root) {
863 s->s_flags = flags;
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800864 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (error) {
866 up_write(&s->s_umount);
867 deactivate_super(s);
David Howells454e2392006-06-23 02:02:57 -0700868 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 s->s_flags |= MS_ACTIVE;
871 }
872 do_remount_sb(s, flags, data, 0);
David Howells454e2392006-06-23 02:02:57 -0700873 return simple_set_mnt(mnt, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876EXPORT_SYMBOL(get_sb_single);
877
878struct vfsmount *
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400879vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 char *secdata = NULL;
David Howells454e2392006-06-23 02:02:57 -0700883 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (!type)
886 return ERR_PTR(-ENODEV);
887
David Howells454e2392006-06-23 02:02:57 -0700888 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 mnt = alloc_vfsmnt(name);
890 if (!mnt)
891 goto out;
892
Eric Parise0007522008-03-05 10:31:54 -0500893 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 secdata = alloc_secdata();
David Howells454e2392006-06-23 02:02:57 -0700895 if (!secdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 goto out_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Eric Parise0007522008-03-05 10:31:54 -0500898 error = security_sb_copy_data(data, secdata);
David Howells454e2392006-06-23 02:02:57 -0700899 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto out_free_secdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
David Howells454e2392006-06-23 02:02:57 -0700903 error = type->get_sb(type, flags, name, data, mnt);
904 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto out_free_secdata;
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -0700906 BUG_ON(!mnt->mnt_sb);
David Howells454e2392006-06-23 02:02:57 -0700907
908 error = security_sb_kern_mount(mnt->mnt_sb, secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (error)
910 goto out_sb;
David Howells454e2392006-06-23 02:02:57 -0700911
912 mnt->mnt_mountpoint = mnt->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 mnt->mnt_parent = mnt;
David Howells454e2392006-06-23 02:02:57 -0700914 up_write(&mnt->mnt_sb->s_umount);
Gerald Schaefer8680e222005-06-21 17:15:16 -0700915 free_secdata(secdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return mnt;
917out_sb:
David Howells454e2392006-06-23 02:02:57 -0700918 dput(mnt->mnt_root);
919 up_write(&mnt->mnt_sb->s_umount);
920 deactivate_super(mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921out_free_secdata:
922 free_secdata(secdata);
923out_mnt:
924 free_vfsmnt(mnt);
925out:
David Howells454e2392006-06-23 02:02:57 -0700926 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400929EXPORT_SYMBOL_GPL(vfs_kern_mount);
930
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700931static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
932{
933 int err;
934 const char *subtype = strchr(fstype, '.');
935 if (subtype) {
936 subtype++;
937 err = -EINVAL;
938 if (!subtype[0])
939 goto err;
940 } else
941 subtype = "";
942
943 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
944 err = -ENOMEM;
945 if (!mnt->mnt_sb->s_subtype)
946 goto err;
947 return mnt;
948
949 err:
950 mntput(mnt);
951 return ERR_PTR(err);
952}
953
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400954struct vfsmount *
955do_kern_mount(const char *fstype, int flags, const char *name, void *data)
956{
957 struct file_system_type *type = get_fs_type(fstype);
958 struct vfsmount *mnt;
959 if (!type)
960 return ERR_PTR(-ENODEV);
961 mnt = vfs_kern_mount(type, flags, name, data);
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -0700962 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
963 !mnt->mnt_sb->s_subtype)
964 mnt = fs_set_subtype(mnt, fstype);
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400965 put_filesystem(type);
966 return mnt;
967}
Al Viro8a4e98d2008-02-24 01:43:03 -0500968EXPORT_SYMBOL_GPL(do_kern_mount);
Trond Myklebustbb4a58b2006-06-09 09:34:15 -0400969
Pavel Emelyanov8bf97252007-10-18 23:40:02 -0700970struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Pavel Emelyanov8bf97252007-10-18 23:40:02 -0700972 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Pavel Emelyanov8bf97252007-10-18 23:40:02 -0700975EXPORT_SYMBOL_GPL(kern_mount_data);