xref: /minix3/minix/fs/mfs/mount.c (revision 0314acfb2d68447dfa1b0b33aa4c25b1cbfa85d3)
1433d6423SLionel Sambuc #include "fs.h"
2433d6423SLionel Sambuc #include "inode.h"
3433d6423SLionel Sambuc #include "super.h"
4433d6423SLionel Sambuc #include <minix/vfsif.h>
5433d6423SLionel Sambuc #include <minix/bdev.h>
6433d6423SLionel Sambuc 
7433d6423SLionel Sambuc /*===========================================================================*
8ccaeedb2SDavid van Moolenbroek  *				fs_mount				     *
9433d6423SLionel Sambuc  *===========================================================================*/
10ccaeedb2SDavid van Moolenbroek int fs_mount(dev_t dev, unsigned int flags, struct fsdriver_node *root_node,
11ccaeedb2SDavid van Moolenbroek 	unsigned int *res_flags)
12433d6423SLionel Sambuc {
13433d6423SLionel Sambuc /* This function reads the superblock of the partition, gets the root inode
14ccaeedb2SDavid van Moolenbroek  * and sends back the details of them.
15433d6423SLionel Sambuc  */
16433d6423SLionel Sambuc   struct inode *root_ip;
17ccaeedb2SDavid van Moolenbroek   int r, readonly;
18433d6423SLionel Sambuc 
19ccaeedb2SDavid van Moolenbroek   fs_dev = dev;
20ccaeedb2SDavid van Moolenbroek   readonly = (flags & REQ_RDONLY) ? 1 : 0;
21433d6423SLionel Sambuc 
22433d6423SLionel Sambuc   /* Open the device the file system lives on. */
23433d6423SLionel Sambuc   if (bdev_open(fs_dev, readonly ? BDEV_R_BIT : (BDEV_R_BIT|BDEV_W_BIT) ) !=
24433d6423SLionel Sambuc 		OK) {
25433d6423SLionel Sambuc         return(EINVAL);
26433d6423SLionel Sambuc   }
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc   /* Fill in the super block. */
29433d6423SLionel Sambuc   superblock.s_dev = fs_dev;	/* read_super() needs to know which dev */
30433d6423SLionel Sambuc   r = read_super(&superblock);
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc   /* Is it recognized as a Minix filesystem? */
33433d6423SLionel Sambuc   if (r != OK) {
34433d6423SLionel Sambuc 	superblock.s_dev = NO_DEV;
35433d6423SLionel Sambuc 	bdev_close(fs_dev);
36433d6423SLionel Sambuc 	return(r);
37433d6423SLionel Sambuc   }
38433d6423SLionel Sambuc 
39433d6423SLionel Sambuc   /* clean check: if rw and not clean, switch to readonly */
40433d6423SLionel Sambuc   if(!(superblock.s_flags & MFSFLAG_CLEAN) && !readonly) {
41433d6423SLionel Sambuc 	if(bdev_close(fs_dev) != OK)
42433d6423SLionel Sambuc 		panic("couldn't bdev_close after found unclean FS");
43433d6423SLionel Sambuc 	readonly = 1;
44433d6423SLionel Sambuc 
45433d6423SLionel Sambuc 	if (bdev_open(fs_dev, BDEV_R_BIT) != OK) {
46433d6423SLionel Sambuc 		panic("couldn't bdev_open after found unclean FS");
47433d6423SLionel Sambuc 		return(EINVAL);
48433d6423SLionel Sambuc   	}
49433d6423SLionel Sambuc 	printf("MFS: WARNING: FS 0x%llx unclean, mounting readonly\n", fs_dev);
50433d6423SLionel Sambuc   }
51433d6423SLionel Sambuc 
52*0314acfbSDavid van Moolenbroek   lmfs_set_blocksize(superblock.s_block_size);
53433d6423SLionel Sambuc 
54433d6423SLionel Sambuc   /* Get the root inode of the mounted file system. */
55433d6423SLionel Sambuc   if( (root_ip = get_inode(fs_dev, ROOT_INODE)) == NULL)  {
56433d6423SLionel Sambuc 	printf("MFS: couldn't get root inode\n");
57433d6423SLionel Sambuc 	superblock.s_dev = NO_DEV;
58433d6423SLionel Sambuc 	bdev_close(fs_dev);
59433d6423SLionel Sambuc 	return(EINVAL);
60433d6423SLionel Sambuc   }
61433d6423SLionel Sambuc 
62433d6423SLionel Sambuc   if(root_ip->i_mode == 0) {
63433d6423SLionel Sambuc 	printf("%s:%d zero mode for root inode?\n", __FILE__, __LINE__);
64433d6423SLionel Sambuc 	put_inode(root_ip);
65433d6423SLionel Sambuc 	superblock.s_dev = NO_DEV;
66433d6423SLionel Sambuc 	bdev_close(fs_dev);
67433d6423SLionel Sambuc 	return(EINVAL);
68433d6423SLionel Sambuc   }
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc   superblock.s_rd_only = readonly;
71433d6423SLionel Sambuc 
72433d6423SLionel Sambuc   /* Root inode properties */
73ccaeedb2SDavid van Moolenbroek   root_node->fn_ino_nr = root_ip->i_num;
74ccaeedb2SDavid van Moolenbroek   root_node->fn_mode = root_ip->i_mode;
75ccaeedb2SDavid van Moolenbroek   root_node->fn_size = root_ip->i_size;
76ccaeedb2SDavid van Moolenbroek   root_node->fn_uid = root_ip->i_uid;
77ccaeedb2SDavid van Moolenbroek   root_node->fn_gid = root_ip->i_gid;
78ccaeedb2SDavid van Moolenbroek   root_node->fn_dev = NO_DEV;
79ccaeedb2SDavid van Moolenbroek 
80ccaeedb2SDavid van Moolenbroek   *res_flags = RES_NOFLAGS;
81433d6423SLionel Sambuc 
82433d6423SLionel Sambuc   /* Mark it dirty */
83433d6423SLionel Sambuc   if(!superblock.s_rd_only) {
84433d6423SLionel Sambuc 	  superblock.s_flags &= ~MFSFLAG_CLEAN;
85433d6423SLionel Sambuc 	  if(write_super(&superblock) != OK)
86433d6423SLionel Sambuc 		panic("mounting: couldn't write dirty superblock");
87433d6423SLionel Sambuc   }
88433d6423SLionel Sambuc 
89433d6423SLionel Sambuc   return(r);
90433d6423SLionel Sambuc }
91433d6423SLionel Sambuc 
92433d6423SLionel Sambuc 
93433d6423SLionel Sambuc /*===========================================================================*
94ccaeedb2SDavid van Moolenbroek  *				fs_mountpt				     *
95433d6423SLionel Sambuc  *===========================================================================*/
96ccaeedb2SDavid van Moolenbroek int fs_mountpt(ino_t ino_nr)
97433d6423SLionel Sambuc {
98433d6423SLionel Sambuc /* This function looks up the mount point, it checks the condition whether
99433d6423SLionel Sambuc  * the partition can be mounted on the inode or not.
100433d6423SLionel Sambuc  */
101433d6423SLionel Sambuc   register struct inode *rip;
102433d6423SLionel Sambuc   int r = OK;
103433d6423SLionel Sambuc   mode_t bits;
104433d6423SLionel Sambuc 
105433d6423SLionel Sambuc   /* Temporarily open the file. */
106ccaeedb2SDavid van Moolenbroek   if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
107433d6423SLionel Sambuc 	  return(EINVAL);
108433d6423SLionel Sambuc 
109433d6423SLionel Sambuc   if(rip->i_mountpoint) r = EBUSY;
110433d6423SLionel Sambuc 
111433d6423SLionel Sambuc   /* It may not be special. */
112433d6423SLionel Sambuc   bits = rip->i_mode & I_TYPE;
113433d6423SLionel Sambuc   if (bits == I_BLOCK_SPECIAL || bits == I_CHAR_SPECIAL) r = ENOTDIR;
114433d6423SLionel Sambuc 
115433d6423SLionel Sambuc   put_inode(rip);
116433d6423SLionel Sambuc 
117433d6423SLionel Sambuc   if(r == OK) rip->i_mountpoint = TRUE;
118433d6423SLionel Sambuc 
119433d6423SLionel Sambuc   return(r);
120433d6423SLionel Sambuc }
121433d6423SLionel Sambuc 
122433d6423SLionel Sambuc 
123433d6423SLionel Sambuc /*===========================================================================*
124433d6423SLionel Sambuc  *				fs_unmount				     *
125433d6423SLionel Sambuc  *===========================================================================*/
126ccaeedb2SDavid van Moolenbroek void fs_unmount(void)
127433d6423SLionel Sambuc {
128ccaeedb2SDavid van Moolenbroek /* Unmount a file system. */
129433d6423SLionel Sambuc   int count;
130433d6423SLionel Sambuc   struct inode *rip, *root_ip;
131433d6423SLionel Sambuc 
132433d6423SLionel Sambuc   /* See if the mounted device is busy.  Only 1 inode using it should be
133ccaeedb2SDavid van Moolenbroek    * open --the root inode-- and that inode only 1 time.  This is an integrity
134ccaeedb2SDavid van Moolenbroek    * check only: VFS expects the unmount to succeed either way.
135ccaeedb2SDavid van Moolenbroek    */
136433d6423SLionel Sambuc   count = 0;
137433d6423SLionel Sambuc   for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
138433d6423SLionel Sambuc 	  if (rip->i_count > 0 && rip->i_dev == fs_dev) count += rip->i_count;
139ccaeedb2SDavid van Moolenbroek   if (count != 1)
140ccaeedb2SDavid van Moolenbroek 	printf("MFS: file system has %d in-use inodes!\n", count);
141433d6423SLionel Sambuc 
142ccaeedb2SDavid van Moolenbroek   if ((root_ip = find_inode(fs_dev, ROOT_INODE)) == NULL)
143433d6423SLionel Sambuc 	panic("MFS: couldn't find root inode\n");
144433d6423SLionel Sambuc 
145433d6423SLionel Sambuc   put_inode(root_ip);
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc   /* force any cached blocks out of memory */
148ccaeedb2SDavid van Moolenbroek   fs_sync();
149433d6423SLionel Sambuc 
150433d6423SLionel Sambuc   /* Mark it clean if we're allowed to write _and_ it was clean originally. */
151ccaeedb2SDavid van Moolenbroek   if (!superblock.s_rd_only) {
152433d6423SLionel Sambuc 	superblock.s_flags |= MFSFLAG_CLEAN;
153433d6423SLionel Sambuc 	write_super(&superblock);
154433d6423SLionel Sambuc   }
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc   /* Close the device the file system lives on. */
157433d6423SLionel Sambuc   bdev_close(fs_dev);
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc   /* Throw out blocks out of the VM cache, to prevent corruption later. */
160433d6423SLionel Sambuc   lmfs_invalidate(fs_dev);
161433d6423SLionel Sambuc 
162433d6423SLionel Sambuc   /* Finish off the unmount. */
163433d6423SLionel Sambuc   superblock.s_dev = NO_DEV;
164433d6423SLionel Sambuc }
165433d6423SLionel Sambuc 
166