Fixed extern declaration from pointer to array
[minix.git] / servers / mfs / mount.c
blob64e12900317d3e16d4a50e2929007a6f7bef199e
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 super_block *xp;
27 struct inode *root_ip;
28 cp_grant_id_t label_gid;
29 size_t label_len;
30 int r = OK;
31 unsigned long tasknr;
32 endpoint_t driver_e;
33 int readonly, isroot;
35 fs_dev = fs_m_in.REQ_DEV;
36 label_gid = fs_m_in.REQ_GRANT;
37 label_len = fs_m_in.REQ_PATH_LEN;
38 readonly = (fs_m_in.REQ_FLAGS & REQ_RDONLY) ? 1 : 0;
39 isroot = (fs_m_in.REQ_FLAGS & REQ_ISROOT) ? 1 : 0;
41 if (label_len > sizeof(fs_dev_label))
42 return(EINVAL);
44 r = sys_safecopyfrom(fs_m_in.m_source, label_gid, 0,
45 (vir_bytes)fs_dev_label, label_len, D);
46 if (r != OK) {
47 printf("%s:%d fs_readsuper: safecopyfrom failed: %d\n",
48 __FILE__, __LINE__, r);
49 return(EINVAL);
52 r= ds_retrieve_label_num(fs_dev_label, &tasknr);
53 if (r != OK)
55 printf("mfs:fs_readsuper: ds_retrieve_label_num failed for '%s': %d\n",
56 fs_dev_label, r);
57 return EINVAL;
60 driver_e = tasknr;
62 /* Map the driver endpoint for this major */
63 driver_endpoints[(fs_dev >> MAJOR) & BYTE].driver_e = driver_e;
64 use_getuptime2 = TRUE; /* Should be removed with old getuptime call. */
65 vfs_slink_storage = (char *)0xdeadbeef; /* Should be removed together
66 * with old lookup code.
67 */;
69 /* Open the device the file system lives on. */
70 if (dev_open(driver_e, fs_dev, driver_e,
71 readonly ? R_BIT : (R_BIT|W_BIT)) != OK) {
72 return(EINVAL);
75 /* Fill in the super block. */
76 superblock.s_dev = fs_dev; /* read_super() needs to know which dev */
77 r = read_super(&superblock);
79 /* Is it recognized as a Minix filesystem? */
80 if (r != OK) {
81 superblock.s_dev = NO_DEV;
82 dev_close(driver_e, fs_dev);
83 return(r);
86 set_blocksize(superblock.s_block_size);
88 /* Get the root inode of the mounted file system. */
89 if( (root_ip = get_inode(fs_dev, ROOT_INODE)) == NIL_INODE) {
90 printf("MFS: couldn't get root inode\n");
91 superblock.s_dev = NO_DEV;
92 dev_close(driver_e, fs_dev);
93 return(EINVAL);
96 if(root_ip != NIL_INODE && root_ip->i_mode == 0) {
97 printf("%s:%d zero mode for root inode?\n", __FILE__, __LINE__);
98 put_inode(root_ip);
99 superblock.s_dev = NO_DEV;
100 dev_close(driver_e, fs_dev);
101 return(EINVAL);
104 superblock.s_rd_only = readonly;
105 superblock.s_is_root = isroot;
107 /* Root inode properties */
108 fs_m_out.RES_INODE_NR = root_ip->i_num;
109 fs_m_out.RES_MODE = root_ip->i_mode;
110 fs_m_out.RES_FILE_SIZE_LO = root_ip->i_size;
111 fs_m_out.RES_UID = root_ip->i_uid;
112 fs_m_out.RES_GID = root_ip->i_gid;
114 return(r);
118 /*===========================================================================*
119 * fs_mountpoint *
120 *===========================================================================*/
121 PUBLIC int fs_mountpoint()
123 /* This function looks up the mount point, it checks the condition whether
124 * the partition can be mounted on the inode or not.
126 register struct inode *rip;
127 int r = OK;
128 mode_t bits;
130 /* Temporarily open the file. */
131 if( (rip = get_inode(fs_dev, fs_m_in.REQ_INODE_NR)) == NIL_INODE)
132 return(EINVAL);
135 if(rip->i_mountpoint) r = EBUSY;
137 /* It may not be special. */
138 bits = rip->i_mode & I_TYPE;
139 if (bits == I_BLOCK_SPECIAL || bits == I_CHAR_SPECIAL) r = ENOTDIR;
141 put_inode(rip);
143 if(r == OK) rip->i_mountpoint = TRUE;
145 return(r);
149 /*===========================================================================*
150 * fs_unmount *
151 *===========================================================================*/
152 PUBLIC int fs_unmount()
154 /* Unmount a file system by device number. */
155 struct super_block *sp1;
156 int count;
157 struct inode *rip, *root_ip;
159 if(superblock.s_dev != fs_dev) return(EINVAL);
161 /* See if the mounted device is busy. Only 1 inode using it should be
162 * open --the root inode-- and that inode only 1 time. */
163 count = 0;
164 for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
165 if (rip->i_count > 0 && rip->i_dev == fs_dev) count += rip->i_count;
167 if ((root_ip = find_inode(fs_dev, ROOT_INODE)) == NIL_INODE) {
168 printf("MFS: couldn't find root inode. Unmount failed.\n");
169 panic(__FILE__, "MFS: couldn't find root inode", EINVAL);
170 return(EINVAL);
173 if (count > 1) return(EBUSY); /* can't umount a busy file system */
174 put_inode(root_ip);
176 /* force any cached blocks out of memory */
177 (void) fs_sync();
179 /* Close the device the file system lives on. */
180 dev_close(driver_endpoints[(fs_dev >> MAJOR) & BYTE].driver_e, fs_dev);
182 /* Finish off the unmount. */
183 superblock.s_dev = NO_DEV;
184 unmountdone = TRUE;
186 return(OK);