Rev 2681 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2681 | Rev 2687 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Jakub Jermar |
2 | * Copyright (c) 2007 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file vfs_mount.c |
34 | * @file vfs_mount.c |
35 | * @brief VFS_MOUNT method. |
35 | * @brief VFS_MOUNT method. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <ipc/ipc.h> |
38 | #include <ipc/ipc.h> |
39 | #include <async.h> |
39 | #include <async.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <stdlib.h> |
41 | #include <stdlib.h> |
42 | #include <string.h> |
42 | #include <string.h> |
43 | #include <bool.h> |
43 | #include <bool.h> |
44 | #include <futex.h> |
44 | #include <futex.h> |
45 | #include <libadt/list.h> |
45 | #include <libadt/list.h> |
46 | #include "vfs.h" |
46 | #include "vfs.h" |
47 | 47 | ||
48 | atomic_t rootfs_futex = FUTEX_INITIALIZER; |
48 | atomic_t rootfs_futex = FUTEX_INITIALIZER; |
49 | vfs_triplet_t rootfs = { |
49 | vfs_triplet_t rootfs = { |
50 | .fs_handle = 0, |
50 | .fs_handle = 0, |
51 | .dev_handle = 0, |
51 | .dev_handle = 0, |
52 | .index = 0, |
52 | .index = 0, |
53 | }; |
53 | }; |
54 | 54 | ||
55 | static int lookup_root(int fs_handle, int dev_handle, vfs_triplet_t *root) |
55 | static int lookup_root(int fs_handle, int dev_handle, vfs_triplet_t *root, |
- | 56 | size_t *size) |
|
56 | { |
57 | { |
57 | vfs_pair_t altroot = { |
58 | vfs_pair_t altroot = { |
58 | .fs_handle = fs_handle, |
59 | .fs_handle = fs_handle, |
59 | .dev_handle = dev_handle, |
60 | .dev_handle = dev_handle, |
60 | }; |
61 | }; |
61 | 62 | ||
62 | return vfs_lookup_internal("/", strlen("/"), root, &altroot); |
63 | return vfs_lookup_internal("/", strlen("/"), root, size, &altroot); |
63 | } |
64 | } |
64 | 65 | ||
65 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
66 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
66 | { |
67 | { |
67 | int dev_handle; |
68 | int dev_handle; |
68 | vfs_node_t *mp_node = NULL; |
69 | vfs_node_t *mp_node = NULL; |
69 | 70 | ||
70 | /* |
71 | /* |
71 | * We expect the library to do the device-name to device-handle |
72 | * We expect the library to do the device-name to device-handle |
72 | * translation for us, thus the device handle will arrive as ARG1 |
73 | * translation for us, thus the device handle will arrive as ARG1 |
73 | * in the request. |
74 | * in the request. |
74 | */ |
75 | */ |
75 | dev_handle = IPC_GET_ARG1(*request); |
76 | dev_handle = IPC_GET_ARG1(*request); |
76 | 77 | ||
77 | /* |
78 | /* |
78 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
79 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
79 | * carry mount options in the future. |
80 | * carry mount options in the future. |
80 | */ |
81 | */ |
81 | 82 | ||
82 | ipc_callid_t callid; |
83 | ipc_callid_t callid; |
83 | size_t size; |
84 | size_t size; |
84 | 85 | ||
85 | /* |
86 | /* |
86 | * Now, we expect the client to send us data with the name of the file |
87 | * Now, we expect the client to send us data with the name of the file |
87 | * system. |
88 | * system. |
88 | */ |
89 | */ |
89 | if (!ipc_data_write_receive(&callid, &size)) { |
90 | if (!ipc_data_write_receive(&callid, &size)) { |
90 | ipc_answer_0(callid, EINVAL); |
91 | ipc_answer_0(callid, EINVAL); |
91 | ipc_answer_0(rid, EINVAL); |
92 | ipc_answer_0(rid, EINVAL); |
92 | return; |
93 | return; |
93 | } |
94 | } |
94 | 95 | ||
95 | /* |
96 | /* |
96 | * Don't receive more than is necessary for storing a full file system |
97 | * Don't receive more than is necessary for storing a full file system |
97 | * name. |
98 | * name. |
98 | */ |
99 | */ |
99 | if (size < 1 || size > FS_NAME_MAXLEN) { |
100 | if (size < 1 || size > FS_NAME_MAXLEN) { |
100 | ipc_answer_0(callid, EINVAL); |
101 | ipc_answer_0(callid, EINVAL); |
101 | ipc_answer_0(rid, EINVAL); |
102 | ipc_answer_0(rid, EINVAL); |
102 | return; |
103 | return; |
103 | } |
104 | } |
104 | 105 | ||
105 | /* |
106 | /* |
106 | * Deliver the file system name. |
107 | * Deliver the file system name. |
107 | */ |
108 | */ |
108 | char fs_name[FS_NAME_MAXLEN + 1]; |
109 | char fs_name[FS_NAME_MAXLEN + 1]; |
109 | (void) ipc_data_write_finalize(callid, fs_name, size); |
110 | (void) ipc_data_write_finalize(callid, fs_name, size); |
110 | fs_name[size] = '\0'; |
111 | fs_name[size] = '\0'; |
111 | 112 | ||
112 | /* |
113 | /* |
113 | * Check if we know a file system with the same name as is in fs_name. |
114 | * Check if we know a file system with the same name as is in fs_name. |
114 | * This will also give us its file system handle. |
115 | * This will also give us its file system handle. |
115 | */ |
116 | */ |
116 | int fs_handle = fs_name_to_handle(fs_name, true); |
117 | int fs_handle = fs_name_to_handle(fs_name, true); |
117 | if (!fs_handle) { |
118 | if (!fs_handle) { |
118 | ipc_answer_0(rid, ENOENT); |
119 | ipc_answer_0(rid, ENOENT); |
119 | return; |
120 | return; |
120 | } |
121 | } |
121 | 122 | ||
122 | /* |
123 | /* |
123 | * Now, we want the client to send us the mount point. |
124 | * Now, we want the client to send us the mount point. |
124 | */ |
125 | */ |
125 | if (!ipc_data_write_receive(&callid, &size)) { |
126 | if (!ipc_data_write_receive(&callid, &size)) { |
126 | ipc_answer_0(callid, EINVAL); |
127 | ipc_answer_0(callid, EINVAL); |
127 | ipc_answer_0(rid, EINVAL); |
128 | ipc_answer_0(rid, EINVAL); |
128 | return; |
129 | return; |
129 | } |
130 | } |
130 | 131 | ||
131 | /* |
132 | /* |
132 | * Check whether size is reasonable wrt. the mount point. |
133 | * Check whether size is reasonable wrt. the mount point. |
133 | */ |
134 | */ |
134 | if (size < 1 || size > MAX_PATH_LEN) { |
135 | if (size < 1 || size > MAX_PATH_LEN) { |
135 | ipc_answer_0(callid, EINVAL); |
136 | ipc_answer_0(callid, EINVAL); |
136 | ipc_answer_0(rid, EINVAL); |
137 | ipc_answer_0(rid, EINVAL); |
137 | return; |
138 | return; |
138 | } |
139 | } |
139 | /* |
140 | /* |
140 | * Allocate buffer for the mount point data being received. |
141 | * Allocate buffer for the mount point data being received. |
141 | */ |
142 | */ |
142 | uint8_t *buf; |
143 | uint8_t *buf; |
143 | buf = malloc(size); |
144 | buf = malloc(size); |
144 | if (!buf) { |
145 | if (!buf) { |
145 | ipc_answer_0(callid, ENOMEM); |
146 | ipc_answer_0(callid, ENOMEM); |
146 | ipc_answer_0(rid, ENOMEM); |
147 | ipc_answer_0(rid, ENOMEM); |
147 | return; |
148 | return; |
148 | } |
149 | } |
149 | 150 | ||
150 | /* |
151 | /* |
151 | * Deliver the mount point. |
152 | * Deliver the mount point. |
152 | */ |
153 | */ |
153 | (void) ipc_data_write_finalize(callid, buf, size); |
154 | (void) ipc_data_write_finalize(callid, buf, size); |
154 | 155 | ||
155 | /* |
156 | /* |
156 | * Lookup the root node of the filesystem being mounted. |
157 | * Lookup the root node of the filesystem being mounted. |
157 | * In this case, we don't need to take the namespace_futex as the root |
158 | * In this case, we don't need to take the namespace_futex as the root |
158 | * node cannot be removed. However, we do take a reference to it so |
159 | * node cannot be removed. However, we do take a reference to it so |
159 | * that we can track how many times it has been mounted. |
160 | * that we can track how many times it has been mounted. |
160 | */ |
161 | */ |
161 | int rc; |
162 | int rc; |
162 | vfs_triplet_t mounted_root; |
163 | vfs_triplet_t mounted_root; |
- | 164 | size_t mrsz; |
|
163 | rc = lookup_root(fs_handle, dev_handle, &mounted_root); |
165 | rc = lookup_root(fs_handle, dev_handle, &mounted_root, &mrsz); |
164 | if (rc != EOK) { |
166 | if (rc != EOK) { |
165 | free(buf); |
167 | free(buf); |
166 | ipc_answer_0(rid, rc); |
168 | ipc_answer_0(rid, rc); |
167 | return; |
169 | return; |
168 | } |
170 | } |
169 | vfs_node_t *mr_node = vfs_node_get(&mounted_root); |
171 | vfs_node_t *mr_node = vfs_node_get(&mounted_root, mrsz); |
170 | if (!mr_node) { |
172 | if (!mr_node) { |
171 | free(buf); |
173 | free(buf); |
172 | ipc_answer_0(rid, ENOMEM); |
174 | ipc_answer_0(rid, ENOMEM); |
173 | return; |
175 | return; |
174 | } |
176 | } |
175 | 177 | ||
176 | /* |
178 | /* |
177 | * Finally, we need to resolve the path to the mountpoint. |
179 | * Finally, we need to resolve the path to the mountpoint. |
178 | */ |
180 | */ |
179 | vfs_triplet_t mp; |
181 | vfs_triplet_t mp; |
- | 182 | size_t mpsz; |
|
180 | futex_down(&rootfs_futex); |
183 | futex_down(&rootfs_futex); |
181 | if (rootfs.fs_handle) { |
184 | if (rootfs.fs_handle) { |
182 | /* |
185 | /* |
183 | * We already have the root FS. |
186 | * We already have the root FS. |
184 | */ |
187 | */ |
185 | rwlock_writer_lock(&namespace_rwlock); |
188 | rwlock_writer_lock(&namespace_rwlock); |
186 | rc = vfs_lookup_internal(buf, size, &mp, NULL); |
189 | rc = vfs_lookup_internal(buf, size, &mp, &mpsz, NULL); |
187 | if (rc != EOK) { |
190 | if (rc != EOK) { |
188 | /* |
191 | /* |
189 | * The lookup failed for some reason. |
192 | * The lookup failed for some reason. |
190 | */ |
193 | */ |
191 | rwlock_writer_unlock(&namespace_rwlock); |
194 | rwlock_writer_unlock(&namespace_rwlock); |
192 | futex_up(&rootfs_futex); |
195 | futex_up(&rootfs_futex); |
193 | vfs_node_put(mr_node); /* failed -> drop reference */ |
196 | vfs_node_put(mr_node); /* failed -> drop reference */ |
194 | free(buf); |
197 | free(buf); |
195 | ipc_answer_0(rid, rc); |
198 | ipc_answer_0(rid, rc); |
196 | return; |
199 | return; |
197 | } |
200 | } |
198 | mp_node = vfs_node_get(&mp); |
201 | mp_node = vfs_node_get(&mp, mpsz); |
199 | if (!mp_node) { |
202 | if (!mp_node) { |
200 | rwlock_writer_unlock(&namespace_rwlock); |
203 | rwlock_writer_unlock(&namespace_rwlock); |
201 | futex_up(&rootfs_futex); |
204 | futex_up(&rootfs_futex); |
202 | vfs_node_put(mr_node); /* failed -> drop reference */ |
205 | vfs_node_put(mr_node); /* failed -> drop reference */ |
203 | free(buf); |
206 | free(buf); |
204 | ipc_answer_0(rid, ENOMEM); |
207 | ipc_answer_0(rid, ENOMEM); |
205 | return; |
208 | return; |
206 | } |
209 | } |
207 | /* |
210 | /* |
208 | * Now we hold a reference to mp_node. |
211 | * Now we hold a reference to mp_node. |
209 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
212 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
210 | * This prevents the mount point from being deleted. |
213 | * This prevents the mount point from being deleted. |
211 | */ |
214 | */ |
212 | rwlock_writer_unlock(&namespace_rwlock); |
215 | rwlock_writer_unlock(&namespace_rwlock); |
213 | } else { |
216 | } else { |
214 | /* |
217 | /* |
215 | * We still don't have the root file system mounted. |
218 | * We still don't have the root file system mounted. |
216 | */ |
219 | */ |
217 | if ((size == 1) && (buf[0] == '/')) { |
220 | if ((size == 1) && (buf[0] == '/')) { |
218 | /* |
221 | /* |
219 | * For this simple, but important case, we are done. |
222 | * For this simple, but important case, we are done. |
220 | */ |
223 | */ |
221 | rootfs = mounted_root; |
224 | rootfs = mounted_root; |
222 | futex_up(&rootfs_futex); |
225 | futex_up(&rootfs_futex); |
223 | free(buf); |
226 | free(buf); |
224 | ipc_answer_0(rid, EOK); |
227 | ipc_answer_0(rid, EOK); |
225 | return; |
228 | return; |
226 | } else { |
229 | } else { |
227 | /* |
230 | /* |
228 | * We can't resolve this without the root filesystem |
231 | * We can't resolve this without the root filesystem |
229 | * being mounted first. |
232 | * being mounted first. |
230 | */ |
233 | */ |
231 | futex_up(&rootfs_futex); |
234 | futex_up(&rootfs_futex); |
232 | free(buf); |
235 | free(buf); |
233 | vfs_node_put(mr_node); /* failed -> drop reference */ |
236 | vfs_node_put(mr_node); /* failed -> drop reference */ |
234 | ipc_answer_0(rid, ENOENT); |
237 | ipc_answer_0(rid, ENOENT); |
235 | return; |
238 | return; |
236 | } |
239 | } |
237 | } |
240 | } |
238 | futex_up(&rootfs_futex); |
241 | futex_up(&rootfs_futex); |
239 | 242 | ||
240 | free(buf); /* The buffer is not needed anymore. */ |
243 | free(buf); /* The buffer is not needed anymore. */ |
241 | 244 | ||
242 | /* |
245 | /* |
243 | * At this point, we have all necessary pieces: file system and device |
246 | * At this point, we have all necessary pieces: file system and device |
244 | * handles, and we know the mount point VFS node and also the root node |
247 | * handles, and we know the mount point VFS node and also the root node |
245 | * of the file system being mounted. |
248 | * of the file system being mounted. |
246 | */ |
249 | */ |
247 | 250 | ||
248 | int phone = vfs_grab_phone(mp.fs_handle); |
251 | int phone = vfs_grab_phone(mp.fs_handle); |
249 | /* Later we can use ARG3 to pass mode/flags. */ |
252 | /* Later we can use ARG3 to pass mode/flags. */ |
250 | aid_t req1 = async_send_3(phone, VFS_MOUNT, (ipcarg_t) mp.dev_handle, |
253 | aid_t req1 = async_send_3(phone, VFS_MOUNT, (ipcarg_t) mp.dev_handle, |
251 | (ipcarg_t) mp.index, 0, NULL); |
254 | (ipcarg_t) mp.index, 0, NULL); |
252 | /* The second call uses the same method. */ |
255 | /* The second call uses the same method. */ |
253 | aid_t req2 = async_send_3(phone, VFS_MOUNT, |
256 | aid_t req2 = async_send_3(phone, VFS_MOUNT, |
254 | (ipcarg_t) mounted_root.fs_handle, |
257 | (ipcarg_t) mounted_root.fs_handle, |
255 | (ipcarg_t) mounted_root.dev_handle, (ipcarg_t) mounted_root.index, |
258 | (ipcarg_t) mounted_root.dev_handle, (ipcarg_t) mounted_root.index, |
256 | NULL); |
259 | NULL); |
257 | vfs_release_phone(phone); |
260 | vfs_release_phone(phone); |
258 | 261 | ||
259 | ipcarg_t rc1; |
262 | ipcarg_t rc1; |
260 | ipcarg_t rc2; |
263 | ipcarg_t rc2; |
261 | async_wait_for(req1, &rc1); |
264 | async_wait_for(req1, &rc1); |
262 | async_wait_for(req2, &rc2); |
265 | async_wait_for(req2, &rc2); |
263 | 266 | ||
264 | if ((rc1 != EOK) || (rc2 != EOK)) { |
267 | if ((rc1 != EOK) || (rc2 != EOK)) { |
265 | /* Mount failed, drop references to mr_node and mp_node. */ |
268 | /* Mount failed, drop references to mr_node and mp_node. */ |
266 | vfs_node_put(mr_node); |
269 | vfs_node_put(mr_node); |
267 | if (mp_node) |
270 | if (mp_node) |
268 | vfs_node_put(mp_node); |
271 | vfs_node_put(mp_node); |
269 | } |
272 | } |
270 | 273 | ||
271 | if (rc2 == EOK) |
274 | if (rc2 == EOK) |
272 | ipc_answer_0(rid, rc1); |
275 | ipc_answer_0(rid, rc1); |
273 | else if (rc1 == EOK) |
276 | else if (rc1 == EOK) |
274 | ipc_answer_0(rid, rc2); |
277 | ipc_answer_0(rid, rc2); |
275 | else |
278 | else |
276 | ipc_answer_0(rid, rc1); |
279 | ipc_answer_0(rid, rc1); |
277 | } |
280 | } |
278 | 281 | ||
279 | /** |
282 | /** |
280 | * @} |
283 | * @} |
281 | */ |
284 | */ |
282 | 285 |