vm: fix potential null deref
[minix.git] / servers / mfs / mount.c
blob689e3c87c035bdbf6a996cf29f3d912ff1641f8e
1 #include "fs.h"
2 #include "inode.h"
3 #include "super.h"
4 #include <minix/vfsif.h>
5 #include <minix/bdev.h>
7 static int cleanmount = 1;
9 /*===========================================================================*
10 * fs_readsuper *
11 *===========================================================================*/
12 int fs_readsuper()
14 /* This function reads the superblock of the partition, gets the root inode
15 * and sends back the details of them. Note, that the FS process does not
16 * know the index of the vmnt object which refers to it, whenever the pathname
17 * lookup leaves a partition an ELEAVEMOUNT error is transferred back
18 * so that the VFS knows that it has to find the vnode on which this FS
19 * process' partition is mounted on.
21 struct inode *root_ip;
22 cp_grant_id_t label_gid;
23 size_t label_len;
24 int r;
25 int readonly, isroot;
27 fs_dev = (dev_t) fs_m_in.REQ_DEV;
28 label_gid = (cp_grant_id_t) fs_m_in.REQ_GRANT;
29 label_len = (size_t) fs_m_in.REQ_PATH_LEN;
30 readonly = (fs_m_in.REQ_FLAGS & REQ_RDONLY) ? 1 : 0;
31 isroot = (fs_m_in.REQ_FLAGS & REQ_ISROOT) ? 1 : 0;
33 if (label_len > sizeof(fs_dev_label))
34 return(EINVAL);
36 r = sys_safecopyfrom(fs_m_in.m_source, label_gid, (vir_bytes) 0,
37 (vir_bytes) fs_dev_label, label_len);
38 if (r != OK) {
39 printf("MFS %s:%d safecopyfrom failed: %d\n", __FILE__, __LINE__, r);
40 return(EINVAL);
43 /* Map the driver label for this major. */
44 bdev_driver(fs_dev, fs_dev_label);
46 /* Open the device the file system lives on. */
47 if (bdev_open(fs_dev, readonly ? R_BIT : (R_BIT|W_BIT) ) != OK) {
48 return(EINVAL);
51 /* Fill in the super block. */
52 superblock.s_dev = fs_dev; /* read_super() needs to know which dev */
53 r = read_super(&superblock);
55 /* Is it recognized as a Minix filesystem? */
56 if (r != OK) {
57 superblock.s_dev = NO_DEV;
58 bdev_close(fs_dev);
59 return(r);
62 /* Remember whether we were mounted cleanly so we know what to
63 * do at unmount time
65 if(superblock.s_flags & MFSFLAG_CLEAN)
66 cleanmount = 1;
68 /* clean check: if rw and not clean, switch to readonly */
69 if(!(superblock.s_flags & MFSFLAG_CLEAN) && !readonly) {
70 if(bdev_close(fs_dev) != OK)
71 panic("couldn't bdev_close after found unclean FS");
72 readonly = 1;
74 if (bdev_open(fs_dev, R_BIT) != OK) {
75 panic("couldn't bdev_open after found unclean FS");
76 return(EINVAL);
78 printf("MFS: WARNING: FS 0x%x unclean, mounting readonly\n", fs_dev);
81 lmfs_set_blocksize(superblock.s_block_size, major(fs_dev));
83 /* Get the root inode of the mounted file system. */
84 if( (root_ip = get_inode(fs_dev, ROOT_INODE)) == NULL) {
85 printf("MFS: couldn't get root inode\n");
86 superblock.s_dev = NO_DEV;
87 bdev_close(fs_dev);
88 return(EINVAL);
91 if(root_ip->i_mode == 0) {
92 printf("%s:%d zero mode for root inode?\n", __FILE__, __LINE__);
93 put_inode(root_ip);
94 superblock.s_dev = NO_DEV;
95 bdev_close(fs_dev);
96 return(EINVAL);
99 superblock.s_rd_only = readonly;
100 superblock.s_is_root = isroot;
102 /* Root inode properties */
103 fs_m_out.RES_INODE_NR = root_ip->i_num;
104 fs_m_out.RES_MODE = root_ip->i_mode;
105 fs_m_out.RES_FILE_SIZE_LO = root_ip->i_size;
106 fs_m_out.RES_UID = root_ip->i_uid;
107 fs_m_out.RES_GID = root_ip->i_gid;
109 fs_m_out.RES_CONREQS = 1; /* We can handle only 1 request at a time */
111 /* Mark it dirty */
112 if(!superblock.s_rd_only) {
113 superblock.s_flags &= ~MFSFLAG_CLEAN;
114 if(write_super(&superblock) != OK)
115 panic("mounting: couldn't write dirty superblock");
118 return(r);
122 /*===========================================================================*
123 * fs_mountpoint *
124 *===========================================================================*/
125 int fs_mountpoint()
127 /* This function looks up the mount point, it checks the condition whether
128 * the partition can be mounted on the inode or not.
130 register struct inode *rip;
131 int r = OK;
132 mode_t bits;
134 /* Temporarily open the file. */
135 if( (rip = get_inode(fs_dev, (ino_t) fs_m_in.REQ_INODE_NR)) == NULL)
136 return(EINVAL);
139 if(rip->i_mountpoint) r = EBUSY;
141 /* It may not be special. */
142 bits = rip->i_mode & I_TYPE;
143 if (bits == I_BLOCK_SPECIAL || bits == I_CHAR_SPECIAL) r = ENOTDIR;
145 put_inode(rip);
147 if(r == OK) rip->i_mountpoint = TRUE;
149 return(r);
153 /*===========================================================================*
154 * fs_unmount *
155 *===========================================================================*/
156 int fs_unmount()
158 /* Unmount a file system by device number. */
159 int count;
160 struct inode *rip, *root_ip;
162 if(superblock.s_dev != fs_dev) return(EINVAL);
164 /* See if the mounted device is busy. Only 1 inode using it should be
165 * open --the root inode-- and that inode only 1 time. */
166 count = 0;
167 for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
168 if (rip->i_count > 0 && rip->i_dev == fs_dev) count += rip->i_count;
170 if ((root_ip = find_inode(fs_dev, ROOT_INODE)) == NULL) {
171 panic("MFS: couldn't find root inode\n");
172 return(EINVAL);
175 if (count > 1) return(EBUSY); /* can't umount a busy file system */
176 put_inode(root_ip);
178 /* force any cached blocks out of memory */
179 (void) fs_sync();
181 /* Mark it clean if we're allowed to write _and_ it was clean originally. */
182 if(cleanmount && !superblock.s_rd_only) {
183 superblock.s_flags |= MFSFLAG_CLEAN;
184 write_super(&superblock);
187 /* Close the device the file system lives on. */
188 bdev_close(fs_dev);
190 /* Finish off the unmount. */
191 superblock.s_dev = NO_DEV;
192 unmountdone = TRUE;
194 return(OK);