Rev 4419 | Rev 4537 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4419 | Rev 4420 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 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_ops.c |
34 | * @file vfs_ops.c |
35 | * @brief Operations that VFS offers to its clients. |
35 | * @brief Operations that VFS offers to its clients. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "vfs.h" |
38 | #include "vfs.h" |
39 | #include <ipc/ipc.h> |
39 | #include <ipc/ipc.h> |
40 | #include <async.h> |
40 | #include <async.h> |
41 | #include <errno.h> |
41 | #include <errno.h> |
42 | #include <stdio.h> |
42 | #include <stdio.h> |
43 | #include <stdlib.h> |
43 | #include <stdlib.h> |
44 | #include <string.h> |
44 | #include <string.h> |
45 | #include <bool.h> |
45 | #include <bool.h> |
46 | #include <futex.h> |
46 | #include <futex.h> |
47 | #include <rwlock.h> |
47 | #include <rwlock.h> |
48 | #include <libadt/list.h> |
48 | #include <libadt/list.h> |
49 | #include <unistd.h> |
49 | #include <unistd.h> |
50 | #include <ctype.h> |
50 | #include <ctype.h> |
51 | #include <fcntl.h> |
51 | #include <fcntl.h> |
52 | #include <assert.h> |
52 | #include <assert.h> |
53 | #include <vfs/canonify.h> |
53 | #include <vfs/canonify.h> |
54 | 54 | ||
55 | /* Forward declarations of static functions. */ |
55 | /* Forward declarations of static functions. */ |
56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
57 | 57 | ||
58 | /** Pending mount structure. */ |
58 | /** Pending mount structure. */ |
59 | typedef struct { |
59 | typedef struct { |
60 | link_t link; |
60 | link_t link; |
61 | char *fs_name; /**< File system name */ |
61 | char *fs_name; /**< File system name */ |
62 | char *mp; /**< Mount point */ |
62 | char *mp; /**< Mount point */ |
- | 63 | char *opts; /**< Mount options. */ |
|
63 | ipc_callid_t callid; /**< Call ID waiting for the mount */ |
64 | ipc_callid_t callid; /**< Call ID waiting for the mount */ |
64 | ipc_callid_t rid; /**< Request ID */ |
65 | ipc_callid_t rid; /**< Request ID */ |
65 | dev_handle_t dev_handle; /**< Device handle */ |
66 | dev_handle_t dev_handle; /**< Device handle */ |
66 | } pending_req_t; |
67 | } pending_req_t; |
67 | 68 | ||
68 | LIST_INITIALIZE(pending_req); |
69 | LIST_INITIALIZE(pending_req); |
69 | 70 | ||
70 | /** |
71 | /** |
71 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
72 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
72 | * concurrent VFS operation which modifies the file system namespace. |
73 | * concurrent VFS operation which modifies the file system namespace. |
73 | */ |
74 | */ |
74 | RWLOCK_INITIALIZE(namespace_rwlock); |
75 | RWLOCK_INITIALIZE(namespace_rwlock); |
75 | 76 | ||
76 | futex_t rootfs_futex = FUTEX_INITIALIZER; |
77 | futex_t rootfs_futex = FUTEX_INITIALIZER; |
77 | vfs_pair_t rootfs = { |
78 | vfs_pair_t rootfs = { |
78 | .fs_handle = 0, |
79 | .fs_handle = 0, |
79 | .dev_handle = 0 |
80 | .dev_handle = 0 |
80 | }; |
81 | }; |
81 | 82 | ||
82 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle, |
83 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle, |
83 | fs_handle_t fs_handle, char *mp) |
84 | fs_handle_t fs_handle, char *mp, char *opts) |
84 | { |
85 | { |
85 | /* Resolve the path to the mountpoint. */ |
- | |
86 | vfs_lookup_res_t mp_res; |
86 | vfs_lookup_res_t mp_res; |
- | 87 | vfs_lookup_res_t mr_res; |
|
87 | vfs_node_t *mp_node = NULL; |
88 | vfs_node_t *mp_node = NULL; |
- | 89 | vfs_node_t *mr_node; |
|
- | 90 | fs_index_t rindex; |
|
- | 91 | size_t rsize; |
|
- | 92 | unsigned rlnkcnt; |
|
88 | int rc; |
93 | ipcarg_t rc; |
89 | int phone; |
94 | int phone; |
- | 95 | aid_t msg; |
|
- | 96 | ipc_call_t answer; |
|
- | 97 | ||
- | 98 | ||
- | 99 | /* Resolve the path to the mountpoint. */ |
|
90 | futex_down(&rootfs_futex); |
100 | futex_down(&rootfs_futex); |
91 | if (rootfs.fs_handle) { |
101 | if (rootfs.fs_handle) { |
92 | /* We already have the root FS. */ |
102 | /* We already have the root FS. */ |
93 | rwlock_write_lock(&namespace_rwlock); |
103 | rwlock_write_lock(&namespace_rwlock); |
94 | if (str_cmp(mp, "/") == 0) { |
104 | if (str_cmp(mp, "/") == 0) { |
95 | /* Trying to mount root FS over root FS */ |
105 | /* Trying to mount root FS over root FS */ |
96 | rwlock_write_unlock(&namespace_rwlock); |
106 | rwlock_write_unlock(&namespace_rwlock); |
97 | futex_up(&rootfs_futex); |
107 | futex_up(&rootfs_futex); |
98 | ipc_answer_0(rid, EBUSY); |
108 | ipc_answer_0(rid, EBUSY); |
99 | return; |
109 | return; |
100 | } |
110 | } |
101 | 111 | ||
102 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
112 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
103 | if (rc != EOK) { |
113 | if (rc != EOK) { |
104 | /* The lookup failed for some reason. */ |
114 | /* The lookup failed for some reason. */ |
105 | rwlock_write_unlock(&namespace_rwlock); |
115 | rwlock_write_unlock(&namespace_rwlock); |
106 | futex_up(&rootfs_futex); |
116 | futex_up(&rootfs_futex); |
107 | ipc_answer_0(rid, rc); |
117 | ipc_answer_0(rid, rc); |
108 | return; |
118 | return; |
109 | } |
119 | } |
110 | 120 | ||
111 | mp_node = vfs_node_get(&mp_res); |
121 | mp_node = vfs_node_get(&mp_res); |
112 | if (!mp_node) { |
122 | if (!mp_node) { |
113 | rwlock_write_unlock(&namespace_rwlock); |
123 | rwlock_write_unlock(&namespace_rwlock); |
114 | futex_up(&rootfs_futex); |
124 | futex_up(&rootfs_futex); |
115 | ipc_answer_0(rid, ENOMEM); |
125 | ipc_answer_0(rid, ENOMEM); |
116 | return; |
126 | return; |
117 | } |
127 | } |
118 | 128 | ||
119 | /* |
129 | /* |
120 | * Now we hold a reference to mp_node. |
130 | * Now we hold a reference to mp_node. |
121 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
131 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
122 | * This prevents the mount point from being deleted. |
132 | * This prevents the mount point from being deleted. |
123 | */ |
133 | */ |
124 | rwlock_write_unlock(&namespace_rwlock); |
134 | rwlock_write_unlock(&namespace_rwlock); |
125 | } else { |
135 | } else { |
126 | /* We still don't have the root file system mounted. */ |
136 | /* We still don't have the root file system mounted. */ |
127 | if (str_cmp(mp, "/") == 0) { |
137 | if (str_cmp(mp, "/") == 0) { |
128 | vfs_lookup_res_t mr_res; |
- | |
129 | vfs_node_t *mr_node; |
- | |
130 | ipcarg_t rindex; |
- | |
131 | ipcarg_t rsize; |
- | |
132 | ipcarg_t rlnkcnt; |
- | |
133 | - | ||
134 | /* |
138 | /* |
135 | * For this simple, but important case, |
139 | * For this simple, but important case, |
136 | * we are almost done. |
140 | * we are almost done. |
137 | */ |
141 | */ |
138 | 142 | ||
139 | /* Tell the mountee that it is being mounted. */ |
143 | /* Tell the mountee that it is being mounted. */ |
140 | phone = vfs_grab_phone(fs_handle); |
144 | phone = vfs_grab_phone(fs_handle); |
141 | rc = async_req_1_3(phone, VFS_MOUNTED, |
145 | msg = async_send_1(phone, VFS_MOUNTED, |
142 | (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt); |
146 | (ipcarg_t) dev_handle, &answer); |
- | 147 | /* send the mount options */ |
|
- | 148 | rc = ipc_data_write_start(phone, (void *)opts, |
|
- | 149 | str_size(opts)); |
|
- | 150 | if (rc != EOK) { |
|
- | 151 | async_wait_for(msg, NULL); |
|
- | 152 | vfs_release_phone(phone); |
|
- | 153 | futex_up(&rootfs_futex); |
|
- | 154 | ipc_answer_0(rid, rc); |
|
- | 155 | return; |
|
- | 156 | } |
|
- | 157 | async_wait_for(msg, &rc); |
|
143 | vfs_release_phone(phone); |
158 | vfs_release_phone(phone); |
144 | 159 | ||
145 | if (rc != EOK) { |
160 | if (rc != EOK) { |
146 | futex_up(&rootfs_futex); |
161 | futex_up(&rootfs_futex); |
147 | ipc_answer_0(rid, rc); |
162 | ipc_answer_0(rid, rc); |
148 | return; |
163 | return; |
149 | } |
164 | } |
- | 165 | ||
- | 166 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
|
- | 167 | rsize = (size_t) IPC_GET_ARG2(answer); |
|
- | 168 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
|
150 | 169 | ||
151 | mr_res.triplet.fs_handle = fs_handle; |
170 | mr_res.triplet.fs_handle = fs_handle; |
152 | mr_res.triplet.dev_handle = dev_handle; |
171 | mr_res.triplet.dev_handle = dev_handle; |
153 | mr_res.triplet.index = (fs_index_t) rindex; |
172 | mr_res.triplet.index = rindex; |
154 | mr_res.size = (size_t) rsize; |
173 | mr_res.size = rsize; |
155 | mr_res.lnkcnt = (unsigned) rlnkcnt; |
174 | mr_res.lnkcnt = rlnkcnt; |
156 | mr_res.type = VFS_NODE_DIRECTORY; |
175 | mr_res.type = VFS_NODE_DIRECTORY; |
157 | 176 | ||
158 | rootfs.fs_handle = fs_handle; |
177 | rootfs.fs_handle = fs_handle; |
159 | rootfs.dev_handle = dev_handle; |
178 | rootfs.dev_handle = dev_handle; |
160 | futex_up(&rootfs_futex); |
179 | futex_up(&rootfs_futex); |
161 | 180 | ||
162 | /* Add reference to the mounted root. */ |
181 | /* Add reference to the mounted root. */ |
163 | mr_node = vfs_node_get(&mr_res); |
182 | mr_node = vfs_node_get(&mr_res); |
164 | assert(mr_node); |
183 | assert(mr_node); |
165 | 184 | ||
166 | ipc_answer_0(rid, rc); |
185 | ipc_answer_0(rid, rc); |
167 | return; |
186 | return; |
168 | } else { |
187 | } else { |
169 | /* |
188 | /* |
170 | * We can't resolve this without the root filesystem |
189 | * We can't resolve this without the root filesystem |
171 | * being mounted first. |
190 | * being mounted first. |
172 | */ |
191 | */ |
173 | futex_up(&rootfs_futex); |
192 | futex_up(&rootfs_futex); |
174 | ipc_answer_0(rid, ENOENT); |
193 | ipc_answer_0(rid, ENOENT); |
175 | return; |
194 | return; |
176 | } |
195 | } |
177 | } |
196 | } |
178 | futex_up(&rootfs_futex); |
197 | futex_up(&rootfs_futex); |
179 | 198 | ||
180 | /* |
199 | /* |
181 | * At this point, we have all necessary pieces: file system and device |
200 | * At this point, we have all necessary pieces: file system and device |
182 | * handles, and we know the mount point VFS node. |
201 | * handles, and we know the mount point VFS node. |
183 | */ |
202 | */ |
184 | 203 | ||
- | 204 | int mountee_phone = vfs_grab_phone(fs_handle); |
|
- | 205 | assert(mountee_phone >= 0); |
|
- | 206 | vfs_release_phone(mountee_phone); |
|
- | 207 | ||
185 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
208 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
186 | rc = async_req_4_0(phone, VFS_MOUNT, |
209 | msg = async_send_4(phone, VFS_MOUNT, |
187 | (ipcarg_t) mp_res.triplet.dev_handle, |
210 | (ipcarg_t) mp_res.triplet.dev_handle, |
188 | (ipcarg_t) mp_res.triplet.index, |
211 | (ipcarg_t) mp_res.triplet.index, |
189 | (ipcarg_t) fs_handle, |
212 | (ipcarg_t) fs_handle, |
190 | (ipcarg_t) dev_handle); |
213 | (ipcarg_t) dev_handle, &answer); |
- | 214 | ||
- | 215 | /* send connection */ |
|
- | 216 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
|
- | 217 | if (rc != EOK) { |
|
- | 218 | async_wait_for(msg, NULL); |
|
- | 219 | vfs_release_phone(phone); |
|
- | 220 | /* Mount failed, drop reference to mp_node. */ |
|
- | 221 | if (mp_node) |
|
- | 222 | vfs_node_put(mp_node); |
|
- | 223 | ipc_answer_0(rid, rc); |
|
- | 224 | return; |
|
- | 225 | } |
|
- | 226 | ||
- | 227 | /* send the mount options */ |
|
- | 228 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
|
- | 229 | if (rc != EOK) { |
|
- | 230 | async_wait_for(msg, NULL); |
|
- | 231 | vfs_release_phone(phone); |
|
- | 232 | /* Mount failed, drop reference to mp_node. */ |
|
- | 233 | if (mp_node) |
|
- | 234 | vfs_node_put(mp_node); |
|
- | 235 | ipc_answer_0(rid, rc); |
|
- | 236 | return; |
|
- | 237 | } |
|
- | 238 | async_wait_for(msg, &rc); |
|
191 | vfs_release_phone(phone); |
239 | vfs_release_phone(phone); |
192 | 240 | ||
193 | if (rc != EOK) { |
241 | if (rc != EOK) { |
194 | /* Mount failed, drop reference to mp_node. */ |
242 | /* Mount failed, drop reference to mp_node. */ |
195 | if (mp_node) |
243 | if (mp_node) |
196 | vfs_node_put(mp_node); |
244 | vfs_node_put(mp_node); |
197 | } |
245 | } |
198 | 246 | ||
- | 247 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
|
- | 248 | rsize = (size_t) IPC_GET_ARG2(answer); |
|
- | 249 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
|
- | 250 | ||
- | 251 | mr_res.triplet.fs_handle = fs_handle; |
|
- | 252 | mr_res.triplet.dev_handle = dev_handle; |
|
- | 253 | mr_res.triplet.index = rindex; |
|
- | 254 | mr_res.size = rsize; |
|
- | 255 | mr_res.lnkcnt = rlnkcnt; |
|
- | 256 | mr_res.type = VFS_NODE_DIRECTORY; |
|
- | 257 | ||
- | 258 | /* Add reference to the mounted root. */ |
|
- | 259 | mr_node = vfs_node_get(&mr_res); |
|
- | 260 | assert(mr_node); |
|
- | 261 | ||
199 | ipc_answer_0(rid, rc); |
262 | ipc_answer_0(rid, rc); |
200 | } |
263 | } |
201 | 264 | ||
202 | /** Process pending mount requests */ |
265 | /** Process pending mount requests */ |
203 | void vfs_process_pending_mount() |
266 | void vfs_process_pending_mount() |
204 | { |
267 | { |
205 | link_t *cur; |
268 | link_t *cur; |
206 | 269 | ||
207 | loop: |
270 | loop: |
208 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
271 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
209 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
272 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
210 | 273 | ||
211 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true); |
274 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true); |
212 | if (!fs_handle) |
275 | if (!fs_handle) |
213 | continue; |
276 | continue; |
214 | 277 | ||
215 | /* Acknowledge that we know fs_name. */ |
278 | /* Acknowledge that we know fs_name. */ |
216 | ipc_answer_0(pr->callid, EOK); |
279 | ipc_answer_0(pr->callid, EOK); |
217 | 280 | ||
218 | /* Do the mount */ |
281 | /* Do the mount */ |
219 | vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp); |
282 | vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp, |
- | 283 | pr->opts); |
|
220 | 284 | ||
221 | free(pr->fs_name); |
285 | free(pr->fs_name); |
222 | free(pr->mp); |
286 | free(pr->mp); |
- | 287 | free(pr->opts); |
|
223 | list_remove(cur); |
288 | list_remove(cur); |
224 | free(pr); |
289 | free(pr); |
225 | goto loop; |
290 | goto loop; |
226 | } |
291 | } |
227 | } |
292 | } |
228 | 293 | ||
229 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
294 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
230 | { |
295 | { |
231 | /* |
296 | /* |
232 | * We expect the library to do the device-name to device-handle |
297 | * We expect the library to do the device-name to device-handle |
233 | * translation for us, thus the device handle will arrive as ARG1 |
298 | * translation for us, thus the device handle will arrive as ARG1 |
234 | * in the request. |
299 | * in the request. |
235 | */ |
300 | */ |
236 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
301 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
237 | 302 | ||
238 | /* |
303 | /* |
239 | * Mount flags are passed as ARG2. |
304 | * Mount flags are passed as ARG2. |
240 | */ |
305 | */ |
241 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request); |
306 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request); |
242 | 307 | ||
243 | /* |
308 | /* |
244 | * For now, don't make use of ARG3, but it can be used to |
309 | * For now, don't make use of ARG3, but it can be used to |
245 | * carry mount options in the future. |
310 | * carry mount options in the future. |
246 | */ |
311 | */ |
247 | 312 | ||
248 | /* We want the client to send us the mount point. */ |
313 | /* We want the client to send us the mount point. */ |
249 | ipc_callid_t callid; |
314 | ipc_callid_t callid; |
250 | size_t size; |
315 | size_t size; |
251 | if (!ipc_data_write_receive(&callid, &size)) { |
316 | if (!ipc_data_write_receive(&callid, &size)) { |
252 | ipc_answer_0(callid, EINVAL); |
317 | ipc_answer_0(callid, EINVAL); |
253 | ipc_answer_0(rid, EINVAL); |
318 | ipc_answer_0(rid, EINVAL); |
254 | return; |
319 | return; |
255 | } |
320 | } |
256 | 321 | ||
257 | /* Check whether size is reasonable wrt. the mount point. */ |
322 | /* Check whether size is reasonable wrt. the mount point. */ |
258 | if ((size < 1) || (size > MAX_PATH_LEN)) { |
323 | if ((size < 1) || (size > MAX_PATH_LEN)) { |
259 | ipc_answer_0(callid, EINVAL); |
324 | ipc_answer_0(callid, EINVAL); |
260 | ipc_answer_0(rid, EINVAL); |
325 | ipc_answer_0(rid, EINVAL); |
261 | return; |
326 | return; |
262 | } |
327 | } |
263 | 328 | ||
264 | /* Allocate buffer for the mount point data being received. */ |
329 | /* Allocate buffer for the mount point data being received. */ |
265 | char *mp = malloc(size + 1); |
330 | char *mp = malloc(size + 1); |
266 | if (!mp) { |
331 | if (!mp) { |
267 | ipc_answer_0(callid, ENOMEM); |
332 | ipc_answer_0(callid, ENOMEM); |
268 | ipc_answer_0(rid, ENOMEM); |
333 | ipc_answer_0(rid, ENOMEM); |
269 | return; |
334 | return; |
270 | } |
335 | } |
271 | 336 | ||
272 | /* Deliver the mount point. */ |
337 | /* Deliver the mount point. */ |
273 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size); |
338 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size); |
274 | if (retval != EOK) { |
339 | if (retval != EOK) { |
275 | ipc_answer_0(rid, EREFUSED); |
340 | ipc_answer_0(rid, retval); |
276 | free(mp); |
341 | free(mp); |
277 | return; |
342 | return; |
278 | } |
343 | } |
279 | mp[size] = '\0'; |
344 | mp[size] = '\0'; |
280 | 345 | ||
- | 346 | /* Now we expect to receive the mount options. */ |
|
- | 347 | if (!ipc_data_write_receive(&callid, &size)) { |
|
- | 348 | ipc_answer_0(callid, EINVAL); |
|
- | 349 | ipc_answer_0(rid, EINVAL); |
|
- | 350 | free(mp); |
|
- | 351 | return; |
|
- | 352 | } |
|
- | 353 | ||
- | 354 | /* Check the offered options size. */ |
|
- | 355 | if (size < 0 || size > MAX_MNTOPTS_LEN) { |
|
- | 356 | ipc_answer_0(callid, EINVAL); |
|
- | 357 | ipc_answer_0(rid, EINVAL); |
|
- | 358 | free(mp); |
|
- | 359 | return; |
|
- | 360 | } |
|
- | 361 | ||
- | 362 | /* Allocate buffer for the mount options. */ |
|
- | 363 | char *opts = (char *) malloc(size + 1); |
|
- | 364 | if (!opts) { |
|
- | 365 | ipc_answer_0(callid, ENOMEM); |
|
- | 366 | ipc_answer_0(rid, ENOMEM); |
|
- | 367 | free(mp); |
|
- | 368 | return; |
|
- | 369 | } |
|
- | 370 | ||
- | 371 | /* Deliver the mount options. */ |
|
- | 372 | retval = ipc_data_write_finalize(callid, opts, size); |
|
- | 373 | if (retval != EOK) { |
|
- | 374 | ipc_answer_0(rid, retval); |
|
- | 375 | free(mp); |
|
- | 376 | free(opts); |
|
- | 377 | return; |
|
- | 378 | } |
|
- | 379 | opts[size] = '\0'; |
|
- | 380 | ||
281 | /* |
381 | /* |
282 | * Now, we expect the client to send us data with the name of the file |
382 | * Now, we expect the client to send us data with the name of the file |
283 | * system. |
383 | * system. |
284 | */ |
384 | */ |
285 | if (!ipc_data_write_receive(&callid, &size)) { |
385 | if (!ipc_data_write_receive(&callid, &size)) { |
286 | ipc_answer_0(callid, EINVAL); |
386 | ipc_answer_0(callid, EINVAL); |
287 | ipc_answer_0(rid, EINVAL); |
387 | ipc_answer_0(rid, EINVAL); |
288 | free(mp); |
388 | free(mp); |
- | 389 | free(opts); |
|
289 | return; |
390 | return; |
290 | } |
391 | } |
291 | 392 | ||
292 | /* |
393 | /* |
293 | * Don't receive more than is necessary for storing a full file system |
394 | * Don't receive more than is necessary for storing a full file system |
294 | * name. |
395 | * name. |
295 | */ |
396 | */ |
296 | if ((size < 1) || (size > FS_NAME_MAXLEN)) { |
397 | if ((size < 1) || (size > FS_NAME_MAXLEN)) { |
297 | ipc_answer_0(callid, EINVAL); |
398 | ipc_answer_0(callid, EINVAL); |
298 | ipc_answer_0(rid, EINVAL); |
399 | ipc_answer_0(rid, EINVAL); |
299 | free(mp); |
400 | free(mp); |
- | 401 | free(opts); |
|
300 | return; |
402 | return; |
301 | } |
403 | } |
302 | 404 | ||
303 | /* |
405 | /* |
304 | * Allocate buffer for file system name. |
406 | * Allocate buffer for file system name. |
305 | */ |
407 | */ |
306 | char *fs_name = (char *) malloc(size + 1); |
408 | char *fs_name = (char *) malloc(size + 1); |
307 | if (fs_name == NULL) { |
409 | if (fs_name == NULL) { |
308 | ipc_answer_0(callid, ENOMEM); |
410 | ipc_answer_0(callid, ENOMEM); |
309 | ipc_answer_0(rid, EREFUSED); |
411 | ipc_answer_0(rid, ENOMEM); |
310 | free(mp); |
412 | free(mp); |
- | 413 | free(opts); |
|
311 | return; |
414 | return; |
312 | } |
415 | } |
313 | 416 | ||
314 | /* Deliver the file system name. */ |
417 | /* Deliver the file system name. */ |
315 | retval = ipc_data_write_finalize(callid, fs_name, size); |
418 | retval = ipc_data_write_finalize(callid, fs_name, size); |
316 | if (retval != EOK) { |
419 | if (retval != EOK) { |
317 | ipc_answer_0(rid, EREFUSED); |
420 | ipc_answer_0(rid, retval); |
318 | free(mp); |
421 | free(mp); |
- | 422 | free(opts); |
|
319 | free(fs_name); |
423 | free(fs_name); |
320 | return; |
424 | return; |
321 | } |
425 | } |
322 | fs_name[size] = '\0'; |
426 | fs_name[size] = '\0'; |
- | 427 | ||
- | 428 | /* |
|
- | 429 | * Wait for IPC_M_PING so that we can return an error if we don't know |
|
- | 430 | * fs_name. |
|
- | 431 | */ |
|
- | 432 | ipc_call_t data; |
|
- | 433 | callid = async_get_call(&data); |
|
- | 434 | if (IPC_GET_METHOD(data) != IPC_M_PING) { |
|
- | 435 | ipc_answer_0(callid, ENOTSUP); |
|
- | 436 | ipc_answer_0(rid, ENOTSUP); |
|
- | 437 | free(mp); |
|
- | 438 | free(opts); |
|
- | 439 | free(fs_name); |
|
- | 440 | return; |
|
323 | 441 | } |
|
- | 442 | ||
324 | /* |
443 | /* |
325 | * Check if we know a file system with the same name as is in fs_name. |
444 | * Check if we know a file system with the same name as is in fs_name. |
326 | * This will also give us its file system handle. |
445 | * This will also give us its file system handle. |
327 | */ |
446 | */ |
328 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
447 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
329 | if (!fs_handle) { |
448 | if (!fs_handle) { |
330 | if (flags & IPC_FLAG_BLOCKING) { |
449 | if (flags & IPC_FLAG_BLOCKING) { |
- | 450 | pending_req_t *pr; |
|
- | 451 | ||
331 | /* Blocking mount, add to pending list */ |
452 | /* Blocking mount, add to pending list */ |
332 | pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
453 | pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
333 | if (!pr) { |
454 | if (!pr) { |
334 | ipc_answer_0(callid, ENOMEM); |
455 | ipc_answer_0(callid, ENOMEM); |
335 | ipc_answer_0(rid, ENOMEM); |
456 | ipc_answer_0(rid, ENOMEM); |
336 | free(mp); |
457 | free(mp); |
337 | free(fs_name); |
458 | free(fs_name); |
- | 459 | free(opts); |
|
338 | return; |
460 | return; |
339 | } |
461 | } |
340 | 462 | ||
341 | pr->fs_name = fs_name; |
463 | pr->fs_name = fs_name; |
342 | pr->mp = mp; |
464 | pr->mp = mp; |
- | 465 | pr->opts = opts; |
|
343 | pr->callid = callid; |
466 | pr->callid = callid; |
344 | pr->rid = rid; |
467 | pr->rid = rid; |
345 | pr->dev_handle = dev_handle; |
468 | pr->dev_handle = dev_handle; |
346 | link_initialize(&pr->link); |
469 | link_initialize(&pr->link); |
347 | list_append(&pr->link, &pending_req); |
470 | list_append(&pr->link, &pending_req); |
348 | return; |
471 | return; |
349 | } |
472 | } |
350 | 473 | ||
351 | ipc_answer_0(callid, ENOENT); |
474 | ipc_answer_0(callid, ENOENT); |
352 | ipc_answer_0(rid, ENOENT); |
475 | ipc_answer_0(rid, ENOENT); |
353 | free(mp); |
476 | free(mp); |
354 | free(fs_name); |
477 | free(fs_name); |
- | 478 | free(opts); |
|
355 | return; |
479 | return; |
356 | } |
480 | } |
357 | 481 | ||
358 | /* Acknowledge that we know fs_name. */ |
482 | /* Acknowledge that we know fs_name. */ |
359 | ipc_answer_0(callid, EOK); |
483 | ipc_answer_0(callid, EOK); |
360 | 484 | ||
361 | /* Do the mount */ |
485 | /* Do the mount */ |
362 | vfs_mount_internal(rid, dev_handle, fs_handle, mp); |
486 | vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts); |
363 | free(mp); |
487 | free(mp); |
364 | free(fs_name); |
488 | free(fs_name); |
- | 489 | free(opts); |
|
365 | } |
490 | } |
366 | 491 | ||
367 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
492 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
368 | { |
493 | { |
369 | if (!vfs_files_init()) { |
494 | if (!vfs_files_init()) { |
370 | ipc_answer_0(rid, ENOMEM); |
495 | ipc_answer_0(rid, ENOMEM); |
371 | return; |
496 | return; |
372 | } |
497 | } |
373 | 498 | ||
374 | /* |
499 | /* |
375 | * The POSIX interface is open(path, oflag, mode). |
500 | * The POSIX interface is open(path, oflag, mode). |
376 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
501 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
377 | * will need to arrive in another call. |
502 | * will need to arrive in another call. |
378 | * |
503 | * |
379 | * We also receive one private, non-POSIX set of flags called lflag |
504 | * We also receive one private, non-POSIX set of flags called lflag |
380 | * used to pass information to vfs_lookup_internal(). |
505 | * used to pass information to vfs_lookup_internal(). |
381 | */ |
506 | */ |
382 | int lflag = IPC_GET_ARG1(*request); |
507 | int lflag = IPC_GET_ARG1(*request); |
383 | int oflag = IPC_GET_ARG2(*request); |
508 | int oflag = IPC_GET_ARG2(*request); |
384 | int mode = IPC_GET_ARG3(*request); |
509 | int mode = IPC_GET_ARG3(*request); |
385 | size_t len; |
510 | size_t len; |
386 | 511 | ||
387 | /* |
512 | /* |
388 | * Make sure that we are called with exactly one of L_FILE and |
513 | * Make sure that we are called with exactly one of L_FILE and |
389 | * L_DIRECTORY. |
514 | * L_DIRECTORY. |
390 | */ |
515 | */ |
391 | if ((lflag & (L_FILE | L_DIRECTORY)) == 0 || |
516 | if ((lflag & (L_FILE | L_DIRECTORY)) == 0 || |
392 | (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) { |
517 | (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) { |
393 | ipc_answer_0(rid, EINVAL); |
518 | ipc_answer_0(rid, EINVAL); |
394 | return; |
519 | return; |
395 | } |
520 | } |
396 | 521 | ||
397 | if (oflag & O_CREAT) |
522 | if (oflag & O_CREAT) |
398 | lflag |= L_CREATE; |
523 | lflag |= L_CREATE; |
399 | if (oflag & O_EXCL) |
524 | if (oflag & O_EXCL) |
400 | lflag |= L_EXCLUSIVE; |
525 | lflag |= L_EXCLUSIVE; |
401 | 526 | ||
402 | ipc_callid_t callid; |
527 | ipc_callid_t callid; |
403 | 528 | ||
404 | if (!ipc_data_write_receive(&callid, &len)) { |
529 | if (!ipc_data_write_receive(&callid, &len)) { |
405 | ipc_answer_0(callid, EINVAL); |
530 | ipc_answer_0(callid, EINVAL); |
406 | ipc_answer_0(rid, EINVAL); |
531 | ipc_answer_0(rid, EINVAL); |
407 | return; |
532 | return; |
408 | } |
533 | } |
409 | char *path = malloc(len + 1); |
534 | char *path = malloc(len + 1); |
410 | if (!path) { |
535 | if (!path) { |
411 | ipc_answer_0(callid, ENOMEM); |
536 | ipc_answer_0(callid, ENOMEM); |
412 | ipc_answer_0(rid, ENOMEM); |
537 | ipc_answer_0(rid, ENOMEM); |
413 | return; |
538 | return; |
414 | } |
539 | } |
415 | int rc; |
540 | int rc; |
416 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
541 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
417 | ipc_answer_0(rid, rc); |
542 | ipc_answer_0(rid, rc); |
418 | free(path); |
543 | free(path); |
419 | return; |
544 | return; |
420 | } |
545 | } |
421 | path[len] = '\0'; |
546 | path[len] = '\0'; |
422 | 547 | ||
423 | /* |
548 | /* |
424 | * Avoid the race condition in which the file can be deleted before we |
549 | * Avoid the race condition in which the file can be deleted before we |
425 | * find/create-and-lock the VFS node corresponding to the looked-up |
550 | * find/create-and-lock the VFS node corresponding to the looked-up |
426 | * triplet. |
551 | * triplet. |
427 | */ |
552 | */ |
428 | if (lflag & L_CREATE) |
553 | if (lflag & L_CREATE) |
429 | rwlock_write_lock(&namespace_rwlock); |
554 | rwlock_write_lock(&namespace_rwlock); |
430 | else |
555 | else |
431 | rwlock_read_lock(&namespace_rwlock); |
556 | rwlock_read_lock(&namespace_rwlock); |
432 | 557 | ||
433 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
558 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
434 | vfs_lookup_res_t lr; |
559 | vfs_lookup_res_t lr; |
435 | rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
560 | rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
436 | if (rc) { |
561 | if (rc) { |
437 | if (lflag & L_CREATE) |
562 | if (lflag & L_CREATE) |
438 | rwlock_write_unlock(&namespace_rwlock); |
563 | rwlock_write_unlock(&namespace_rwlock); |
439 | else |
564 | else |
440 | rwlock_read_unlock(&namespace_rwlock); |
565 | rwlock_read_unlock(&namespace_rwlock); |
441 | ipc_answer_0(rid, rc); |
566 | ipc_answer_0(rid, rc); |
442 | free(path); |
567 | free(path); |
443 | return; |
568 | return; |
444 | } |
569 | } |
445 | 570 | ||
446 | /* Path is no longer needed. */ |
571 | /* Path is no longer needed. */ |
447 | free(path); |
572 | free(path); |
448 | 573 | ||
449 | vfs_node_t *node = vfs_node_get(&lr); |
574 | vfs_node_t *node = vfs_node_get(&lr); |
450 | if (lflag & L_CREATE) |
575 | if (lflag & L_CREATE) |
451 | rwlock_write_unlock(&namespace_rwlock); |
576 | rwlock_write_unlock(&namespace_rwlock); |
452 | else |
577 | else |
453 | rwlock_read_unlock(&namespace_rwlock); |
578 | rwlock_read_unlock(&namespace_rwlock); |
454 | 579 | ||
455 | /* Truncate the file if requested and if necessary. */ |
580 | /* Truncate the file if requested and if necessary. */ |
456 | if (oflag & O_TRUNC) { |
581 | if (oflag & O_TRUNC) { |
457 | rwlock_write_lock(&node->contents_rwlock); |
582 | rwlock_write_lock(&node->contents_rwlock); |
458 | if (node->size) { |
583 | if (node->size) { |
459 | rc = vfs_truncate_internal(node->fs_handle, |
584 | rc = vfs_truncate_internal(node->fs_handle, |
460 | node->dev_handle, node->index, 0); |
585 | node->dev_handle, node->index, 0); |
461 | if (rc) { |
586 | if (rc) { |
462 | rwlock_write_unlock(&node->contents_rwlock); |
587 | rwlock_write_unlock(&node->contents_rwlock); |
463 | vfs_node_put(node); |
588 | vfs_node_put(node); |
464 | ipc_answer_0(rid, rc); |
589 | ipc_answer_0(rid, rc); |
465 | return; |
590 | return; |
466 | } |
591 | } |
467 | node->size = 0; |
592 | node->size = 0; |
468 | } |
593 | } |
469 | rwlock_write_unlock(&node->contents_rwlock); |
594 | rwlock_write_unlock(&node->contents_rwlock); |
470 | } |
595 | } |
471 | 596 | ||
472 | /* |
597 | /* |
473 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
598 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
474 | * structure. |
599 | * structure. |
475 | */ |
600 | */ |
476 | int fd = vfs_fd_alloc(); |
601 | int fd = vfs_fd_alloc(); |
477 | if (fd < 0) { |
602 | if (fd < 0) { |
478 | vfs_node_put(node); |
603 | vfs_node_put(node); |
479 | ipc_answer_0(rid, fd); |
604 | ipc_answer_0(rid, fd); |
480 | return; |
605 | return; |
481 | } |
606 | } |
482 | vfs_file_t *file = vfs_file_get(fd); |
607 | vfs_file_t *file = vfs_file_get(fd); |
483 | file->node = node; |
608 | file->node = node; |
484 | if (oflag & O_APPEND) |
609 | if (oflag & O_APPEND) |
485 | file->append = true; |
610 | file->append = true; |
486 | 611 | ||
487 | /* |
612 | /* |
488 | * The following increase in reference count is for the fact that the |
613 | * The following increase in reference count is for the fact that the |
489 | * file is being opened and that a file structure is pointing to it. |
614 | * file is being opened and that a file structure is pointing to it. |
490 | * It is necessary so that the file will not disappear when |
615 | * It is necessary so that the file will not disappear when |
491 | * vfs_node_put() is called. The reference will be dropped by the |
616 | * vfs_node_put() is called. The reference will be dropped by the |
492 | * respective VFS_CLOSE. |
617 | * respective VFS_CLOSE. |
493 | */ |
618 | */ |
494 | vfs_node_addref(node); |
619 | vfs_node_addref(node); |
495 | vfs_node_put(node); |
620 | vfs_node_put(node); |
496 | 621 | ||
497 | /* Success! Return the new file descriptor to the client. */ |
622 | /* Success! Return the new file descriptor to the client. */ |
498 | ipc_answer_1(rid, EOK, fd); |
623 | ipc_answer_1(rid, EOK, fd); |
499 | } |
624 | } |
500 | 625 | ||
501 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
626 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
502 | { |
627 | { |
503 | int fd = IPC_GET_ARG1(*request); |
628 | int fd = IPC_GET_ARG1(*request); |
504 | int rc = vfs_fd_free(fd); |
629 | int rc = vfs_fd_free(fd); |
505 | ipc_answer_0(rid, rc); |
630 | ipc_answer_0(rid, rc); |
506 | } |
631 | } |
507 | 632 | ||
508 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
633 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
509 | { |
634 | { |
510 | 635 | ||
511 | /* |
636 | /* |
512 | * The following code strongly depends on the fact that the files data |
637 | * The following code strongly depends on the fact that the files data |
513 | * structure can be only accessed by a single fibril and all file |
638 | * structure can be only accessed by a single fibril and all file |
514 | * operations are serialized (i.e. the reads and writes cannot |
639 | * operations are serialized (i.e. the reads and writes cannot |
515 | * interleave and a file cannot be closed while it is being read). |
640 | * interleave and a file cannot be closed while it is being read). |
516 | * |
641 | * |
517 | * Additional synchronization needs to be added once the table of |
642 | * Additional synchronization needs to be added once the table of |
518 | * open files supports parallel access! |
643 | * open files supports parallel access! |
519 | */ |
644 | */ |
520 | 645 | ||
521 | int fd = IPC_GET_ARG1(*request); |
646 | int fd = IPC_GET_ARG1(*request); |
522 | 647 | ||
523 | /* Lookup the file structure corresponding to the file descriptor. */ |
648 | /* Lookup the file structure corresponding to the file descriptor. */ |
524 | vfs_file_t *file = vfs_file_get(fd); |
649 | vfs_file_t *file = vfs_file_get(fd); |
525 | if (!file) { |
650 | if (!file) { |
526 | ipc_answer_0(rid, ENOENT); |
651 | ipc_answer_0(rid, ENOENT); |
527 | return; |
652 | return; |
528 | } |
653 | } |
529 | 654 | ||
530 | /* |
655 | /* |
531 | * Now we need to receive a call with client's |
656 | * Now we need to receive a call with client's |
532 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
657 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
533 | */ |
658 | */ |
534 | ipc_callid_t callid; |
659 | ipc_callid_t callid; |
535 | int res; |
660 | int res; |
536 | if (read) |
661 | if (read) |
537 | res = ipc_data_read_receive(&callid, NULL); |
662 | res = ipc_data_read_receive(&callid, NULL); |
538 | else |
663 | else |
539 | res = ipc_data_write_receive(&callid, NULL); |
664 | res = ipc_data_write_receive(&callid, NULL); |
540 | if (!res) { |
665 | if (!res) { |
541 | ipc_answer_0(callid, EINVAL); |
666 | ipc_answer_0(callid, EINVAL); |
542 | ipc_answer_0(rid, EINVAL); |
667 | ipc_answer_0(rid, EINVAL); |
543 | return; |
668 | return; |
544 | } |
669 | } |
545 | 670 | ||
546 | /* |
671 | /* |
547 | * Lock the open file structure so that no other thread can manipulate |
672 | * Lock the open file structure so that no other thread can manipulate |
548 | * the same open file at a time. |
673 | * the same open file at a time. |
549 | */ |
674 | */ |
550 | futex_down(&file->lock); |
675 | futex_down(&file->lock); |
551 | 676 | ||
552 | /* |
677 | /* |
553 | * Lock the file's node so that no other client can read/write to it at |
678 | * Lock the file's node so that no other client can read/write to it at |
554 | * the same time. |
679 | * the same time. |
555 | */ |
680 | */ |
556 | if (read) |
681 | if (read) |
557 | rwlock_read_lock(&file->node->contents_rwlock); |
682 | rwlock_read_lock(&file->node->contents_rwlock); |
558 | else |
683 | else |
559 | rwlock_write_lock(&file->node->contents_rwlock); |
684 | rwlock_write_lock(&file->node->contents_rwlock); |
560 | 685 | ||
561 | if (file->node->type == VFS_NODE_DIRECTORY) { |
686 | if (file->node->type == VFS_NODE_DIRECTORY) { |
562 | /* |
687 | /* |
563 | * Make sure that no one is modifying the namespace |
688 | * Make sure that no one is modifying the namespace |
564 | * while we are in readdir(). |
689 | * while we are in readdir(). |
565 | */ |
690 | */ |
566 | assert(read); |
691 | assert(read); |
567 | rwlock_read_lock(&namespace_rwlock); |
692 | rwlock_read_lock(&namespace_rwlock); |
568 | } |
693 | } |
569 | 694 | ||
570 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
695 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
571 | 696 | ||
572 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
697 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
573 | aid_t msg; |
698 | aid_t msg; |
574 | ipc_call_t answer; |
699 | ipc_call_t answer; |
575 | if (!read && file->append) |
700 | if (!read && file->append) |
576 | file->pos = file->node->size; |
701 | file->pos = file->node->size; |
577 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
702 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
578 | file->node->dev_handle, file->node->index, file->pos, &answer); |
703 | file->node->dev_handle, file->node->index, file->pos, &answer); |
579 | 704 | ||
580 | /* |
705 | /* |
581 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
706 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
582 | * destination FS server. The call will be routed as if sent by |
707 | * destination FS server. The call will be routed as if sent by |
583 | * ourselves. Note that call arguments are immutable in this case so we |
708 | * ourselves. Note that call arguments are immutable in this case so we |
584 | * don't have to bother. |
709 | * don't have to bother. |
585 | */ |
710 | */ |
586 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
711 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
587 | 712 | ||
588 | vfs_release_phone(fs_phone); |
713 | vfs_release_phone(fs_phone); |
589 | 714 | ||
590 | /* Wait for reply from the FS server. */ |
715 | /* Wait for reply from the FS server. */ |
591 | ipcarg_t rc; |
716 | ipcarg_t rc; |
592 | async_wait_for(msg, &rc); |
717 | async_wait_for(msg, &rc); |
593 | size_t bytes = IPC_GET_ARG1(answer); |
718 | size_t bytes = IPC_GET_ARG1(answer); |
594 | 719 | ||
595 | if (file->node->type == VFS_NODE_DIRECTORY) |
720 | if (file->node->type == VFS_NODE_DIRECTORY) |
596 | rwlock_read_unlock(&namespace_rwlock); |
721 | rwlock_read_unlock(&namespace_rwlock); |
597 | 722 | ||
598 | /* Unlock the VFS node. */ |
723 | /* Unlock the VFS node. */ |
599 | if (read) |
724 | if (read) |
600 | rwlock_read_unlock(&file->node->contents_rwlock); |
725 | rwlock_read_unlock(&file->node->contents_rwlock); |
601 | else { |
726 | else { |
602 | /* Update the cached version of node's size. */ |
727 | /* Update the cached version of node's size. */ |
603 | if (rc == EOK) |
728 | if (rc == EOK) |
604 | file->node->size = IPC_GET_ARG2(answer); |
729 | file->node->size = IPC_GET_ARG2(answer); |
605 | rwlock_write_unlock(&file->node->contents_rwlock); |
730 | rwlock_write_unlock(&file->node->contents_rwlock); |
606 | } |
731 | } |
607 | 732 | ||
608 | /* Update the position pointer and unlock the open file. */ |
733 | /* Update the position pointer and unlock the open file. */ |
609 | if (rc == EOK) |
734 | if (rc == EOK) |
610 | file->pos += bytes; |
735 | file->pos += bytes; |
611 | futex_up(&file->lock); |
736 | futex_up(&file->lock); |
612 | 737 | ||
613 | /* |
738 | /* |
614 | * FS server's reply is the final result of the whole operation we |
739 | * FS server's reply is the final result of the whole operation we |
615 | * return to the client. |
740 | * return to the client. |
616 | */ |
741 | */ |
617 | ipc_answer_1(rid, rc, bytes); |
742 | ipc_answer_1(rid, rc, bytes); |
618 | } |
743 | } |
619 | 744 | ||
620 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
745 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
621 | { |
746 | { |
622 | vfs_rdwr(rid, request, true); |
747 | vfs_rdwr(rid, request, true); |
623 | } |
748 | } |
624 | 749 | ||
625 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
750 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
626 | { |
751 | { |
627 | vfs_rdwr(rid, request, false); |
752 | vfs_rdwr(rid, request, false); |
628 | } |
753 | } |
629 | 754 | ||
630 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
755 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
631 | { |
756 | { |
632 | int fd = (int) IPC_GET_ARG1(*request); |
757 | int fd = (int) IPC_GET_ARG1(*request); |
633 | off_t off = (off_t) IPC_GET_ARG2(*request); |
758 | off_t off = (off_t) IPC_GET_ARG2(*request); |
634 | int whence = (int) IPC_GET_ARG3(*request); |
759 | int whence = (int) IPC_GET_ARG3(*request); |
635 | 760 | ||
636 | 761 | ||
637 | /* Lookup the file structure corresponding to the file descriptor. */ |
762 | /* Lookup the file structure corresponding to the file descriptor. */ |
638 | vfs_file_t *file = vfs_file_get(fd); |
763 | vfs_file_t *file = vfs_file_get(fd); |
639 | if (!file) { |
764 | if (!file) { |
640 | ipc_answer_0(rid, ENOENT); |
765 | ipc_answer_0(rid, ENOENT); |
641 | return; |
766 | return; |
642 | } |
767 | } |
643 | 768 | ||
644 | off_t newpos; |
769 | off_t newpos; |
645 | futex_down(&file->lock); |
770 | futex_down(&file->lock); |
646 | if (whence == SEEK_SET) { |
771 | if (whence == SEEK_SET) { |
647 | file->pos = off; |
772 | file->pos = off; |
648 | futex_up(&file->lock); |
773 | futex_up(&file->lock); |
649 | ipc_answer_1(rid, EOK, off); |
774 | ipc_answer_1(rid, EOK, off); |
650 | return; |
775 | return; |
651 | } |
776 | } |
652 | if (whence == SEEK_CUR) { |
777 | if (whence == SEEK_CUR) { |
653 | if (file->pos + off < file->pos) { |
778 | if (file->pos + off < file->pos) { |
654 | futex_up(&file->lock); |
779 | futex_up(&file->lock); |
655 | ipc_answer_0(rid, EOVERFLOW); |
780 | ipc_answer_0(rid, EOVERFLOW); |
656 | return; |
781 | return; |
657 | } |
782 | } |
658 | file->pos += off; |
783 | file->pos += off; |
659 | newpos = file->pos; |
784 | newpos = file->pos; |
660 | futex_up(&file->lock); |
785 | futex_up(&file->lock); |
661 | ipc_answer_1(rid, EOK, newpos); |
786 | ipc_answer_1(rid, EOK, newpos); |
662 | return; |
787 | return; |
663 | } |
788 | } |
664 | if (whence == SEEK_END) { |
789 | if (whence == SEEK_END) { |
665 | rwlock_read_lock(&file->node->contents_rwlock); |
790 | rwlock_read_lock(&file->node->contents_rwlock); |
666 | size_t size = file->node->size; |
791 | size_t size = file->node->size; |
667 | rwlock_read_unlock(&file->node->contents_rwlock); |
792 | rwlock_read_unlock(&file->node->contents_rwlock); |
668 | if (size + off < size) { |
793 | if (size + off < size) { |
669 | futex_up(&file->lock); |
794 | futex_up(&file->lock); |
670 | ipc_answer_0(rid, EOVERFLOW); |
795 | ipc_answer_0(rid, EOVERFLOW); |
671 | return; |
796 | return; |
672 | } |
797 | } |
673 | newpos = size + off; |
798 | newpos = size + off; |
674 | futex_up(&file->lock); |
799 | futex_up(&file->lock); |
675 | ipc_answer_1(rid, EOK, newpos); |
800 | ipc_answer_1(rid, EOK, newpos); |
676 | return; |
801 | return; |
677 | } |
802 | } |
678 | futex_up(&file->lock); |
803 | futex_up(&file->lock); |
679 | ipc_answer_0(rid, EINVAL); |
804 | ipc_answer_0(rid, EINVAL); |
680 | } |
805 | } |
681 | 806 | ||
682 | int |
807 | int |
683 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
808 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
684 | fs_index_t index, size_t size) |
809 | fs_index_t index, size_t size) |
685 | { |
810 | { |
686 | ipcarg_t rc; |
811 | ipcarg_t rc; |
687 | int fs_phone; |
812 | int fs_phone; |
688 | 813 | ||
689 | fs_phone = vfs_grab_phone(fs_handle); |
814 | fs_phone = vfs_grab_phone(fs_handle); |
690 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
815 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
691 | (ipcarg_t)index, (ipcarg_t)size); |
816 | (ipcarg_t)index, (ipcarg_t)size); |
692 | vfs_release_phone(fs_phone); |
817 | vfs_release_phone(fs_phone); |
693 | return (int)rc; |
818 | return (int)rc; |
694 | } |
819 | } |
695 | 820 | ||
696 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
821 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
697 | { |
822 | { |
698 | int fd = IPC_GET_ARG1(*request); |
823 | int fd = IPC_GET_ARG1(*request); |
699 | size_t size = IPC_GET_ARG2(*request); |
824 | size_t size = IPC_GET_ARG2(*request); |
700 | int rc; |
825 | int rc; |
701 | 826 | ||
702 | vfs_file_t *file = vfs_file_get(fd); |
827 | vfs_file_t *file = vfs_file_get(fd); |
703 | if (!file) { |
828 | if (!file) { |
704 | ipc_answer_0(rid, ENOENT); |
829 | ipc_answer_0(rid, ENOENT); |
705 | return; |
830 | return; |
706 | } |
831 | } |
707 | futex_down(&file->lock); |
832 | futex_down(&file->lock); |
708 | 833 | ||
709 | rwlock_write_lock(&file->node->contents_rwlock); |
834 | rwlock_write_lock(&file->node->contents_rwlock); |
710 | rc = vfs_truncate_internal(file->node->fs_handle, |
835 | rc = vfs_truncate_internal(file->node->fs_handle, |
711 | file->node->dev_handle, file->node->index, size); |
836 | file->node->dev_handle, file->node->index, size); |
712 | if (rc == EOK) |
837 | if (rc == EOK) |
713 | file->node->size = size; |
838 | file->node->size = size; |
714 | rwlock_write_unlock(&file->node->contents_rwlock); |
839 | rwlock_write_unlock(&file->node->contents_rwlock); |
715 | 840 | ||
716 | futex_up(&file->lock); |
841 | futex_up(&file->lock); |
717 | ipc_answer_0(rid, (ipcarg_t)rc); |
842 | ipc_answer_0(rid, (ipcarg_t)rc); |
718 | } |
843 | } |
719 | 844 | ||
720 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
845 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
721 | { |
846 | { |
722 | int mode = IPC_GET_ARG1(*request); |
847 | int mode = IPC_GET_ARG1(*request); |
723 | 848 | ||
724 | size_t len; |
849 | size_t len; |
725 | ipc_callid_t callid; |
850 | ipc_callid_t callid; |
726 | 851 | ||
727 | if (!ipc_data_write_receive(&callid, &len)) { |
852 | if (!ipc_data_write_receive(&callid, &len)) { |
728 | ipc_answer_0(callid, EINVAL); |
853 | ipc_answer_0(callid, EINVAL); |
729 | ipc_answer_0(rid, EINVAL); |
854 | ipc_answer_0(rid, EINVAL); |
730 | return; |
855 | return; |
731 | } |
856 | } |
732 | char *path = malloc(len + 1); |
857 | char *path = malloc(len + 1); |
733 | if (!path) { |
858 | if (!path) { |
734 | ipc_answer_0(callid, ENOMEM); |
859 | ipc_answer_0(callid, ENOMEM); |
735 | ipc_answer_0(rid, ENOMEM); |
860 | ipc_answer_0(rid, ENOMEM); |
736 | return; |
861 | return; |
737 | } |
862 | } |
738 | int rc; |
863 | int rc; |
739 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
864 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
740 | ipc_answer_0(rid, rc); |
865 | ipc_answer_0(rid, rc); |
741 | free(path); |
866 | free(path); |
742 | return; |
867 | return; |
743 | } |
868 | } |
744 | path[len] = '\0'; |
869 | path[len] = '\0'; |
745 | 870 | ||
746 | rwlock_write_lock(&namespace_rwlock); |
871 | rwlock_write_lock(&namespace_rwlock); |
747 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
872 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
748 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
873 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
749 | rwlock_write_unlock(&namespace_rwlock); |
874 | rwlock_write_unlock(&namespace_rwlock); |
750 | free(path); |
875 | free(path); |
751 | ipc_answer_0(rid, rc); |
876 | ipc_answer_0(rid, rc); |
752 | } |
877 | } |
753 | 878 | ||
754 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
879 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
755 | { |
880 | { |
756 | int lflag = IPC_GET_ARG1(*request); |
881 | int lflag = IPC_GET_ARG1(*request); |
757 | 882 | ||
758 | size_t len; |
883 | size_t len; |
759 | ipc_callid_t callid; |
884 | ipc_callid_t callid; |
760 | 885 | ||
761 | if (!ipc_data_write_receive(&callid, &len)) { |
886 | if (!ipc_data_write_receive(&callid, &len)) { |
762 | ipc_answer_0(callid, EINVAL); |
887 | ipc_answer_0(callid, EINVAL); |
763 | ipc_answer_0(rid, EINVAL); |
888 | ipc_answer_0(rid, EINVAL); |
764 | return; |
889 | return; |
765 | } |
890 | } |
766 | char *path = malloc(len + 1); |
891 | char *path = malloc(len + 1); |
767 | if (!path) { |
892 | if (!path) { |
768 | ipc_answer_0(callid, ENOMEM); |
893 | ipc_answer_0(callid, ENOMEM); |
769 | ipc_answer_0(rid, ENOMEM); |
894 | ipc_answer_0(rid, ENOMEM); |
770 | return; |
895 | return; |
771 | } |
896 | } |
772 | int rc; |
897 | int rc; |
773 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
898 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
774 | ipc_answer_0(rid, rc); |
899 | ipc_answer_0(rid, rc); |
775 | free(path); |
900 | free(path); |
776 | return; |
901 | return; |
777 | } |
902 | } |
778 | path[len] = '\0'; |
903 | path[len] = '\0'; |
779 | 904 | ||
780 | rwlock_write_lock(&namespace_rwlock); |
905 | rwlock_write_lock(&namespace_rwlock); |
781 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
906 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
782 | vfs_lookup_res_t lr; |
907 | vfs_lookup_res_t lr; |
783 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
908 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
784 | free(path); |
909 | free(path); |
785 | if (rc != EOK) { |
910 | if (rc != EOK) { |
786 | rwlock_write_unlock(&namespace_rwlock); |
911 | rwlock_write_unlock(&namespace_rwlock); |
787 | ipc_answer_0(rid, rc); |
912 | ipc_answer_0(rid, rc); |
788 | return; |
913 | return; |
789 | } |
914 | } |
790 | 915 | ||
791 | /* |
916 | /* |
792 | * The name has already been unlinked by vfs_lookup_internal(). |
917 | * The name has already been unlinked by vfs_lookup_internal(). |
793 | * We have to get and put the VFS node to ensure that it is |
918 | * We have to get and put the VFS node to ensure that it is |
794 | * VFS_DESTROY'ed after the last reference to it is dropped. |
919 | * VFS_DESTROY'ed after the last reference to it is dropped. |
795 | */ |
920 | */ |
796 | vfs_node_t *node = vfs_node_get(&lr); |
921 | vfs_node_t *node = vfs_node_get(&lr); |
797 | futex_down(&nodes_futex); |
922 | futex_down(&nodes_futex); |
798 | node->lnkcnt--; |
923 | node->lnkcnt--; |
799 | futex_up(&nodes_futex); |
924 | futex_up(&nodes_futex); |
800 | rwlock_write_unlock(&namespace_rwlock); |
925 | rwlock_write_unlock(&namespace_rwlock); |
801 | vfs_node_put(node); |
926 | vfs_node_put(node); |
802 | ipc_answer_0(rid, EOK); |
927 | ipc_answer_0(rid, EOK); |
803 | } |
928 | } |
804 | 929 | ||
805 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
930 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
806 | { |
931 | { |
807 | size_t olen, nlen; |
932 | size_t olen, nlen; |
808 | ipc_callid_t callid; |
933 | ipc_callid_t callid; |
809 | int rc; |
934 | int rc; |
810 | 935 | ||
811 | /* Retrieve the old path. */ |
936 | /* Retrieve the old path. */ |
812 | if (!ipc_data_write_receive(&callid, &olen)) { |
937 | if (!ipc_data_write_receive(&callid, &olen)) { |
813 | ipc_answer_0(callid, EINVAL); |
938 | ipc_answer_0(callid, EINVAL); |
814 | ipc_answer_0(rid, EINVAL); |
939 | ipc_answer_0(rid, EINVAL); |
815 | return; |
940 | return; |
816 | } |
941 | } |
817 | char *old = malloc(olen + 1); |
942 | char *old = malloc(olen + 1); |
818 | if (!old) { |
943 | if (!old) { |
819 | ipc_answer_0(callid, ENOMEM); |
944 | ipc_answer_0(callid, ENOMEM); |
820 | ipc_answer_0(rid, ENOMEM); |
945 | ipc_answer_0(rid, ENOMEM); |
821 | return; |
946 | return; |
822 | } |
947 | } |
823 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
948 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
824 | ipc_answer_0(rid, rc); |
949 | ipc_answer_0(rid, rc); |
825 | free(old); |
950 | free(old); |
826 | return; |
951 | return; |
827 | } |
952 | } |
828 | old[olen] = '\0'; |
953 | old[olen] = '\0'; |
829 | 954 | ||
830 | /* Retrieve the new path. */ |
955 | /* Retrieve the new path. */ |
831 | if (!ipc_data_write_receive(&callid, &nlen)) { |
956 | if (!ipc_data_write_receive(&callid, &nlen)) { |
832 | ipc_answer_0(callid, EINVAL); |
957 | ipc_answer_0(callid, EINVAL); |
833 | ipc_answer_0(rid, EINVAL); |
958 | ipc_answer_0(rid, EINVAL); |
834 | free(old); |
959 | free(old); |
835 | return; |
960 | return; |
836 | } |
961 | } |
837 | char *new = malloc(nlen + 1); |
962 | char *new = malloc(nlen + 1); |
838 | if (!new) { |
963 | if (!new) { |
839 | ipc_answer_0(callid, ENOMEM); |
964 | ipc_answer_0(callid, ENOMEM); |
840 | ipc_answer_0(rid, ENOMEM); |
965 | ipc_answer_0(rid, ENOMEM); |
841 | free(old); |
966 | free(old); |
842 | return; |
967 | return; |
843 | } |
968 | } |
844 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
969 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
845 | ipc_answer_0(rid, rc); |
970 | ipc_answer_0(rid, rc); |
846 | free(old); |
971 | free(old); |
847 | free(new); |
972 | free(new); |
848 | return; |
973 | return; |
849 | } |
974 | } |
850 | new[nlen] = '\0'; |
975 | new[nlen] = '\0'; |
851 | 976 | ||
852 | char *oldc = canonify(old, &olen); |
977 | char *oldc = canonify(old, &olen); |
853 | char *newc = canonify(new, &nlen); |
978 | char *newc = canonify(new, &nlen); |
854 | if (!oldc || !newc) { |
979 | if (!oldc || !newc) { |
855 | ipc_answer_0(rid, EINVAL); |
980 | ipc_answer_0(rid, EINVAL); |
856 | free(old); |
981 | free(old); |
857 | free(new); |
982 | free(new); |
858 | return; |
983 | return; |
859 | } |
984 | } |
860 | oldc[olen] = '\0'; |
985 | oldc[olen] = '\0'; |
861 | newc[nlen] = '\0'; |
986 | newc[nlen] = '\0'; |
862 | if (!str_lcmp(newc, oldc, str_length(oldc))) { |
987 | if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
- | 988 | ((newc[str_length(oldc)] == '/') || |
|
- | 989 | (str_length(oldc) == 1) || |
|
- | 990 | (str_length(oldc) == str_length(newc)))) { |
|
- | 991 | /* |
|
863 | /* oldc is a prefix of newc */ |
992 | * oldc is a prefix of newc and either |
- | 993 | * - newc continues with a / where oldc ends, or |
|
- | 994 | * - oldc was / itself, or |
|
- | 995 | * - oldc and newc are equal. |
|
- | 996 | */ |
|
864 | ipc_answer_0(rid, EINVAL); |
997 | ipc_answer_0(rid, EINVAL); |
865 | free(old); |
998 | free(old); |
866 | free(new); |
999 | free(new); |
867 | return; |
1000 | return; |
868 | } |
1001 | } |
869 | 1002 | ||
870 | vfs_lookup_res_t old_lr; |
1003 | vfs_lookup_res_t old_lr; |
871 | vfs_lookup_res_t new_lr; |
1004 | vfs_lookup_res_t new_lr; |
872 | vfs_lookup_res_t new_par_lr; |
1005 | vfs_lookup_res_t new_par_lr; |
873 | rwlock_write_lock(&namespace_rwlock); |
1006 | rwlock_write_lock(&namespace_rwlock); |
874 | /* Lookup the node belonging to the old file name. */ |
1007 | /* Lookup the node belonging to the old file name. */ |
875 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
1008 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
876 | if (rc != EOK) { |
1009 | if (rc != EOK) { |
877 | rwlock_write_unlock(&namespace_rwlock); |
1010 | rwlock_write_unlock(&namespace_rwlock); |
878 | ipc_answer_0(rid, rc); |
1011 | ipc_answer_0(rid, rc); |
879 | free(old); |
1012 | free(old); |
880 | free(new); |
1013 | free(new); |
881 | return; |
1014 | return; |
882 | } |
1015 | } |
883 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
1016 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
884 | if (!old_node) { |
1017 | if (!old_node) { |
885 | rwlock_write_unlock(&namespace_rwlock); |
1018 | rwlock_write_unlock(&namespace_rwlock); |
886 | ipc_answer_0(rid, ENOMEM); |
1019 | ipc_answer_0(rid, ENOMEM); |
887 | free(old); |
1020 | free(old); |
888 | free(new); |
1021 | free(new); |
889 | return; |
1022 | return; |
890 | } |
1023 | } |
- | 1024 | /* Determine the path to the parent of the node with the new name. */ |
|
- | 1025 | char *parentc = str_dup(newc); |
|
- | 1026 | if (!parentc) { |
|
- | 1027 | rwlock_write_unlock(&namespace_rwlock); |
|
- | 1028 | ipc_answer_0(rid, rc); |
|
- | 1029 | free(old); |
|
- | 1030 | free(new); |
|
- | 1031 | return; |
|
- | 1032 | } |
|
- | 1033 | char *lastsl = str_rchr(parentc + 1, L'/'); |
|
- | 1034 | if (lastsl) |
|
- | 1035 | *lastsl = '\0'; |
|
- | 1036 | else |
|
- | 1037 | parentc[1] = '\0'; |
|
891 | /* Lookup parent of the new file name. */ |
1038 | /* Lookup parent of the new file name. */ |
892 | rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL); |
1039 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
- | 1040 | free(parentc); /* not needed anymore */ |
|
893 | if (rc != EOK) { |
1041 | if (rc != EOK) { |
894 | rwlock_write_unlock(&namespace_rwlock); |
1042 | rwlock_write_unlock(&namespace_rwlock); |
895 | ipc_answer_0(rid, rc); |
1043 | ipc_answer_0(rid, rc); |
896 | free(old); |
1044 | free(old); |
897 | free(new); |
1045 | free(new); |
898 | return; |
1046 | return; |
899 | } |
1047 | } |
900 | /* Check whether linking to the same file system instance. */ |
1048 | /* Check whether linking to the same file system instance. */ |
901 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
1049 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
902 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
1050 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
903 | rwlock_write_unlock(&namespace_rwlock); |
1051 | rwlock_write_unlock(&namespace_rwlock); |
904 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
1052 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
905 | free(old); |
1053 | free(old); |
906 | free(new); |
1054 | free(new); |
907 | return; |
1055 | return; |
908 | } |
1056 | } |
909 | /* Destroy the old link for the new name. */ |
1057 | /* Destroy the old link for the new name. */ |
910 | vfs_node_t *new_node = NULL; |
1058 | vfs_node_t *new_node = NULL; |
911 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
1059 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
912 | switch (rc) { |
1060 | switch (rc) { |
913 | case ENOENT: |
1061 | case ENOENT: |
914 | /* simply not in our way */ |
1062 | /* simply not in our way */ |
915 | break; |
1063 | break; |
916 | case EOK: |
1064 | case EOK: |
917 | new_node = vfs_node_get(&new_lr); |
1065 | new_node = vfs_node_get(&new_lr); |
918 | if (!new_node) { |
1066 | if (!new_node) { |
919 | rwlock_write_unlock(&namespace_rwlock); |
1067 | rwlock_write_unlock(&namespace_rwlock); |
920 | ipc_answer_0(rid, ENOMEM); |
1068 | ipc_answer_0(rid, ENOMEM); |
921 | free(old); |
1069 | free(old); |
922 | free(new); |
1070 | free(new); |
923 | return; |
1071 | return; |
924 | } |
1072 | } |
925 | futex_down(&nodes_futex); |
1073 | futex_down(&nodes_futex); |
926 | new_node->lnkcnt--; |
1074 | new_node->lnkcnt--; |
927 | futex_up(&nodes_futex); |
1075 | futex_up(&nodes_futex); |
928 | break; |
1076 | break; |
929 | default: |
1077 | default: |
930 | rwlock_write_unlock(&namespace_rwlock); |
1078 | rwlock_write_unlock(&namespace_rwlock); |
931 | ipc_answer_0(rid, ENOTEMPTY); |
1079 | ipc_answer_0(rid, ENOTEMPTY); |
932 | free(old); |
1080 | free(old); |
933 | free(new); |
1081 | free(new); |
934 | return; |
1082 | return; |
935 | } |
1083 | } |
936 | /* Create the new link for the new name. */ |
1084 | /* Create the new link for the new name. */ |
937 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
1085 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
938 | if (rc != EOK) { |
1086 | if (rc != EOK) { |
939 | rwlock_write_unlock(&namespace_rwlock); |
1087 | rwlock_write_unlock(&namespace_rwlock); |
940 | if (new_node) |
1088 | if (new_node) |
941 | vfs_node_put(new_node); |
1089 | vfs_node_put(new_node); |
942 | ipc_answer_0(rid, rc); |
1090 | ipc_answer_0(rid, rc); |
943 | free(old); |
1091 | free(old); |
944 | free(new); |
1092 | free(new); |
945 | return; |
1093 | return; |
946 | } |
1094 | } |
947 | futex_down(&nodes_futex); |
1095 | futex_down(&nodes_futex); |
948 | old_node->lnkcnt++; |
1096 | old_node->lnkcnt++; |
949 | futex_up(&nodes_futex); |
1097 | futex_up(&nodes_futex); |
950 | /* Destroy the link for the old name. */ |
1098 | /* Destroy the link for the old name. */ |
951 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
1099 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
952 | if (rc != EOK) { |
1100 | if (rc != EOK) { |
953 | rwlock_write_unlock(&namespace_rwlock); |
1101 | rwlock_write_unlock(&namespace_rwlock); |
954 | vfs_node_put(old_node); |
1102 | vfs_node_put(old_node); |
955 | if (new_node) |
1103 | if (new_node) |
956 | vfs_node_put(new_node); |
1104 | vfs_node_put(new_node); |
957 | ipc_answer_0(rid, rc); |
1105 | ipc_answer_0(rid, rc); |
958 | free(old); |
1106 | free(old); |
959 | free(new); |
1107 | free(new); |
960 | return; |
1108 | return; |
961 | } |
1109 | } |
962 | futex_down(&nodes_futex); |
1110 | futex_down(&nodes_futex); |
963 | old_node->lnkcnt--; |
1111 | old_node->lnkcnt--; |
964 | futex_up(&nodes_futex); |
1112 | futex_up(&nodes_futex); |
965 | rwlock_write_unlock(&namespace_rwlock); |
1113 | rwlock_write_unlock(&namespace_rwlock); |
966 | vfs_node_put(old_node); |
1114 | vfs_node_put(old_node); |
967 | if (new_node) |
1115 | if (new_node) |
968 | vfs_node_put(new_node); |
1116 | vfs_node_put(new_node); |
969 | free(old); |
1117 | free(old); |
970 | free(new); |
1118 | free(new); |
971 | ipc_answer_0(rid, EOK); |
1119 | ipc_answer_0(rid, EOK); |
972 | } |
1120 | } |
973 | 1121 | ||
974 | /** |
1122 | /** |
975 | * @} |
1123 | * @} |
976 | */ |
1124 | */ |
977 | 1125 |