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/slab.h>
25 #include <linux/smp_lock.h>
26 #include <linux/acct.h>
27 #include <linux/blkdev.h>
28 #include <linux/quotaops.h>
29 #include <linux/namei.h>
30 #include <linux/buffer_head.h> /* for fsync_super() */
31 #include <linux/mount.h>
32 #include <linux/security.h>
33 #include <linux/vfs.h>
34 #include <linux/writeback.h> /* for the emergency remount stuff */
35 #include <asm/uaccess.h>
38 void get_filesystem(struct file_system_type
*fs
);
39 void put_filesystem(struct file_system_type
*fs
);
40 struct file_system_type
*get_fs_type(const char *name
);
42 LIST_HEAD(super_blocks
);
43 spinlock_t sb_lock
= SPIN_LOCK_UNLOCKED
;
46 * alloc_super - create new superblock
48 * Allocates and initializes a new &struct super_block. alloc_super()
49 * returns a pointer new superblock or %NULL if allocation had failed.
51 static struct super_block
*alloc_super(void)
53 struct super_block
*s
= kmalloc(sizeof(struct super_block
), GFP_USER
);
54 static struct super_operations default_op
;
57 memset(s
, 0, sizeof(struct super_block
));
58 if (security_sb_alloc(s
)) {
63 INIT_LIST_HEAD(&s
->s_dirty
);
64 INIT_LIST_HEAD(&s
->s_io
);
65 INIT_LIST_HEAD(&s
->s_files
);
66 INIT_LIST_HEAD(&s
->s_instances
);
67 INIT_HLIST_HEAD(&s
->s_anon
);
68 init_rwsem(&s
->s_umount
);
69 sema_init(&s
->s_lock
, 1);
70 down_write(&s
->s_umount
);
72 atomic_set(&s
->s_active
, 1);
73 sema_init(&s
->s_vfs_rename_sem
,1);
74 sema_init(&s
->s_dquot
.dqio_sem
, 1);
75 sema_init(&s
->s_dquot
.dqonoff_sem
, 1);
76 init_rwsem(&s
->s_dquot
.dqptr_sem
);
77 s
->s_maxbytes
= MAX_NON_LFS
;
78 s
->dq_op
= sb_dquot_ops
;
79 s
->s_qcop
= sb_quotactl_ops
;
80 s
->s_op
= &default_op
;
87 * destroy_super - frees a superblock
88 * @s: superblock to free
92 static inline void destroy_super(struct super_block
*s
)
98 /* Superblock refcounting */
101 * put_super - drop a temporary reference to superblock
102 * @s: superblock in question
104 * Drops a temporary reference, frees superblock if there's no
107 static inline void put_super(struct super_block
*s
)
112 spin_unlock(&sb_lock
);
116 * deactivate_super - drop an active reference to superblock
117 * @s: superblock to deactivate
119 * Drops an active reference to superblock, acquiring a temprory one if
120 * there is no active references left. In that case we lock superblock,
121 * tell fs driver to shut it down and drop the temporary reference we
124 void deactivate_super(struct super_block
*s
)
126 struct file_system_type
*fs
= s
->s_type
;
127 if (atomic_dec_and_lock(&s
->s_active
, &sb_lock
)) {
128 s
->s_count
-= S_BIAS
-1;
129 spin_unlock(&sb_lock
);
130 down_write(&s
->s_umount
);
138 * grab_super - acquire an active reference
139 * @s - reference we are trying to make active
141 * Tries to acquire an active reference. grab_super() is used when we
142 * had just found a superblock in super_blocks or fs_type->fs_supers
143 * and want to turn it into a full-blown active reference. grab_super()
144 * is called with sb_lock held and drops it. Returns 1 in case of
145 * success, 0 if we had failed (superblock contents was already dead or
146 * dying when grab_super() had been called).
148 static int grab_super(struct super_block
*s
)
151 spin_unlock(&sb_lock
);
152 down_write(&s
->s_umount
);
155 if (s
->s_count
> S_BIAS
) {
156 atomic_inc(&s
->s_active
);
158 spin_unlock(&sb_lock
);
161 spin_unlock(&sb_lock
);
163 up_write(&s
->s_umount
);
170 * generic_shutdown_super - common helper for ->kill_sb()
171 * @sb: superblock to kill
173 * generic_shutdown_super() does all fs-independent work on superblock
174 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
175 * that need destruction out of superblock, call generic_shutdown_super()
176 * and release aforementioned objects. Note: dentries and inodes _are_
177 * taken care of and do not need specific handling.
179 void generic_shutdown_super(struct super_block
*sb
)
181 struct dentry
*root
= sb
->s_root
;
182 struct super_operations
*sop
= sb
->s_op
;
186 shrink_dcache_parent(root
);
187 shrink_dcache_anon(&sb
->s_anon
);
192 sb
->s_flags
&= ~MS_ACTIVE
;
193 /* bad name - it should be evict_inodes() */
194 invalidate_inodes(sb
);
196 if (sop
->write_super
&& sb
->s_dirt
)
197 sop
->write_super(sb
);
201 /* Forget any remaining inodes */
202 if (invalidate_inodes(sb
)) {
203 printk("VFS: Busy inodes after unmount. "
204 "Self-destruct in 5 seconds. Have a nice day...\n");
211 list_del(&sb
->s_list
);
212 list_del(&sb
->s_instances
);
213 spin_unlock(&sb_lock
);
214 up_write(&sb
->s_umount
);
218 * sget - find or create a superblock
219 * @type: filesystem type superblock should belong to
220 * @test: comparison callback
221 * @set: setup callback
222 * @data: argument to each of them
224 struct super_block
*sget(struct file_system_type
*type
,
225 int (*test
)(struct super_block
*,void *),
226 int (*set
)(struct super_block
*,void *),
229 struct super_block
*s
= alloc_super();
234 return ERR_PTR(-ENOMEM
);
238 if (test
) list_for_each(p
, &type
->fs_supers
) {
239 struct super_block
*old
;
240 old
= list_entry(p
, struct super_block
, s_instances
);
241 if (!test(old
, data
))
243 if (!grab_super(old
))
250 spin_unlock(&sb_lock
);
255 list_add(&s
->s_list
, super_blocks
.prev
);
256 list_add(&s
->s_instances
, &type
->fs_supers
);
257 spin_unlock(&sb_lock
);
258 get_filesystem(type
);
262 void drop_super(struct super_block
*sb
)
264 up_read(&sb
->s_umount
);
268 static inline void write_super(struct super_block
*sb
)
271 if (sb
->s_root
&& sb
->s_dirt
)
272 if (sb
->s_op
->write_super
)
273 sb
->s_op
->write_super(sb
);
278 * Note: check the dirty flag before waiting, so we don't
279 * hold up the sync while mounting a device. (The newly
280 * mounted device won't need syncing.)
282 void sync_supers(void)
284 struct super_block
* sb
;
287 sb
= sb_entry(super_blocks
.next
);
288 while (sb
!= sb_entry(&super_blocks
))
291 spin_unlock(&sb_lock
);
292 down_read(&sb
->s_umount
);
297 sb
= sb_entry(sb
->s_list
.next
);
298 spin_unlock(&sb_lock
);
302 * Call the ->sync_fs super_op against all filesytems which are r/w and
303 * which implement it.
305 * This operation is careful to avoid the livelock which could easily happen
306 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
307 * is used only here. We set it against all filesystems and then clear it as
308 * we sync them. So redirtied filesystems are skipped.
310 * But if process A is currently running sync_filesytems and then process B
311 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
312 * flags again, which will cause process A to resync everything. Fix that with
315 * FIXME: If wait==0, we only really need to call ->sync_fs if s_dirt is true.
317 void sync_filesystems(int wait
)
319 struct super_block
*sb
;
320 static DECLARE_MUTEX(mutex
);
322 down(&mutex
); /* Could be down_interruptible */
324 for (sb
= sb_entry(super_blocks
.next
); sb
!= sb_entry(&super_blocks
);
325 sb
= sb_entry(sb
->s_list
.next
)) {
326 if (!sb
->s_op
->sync_fs
)
328 if (sb
->s_flags
& MS_RDONLY
)
330 sb
->s_need_sync_fs
= 1;
332 spin_unlock(&sb_lock
);
336 for (sb
= sb_entry(super_blocks
.next
); sb
!= sb_entry(&super_blocks
);
337 sb
= sb_entry(sb
->s_list
.next
)) {
338 if (!sb
->s_need_sync_fs
)
340 sb
->s_need_sync_fs
= 0;
341 if (sb
->s_flags
& MS_RDONLY
)
342 continue; /* hm. Was remounted r/o meanwhile */
344 spin_unlock(&sb_lock
);
345 down_read(&sb
->s_umount
);
347 sb
->s_op
->sync_fs(sb
, wait
);
351 spin_unlock(&sb_lock
);
356 * get_super - get the superblock of a device
357 * @dev: device to get the superblock for
359 * Scans the superblock list and finds the superblock of the file system
360 * mounted on the device given. %NULL is returned if no match is found.
363 struct super_block
* get_super(struct block_device
*bdev
)
370 list_for_each(p
, &super_blocks
) {
371 struct super_block
*s
= sb_entry(p
);
372 if (s
->s_bdev
== bdev
) {
374 spin_unlock(&sb_lock
);
375 down_read(&s
->s_umount
);
382 spin_unlock(&sb_lock
);
386 struct super_block
* user_get_super(dev_t dev
)
392 list_for_each(p
, &super_blocks
) {
393 struct super_block
*s
= sb_entry(p
);
394 if (s
->s_dev
== dev
) {
396 spin_unlock(&sb_lock
);
397 down_read(&s
->s_umount
);
404 spin_unlock(&sb_lock
);
408 asmlinkage
long sys_ustat(dev_t dev
, struct ustat __user
* ubuf
)
410 struct super_block
*s
;
415 s
= user_get_super(dev
);
418 err
= vfs_statfs(s
, &sbuf
);
423 memset(&tmp
,0,sizeof(struct ustat
));
424 tmp
.f_tfree
= sbuf
.f_bfree
;
425 tmp
.f_tinode
= sbuf
.f_ffree
;
427 err
= copy_to_user(ubuf
,&tmp
,sizeof(struct ustat
)) ? -EFAULT
: 0;
432 static void mark_files_ro(struct super_block
*sb
)
437 list_for_each_entry(f
, &sb
->s_files
, f_list
) {
438 if (S_ISREG(f
->f_dentry
->d_inode
->i_mode
) && file_count(f
))
439 f
->f_mode
&= ~FMODE_WRITE
;
445 * do_remount_sb - asks filesystem to change mount options.
446 * @sb: superblock in question
447 * @flags: numeric part of options
448 * @data: the rest of options
450 * Alters the mount options of a mounted file system.
452 int do_remount_sb(struct super_block
*sb
, int flags
, void *data
, int force
)
456 if (!(flags
& MS_RDONLY
) && bdev_read_only(sb
->s_bdev
))
458 if (flags
& MS_RDONLY
)
460 shrink_dcache_sb(sb
);
463 /* If we are remounting RDONLY, make sure there are no rw files open */
464 if ((flags
& MS_RDONLY
) && !(sb
->s_flags
& MS_RDONLY
)) {
467 else if (!fs_may_remount_ro(sb
))
471 if (sb
->s_op
->remount_fs
) {
473 retval
= sb
->s_op
->remount_fs(sb
, &flags
, data
);
478 sb
->s_flags
= (sb
->s_flags
& ~MS_RMT_MASK
) | (flags
& MS_RMT_MASK
);
482 static void do_emergency_remount(unsigned long foo
)
484 struct super_block
*sb
;
487 list_for_each_entry(sb
, &super_blocks
, s_list
) {
489 spin_unlock(&sb_lock
);
490 down_read(&sb
->s_umount
);
491 if (sb
->s_root
&& sb
->s_bdev
&& !(sb
->s_flags
& MS_RDONLY
))
492 do_remount_sb(sb
, MS_RDONLY
, NULL
, 1);
496 spin_unlock(&sb_lock
);
497 printk("Emergency Remount complete\n");
500 void emergency_remount(void)
502 pdflush_operation(do_emergency_remount
, 0);
506 * Unnamed block devices are dummy devices used by virtual
507 * filesystems which don't use real block-devices. -- jrs
510 enum {Max_anon
= 256};
511 static unsigned long unnamed_dev_in_use
[Max_anon
/(8*sizeof(unsigned long))];
512 static spinlock_t unnamed_dev_lock
= SPIN_LOCK_UNLOCKED
;/* protects the above */
514 int set_anon_super(struct super_block
*s
, void *data
)
517 spin_lock(&unnamed_dev_lock
);
518 dev
= find_first_zero_bit(unnamed_dev_in_use
, Max_anon
);
519 if (dev
== Max_anon
) {
520 spin_unlock(&unnamed_dev_lock
);
523 set_bit(dev
, unnamed_dev_in_use
);
524 spin_unlock(&unnamed_dev_lock
);
525 s
->s_dev
= MKDEV(0, dev
);
529 void kill_anon_super(struct super_block
*sb
)
531 int slot
= MINOR(sb
->s_dev
);
532 generic_shutdown_super(sb
);
533 spin_lock(&unnamed_dev_lock
);
534 clear_bit(slot
, unnamed_dev_in_use
);
535 spin_unlock(&unnamed_dev_lock
);
538 void kill_litter_super(struct super_block
*sb
)
541 d_genocide(sb
->s_root
);
545 static int set_bdev_super(struct super_block
*s
, void *data
)
548 s
->s_dev
= s
->s_bdev
->bd_dev
;
552 static int test_bdev_super(struct super_block
*s
, void *data
)
554 return (void *)s
->s_bdev
== data
;
557 struct super_block
*get_sb_bdev(struct file_system_type
*fs_type
,
558 int flags
, const char *dev_name
, void *data
,
559 int (*fill_super
)(struct super_block
*, void *, int))
561 struct block_device
*bdev
;
562 struct super_block
*s
;
565 bdev
= open_bdev_excl(dev_name
, flags
, BDEV_FS
, fs_type
);
567 return (struct super_block
*)bdev
;
569 s
= sget(fs_type
, test_bdev_super
, set_bdev_super
, bdev
);
574 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
575 up_write(&s
->s_umount
);
581 char b
[BDEVNAME_SIZE
];
584 strncpy(s
->s_id
, bdevname(bdev
, b
), sizeof(s
->s_id
));
585 s
->s_old_blocksize
= block_size(bdev
);
586 sb_set_blocksize(s
, s
->s_old_blocksize
);
587 error
= fill_super(s
, data
, flags
& MS_VERBOSE
? 1 : 0);
589 up_write(&s
->s_umount
);
593 s
->s_flags
|= MS_ACTIVE
;
599 close_bdev_excl(bdev
, BDEV_FS
);
603 void kill_block_super(struct super_block
*sb
)
605 struct block_device
*bdev
= sb
->s_bdev
;
606 generic_shutdown_super(sb
);
607 set_blocksize(bdev
, sb
->s_old_blocksize
);
608 close_bdev_excl(bdev
, BDEV_FS
);
611 struct super_block
*get_sb_nodev(struct file_system_type
*fs_type
,
612 int flags
, void *data
,
613 int (*fill_super
)(struct super_block
*, void *, int))
616 struct super_block
*s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
623 error
= fill_super(s
, data
, flags
& MS_VERBOSE
? 1 : 0);
625 up_write(&s
->s_umount
);
627 return ERR_PTR(error
);
629 s
->s_flags
|= MS_ACTIVE
;
633 static int compare_single(struct super_block
*s
, void *p
)
638 struct super_block
*get_sb_single(struct file_system_type
*fs_type
,
639 int flags
, void *data
,
640 int (*fill_super
)(struct super_block
*, void *, int))
642 struct super_block
*s
;
645 s
= sget(fs_type
, compare_single
, set_anon_super
, NULL
);
650 error
= fill_super(s
, data
, flags
& MS_VERBOSE
? 1 : 0);
652 up_write(&s
->s_umount
);
654 return ERR_PTR(error
);
656 s
->s_flags
|= MS_ACTIVE
;
658 do_remount_sb(s
, flags
, data
, 0);
663 do_kern_mount(const char *fstype
, int flags
, const char *name
, void *data
)
665 struct file_system_type
*type
= get_fs_type(fstype
);
666 struct super_block
*sb
= ERR_PTR(-ENOMEM
);
667 struct vfsmount
*mnt
;
671 return ERR_PTR(-ENODEV
);
673 mnt
= alloc_vfsmnt(name
);
676 sb
= type
->get_sb(type
, flags
, name
, data
);
679 error
= security_sb_kern_mount(sb
);
683 mnt
->mnt_root
= dget(sb
->s_root
);
684 mnt
->mnt_mountpoint
= sb
->s_root
;
685 mnt
->mnt_parent
= mnt
;
686 up_write(&sb
->s_umount
);
687 put_filesystem(type
);
690 up_write(&sb
->s_umount
);
691 deactivate_super(sb
);
696 put_filesystem(type
);
697 return (struct vfsmount
*)sb
;
700 struct vfsmount
*kern_mount(struct file_system_type
*type
)
702 return do_kern_mount(type
->name
, 0, type
->name
, NULL
);