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