/*===========================================================================* * mount_fs * *===========================================================================*/ int mount_fs( dev_t dev, char mountpoint[PATH_MAX], endpoint_t fs_e, int rdonly, char mount_label[LABEL_MAX] ) { int i, r = OK, found, isroot, mount_root, con_reqs, slot; struct fproc *tfp, *rfp; struct dmap *dp; struct vnode *root_node, *vp = NULL; struct vmnt *new_vmp, *parent_vmp; char *label; struct node_details res; struct lookup resolve; /* Look up block device driver label when dev is not a pseudo-device */ label = ""; if (!is_nonedev(dev)) { /* Get driver process' endpoint */ dp = &dmap[major(dev)]; if (dp->dmap_driver == NONE) { printf("VFS: no driver for dev %d\n", dev); return(EINVAL); } label = dp->dmap_label; assert(strlen(label) > 0); } /* Scan vmnt table to see if dev already mounted. If not, find a free slot.*/ found = FALSE; for (i = 0; i < NR_MNTS; ++i) { if (vmnt[i].m_dev == dev) found = TRUE; } if (found) { return(EBUSY); } else if ((new_vmp = get_free_vmnt()) == NULL) { return(ENOMEM); } if ((r = lock_vmnt(new_vmp, VMNT_EXCL)) != OK) return(r); isroot = (strcmp(mountpoint, "/") == 0); mount_root = (isroot && have_root < 2); /* Root can be mounted twice: * 1: ramdisk * 2: boot disk (e.g., harddisk) */ if (!mount_root) { /* Get vnode of mountpoint */ lookup_init(&resolve, mountpoint, PATH_NOFLAGS, &parent_vmp, &vp); resolve.l_vmnt_lock = VMNT_EXCL; resolve.l_vnode_lock = VNODE_WRITE; if ((vp = eat_path(&resolve, fp)) == NULL) r = err_code; else if (vp->v_ref_count == 1) { /*Tell FS on which vnode it is mounted (glue into mount tree)*/ r = req_mountpoint(vp->v_fs_e, vp->v_inode_nr); } else r = EBUSY; if (vp != NULL) { /* Quickly unlock to allow back calls (from e.g. FUSE) to * relock */ unlock_vmnt(parent_vmp); } if (r != OK) { if (vp != NULL) { unlock_vnode(vp); put_vnode(vp); } unlock_vmnt(new_vmp); return(r); } } /* We'll need a vnode for the root inode */ if ((root_node = get_free_vnode()) == NULL) { if (vp != NULL) { unlock_vnode(vp); put_vnode(vp); } unlock_vmnt(new_vmp); return(err_code); } lock_vnode(root_node, VNODE_OPCL); /* Record process as a system process */ if (isokendpt(fs_e, &slot) != OK) { if (vp != NULL) { unlock_vnode(vp); put_vnode(vp); } unlock_vnode(root_node); unlock_vmnt(new_vmp); return(EINVAL); } rfp = &fproc[slot]; rfp->fp_flags |= FP_SRV_PROC; /* File Servers are also services */ /* Store some essential vmnt data first */ new_vmp->m_fs_e = fs_e; new_vmp->m_dev = dev; if (rdonly) new_vmp->m_flags |= VMNT_READONLY; else new_vmp->m_flags &= ~VMNT_READONLY; /* Tell FS which device to mount */ new_vmp->m_flags |= VMNT_MOUNTING; r = req_readsuper(fs_e, label, dev, rdonly, isroot, &res, &con_reqs); new_vmp->m_flags &= ~VMNT_MOUNTING; if (r != OK) { mark_vmnt_free(new_vmp); unlock_vnode(root_node); if (vp != NULL) { unlock_vnode(vp); put_vnode(vp); } unlock_vmnt(new_vmp); return(r); } lock_bsf(); /* Fill in root node's fields */ root_node->v_fs_e = res.fs_e; root_node->v_inode_nr = res.inode_nr; root_node->v_mode = res.fmode; root_node->v_uid = res.uid; root_node->v_gid = res.gid; root_node->v_size = res.fsize; root_node->v_sdev = NO_DEV; root_node->v_fs_count = 1; root_node->v_ref_count = 1; /* Root node is indeed on the partition */ root_node->v_vmnt = new_vmp; root_node->v_dev = new_vmp->m_dev; if (con_reqs == 0) new_vmp->m_comm.c_max_reqs = 1; /* Default if FS doesn't tell us */ else new_vmp->m_comm.c_max_reqs = con_reqs; new_vmp->m_comm.c_cur_reqs = 0; if (mount_root) { /* Superblock and root node already read. * Nothing else can go wrong. Perform the mount. */ new_vmp->m_root_node = root_node; new_vmp->m_mounted_on = NULL; strlcpy(new_vmp->m_label, mount_label, LABEL_MAX); if (is_nonedev(dev)) alloc_nonedev(dev); update_bspec(dev, fs_e, 0 /* Don't send new driver endpoint */); ROOT_DEV = dev; ROOT_FS_E = fs_e; /* Replace all root and working directories */ for (i = 0, tfp = fproc; i < NR_PROCS; i++, tfp++) { if (tfp->fp_pid == PID_FREE) continue; #define MAKEROOT(what) { \ if (what) put_vnode(what); \ dup_vnode(root_node); \ what = root_node; \ } MAKEROOT(tfp->fp_rd); MAKEROOT(tfp->fp_wd); } unlock_vnode(root_node); unlock_vmnt(new_vmp); have_root++; /* We have a (new) root */ unlock_bsf(); return(OK); } /* File types may not conflict. */ if (!S_ISDIR(vp->v_mode) && S_ISDIR(root_node->v_mode)) r = EISDIR; /* If error, return the super block and both inodes; release the vmnt. */ if (r != OK) { unlock_vnode(vp); unlock_vnode(root_node); mark_vmnt_free(new_vmp); unlock_vmnt(new_vmp); put_vnode(vp); put_vnode(root_node); unlock_bsf(); return(r); } /* Nothing else can go wrong. Perform the mount. */ new_vmp->m_mounted_on = vp; new_vmp->m_root_node = root_node; strlcpy(new_vmp->m_label, mount_label, LABEL_MAX); /* Allocate the pseudo device that was found, if not using a real device. */ if (is_nonedev(dev)) alloc_nonedev(dev); /* The new FS will handle block I/O requests for its device now. */ if (!(new_vmp->m_flags & VMNT_FORCEROOTBSF)) update_bspec(dev, fs_e, 0 /* Don't send new driver endpoint */); unlock_vnode(vp); unlock_vnode(root_node); unlock_vmnt(new_vmp); unlock_bsf(); return(OK); }
/*===========================================================================* * mount * *===========================================================================*/ PRIVATE int mount_fs(endpoint_t fs_e) { /* Perform the mount(name, mfile, rd_only) system call. */ int rdir, mdir; /* TRUE iff {root|mount} file is dir */ int i, r, found, isroot, replace_root; struct fproc *tfp; struct dmap *dp; dev_t dev; message m; struct vnode *root_node, *mounted_on = NULL, *bspec; struct vmnt *vmp; char *label; struct node_details res; SANITYCHECK; /* Only the super-user may do MOUNT. */ if (!super_user) return(EPERM); /* If FS not yet logged in, save message and suspend mount */ if (last_login_fs_e != fs_e) { mount_m_in = m_in; return SUSPEND; } /* Mount request got after FS login or * FS login arrived after a suspended mount */ last_login_fs_e = NONE; /* Clear endpoint field */ mount_m_in.m1_p3 = (char *) NONE; /* If 'name' is not for a block special file, return error. */ if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code); /* Convert name to device number */ if ((dev = name_to_dev()) == NO_DEV) return(err_code); /* Check whether there is a block special file open which uses the * same device (partition) */ for (bspec = &vnode[0]; bspec < &vnode[NR_VNODES]; ++bspec) { if (bspec->v_ref_count > 0 && bspec->v_sdev == dev) { /* Found, sync the buffer cache */ req_sync(bspec->v_fs_e); break; /* Note: there are probably some blocks in the FS process' buffer * cache which contain data on this minor, although they will be * purged since the handling moves to the new FS process (if * everything goes well with the mount...) */ } } /* Didn't find? */ if (bspec == &vnode[NR_VNODES] && bspec->v_sdev != dev) bspec = NULL; /* Scan vmnt table to see if dev already mounted, if not, * find a free slot.*/ found = FALSE; vmp = NIL_VMNT; for (i = 0; i < NR_MNTS; ++i) { if (vmnt[i].m_dev == dev) { vmp = &vmnt[i]; found = TRUE; break; } else if (!vmp && vmnt[i].m_dev == NO_DEV) { vmp = &vmnt[i]; } } /* Partition was/is already mounted */ if (found) { /* It is possible that we have an old root lying around that * needs to be remounted. */ if (vmp->m_mounted_on || vmp->m_mounted_on == fproc[FS_PROC_NR].fp_rd) { /* Normally, m_mounted_on refers to the mount point. For a * root filesystem, m_mounted_on is equal to the root vnode. * We assume that the root of FS is always the real root. If * the two vnodes are different or if the root of FS is equal * to the root of the filesystem we found, we found a * filesystem that is in use. */ return EBUSY; /* already mounted */ } if(vmp->m_mounted_on) panic("vfs", "root unexpectedly mounted somewhere", NO_NUM); if (root_dev == vmp->m_dev) panic("fs", "inconsistency remounting old root", NO_NUM); /* Now get the inode of the file to be mounted on. */ if (fetch_name(m_in.name2, m_in.name2_length, M1) != OK) { return(err_code); } /* Request lookup */ r = lookup_vp(0 /*flags*/, 0 /*!use_realuid*/, &mounted_on); if (r != OK) return r; if (mounted_on->v_ref_count != 1) { put_vnode(mounted_on); printf("vfs:mount_fs: mount point is busy\n"); return EBUSY; } /* Issue mountpoint request */ r = req_mountpoint(mounted_on->v_fs_e, mounted_on->v_inode_nr); if (r != OK) { put_vnode(mounted_on); printf("vfs:mount_fs: req_mountpoint_s failed with %d\n", r); return r; } /* Get the root inode of the mounted file system. */ root_node = vmp->m_root_node; /* File types may not conflict. */ if (r == OK) { mdir = ((mounted_on->v_mode & I_TYPE) == I_DIRECTORY); /* TRUE iff dir */ rdir = ((root_node->v_mode & I_TYPE) == I_DIRECTORY); if (!mdir && rdir) r = EISDIR; } /* If error, return the mount point. */ if (r != OK) { put_vnode(mounted_on); return(r); } /* Nothing else can go wrong. Perform the mount. */ vmp->m_mounted_on = mounted_on; vmp->m_flags = m_in.rd_only; allow_newroot = 0; /* The root is now fixed */ return(OK); } /* Fetch the name of the mountpoint */ if (fetch_name(m_in.name2, m_in.name2_length, M1) != OK) { return(err_code); } isroot= (strcmp(user_fullpath, "/") == 0); replace_root= (isroot && allow_newroot); if (!replace_root) { /* Get mount point and inform the FS it is on. */ #if 0 printf("vfs:mount_fs: mount point at '%s'\n", user_fullpath); #endif r = lookup_vp(0 /*flags*/, 0 /*!use_realuid*/, &mounted_on); if (r != OK) return r; /* Issue mountpoint request */ r = req_mountpoint(mounted_on->v_fs_e, mounted_on->v_inode_nr); if (r != OK) { put_vnode(mounted_on); printf("vfs:mount_fs: req_mountpoint_s failed with %d\n", r); return r; } } /* We'll need a vnode for the root inode, check whether there is one */ if ((root_node = get_free_vnode(__FILE__, __LINE__)) == NIL_VNODE) { printf("VFSmount: no free vnode available\n"); return ENFILE; } /* Get driver process' endpoint */ dp = &dmap[(dev >> MAJOR) & BYTE]; if (dp->dmap_driver == NONE) { printf("VFSmount: no driver for dev %x\n", dev); return(EINVAL); } label= dp->dmap_label; if (strlen(label) == 0) { panic(__FILE__, "vfs:mount_fs: no label for major", dev >> MAJOR); }