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