secondary cache feature in vm.
[minix.git] / servers / mfs / mount.c
blob8a84497cd32db14dba2ceabc099387dac3d53774
1 #include "fs.h"
2 #include <fcntl.h>
3 #include <string.h>
4 #include <minix/com.h>
5 #include <sys/stat.h>
6 #include "buf.h"
7 #include "inode.h"
8 #include "super.h"
9 #include "drivers.h"
10 #include <minix/ds.h>
11 #include <minix/vfsif.h>
14 /*===========================================================================*
15 * fs_readsuper *
16 *===========================================================================*/
17 PUBLIC int fs_readsuper()
19 /* This function reads the superblock of the partition, gets the root inode
20 * and sends back the details of them. Note, that the FS process does not
21 * know the index of the vmnt object which refers to it, whenever the pathname
22 * lookup leaves a partition an ELEAVEMOUNT error is transferred back
23 * so that the VFS knows that it has to find the vnode on which this FS
24 * process' partition is mounted on.
26 struct inode *root_ip;
27 cp_grant_id_t label_gid;
28 size_t label_len;
29 int r = OK;
30 endpoint_t driver_e;
31 int readonly, isroot;
33 fs_dev = fs_m_in.REQ_DEV;
34 label_gid = fs_m_in.REQ_GRANT;
35 label_len = fs_m_in.REQ_PATH_LEN;
36 readonly = (fs_m_in.REQ_FLAGS & REQ_RDONLY) ? 1 : 0;
37 isroot = (fs_m_in.REQ_FLAGS & REQ_ISROOT) ? 1 : 0;
39 if (label_len > sizeof(fs_dev_label))
40 return(EINVAL);
42 r = sys_safecopyfrom(fs_m_in.m_source, label_gid, 0,
43 (vir_bytes)fs_dev_label, label_len, D);
44 if (r != OK) {
45 printf("%s:%d fs_readsuper: safecopyfrom failed: %d\n",
46 __FILE__, __LINE__, r);
47 return(EINVAL);
50 r= ds_retrieve_label_endpt(fs_dev_label, &driver_e);
51 if (r != OK)
53 printf("mfs:fs_readsuper: ds_retrieve_label_endpt failed for '%s': %d\n",
54 fs_dev_label, r);
55 return EINVAL;
58 /* Map the driver endpoint for this major */
59 driver_endpoints[(fs_dev >> MAJOR) & BYTE].driver_e = driver_e;
60 use_getuptime2 = TRUE; /* Should be removed with old getuptime call. */
61 vfs_slink_storage = (char *)0xdeadbeef; /* Should be removed together
62 * with old lookup code.
63 */;
65 /* Open the device the file system lives on. */
66 if (dev_open(driver_e, fs_dev, driver_e,
67 readonly ? R_BIT : (R_BIT|W_BIT)) != OK) {
68 return(EINVAL);
71 /* Fill in the super block. */
72 superblock.s_dev = fs_dev; /* read_super() needs to know which dev */
73 r = read_super(&superblock);
75 /* Is it recognized as a Minix filesystem? */
76 if (r != OK) {
77 superblock.s_dev = NO_DEV;
78 dev_close(driver_e, fs_dev);
79 return(r);
82 set_blocksize(superblock.s_block_size);
84 /* Get the root inode of the mounted file system. */
85 if( (root_ip = get_inode(fs_dev, ROOT_INODE)) == NIL_INODE) {
86 printf("MFS: couldn't get root inode\n");
87 superblock.s_dev = NO_DEV;
88 dev_close(driver_e, fs_dev);
89 return(EINVAL);
92 if(root_ip != NIL_INODE && root_ip->i_mode == 0) {
93 printf("%s:%d zero mode for root inode?\n", __FILE__, __LINE__);
94 put_inode(root_ip);
95 superblock.s_dev = NO_DEV;
96 dev_close(driver_e, fs_dev);
97 return(EINVAL);
100 superblock.s_rd_only = readonly;
101 superblock.s_is_root = isroot;
103 /* Root inode properties */
104 fs_m_out.RES_INODE_NR = root_ip->i_num;
105 fs_m_out.RES_MODE = root_ip->i_mode;
106 fs_m_out.RES_FILE_SIZE_LO = root_ip->i_size;
107 fs_m_out.RES_UID = root_ip->i_uid;
108 fs_m_out.RES_GID = root_ip->i_gid;
110 return(r);
114 /*===========================================================================*
115 * fs_mountpoint *
116 *===========================================================================*/
117 PUBLIC int fs_mountpoint()
119 /* This function looks up the mount point, it checks the condition whether
120 * the partition can be mounted on the inode or not.
122 register struct inode *rip;
123 int r = OK;
124 mode_t bits;
126 /* Temporarily open the file. */
127 if( (rip = get_inode(fs_dev, fs_m_in.REQ_INODE_NR)) == NIL_INODE)
128 return(EINVAL);
131 if(rip->i_mountpoint) r = EBUSY;
133 /* It may not be special. */
134 bits = rip->i_mode & I_TYPE;
135 if (bits == I_BLOCK_SPECIAL || bits == I_CHAR_SPECIAL) r = ENOTDIR;
137 put_inode(rip);
139 if(r == OK) rip->i_mountpoint = TRUE;
141 return(r);
145 /*===========================================================================*
146 * fs_unmount *
147 *===========================================================================*/
148 PUBLIC int fs_unmount()
150 /* Unmount a file system by device number. */
151 int count;
152 struct inode *rip, *root_ip;
154 if(superblock.s_dev != fs_dev) return(EINVAL);
156 /* See if the mounted device is busy. Only 1 inode using it should be
157 * open --the root inode-- and that inode only 1 time. */
158 count = 0;
159 for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
160 if (rip->i_count > 0 && rip->i_dev == fs_dev) count += rip->i_count;
162 if ((root_ip = find_inode(fs_dev, ROOT_INODE)) == NIL_INODE) {
163 printf("MFS: couldn't find root inode. Unmount failed.\n");
164 panic("MFS: couldn't find root inode: %d", EINVAL);
165 return(EINVAL);
168 if (count > 1) return(EBUSY); /* can't umount a busy file system */
169 put_inode(root_ip);
171 /* force any cached blocks out of memory */
172 (void) fs_sync();
174 /* Close the device the file system lives on. */
175 dev_close(driver_endpoints[(fs_dev >> MAJOR) & BYTE].driver_e, fs_dev);
177 /* Finish off the unmount. */
178 superblock.s_dev = NO_DEV;
179 unmountdone = TRUE;
181 return(OK);