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