4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * super.c contains code to handle: - mount structures
8 * - filesystem drivers list
10 * - umount 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
;
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
;
58 memset(s
, 0, sizeof(struct super_block
));
59 if (security_sb_alloc(s
)) {
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
);
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
;
88 * destroy_super - frees a superblock
89 * @s: superblock to free
93 static inline void destroy_super(struct super_block
*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
108 static inline void put_super(struct super_block
*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
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
);
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
)
154 spin_unlock(&sb_lock
);
155 down_write(&s
->s_umount
);
158 if (s
->s_count
> S_BIAS
) {
159 atomic_inc(&s
->s_active
);
161 spin_unlock(&sb_lock
);
164 spin_unlock(&sb_lock
);
166 up_write(&s
->s_umount
);
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
;
189 shrink_dcache_parent(root
);
190 shrink_dcache_anon(&sb
->s_anon
);
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
);
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");
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 *),
234 struct super_block
*s
= NULL
;
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
))
245 if (!grab_super(old
))
252 spin_unlock(&sb_lock
);
255 return ERR_PTR(-ENOMEM
);
261 spin_unlock(&sb_lock
);
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
);
275 void drop_super(struct super_block
*sb
)
277 up_read(&sb
->s_umount
);
281 EXPORT_SYMBOL(drop_super
);
283 static inline void write_super(struct super_block
*sb
)
286 if (sb
->s_root
&& sb
->s_dirt
)
287 if (sb
->s_op
->write_super
)
288 sb
->s_op
->write_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
;
302 sb
= sb_entry(super_blocks
.next
);
303 while (sb
!= sb_entry(&super_blocks
))
306 spin_unlock(&sb_lock
);
307 down_read(&sb
->s_umount
);
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
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 */
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
)
343 if (sb
->s_flags
& MS_RDONLY
)
345 sb
->s_need_sync_fs
= 1;
347 spin_unlock(&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
)
355 sb
->s_need_sync_fs
= 0;
356 if (sb
->s_flags
& MS_RDONLY
)
357 continue; /* hm. Was remounted r/o meanwhile */
359 spin_unlock(&sb_lock
);
360 down_read(&sb
->s_umount
);
362 sb
->s_op
->sync_fs(sb
, wait
);
366 spin_unlock(&sb_lock
);
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
)
385 list_for_each(p
, &super_blocks
) {
386 struct super_block
*s
= sb_entry(p
);
387 if (s
->s_bdev
== bdev
) {
389 spin_unlock(&sb_lock
);
390 down_read(&s
->s_umount
);
397 spin_unlock(&sb_lock
);
401 EXPORT_SYMBOL(get_super
);
403 struct super_block
* user_get_super(dev_t dev
)
409 list_for_each(p
, &super_blocks
) {
410 struct super_block
*s
= sb_entry(p
);
411 if (s
->s_dev
== dev
) {
413 spin_unlock(&sb_lock
);
414 down_read(&s
->s_umount
);
421 spin_unlock(&sb_lock
);
425 EXPORT_SYMBOL(user_get_super
);
427 asmlinkage
long sys_ustat(unsigned dev
, struct ustat __user
* ubuf
)
429 struct super_block
*s
;
434 s
= user_get_super(new_decode_dev(dev
));
437 err
= vfs_statfs(s
, &sbuf
);
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;
451 static void mark_files_ro(struct super_block
*sb
)
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
;
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
)
476 if (!(flags
& MS_RDONLY
) && bdev_read_only(sb
->s_bdev
))
478 if (flags
& MS_RDONLY
)
480 shrink_dcache_sb(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
)) {
487 else if (!fs_may_remount_ro(sb
))
491 if (sb
->s_op
->remount_fs
) {
493 retval
= sb
->s_op
->remount_fs(sb
, &flags
, data
);
498 sb
->s_flags
= (sb
->s_flags
& ~MS_RMT_MASK
) | (flags
& MS_RMT_MASK
);
502 static void do_emergency_remount(unsigned long foo
)
504 struct super_block
*sb
;
507 list_for_each_entry(sb
, &super_blocks
, s_list
) {
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??
518 do_remount_sb(sb
, MS_RDONLY
, NULL
, 1);
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
)
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
);
551 set_bit(dev
, unnamed_dev_in_use
);
552 spin_unlock(&unnamed_dev_lock
);
553 s
->s_dev
= MKDEV(0, dev
);
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
)
573 d_genocide(sb
->s_root
);
577 EXPORT_SYMBOL(kill_litter_super
);
579 static int set_bdev_super(struct super_block
*s
, void *data
)
582 s
->s_dev
= s
->s_bdev
->bd_dev
;
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
;
599 bdev
= open_bdev_excl(dev_name
, flags
, BDEV_FS
, fs_type
);
601 return (struct super_block
*)bdev
;
603 s
= sget(fs_type
, test_bdev_super
, set_bdev_super
, bdev
);
608 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
609 up_write(&s
->s_umount
);
615 char b
[BDEVNAME_SIZE
];
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);
623 up_write(&s
->s_umount
);
627 s
->s_flags
|= MS_ACTIVE
;
633 close_bdev_excl(bdev
, BDEV_FS
);
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))
654 struct super_block
*s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
661 error
= fill_super(s
, data
, flags
& MS_VERBOSE
? 1 : 0);
663 up_write(&s
->s_umount
);
665 return ERR_PTR(error
);
667 s
->s_flags
|= MS_ACTIVE
;
671 EXPORT_SYMBOL(get_sb_nodev
);
673 static int compare_single(struct super_block
*s
, void *p
)
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
;
685 s
= sget(fs_type
, compare_single
, set_anon_super
, NULL
);
690 error
= fill_super(s
, data
, flags
& MS_VERBOSE
? 1 : 0);
692 up_write(&s
->s_umount
);
694 return ERR_PTR(error
);
696 s
->s_flags
|= MS_ACTIVE
;
698 do_remount_sb(s
, flags
, data
, 0);
702 EXPORT_SYMBOL(get_sb_single
);
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
;
713 return ERR_PTR(-ENODEV
);
715 mnt
= alloc_vfsmnt(name
);
718 sb
= type
->get_sb(type
, flags
, name
, data
);
721 error
= security_sb_kern_mount(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
);
732 up_write(&sb
->s_umount
);
733 deactivate_super(sb
);
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
);