root/fs/super.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. register_filesystem
  2. unregister_filesystem
  3. fs_index
  4. fs_name
  5. fs_maxindex
  6. sys_sysfs
  7. get_filesystem_list
  8. get_fs_type
  9. __wait_on_super
  10. sync_supers
  11. get_super
  12. put_super
  13. read_super
  14. get_unnamed_dev
  15. put_unnamed_dev
  16. do_umount
  17. sys_umount
  18. do_mount
  19. do_remount_sb
  20. do_remount
  21. copy_mount_options
  22. sys_mount
  23. mount_root

   1 /*
   2  *  linux/fs/super.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * super.c contains code to handle the super-block tables.
   9  */
  10 #include <stdarg.h>
  11 
  12 #include <linux/config.h>
  13 #include <linux/sched.h>
  14 #include <linux/kernel.h>
  15 #include <linux/major.h>
  16 #include <linux/stat.h>
  17 #include <linux/errno.h>
  18 #include <linux/string.h>
  19 #include <linux/locks.h>
  20 
  21 #include <asm/system.h>
  22 #include <asm/segment.h>
  23 
  24  
  25 extern struct file_operations * get_blkfops(unsigned int);
  26 extern struct file_operations * get_chrfops(unsigned int);
  27 
  28 extern void wait_for_keypress(void);
  29 extern void fcntl_init_locks(void);
  30 
  31 extern int root_mountflags;
  32 
  33 struct super_block super_blocks[NR_SUPER];
  34 
  35 static int do_remount_sb(struct super_block *sb, int flags, char * data);
  36 
  37 /* this is initialized in init/main.c */
  38 dev_t ROOT_DEV = 0;
  39 
  40 static struct file_system_type * file_systems = NULL;
  41 
  42 int register_filesystem(struct file_system_type * fs)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44         struct file_system_type ** tmp;
  45 
  46         if (!fs)
  47                 return -EINVAL;
  48         if (fs->next)
  49                 return -EBUSY;
  50         tmp = &file_systems;
  51         while (*tmp) {
  52                 if (strcmp((*tmp)->name, fs->name) == 0)
  53                         return -EBUSY;
  54                 tmp = &(*tmp)->next;
  55         }
  56         *tmp = fs;
  57         return 0;
  58 }
  59 
  60 int unregister_filesystem(struct file_system_type * fs)
     /* [previous][next][first][last][top][bottom][index][help] */
  61 {
  62         struct file_system_type ** tmp;
  63 
  64         tmp = &file_systems;
  65         while (*tmp) {
  66                 if (fs == *tmp) {
  67                         *tmp = fs->next;
  68                         fs->next = NULL;
  69                         return 0;
  70                 }
  71                 tmp = &(*tmp)->next;
  72         }
  73         return -EINVAL;
  74 }
  75 
  76 static int fs_index(const char * __name)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78         struct file_system_type * tmp;
  79         char * name;
  80         int err, index;
  81 
  82         err = getname(__name, &name);
  83         if (err)
  84                 return err;
  85         index = 0;
  86         for (tmp = file_systems ; tmp ; tmp = tmp->next) {
  87                 if (strcmp(tmp->name, name) == 0) {
  88                         putname(name);
  89                         return index;
  90                 }
  91                 index++;
  92         }
  93         putname(name);
  94         return -EINVAL;
  95 }
  96 
  97 static int fs_name(unsigned int index, char * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         struct file_system_type * tmp;
 100         int err, len;
 101 
 102         tmp = file_systems;
 103         while (tmp && index > 0) {
 104                 tmp = tmp->next;
 105                 index--;
 106         }
 107         if (!tmp)
 108                 return -EINVAL;
 109         len = strlen(tmp->name) + 1;
 110         err = verify_area(VERIFY_WRITE, buf, len);
 111         if (err)
 112                 return err;
 113         memcpy_tofs(buf, tmp->name, len);
 114         return 0;
 115 }
 116 
 117 static int fs_maxindex(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119         struct file_system_type * tmp;
 120         int index;
 121 
 122         index = 0;
 123         for (tmp = file_systems ; tmp ; tmp = tmp->next)
 124                 index++;
 125         return index;
 126 }
 127 
 128 /*
 129  * Whee.. Weird sysv syscall. 
 130  */
 131 asmlinkage int sys_sysfs(int option, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133         va_list args;
 134         int retval = -EINVAL;
 135         unsigned int index;
 136 
 137         va_start(args, option);
 138         switch (option) {
 139                 case 1:
 140                         retval = fs_index(va_arg(args, const char *));
 141                         break;
 142 
 143                 case 2:
 144                         index = va_arg(args, unsigned int);
 145                         retval = fs_name(index, va_arg(args, char *));
 146                         break;
 147 
 148                 case 3:
 149                         retval = fs_maxindex();
 150                         break;
 151         }
 152         va_end(args);
 153         return retval;
 154 }
 155 
 156 int get_filesystem_list(char * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158         int len = 0;
 159         struct file_system_type * tmp;
 160 
 161         tmp = file_systems;
 162         while (tmp && len < PAGE_SIZE - 80) {
 163                 len += sprintf(buf+len, "%s\t%s\n",
 164                         tmp->requires_dev ? "" : "nodev",
 165                         tmp->name);
 166                 tmp = tmp->next;
 167         }
 168         return len;
 169 }
 170 
 171 struct file_system_type *get_fs_type(char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173         struct file_system_type * fs = file_systems;
 174         
 175         if (!name)
 176                 return fs;
 177         while (fs) {
 178                 if (!strcmp(name,fs->name))
 179                         break;
 180                 fs = fs->next;
 181         }
 182         return fs;
 183 }
 184 
 185 void __wait_on_super(struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187         struct wait_queue wait = { current, NULL };
 188 
 189         add_wait_queue(&sb->s_wait, &wait);
 190 repeat:
 191         current->state = TASK_UNINTERRUPTIBLE;
 192         if (sb->s_lock) {
 193                 schedule();
 194                 goto repeat;
 195         }
 196         remove_wait_queue(&sb->s_wait, &wait);
 197         current->state = TASK_RUNNING;
 198 }
 199 
 200 void sync_supers(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 201 {
 202         struct super_block * sb;
 203 
 204         for (sb = super_blocks + 0 ; sb < super_blocks + NR_SUPER ; sb++) {
 205                 if (!sb->s_dev)
 206                         continue;
 207                 if (dev && sb->s_dev != dev)
 208                         continue;
 209                 wait_on_super(sb);
 210                 if (!sb->s_dev || !sb->s_dirt)
 211                         continue;
 212                 if (dev && (dev != sb->s_dev))
 213                         continue;
 214                 if (sb->s_op && sb->s_op->write_super)
 215                         sb->s_op->write_super(sb);
 216         }
 217 }
 218 
 219 static struct super_block * get_super(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 {
 221         struct super_block * s;
 222 
 223         if (!dev)
 224                 return NULL;
 225         s = 0+super_blocks;
 226         while (s < NR_SUPER+super_blocks)
 227                 if (s->s_dev == dev) {
 228                         wait_on_super(s);
 229                         if (s->s_dev == dev)
 230                                 return s;
 231                         s = 0+super_blocks;
 232                 } else
 233                         s++;
 234         return NULL;
 235 }
 236 
 237 void put_super(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 238 {
 239         struct super_block * sb;
 240 
 241         if (dev == ROOT_DEV) {
 242                 printk("VFS: Root device %d/%d: prepare for armageddon\n",
 243                                                         MAJOR(dev), MINOR(dev));
 244                 return;
 245         }
 246         if (!(sb = get_super(dev)))
 247                 return;
 248         if (sb->s_covered) {
 249                 printk("VFS: Mounted device %d/%d - tssk, tssk\n",
 250                                                 MAJOR(dev), MINOR(dev));
 251                 return;
 252         }
 253         if (sb->s_op && sb->s_op->put_super)
 254                 sb->s_op->put_super(sb);
 255 }
 256 
 257 static struct super_block * read_super(dev_t dev,char *name,int flags,
     /* [previous][next][first][last][top][bottom][index][help] */
 258                                        void *data, int silent)
 259 {
 260         struct super_block * s;
 261         struct file_system_type *type;
 262 
 263         if (!dev)
 264                 return NULL;
 265         check_disk_change(dev);
 266         s = get_super(dev);
 267         if (s)
 268                 return s;
 269         if (!(type = get_fs_type(name))) {
 270                 printk("VFS: on device %d/%d: get_fs_type(%s) failed\n",
 271                                                 MAJOR(dev), MINOR(dev), name);
 272                 return NULL;
 273         }
 274         for (s = 0+super_blocks ;; s++) {
 275                 if (s >= NR_SUPER+super_blocks)
 276                         return NULL;
 277                 if (!s->s_dev)
 278                         break;
 279         }
 280         s->s_dev = dev;
 281         s->s_flags = flags;
 282         if (!type->read_super(s,data, silent)) {
 283                 s->s_dev = 0;
 284                 return NULL;
 285         }
 286         s->s_dev = dev;
 287         s->s_covered = NULL;
 288         s->s_rd_only = 0;
 289         s->s_dirt = 0;
 290         return s;
 291 }
 292 
 293 /*
 294  * Unnamed block devices are dummy devices used by virtual
 295  * filesystems which don't use real block-devices.  -- jrs
 296  */
 297 
 298 static char unnamed_dev_in_use[256];
 299 
 300 static dev_t get_unnamed_dev(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 301 {
 302         static int first_use = 0;
 303         int i;
 304 
 305         if (first_use == 0) {
 306                 first_use = 1;
 307                 memset(unnamed_dev_in_use, 0, sizeof(unnamed_dev_in_use));
 308                 unnamed_dev_in_use[0] = 1; /* minor 0 (nodev) is special */
 309         }
 310         for (i = 0; i < sizeof unnamed_dev_in_use/sizeof unnamed_dev_in_use[0]; i++) {
 311                 if (!unnamed_dev_in_use[i]) {
 312                         unnamed_dev_in_use[i] = 1;
 313                         return (UNNAMED_MAJOR << 8) | i;
 314                 }
 315         }
 316         return 0;
 317 }
 318 
 319 static void put_unnamed_dev(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 320 {
 321         if (!dev)
 322                 return;
 323         if (!unnamed_dev_in_use[dev]) {
 324                 printk("VFS: put_unnamed_dev: freeing unused device %d/%d\n",
 325                                                         MAJOR(dev), MINOR(dev));
 326                 return;
 327         }
 328         unnamed_dev_in_use[dev] = 0;
 329 }
 330 
 331 static int do_umount(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 332 {
 333         struct super_block * sb;
 334         int retval;
 335         
 336         if (dev==ROOT_DEV) {
 337                 /* Special case for "unmounting" root.  We just try to remount
 338                    it readonly, and sync() the device. */
 339                 if (!(sb=get_super(dev)))
 340                         return -ENOENT;
 341                 if (!(sb->s_flags & MS_RDONLY)) {
 342                         fsync_dev(dev);
 343                         retval = do_remount_sb(sb, MS_RDONLY, 0);
 344                         if (retval)
 345                                 return retval;
 346                 }
 347                 return 0;
 348         }
 349         if (!(sb=get_super(dev)) || !(sb->s_covered))
 350                 return -ENOENT;
 351         if (!sb->s_covered->i_mount)
 352                 printk("VFS: umount(%d/%d): mounted inode has i_mount=NULL\n",
 353                                                         MAJOR(dev), MINOR(dev));
 354         if (!fs_may_umount(dev, sb->s_mounted))
 355                 return -EBUSY;
 356         sb->s_covered->i_mount = NULL;
 357         iput(sb->s_covered);
 358         sb->s_covered = NULL;
 359         iput(sb->s_mounted);
 360         sb->s_mounted = NULL;
 361         if (sb->s_op && sb->s_op->write_super && sb->s_dirt)
 362                 sb->s_op->write_super(sb);
 363         put_super(dev);
 364         return 0;
 365 }
 366 
 367 /*
 368  * Now umount can handle mount points as well as block devices.
 369  * This is important for filesystems which use unnamed block devices.
 370  *
 371  * There is a little kludge here with the dummy_inode.  The current
 372  * vfs release functions only use the r_dev field in the inode so
 373  * we give them the info they need without using a real inode.
 374  * If any other fields are ever needed by any block device release
 375  * functions, they should be faked here.  -- jrs
 376  */
 377 
 378 asmlinkage int sys_umount(char * name)
     /* [previous][next][first][last][top][bottom][index][help] */
 379 {
 380         struct inode * inode;
 381         dev_t dev;
 382         int retval;
 383         struct inode dummy_inode;
 384         struct file_operations * fops;
 385 
 386         if (!suser())
 387                 return -EPERM;
 388         retval = namei(name,&inode);
 389         if (retval) {
 390                 retval = lnamei(name,&inode);
 391                 if (retval)
 392                         return retval;
 393         }
 394         if (S_ISBLK(inode->i_mode)) {
 395                 dev = inode->i_rdev;
 396                 if (IS_NODEV(inode)) {
 397                         iput(inode);
 398                         return -EACCES;
 399                 }
 400         } else {
 401                 if (!inode || !inode->i_sb || inode != inode->i_sb->s_mounted) {
 402                         iput(inode);
 403                         return -EINVAL;
 404                 }
 405                 dev = inode->i_sb->s_dev;
 406                 iput(inode);
 407                 memset(&dummy_inode, 0, sizeof(dummy_inode));
 408                 dummy_inode.i_rdev = dev;
 409                 inode = &dummy_inode;
 410         }
 411         if (MAJOR(dev) >= MAX_BLKDEV) {
 412                 iput(inode);
 413                 return -ENXIO;
 414         }
 415         if (!(retval = do_umount(dev)) && dev != ROOT_DEV) {
 416                 fops = get_blkfops(MAJOR(dev));
 417                 if (fops && fops->release)
 418                         fops->release(inode,NULL);
 419                 if (MAJOR(dev) == UNNAMED_MAJOR)
 420                         put_unnamed_dev(dev);
 421         }
 422         if (inode != &dummy_inode)
 423                 iput(inode);
 424         if (retval)
 425                 return retval;
 426         fsync_dev(dev);
 427         return 0;
 428 }
 429 
 430 /*
 431  * do_mount() does the actual mounting after sys_mount has done the ugly
 432  * parameter parsing. When enough time has gone by, and everything uses the
 433  * new mount() parameters, sys_mount() can then be cleaned up.
 434  *
 435  * We cannot mount a filesystem if it has active, used, or dirty inodes.
 436  * We also have to flush all inode-data for this device, as the new mount
 437  * might need new info.
 438  */
 439 static int do_mount(dev_t dev, const char * dir, char * type, int flags, void * data)
     /* [previous][next][first][last][top][bottom][index][help] */
 440 {
 441         struct inode * dir_i;
 442         struct super_block * sb;
 443         int error;
 444 
 445         error = namei(dir,&dir_i);
 446         if (error)
 447                 return error;
 448         if (dir_i->i_count != 1 || dir_i->i_mount) {
 449                 iput(dir_i);
 450                 return -EBUSY;
 451         }
 452         if (!S_ISDIR(dir_i->i_mode)) {
 453                 iput(dir_i);
 454                 return -EPERM;
 455         }
 456         if (!fs_may_mount(dev)) {
 457                 iput(dir_i);
 458                 return -EBUSY;
 459         }
 460         sb = read_super(dev,type,flags,data,0);
 461         if (!sb || sb->s_covered) {
 462                 iput(dir_i);
 463                 return -EBUSY;
 464         }
 465         sb->s_covered = dir_i;
 466         dir_i->i_mount = sb->s_mounted;
 467         return 0;               /* we don't iput(dir_i) - see umount */
 468 }
 469 
 470 
 471 /*
 472  * Alters the mount flags of a mounted file system. Only the mount point
 473  * is used as a reference - file system type and the device are ignored.
 474  * FS-specific mount options can't be altered by remounting.
 475  */
 476 
 477 static int do_remount_sb(struct super_block *sb, int flags, char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 478 {
 479         int retval;
 480         
 481         /* If we are remounting RDONLY, make sure there are no rw files open */
 482         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY))
 483                 if (!fs_may_remount_ro(sb->s_dev))
 484                         return -EBUSY;
 485         if (sb->s_op && sb->s_op->remount_fs) {
 486                 retval = sb->s_op->remount_fs(sb, &flags, data);
 487                 if (retval)
 488                         return retval;
 489         }
 490         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) |
 491                 (flags & MS_RMT_MASK);
 492         return 0;
 493 }
 494 
 495 static int do_remount(const char *dir,int flags,char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 496 {
 497         struct inode *dir_i;
 498         int retval;
 499 
 500         retval = namei(dir,&dir_i);
 501         if (retval)
 502                 return retval;
 503         if (dir_i != dir_i->i_sb->s_mounted) {
 504                 iput(dir_i);
 505                 return -EINVAL;
 506         }
 507         retval = do_remount_sb(dir_i->i_sb, flags, data);
 508         iput(dir_i);
 509         return retval;
 510 }
 511 
 512 static int copy_mount_options (const void * data, unsigned long *where)
     /* [previous][next][first][last][top][bottom][index][help] */
 513 {
 514         int i;
 515         unsigned long page;
 516         struct vm_area_struct * vma;
 517 
 518         *where = 0;
 519         if (!data)
 520                 return 0;
 521 
 522         for (vma = current->mm->mmap ; ; ) {
 523                 if (!vma ||
 524                     (unsigned long) data < vma->vm_start) {
 525                         return -EFAULT;
 526                 }
 527                 if ((unsigned long) data < vma->vm_end)
 528                         break;
 529                 vma = vma->vm_next;
 530         }
 531         i = vma->vm_end - (unsigned long) data;
 532         if (PAGE_SIZE <= (unsigned long) i)
 533                 i = PAGE_SIZE-1;
 534         if (!(page = __get_free_page(GFP_KERNEL))) {
 535                 return -ENOMEM;
 536         }
 537         memcpy_fromfs((void *) page,data,i);
 538         *where = page;
 539         return 0;
 540 }
 541 
 542 /*
 543  * Flags is a 16-bit value that allows up to 16 non-fs dependent flags to
 544  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
 545  *
 546  * data is a (void *) that can point to any structure up to
 547  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
 548  * information (or be NULL).
 549  *
 550  * NOTE! As old versions of mount() didn't use this setup, the flags
 551  * has to have a special 16-bit magic number in the hight word:
 552  * 0xC0ED. If this magic word isn't present, the flags and data info
 553  * isn't used, as the syscall assumes we are talking to an older
 554  * version that didn't understand them.
 555  */
 556 asmlinkage int sys_mount(char * dev_name, char * dir_name, char * type,
     /* [previous][next][first][last][top][bottom][index][help] */
 557         unsigned long new_flags, void * data)
 558 {
 559         struct file_system_type * fstype;
 560         struct inode * inode;
 561         struct file_operations * fops;
 562         dev_t dev;
 563         int retval;
 564         char * t;
 565         unsigned long flags = 0;
 566         unsigned long page = 0;
 567 
 568         if (!suser())
 569                 return -EPERM;
 570         if ((new_flags &
 571              (MS_MGC_MSK | MS_REMOUNT)) == (MS_MGC_VAL | MS_REMOUNT)) {
 572                 retval = copy_mount_options (data, &page);
 573                 if (retval < 0)
 574                         return retval;
 575                 retval = do_remount(dir_name,
 576                                     new_flags & ~MS_MGC_MSK & ~MS_REMOUNT,
 577                                     (char *) page);
 578                 free_page(page);
 579                 return retval;
 580         }
 581         retval = copy_mount_options (type, &page);
 582         if (retval < 0)
 583                 return retval;
 584         fstype = get_fs_type((char *) page);
 585         free_page(page);
 586         if (!fstype)            
 587                 return -ENODEV;
 588         t = fstype->name;
 589         if (fstype->requires_dev) {
 590                 retval = namei(dev_name,&inode);
 591                 if (retval)
 592                         return retval;
 593                 if (!S_ISBLK(inode->i_mode)) {
 594                         iput(inode);
 595                         return -ENOTBLK;
 596                 }
 597                 if (IS_NODEV(inode)) {
 598                         iput(inode);
 599                         return -EACCES;
 600                 }
 601                 dev = inode->i_rdev;
 602                 if (MAJOR(dev) >= MAX_BLKDEV) {
 603                         iput(inode);
 604                         return -ENXIO;
 605                 }
 606         } else {
 607                 if (!(dev = get_unnamed_dev()))
 608                         return -EMFILE;
 609                 inode = NULL;
 610         }
 611         fops = get_blkfops(MAJOR(dev));
 612         if (fops && fops->open) {
 613                 retval = fops->open(inode,NULL);
 614                 if (retval) {
 615                         iput(inode);
 616                         return retval;
 617                 }
 618         }
 619         page = 0;
 620         if ((new_flags & MS_MGC_MSK) == MS_MGC_VAL) {
 621                 flags = new_flags & ~MS_MGC_MSK;
 622                 retval = copy_mount_options(data, &page);
 623                 if (retval < 0) {
 624                         iput(inode);
 625                         return retval;
 626                 }
 627         }
 628         retval = do_mount(dev,dir_name,t,flags,(void *) page);
 629         free_page(page);
 630         if (retval && fops && fops->release)
 631                 fops->release(inode,NULL);
 632         iput(inode);
 633         return retval;
 634 }
 635 
 636 void mount_root(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 637 {
 638         struct file_system_type * fs_type;
 639         struct super_block * sb;
 640         struct inode * inode;
 641 
 642         memset(super_blocks, 0, sizeof(super_blocks));
 643         fcntl_init_locks();
 644         if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
 645                 printk(KERN_NOTICE "VFS: Insert root floppy and press ENTER\n");
 646                 wait_for_keypress();
 647         }
 648         for (fs_type = file_systems ; fs_type ; fs_type = fs_type->next) {
 649                 if (!fs_type->requires_dev)
 650                         continue;
 651                 sb = read_super(ROOT_DEV,fs_type->name,root_mountflags,NULL,1);
 652                 if (sb) {
 653                         inode = sb->s_mounted;
 654                         inode->i_count += 3 ;   /* NOTE! it is logically used 4 times, not 1 */
 655                         sb->s_covered = inode;
 656                         sb->s_flags = root_mountflags;
 657                         current->fs->pwd = inode;
 658                         current->fs->root = inode;
 659                         printk ("VFS: Mounted root (%s filesystem)%s.\n",
 660                                 fs_type->name,
 661                                 (sb->s_flags & MS_RDONLY) ? " readonly" : "");
 662                         return;
 663                 }
 664         }
 665         panic("VFS: Unable to mount root");
 666 }

/* [previous][next][first][last][top][bottom][index][help] */