[PATCH] DVB: Update documentation and credits
[linux-2.6/history.git] / fs / super.c
blob2117f5c124235b03de95f7bd9543926f7c91e63b
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/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/vfs.h>
35 #include <linux/writeback.h> /* for the emergency remount stuff */
36 #include <asm/uaccess.h>
39 void get_filesystem(struct file_system_type *fs);
40 void put_filesystem(struct file_system_type *fs);
41 struct file_system_type *get_fs_type(const char *name);
43 LIST_HEAD(super_blocks);
44 spinlock_t sb_lock = SPIN_LOCK_UNLOCKED;
46 /**
47 * alloc_super - create new superblock
49 * Allocates and initializes a new &struct super_block. alloc_super()
50 * returns a pointer new superblock or %NULL if allocation had failed.
52 static struct super_block *alloc_super(void)
54 struct super_block *s = kmalloc(sizeof(struct super_block), GFP_USER);
55 static struct super_operations default_op;
57 if (s) {
58 memset(s, 0, sizeof(struct super_block));
59 if (security_sb_alloc(s)) {
60 kfree(s);
61 s = NULL;
62 goto out;
64 INIT_LIST_HEAD(&s->s_dirty);
65 INIT_LIST_HEAD(&s->s_io);
66 INIT_LIST_HEAD(&s->s_files);
67 INIT_LIST_HEAD(&s->s_instances);
68 INIT_HLIST_HEAD(&s->s_anon);
69 init_rwsem(&s->s_umount);
70 sema_init(&s->s_lock, 1);
71 down_write(&s->s_umount);
72 s->s_count = S_BIAS;
73 atomic_set(&s->s_active, 1);
74 sema_init(&s->s_vfs_rename_sem,1);
75 sema_init(&s->s_dquot.dqio_sem, 1);
76 sema_init(&s->s_dquot.dqonoff_sem, 1);
77 init_rwsem(&s->s_dquot.dqptr_sem);
78 s->s_maxbytes = MAX_NON_LFS;
79 s->dq_op = sb_dquot_ops;
80 s->s_qcop = sb_quotactl_ops;
81 s->s_op = &default_op;
83 out:
84 return s;
87 /**
88 * destroy_super - frees a superblock
89 * @s: superblock to free
91 * Frees a superblock.
93 static inline void destroy_super(struct super_block *s)
95 security_sb_free(s);
96 kfree(s);
99 /* Superblock refcounting */
102 * put_super - drop a temporary reference to superblock
103 * @s: superblock in question
105 * Drops a temporary reference, frees superblock if there's no
106 * references left.
108 static inline void put_super(struct super_block *s)
110 spin_lock(&sb_lock);
111 if (!--s->s_count)
112 destroy_super(s);
113 spin_unlock(&sb_lock);
117 * deactivate_super - drop an active reference to superblock
118 * @s: superblock to deactivate
120 * Drops an active reference to superblock, acquiring a temprory one if
121 * there is no active references left. In that case we lock superblock,
122 * tell fs driver to shut it down and drop the temporary reference we
123 * had just acquired.
125 void deactivate_super(struct super_block *s)
127 struct file_system_type *fs = s->s_type;
128 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
129 s->s_count -= S_BIAS-1;
130 spin_unlock(&sb_lock);
131 down_write(&s->s_umount);
132 fs->kill_sb(s);
133 put_filesystem(fs);
134 put_super(s);
138 EXPORT_SYMBOL(deactivate_super);
141 * grab_super - acquire an active reference
142 * @s: reference we are trying to make active
144 * Tries to acquire an active reference. grab_super() is used when we
145 * had just found a superblock in super_blocks or fs_type->fs_supers
146 * and want to turn it into a full-blown active reference. grab_super()
147 * is called with sb_lock held and drops it. Returns 1 in case of
148 * success, 0 if we had failed (superblock contents was already dead or
149 * dying when grab_super() had been called).
151 static int grab_super(struct super_block *s)
153 s->s_count++;
154 spin_unlock(&sb_lock);
155 down_write(&s->s_umount);
156 if (s->s_root) {
157 spin_lock(&sb_lock);
158 if (s->s_count > S_BIAS) {
159 atomic_inc(&s->s_active);
160 s->s_count--;
161 spin_unlock(&sb_lock);
162 return 1;
164 spin_unlock(&sb_lock);
166 up_write(&s->s_umount);
167 put_super(s);
168 yield();
169 return 0;
173 * generic_shutdown_super - common helper for ->kill_sb()
174 * @sb: superblock to kill
176 * generic_shutdown_super() does all fs-independent work on superblock
177 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
178 * that need destruction out of superblock, call generic_shutdown_super()
179 * and release aforementioned objects. Note: dentries and inodes _are_
180 * taken care of and do not need specific handling.
182 void generic_shutdown_super(struct super_block *sb)
184 struct dentry *root = sb->s_root;
185 struct super_operations *sop = sb->s_op;
187 if (root) {
188 sb->s_root = NULL;
189 shrink_dcache_parent(root);
190 shrink_dcache_anon(&sb->s_anon);
191 dput(root);
192 fsync_super(sb);
193 lock_super(sb);
194 lock_kernel();
195 sb->s_flags &= ~MS_ACTIVE;
196 /* bad name - it should be evict_inodes() */
197 invalidate_inodes(sb);
199 if (sop->write_super && sb->s_dirt)
200 sop->write_super(sb);
201 if (sop->put_super)
202 sop->put_super(sb);
204 /* Forget any remaining inodes */
205 if (invalidate_inodes(sb)) {
206 printk("VFS: Busy inodes after unmount. "
207 "Self-destruct in 5 seconds. Have a nice day...\n");
210 unlock_kernel();
211 unlock_super(sb);
213 spin_lock(&sb_lock);
214 list_del(&sb->s_list);
215 list_del(&sb->s_instances);
216 spin_unlock(&sb_lock);
217 up_write(&sb->s_umount);
220 EXPORT_SYMBOL(generic_shutdown_super);
223 * sget - find or create a superblock
224 * @type: filesystem type superblock should belong to
225 * @test: comparison callback
226 * @set: setup callback
227 * @data: argument to each of them
229 struct super_block *sget(struct file_system_type *type,
230 int (*test)(struct super_block *,void *),
231 int (*set)(struct super_block *,void *),
232 void *data)
234 struct super_block *s = NULL;
235 struct list_head *p;
236 int err;
238 retry:
239 spin_lock(&sb_lock);
240 if (test) list_for_each(p, &type->fs_supers) {
241 struct super_block *old;
242 old = list_entry(p, struct super_block, s_instances);
243 if (!test(old, data))
244 continue;
245 if (!grab_super(old))
246 goto retry;
247 if (s)
248 destroy_super(s);
249 return old;
251 if (!s) {
252 spin_unlock(&sb_lock);
253 s = alloc_super();
254 if (!s)
255 return ERR_PTR(-ENOMEM);
256 goto retry;
259 err = set(s, data);
260 if (err) {
261 spin_unlock(&sb_lock);
262 destroy_super(s);
263 return ERR_PTR(err);
265 s->s_type = type;
266 list_add(&s->s_list, super_blocks.prev);
267 list_add(&s->s_instances, &type->fs_supers);
268 spin_unlock(&sb_lock);
269 get_filesystem(type);
270 return s;
273 EXPORT_SYMBOL(sget);
275 void drop_super(struct super_block *sb)
277 up_read(&sb->s_umount);
278 put_super(sb);
281 EXPORT_SYMBOL(drop_super);
283 static inline void write_super(struct super_block *sb)
285 lock_super(sb);
286 if (sb->s_root && sb->s_dirt)
287 if (sb->s_op->write_super)
288 sb->s_op->write_super(sb);
289 unlock_super(sb);
293 * Note: check the dirty flag before waiting, so we don't
294 * hold up the sync while mounting a device. (The newly
295 * mounted device won't need syncing.)
297 void sync_supers(void)
299 struct super_block * sb;
300 restart:
301 spin_lock(&sb_lock);
302 sb = sb_entry(super_blocks.next);
303 while (sb != sb_entry(&super_blocks))
304 if (sb->s_dirt) {
305 sb->s_count++;
306 spin_unlock(&sb_lock);
307 down_read(&sb->s_umount);
308 write_super(sb);
309 drop_super(sb);
310 goto restart;
311 } else
312 sb = sb_entry(sb->s_list.next);
313 spin_unlock(&sb_lock);
317 * Call the ->sync_fs super_op against all filesytems which are r/w and
318 * which implement it.
320 * This operation is careful to avoid the livelock which could easily happen
321 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
322 * is used only here. We set it against all filesystems and then clear it as
323 * we sync them. So redirtied filesystems are skipped.
325 * But if process A is currently running sync_filesytems and then process B
326 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
327 * flags again, which will cause process A to resync everything. Fix that with
328 * a local mutex.
330 * FIXME: If wait==0, we only really need to call ->sync_fs if s_dirt is true.
332 void sync_filesystems(int wait)
334 struct super_block *sb;
335 static DECLARE_MUTEX(mutex);
337 down(&mutex); /* Could be down_interruptible */
338 spin_lock(&sb_lock);
339 for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
340 sb = sb_entry(sb->s_list.next)) {
341 if (!sb->s_op->sync_fs)
342 continue;
343 if (sb->s_flags & MS_RDONLY)
344 continue;
345 sb->s_need_sync_fs = 1;
347 spin_unlock(&sb_lock);
349 restart:
350 spin_lock(&sb_lock);
351 for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
352 sb = sb_entry(sb->s_list.next)) {
353 if (!sb->s_need_sync_fs)
354 continue;
355 sb->s_need_sync_fs = 0;
356 if (sb->s_flags & MS_RDONLY)
357 continue; /* hm. Was remounted r/o meanwhile */
358 sb->s_count++;
359 spin_unlock(&sb_lock);
360 down_read(&sb->s_umount);
361 if (sb->s_root)
362 sb->s_op->sync_fs(sb, wait);
363 drop_super(sb);
364 goto restart;
366 spin_unlock(&sb_lock);
367 up(&mutex);
371 * get_super - get the superblock of a device
372 * @bdev: device to get the superblock for
374 * Scans the superblock list and finds the superblock of the file system
375 * mounted on the device given. %NULL is returned if no match is found.
378 struct super_block * get_super(struct block_device *bdev)
380 struct list_head *p;
381 if (!bdev)
382 return NULL;
383 rescan:
384 spin_lock(&sb_lock);
385 list_for_each(p, &super_blocks) {
386 struct super_block *s = sb_entry(p);
387 if (s->s_bdev == bdev) {
388 s->s_count++;
389 spin_unlock(&sb_lock);
390 down_read(&s->s_umount);
391 if (s->s_root)
392 return s;
393 drop_super(s);
394 goto rescan;
397 spin_unlock(&sb_lock);
398 return NULL;
401 EXPORT_SYMBOL(get_super);
403 struct super_block * user_get_super(dev_t dev)
405 struct list_head *p;
407 rescan:
408 spin_lock(&sb_lock);
409 list_for_each(p, &super_blocks) {
410 struct super_block *s = sb_entry(p);
411 if (s->s_dev == dev) {
412 s->s_count++;
413 spin_unlock(&sb_lock);
414 down_read(&s->s_umount);
415 if (s->s_root)
416 return s;
417 drop_super(s);
418 goto rescan;
421 spin_unlock(&sb_lock);
422 return NULL;
425 EXPORT_SYMBOL(user_get_super);
427 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
429 struct super_block *s;
430 struct ustat tmp;
431 struct kstatfs sbuf;
432 int err = -EINVAL;
434 s = user_get_super(new_decode_dev(dev));
435 if (s == NULL)
436 goto out;
437 err = vfs_statfs(s, &sbuf);
438 drop_super(s);
439 if (err)
440 goto out;
442 memset(&tmp,0,sizeof(struct ustat));
443 tmp.f_tfree = sbuf.f_bfree;
444 tmp.f_tinode = sbuf.f_ffree;
446 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
447 out:
448 return err;
451 static void mark_files_ro(struct super_block *sb)
453 struct file *f;
455 file_list_lock();
456 list_for_each_entry(f, &sb->s_files, f_list) {
457 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
458 f->f_mode &= ~FMODE_WRITE;
460 file_list_unlock();
464 * do_remount_sb - asks filesystem to change mount options.
465 * @sb: superblock in question
466 * @flags: numeric part of options
467 * @data: the rest of options
468 * @force: whether or not to force the change
470 * Alters the mount options of a mounted file system.
472 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
474 int retval;
476 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
477 return -EACCES;
478 if (flags & MS_RDONLY)
479 acct_auto_close(sb);
480 shrink_dcache_sb(sb);
481 fsync_super(sb);
483 /* If we are remounting RDONLY, make sure there are no rw files open */
484 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
485 if (force)
486 mark_files_ro(sb);
487 else if (!fs_may_remount_ro(sb))
488 return -EBUSY;
491 if (sb->s_op->remount_fs) {
492 lock_super(sb);
493 retval = sb->s_op->remount_fs(sb, &flags, data);
494 unlock_super(sb);
495 if (retval)
496 return retval;
498 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
499 return 0;
502 static void do_emergency_remount(unsigned long foo)
504 struct super_block *sb;
506 spin_lock(&sb_lock);
507 list_for_each_entry(sb, &super_blocks, s_list) {
508 sb->s_count++;
509 spin_unlock(&sb_lock);
510 down_read(&sb->s_umount);
511 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
513 * ->remount_fs needs lock_kernel().
515 * What lock protects sb->s_flags??
517 lock_kernel();
518 do_remount_sb(sb, MS_RDONLY, NULL, 1);
519 unlock_kernel();
521 drop_super(sb);
522 spin_lock(&sb_lock);
524 spin_unlock(&sb_lock);
525 printk("Emergency Remount complete\n");
528 void emergency_remount(void)
530 pdflush_operation(do_emergency_remount, 0);
534 * Unnamed block devices are dummy devices used by virtual
535 * filesystems which don't use real block-devices. -- jrs
538 enum {Max_anon = 256};
539 static unsigned long unnamed_dev_in_use[Max_anon/(8*sizeof(unsigned long))];
540 static spinlock_t unnamed_dev_lock = SPIN_LOCK_UNLOCKED;/* protects the above */
542 int set_anon_super(struct super_block *s, void *data)
544 int dev;
545 spin_lock(&unnamed_dev_lock);
546 dev = find_first_zero_bit(unnamed_dev_in_use, Max_anon);
547 if (dev == Max_anon) {
548 spin_unlock(&unnamed_dev_lock);
549 return -EMFILE;
551 set_bit(dev, unnamed_dev_in_use);
552 spin_unlock(&unnamed_dev_lock);
553 s->s_dev = MKDEV(0, dev);
554 return 0;
557 EXPORT_SYMBOL(set_anon_super);
559 void kill_anon_super(struct super_block *sb)
561 int slot = MINOR(sb->s_dev);
562 generic_shutdown_super(sb);
563 spin_lock(&unnamed_dev_lock);
564 clear_bit(slot, unnamed_dev_in_use);
565 spin_unlock(&unnamed_dev_lock);
568 EXPORT_SYMBOL(kill_anon_super);
570 void kill_litter_super(struct super_block *sb)
572 if (sb->s_root)
573 d_genocide(sb->s_root);
574 kill_anon_super(sb);
577 EXPORT_SYMBOL(kill_litter_super);
579 static int set_bdev_super(struct super_block *s, void *data)
581 s->s_bdev = data;
582 s->s_dev = s->s_bdev->bd_dev;
583 return 0;
586 static int test_bdev_super(struct super_block *s, void *data)
588 return (void *)s->s_bdev == data;
591 struct super_block *get_sb_bdev(struct file_system_type *fs_type,
592 int flags, const char *dev_name, void *data,
593 int (*fill_super)(struct super_block *, void *, int))
595 struct block_device *bdev;
596 struct super_block *s;
597 int error = 0;
599 bdev = open_bdev_excl(dev_name, flags, BDEV_FS, fs_type);
600 if (IS_ERR(bdev))
601 return (struct super_block *)bdev;
603 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
604 if (IS_ERR(s))
605 goto out;
607 if (s->s_root) {
608 if ((flags ^ s->s_flags) & MS_RDONLY) {
609 up_write(&s->s_umount);
610 deactivate_super(s);
611 s = ERR_PTR(-EBUSY);
613 goto out;
614 } else {
615 char b[BDEVNAME_SIZE];
617 s->s_flags = flags;
618 strncpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
619 s->s_old_blocksize = block_size(bdev);
620 sb_set_blocksize(s, s->s_old_blocksize);
621 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
622 if (error) {
623 up_write(&s->s_umount);
624 deactivate_super(s);
625 s = ERR_PTR(error);
626 } else
627 s->s_flags |= MS_ACTIVE;
630 return s;
632 out:
633 close_bdev_excl(bdev, BDEV_FS);
634 return s;
637 EXPORT_SYMBOL(get_sb_bdev);
639 void kill_block_super(struct super_block *sb)
641 struct block_device *bdev = sb->s_bdev;
642 generic_shutdown_super(sb);
643 set_blocksize(bdev, sb->s_old_blocksize);
644 close_bdev_excl(bdev, BDEV_FS);
647 EXPORT_SYMBOL(kill_block_super);
649 struct super_block *get_sb_nodev(struct file_system_type *fs_type,
650 int flags, void *data,
651 int (*fill_super)(struct super_block *, void *, int))
653 int error;
654 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
656 if (IS_ERR(s))
657 return s;
659 s->s_flags = flags;
661 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
662 if (error) {
663 up_write(&s->s_umount);
664 deactivate_super(s);
665 return ERR_PTR(error);
667 s->s_flags |= MS_ACTIVE;
668 return s;
671 EXPORT_SYMBOL(get_sb_nodev);
673 static int compare_single(struct super_block *s, void *p)
675 return 1;
678 struct super_block *get_sb_single(struct file_system_type *fs_type,
679 int flags, void *data,
680 int (*fill_super)(struct super_block *, void *, int))
682 struct super_block *s;
683 int error;
685 s = sget(fs_type, compare_single, set_anon_super, NULL);
686 if (IS_ERR(s))
687 return s;
688 if (!s->s_root) {
689 s->s_flags = flags;
690 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
691 if (error) {
692 up_write(&s->s_umount);
693 deactivate_super(s);
694 return ERR_PTR(error);
696 s->s_flags |= MS_ACTIVE;
698 do_remount_sb(s, flags, data, 0);
699 return s;
702 EXPORT_SYMBOL(get_sb_single);
704 struct vfsmount *
705 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
707 struct file_system_type *type = get_fs_type(fstype);
708 struct super_block *sb = ERR_PTR(-ENOMEM);
709 struct vfsmount *mnt;
710 int error;
712 if (!type)
713 return ERR_PTR(-ENODEV);
715 mnt = alloc_vfsmnt(name);
716 if (!mnt)
717 goto out;
718 sb = type->get_sb(type, flags, name, data);
719 if (IS_ERR(sb))
720 goto out_mnt;
721 error = security_sb_kern_mount(sb);
722 if (error)
723 goto out_sb;
724 mnt->mnt_sb = sb;
725 mnt->mnt_root = dget(sb->s_root);
726 mnt->mnt_mountpoint = sb->s_root;
727 mnt->mnt_parent = mnt;
728 up_write(&sb->s_umount);
729 put_filesystem(type);
730 return mnt;
731 out_sb:
732 up_write(&sb->s_umount);
733 deactivate_super(sb);
734 sb = ERR_PTR(error);
735 out_mnt:
736 free_vfsmnt(mnt);
737 out:
738 put_filesystem(type);
739 return (struct vfsmount *)sb;
742 struct vfsmount *kern_mount(struct file_system_type *type)
744 return do_kern_mount(type->name, 0, type->name, NULL);
747 EXPORT_SYMBOL(kern_mount);