[PATCH] fix make rpm versioning
[linux-2.6/history.git] / fs / super.c
blob8f1f26de16736674b0633215f29bc458cd7944dd
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/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;
45 /**
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;
56 if (s) {
57 memset(s, 0, sizeof(struct super_block));
58 if (security_sb_alloc(s)) {
59 kfree(s);
60 s = NULL;
61 goto out;
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);
71 s->s_count = S_BIAS;
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;
82 out:
83 return s;
86 /**
87 * destroy_super - frees a superblock
88 * @s: superblock to free
90 * Frees a superblock.
92 static inline void destroy_super(struct super_block *s)
94 security_sb_free(s);
95 kfree(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
105 * references left.
107 static inline void put_super(struct super_block *s)
109 spin_lock(&sb_lock);
110 if (!--s->s_count)
111 destroy_super(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
122 * had just acquired.
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);
131 fs->kill_sb(s);
132 put_filesystem(fs);
133 put_super(s);
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)
150 s->s_count++;
151 spin_unlock(&sb_lock);
152 down_write(&s->s_umount);
153 if (s->s_root) {
154 spin_lock(&sb_lock);
155 if (s->s_count > S_BIAS) {
156 atomic_inc(&s->s_active);
157 s->s_count--;
158 spin_unlock(&sb_lock);
159 return 1;
161 spin_unlock(&sb_lock);
163 up_write(&s->s_umount);
164 put_super(s);
165 yield();
166 return 0;
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;
184 if (root) {
185 sb->s_root = NULL;
186 shrink_dcache_parent(root);
187 shrink_dcache_anon(&sb->s_anon);
188 dput(root);
189 fsync_super(sb);
190 lock_super(sb);
191 lock_kernel();
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);
198 if (sop->put_super)
199 sop->put_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");
207 unlock_kernel();
208 unlock_super(sb);
210 spin_lock(&sb_lock);
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 *),
227 void *data)
229 struct super_block *s = alloc_super();
230 struct list_head *p;
231 int err;
233 if (!s)
234 return ERR_PTR(-ENOMEM);
236 retry:
237 spin_lock(&sb_lock);
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))
242 continue;
243 if (!grab_super(old))
244 goto retry;
245 destroy_super(s);
246 return old;
248 err = set(s, data);
249 if (err) {
250 spin_unlock(&sb_lock);
251 destroy_super(s);
252 return ERR_PTR(err);
254 s->s_type = type;
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);
259 return s;
262 void drop_super(struct super_block *sb)
264 up_read(&sb->s_umount);
265 put_super(sb);
268 static inline void write_super(struct super_block *sb)
270 lock_super(sb);
271 if (sb->s_root && sb->s_dirt)
272 if (sb->s_op->write_super)
273 sb->s_op->write_super(sb);
274 unlock_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;
285 restart:
286 spin_lock(&sb_lock);
287 sb = sb_entry(super_blocks.next);
288 while (sb != sb_entry(&super_blocks))
289 if (sb->s_dirt) {
290 sb->s_count++;
291 spin_unlock(&sb_lock);
292 down_read(&sb->s_umount);
293 write_super(sb);
294 drop_super(sb);
295 goto restart;
296 } else
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
313 * a local mutex.
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 */
323 spin_lock(&sb_lock);
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)
327 continue;
328 if (sb->s_flags & MS_RDONLY)
329 continue;
330 sb->s_need_sync_fs = 1;
332 spin_unlock(&sb_lock);
334 restart:
335 spin_lock(&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)
339 continue;
340 sb->s_need_sync_fs = 0;
341 if (sb->s_flags & MS_RDONLY)
342 continue; /* hm. Was remounted r/o meanwhile */
343 sb->s_count++;
344 spin_unlock(&sb_lock);
345 down_read(&sb->s_umount);
346 if (sb->s_root)
347 sb->s_op->sync_fs(sb, wait);
348 drop_super(sb);
349 goto restart;
351 spin_unlock(&sb_lock);
352 up(&mutex);
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)
365 struct list_head *p;
366 if (!bdev)
367 return NULL;
368 rescan:
369 spin_lock(&sb_lock);
370 list_for_each(p, &super_blocks) {
371 struct super_block *s = sb_entry(p);
372 if (s->s_bdev == bdev) {
373 s->s_count++;
374 spin_unlock(&sb_lock);
375 down_read(&s->s_umount);
376 if (s->s_root)
377 return s;
378 drop_super(s);
379 goto rescan;
382 spin_unlock(&sb_lock);
383 return NULL;
386 struct super_block * user_get_super(dev_t dev)
388 struct list_head *p;
390 rescan:
391 spin_lock(&sb_lock);
392 list_for_each(p, &super_blocks) {
393 struct super_block *s = sb_entry(p);
394 if (s->s_dev == dev) {
395 s->s_count++;
396 spin_unlock(&sb_lock);
397 down_read(&s->s_umount);
398 if (s->s_root)
399 return s;
400 drop_super(s);
401 goto rescan;
404 spin_unlock(&sb_lock);
405 return NULL;
408 asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf)
410 struct super_block *s;
411 struct ustat tmp;
412 struct kstatfs sbuf;
413 int err = -EINVAL;
415 s = user_get_super(dev);
416 if (s == NULL)
417 goto out;
418 err = vfs_statfs(s, &sbuf);
419 drop_super(s);
420 if (err)
421 goto out;
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;
428 out:
429 return err;
432 static void mark_files_ro(struct super_block *sb)
434 struct file *f;
436 file_list_lock();
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;
441 file_list_unlock();
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)
454 int retval;
456 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
457 return -EACCES;
458 if (flags & MS_RDONLY)
459 acct_auto_close(sb);
460 shrink_dcache_sb(sb);
461 fsync_super(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)) {
465 if (force)
466 mark_files_ro(sb);
467 else if (!fs_may_remount_ro(sb))
468 return -EBUSY;
471 if (sb->s_op->remount_fs) {
472 lock_super(sb);
473 retval = sb->s_op->remount_fs(sb, &flags, data);
474 unlock_super(sb);
475 if (retval)
476 return retval;
478 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
479 return 0;
482 static void do_emergency_remount(unsigned long foo)
484 struct super_block *sb;
486 spin_lock(&sb_lock);
487 list_for_each_entry(sb, &super_blocks, s_list) {
488 sb->s_count++;
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);
493 drop_super(sb);
494 spin_lock(&sb_lock);
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)
516 int dev;
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);
521 return -EMFILE;
523 set_bit(dev, unnamed_dev_in_use);
524 spin_unlock(&unnamed_dev_lock);
525 s->s_dev = MKDEV(0, dev);
526 return 0;
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)
540 if (sb->s_root)
541 d_genocide(sb->s_root);
542 kill_anon_super(sb);
545 static int set_bdev_super(struct super_block *s, void *data)
547 s->s_bdev = data;
548 s->s_dev = s->s_bdev->bd_dev;
549 return 0;
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;
563 int error = 0;
565 bdev = open_bdev_excl(dev_name, flags, BDEV_FS, fs_type);
566 if (IS_ERR(bdev))
567 return (struct super_block *)bdev;
569 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
570 if (IS_ERR(s))
571 goto out;
573 if (s->s_root) {
574 if ((flags ^ s->s_flags) & MS_RDONLY) {
575 up_write(&s->s_umount);
576 deactivate_super(s);
577 s = ERR_PTR(-EBUSY);
579 goto out;
580 } else {
581 char b[BDEVNAME_SIZE];
583 s->s_flags = flags;
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);
588 if (error) {
589 up_write(&s->s_umount);
590 deactivate_super(s);
591 s = ERR_PTR(error);
592 } else
593 s->s_flags |= MS_ACTIVE;
596 return s;
598 out:
599 close_bdev_excl(bdev, BDEV_FS);
600 return s;
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))
615 int error;
616 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
618 if (IS_ERR(s))
619 return s;
621 s->s_flags = flags;
623 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
624 if (error) {
625 up_write(&s->s_umount);
626 deactivate_super(s);
627 return ERR_PTR(error);
629 s->s_flags |= MS_ACTIVE;
630 return s;
633 static int compare_single(struct super_block *s, void *p)
635 return 1;
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;
643 int error;
645 s = sget(fs_type, compare_single, set_anon_super, NULL);
646 if (IS_ERR(s))
647 return s;
648 if (!s->s_root) {
649 s->s_flags = flags;
650 error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
651 if (error) {
652 up_write(&s->s_umount);
653 deactivate_super(s);
654 return ERR_PTR(error);
656 s->s_flags |= MS_ACTIVE;
658 do_remount_sb(s, flags, data, 0);
659 return s;
662 struct vfsmount *
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;
668 int error;
670 if (!type)
671 return ERR_PTR(-ENODEV);
673 mnt = alloc_vfsmnt(name);
674 if (!mnt)
675 goto out;
676 sb = type->get_sb(type, flags, name, data);
677 if (IS_ERR(sb))
678 goto out_mnt;
679 error = security_sb_kern_mount(sb);
680 if (error)
681 goto out_sb;
682 mnt->mnt_sb = 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);
688 return mnt;
689 out_sb:
690 up_write(&sb->s_umount);
691 deactivate_super(sb);
692 sb = ERR_PTR(error);
693 out_mnt:
694 free_vfsmnt(mnt);
695 out:
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);