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 *===========================================================================*/
fs_mount(dev_t dev,unsigned int flags,struct fsdriver_node * root_node,unsigned int * res_flags)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
520314acfbSDavid van Moolenbroek lmfs_set_blocksize(superblock.s_block_size);
53*1311233cSDavid van Moolenbroek
54*1311233cSDavid van Moolenbroek /* Compute the current number of used zones, and report it to libminixfs.
55*1311233cSDavid van Moolenbroek * Note that libminixfs really wants numbers of *blocks*, but this MFS
56*1311233cSDavid van Moolenbroek * implementation dropped support for differing zone/block sizes a while ago.
57*1311233cSDavid van Moolenbroek */
58*1311233cSDavid van Moolenbroek used_zones = superblock.s_zones - count_free_bits(&superblock, ZMAP);
59*1311233cSDavid van Moolenbroek
60*1311233cSDavid van Moolenbroek lmfs_set_blockusage(superblock.s_zones, used_zones);
61433d6423SLionel Sambuc
62433d6423SLionel Sambuc /* Get the root inode of the mounted file system. */
63433d6423SLionel Sambuc if( (root_ip = get_inode(fs_dev, ROOT_INODE)) == NULL) {
64433d6423SLionel Sambuc printf("MFS: couldn't get root inode\n");
65433d6423SLionel Sambuc superblock.s_dev = NO_DEV;
66433d6423SLionel Sambuc bdev_close(fs_dev);
67433d6423SLionel Sambuc return(EINVAL);
68433d6423SLionel Sambuc }
69433d6423SLionel Sambuc
70433d6423SLionel Sambuc if(root_ip->i_mode == 0) {
71433d6423SLionel Sambuc printf("%s:%d zero mode for root inode?\n", __FILE__, __LINE__);
72433d6423SLionel Sambuc put_inode(root_ip);
73433d6423SLionel Sambuc superblock.s_dev = NO_DEV;
74433d6423SLionel Sambuc bdev_close(fs_dev);
75433d6423SLionel Sambuc return(EINVAL);
76433d6423SLionel Sambuc }
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc superblock.s_rd_only = readonly;
79433d6423SLionel Sambuc
80433d6423SLionel Sambuc /* Root inode properties */
81ccaeedb2SDavid van Moolenbroek root_node->fn_ino_nr = root_ip->i_num;
82ccaeedb2SDavid van Moolenbroek root_node->fn_mode = root_ip->i_mode;
83ccaeedb2SDavid van Moolenbroek root_node->fn_size = root_ip->i_size;
84ccaeedb2SDavid van Moolenbroek root_node->fn_uid = root_ip->i_uid;
85ccaeedb2SDavid van Moolenbroek root_node->fn_gid = root_ip->i_gid;
86ccaeedb2SDavid van Moolenbroek root_node->fn_dev = NO_DEV;
87ccaeedb2SDavid van Moolenbroek
88ccaeedb2SDavid van Moolenbroek *res_flags = RES_NOFLAGS;
89433d6423SLionel Sambuc
90433d6423SLionel Sambuc /* Mark it dirty */
91433d6423SLionel Sambuc if(!superblock.s_rd_only) {
92433d6423SLionel Sambuc superblock.s_flags &= ~MFSFLAG_CLEAN;
93433d6423SLionel Sambuc if(write_super(&superblock) != OK)
94433d6423SLionel Sambuc panic("mounting: couldn't write dirty superblock");
95433d6423SLionel Sambuc }
96433d6423SLionel Sambuc
97433d6423SLionel Sambuc return(r);
98433d6423SLionel Sambuc }
99433d6423SLionel Sambuc
100433d6423SLionel Sambuc
101433d6423SLionel Sambuc /*===========================================================================*
102ccaeedb2SDavid van Moolenbroek * fs_mountpt *
103433d6423SLionel Sambuc *===========================================================================*/
fs_mountpt(ino_t ino_nr)104ccaeedb2SDavid van Moolenbroek int fs_mountpt(ino_t ino_nr)
105433d6423SLionel Sambuc {
106433d6423SLionel Sambuc /* This function looks up the mount point, it checks the condition whether
107433d6423SLionel Sambuc * the partition can be mounted on the inode or not.
108433d6423SLionel Sambuc */
109433d6423SLionel Sambuc register struct inode *rip;
110433d6423SLionel Sambuc int r = OK;
111433d6423SLionel Sambuc mode_t bits;
112433d6423SLionel Sambuc
113433d6423SLionel Sambuc /* Temporarily open the file. */
114ccaeedb2SDavid van Moolenbroek if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
115433d6423SLionel Sambuc return(EINVAL);
116433d6423SLionel Sambuc
117433d6423SLionel Sambuc if(rip->i_mountpoint) r = EBUSY;
118433d6423SLionel Sambuc
119433d6423SLionel Sambuc /* It may not be special. */
120433d6423SLionel Sambuc bits = rip->i_mode & I_TYPE;
121433d6423SLionel Sambuc if (bits == I_BLOCK_SPECIAL || bits == I_CHAR_SPECIAL) r = ENOTDIR;
122433d6423SLionel Sambuc
123433d6423SLionel Sambuc put_inode(rip);
124433d6423SLionel Sambuc
125433d6423SLionel Sambuc if(r == OK) rip->i_mountpoint = TRUE;
126433d6423SLionel Sambuc
127433d6423SLionel Sambuc return(r);
128433d6423SLionel Sambuc }
129433d6423SLionel Sambuc
130433d6423SLionel Sambuc
131433d6423SLionel Sambuc /*===========================================================================*
132433d6423SLionel Sambuc * fs_unmount *
133433d6423SLionel Sambuc *===========================================================================*/
fs_unmount(void)134ccaeedb2SDavid van Moolenbroek void fs_unmount(void)
135433d6423SLionel Sambuc {
136ccaeedb2SDavid van Moolenbroek /* Unmount a file system. */
137433d6423SLionel Sambuc int count;
138433d6423SLionel Sambuc struct inode *rip, *root_ip;
139433d6423SLionel Sambuc
140433d6423SLionel Sambuc /* See if the mounted device is busy. Only 1 inode using it should be
141ccaeedb2SDavid van Moolenbroek * open --the root inode-- and that inode only 1 time. This is an integrity
142ccaeedb2SDavid van Moolenbroek * check only: VFS expects the unmount to succeed either way.
143ccaeedb2SDavid van Moolenbroek */
144433d6423SLionel Sambuc count = 0;
145433d6423SLionel Sambuc for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
146433d6423SLionel Sambuc if (rip->i_count > 0 && rip->i_dev == fs_dev) count += rip->i_count;
147ccaeedb2SDavid van Moolenbroek if (count != 1)
148ccaeedb2SDavid van Moolenbroek printf("MFS: file system has %d in-use inodes!\n", count);
149433d6423SLionel Sambuc
150ccaeedb2SDavid van Moolenbroek if ((root_ip = find_inode(fs_dev, ROOT_INODE)) == NULL)
151433d6423SLionel Sambuc panic("MFS: couldn't find root inode\n");
152433d6423SLionel Sambuc
153433d6423SLionel Sambuc put_inode(root_ip);
154433d6423SLionel Sambuc
155433d6423SLionel Sambuc /* force any cached blocks out of memory */
156ccaeedb2SDavid van Moolenbroek fs_sync();
157433d6423SLionel Sambuc
158433d6423SLionel Sambuc /* Mark it clean if we're allowed to write _and_ it was clean originally. */
159ccaeedb2SDavid van Moolenbroek if (!superblock.s_rd_only) {
160433d6423SLionel Sambuc superblock.s_flags |= MFSFLAG_CLEAN;
161433d6423SLionel Sambuc write_super(&superblock);
162433d6423SLionel Sambuc }
163433d6423SLionel Sambuc
164433d6423SLionel Sambuc /* Close the device the file system lives on. */
165433d6423SLionel Sambuc bdev_close(fs_dev);
166433d6423SLionel Sambuc
167433d6423SLionel Sambuc /* Throw out blocks out of the VM cache, to prevent corruption later. */
168433d6423SLionel Sambuc lmfs_invalidate(fs_dev);
169433d6423SLionel Sambuc
170433d6423SLionel Sambuc /* Finish off the unmount. */
171433d6423SLionel Sambuc superblock.s_dev = NO_DEV;
172433d6423SLionel Sambuc }
173433d6423SLionel Sambuc
174