diff options
Diffstat (limited to 'fs/super.c')
-rw-r--r-- | fs/super.c | 860 |
1 files changed, 860 insertions, 0 deletions
diff --git a/fs/super.c b/fs/super.c new file mode 100644 index 000000000000..3a1b8ca04ba6 --- /dev/null +++ b/fs/super.c | |||
@@ -0,0 +1,860 @@ | |||
1 | /* | ||
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 | |||
23 | #include <linux/config.h> | ||
24 | #include <linux/module.h> | ||
25 | #include <linux/slab.h> | ||
26 | #include <linux/init.h> | ||
27 | #include <linux/smp_lock.h> | ||
28 | #include <linux/acct.h> | ||
29 | #include <linux/blkdev.h> | ||
30 | #include <linux/quotaops.h> | ||
31 | #include <linux/namei.h> | ||
32 | #include <linux/buffer_head.h> /* for fsync_super() */ | ||
33 | #include <linux/mount.h> | ||
34 | #include <linux/security.h> | ||
35 | #include <linux/syscalls.h> | ||
36 | #include <linux/vfs.h> | ||
37 | #include <linux/writeback.h> /* for the emergency remount stuff */ | ||
38 | #include <linux/idr.h> | ||
39 | #include <linux/kobject.h> | ||
40 | #include <asm/uaccess.h> | ||
41 | |||
42 | |||
43 | void get_filesystem(struct file_system_type *fs); | ||
44 | void put_filesystem(struct file_system_type *fs); | ||
45 | struct file_system_type *get_fs_type(const char *name); | ||
46 | |||
47 | LIST_HEAD(super_blocks); | ||
48 | DEFINE_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 | */ | ||
56 | static struct super_block *alloc_super(void) | ||
57 | { | ||
58 | struct super_block *s = kmalloc(sizeof(struct super_block), GFP_USER); | ||
59 | static struct super_operations default_op; | ||
60 | |||
61 | if (s) { | ||
62 | memset(s, 0, sizeof(struct super_block)); | ||
63 | if (security_sb_alloc(s)) { | ||
64 | kfree(s); | ||
65 | s = NULL; | ||
66 | goto out; | ||
67 | } | ||
68 | INIT_LIST_HEAD(&s->s_dirty); | ||
69 | INIT_LIST_HEAD(&s->s_io); | ||
70 | INIT_LIST_HEAD(&s->s_files); | ||
71 | INIT_LIST_HEAD(&s->s_instances); | ||
72 | INIT_HLIST_HEAD(&s->s_anon); | ||
73 | INIT_LIST_HEAD(&s->s_inodes); | ||
74 | init_rwsem(&s->s_umount); | ||
75 | sema_init(&s->s_lock, 1); | ||
76 | down_write(&s->s_umount); | ||
77 | s->s_count = S_BIAS; | ||
78 | atomic_set(&s->s_active, 1); | ||
79 | sema_init(&s->s_vfs_rename_sem,1); | ||
80 | sema_init(&s->s_dquot.dqio_sem, 1); | ||
81 | sema_init(&s->s_dquot.dqonoff_sem, 1); | ||
82 | init_rwsem(&s->s_dquot.dqptr_sem); | ||
83 | init_waitqueue_head(&s->s_wait_unfrozen); | ||
84 | s->s_maxbytes = MAX_NON_LFS; | ||
85 | s->dq_op = sb_dquot_ops; | ||
86 | s->s_qcop = sb_quotactl_ops; | ||
87 | s->s_op = &default_op; | ||
88 | s->s_time_gran = 1000000000; | ||
89 | } | ||
90 | out: | ||
91 | return s; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * destroy_super - frees a superblock | ||
96 | * @s: superblock to free | ||
97 | * | ||
98 | * Frees a superblock. | ||
99 | */ | ||
100 | static inline void destroy_super(struct super_block *s) | ||
101 | { | ||
102 | security_sb_free(s); | ||
103 | kfree(s); | ||
104 | } | ||
105 | |||
106 | /* Superblock refcounting */ | ||
107 | |||
108 | /* | ||
109 | * Drop a superblock's refcount. Returns non-zero if the superblock was | ||
110 | * destroyed. The caller must hold sb_lock. | ||
111 | */ | ||
112 | int __put_super(struct super_block *sb) | ||
113 | { | ||
114 | int ret = 0; | ||
115 | |||
116 | if (!--sb->s_count) { | ||
117 | destroy_super(sb); | ||
118 | ret = 1; | ||
119 | } | ||
120 | return ret; | ||
121 | } | ||
122 | |||
123 | /* | ||
124 | * Drop a superblock's refcount. | ||
125 | * Returns non-zero if the superblock is about to be destroyed and | ||
126 | * at least is already removed from super_blocks list, so if we are | ||
127 | * making a loop through super blocks then we need to restart. | ||
128 | * The caller must hold sb_lock. | ||
129 | */ | ||
130 | int __put_super_and_need_restart(struct super_block *sb) | ||
131 | { | ||
132 | /* check for race with generic_shutdown_super() */ | ||
133 | if (list_empty(&sb->s_list)) { | ||
134 | /* super block is removed, need to restart... */ | ||
135 | __put_super(sb); | ||
136 | return 1; | ||
137 | } | ||
138 | /* can't be the last, since s_list is still in use */ | ||
139 | sb->s_count--; | ||
140 | BUG_ON(sb->s_count == 0); | ||
141 | return 0; | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * put_super - drop a temporary reference to superblock | ||
146 | * @sb: superblock in question | ||
147 | * | ||
148 | * Drops a temporary reference, frees superblock if there's no | ||
149 | * references left. | ||
150 | */ | ||
151 | static void put_super(struct super_block *sb) | ||
152 | { | ||
153 | spin_lock(&sb_lock); | ||
154 | __put_super(sb); | ||
155 | spin_unlock(&sb_lock); | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * deactivate_super - drop an active reference to superblock | ||
161 | * @s: superblock to deactivate | ||
162 | * | ||
163 | * Drops an active reference to superblock, acquiring a temprory one if | ||
164 | * there is no active references left. In that case we lock superblock, | ||
165 | * tell fs driver to shut it down and drop the temporary reference we | ||
166 | * had just acquired. | ||
167 | */ | ||
168 | void deactivate_super(struct super_block *s) | ||
169 | { | ||
170 | struct file_system_type *fs = s->s_type; | ||
171 | if (atomic_dec_and_lock(&s->s_active, &sb_lock)) { | ||
172 | s->s_count -= S_BIAS-1; | ||
173 | spin_unlock(&sb_lock); | ||
174 | down_write(&s->s_umount); | ||
175 | fs->kill_sb(s); | ||
176 | put_filesystem(fs); | ||
177 | put_super(s); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | EXPORT_SYMBOL(deactivate_super); | ||
182 | |||
183 | /** | ||
184 | * grab_super - acquire an active reference | ||
185 | * @s: reference we are trying to make active | ||
186 | * | ||
187 | * Tries to acquire an active reference. grab_super() is used when we | ||
188 | * had just found a superblock in super_blocks or fs_type->fs_supers | ||
189 | * and want to turn it into a full-blown active reference. grab_super() | ||
190 | * is called with sb_lock held and drops it. Returns 1 in case of | ||
191 | * success, 0 if we had failed (superblock contents was already dead or | ||
192 | * dying when grab_super() had been called). | ||
193 | */ | ||
194 | static int grab_super(struct super_block *s) | ||
195 | { | ||
196 | s->s_count++; | ||
197 | spin_unlock(&sb_lock); | ||
198 | down_write(&s->s_umount); | ||
199 | if (s->s_root) { | ||
200 | spin_lock(&sb_lock); | ||
201 | if (s->s_count > S_BIAS) { | ||
202 | atomic_inc(&s->s_active); | ||
203 | s->s_count--; | ||
204 | spin_unlock(&sb_lock); | ||
205 | return 1; | ||
206 | } | ||
207 | spin_unlock(&sb_lock); | ||
208 | } | ||
209 | up_write(&s->s_umount); | ||
210 | put_super(s); | ||
211 | yield(); | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | /** | ||
216 | * generic_shutdown_super - common helper for ->kill_sb() | ||
217 | * @sb: superblock to kill | ||
218 | * | ||
219 | * generic_shutdown_super() does all fs-independent work on superblock | ||
220 | * shutdown. Typical ->kill_sb() should pick all fs-specific objects | ||
221 | * that need destruction out of superblock, call generic_shutdown_super() | ||
222 | * and release aforementioned objects. Note: dentries and inodes _are_ | ||
223 | * taken care of and do not need specific handling. | ||
224 | */ | ||
225 | void generic_shutdown_super(struct super_block *sb) | ||
226 | { | ||
227 | struct dentry *root = sb->s_root; | ||
228 | struct super_operations *sop = sb->s_op; | ||
229 | |||
230 | if (root) { | ||
231 | sb->s_root = NULL; | ||
232 | shrink_dcache_parent(root); | ||
233 | shrink_dcache_anon(&sb->s_anon); | ||
234 | dput(root); | ||
235 | fsync_super(sb); | ||
236 | lock_super(sb); | ||
237 | sb->s_flags &= ~MS_ACTIVE; | ||
238 | /* bad name - it should be evict_inodes() */ | ||
239 | invalidate_inodes(sb); | ||
240 | lock_kernel(); | ||
241 | |||
242 | if (sop->write_super && sb->s_dirt) | ||
243 | sop->write_super(sb); | ||
244 | if (sop->put_super) | ||
245 | sop->put_super(sb); | ||
246 | |||
247 | /* Forget any remaining inodes */ | ||
248 | if (invalidate_inodes(sb)) { | ||
249 | printk("VFS: Busy inodes after unmount. " | ||
250 | "Self-destruct in 5 seconds. Have a nice day...\n"); | ||
251 | } | ||
252 | |||
253 | unlock_kernel(); | ||
254 | unlock_super(sb); | ||
255 | } | ||
256 | spin_lock(&sb_lock); | ||
257 | /* should be initialized for __put_super_and_need_restart() */ | ||
258 | list_del_init(&sb->s_list); | ||
259 | list_del(&sb->s_instances); | ||
260 | spin_unlock(&sb_lock); | ||
261 | up_write(&sb->s_umount); | ||
262 | } | ||
263 | |||
264 | EXPORT_SYMBOL(generic_shutdown_super); | ||
265 | |||
266 | /** | ||
267 | * sget - find or create a superblock | ||
268 | * @type: filesystem type superblock should belong to | ||
269 | * @test: comparison callback | ||
270 | * @set: setup callback | ||
271 | * @data: argument to each of them | ||
272 | */ | ||
273 | struct super_block *sget(struct file_system_type *type, | ||
274 | int (*test)(struct super_block *,void *), | ||
275 | int (*set)(struct super_block *,void *), | ||
276 | void *data) | ||
277 | { | ||
278 | struct super_block *s = NULL; | ||
279 | struct list_head *p; | ||
280 | int err; | ||
281 | |||
282 | retry: | ||
283 | spin_lock(&sb_lock); | ||
284 | if (test) list_for_each(p, &type->fs_supers) { | ||
285 | struct super_block *old; | ||
286 | old = list_entry(p, struct super_block, s_instances); | ||
287 | if (!test(old, data)) | ||
288 | continue; | ||
289 | if (!grab_super(old)) | ||
290 | goto retry; | ||
291 | if (s) | ||
292 | destroy_super(s); | ||
293 | return old; | ||
294 | } | ||
295 | if (!s) { | ||
296 | spin_unlock(&sb_lock); | ||
297 | s = alloc_super(); | ||
298 | if (!s) | ||
299 | return ERR_PTR(-ENOMEM); | ||
300 | goto retry; | ||
301 | } | ||
302 | |||
303 | err = set(s, data); | ||
304 | if (err) { | ||
305 | spin_unlock(&sb_lock); | ||
306 | destroy_super(s); | ||
307 | return ERR_PTR(err); | ||
308 | } | ||
309 | s->s_type = type; | ||
310 | strlcpy(s->s_id, type->name, sizeof(s->s_id)); | ||
311 | list_add_tail(&s->s_list, &super_blocks); | ||
312 | list_add(&s->s_instances, &type->fs_supers); | ||
313 | spin_unlock(&sb_lock); | ||
314 | get_filesystem(type); | ||
315 | return s; | ||
316 | } | ||
317 | |||
318 | EXPORT_SYMBOL(sget); | ||
319 | |||
320 | void drop_super(struct super_block *sb) | ||
321 | { | ||
322 | up_read(&sb->s_umount); | ||
323 | put_super(sb); | ||
324 | } | ||
325 | |||
326 | EXPORT_SYMBOL(drop_super); | ||
327 | |||
328 | static inline void write_super(struct super_block *sb) | ||
329 | { | ||
330 | lock_super(sb); | ||
331 | if (sb->s_root && sb->s_dirt) | ||
332 | if (sb->s_op->write_super) | ||
333 | sb->s_op->write_super(sb); | ||
334 | unlock_super(sb); | ||
335 | } | ||
336 | |||
337 | /* | ||
338 | * Note: check the dirty flag before waiting, so we don't | ||
339 | * hold up the sync while mounting a device. (The newly | ||
340 | * mounted device won't need syncing.) | ||
341 | */ | ||
342 | void sync_supers(void) | ||
343 | { | ||
344 | struct super_block * sb; | ||
345 | restart: | ||
346 | spin_lock(&sb_lock); | ||
347 | sb = sb_entry(super_blocks.next); | ||
348 | while (sb != sb_entry(&super_blocks)) | ||
349 | if (sb->s_dirt) { | ||
350 | sb->s_count++; | ||
351 | spin_unlock(&sb_lock); | ||
352 | down_read(&sb->s_umount); | ||
353 | write_super(sb); | ||
354 | drop_super(sb); | ||
355 | goto restart; | ||
356 | } else | ||
357 | sb = sb_entry(sb->s_list.next); | ||
358 | spin_unlock(&sb_lock); | ||
359 | } | ||
360 | |||
361 | /* | ||
362 | * Call the ->sync_fs super_op against all filesytems which are r/w and | ||
363 | * which implement it. | ||
364 | * | ||
365 | * This operation is careful to avoid the livelock which could easily happen | ||
366 | * if two or more filesystems are being continuously dirtied. s_need_sync_fs | ||
367 | * is used only here. We set it against all filesystems and then clear it as | ||
368 | * we sync them. So redirtied filesystems are skipped. | ||
369 | * | ||
370 | * But if process A is currently running sync_filesytems and then process B | ||
371 | * calls sync_filesystems as well, process B will set all the s_need_sync_fs | ||
372 | * flags again, which will cause process A to resync everything. Fix that with | ||
373 | * a local mutex. | ||
374 | * | ||
375 | * (Fabian) Avoid sync_fs with clean fs & wait mode 0 | ||
376 | */ | ||
377 | void sync_filesystems(int wait) | ||
378 | { | ||
379 | struct super_block *sb; | ||
380 | static DECLARE_MUTEX(mutex); | ||
381 | |||
382 | down(&mutex); /* Could be down_interruptible */ | ||
383 | spin_lock(&sb_lock); | ||
384 | for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks); | ||
385 | sb = sb_entry(sb->s_list.next)) { | ||
386 | if (!sb->s_op->sync_fs) | ||
387 | continue; | ||
388 | if (sb->s_flags & MS_RDONLY) | ||
389 | continue; | ||
390 | sb->s_need_sync_fs = 1; | ||
391 | } | ||
392 | spin_unlock(&sb_lock); | ||
393 | |||
394 | restart: | ||
395 | spin_lock(&sb_lock); | ||
396 | for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks); | ||
397 | sb = sb_entry(sb->s_list.next)) { | ||
398 | if (!sb->s_need_sync_fs) | ||
399 | continue; | ||
400 | sb->s_need_sync_fs = 0; | ||
401 | if (sb->s_flags & MS_RDONLY) | ||
402 | continue; /* hm. Was remounted r/o meanwhile */ | ||
403 | sb->s_count++; | ||
404 | spin_unlock(&sb_lock); | ||
405 | down_read(&sb->s_umount); | ||
406 | if (sb->s_root && (wait || sb->s_dirt)) | ||
407 | sb->s_op->sync_fs(sb, wait); | ||
408 | drop_super(sb); | ||
409 | goto restart; | ||
410 | } | ||
411 | spin_unlock(&sb_lock); | ||
412 | up(&mutex); | ||
413 | } | ||
414 | |||
415 | /** | ||
416 | * get_super - get the superblock of a device | ||
417 | * @bdev: device to get the superblock for | ||
418 | * | ||
419 | * Scans the superblock list and finds the superblock of the file system | ||
420 | * mounted on the device given. %NULL is returned if no match is found. | ||
421 | */ | ||
422 | |||
423 | struct super_block * get_super(struct block_device *bdev) | ||
424 | { | ||
425 | struct list_head *p; | ||
426 | if (!bdev) | ||
427 | return NULL; | ||
428 | rescan: | ||
429 | spin_lock(&sb_lock); | ||
430 | list_for_each(p, &super_blocks) { | ||
431 | struct super_block *s = sb_entry(p); | ||
432 | if (s->s_bdev == bdev) { | ||
433 | s->s_count++; | ||
434 | spin_unlock(&sb_lock); | ||
435 | down_read(&s->s_umount); | ||
436 | if (s->s_root) | ||
437 | return s; | ||
438 | drop_super(s); | ||
439 | goto rescan; | ||
440 | } | ||
441 | } | ||
442 | spin_unlock(&sb_lock); | ||
443 | return NULL; | ||
444 | } | ||
445 | |||
446 | EXPORT_SYMBOL(get_super); | ||
447 | |||
448 | struct super_block * user_get_super(dev_t dev) | ||
449 | { | ||
450 | struct list_head *p; | ||
451 | |||
452 | rescan: | ||
453 | spin_lock(&sb_lock); | ||
454 | list_for_each(p, &super_blocks) { | ||
455 | struct super_block *s = sb_entry(p); | ||
456 | if (s->s_dev == dev) { | ||
457 | s->s_count++; | ||
458 | spin_unlock(&sb_lock); | ||
459 | down_read(&s->s_umount); | ||
460 | if (s->s_root) | ||
461 | return s; | ||
462 | drop_super(s); | ||
463 | goto rescan; | ||
464 | } | ||
465 | } | ||
466 | spin_unlock(&sb_lock); | ||
467 | return NULL; | ||
468 | } | ||
469 | |||
470 | EXPORT_SYMBOL(user_get_super); | ||
471 | |||
472 | asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf) | ||
473 | { | ||
474 | struct super_block *s; | ||
475 | struct ustat tmp; | ||
476 | struct kstatfs sbuf; | ||
477 | int err = -EINVAL; | ||
478 | |||
479 | s = user_get_super(new_decode_dev(dev)); | ||
480 | if (s == NULL) | ||
481 | goto out; | ||
482 | err = vfs_statfs(s, &sbuf); | ||
483 | drop_super(s); | ||
484 | if (err) | ||
485 | goto out; | ||
486 | |||
487 | memset(&tmp,0,sizeof(struct ustat)); | ||
488 | tmp.f_tfree = sbuf.f_bfree; | ||
489 | tmp.f_tinode = sbuf.f_ffree; | ||
490 | |||
491 | err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0; | ||
492 | out: | ||
493 | return err; | ||
494 | } | ||
495 | |||
496 | /** | ||
497 | * mark_files_ro | ||
498 | * @sb: superblock in question | ||
499 | * | ||
500 | * All files are marked read/only. We don't care about pending | ||
501 | * delete files so this should be used in 'force' mode only | ||
502 | */ | ||
503 | |||
504 | static void mark_files_ro(struct super_block *sb) | ||
505 | { | ||
506 | struct file *f; | ||
507 | |||
508 | file_list_lock(); | ||
509 | list_for_each_entry(f, &sb->s_files, f_list) { | ||
510 | if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f)) | ||
511 | f->f_mode &= ~FMODE_WRITE; | ||
512 | } | ||
513 | file_list_unlock(); | ||
514 | } | ||
515 | |||
516 | /** | ||
517 | * do_remount_sb - asks filesystem to change mount options. | ||
518 | * @sb: superblock in question | ||
519 | * @flags: numeric part of options | ||
520 | * @data: the rest of options | ||
521 | * @force: whether or not to force the change | ||
522 | * | ||
523 | * Alters the mount options of a mounted file system. | ||
524 | */ | ||
525 | int do_remount_sb(struct super_block *sb, int flags, void *data, int force) | ||
526 | { | ||
527 | int retval; | ||
528 | |||
529 | if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev)) | ||
530 | return -EACCES; | ||
531 | if (flags & MS_RDONLY) | ||
532 | acct_auto_close(sb); | ||
533 | shrink_dcache_sb(sb); | ||
534 | fsync_super(sb); | ||
535 | |||
536 | /* If we are remounting RDONLY and current sb is read/write, | ||
537 | make sure there are no rw files opened */ | ||
538 | if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) { | ||
539 | if (force) | ||
540 | mark_files_ro(sb); | ||
541 | else if (!fs_may_remount_ro(sb)) | ||
542 | return -EBUSY; | ||
543 | } | ||
544 | |||
545 | if (sb->s_op->remount_fs) { | ||
546 | lock_super(sb); | ||
547 | retval = sb->s_op->remount_fs(sb, &flags, data); | ||
548 | unlock_super(sb); | ||
549 | if (retval) | ||
550 | return retval; | ||
551 | } | ||
552 | sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK); | ||
553 | return 0; | ||
554 | } | ||
555 | |||
556 | static void do_emergency_remount(unsigned long foo) | ||
557 | { | ||
558 | struct super_block *sb; | ||
559 | |||
560 | spin_lock(&sb_lock); | ||
561 | list_for_each_entry(sb, &super_blocks, s_list) { | ||
562 | sb->s_count++; | ||
563 | spin_unlock(&sb_lock); | ||
564 | down_read(&sb->s_umount); | ||
565 | if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) { | ||
566 | /* | ||
567 | * ->remount_fs needs lock_kernel(). | ||
568 | * | ||
569 | * What lock protects sb->s_flags?? | ||
570 | */ | ||
571 | lock_kernel(); | ||
572 | do_remount_sb(sb, MS_RDONLY, NULL, 1); | ||
573 | unlock_kernel(); | ||
574 | } | ||
575 | drop_super(sb); | ||
576 | spin_lock(&sb_lock); | ||
577 | } | ||
578 | spin_unlock(&sb_lock); | ||
579 | printk("Emergency Remount complete\n"); | ||
580 | } | ||
581 | |||
582 | void emergency_remount(void) | ||
583 | { | ||
584 | pdflush_operation(do_emergency_remount, 0); | ||
585 | } | ||
586 | |||
587 | /* | ||
588 | * Unnamed block devices are dummy devices used by virtual | ||
589 | * filesystems which don't use real block-devices. -- jrs | ||
590 | */ | ||
591 | |||
592 | static struct idr unnamed_dev_idr; | ||
593 | static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */ | ||
594 | |||
595 | int set_anon_super(struct super_block *s, void *data) | ||
596 | { | ||
597 | int dev; | ||
598 | int error; | ||
599 | |||
600 | retry: | ||
601 | if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0) | ||
602 | return -ENOMEM; | ||
603 | spin_lock(&unnamed_dev_lock); | ||
604 | error = idr_get_new(&unnamed_dev_idr, NULL, &dev); | ||
605 | spin_unlock(&unnamed_dev_lock); | ||
606 | if (error == -EAGAIN) | ||
607 | /* We raced and lost with another CPU. */ | ||
608 | goto retry; | ||
609 | else if (error) | ||
610 | return -EAGAIN; | ||
611 | |||
612 | if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) { | ||
613 | spin_lock(&unnamed_dev_lock); | ||
614 | idr_remove(&unnamed_dev_idr, dev); | ||
615 | spin_unlock(&unnamed_dev_lock); | ||
616 | return -EMFILE; | ||
617 | } | ||
618 | s->s_dev = MKDEV(0, dev & MINORMASK); | ||
619 | return 0; | ||
620 | } | ||
621 | |||
622 | EXPORT_SYMBOL(set_anon_super); | ||
623 | |||
624 | void kill_anon_super(struct super_block *sb) | ||
625 | { | ||
626 | int slot = MINOR(sb->s_dev); | ||
627 | |||
628 | generic_shutdown_super(sb); | ||
629 | spin_lock(&unnamed_dev_lock); | ||
630 | idr_remove(&unnamed_dev_idr, slot); | ||
631 | spin_unlock(&unnamed_dev_lock); | ||
632 | } | ||
633 | |||
634 | EXPORT_SYMBOL(kill_anon_super); | ||
635 | |||
636 | void __init unnamed_dev_init(void) | ||
637 | { | ||
638 | idr_init(&unnamed_dev_idr); | ||
639 | } | ||
640 | |||
641 | void kill_litter_super(struct super_block *sb) | ||
642 | { | ||
643 | if (sb->s_root) | ||
644 | d_genocide(sb->s_root); | ||
645 | kill_anon_super(sb); | ||
646 | } | ||
647 | |||
648 | EXPORT_SYMBOL(kill_litter_super); | ||
649 | |||
650 | static int set_bdev_super(struct super_block *s, void *data) | ||
651 | { | ||
652 | s->s_bdev = data; | ||
653 | s->s_dev = s->s_bdev->bd_dev; | ||
654 | return 0; | ||
655 | } | ||
656 | |||
657 | static int test_bdev_super(struct super_block *s, void *data) | ||
658 | { | ||
659 | return (void *)s->s_bdev == data; | ||
660 | } | ||
661 | |||
662 | static void bdev_uevent(struct block_device *bdev, enum kobject_action action) | ||
663 | { | ||
664 | if (bdev->bd_disk) { | ||
665 | if (bdev->bd_part) | ||
666 | kobject_uevent(&bdev->bd_part->kobj, action, NULL); | ||
667 | else | ||
668 | kobject_uevent(&bdev->bd_disk->kobj, action, NULL); | ||
669 | } | ||
670 | } | ||
671 | |||
672 | struct super_block *get_sb_bdev(struct file_system_type *fs_type, | ||
673 | int flags, const char *dev_name, void *data, | ||
674 | int (*fill_super)(struct super_block *, void *, int)) | ||
675 | { | ||
676 | struct block_device *bdev; | ||
677 | struct super_block *s; | ||
678 | int error = 0; | ||
679 | |||
680 | bdev = open_bdev_excl(dev_name, flags, fs_type); | ||
681 | if (IS_ERR(bdev)) | ||
682 | return (struct super_block *)bdev; | ||
683 | |||
684 | /* | ||
685 | * once the super is inserted into the list by sget, s_umount | ||
686 | * will protect the lockfs code from trying to start a snapshot | ||
687 | * while we are mounting | ||
688 | */ | ||
689 | down(&bdev->bd_mount_sem); | ||
690 | s = sget(fs_type, test_bdev_super, set_bdev_super, bdev); | ||
691 | up(&bdev->bd_mount_sem); | ||
692 | if (IS_ERR(s)) | ||
693 | goto out; | ||
694 | |||
695 | if (s->s_root) { | ||
696 | if ((flags ^ s->s_flags) & MS_RDONLY) { | ||
697 | up_write(&s->s_umount); | ||
698 | deactivate_super(s); | ||
699 | s = ERR_PTR(-EBUSY); | ||
700 | } | ||
701 | goto out; | ||
702 | } else { | ||
703 | char b[BDEVNAME_SIZE]; | ||
704 | |||
705 | s->s_flags = flags; | ||
706 | strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); | ||
707 | s->s_old_blocksize = block_size(bdev); | ||
708 | sb_set_blocksize(s, s->s_old_blocksize); | ||
709 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); | ||
710 | if (error) { | ||
711 | up_write(&s->s_umount); | ||
712 | deactivate_super(s); | ||
713 | s = ERR_PTR(error); | ||
714 | } else { | ||
715 | s->s_flags |= MS_ACTIVE; | ||
716 | bdev_uevent(bdev, KOBJ_MOUNT); | ||
717 | } | ||
718 | } | ||
719 | |||
720 | return s; | ||
721 | |||
722 | out: | ||
723 | close_bdev_excl(bdev); | ||
724 | return s; | ||
725 | } | ||
726 | |||
727 | EXPORT_SYMBOL(get_sb_bdev); | ||
728 | |||
729 | void kill_block_super(struct super_block *sb) | ||
730 | { | ||
731 | struct block_device *bdev = sb->s_bdev; | ||
732 | |||
733 | bdev_uevent(bdev, KOBJ_UMOUNT); | ||
734 | generic_shutdown_super(sb); | ||
735 | sync_blockdev(bdev); | ||
736 | close_bdev_excl(bdev); | ||
737 | } | ||
738 | |||
739 | EXPORT_SYMBOL(kill_block_super); | ||
740 | |||
741 | struct super_block *get_sb_nodev(struct file_system_type *fs_type, | ||
742 | int flags, void *data, | ||
743 | int (*fill_super)(struct super_block *, void *, int)) | ||
744 | { | ||
745 | int error; | ||
746 | struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL); | ||
747 | |||
748 | if (IS_ERR(s)) | ||
749 | return s; | ||
750 | |||
751 | s->s_flags = flags; | ||
752 | |||
753 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); | ||
754 | if (error) { | ||
755 | up_write(&s->s_umount); | ||
756 | deactivate_super(s); | ||
757 | return ERR_PTR(error); | ||
758 | } | ||
759 | s->s_flags |= MS_ACTIVE; | ||
760 | return s; | ||
761 | } | ||
762 | |||
763 | EXPORT_SYMBOL(get_sb_nodev); | ||
764 | |||
765 | static int compare_single(struct super_block *s, void *p) | ||
766 | { | ||
767 | return 1; | ||
768 | } | ||
769 | |||
770 | struct super_block *get_sb_single(struct file_system_type *fs_type, | ||
771 | int flags, void *data, | ||
772 | int (*fill_super)(struct super_block *, void *, int)) | ||
773 | { | ||
774 | struct super_block *s; | ||
775 | int error; | ||
776 | |||
777 | s = sget(fs_type, compare_single, set_anon_super, NULL); | ||
778 | if (IS_ERR(s)) | ||
779 | return s; | ||
780 | if (!s->s_root) { | ||
781 | s->s_flags = flags; | ||
782 | error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); | ||
783 | if (error) { | ||
784 | up_write(&s->s_umount); | ||
785 | deactivate_super(s); | ||
786 | return ERR_PTR(error); | ||
787 | } | ||
788 | s->s_flags |= MS_ACTIVE; | ||
789 | } | ||
790 | do_remount_sb(s, flags, data, 0); | ||
791 | return s; | ||
792 | } | ||
793 | |||
794 | EXPORT_SYMBOL(get_sb_single); | ||
795 | |||
796 | struct vfsmount * | ||
797 | do_kern_mount(const char *fstype, int flags, const char *name, void *data) | ||
798 | { | ||
799 | struct file_system_type *type = get_fs_type(fstype); | ||
800 | struct super_block *sb = ERR_PTR(-ENOMEM); | ||
801 | struct vfsmount *mnt; | ||
802 | int error; | ||
803 | char *secdata = NULL; | ||
804 | |||
805 | if (!type) | ||
806 | return ERR_PTR(-ENODEV); | ||
807 | |||
808 | mnt = alloc_vfsmnt(name); | ||
809 | if (!mnt) | ||
810 | goto out; | ||
811 | |||
812 | if (data) { | ||
813 | secdata = alloc_secdata(); | ||
814 | if (!secdata) { | ||
815 | sb = ERR_PTR(-ENOMEM); | ||
816 | goto out_mnt; | ||
817 | } | ||
818 | |||
819 | error = security_sb_copy_data(type, data, secdata); | ||
820 | if (error) { | ||
821 | sb = ERR_PTR(error); | ||
822 | goto out_free_secdata; | ||
823 | } | ||
824 | } | ||
825 | |||
826 | sb = type->get_sb(type, flags, name, data); | ||
827 | if (IS_ERR(sb)) | ||
828 | goto out_free_secdata; | ||
829 | error = security_sb_kern_mount(sb, secdata); | ||
830 | if (error) | ||
831 | goto out_sb; | ||
832 | mnt->mnt_sb = sb; | ||
833 | mnt->mnt_root = dget(sb->s_root); | ||
834 | mnt->mnt_mountpoint = sb->s_root; | ||
835 | mnt->mnt_parent = mnt; | ||
836 | mnt->mnt_namespace = current->namespace; | ||
837 | up_write(&sb->s_umount); | ||
838 | put_filesystem(type); | ||
839 | return mnt; | ||
840 | out_sb: | ||
841 | up_write(&sb->s_umount); | ||
842 | deactivate_super(sb); | ||
843 | sb = ERR_PTR(error); | ||
844 | out_free_secdata: | ||
845 | free_secdata(secdata); | ||
846 | out_mnt: | ||
847 | free_vfsmnt(mnt); | ||
848 | out: | ||
849 | put_filesystem(type); | ||
850 | return (struct vfsmount *)sb; | ||
851 | } | ||
852 | |||
853 | EXPORT_SYMBOL_GPL(do_kern_mount); | ||
854 | |||
855 | struct vfsmount *kern_mount(struct file_system_type *type) | ||
856 | { | ||
857 | return do_kern_mount(type->name, 0, type->name, NULL); | ||
858 | } | ||
859 | |||
860 | EXPORT_SYMBOL(kern_mount); | ||