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