random discoveries
[ZeXOS.git] / kernel / apps / fs / mount.c
blob3a02de0c84009480f2e03329c27ac7988dd54374
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <mount.h>
23 #include <dev.h>
24 #include <vfs.h>
26 mount_t mount_list;
28 void mount_display ()
30 mount_t *mnt;
31 for (mnt = mount_list.next; mnt != &mount_list; mnt = mnt->next)
32 kprintf ("%s on %s\n", mnt->dev->devname, mnt->mountpoint);
35 dev_t *mount_find (char *mountpoint)
37 mount_t *mnt;
38 for (mnt = mount_list.next; mnt != &mount_list; mnt = mnt->next) {
39 if (!strcmp (mnt->mountpoint, mountpoint))
40 return mnt->dev;
43 return 0;
46 unsigned mount_list_add (dev_t *dev, char *mountpoint)
48 unsigned mp_len = strlen (mountpoint);
50 if (!mp_len) // HACK
51 return 2;
53 if (mount_find (mountpoint))
54 return 1;
56 if (vfs_list_findbymp (mountpoint))
57 return 3;
59 mount_t *mnt;
61 /* alloc and init context */
62 mnt = (mount_t *) kmalloc (sizeof (mount_t));
64 memcpy (mnt->mountpoint, mountpoint, mp_len);
65 mnt->mountpoint[mp_len] = '\0';
67 mnt->dev = dev;
69 /* add into list */
70 mnt->next = &mount_list;
71 mnt->prev = mount_list.prev;
72 mnt->prev->next = mnt;
73 mnt->next->prev = mnt;
75 return 0;
78 bool mount (dev_t *dev, char *dir, char *mountpoint)
80 if (dev->attrib & DEV_ATTR_BLOCK) {
81 unsigned err = mount_list_add (dev, mountpoint);
83 if (err == 1) {
84 kprintf ("ERROR -> device %s is already mounted to %s\n", dev->devname, mountpoint);
85 return 0;
87 if (err == 3) {
88 kprintf ("ERROR -> mountpoint %s does not exists\n", mountpoint);
89 return 0;
92 if (!dev->handler (DEV_ACT_MOUNT, mountpoint, dir, NULL)) {
93 kprintf ("ERROR -> mount failed: %s\n", dev->devname);
94 return 0;
97 return 1;
98 } else
99 kprintf ("ERROR -> device %s (%s) is'nt block type\n", dev->devname, dev->desc);
100 return 0;
103 bool umount (dev_t *dev, char *mountpoint)
105 bool f = 0;
106 mount_t *mnt;
107 for (mnt = mount_list.next; mnt != &mount_list; mnt = mnt->next) {
108 if (!strcmp (mnt->mountpoint, mountpoint) &&
109 mnt->dev == dev) {
110 f = 1;
111 break;
115 if (f) {
116 mnt->next->prev = mnt->prev;
117 mnt->prev->next = mnt->next;
119 //kfree (mnt);
120 return 1;
123 return 0;