root/fs/super.c

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

DEFINITIONS

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

   1 /*
   2  *  linux/fs/super.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  *
   6  *  super.c contains code to handle: - mount structures
   7  *                                   - super-block tables.
   8  *                                   - mount systemcall
   9  *                                   - umount systemcall
  10  *
  11  * GK 2/5/95  -  Changed to support mounting the root fs via NFS
  12  */
  13 
  14 #include <stdarg.h>
  15 
  16 #include <linux/config.h>
  17 #include <linux/sched.h>
  18 #include <linux/kernel.h>
  19 #include <linux/mount.h>
  20 #include <linux/malloc.h>
  21 #include <linux/major.h>
  22 #include <linux/stat.h>
  23 #include <linux/errno.h>
  24 #include <linux/string.h>
  25 #include <linux/locks.h>
  26 #include <linux/mm.h>
  27 
  28 #include <asm/system.h>
  29 #include <asm/segment.h>
  30 #include <asm/bitops.h>
  31  
  32 extern struct file_operations * get_blkfops(unsigned int);
  33 extern struct file_operations * get_chrfops(unsigned int);
  34 
  35 extern void wait_for_keypress(void);
  36 
  37 extern int root_mountflags;
  38 
  39 static int do_remount_sb(struct super_block *sb, int flags, char * data);
  40 
  41 #ifdef CONFIG_ROOT_NFS
  42 extern int nfs_root_mount(struct super_block *sb);
  43 #endif
  44 
  45 /* this is initialized in init/main.c */
  46 kdev_t ROOT_DEV;
  47 
  48 struct super_block super_blocks[NR_SUPER];
  49 static struct file_system_type *file_systems = (struct file_system_type *) NULL;
  50 static struct vfsmount *vfsmntlist = (struct vfsmount *) NULL,
  51                        *vfsmnttail = (struct vfsmount *) NULL,
  52                        *mru_vfsmnt = (struct vfsmount *) NULL;
  53 
  54 /* 
  55  * This part handles the management of the list of mounted filesystems.
  56  */
  57 struct vfsmount *lookup_vfsmnt(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59         struct vfsmount *lptr;
  60 
  61         if (vfsmntlist == (struct vfsmount *)NULL)
  62                 return ((struct vfsmount *)NULL);
  63 
  64         if (mru_vfsmnt != (struct vfsmount *)NULL &&
  65             mru_vfsmnt->mnt_dev == dev)
  66                 return (mru_vfsmnt);
  67 
  68         for (lptr = vfsmntlist;
  69              lptr != (struct vfsmount *)NULL;
  70              lptr = lptr->mnt_next)
  71                 if (lptr->mnt_dev == dev) {
  72                         mru_vfsmnt = lptr;
  73                         return (lptr);
  74                 }
  75 
  76         return ((struct vfsmount *)NULL);
  77         /* NOTREACHED */
  78 }
  79 
  80 struct vfsmount *add_vfsmnt(kdev_t dev, const char *dev_name, const char *dir_name)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82         struct vfsmount *lptr;
  83         char *tmp;
  84 
  85         if ((lptr = (struct vfsmount *)
  86              kmalloc(sizeof(struct vfsmount), GFP_KERNEL)) == (struct vfsmount *)NULL)
  87                 return ((struct vfsmount *)NULL);
  88         memset(lptr, 0, sizeof(struct vfsmount));
  89 
  90         lptr->mnt_dev = dev;
  91         lptr->mnt_sem.count = 1;
  92         if (dev_name && !getname(dev_name, &tmp)) {
  93                 if ((lptr->mnt_devname =
  94                     (char *) kmalloc(strlen(tmp)+1, GFP_KERNEL)) != (char *)NULL)
  95                         strcpy(lptr->mnt_devname, tmp);
  96                 putname(tmp);
  97         }
  98         if (dir_name && !getname(dir_name, &tmp)) {
  99                 if ((lptr->mnt_dirname =
 100                     (char *) kmalloc(strlen(tmp)+1, GFP_KERNEL)) != (char *)NULL)
 101                         strcpy(lptr->mnt_dirname, tmp);
 102                 putname(tmp);
 103         }
 104 
 105         if (vfsmntlist == (struct vfsmount *)NULL) {
 106                 vfsmntlist = vfsmnttail = lptr;
 107         } else {
 108                 vfsmnttail->mnt_next = lptr;
 109                 vfsmnttail = lptr;
 110         }
 111         return (lptr);
 112 }
 113 
 114 void remove_vfsmnt(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116         struct vfsmount *lptr, *tofree;
 117 
 118         if (vfsmntlist == (struct vfsmount *)NULL)
 119                 return;
 120         lptr = vfsmntlist;
 121         if (lptr->mnt_dev == dev) {
 122                 tofree = lptr;
 123                 vfsmntlist = lptr->mnt_next;
 124                 if (vfsmnttail->mnt_dev == dev)
 125                         vfsmnttail = vfsmntlist;
 126         } else {
 127                 while (lptr->mnt_next != (struct vfsmount *)NULL) {
 128                         if (lptr->mnt_next->mnt_dev == dev)
 129                                 break;
 130                         lptr = lptr->mnt_next;
 131                 }
 132                 tofree = lptr->mnt_next;
 133                 if (tofree == (struct vfsmount *)NULL)
 134                         return;
 135                 lptr->mnt_next = lptr->mnt_next->mnt_next;
 136                 if (vfsmnttail->mnt_dev == dev)
 137                         vfsmnttail = lptr;
 138         }
 139         kfree(tofree->mnt_devname);
 140         kfree(tofree->mnt_dirname);
 141         kfree_s(tofree, sizeof(struct vfsmount));
 142 }
 143 
 144 int register_filesystem(struct file_system_type * fs)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146         struct file_system_type ** tmp;
 147 
 148         if (!fs)
 149                 return -EINVAL;
 150         if (fs->next)
 151                 return -EBUSY;
 152         tmp = &file_systems;
 153         while (*tmp) {
 154                 if (strcmp((*tmp)->name, fs->name) == 0)
 155                         return -EBUSY;
 156                 tmp = &(*tmp)->next;
 157         }
 158         *tmp = fs;
 159         return 0;
 160 }
 161 
 162 int unregister_filesystem(struct file_system_type * fs)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164         struct file_system_type ** tmp;
 165 
 166         tmp = &file_systems;
 167         while (*tmp) {
 168                 if (fs == *tmp) {
 169                         *tmp = fs->next;
 170                         fs->next = NULL;
 171                         return 0;
 172                 }
 173                 tmp = &(*tmp)->next;
 174         }
 175         return -EINVAL;
 176 }
 177 
 178 static int fs_index(const char * __name)
     /* [previous][next][first][last][top][bottom][index][help] */
 179 {
 180         struct file_system_type * tmp;
 181         char * name;
 182         int err, index;
 183 
 184         err = getname(__name, &name);
 185         if (err)
 186                 return err;
 187         index = 0;
 188         for (tmp = file_systems ; tmp ; tmp = tmp->next) {
 189                 if (strcmp(tmp->name, name) == 0) {
 190                         putname(name);
 191                         return index;
 192                 }
 193                 index++;
 194         }
 195         putname(name);
 196         return -EINVAL;
 197 }
 198 
 199 static int fs_name(unsigned int index, char * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         struct file_system_type * tmp;
 202         int err, len;
 203 
 204         tmp = file_systems;
 205         while (tmp && index > 0) {
 206                 tmp = tmp->next;
 207                 index--;
 208         }
 209         if (!tmp)
 210                 return -EINVAL;
 211         len = strlen(tmp->name) + 1;
 212         err = verify_area(VERIFY_WRITE, buf, len);
 213         if (err)
 214                 return err;
 215         memcpy_tofs(buf, tmp->name, len);
 216         return 0;
 217 }
 218 
 219 static int fs_maxindex(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 {
 221         struct file_system_type * tmp;
 222         int index;
 223 
 224         index = 0;
 225         for (tmp = file_systems ; tmp ; tmp = tmp->next)
 226                 index++;
 227         return index;
 228 }
 229 
 230 /*
 231  * Whee.. Weird sysv syscall. 
 232  */
 233 asmlinkage int sys_sysfs(int option, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235         va_list args;
 236         int retval = -EINVAL;
 237         unsigned int index;
 238 
 239         va_start(args, option);
 240         switch (option) {
 241                 case 1:
 242                         retval = fs_index(va_arg(args, const char *));
 243                         break;
 244 
 245                 case 2:
 246                         index = va_arg(args, unsigned int);
 247                         retval = fs_name(index, va_arg(args, char *));
 248                         break;
 249 
 250                 case 3:
 251                         retval = fs_maxindex();
 252                         break;
 253         }
 254         va_end(args);
 255         return retval;
 256 }
 257 
 258 int get_filesystem_list(char * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 259 {
 260         int len = 0;
 261         struct file_system_type * tmp;
 262 
 263         tmp = file_systems;
 264         while (tmp && len < PAGE_SIZE - 80) {
 265                 len += sprintf(buf+len, "%s\t%s\n",
 266                         tmp->requires_dev ? "" : "nodev",
 267                         tmp->name);
 268                 tmp = tmp->next;
 269         }
 270         return len;
 271 }
 272 
 273 struct file_system_type *get_fs_type(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 274 {
 275         struct file_system_type * fs = file_systems;
 276         
 277         if (!name)
 278                 return fs;
 279         while (fs) {
 280                 if (!strcmp(name,fs->name))
 281                         break;
 282                 fs = fs->next;
 283         }
 284         return fs;
 285 }
 286 
 287 void __wait_on_super(struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289         struct wait_queue wait = { current, NULL };
 290 
 291         add_wait_queue(&sb->s_wait, &wait);
 292 repeat:
 293         current->state = TASK_UNINTERRUPTIBLE;
 294         if (sb->s_lock) {
 295                 schedule();
 296                 goto repeat;
 297         }
 298         remove_wait_queue(&sb->s_wait, &wait);
 299         current->state = TASK_RUNNING;
 300 }
 301 
 302 void sync_supers(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 303 {
 304         struct super_block * sb;
 305 
 306         for (sb = super_blocks + 0 ; sb < super_blocks + NR_SUPER ; sb++) {
 307                 if (!sb->s_dev)
 308                         continue;
 309                 if (dev && sb->s_dev != dev)
 310                         continue;
 311                 wait_on_super(sb);
 312                 if (!sb->s_dev || !sb->s_dirt)
 313                         continue;
 314                 if (dev && (dev != sb->s_dev))
 315                         continue;
 316                 if (sb->s_op && sb->s_op->write_super)
 317                         sb->s_op->write_super(sb);
 318         }
 319 }
 320 
 321 static struct super_block * get_super(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 322 {
 323         struct super_block * s;
 324 
 325         if (!dev)
 326                 return NULL;
 327         s = 0+super_blocks;
 328         while (s < NR_SUPER+super_blocks)
 329                 if (s->s_dev == dev) {
 330                         wait_on_super(s);
 331                         if (s->s_dev == dev)
 332                                 return s;
 333                         s = 0+super_blocks;
 334                 } else
 335                         s++;
 336         return NULL;
 337 }
 338 
 339 void put_super(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 340 {
 341         struct super_block * sb;
 342 
 343         if (dev == ROOT_DEV) {
 344                 printk("VFS: Root device %s: prepare for armageddon\n",
 345                        kdevname(dev));
 346                 return;
 347         }
 348         if (!(sb = get_super(dev)))
 349                 return;
 350         if (sb->s_covered) {
 351                 printk("VFS: Mounted device %s - tssk, tssk\n",
 352                        kdevname(dev));
 353                 return;
 354         }
 355         if (sb->s_op && sb->s_op->put_super)
 356                 sb->s_op->put_super(sb);
 357 }
 358 
 359 asmlinkage int sys_ustat(dev_t dev, struct ustat * ubuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 360 {
 361         struct super_block *s;
 362         struct ustat tmp;
 363         struct statfs sbuf;
 364         unsigned long old_fs;
 365         int error;
 366 
 367         s = get_super(to_kdev_t(dev));
 368         if (s == NULL)
 369                 return -EINVAL;
 370 
 371         if (!(s->s_op->statfs))
 372                 return -ENOSYS;
 373 
 374         error = verify_area(VERIFY_WRITE,ubuf,sizeof(struct ustat));
 375         if (error)
 376                 return error;
 377 
 378         old_fs = get_fs();
 379         set_fs(get_ds());
 380         s->s_op->statfs(s,&sbuf,sizeof(struct statfs));
 381         set_fs(old_fs);
 382 
 383         memset(&tmp,0,sizeof(struct ustat));
 384         tmp.f_tfree = sbuf.f_bfree;
 385         tmp.f_tinode = sbuf.f_ffree;
 386 
 387         memcpy_tofs(ubuf,&tmp,sizeof(struct ustat));
 388         return 0;
 389 }
 390 
 391 static struct super_block * read_super(kdev_t dev,const char *name,int flags,
     /* [previous][next][first][last][top][bottom][index][help] */
 392                                        void *data, int silent)
 393 {
 394         struct super_block * s;
 395         struct file_system_type *type;
 396 
 397         if (!dev)
 398                 return NULL;
 399         check_disk_change(dev);
 400         s = get_super(dev);
 401         if (s)
 402                 return s;
 403         if (!(type = get_fs_type(name))) {
 404                 printk("VFS: on device %s: get_fs_type(%s) failed\n",
 405                        kdevname(dev), name);
 406                 return NULL;
 407         }
 408         for (s = 0+super_blocks ;; s++) {
 409                 if (s >= NR_SUPER+super_blocks)
 410                         return NULL;
 411                 if (!(s->s_dev))
 412                         break;
 413         }
 414         s->s_dev = dev;
 415         s->s_flags = flags;
 416         if (!type->read_super(s,data, silent)) {
 417                 s->s_dev = 0;
 418                 return NULL;
 419         }
 420         s->s_dev = dev;
 421         s->s_covered = NULL;
 422         s->s_rd_only = 0;
 423         s->s_dirt = 0;
 424         s->s_type = type;
 425         return s;
 426 }
 427 
 428 /*
 429  * Unnamed block devices are dummy devices used by virtual
 430  * filesystems which don't use real block-devices.  -- jrs
 431  */
 432 
 433 static char unnamed_dev_in_use[256/8] = { 0, };
 434 
 435 kdev_t get_unnamed_dev(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 436 {
 437         int i;
 438 
 439         for (i = 1; i < 256; i++) {
 440                 if (!set_bit(i,unnamed_dev_in_use))
 441                         return MKDEV(UNNAMED_MAJOR, i);
 442         }
 443         return 0;
 444 }
 445 
 446 void put_unnamed_dev(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 447 {
 448         if (!dev)
 449                 return;
 450         if (MAJOR(dev) == UNNAMED_MAJOR &&
 451             clear_bit(MINOR(dev), unnamed_dev_in_use))
 452                 return;
 453         printk("VFS: put_unnamed_dev: freeing unused device %s\n",
 454                         kdevname(dev));
 455 }
 456 
 457 static int do_umount(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 458 {
 459         struct super_block * sb;
 460         int retval;
 461         
 462         if (dev==ROOT_DEV) {
 463                 /*
 464                  * Special case for "unmounting" root. We just try to remount
 465                  * it readonly, and sync() the device.
 466                  */
 467                 if (!(sb=get_super(dev)))
 468                         return -ENOENT;
 469                 if (!(sb->s_flags & MS_RDONLY)) {
 470                         /*
 471                          * Make sure all quotas are turned off on this device we need to mount
 472                          * it readonly so no more writes by the quotasystem.
 473                          * If later on the remount fails to bad there are no quotas running
 474                          * anymore. Turn them on again by hand.
 475                          */
 476                         quota_off(dev, -1);
 477                         fsync_dev(dev);
 478                         retval = do_remount_sb(sb, MS_RDONLY, 0);
 479                         if (retval)
 480                                 return retval;
 481                 }
 482                 return 0;
 483         }
 484         if (!(sb=get_super(dev)) || !(sb->s_covered))
 485                 return -ENOENT;
 486         if (!sb->s_covered->i_mount)
 487                 printk("VFS: umount(%s): mounted inode has i_mount=NULL\n",
 488                        kdevname(dev));
 489         /*
 490          * Before checking if the filesystem is still busy make sure the kernel
 491          * doesn't hold any quotafiles open on that device. If the umount fails
 492          * to bad there are no quotas running anymore. Turn them on again by hand.
 493          */
 494         quota_off(dev, -1);
 495         if (!fs_may_umount(dev, sb->s_mounted))
 496                 return -EBUSY;
 497         sb->s_covered->i_mount = NULL;
 498         iput(sb->s_covered);
 499         sb->s_covered = NULL;
 500         iput(sb->s_mounted);
 501         sb->s_mounted = NULL;
 502         if (sb->s_op && sb->s_op->write_super && sb->s_dirt)
 503                 sb->s_op->write_super(sb);
 504         put_super(dev);
 505         remove_vfsmnt(dev);
 506         return 0;
 507 }
 508 
 509 /*
 510  * Now umount can handle mount points as well as block devices.
 511  * This is important for filesystems which use unnamed block devices.
 512  *
 513  * There is a little kludge here with the dummy_inode.  The current
 514  * vfs release functions only use the r_dev field in the inode so
 515  * we give them the info they need without using a real inode.
 516  * If any other fields are ever needed by any block device release
 517  * functions, they should be faked here.  -- jrs
 518  */
 519 
 520 asmlinkage int sys_umount(char * name)
     /* [previous][next][first][last][top][bottom][index][help] */
 521 {
 522         struct inode * inode;
 523         kdev_t dev;
 524         int retval;
 525         struct inode dummy_inode;
 526         struct file_operations * fops;
 527 
 528         if (!suser())
 529                 return -EPERM;
 530         retval = namei(name, &inode);
 531         if (retval) {
 532                 retval = lnamei(name, &inode);
 533                 if (retval)
 534                         return retval;
 535         }
 536         if (S_ISBLK(inode->i_mode)) {
 537                 dev = inode->i_rdev;
 538                 if (IS_NODEV(inode)) {
 539                         iput(inode);
 540                         return -EACCES;
 541                 }
 542         } else {
 543                 if (!inode->i_sb || inode != inode->i_sb->s_mounted) {
 544                         iput(inode);
 545                         return -EINVAL;
 546                 }
 547                 dev = inode->i_sb->s_dev;
 548                 iput(inode);
 549                 memset(&dummy_inode, 0, sizeof(dummy_inode));
 550                 dummy_inode.i_rdev = dev;
 551                 inode = &dummy_inode;
 552         }
 553         if (MAJOR(dev) >= MAX_BLKDEV) {
 554                 iput(inode);
 555                 return -ENXIO;
 556         }
 557         if (!(retval = do_umount(dev)) && dev != ROOT_DEV) {
 558                 fops = get_blkfops(MAJOR(dev));
 559                 if (fops && fops->release)
 560                         fops->release(inode,NULL);
 561                 if (MAJOR(dev) == UNNAMED_MAJOR)
 562                         put_unnamed_dev(dev);
 563         }
 564         if (inode != &dummy_inode)
 565                 iput(inode);
 566         if (retval)
 567                 return retval;
 568         fsync_dev(dev);
 569         return 0;
 570 }
 571 
 572 /*
 573  * do_mount() does the actual mounting after sys_mount has done the ugly
 574  * parameter parsing. When enough time has gone by, and everything uses the
 575  * new mount() parameters, sys_mount() can then be cleaned up.
 576  *
 577  * We cannot mount a filesystem if it has active, used, or dirty inodes.
 578  * We also have to flush all inode-data for this device, as the new mount
 579  * might need new info.
 580  */
 581 
 582 int do_mount(kdev_t dev, const char * dev_name, const char * dir_name, const char * type, int flags, void * data)
     /* [previous][next][first][last][top][bottom][index][help] */
 583 {
 584         struct inode * dir_i;
 585         struct super_block * sb;
 586         struct vfsmount *vfsmnt;
 587         int error;
 588 
 589         error = namei(dir_name, &dir_i);
 590         if (error)
 591                 return error;
 592         if (dir_i->i_count != 1 || dir_i->i_mount) {
 593                 iput(dir_i);
 594                 return -EBUSY;
 595         }
 596         if (!S_ISDIR(dir_i->i_mode)) {
 597                 iput(dir_i);
 598                 return -ENOTDIR;
 599         }
 600         if (!fs_may_mount(dev)) {
 601                 iput(dir_i);
 602                 return -EBUSY;
 603         }
 604         sb = read_super(dev,type,flags,data,0);
 605         if (!sb) {
 606                 iput(dir_i);
 607                 return -EINVAL;
 608         }
 609         if (sb->s_covered) {
 610                 iput(dir_i);
 611                 return -EBUSY;
 612         }
 613         vfsmnt = add_vfsmnt(dev, dev_name, dir_name);
 614         vfsmnt->mnt_sb = sb;
 615         sb->s_covered = dir_i;
 616         dir_i->i_mount = sb->s_mounted;
 617         return 0;               /* we don't iput(dir_i) - see umount */
 618 }
 619 
 620 
 621 /*
 622  * Alters the mount flags of a mounted file system. Only the mount point
 623  * is used as a reference - file system type and the device are ignored.
 624  * FS-specific mount options can't be altered by remounting.
 625  */
 626 
 627 static int do_remount_sb(struct super_block *sb, int flags, char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 628 {
 629         int retval;
 630         
 631         if (!(flags & MS_RDONLY) && sb->s_dev && is_read_only(sb->s_dev))
 632                 return -EACCES;
 633                 /*flags |= MS_RDONLY;*/
 634         /* If we are remounting RDONLY, make sure there are no rw files open */
 635         if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY))
 636                 if (!fs_may_remount_ro(sb->s_dev))
 637                         return -EBUSY;
 638         if (sb->s_op && sb->s_op->remount_fs) {
 639                 retval = sb->s_op->remount_fs(sb, &flags, data);
 640                 if (retval)
 641                         return retval;
 642         }
 643         sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) |
 644                 (flags & MS_RMT_MASK);
 645         return 0;
 646 }
 647 
 648 static int do_remount(const char *dir,int flags,char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 649 {
 650         struct inode *dir_i;
 651         int retval;
 652 
 653         retval = namei(dir, &dir_i);
 654         if (retval)
 655                 return retval;
 656         if (dir_i != dir_i->i_sb->s_mounted) {
 657                 iput(dir_i);
 658                 return -EINVAL;
 659         }
 660         retval = do_remount_sb(dir_i->i_sb, flags, data);
 661         iput(dir_i);
 662         return retval;
 663 }
 664 
 665 static int copy_mount_options (const void * data, unsigned long *where)
     /* [previous][next][first][last][top][bottom][index][help] */
 666 {
 667         int i;
 668         unsigned long page;
 669         struct vm_area_struct * vma;
 670 
 671         *where = 0;
 672         if (!data)
 673                 return 0;
 674 
 675         vma = find_vma(current, (unsigned long) data);
 676         if (!vma || (unsigned long) data < vma->vm_start)
 677                 return -EFAULT;
 678         i = vma->vm_end - (unsigned long) data;
 679         if (PAGE_SIZE <= (unsigned long) i)
 680                 i = PAGE_SIZE-1;
 681         if (!(page = __get_free_page(GFP_KERNEL))) {
 682                 return -ENOMEM;
 683         }
 684         memcpy_fromfs((void *) page,data,i);
 685         *where = page;
 686         return 0;
 687 }
 688 
 689 /*
 690  * Flags is a 16-bit value that allows up to 16 non-fs dependent flags to
 691  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
 692  *
 693  * data is a (void *) that can point to any structure up to
 694  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
 695  * information (or be NULL).
 696  *
 697  * NOTE! As old versions of mount() didn't use this setup, the flags
 698  * has to have a special 16-bit magic number in the hight word:
 699  * 0xC0ED. If this magic word isn't present, the flags and data info
 700  * isn't used, as the syscall assumes we are talking to an older
 701  * version that didn't understand them.
 702  */
 703 asmlinkage int sys_mount(char * dev_name, char * dir_name, char * type,
     /* [previous][next][first][last][top][bottom][index][help] */
 704         unsigned long new_flags, void * data)
 705 {
 706         struct file_system_type * fstype;
 707         struct inode * inode;
 708         struct file_operations * fops;
 709         kdev_t dev;
 710         int retval;
 711         const char * t;
 712         unsigned long flags = 0;
 713         unsigned long page = 0;
 714 
 715         if (!suser())
 716                 return -EPERM;
 717         if ((new_flags &
 718              (MS_MGC_MSK | MS_REMOUNT)) == (MS_MGC_VAL | MS_REMOUNT)) {
 719                 retval = copy_mount_options (data, &page);
 720                 if (retval < 0)
 721                         return retval;
 722                 retval = do_remount(dir_name,
 723                                     new_flags & ~MS_MGC_MSK & ~MS_REMOUNT,
 724                                     (char *) page);
 725                 free_page(page);
 726                 return retval;
 727         }
 728         retval = copy_mount_options (type, &page);
 729         if (retval < 0)
 730                 return retval;
 731         fstype = get_fs_type((char *) page);
 732         free_page(page);
 733         if (!fstype)            
 734                 return -ENODEV;
 735         t = fstype->name;
 736         fops = NULL;
 737         if (fstype->requires_dev) {
 738                 retval = namei(dev_name, &inode);
 739                 if (retval)
 740                         return retval;
 741                 if (!S_ISBLK(inode->i_mode)) {
 742                         iput(inode);
 743                         return -ENOTBLK;
 744                 }
 745                 if (IS_NODEV(inode)) {
 746                         iput(inode);
 747                         return -EACCES;
 748                 }
 749                 dev = inode->i_rdev;
 750                 if (MAJOR(dev) >= MAX_BLKDEV) {
 751                         iput(inode);
 752                         return -ENXIO;
 753                 }
 754                 fops = get_blkfops(MAJOR(dev));
 755                 if (!fops) {
 756                         iput(inode);
 757                         return -ENOTBLK;
 758                 }
 759                 if (fops->open) {
 760                         struct file dummy;      /* allows read-write or read-only flag */
 761                         memset(&dummy, 0, sizeof(dummy));
 762                         dummy.f_inode = inode;
 763                         dummy.f_mode = (new_flags & MS_RDONLY) ? 1 : 3;
 764                         retval = fops->open(inode, &dummy);
 765                         if (retval) {
 766                                 iput(inode);
 767                                 return retval;
 768                         }
 769                 }
 770 
 771         } else {
 772                 if (!(dev = get_unnamed_dev()))
 773                         return -EMFILE;
 774                 inode = NULL;
 775         }
 776         page = 0;
 777         if ((new_flags & MS_MGC_MSK) == MS_MGC_VAL) {
 778                 flags = new_flags & ~MS_MGC_MSK;
 779                 retval = copy_mount_options(data, &page);
 780                 if (retval < 0) {
 781                         iput(inode);
 782                         return retval;
 783                 }
 784         }
 785         retval = do_mount(dev,dev_name,dir_name,t,flags,(void *) page);
 786         free_page(page);
 787         if (retval && fops && fops->release)
 788                 fops->release(inode, NULL);
 789         iput(inode);
 790         return retval;
 791 }
 792 
 793 void mount_root(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 794 {
 795         struct file_system_type * fs_type;
 796         struct super_block * sb;
 797         struct vfsmount *vfsmnt;
 798         struct inode * inode, d_inode;
 799         struct file filp;
 800         int retval;
 801   
 802         memset(super_blocks, 0, sizeof(super_blocks));
 803 #ifdef CONFIG_ROOT_NFS
 804         if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
 805                 ROOT_DEV = 0;
 806                 if ((fs_type = get_fs_type("nfs"))) {
 807                         sb = &super_blocks[0];
 808                         sb->s_dev = get_unnamed_dev();
 809                         sb->s_flags = root_mountflags & ~MS_RDONLY;
 810                         if (nfs_root_mount(sb) >= 0) {
 811                                 inode = sb->s_mounted;
 812                                 inode->i_count += 3 ;
 813                                 sb->s_covered = inode;
 814                                 sb->s_rd_only = 0;
 815                                 sb->s_dirt = 0;
 816                                 sb->s_type = fs_type;
 817                                 current->fs->pwd = inode;
 818                                 current->fs->root = inode;
 819                                 ROOT_DEV = sb->s_dev;
 820                                 printk (KERN_NOTICE "VFS: Mounted root (nfs filesystem).\n");
 821                                 return;
 822                         }
 823                         sb->s_dev = 0;
 824                 }
 825                 if (!ROOT_DEV) {
 826                         printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
 827                         ROOT_DEV = MKDEV(FLOPPY_MAJOR, 0);
 828                 }
 829         }
 830 #endif
 831 
 832 #ifdef CONFIG_BLK_DEV_FD
 833         if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
 834                 printk(KERN_NOTICE "VFS: Insert root floppy and press ENTER\n");
 835                 wait_for_keypress();
 836         }
 837 #endif
 838 
 839         memset(&filp, 0, sizeof(filp));
 840         memset(&d_inode, 0, sizeof(d_inode));
 841         d_inode.i_rdev = ROOT_DEV;
 842         filp.f_inode = &d_inode;
 843         if ( root_mountflags & MS_RDONLY)
 844                 filp.f_mode = 1; /* read only */
 845         else
 846                 filp.f_mode = 3; /* read write */
 847         retval = blkdev_open(&d_inode, &filp);
 848         if(retval == -EROFS){
 849                 root_mountflags |= MS_RDONLY;
 850                 filp.f_mode = 1;
 851                 retval = blkdev_open(&d_inode, &filp);
 852         }
 853 
 854         for (fs_type = file_systems ; fs_type ; fs_type = fs_type->next) {
 855                 if(retval)
 856                         break;
 857                 if (!fs_type->requires_dev)
 858                         continue;
 859                 sb = read_super(ROOT_DEV,fs_type->name,root_mountflags,NULL,1);
 860                 if (sb) {
 861                         inode = sb->s_mounted;
 862                         inode->i_count += 3 ;   /* NOTE! it is logically used 4 times, not 1 */
 863                         sb->s_covered = inode;
 864                         sb->s_flags = root_mountflags;
 865                         current->fs->pwd = inode;
 866                         current->fs->root = inode;
 867                         printk ("VFS: Mounted root (%s filesystem)%s.\n",
 868                                 fs_type->name,
 869                                 (sb->s_flags & MS_RDONLY) ? " readonly" : "");
 870                         vfsmnt = add_vfsmnt(ROOT_DEV, "rootfs", "/");
 871                         vfsmnt->mnt_sb = sb;
 872                         return;
 873                 }
 874         }
 875         panic("VFS: Unable to mount root fs on %s",
 876                 kdevname(ROOT_DEV));
 877 }

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