[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / super.c
blob9aa36aee628a8538035bdbf6d98384d0d1e28574
1 /*
2 * linux/fs/super.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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
13 * GK 2/5/95 - Changed to support mounting the root fs via NFS
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
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/vfs.h>
36 #include <linux/writeback.h> /* for the emergency remount stuff */
37 #include <linux/idr.h>
38 #include <asm/uaccess.h>
41 void get_filesystem(struct file_system_type *fs);
42 void put_filesystem(struct file_system_type *fs);
43 struct file_system_type *get_fs_type(const char *name);
45 LIST_HEAD(super_blocks);
46 spinlock_t sb_lock = SPIN_LOCK_UNLOCKED;
48 /**
49 * alloc_super - create new superblock
51 * Allocates and initializes a new &struct super_block. alloc_super()
52 * returns a pointer new superblock or %NULL if allocation had failed.
54 static struct super_block *alloc_super(void)
56 struct super_block *s = kmalloc(sizeof(struct super_block), GFP_USER);
57 static struct super_operations default_op;
59 if (s) {
60 memset(s, 0, sizeof(struct super_block));
61 if (security_sb_alloc(s)) {
62 kfree(s);
63 s = NULL;
64 goto out;
66 INIT_LIST_HEAD(&s->s_dirty);
67 INIT_LIST_HEAD(&s->s_io);
68 INIT_LIST_HEAD(&s->s_files);
69 INIT_LIST_HEAD(&s->s_instances);
70 INIT_HLIST_HEAD(&s->s_anon);
71 init_rwsem(&s->s_umount);
72 sema_init(&s->s_lock, 1);
73 down_write(&s->s_umount);
74 s->s_count = S_BIAS;
75 atomic_set(&s->s_active, 1);
76 sema_init(&s->s_vfs_rename_sem,1);
77 sema_init(&s->s_dquot.dqio_sem, 1);
78 sema_init(&s->s_dquot.dqonoff_sem, 1);
79 init_rwsem(&s->s_dquot.dqptr_sem);
80 s->s_maxbytes = MAX_NON_LFS;
81 s->dq_op = sb_dquot_ops;
82 s->s_qcop = sb_quotactl_ops;
83 s->s_op = &default_op;
85 out:
86 return s;
89 /**
90 * destroy_super - frees a superblock
91 * @s: superblock to free
93 * Frees a superblock.
95 static inline void destroy_super(struct super_block *s)
97 security_sb_free(s);
98 kfree(s);
101 /* Superblock refcounting */
104 * put_super - drop a temporary reference to superblock
105 * @s: superblock in question
107 * Drops a temporary reference, frees superblock if there's no
108 * references left.
110 static inline void put_super(struct super_block *s)
112 spin_lock(&sb_lock);
113 if (!--s->s_count)
114 destroy_super(s);
115 spin_unlock(&sb_lock);
119 * deactivate_super - drop an active reference to superblock
120 * @s: superblock to deactivate
122 * Drops an active reference to superblock, acquiring a temprory one if
123 * there is no active references left. In that case we lock superblock,
124 * tell fs driver to shut it down and drop the temporary reference we
125 * had just acquired.
127 void deactivate_super(struct super_block *s)
129 struct file_system_type *fs = s->s_type;
130 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
131 s->s_count -= S_BIAS-1;
132 spin_unlock(&sb_lock);
133 down_write(&s->s_umount);
134 fs->kill_sb(s);
135 put_filesystem(fs);
136 put_super(s);
140 EXPORT_SYMBOL(deactivate_super);
143 * grab_super - acquire an active reference
144 * @s: reference we are trying to make active
146 * Tries to acquire an active reference. grab_super() is used when we
147 * had just found a superblock in super_blocks or fs_type->fs_supers
148 * and want to turn it into a full-blown active reference. grab_super()
149 * is called with sb_lock held and drops it. Returns 1 in case of
150 * success, 0 if we had failed (superblock contents was already dead or
151 * dying when grab_super() had been called).
153 static int grab_super(struct super_block *s)
155 s->s_count++;
156 spin_unlock(&sb_lock);
157 down_write(&s->s_umount);
158 if (s->s_root) {
159 spin_lock(&sb_lock);
160 if (s->s_count > S_BIAS) {
161 atomic_inc(&s->s_active);
162 s->s_count--;
163 spin_unlock(&sb_lock);
164 return 1;
166 spin_unlock(&sb_lock);
168 up_write(&s->s_umount);
169 put_super(s);
170 yield();
171 return 0;
175 * generic_shutdown_super - common helper for ->kill_sb()
176 * @sb: superblock to kill
178 * generic_shutdown_super() does all fs-independent work on superblock
179 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
180 * that need destruction out of superblock, call generic_shutdown_super()
181 * and release aforementioned objects. Note: dentries and inodes _are_
182 * taken care of and do not need specific handling.
184 void generic_shutdown_super(struct super_block *sb)
186 struct dentry *root = sb->s_root;
187 struct super_operations *sop = sb->s_op;
189 if (root) {
190 sb->s_root = NULL;
191 shrink_dcache_parent(root);
192 shrink_dcache_anon(&sb->s_anon);
193 dput(root);
194 fsync_super(sb);
195 lock_super(sb);
196 lock_kernel();
197 sb->s_flags &= ~MS_ACTIVE;
198 /* bad name - it should be evict_inodes() */
199 invalidate_inodes(sb);
201 if (sop->write_super && sb->s_dirt)
202 sop->write_super(sb);
203 if (sop->put_super)
204 sop->put_super(sb);
206 /* Forget any remaining inodes */
207 if (invalidate_inodes(sb)) {
208 printk("VFS: Busy inodes after unmount. "
209 "Self-destruct in 5 seconds. Have a nice day...\n");
212 unlock_kernel();
213 unlock_super(sb);
215 spin_lock(&sb_lock);
216 list_del(&sb->s_list);
217 list_del(&sb->s_instances);
218 spin_unlock(&sb_lock);
219 up_write(&sb->s_umount);
222 EXPORT_SYMBOL(generic_shutdown_super);
225 * sget - find or create a superblock
226 * @type: filesystem type superblock should belong to
227 * @test: comparison callback
228 * @set: setup callback
229 * @data: argument to each of them
231 struct super_block *sget(struct file_system_type *type,
232 int (*test)(struct super_block *,void *),
233 int (*set)(struct super_block *,void *),
234 void *data)
236 struct super_block *s = NULL;
237 struct list_head *p;
238 int err;
240 retry:
241 spin_lock(&sb_lock);
242 if (test) list_for_each(p, &type->fs_supers) {
243 struct super_block *old;
244 old = list_entry(p, struct super_block, s_instances);
245 if (!test(old, data))
246 continue;
247 if (!grab_super(old))
248 goto retry;
249 if (s)
250 destroy_super(s);
251 return old;
253 if (!s) {
254 spin_unlock(&sb_lock);
255 s = alloc_super();
256 if (!s)
257 return ERR_PTR(-ENOMEM);
258 goto retry;
261 err = set(s, data);
262 if (err) {
263 spin_unlock(&sb_lock);
264 destroy_super(s);
265 return ERR_PTR(err);
267 s->s_type = type;
268 list_add(&s->s_list, super_blocks.prev);
269 list_add(&s->s_instances, &type->fs_supers);
270 spin_unlock(&sb_lock);
271 get_filesystem(type);
272 return s;
275 EXPORT_SYMBOL(sget);
277 void drop_super(struct super_block *sb)
279 up_read(&sb->s_umount);
280 put_super(sb);
283 EXPORT_SYMBOL(drop_super);
285 static inline void write_super(struct super_block *sb)
287 lock_super(sb);
288 if (sb->s_root && sb->s_dirt)
289 if (sb->s_op->write_super)
290 sb->s_op->write_super(sb);
291 unlock_super(sb);
295 * Note: check the dirty flag before waiting, so we don't
296 * hold up the sync while mounting a device. (The newly
297 * mounted device won't need syncing.)
299 void sync_supers(void)
301 struct super_block * sb;
302 restart:
303 spin_lock(&sb_lock);
304 sb = sb_entry(super_blocks.next);
305 while (sb != sb_entry(&super_blocks))
306 if (sb->s_dirt) {
307 sb->s_count++;
308 spin_unlock(&sb_lock);
309 down_read(&sb->s_umount);
310 write_super(sb);
311 drop_super(sb);
312 goto restart;
313 } else
314 sb = sb_entry(sb->s_list.next);
315 spin_unlock(&sb_lock);
319 * Call the ->sync_fs super_op against all filesytems which are r/w and
320 * which implement it.
322 * This operation is careful to avoid the livelock which could easily happen
323 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
324 * is used only here. We set it against all filesystems and then clear it as
325 * we sync them. So redirtied filesystems are skipped.
327 * But if process A is currently running sync_filesytems and then process B
328 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
329 * flags again, which will cause process A to resync everything. Fix that with
330 * a local mutex.
332 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
334 void sync_filesystems(int wait)
336 struct super_block *sb;
337 static DECLARE_MUTEX(mutex);
339 down(&mutex); /* Could be down_interruptible */
340 spin_lock(&sb_lock);
341 for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
342 sb = sb_entry(sb->s_list.next)) {
343 if (!sb->s_op->sync_fs)
344 continue;
345 if (sb->s_flags & MS_RDONLY)
346 continue;
347 sb->s_need_sync_fs = 1;
349 spin_unlock(&sb_lock);
351 restart:
352 spin_lock(&sb_lock);
353 for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
354 sb = sb_entry(sb->s_list.next)) {
355 if (!sb->s_need_sync_fs)
356 continue;
357 sb->s_need_sync_fs = 0;
358 if (sb->s_flags & MS_RDONLY)
359 continue; /* hm. Was remounted r/o meanwhile */
360 sb->s_count++;
361 spin_unlock(&sb_lock);
362 down_read(&sb->s_umount);
363 if (sb->s_root && (wait || sb->s_dirt))
364 sb->s_op->sync_fs(sb, wait);
365 drop_super(sb);
366 goto restart;
368 spin_unlock(&sb_lock);
369 up(&mutex);
373 * get_super - get the superblock of a device
374 * @bdev: device to get the superblock for
376 * Scans the superblock list and finds the superblock of the file system
377 * mounted on the device given. %NULL is returned if no match is found.
380 struct super_block * get_super(struct block_device *bdev)
382 struct list_head *p;
383 if (!bdev)
384 return NULL;
385 rescan:
386 spin_lock(&sb_lock);
387 list_for_each(p, &super_blocks) {
388 struct super_block *s = sb_entry(p);
389 if (s->s_bdev == bdev) {
390 s->s_count++;
391 spin_unlock(&sb_lock);
392 down_read(&s->s_umount);
393 if (s->s_root)
394 return s;
395 drop_super(s);
396 goto rescan;
399 spin_unlock(&sb_lock);
400 return NULL;
403 EXPORT_SYMBOL(get_super);
405 struct super_block * user_get_super(dev_t dev)
407 struct list_head *p;
409 rescan:
410 spin_lock(&sb_lock);
411 list_for_each(p, &super_blocks) {
412 struct super_block *s = sb_entry(p);
413 if (s->s_dev == dev) {
414 s->s_count++;
415 spin_unlock(&sb_lock);
416 down_read(&s->s_umount);
417 if (s->s_root)
418 return s;
419 drop_super(s);
420 goto rescan;
423 spin_unlock(&sb_lock);
424 return NULL;
427 EXPORT_SYMBOL(user_get_super);
429 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
431 struct super_block *s;
432 struct ustat tmp;
433 struct kstatfs sbuf;
434 int err = -EINVAL;
436 s = user_get_super(new_decode_dev(dev));
437 if (s == NULL)
438 goto out;
439 err = vfs_statfs(s, &sbuf);
440 drop_super(s);
441 if (err)
442 goto out;
444 memset(&tmp,0,sizeof(struct ustat));
445 tmp.f_tfree = sbuf.f_bfree;
446 tmp.f_tinode = sbuf.f_ffree;
448 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
449 out:
450 return err;
454 * mark_files_ro
455 * @sb: superblock in question
457 * All files are marked read/only. We don't care about pending
458 * delete files so this should be used in 'force' mode only
461 static void mark_files_ro(struct super_block *sb)
463 struct file *f;
465 file_list_lock();
466 list_for_each_entry(f, &sb->s_files, f_list) {
467 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
468 f->f_mode &= ~FMODE_WRITE;
470 file_list_unlock();
474 * do_remount_sb - asks filesystem to change mount options.
475 * @sb: superblock in question
476 * @flags: numeric part of options
477 * @data: the rest of options
478 * @force: whether or not to force the change
480 * Alters the mount options of a mounted file system.
482 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
484 int retval;
486 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
487 return -EACCES;
488 if (flags & MS_RDONLY)
489 acct_auto_close(sb);
490 shrink_dcache_sb(sb);
491 fsync_super(sb);
493 /* If we are remounting RDONLY and current sb is read/write,
494 make sure there are no rw files opened */
495 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
496 if (force)
497 mark_files_ro(sb);
498 else if (!fs_may_remount_ro(sb))
499 return -EBUSY;
502 if (sb->s_op->remount_fs) {
503 lock_super(sb);
504 retval = sb->s_op->remount_fs(sb, &flags, data);
505 unlock_super(sb);
506 if (retval)
507 return retval;
509 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
510 return 0;
513 static void do_emergency_remount(unsigned long foo)
515 struct super_block *sb;
517 spin_lock(&sb_lock);
518 list_for_each_entry(sb, &super_blocks, s_list) {
519 sb->s_count++;
520 spin_unlock(&sb_lock);
521 down_read(&sb->s_umount);
522 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
524 * ->remount_fs needs lock_kernel().
526 * What lock protects sb->s_flags??
528 lock_kernel();
529 do_remount_sb(sb, MS_RDONLY, NULL, 1);
530 unlock_kernel();
532 drop_super(sb);
533 spin_lock(&sb_lock);
535 spin_unlock(&sb_lock);
536 printk("Emergency Remount complete\n");
539 void emergency_remount(void)
541 pdflush_operation(do_emergency_remount, 0);
545 * Unnamed block devices are dummy devices used by virtual
546 * filesystems which don't use real block-devices. -- jrs
549 static struct idr unnamed_dev_idr;
550 static spinlock_t unnamed_dev_lock = SPIN_LOCK_UNLOCKED;/* protects the above */
552 int set_anon_super(struct super_block *s, void *data)
554 int dev;
556 spin_lock(&unnamed_dev_lock);
557 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0) {
558 spin_unlock(&unnamed_dev_lock);
559 return -ENOMEM;
561 dev = idr_get_new(&unnamed_dev_idr, NULL);
562 spin_unlock(&unnamed_dev_lock);
564 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
565 idr_remove(&unnamed_dev_idr, dev);
566 return -EMFILE;
568 s->s_dev = MKDEV(0, dev & MINORMASK);
569 return 0;
572 EXPORT_SYMBOL(set_anon_super);
574 void kill_anon_super(struct super_block *sb)
576 int slot = MINOR(sb->s_dev);
578 generic_shutdown_super(sb);
579 spin_lock(&unnamed_dev_lock);
580 idr_remove(&unnamed_dev_idr, slot);
581 spin_unlock(&unnamed_dev_lock);
584 EXPORT_SYMBOL(kill_anon_super);
586 void __init unnamed_dev_init(void)
588 idr_init(&unnamed_dev_idr);
591 void kill_litter_super(struct super_block *sb)
593 if (sb->s_root)
594 d_genocide(sb->s_root);
595 kill_anon_super(sb);
598 EXPORT_SYMBOL(kill_litter_super);
600 static int set_bdev_super(struct super_block *s, void *data)
602 s->s_bdev = data;
603 s->s_dev = s->s_bdev->bd_dev;
604 return 0;
607 static int test_bdev_super(struct super_block *s, void *data)
609 return (void *)s->s_bdev == data;
612 struct super_block *get_sb_bdev(struct file_system_type *fs_type,
613 int flags, const char *dev_name, void *data,
614 int (*fill_super)(struct super_block *, void *, int))
616 struct block_device *bdev;
617 struct super_block *s;
618 int error = 0;
620 bdev = open_bdev_excl(dev_name, flags, fs_type);
621 if (IS_ERR(bdev))
622 return (struct super_block *)bdev;
624 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
625 if (IS_ERR(s))
626 goto out;
628 if (s->s_root) {
629 if ((flags ^ s->s_flags) & MS_RDONLY) {
630 up_write(&s->s_umount);
631 deactivate_super(s);
632 s = ERR_PTR(-EBUSY);
634 goto out;
635 } else {
636 char b[BDEVNAME_SIZE];
638 s->s_flags = flags;
639 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
640 s->s_old_blocksize = block_size(bdev);
641 sb_set_blocksize(s, s->s_old_blocksize);
642 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
643 if (error) {
644 up_write(&s->s_umount);
645 deactivate_super(s);
646 s = ERR_PTR(error);
647 } else
648 s->s_flags |= MS_ACTIVE;
651 return s;
653 out:
654 close_bdev_excl(bdev);
655 return s;
658 EXPORT_SYMBOL(get_sb_bdev);
660 void kill_block_super(struct super_block *sb)
662 struct block_device *bdev = sb->s_bdev;
663 generic_shutdown_super(sb);
664 set_blocksize(bdev, sb->s_old_blocksize);
665 close_bdev_excl(bdev);
668 EXPORT_SYMBOL(kill_block_super);
670 struct super_block *get_sb_nodev(struct file_system_type *fs_type,
671 int flags, void *data,
672 int (*fill_super)(struct super_block *, void *, int))
674 int error;
675 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
677 if (IS_ERR(s))
678 return s;
680 s->s_flags = flags;
682 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
683 if (error) {
684 up_write(&s->s_umount);
685 deactivate_super(s);
686 return ERR_PTR(error);
688 s->s_flags |= MS_ACTIVE;
689 return s;
692 EXPORT_SYMBOL(get_sb_nodev);
694 static int compare_single(struct super_block *s, void *p)
696 return 1;
699 struct super_block *get_sb_single(struct file_system_type *fs_type,
700 int flags, void *data,
701 int (*fill_super)(struct super_block *, void *, int))
703 struct super_block *s;
704 int error;
706 s = sget(fs_type, compare_single, set_anon_super, NULL);
707 if (IS_ERR(s))
708 return s;
709 if (!s->s_root) {
710 s->s_flags = flags;
711 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
712 if (error) {
713 up_write(&s->s_umount);
714 deactivate_super(s);
715 return ERR_PTR(error);
717 s->s_flags |= MS_ACTIVE;
719 do_remount_sb(s, flags, data, 0);
720 return s;
723 EXPORT_SYMBOL(get_sb_single);
725 struct vfsmount *
726 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
728 struct file_system_type *type = get_fs_type(fstype);
729 struct super_block *sb = ERR_PTR(-ENOMEM);
730 struct vfsmount *mnt;
731 int error;
732 char *secdata = NULL;
734 if (!type)
735 return ERR_PTR(-ENODEV);
737 mnt = alloc_vfsmnt(name);
738 if (!mnt)
739 goto out;
741 if (data) {
742 secdata = alloc_secdata();
743 if (!secdata) {
744 sb = ERR_PTR(-ENOMEM);
745 goto out_mnt;
748 error = security_sb_copy_data(type, data, secdata);
749 if (error) {
750 sb = ERR_PTR(error);
751 goto out_free_secdata;
755 sb = type->get_sb(type, flags, name, data);
756 if (IS_ERR(sb))
757 goto out_free_secdata;
758 error = security_sb_kern_mount(sb, secdata);
759 if (error)
760 goto out_sb;
761 mnt->mnt_sb = sb;
762 mnt->mnt_root = dget(sb->s_root);
763 mnt->mnt_mountpoint = sb->s_root;
764 mnt->mnt_parent = mnt;
765 up_write(&sb->s_umount);
766 put_filesystem(type);
767 return mnt;
768 out_sb:
769 up_write(&sb->s_umount);
770 deactivate_super(sb);
771 sb = ERR_PTR(error);
772 out_free_secdata:
773 free_secdata(secdata);
774 out_mnt:
775 free_vfsmnt(mnt);
776 out:
777 put_filesystem(type);
778 return (struct vfsmount *)sb;
781 struct vfsmount *kern_mount(struct file_system_type *type)
783 return do_kern_mount(type->name, 0, type->name, NULL);
786 EXPORT_SYMBOL(kern_mount);