Rev 3424 | Rev 3536 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3424 | Rev 3425 | ||
---|---|---|---|
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 | /** |
58 | /** |
59 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
59 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
60 | * concurrent VFS operation which modifies the file system namespace. |
60 | * concurrent VFS operation which modifies the file system namespace. |
61 | */ |
61 | */ |
62 | RWLOCK_INITIALIZE(namespace_rwlock); |
62 | RWLOCK_INITIALIZE(namespace_rwlock); |
63 | 63 | ||
64 | futex_t rootfs_futex = FUTEX_INITIALIZER; |
64 | futex_t rootfs_futex = FUTEX_INITIALIZER; |
65 | vfs_pair_t rootfs = { |
65 | vfs_pair_t rootfs = { |
66 | .fs_handle = 0, |
66 | .fs_handle = 0, |
67 | .dev_handle = 0 |
67 | .dev_handle = 0 |
68 | }; |
68 | }; |
69 | 69 | ||
70 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
70 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
71 | { |
71 | { |
72 | dev_handle_t dev_handle; |
72 | dev_handle_t dev_handle; |
73 | vfs_node_t *mp_node = NULL; |
73 | vfs_node_t *mp_node = NULL; |
- | 74 | ipc_callid_t callid; |
|
- | 75 | ipc_call_t data; |
|
74 | int rc; |
76 | int rc; |
75 | int phone; |
77 | int phone; |
- | 78 | size_t size; |
|
76 | 79 | ||
77 | /* |
80 | /* |
78 | * We expect the library to do the device-name to device-handle |
81 | * We expect the library to do the device-name to device-handle |
79 | * translation for us, thus the device handle will arrive as ARG1 |
82 | * translation for us, thus the device handle will arrive as ARG1 |
80 | * in the request. |
83 | * in the request. |
81 | */ |
84 | */ |
82 | dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
85 | dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
83 | 86 | ||
84 | /* |
87 | /* |
85 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
88 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
86 | * carry mount options in the future. |
89 | * carry mount options in the future. |
87 | */ |
90 | */ |
88 | 91 | ||
89 | ipc_callid_t callid; |
- | |
90 | size_t size; |
- | |
91 | - | ||
92 | /* |
92 | /* |
93 | * Now, we expect the client to send us data with the name of the file |
93 | * Now, we expect the client to send us data with the name of the file |
94 | * system. |
94 | * system. |
95 | */ |
95 | */ |
96 | if (!ipc_data_write_receive(&callid, &size)) { |
96 | if (!ipc_data_write_receive(&callid, &size)) { |
97 | ipc_answer_0(callid, EINVAL); |
97 | ipc_answer_0(callid, EINVAL); |
98 | ipc_answer_0(rid, EINVAL); |
98 | ipc_answer_0(rid, EINVAL); |
99 | return; |
99 | return; |
100 | } |
100 | } |
101 | 101 | ||
102 | /* |
102 | /* |
103 | * Don't receive more than is necessary for storing a full file system |
103 | * Don't receive more than is necessary for storing a full file system |
104 | * name. |
104 | * name. |
105 | */ |
105 | */ |
106 | if (size < 1 || size > FS_NAME_MAXLEN) { |
106 | if (size < 1 || size > FS_NAME_MAXLEN) { |
107 | ipc_answer_0(callid, EINVAL); |
107 | ipc_answer_0(callid, EINVAL); |
108 | ipc_answer_0(rid, EINVAL); |
108 | ipc_answer_0(rid, EINVAL); |
109 | return; |
109 | return; |
110 | } |
110 | } |
111 | 111 | ||
112 | /* Deliver the file system name. */ |
112 | /* Deliver the file system name. */ |
113 | char fs_name[FS_NAME_MAXLEN + 1]; |
113 | char fs_name[FS_NAME_MAXLEN + 1]; |
114 | (void) ipc_data_write_finalize(callid, fs_name, size); |
114 | (void) ipc_data_write_finalize(callid, fs_name, size); |
115 | fs_name[size] = '\0'; |
115 | fs_name[size] = '\0'; |
116 | 116 | ||
117 | /* |
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 | /* |
|
118 | * Check if we know a file system with the same name as is in fs_name. |
129 | * Check if we know a file system with the same name as is in fs_name. |
119 | * This will also give us its file system handle. |
130 | * This will also give us its file system handle. |
120 | */ |
131 | */ |
121 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
132 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
122 | if (!fs_handle) { |
133 | if (!fs_handle) { |
- | 134 | ipc_answer_0(callid, ENOENT); |
|
123 | ipc_answer_0(rid, ENOENT); |
135 | ipc_answer_0(rid, ENOENT); |
124 | return; |
136 | return; |
125 | } |
137 | } |
126 | 138 | ||
- | 139 | /* Acknowledge that we know fs_name. */ |
|
- | 140 | ipc_answer_0(callid, EOK); |
|
- | 141 | ||
127 | /* Now, we want the client to send us the mount point. */ |
142 | /* Now, we want the client to send us the mount point. */ |
128 | if (!ipc_data_write_receive(&callid, &size)) { |
143 | if (!ipc_data_write_receive(&callid, &size)) { |
129 | ipc_answer_0(callid, EINVAL); |
144 | ipc_answer_0(callid, EINVAL); |
130 | ipc_answer_0(rid, EINVAL); |
145 | ipc_answer_0(rid, EINVAL); |
131 | return; |
146 | return; |
132 | } |
147 | } |
133 | 148 | ||
134 | /* Check whether size is reasonable wrt. the mount point. */ |
149 | /* Check whether size is reasonable wrt. the mount point. */ |
135 | if (size < 1 || size > MAX_PATH_LEN) { |
150 | if (size < 1 || size > MAX_PATH_LEN) { |
136 | ipc_answer_0(callid, EINVAL); |
151 | ipc_answer_0(callid, EINVAL); |
137 | ipc_answer_0(rid, EINVAL); |
152 | ipc_answer_0(rid, EINVAL); |
138 | return; |
153 | return; |
139 | } |
154 | } |
140 | /* Allocate buffer for the mount point data being received. */ |
155 | /* Allocate buffer for the mount point data being received. */ |
141 | uint8_t *buf; |
156 | uint8_t *buf; |
142 | buf = malloc(size + 1); |
157 | buf = malloc(size + 1); |
143 | if (!buf) { |
158 | if (!buf) { |
144 | ipc_answer_0(callid, ENOMEM); |
159 | ipc_answer_0(callid, ENOMEM); |
145 | ipc_answer_0(rid, ENOMEM); |
160 | ipc_answer_0(rid, ENOMEM); |
146 | return; |
161 | return; |
147 | } |
162 | } |
148 | 163 | ||
149 | /* Deliver the mount point. */ |
164 | /* Deliver the mount point. */ |
150 | (void) ipc_data_write_finalize(callid, buf, size); |
165 | (void) ipc_data_write_finalize(callid, buf, size); |
151 | buf[size] = '\0'; |
166 | buf[size] = '\0'; |
152 | 167 | ||
153 | /* Resolve the path to the mountpoint. */ |
168 | /* Resolve the path to the mountpoint. */ |
154 | vfs_lookup_res_t mp_res; |
169 | vfs_lookup_res_t mp_res; |
155 | futex_down(&rootfs_futex); |
170 | futex_down(&rootfs_futex); |
156 | if (rootfs.fs_handle) { |
171 | if (rootfs.fs_handle) { |
157 | /* We already have the root FS. */ |
172 | /* We already have the root FS. */ |
158 | rwlock_write_lock(&namespace_rwlock); |
173 | rwlock_write_lock(&namespace_rwlock); |
159 | if ((size == 1) && (buf[0] == '/')) { |
174 | if ((size == 1) && (buf[0] == '/')) { |
160 | /* Trying to mount root FS over root FS */ |
175 | /* Trying to mount root FS over root FS */ |
161 | rwlock_write_unlock(&namespace_rwlock); |
176 | rwlock_write_unlock(&namespace_rwlock); |
162 | futex_up(&rootfs_futex); |
177 | futex_up(&rootfs_futex); |
163 | free(buf); |
178 | free(buf); |
164 | ipc_answer_0(rid, EBUSY); |
179 | ipc_answer_0(rid, EBUSY); |
165 | return; |
180 | return; |
166 | } |
181 | } |
167 | rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL); |
182 | rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL); |
168 | if (rc != EOK) { |
183 | if (rc != EOK) { |
169 | /* The lookup failed for some reason. */ |
184 | /* The lookup failed for some reason. */ |
170 | rwlock_write_unlock(&namespace_rwlock); |
185 | rwlock_write_unlock(&namespace_rwlock); |
171 | futex_up(&rootfs_futex); |
186 | futex_up(&rootfs_futex); |
172 | free(buf); |
187 | free(buf); |
173 | ipc_answer_0(rid, rc); |
188 | ipc_answer_0(rid, rc); |
174 | return; |
189 | return; |
175 | } |
190 | } |
176 | mp_node = vfs_node_get(&mp_res); |
191 | mp_node = vfs_node_get(&mp_res); |
177 | if (!mp_node) { |
192 | if (!mp_node) { |
178 | rwlock_write_unlock(&namespace_rwlock); |
193 | rwlock_write_unlock(&namespace_rwlock); |
179 | futex_up(&rootfs_futex); |
194 | futex_up(&rootfs_futex); |
180 | free(buf); |
195 | free(buf); |
181 | ipc_answer_0(rid, ENOMEM); |
196 | ipc_answer_0(rid, ENOMEM); |
182 | return; |
197 | return; |
183 | } |
198 | } |
184 | /* |
199 | /* |
185 | * Now we hold a reference to mp_node. |
200 | * Now we hold a reference to mp_node. |
186 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
201 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
187 | * This prevents the mount point from being deleted. |
202 | * This prevents the mount point from being deleted. |
188 | */ |
203 | */ |
189 | rwlock_write_unlock(&namespace_rwlock); |
204 | rwlock_write_unlock(&namespace_rwlock); |
190 | } else { |
205 | } else { |
191 | /* We still don't have the root file system mounted. */ |
206 | /* We still don't have the root file system mounted. */ |
192 | if ((size == 1) && (buf[0] == '/')) { |
207 | if ((size == 1) && (buf[0] == '/')) { |
- | 208 | vfs_lookup_res_t mr_res; |
|
- | 209 | vfs_node_t *mr_node; |
|
- | 210 | ipcarg_t rindex; |
|
- | 211 | ipcarg_t rsize; |
|
- | 212 | ipcarg_t rlnkcnt; |
|
- | 213 | ||
193 | /* |
214 | /* |
194 | * For this simple, but important case, |
215 | * For this simple, but important case, |
195 | * we are almost done. |
216 | * we are almost done. |
196 | */ |
217 | */ |
197 | free(buf); |
218 | free(buf); |
198 | 219 | ||
199 | /* Tell the mountee that it is being mounted. */ |
220 | /* Tell the mountee that it is being mounted. */ |
200 | phone = vfs_grab_phone(fs_handle); |
221 | phone = vfs_grab_phone(fs_handle); |
201 | rc = async_req_1_0(phone, VFS_MOUNTED, |
222 | rc = async_req_1_3(phone, VFS_MOUNTED, |
202 | (ipcarg_t) dev_handle); |
223 | (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt); |
203 | vfs_release_phone(phone); |
224 | vfs_release_phone(phone); |
204 | 225 | ||
205 | if (rc == EOK) { |
226 | if (rc != EOK) { |
206 | rootfs.fs_handle = fs_handle; |
227 | futex_up(&rootfs_futex); |
207 | rootfs.dev_handle = dev_handle; |
228 | ipc_answer_0(rid, rc); |
- | 229 | return; |
|
208 | } |
230 | } |
209 | 231 | ||
- | 232 | mr_res.triplet.fs_handle = fs_handle; |
|
- | 233 | mr_res.triplet.dev_handle = dev_handle; |
|
- | 234 | mr_res.triplet.index = (fs_index_t) rindex; |
|
- | 235 | mr_res.size = (size_t) rsize; |
|
- | 236 | mr_res.lnkcnt = (unsigned) rlnkcnt; |
|
- | 237 | ||
- | 238 | rootfs.fs_handle = fs_handle; |
|
- | 239 | rootfs.dev_handle = dev_handle; |
|
210 | futex_up(&rootfs_futex); |
240 | futex_up(&rootfs_futex); |
- | 241 | ||
- | 242 | /* Add reference to the mounted root. */ |
|
- | 243 | mr_node = vfs_node_get(&mr_res); |
|
- | 244 | assert(mr_node); |
|
- | 245 | ||
211 | ipc_answer_0(rid, rc); |
246 | ipc_answer_0(rid, rc); |
212 | return; |
247 | return; |
213 | } else { |
248 | } else { |
214 | /* |
249 | /* |
215 | * We can't resolve this without the root filesystem |
250 | * We can't resolve this without the root filesystem |
216 | * being mounted first. |
251 | * being mounted first. |
217 | */ |
252 | */ |
218 | futex_up(&rootfs_futex); |
253 | futex_up(&rootfs_futex); |
219 | free(buf); |
254 | free(buf); |
220 | ipc_answer_0(rid, ENOENT); |
255 | ipc_answer_0(rid, ENOENT); |
221 | return; |
256 | return; |
222 | } |
257 | } |
223 | } |
258 | } |
224 | futex_up(&rootfs_futex); |
259 | futex_up(&rootfs_futex); |
225 | 260 | ||
226 | free(buf); /* The buffer is not needed anymore. */ |
261 | free(buf); /* The buffer is not needed anymore. */ |
227 | 262 | ||
228 | /* |
263 | /* |
229 | * At this point, we have all necessary pieces: file system and device |
264 | * At this point, we have all necessary pieces: file system and device |
230 | * handles, and we know the mount point VFS node. |
265 | * handles, and we know the mount point VFS node. |
231 | */ |
266 | */ |
232 | 267 | ||
233 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
268 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
234 | rc = async_req_4_0(phone, VFS_MOUNT, |
269 | rc = async_req_4_0(phone, VFS_MOUNT, |
235 | (ipcarg_t) mp_res.triplet.dev_handle, |
270 | (ipcarg_t) mp_res.triplet.dev_handle, |
236 | (ipcarg_t) mp_res.triplet.index, |
271 | (ipcarg_t) mp_res.triplet.index, |
237 | (ipcarg_t) fs_handle, |
272 | (ipcarg_t) fs_handle, |
238 | (ipcarg_t) dev_handle); |
273 | (ipcarg_t) dev_handle); |
239 | vfs_release_phone(phone); |
274 | vfs_release_phone(phone); |
240 | 275 | ||
241 | if (rc != EOK) { |
276 | if (rc != EOK) { |
242 | /* Mount failed, drop reference to mp_node. */ |
277 | /* Mount failed, drop reference to mp_node. */ |
243 | if (mp_node) |
278 | if (mp_node) |
244 | vfs_node_put(mp_node); |
279 | vfs_node_put(mp_node); |
245 | } |
280 | } |
246 | 281 | ||
247 | ipc_answer_0(rid, rc); |
282 | ipc_answer_0(rid, rc); |
248 | } |
283 | } |
249 | 284 | ||
250 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
285 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
251 | { |
286 | { |
252 | if (!vfs_files_init()) { |
287 | if (!vfs_files_init()) { |
253 | ipc_answer_0(rid, ENOMEM); |
288 | ipc_answer_0(rid, ENOMEM); |
254 | return; |
289 | return; |
255 | } |
290 | } |
256 | 291 | ||
257 | /* |
292 | /* |
258 | * The POSIX interface is open(path, oflag, mode). |
293 | * The POSIX interface is open(path, oflag, mode). |
259 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
294 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
260 | * will need to arrive in another call. |
295 | * will need to arrive in another call. |
261 | * |
296 | * |
262 | * We also receive one private, non-POSIX set of flags called lflag |
297 | * We also receive one private, non-POSIX set of flags called lflag |
263 | * used to pass information to vfs_lookup_internal(). |
298 | * used to pass information to vfs_lookup_internal(). |
264 | */ |
299 | */ |
265 | int lflag = IPC_GET_ARG1(*request); |
300 | int lflag = IPC_GET_ARG1(*request); |
266 | int oflag = IPC_GET_ARG2(*request); |
301 | int oflag = IPC_GET_ARG2(*request); |
267 | int mode = IPC_GET_ARG3(*request); |
302 | int mode = IPC_GET_ARG3(*request); |
268 | size_t len; |
303 | size_t len; |
269 | 304 | ||
270 | if (oflag & O_CREAT) |
305 | if (oflag & O_CREAT) |
271 | lflag |= L_CREATE; |
306 | lflag |= L_CREATE; |
272 | if (oflag & O_EXCL) |
307 | if (oflag & O_EXCL) |
273 | lflag |= L_EXCLUSIVE; |
308 | lflag |= L_EXCLUSIVE; |
274 | 309 | ||
275 | ipc_callid_t callid; |
310 | ipc_callid_t callid; |
276 | 311 | ||
277 | if (!ipc_data_write_receive(&callid, &len)) { |
312 | if (!ipc_data_write_receive(&callid, &len)) { |
278 | ipc_answer_0(callid, EINVAL); |
313 | ipc_answer_0(callid, EINVAL); |
279 | ipc_answer_0(rid, EINVAL); |
314 | ipc_answer_0(rid, EINVAL); |
280 | return; |
315 | return; |
281 | } |
316 | } |
282 | char *path = malloc(len + 1); |
317 | char *path = malloc(len + 1); |
283 | if (!path) { |
318 | if (!path) { |
284 | ipc_answer_0(callid, ENOMEM); |
319 | ipc_answer_0(callid, ENOMEM); |
285 | ipc_answer_0(rid, ENOMEM); |
320 | ipc_answer_0(rid, ENOMEM); |
286 | return; |
321 | return; |
287 | } |
322 | } |
288 | int rc; |
323 | int rc; |
289 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
324 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
290 | ipc_answer_0(rid, rc); |
325 | ipc_answer_0(rid, rc); |
291 | free(path); |
326 | free(path); |
292 | return; |
327 | return; |
293 | } |
328 | } |
294 | path[len] = '\0'; |
329 | path[len] = '\0'; |
295 | 330 | ||
296 | /* |
331 | /* |
297 | * Avoid the race condition in which the file can be deleted before we |
332 | * Avoid the race condition in which the file can be deleted before we |
298 | * find/create-and-lock the VFS node corresponding to the looked-up |
333 | * find/create-and-lock the VFS node corresponding to the looked-up |
299 | * triplet. |
334 | * triplet. |
300 | */ |
335 | */ |
301 | if (lflag & L_CREATE) |
336 | if (lflag & L_CREATE) |
302 | rwlock_write_lock(&namespace_rwlock); |
337 | rwlock_write_lock(&namespace_rwlock); |
303 | else |
338 | else |
304 | rwlock_read_lock(&namespace_rwlock); |
339 | rwlock_read_lock(&namespace_rwlock); |
305 | 340 | ||
306 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
341 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
307 | vfs_lookup_res_t lr; |
342 | vfs_lookup_res_t lr; |
308 | rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
343 | rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
309 | if (rc) { |
344 | if (rc) { |
310 | if (lflag & L_CREATE) |
345 | if (lflag & L_CREATE) |
311 | rwlock_write_unlock(&namespace_rwlock); |
346 | rwlock_write_unlock(&namespace_rwlock); |
312 | else |
347 | else |
313 | rwlock_read_unlock(&namespace_rwlock); |
348 | rwlock_read_unlock(&namespace_rwlock); |
314 | ipc_answer_0(rid, rc); |
349 | ipc_answer_0(rid, rc); |
315 | free(path); |
350 | free(path); |
316 | return; |
351 | return; |
317 | } |
352 | } |
318 | 353 | ||
319 | /* Path is no longer needed. */ |
354 | /* Path is no longer needed. */ |
320 | free(path); |
355 | free(path); |
321 | 356 | ||
322 | vfs_node_t *node = vfs_node_get(&lr); |
357 | vfs_node_t *node = vfs_node_get(&lr); |
323 | if (lflag & L_CREATE) |
358 | if (lflag & L_CREATE) |
324 | rwlock_write_unlock(&namespace_rwlock); |
359 | rwlock_write_unlock(&namespace_rwlock); |
325 | else |
360 | else |
326 | rwlock_read_unlock(&namespace_rwlock); |
361 | rwlock_read_unlock(&namespace_rwlock); |
327 | 362 | ||
328 | /* Truncate the file if requested and if necessary. */ |
363 | /* Truncate the file if requested and if necessary. */ |
329 | if (oflag & O_TRUNC) { |
364 | if (oflag & O_TRUNC) { |
330 | rwlock_write_lock(&node->contents_rwlock); |
365 | rwlock_write_lock(&node->contents_rwlock); |
331 | if (node->size) { |
366 | if (node->size) { |
332 | rc = vfs_truncate_internal(node->fs_handle, |
367 | rc = vfs_truncate_internal(node->fs_handle, |
333 | node->dev_handle, node->index, 0); |
368 | node->dev_handle, node->index, 0); |
334 | if (rc) { |
369 | if (rc) { |
335 | rwlock_write_unlock(&node->contents_rwlock); |
370 | rwlock_write_unlock(&node->contents_rwlock); |
336 | vfs_node_put(node); |
371 | vfs_node_put(node); |
337 | ipc_answer_0(rid, rc); |
372 | ipc_answer_0(rid, rc); |
338 | return; |
373 | return; |
339 | } |
374 | } |
340 | node->size = 0; |
375 | node->size = 0; |
341 | } |
376 | } |
342 | rwlock_write_unlock(&node->contents_rwlock); |
377 | rwlock_write_unlock(&node->contents_rwlock); |
343 | } |
378 | } |
344 | 379 | ||
345 | /* |
380 | /* |
346 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
381 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
347 | * structure. |
382 | * structure. |
348 | */ |
383 | */ |
349 | int fd = vfs_fd_alloc(); |
384 | int fd = vfs_fd_alloc(); |
350 | if (fd < 0) { |
385 | if (fd < 0) { |
351 | vfs_node_put(node); |
386 | vfs_node_put(node); |
352 | ipc_answer_0(rid, fd); |
387 | ipc_answer_0(rid, fd); |
353 | return; |
388 | return; |
354 | } |
389 | } |
355 | vfs_file_t *file = vfs_file_get(fd); |
390 | vfs_file_t *file = vfs_file_get(fd); |
356 | file->node = node; |
391 | file->node = node; |
357 | if (oflag & O_APPEND) |
392 | if (oflag & O_APPEND) |
358 | file->append = true; |
393 | file->append = true; |
359 | 394 | ||
360 | /* |
395 | /* |
361 | * The following increase in reference count is for the fact that the |
396 | * The following increase in reference count is for the fact that the |
362 | * file is being opened and that a file structure is pointing to it. |
397 | * file is being opened and that a file structure is pointing to it. |
363 | * It is necessary so that the file will not disappear when |
398 | * It is necessary so that the file will not disappear when |
364 | * vfs_node_put() is called. The reference will be dropped by the |
399 | * vfs_node_put() is called. The reference will be dropped by the |
365 | * respective VFS_CLOSE. |
400 | * respective VFS_CLOSE. |
366 | */ |
401 | */ |
367 | vfs_node_addref(node); |
402 | vfs_node_addref(node); |
368 | vfs_node_put(node); |
403 | vfs_node_put(node); |
369 | 404 | ||
370 | /* Success! Return the new file descriptor to the client. */ |
405 | /* Success! Return the new file descriptor to the client. */ |
371 | ipc_answer_1(rid, EOK, fd); |
406 | ipc_answer_1(rid, EOK, fd); |
372 | } |
407 | } |
373 | 408 | ||
374 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
409 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
375 | { |
410 | { |
376 | int fd = IPC_GET_ARG1(*request); |
411 | int fd = IPC_GET_ARG1(*request); |
377 | if (fd >= MAX_OPEN_FILES) { |
- | |
378 | ipc_answer_0(rid, EBADF); |
- | |
379 | return; |
- | |
380 | } |
- | |
381 | vfs_fd_free(fd); |
412 | int rc = vfs_fd_free(fd); |
382 | ipc_answer_0(rid, EOK); |
413 | ipc_answer_0(rid, rc); |
383 | } |
414 | } |
384 | 415 | ||
385 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
416 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
386 | { |
417 | { |
387 | 418 | ||
388 | /* |
419 | /* |
389 | * The following code strongly depends on the fact that the files data |
420 | * The following code strongly depends on the fact that the files data |
390 | * structure can be only accessed by a single fibril and all file |
421 | * structure can be only accessed by a single fibril and all file |
391 | * operations are serialized (i.e. the reads and writes cannot |
422 | * operations are serialized (i.e. the reads and writes cannot |
392 | * interleave and a file cannot be closed while it is being read). |
423 | * interleave and a file cannot be closed while it is being read). |
393 | * |
424 | * |
394 | * Additional synchronization needs to be added once the table of |
425 | * Additional synchronization needs to be added once the table of |
395 | * open files supports parallel access! |
426 | * open files supports parallel access! |
396 | */ |
427 | */ |
397 | 428 | ||
398 | int fd = IPC_GET_ARG1(*request); |
429 | int fd = IPC_GET_ARG1(*request); |
399 | 430 | ||
400 | /* Lookup the file structure corresponding to the file descriptor. */ |
431 | /* Lookup the file structure corresponding to the file descriptor. */ |
401 | vfs_file_t *file = vfs_file_get(fd); |
432 | vfs_file_t *file = vfs_file_get(fd); |
402 | if (!file) { |
433 | if (!file) { |
403 | ipc_answer_0(rid, ENOENT); |
434 | ipc_answer_0(rid, ENOENT); |
404 | return; |
435 | return; |
405 | } |
436 | } |
406 | 437 | ||
407 | /* |
438 | /* |
408 | * Now we need to receive a call with client's |
439 | * Now we need to receive a call with client's |
409 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
440 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
410 | */ |
441 | */ |
411 | ipc_callid_t callid; |
442 | ipc_callid_t callid; |
412 | int res; |
443 | int res; |
413 | if (read) |
444 | if (read) |
414 | res = ipc_data_read_receive(&callid, NULL); |
445 | res = ipc_data_read_receive(&callid, NULL); |
415 | else |
446 | else |
416 | res = ipc_data_write_receive(&callid, NULL); |
447 | res = ipc_data_write_receive(&callid, NULL); |
417 | if (!res) { |
448 | if (!res) { |
418 | ipc_answer_0(callid, EINVAL); |
449 | ipc_answer_0(callid, EINVAL); |
419 | ipc_answer_0(rid, EINVAL); |
450 | ipc_answer_0(rid, EINVAL); |
420 | return; |
451 | return; |
421 | } |
452 | } |
422 | 453 | ||
423 | /* |
454 | /* |
424 | * Lock the open file structure so that no other thread can manipulate |
455 | * Lock the open file structure so that no other thread can manipulate |
425 | * the same open file at a time. |
456 | * the same open file at a time. |
426 | */ |
457 | */ |
427 | futex_down(&file->lock); |
458 | futex_down(&file->lock); |
428 | 459 | ||
429 | /* |
460 | /* |
430 | * Lock the file's node so that no other client can read/write to it at |
461 | * Lock the file's node so that no other client can read/write to it at |
431 | * the same time. |
462 | * the same time. |
432 | */ |
463 | */ |
433 | if (read) |
464 | if (read) |
434 | rwlock_read_lock(&file->node->contents_rwlock); |
465 | rwlock_read_lock(&file->node->contents_rwlock); |
435 | else |
466 | else |
436 | rwlock_write_lock(&file->node->contents_rwlock); |
467 | rwlock_write_lock(&file->node->contents_rwlock); |
437 | 468 | ||
438 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
469 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
439 | 470 | ||
440 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
471 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
441 | aid_t msg; |
472 | aid_t msg; |
442 | ipc_call_t answer; |
473 | ipc_call_t answer; |
443 | if (!read && file->append) |
474 | if (!read && file->append) |
444 | file->pos = file->node->size; |
475 | file->pos = file->node->size; |
445 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
476 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
446 | file->node->dev_handle, file->node->index, file->pos, &answer); |
477 | file->node->dev_handle, file->node->index, file->pos, &answer); |
447 | 478 | ||
448 | /* |
479 | /* |
449 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
480 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
450 | * destination FS server. The call will be routed as if sent by |
481 | * destination FS server. The call will be routed as if sent by |
451 | * ourselves. Note that call arguments are immutable in this case so we |
482 | * ourselves. Note that call arguments are immutable in this case so we |
452 | * don't have to bother. |
483 | * don't have to bother. |
453 | */ |
484 | */ |
454 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
485 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
455 | 486 | ||
456 | vfs_release_phone(fs_phone); |
487 | vfs_release_phone(fs_phone); |
457 | 488 | ||
458 | /* Wait for reply from the FS server. */ |
489 | /* Wait for reply from the FS server. */ |
459 | ipcarg_t rc; |
490 | ipcarg_t rc; |
460 | async_wait_for(msg, &rc); |
491 | async_wait_for(msg, &rc); |
461 | size_t bytes = IPC_GET_ARG1(answer); |
492 | size_t bytes = IPC_GET_ARG1(answer); |
462 | 493 | ||
463 | /* Unlock the VFS node. */ |
494 | /* Unlock the VFS node. */ |
464 | if (read) |
495 | if (read) |
465 | rwlock_read_unlock(&file->node->contents_rwlock); |
496 | rwlock_read_unlock(&file->node->contents_rwlock); |
466 | else { |
497 | else { |
467 | /* Update the cached version of node's size. */ |
498 | /* Update the cached version of node's size. */ |
468 | if (rc == EOK) |
499 | if (rc == EOK) |
469 | file->node->size = IPC_GET_ARG2(answer); |
500 | file->node->size = IPC_GET_ARG2(answer); |
470 | rwlock_write_unlock(&file->node->contents_rwlock); |
501 | rwlock_write_unlock(&file->node->contents_rwlock); |
471 | } |
502 | } |
472 | 503 | ||
473 | /* Update the position pointer and unlock the open file. */ |
504 | /* Update the position pointer and unlock the open file. */ |
474 | if (rc == EOK) |
505 | if (rc == EOK) |
475 | file->pos += bytes; |
506 | file->pos += bytes; |
476 | futex_up(&file->lock); |
507 | futex_up(&file->lock); |
477 | 508 | ||
478 | /* |
509 | /* |
479 | * FS server's reply is the final result of the whole operation we |
510 | * FS server's reply is the final result of the whole operation we |
480 | * return to the client. |
511 | * return to the client. |
481 | */ |
512 | */ |
482 | ipc_answer_1(rid, rc, bytes); |
513 | ipc_answer_1(rid, rc, bytes); |
483 | } |
514 | } |
484 | 515 | ||
485 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
516 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
486 | { |
517 | { |
487 | vfs_rdwr(rid, request, true); |
518 | vfs_rdwr(rid, request, true); |
488 | } |
519 | } |
489 | 520 | ||
490 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
521 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
491 | { |
522 | { |
492 | vfs_rdwr(rid, request, false); |
523 | vfs_rdwr(rid, request, false); |
493 | } |
524 | } |
494 | 525 | ||
495 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
526 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
496 | { |
527 | { |
497 | int fd = (int) IPC_GET_ARG1(*request); |
528 | int fd = (int) IPC_GET_ARG1(*request); |
498 | off_t off = (off_t) IPC_GET_ARG2(*request); |
529 | off_t off = (off_t) IPC_GET_ARG2(*request); |
499 | int whence = (int) IPC_GET_ARG3(*request); |
530 | int whence = (int) IPC_GET_ARG3(*request); |
500 | 531 | ||
501 | 532 | ||
502 | /* Lookup the file structure corresponding to the file descriptor. */ |
533 | /* Lookup the file structure corresponding to the file descriptor. */ |
503 | vfs_file_t *file = vfs_file_get(fd); |
534 | vfs_file_t *file = vfs_file_get(fd); |
504 | if (!file) { |
535 | if (!file) { |
505 | ipc_answer_0(rid, ENOENT); |
536 | ipc_answer_0(rid, ENOENT); |
506 | return; |
537 | return; |
507 | } |
538 | } |
508 | 539 | ||
509 | off_t newpos; |
540 | off_t newpos; |
510 | futex_down(&file->lock); |
541 | futex_down(&file->lock); |
511 | if (whence == SEEK_SET) { |
542 | if (whence == SEEK_SET) { |
512 | file->pos = off; |
543 | file->pos = off; |
513 | futex_up(&file->lock); |
544 | futex_up(&file->lock); |
514 | ipc_answer_1(rid, EOK, off); |
545 | ipc_answer_1(rid, EOK, off); |
515 | return; |
546 | return; |
516 | } |
547 | } |
517 | if (whence == SEEK_CUR) { |
548 | if (whence == SEEK_CUR) { |
518 | if (file->pos + off < file->pos) { |
549 | if (file->pos + off < file->pos) { |
519 | futex_up(&file->lock); |
550 | futex_up(&file->lock); |
520 | ipc_answer_0(rid, EOVERFLOW); |
551 | ipc_answer_0(rid, EOVERFLOW); |
521 | return; |
552 | return; |
522 | } |
553 | } |
523 | file->pos += off; |
554 | file->pos += off; |
524 | newpos = file->pos; |
555 | newpos = file->pos; |
525 | futex_up(&file->lock); |
556 | futex_up(&file->lock); |
526 | ipc_answer_1(rid, EOK, newpos); |
557 | ipc_answer_1(rid, EOK, newpos); |
527 | return; |
558 | return; |
528 | } |
559 | } |
529 | if (whence == SEEK_END) { |
560 | if (whence == SEEK_END) { |
530 | rwlock_read_lock(&file->node->contents_rwlock); |
561 | rwlock_read_lock(&file->node->contents_rwlock); |
531 | size_t size = file->node->size; |
562 | size_t size = file->node->size; |
532 | rwlock_read_unlock(&file->node->contents_rwlock); |
563 | rwlock_read_unlock(&file->node->contents_rwlock); |
533 | if (size + off < size) { |
564 | if (size + off < size) { |
534 | futex_up(&file->lock); |
565 | futex_up(&file->lock); |
535 | ipc_answer_0(rid, EOVERFLOW); |
566 | ipc_answer_0(rid, EOVERFLOW); |
536 | return; |
567 | return; |
537 | } |
568 | } |
538 | newpos = size + off; |
569 | newpos = size + off; |
539 | futex_up(&file->lock); |
570 | futex_up(&file->lock); |
540 | ipc_answer_1(rid, EOK, newpos); |
571 | ipc_answer_1(rid, EOK, newpos); |
541 | return; |
572 | return; |
542 | } |
573 | } |
543 | futex_up(&file->lock); |
574 | futex_up(&file->lock); |
544 | ipc_answer_0(rid, EINVAL); |
575 | ipc_answer_0(rid, EINVAL); |
545 | } |
576 | } |
546 | 577 | ||
547 | int |
578 | int |
548 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
579 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
549 | fs_index_t index, size_t size) |
580 | fs_index_t index, size_t size) |
550 | { |
581 | { |
551 | ipcarg_t rc; |
582 | ipcarg_t rc; |
552 | int fs_phone; |
583 | int fs_phone; |
553 | 584 | ||
554 | fs_phone = vfs_grab_phone(fs_handle); |
585 | fs_phone = vfs_grab_phone(fs_handle); |
555 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
586 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
556 | (ipcarg_t)index, (ipcarg_t)size); |
587 | (ipcarg_t)index, (ipcarg_t)size); |
557 | vfs_release_phone(fs_phone); |
588 | vfs_release_phone(fs_phone); |
558 | return (int)rc; |
589 | return (int)rc; |
559 | } |
590 | } |
560 | 591 | ||
561 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
592 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
562 | { |
593 | { |
563 | int fd = IPC_GET_ARG1(*request); |
594 | int fd = IPC_GET_ARG1(*request); |
564 | size_t size = IPC_GET_ARG2(*request); |
595 | size_t size = IPC_GET_ARG2(*request); |
565 | int rc; |
596 | int rc; |
566 | 597 | ||
567 | vfs_file_t *file = vfs_file_get(fd); |
598 | vfs_file_t *file = vfs_file_get(fd); |
568 | if (!file) { |
599 | if (!file) { |
569 | ipc_answer_0(rid, ENOENT); |
600 | ipc_answer_0(rid, ENOENT); |
570 | return; |
601 | return; |
571 | } |
602 | } |
572 | futex_down(&file->lock); |
603 | futex_down(&file->lock); |
573 | 604 | ||
574 | rwlock_write_lock(&file->node->contents_rwlock); |
605 | rwlock_write_lock(&file->node->contents_rwlock); |
575 | rc = vfs_truncate_internal(file->node->fs_handle, |
606 | rc = vfs_truncate_internal(file->node->fs_handle, |
576 | file->node->dev_handle, file->node->index, size); |
607 | file->node->dev_handle, file->node->index, size); |
577 | if (rc == EOK) |
608 | if (rc == EOK) |
578 | file->node->size = size; |
609 | file->node->size = size; |
579 | rwlock_write_unlock(&file->node->contents_rwlock); |
610 | rwlock_write_unlock(&file->node->contents_rwlock); |
580 | 611 | ||
581 | futex_up(&file->lock); |
612 | futex_up(&file->lock); |
582 | ipc_answer_0(rid, (ipcarg_t)rc); |
613 | ipc_answer_0(rid, (ipcarg_t)rc); |
583 | } |
614 | } |
584 | 615 | ||
585 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
616 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
586 | { |
617 | { |
587 | int mode = IPC_GET_ARG1(*request); |
618 | int mode = IPC_GET_ARG1(*request); |
588 | 619 | ||
589 | size_t len; |
620 | size_t len; |
590 | ipc_callid_t callid; |
621 | ipc_callid_t callid; |
591 | 622 | ||
592 | if (!ipc_data_write_receive(&callid, &len)) { |
623 | if (!ipc_data_write_receive(&callid, &len)) { |
593 | ipc_answer_0(callid, EINVAL); |
624 | ipc_answer_0(callid, EINVAL); |
594 | ipc_answer_0(rid, EINVAL); |
625 | ipc_answer_0(rid, EINVAL); |
595 | return; |
626 | return; |
596 | } |
627 | } |
597 | char *path = malloc(len + 1); |
628 | char *path = malloc(len + 1); |
598 | if (!path) { |
629 | if (!path) { |
599 | ipc_answer_0(callid, ENOMEM); |
630 | ipc_answer_0(callid, ENOMEM); |
600 | ipc_answer_0(rid, ENOMEM); |
631 | ipc_answer_0(rid, ENOMEM); |
601 | return; |
632 | return; |
602 | } |
633 | } |
603 | int rc; |
634 | int rc; |
604 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
635 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
605 | ipc_answer_0(rid, rc); |
636 | ipc_answer_0(rid, rc); |
606 | free(path); |
637 | free(path); |
607 | return; |
638 | return; |
608 | } |
639 | } |
609 | path[len] = '\0'; |
640 | path[len] = '\0'; |
610 | 641 | ||
611 | rwlock_write_lock(&namespace_rwlock); |
642 | rwlock_write_lock(&namespace_rwlock); |
612 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
643 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
613 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
644 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
614 | rwlock_write_unlock(&namespace_rwlock); |
645 | rwlock_write_unlock(&namespace_rwlock); |
615 | free(path); |
646 | free(path); |
616 | ipc_answer_0(rid, rc); |
647 | ipc_answer_0(rid, rc); |
617 | } |
648 | } |
618 | 649 | ||
619 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
650 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
620 | { |
651 | { |
621 | int lflag = IPC_GET_ARG1(*request); |
652 | int lflag = IPC_GET_ARG1(*request); |
622 | 653 | ||
623 | size_t len; |
654 | size_t len; |
624 | ipc_callid_t callid; |
655 | ipc_callid_t callid; |
625 | 656 | ||
626 | if (!ipc_data_write_receive(&callid, &len)) { |
657 | if (!ipc_data_write_receive(&callid, &len)) { |
627 | ipc_answer_0(callid, EINVAL); |
658 | ipc_answer_0(callid, EINVAL); |
628 | ipc_answer_0(rid, EINVAL); |
659 | ipc_answer_0(rid, EINVAL); |
629 | return; |
660 | return; |
630 | } |
661 | } |
631 | char *path = malloc(len + 1); |
662 | char *path = malloc(len + 1); |
632 | if (!path) { |
663 | if (!path) { |
633 | ipc_answer_0(callid, ENOMEM); |
664 | ipc_answer_0(callid, ENOMEM); |
634 | ipc_answer_0(rid, ENOMEM); |
665 | ipc_answer_0(rid, ENOMEM); |
635 | return; |
666 | return; |
636 | } |
667 | } |
637 | int rc; |
668 | int rc; |
638 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
669 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
639 | ipc_answer_0(rid, rc); |
670 | ipc_answer_0(rid, rc); |
640 | free(path); |
671 | free(path); |
641 | return; |
672 | return; |
642 | } |
673 | } |
643 | path[len] = '\0'; |
674 | path[len] = '\0'; |
644 | 675 | ||
645 | rwlock_write_lock(&namespace_rwlock); |
676 | rwlock_write_lock(&namespace_rwlock); |
646 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
677 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
647 | vfs_lookup_res_t lr; |
678 | vfs_lookup_res_t lr; |
648 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
679 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
649 | free(path); |
680 | free(path); |
650 | if (rc != EOK) { |
681 | if (rc != EOK) { |
651 | rwlock_write_unlock(&namespace_rwlock); |
682 | rwlock_write_unlock(&namespace_rwlock); |
652 | ipc_answer_0(rid, rc); |
683 | ipc_answer_0(rid, rc); |
653 | return; |
684 | return; |
654 | } |
685 | } |
655 | 686 | ||
656 | /* |
687 | /* |
657 | * The name has already been unlinked by vfs_lookup_internal(). |
688 | * The name has already been unlinked by vfs_lookup_internal(). |
658 | * We have to get and put the VFS node to ensure that it is |
689 | * We have to get and put the VFS node to ensure that it is |
659 | * VFS_DESTROY'ed after the last reference to it is dropped. |
690 | * VFS_DESTROY'ed after the last reference to it is dropped. |
660 | */ |
691 | */ |
661 | vfs_node_t *node = vfs_node_get(&lr); |
692 | vfs_node_t *node = vfs_node_get(&lr); |
662 | futex_down(&nodes_futex); |
693 | futex_down(&nodes_futex); |
663 | node->lnkcnt--; |
694 | node->lnkcnt--; |
664 | futex_up(&nodes_futex); |
695 | futex_up(&nodes_futex); |
665 | rwlock_write_unlock(&namespace_rwlock); |
696 | rwlock_write_unlock(&namespace_rwlock); |
666 | vfs_node_put(node); |
697 | vfs_node_put(node); |
667 | ipc_answer_0(rid, EOK); |
698 | ipc_answer_0(rid, EOK); |
668 | } |
699 | } |
669 | 700 | ||
670 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
701 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
671 | { |
702 | { |
672 | size_t len; |
703 | size_t len; |
673 | ipc_callid_t callid; |
704 | ipc_callid_t callid; |
674 | int rc; |
705 | int rc; |
675 | 706 | ||
676 | /* Retrieve the old path. */ |
707 | /* Retrieve the old path. */ |
677 | if (!ipc_data_write_receive(&callid, &len)) { |
708 | if (!ipc_data_write_receive(&callid, &len)) { |
678 | ipc_answer_0(callid, EINVAL); |
709 | ipc_answer_0(callid, EINVAL); |
679 | ipc_answer_0(rid, EINVAL); |
710 | ipc_answer_0(rid, EINVAL); |
680 | return; |
711 | return; |
681 | } |
712 | } |
682 | char *old = malloc(len + 1); |
713 | char *old = malloc(len + 1); |
683 | if (!old) { |
714 | if (!old) { |
684 | ipc_answer_0(callid, ENOMEM); |
715 | ipc_answer_0(callid, ENOMEM); |
685 | ipc_answer_0(rid, ENOMEM); |
716 | ipc_answer_0(rid, ENOMEM); |
686 | return; |
717 | return; |
687 | } |
718 | } |
688 | if ((rc = ipc_data_write_finalize(callid, old, len))) { |
719 | if ((rc = ipc_data_write_finalize(callid, old, len))) { |
689 | ipc_answer_0(rid, rc); |
720 | ipc_answer_0(rid, rc); |
690 | free(old); |
721 | free(old); |
691 | return; |
722 | return; |
692 | } |
723 | } |
693 | old[len] = '\0'; |
724 | old[len] = '\0'; |
694 | 725 | ||
695 | /* Retrieve the new path. */ |
726 | /* Retrieve the new path. */ |
696 | if (!ipc_data_write_receive(&callid, &len)) { |
727 | if (!ipc_data_write_receive(&callid, &len)) { |
697 | ipc_answer_0(callid, EINVAL); |
728 | ipc_answer_0(callid, EINVAL); |
698 | ipc_answer_0(rid, EINVAL); |
729 | ipc_answer_0(rid, EINVAL); |
699 | free(old); |
730 | free(old); |
700 | return; |
731 | return; |
701 | } |
732 | } |
702 | char *new = malloc(len + 1); |
733 | char *new = malloc(len + 1); |
703 | if (!new) { |
734 | if (!new) { |
704 | ipc_answer_0(callid, ENOMEM); |
735 | ipc_answer_0(callid, ENOMEM); |
705 | ipc_answer_0(rid, ENOMEM); |
736 | ipc_answer_0(rid, ENOMEM); |
706 | free(old); |
737 | free(old); |
707 | return; |
738 | return; |
708 | } |
739 | } |
709 | if ((rc = ipc_data_write_finalize(callid, new, len))) { |
740 | if ((rc = ipc_data_write_finalize(callid, new, len))) { |
710 | ipc_answer_0(rid, rc); |
741 | ipc_answer_0(rid, rc); |
711 | free(old); |
742 | free(old); |
712 | free(new); |
743 | free(new); |
713 | return; |
744 | return; |
714 | } |
745 | } |
715 | new[len] = '\0'; |
746 | new[len] = '\0'; |
716 | 747 | ||
717 | char *oldc = canonify(old, &len); |
748 | char *oldc = canonify(old, &len); |
718 | char *newc = canonify(new, NULL); |
749 | char *newc = canonify(new, NULL); |
719 | if (!oldc || !newc) { |
750 | if (!oldc || !newc) { |
720 | ipc_answer_0(rid, EINVAL); |
751 | ipc_answer_0(rid, EINVAL); |
721 | free(old); |
752 | free(old); |
722 | free(new); |
753 | free(new); |
723 | return; |
754 | return; |
724 | } |
755 | } |
725 | if (!strncmp(newc, oldc, len)) { |
756 | if (!strncmp(newc, oldc, len)) { |
726 | /* oldc is a prefix of newc */ |
757 | /* oldc is a prefix of newc */ |
727 | ipc_answer_0(rid, EINVAL); |
758 | ipc_answer_0(rid, EINVAL); |
728 | free(old); |
759 | free(old); |
729 | free(new); |
760 | free(new); |
730 | return; |
761 | return; |
731 | } |
762 | } |
732 | 763 | ||
733 | vfs_lookup_res_t old_lr; |
764 | vfs_lookup_res_t old_lr; |
734 | vfs_lookup_res_t new_lr; |
765 | vfs_lookup_res_t new_lr; |
735 | vfs_lookup_res_t new_par_lr; |
766 | vfs_lookup_res_t new_par_lr; |
736 | rwlock_write_lock(&namespace_rwlock); |
767 | rwlock_write_lock(&namespace_rwlock); |
737 | /* Lookup the node belonging to the old file name. */ |
768 | /* Lookup the node belonging to the old file name. */ |
738 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
769 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
739 | if (rc != EOK) { |
770 | if (rc != EOK) { |
740 | rwlock_write_unlock(&namespace_rwlock); |
771 | rwlock_write_unlock(&namespace_rwlock); |
741 | ipc_answer_0(rid, rc); |
772 | ipc_answer_0(rid, rc); |
742 | free(old); |
773 | free(old); |
743 | free(new); |
774 | free(new); |
744 | return; |
775 | return; |
745 | } |
776 | } |
746 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
777 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
747 | if (!old_node) { |
778 | if (!old_node) { |
748 | rwlock_write_unlock(&namespace_rwlock); |
779 | rwlock_write_unlock(&namespace_rwlock); |
749 | ipc_answer_0(rid, ENOMEM); |
780 | ipc_answer_0(rid, ENOMEM); |
750 | free(old); |
781 | free(old); |
751 | free(new); |
782 | free(new); |
752 | return; |
783 | return; |
753 | } |
784 | } |
754 | /* Lookup parent of the new file name. */ |
785 | /* Lookup parent of the new file name. */ |
755 | rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL); |
786 | rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL); |
756 | if (rc != EOK) { |
787 | if (rc != EOK) { |
757 | rwlock_write_unlock(&namespace_rwlock); |
788 | rwlock_write_unlock(&namespace_rwlock); |
758 | ipc_answer_0(rid, rc); |
789 | ipc_answer_0(rid, rc); |
759 | free(old); |
790 | free(old); |
760 | free(new); |
791 | free(new); |
761 | return; |
792 | return; |
762 | } |
793 | } |
763 | /* Check whether linking to the same file system instance. */ |
794 | /* Check whether linking to the same file system instance. */ |
764 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
795 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
765 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
796 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
766 | rwlock_write_unlock(&namespace_rwlock); |
797 | rwlock_write_unlock(&namespace_rwlock); |
767 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
798 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
768 | free(old); |
799 | free(old); |
769 | free(new); |
800 | free(new); |
770 | return; |
801 | return; |
771 | } |
802 | } |
772 | /* Destroy the old link for the new name. */ |
803 | /* Destroy the old link for the new name. */ |
773 | vfs_node_t *new_node = NULL; |
804 | vfs_node_t *new_node = NULL; |
774 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
805 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
775 | switch (rc) { |
806 | switch (rc) { |
776 | case ENOENT: |
807 | case ENOENT: |
777 | /* simply not in our way */ |
808 | /* simply not in our way */ |
778 | break; |
809 | break; |
779 | case EOK: |
810 | case EOK: |
780 | new_node = vfs_node_get(&new_lr); |
811 | new_node = vfs_node_get(&new_lr); |
781 | if (!new_node) { |
812 | if (!new_node) { |
782 | rwlock_write_unlock(&namespace_rwlock); |
813 | rwlock_write_unlock(&namespace_rwlock); |
783 | ipc_answer_0(rid, ENOMEM); |
814 | ipc_answer_0(rid, ENOMEM); |
784 | free(old); |
815 | free(old); |
785 | free(new); |
816 | free(new); |
786 | return; |
817 | return; |
787 | } |
818 | } |
788 | futex_down(&nodes_futex); |
819 | futex_down(&nodes_futex); |
789 | new_node->lnkcnt--; |
820 | new_node->lnkcnt--; |
790 | futex_up(&nodes_futex); |
821 | futex_up(&nodes_futex); |
791 | break; |
822 | break; |
792 | default: |
823 | default: |
793 | rwlock_write_unlock(&namespace_rwlock); |
824 | rwlock_write_unlock(&namespace_rwlock); |
794 | ipc_answer_0(rid, ENOTEMPTY); |
825 | ipc_answer_0(rid, ENOTEMPTY); |
795 | free(old); |
826 | free(old); |
796 | free(new); |
827 | free(new); |
797 | return; |
828 | return; |
798 | } |
829 | } |
799 | /* Create the new link for the new name. */ |
830 | /* Create the new link for the new name. */ |
800 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
831 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
801 | if (rc != EOK) { |
832 | if (rc != EOK) { |
802 | rwlock_write_unlock(&namespace_rwlock); |
833 | rwlock_write_unlock(&namespace_rwlock); |
803 | if (new_node) |
834 | if (new_node) |
804 | vfs_node_put(new_node); |
835 | vfs_node_put(new_node); |
805 | ipc_answer_0(rid, rc); |
836 | ipc_answer_0(rid, rc); |
806 | free(old); |
837 | free(old); |
807 | free(new); |
838 | free(new); |
808 | return; |
839 | return; |
809 | } |
840 | } |
810 | futex_down(&nodes_futex); |
841 | futex_down(&nodes_futex); |
811 | old_node->lnkcnt++; |
842 | old_node->lnkcnt++; |
812 | futex_up(&nodes_futex); |
843 | futex_up(&nodes_futex); |
813 | /* Destroy the link for the old name. */ |
844 | /* Destroy the link for the old name. */ |
814 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
845 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
815 | if (rc != EOK) { |
846 | if (rc != EOK) { |
816 | rwlock_write_unlock(&namespace_rwlock); |
847 | rwlock_write_unlock(&namespace_rwlock); |
817 | vfs_node_put(old_node); |
848 | vfs_node_put(old_node); |
818 | if (new_node) |
849 | if (new_node) |
819 | vfs_node_put(new_node); |
850 | vfs_node_put(new_node); |
820 | ipc_answer_0(rid, rc); |
851 | ipc_answer_0(rid, rc); |
821 | free(old); |
852 | free(old); |
822 | free(new); |
853 | free(new); |
823 | return; |
854 | return; |
824 | } |
855 | } |
825 | futex_down(&nodes_futex); |
856 | futex_down(&nodes_futex); |
826 | old_node->lnkcnt--; |
857 | old_node->lnkcnt--; |
827 | futex_up(&nodes_futex); |
858 | futex_up(&nodes_futex); |
828 | rwlock_write_unlock(&namespace_rwlock); |
859 | rwlock_write_unlock(&namespace_rwlock); |
829 | vfs_node_put(old_node); |
860 | vfs_node_put(old_node); |
830 | if (new_node) |
861 | if (new_node) |
831 | vfs_node_put(new_node); |
862 | vfs_node_put(new_node); |
832 | free(old); |
863 | free(old); |
833 | free(new); |
864 | free(new); |
834 | ipc_answer_0(rid, EOK); |
865 | ipc_answer_0(rid, EOK); |
835 | } |
866 | } |
836 | 867 | ||
837 | /** |
868 | /** |
838 | * @} |
869 | * @} |
839 | */ |
870 | */ |
840 | 871 |