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