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