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