Rev 4585 | Rev 4593 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4585 | Rev 4587 | ||
---|---|---|---|
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 <fibril_sync.h> |
46 | #include <fibril_sync.h> |
47 | #include <adt/list.h> |
47 | #include <adt/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 <vfs/canonify.h> |
52 | #include <vfs/canonify.h> |
53 | 53 | ||
54 | /* Forward declarations of static functions. */ |
54 | /* Forward declarations of static functions. */ |
55 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
55 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
56 | 56 | ||
57 | /** |
57 | /** |
58 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
58 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
59 | * concurrent VFS operation which modifies the file system namespace. |
59 | * concurrent VFS operation which modifies the file system namespace. |
60 | */ |
60 | */ |
61 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock); |
61 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock); |
62 | 62 | ||
63 | vfs_pair_t rootfs = { |
63 | vfs_pair_t rootfs = { |
64 | .fs_handle = 0, |
64 | .fs_handle = 0, |
65 | .dev_handle = 0 |
65 | .dev_handle = 0 |
66 | }; |
66 | }; |
67 | 67 | ||
68 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle, |
68 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle, |
69 | fs_handle_t fs_handle, char *mp, char *opts) |
69 | fs_handle_t fs_handle, char *mp, char *opts) |
70 | { |
70 | { |
71 | vfs_lookup_res_t mp_res; |
71 | vfs_lookup_res_t mp_res; |
72 | vfs_lookup_res_t mr_res; |
72 | vfs_lookup_res_t mr_res; |
73 | vfs_node_t *mp_node = NULL; |
73 | vfs_node_t *mp_node = NULL; |
74 | vfs_node_t *mr_node; |
74 | vfs_node_t *mr_node; |
75 | fs_index_t rindex; |
75 | fs_index_t rindex; |
76 | size_t rsize; |
76 | size_t rsize; |
77 | unsigned rlnkcnt; |
77 | unsigned rlnkcnt; |
78 | ipcarg_t rc; |
78 | ipcarg_t rc; |
79 | int phone; |
79 | int phone; |
80 | aid_t msg; |
80 | aid_t msg; |
81 | ipc_call_t answer; |
81 | ipc_call_t answer; |
82 | 82 | ||
83 | /* Resolve the path to the mountpoint. */ |
83 | /* Resolve the path to the mountpoint. */ |
84 | fibril_rwlock_write_lock(&namespace_rwlock); |
84 | fibril_rwlock_write_lock(&namespace_rwlock); |
85 | if (rootfs.fs_handle) { |
85 | if (rootfs.fs_handle) { |
86 | /* We already have the root FS. */ |
86 | /* We already have the root FS. */ |
87 | if (str_cmp(mp, "/") == 0) { |
87 | if (str_cmp(mp, "/") == 0) { |
88 | /* Trying to mount root FS over root FS */ |
88 | /* Trying to mount root FS over root FS */ |
89 | fibril_rwlock_write_unlock(&namespace_rwlock); |
89 | fibril_rwlock_write_unlock(&namespace_rwlock); |
90 | ipc_answer_0(rid, EBUSY); |
90 | ipc_answer_0(rid, EBUSY); |
91 | return; |
91 | return; |
92 | } |
92 | } |
93 | 93 | ||
94 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
94 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
95 | if (rc != EOK) { |
95 | if (rc != EOK) { |
96 | /* The lookup failed for some reason. */ |
96 | /* The lookup failed for some reason. */ |
97 | fibril_rwlock_write_unlock(&namespace_rwlock); |
97 | fibril_rwlock_write_unlock(&namespace_rwlock); |
98 | ipc_answer_0(rid, rc); |
98 | ipc_answer_0(rid, rc); |
99 | return; |
99 | return; |
100 | } |
100 | } |
101 | 101 | ||
102 | mp_node = vfs_node_get(&mp_res); |
102 | mp_node = vfs_node_get(&mp_res); |
103 | if (!mp_node) { |
103 | if (!mp_node) { |
104 | fibril_rwlock_write_unlock(&namespace_rwlock); |
104 | fibril_rwlock_write_unlock(&namespace_rwlock); |
105 | ipc_answer_0(rid, ENOMEM); |
105 | ipc_answer_0(rid, ENOMEM); |
106 | return; |
106 | return; |
107 | } |
107 | } |
108 | 108 | ||
109 | /* |
109 | /* |
110 | * Now we hold a reference to mp_node. |
110 | * Now we hold a reference to mp_node. |
111 | * It will be dropped upon the corresponding VFS_IN_UNMOUNT. |
111 | * It will be dropped upon the corresponding VFS_IN_UNMOUNT. |
112 | * This prevents the mount point from being deleted. |
112 | * This prevents the mount point from being deleted. |
113 | */ |
113 | */ |
114 | } else { |
114 | } else { |
115 | /* We still don't have the root file system mounted. */ |
115 | /* We still don't have the root file system mounted. */ |
116 | if (str_cmp(mp, "/") == 0) { |
116 | if (str_cmp(mp, "/") == 0) { |
117 | /* |
117 | /* |
118 | * For this simple, but important case, |
118 | * For this simple, but important case, |
119 | * we are almost done. |
119 | * we are almost done. |
120 | */ |
120 | */ |
121 | 121 | ||
122 | /* Tell the mountee that it is being mounted. */ |
122 | /* Tell the mountee that it is being mounted. */ |
123 | phone = vfs_grab_phone(fs_handle); |
123 | phone = vfs_grab_phone(fs_handle); |
124 | msg = async_send_1(phone, VFS_OUT_MOUNTED, |
124 | msg = async_send_1(phone, VFS_OUT_MOUNTED, |
125 | (ipcarg_t) dev_handle, &answer); |
125 | (ipcarg_t) dev_handle, &answer); |
126 | /* send the mount options */ |
126 | /* send the mount options */ |
127 | rc = ipc_data_write_start(phone, (void *)opts, |
127 | rc = ipc_data_write_start(phone, (void *)opts, |
128 | str_size(opts)); |
128 | str_size(opts)); |
129 | if (rc != EOK) { |
129 | if (rc != EOK) { |
130 | async_wait_for(msg, NULL); |
130 | async_wait_for(msg, NULL); |
131 | vfs_release_phone(phone); |
131 | vfs_release_phone(phone); |
132 | fibril_rwlock_write_unlock(&namespace_rwlock); |
132 | fibril_rwlock_write_unlock(&namespace_rwlock); |
133 | ipc_answer_0(rid, rc); |
133 | ipc_answer_0(rid, rc); |
134 | return; |
134 | return; |
135 | } |
135 | } |
136 | async_wait_for(msg, &rc); |
136 | async_wait_for(msg, &rc); |
137 | vfs_release_phone(phone); |
137 | vfs_release_phone(phone); |
138 | 138 | ||
139 | if (rc != EOK) { |
139 | if (rc != EOK) { |
140 | fibril_rwlock_write_unlock(&namespace_rwlock); |
140 | fibril_rwlock_write_unlock(&namespace_rwlock); |
141 | ipc_answer_0(rid, rc); |
141 | ipc_answer_0(rid, rc); |
142 | return; |
142 | return; |
143 | } |
143 | } |
144 | 144 | ||
145 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
145 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
146 | rsize = (size_t) IPC_GET_ARG2(answer); |
146 | rsize = (size_t) IPC_GET_ARG2(answer); |
147 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
147 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
148 | 148 | ||
149 | mr_res.triplet.fs_handle = fs_handle; |
149 | mr_res.triplet.fs_handle = fs_handle; |
150 | mr_res.triplet.dev_handle = dev_handle; |
150 | mr_res.triplet.dev_handle = dev_handle; |
151 | mr_res.triplet.index = rindex; |
151 | mr_res.triplet.index = rindex; |
152 | mr_res.size = rsize; |
152 | mr_res.size = rsize; |
153 | mr_res.lnkcnt = rlnkcnt; |
153 | mr_res.lnkcnt = rlnkcnt; |
154 | mr_res.type = VFS_NODE_DIRECTORY; |
154 | mr_res.type = VFS_NODE_DIRECTORY; |
155 | 155 | ||
156 | rootfs.fs_handle = fs_handle; |
156 | rootfs.fs_handle = fs_handle; |
157 | rootfs.dev_handle = dev_handle; |
157 | rootfs.dev_handle = dev_handle; |
158 | 158 | ||
159 | /* Add reference to the mounted root. */ |
159 | /* Add reference to the mounted root. */ |
160 | mr_node = vfs_node_get(&mr_res); |
160 | mr_node = vfs_node_get(&mr_res); |
161 | assert(mr_node); |
161 | assert(mr_node); |
162 | 162 | ||
163 | fibril_rwlock_write_unlock(&namespace_rwlock); |
163 | fibril_rwlock_write_unlock(&namespace_rwlock); |
164 | ipc_answer_0(rid, rc); |
164 | ipc_answer_0(rid, rc); |
165 | return; |
165 | return; |
166 | } else { |
166 | } else { |
167 | /* |
167 | /* |
168 | * We can't resolve this without the root filesystem |
168 | * We can't resolve this without the root filesystem |
169 | * being mounted first. |
169 | * being mounted first. |
170 | */ |
170 | */ |
171 | fibril_rwlock_write_unlock(&namespace_rwlock); |
171 | fibril_rwlock_write_unlock(&namespace_rwlock); |
172 | ipc_answer_0(rid, ENOENT); |
172 | ipc_answer_0(rid, ENOENT); |
173 | return; |
173 | return; |
174 | } |
174 | } |
175 | } |
175 | } |
176 | 176 | ||
177 | /* |
177 | /* |
178 | * At this point, we have all necessary pieces: file system and device |
178 | * At this point, we have all necessary pieces: file system and device |
179 | * handles, and we know the mount point VFS node. |
179 | * handles, and we know the mount point VFS node. |
180 | */ |
180 | */ |
181 | 181 | ||
182 | int mountee_phone = vfs_grab_phone(fs_handle); |
182 | int mountee_phone = vfs_grab_phone(fs_handle); |
183 | assert(mountee_phone >= 0); |
183 | assert(mountee_phone >= 0); |
184 | 184 | ||
185 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
185 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
186 | msg = async_send_4(phone, VFS_OUT_MOUNT, |
186 | msg = async_send_4(phone, VFS_OUT_MOUNT, |
187 | (ipcarg_t) mp_res.triplet.dev_handle, |
187 | (ipcarg_t) mp_res.triplet.dev_handle, |
188 | (ipcarg_t) mp_res.triplet.index, |
188 | (ipcarg_t) mp_res.triplet.index, |
189 | (ipcarg_t) fs_handle, |
189 | (ipcarg_t) fs_handle, |
190 | (ipcarg_t) dev_handle, &answer); |
190 | (ipcarg_t) dev_handle, &answer); |
191 | 191 | ||
192 | /* send connection */ |
192 | /* send connection */ |
193 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
193 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
194 | if (rc != EOK) { |
194 | if (rc != EOK) { |
195 | async_wait_for(msg, NULL); |
195 | async_wait_for(msg, NULL); |
196 | vfs_release_phone(mountee_phone); |
196 | vfs_release_phone(mountee_phone); |
197 | vfs_release_phone(phone); |
197 | vfs_release_phone(phone); |
198 | /* Mount failed, drop reference to mp_node. */ |
198 | /* Mount failed, drop reference to mp_node. */ |
199 | if (mp_node) |
199 | if (mp_node) |
200 | vfs_node_put(mp_node); |
200 | vfs_node_put(mp_node); |
201 | ipc_answer_0(rid, rc); |
201 | ipc_answer_0(rid, rc); |
202 | fibril_rwlock_write_unlock(&namespace_rwlock); |
202 | fibril_rwlock_write_unlock(&namespace_rwlock); |
203 | return; |
203 | return; |
204 | } |
204 | } |
205 | 205 | ||
206 | vfs_release_phone(mountee_phone); |
206 | vfs_release_phone(mountee_phone); |
207 | 207 | ||
208 | /* send the mount options */ |
208 | /* send the mount options */ |
209 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
209 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
210 | if (rc != EOK) { |
210 | if (rc != EOK) { |
211 | async_wait_for(msg, NULL); |
211 | async_wait_for(msg, NULL); |
212 | vfs_release_phone(phone); |
212 | vfs_release_phone(phone); |
213 | /* Mount failed, drop reference to mp_node. */ |
213 | /* Mount failed, drop reference to mp_node. */ |
214 | if (mp_node) |
214 | if (mp_node) |
215 | vfs_node_put(mp_node); |
215 | vfs_node_put(mp_node); |
216 | fibril_rwlock_write_unlock(&namespace_rwlock); |
216 | fibril_rwlock_write_unlock(&namespace_rwlock); |
217 | ipc_answer_0(rid, rc); |
217 | ipc_answer_0(rid, rc); |
218 | return; |
218 | return; |
219 | } |
219 | } |
220 | async_wait_for(msg, &rc); |
220 | async_wait_for(msg, &rc); |
221 | vfs_release_phone(phone); |
221 | vfs_release_phone(phone); |
222 | 222 | ||
223 | if (rc == EOK) { |
223 | if (rc == EOK) { |
224 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
224 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
225 | rsize = (size_t) IPC_GET_ARG2(answer); |
225 | rsize = (size_t) IPC_GET_ARG2(answer); |
226 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
226 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
227 | 227 | ||
228 | mr_res.triplet.fs_handle = fs_handle; |
228 | mr_res.triplet.fs_handle = fs_handle; |
229 | mr_res.triplet.dev_handle = dev_handle; |
229 | mr_res.triplet.dev_handle = dev_handle; |
230 | mr_res.triplet.index = rindex; |
230 | mr_res.triplet.index = rindex; |
231 | mr_res.size = rsize; |
231 | mr_res.size = rsize; |
232 | mr_res.lnkcnt = rlnkcnt; |
232 | mr_res.lnkcnt = rlnkcnt; |
233 | mr_res.type = VFS_NODE_DIRECTORY; |
233 | mr_res.type = VFS_NODE_DIRECTORY; |
234 | 234 | ||
235 | /* Add reference to the mounted root. */ |
235 | /* Add reference to the mounted root. */ |
236 | mr_node = vfs_node_get(&mr_res); |
236 | mr_node = vfs_node_get(&mr_res); |
237 | assert(mr_node); |
237 | assert(mr_node); |
238 | } else { |
238 | } else { |
239 | /* Mount failed, drop reference to mp_node. */ |
239 | /* Mount failed, drop reference to mp_node. */ |
240 | if (mp_node) |
240 | if (mp_node) |
241 | vfs_node_put(mp_node); |
241 | vfs_node_put(mp_node); |
242 | } |
242 | } |
243 | 243 | ||
244 | ipc_answer_0(rid, rc); |
244 | ipc_answer_0(rid, rc); |
245 | fibril_rwlock_write_unlock(&namespace_rwlock); |
245 | fibril_rwlock_write_unlock(&namespace_rwlock); |
246 | } |
246 | } |
247 | 247 | ||
248 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
248 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
249 | { |
249 | { |
250 | /* |
250 | /* |
251 | * We expect the library to do the device-name to device-handle |
251 | * We expect the library to do the device-name to device-handle |
252 | * translation for us, thus the device handle will arrive as ARG1 |
252 | * translation for us, thus the device handle will arrive as ARG1 |
253 | * in the request. |
253 | * in the request. |
254 | */ |
254 | */ |
255 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
255 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
256 | 256 | ||
257 | /* |
257 | /* |
258 | * Mount flags are passed as ARG2. |
258 | * Mount flags are passed as ARG2. |
259 | */ |
259 | */ |
260 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request); |
260 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request); |
261 | 261 | ||
262 | /* |
262 | /* |
263 | * For now, don't make use of ARG3, but it can be used to |
263 | * For now, don't make use of ARG3, but it can be used to |
264 | * carry mount options in the future. |
264 | * carry mount options in the future. |
265 | */ |
265 | */ |
266 | 266 | ||
267 | /* We want the client to send us the mount point. */ |
267 | /* We want the client to send us the mount point. */ |
268 | ipc_callid_t callid; |
268 | ipc_callid_t callid; |
269 | size_t size; |
269 | size_t size; |
270 | if (!ipc_data_write_receive(&callid, &size)) { |
270 | if (!ipc_data_write_receive(&callid, &size)) { |
271 | ipc_answer_0(callid, EINVAL); |
271 | ipc_answer_0(callid, EINVAL); |
272 | ipc_answer_0(rid, EINVAL); |
272 | ipc_answer_0(rid, EINVAL); |
273 | return; |
273 | return; |
274 | } |
274 | } |
275 | 275 | ||
276 | /* Check whether size is reasonable wrt. the mount point. */ |
276 | /* Check whether size is reasonable wrt. the mount point. */ |
277 | if ((size < 1) || (size > MAX_PATH_LEN)) { |
277 | if ((size < 1) || (size > MAX_PATH_LEN)) { |
278 | ipc_answer_0(callid, EINVAL); |
278 | ipc_answer_0(callid, EINVAL); |
279 | ipc_answer_0(rid, EINVAL); |
279 | ipc_answer_0(rid, EINVAL); |
280 | return; |
280 | return; |
281 | } |
281 | } |
282 | 282 | ||
283 | /* Allocate buffer for the mount point data being received. */ |
283 | /* Allocate buffer for the mount point data being received. */ |
284 | char *mp = malloc(size + 1); |
284 | char *mp = malloc(size + 1); |
285 | if (!mp) { |
285 | if (!mp) { |
286 | ipc_answer_0(callid, ENOMEM); |
286 | ipc_answer_0(callid, ENOMEM); |
287 | ipc_answer_0(rid, ENOMEM); |
287 | ipc_answer_0(rid, ENOMEM); |
288 | return; |
288 | return; |
289 | } |
289 | } |
290 | 290 | ||
291 | /* Deliver the mount point. */ |
291 | /* Deliver the mount point. */ |
292 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size); |
292 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size); |
293 | if (retval != EOK) { |
293 | if (retval != EOK) { |
294 | ipc_answer_0(rid, retval); |
294 | ipc_answer_0(rid, retval); |
295 | free(mp); |
295 | free(mp); |
296 | return; |
296 | return; |
297 | } |
297 | } |
298 | mp[size] = '\0'; |
298 | mp[size] = '\0'; |
299 | 299 | ||
300 | /* Now we expect to receive the mount options. */ |
300 | /* Now we expect to receive the mount options. */ |
301 | if (!ipc_data_write_receive(&callid, &size)) { |
301 | if (!ipc_data_write_receive(&callid, &size)) { |
302 | ipc_answer_0(callid, EINVAL); |
302 | ipc_answer_0(callid, EINVAL); |
303 | ipc_answer_0(rid, EINVAL); |
303 | ipc_answer_0(rid, EINVAL); |
304 | free(mp); |
304 | free(mp); |
305 | return; |
305 | return; |
306 | } |
306 | } |
307 | 307 | ||
308 | /* Check the offered options size. */ |
308 | /* Check the offered options size. */ |
309 | if (size < 0 || size > MAX_MNTOPTS_LEN) { |
309 | if (size < 0 || size > MAX_MNTOPTS_LEN) { |
310 | ipc_answer_0(callid, EINVAL); |
310 | ipc_answer_0(callid, EINVAL); |
311 | ipc_answer_0(rid, EINVAL); |
311 | ipc_answer_0(rid, EINVAL); |
312 | free(mp); |
312 | free(mp); |
313 | return; |
313 | return; |
314 | } |
314 | } |
315 | 315 | ||
316 | /* Allocate buffer for the mount options. */ |
316 | /* Allocate buffer for the mount options. */ |
317 | char *opts = (char *) malloc(size + 1); |
317 | char *opts = (char *) malloc(size + 1); |
318 | if (!opts) { |
318 | if (!opts) { |
319 | ipc_answer_0(callid, ENOMEM); |
319 | ipc_answer_0(callid, ENOMEM); |
320 | ipc_answer_0(rid, ENOMEM); |
320 | ipc_answer_0(rid, ENOMEM); |
321 | free(mp); |
321 | free(mp); |
322 | return; |
322 | return; |
323 | } |
323 | } |
324 | 324 | ||
325 | /* Deliver the mount options. */ |
325 | /* Deliver the mount options. */ |
326 | retval = ipc_data_write_finalize(callid, opts, size); |
326 | retval = ipc_data_write_finalize(callid, opts, size); |
327 | if (retval != EOK) { |
327 | if (retval != EOK) { |
328 | ipc_answer_0(rid, retval); |
328 | ipc_answer_0(rid, retval); |
329 | free(mp); |
329 | free(mp); |
330 | free(opts); |
330 | free(opts); |
331 | return; |
331 | return; |
332 | } |
332 | } |
333 | opts[size] = '\0'; |
333 | opts[size] = '\0'; |
334 | 334 | ||
335 | /* |
335 | /* |
336 | * Now, we expect the client to send us data with the name of the file |
336 | * Now, we expect the client to send us data with the name of the file |
337 | * system. |
337 | * system. |
338 | */ |
338 | */ |
339 | if (!ipc_data_write_receive(&callid, &size)) { |
339 | if (!ipc_data_write_receive(&callid, &size)) { |
340 | ipc_answer_0(callid, EINVAL); |
340 | ipc_answer_0(callid, EINVAL); |
341 | ipc_answer_0(rid, EINVAL); |
341 | ipc_answer_0(rid, EINVAL); |
342 | free(mp); |
342 | free(mp); |
343 | free(opts); |
343 | free(opts); |
344 | return; |
344 | return; |
345 | } |
345 | } |
346 | 346 | ||
347 | /* |
347 | /* |
348 | * Don't receive more than is necessary for storing a full file system |
348 | * Don't receive more than is necessary for storing a full file system |
349 | * name. |
349 | * name. |
350 | */ |
350 | */ |
351 | if ((size < 1) || (size > FS_NAME_MAXLEN)) { |
351 | if ((size < 1) || (size > FS_NAME_MAXLEN)) { |
352 | ipc_answer_0(callid, EINVAL); |
352 | ipc_answer_0(callid, EINVAL); |
353 | ipc_answer_0(rid, EINVAL); |
353 | ipc_answer_0(rid, EINVAL); |
354 | free(mp); |
354 | free(mp); |
355 | free(opts); |
355 | free(opts); |
356 | return; |
356 | return; |
357 | } |
357 | } |
358 | 358 | ||
359 | /* |
359 | /* |
360 | * Allocate buffer for file system name. |
360 | * Allocate buffer for file system name. |
361 | */ |
361 | */ |
362 | char *fs_name = (char *) malloc(size + 1); |
362 | char *fs_name = (char *) malloc(size + 1); |
363 | if (fs_name == NULL) { |
363 | if (fs_name == NULL) { |
364 | ipc_answer_0(callid, ENOMEM); |
364 | ipc_answer_0(callid, ENOMEM); |
365 | ipc_answer_0(rid, ENOMEM); |
365 | ipc_answer_0(rid, ENOMEM); |
366 | free(mp); |
366 | free(mp); |
367 | free(opts); |
367 | free(opts); |
368 | return; |
368 | return; |
369 | } |
369 | } |
370 | 370 | ||
371 | /* Deliver the file system name. */ |
371 | /* Deliver the file system name. */ |
372 | retval = ipc_data_write_finalize(callid, fs_name, size); |
372 | retval = ipc_data_write_finalize(callid, fs_name, size); |
373 | if (retval != EOK) { |
373 | if (retval != EOK) { |
374 | ipc_answer_0(rid, retval); |
374 | ipc_answer_0(rid, retval); |
375 | free(mp); |
375 | free(mp); |
376 | free(opts); |
376 | free(opts); |
377 | free(fs_name); |
377 | free(fs_name); |
378 | return; |
378 | return; |
379 | } |
379 | } |
380 | fs_name[size] = '\0'; |
380 | fs_name[size] = '\0'; |
381 | 381 | ||
382 | /* |
382 | /* |
383 | * Wait for IPC_M_PING so that we can return an error if we don't know |
383 | * Wait for IPC_M_PING so that we can return an error if we don't know |
384 | * fs_name. |
384 | * fs_name. |
385 | */ |
385 | */ |
386 | ipc_call_t data; |
386 | ipc_call_t data; |
387 | callid = async_get_call(&data); |
387 | callid = async_get_call(&data); |
388 | if (IPC_GET_METHOD(data) != IPC_M_PING) { |
388 | if (IPC_GET_METHOD(data) != IPC_M_PING) { |
389 | ipc_answer_0(callid, ENOTSUP); |
389 | ipc_answer_0(callid, ENOTSUP); |
390 | ipc_answer_0(rid, ENOTSUP); |
390 | ipc_answer_0(rid, ENOTSUP); |
391 | free(mp); |
391 | free(mp); |
392 | free(opts); |
392 | free(opts); |
393 | free(fs_name); |
393 | free(fs_name); |
394 | return; |
394 | return; |
395 | } |
395 | } |
396 | 396 | ||
397 | /* |
397 | /* |
398 | * Check if we know a file system with the same name as is in fs_name. |
398 | * Check if we know a file system with the same name as is in fs_name. |
399 | * This will also give us its file system handle. |
399 | * This will also give us its file system handle. |
400 | */ |
400 | */ |
401 | fibril_mutex_lock(&fs_head_lock); |
401 | fibril_mutex_lock(&fs_head_lock); |
402 | fs_handle_t fs_handle; |
402 | fs_handle_t fs_handle; |
403 | recheck: |
403 | recheck: |
404 | fs_handle = fs_name_to_handle(fs_name, false); |
404 | fs_handle = fs_name_to_handle(fs_name, false); |
405 | if (!fs_handle) { |
405 | if (!fs_handle) { |
406 | if (flags & IPC_FLAG_BLOCKING) { |
406 | if (flags & IPC_FLAG_BLOCKING) { |
407 | fibril_condvar_wait(&fs_head_cv, &fs_head_lock); |
407 | fibril_condvar_wait(&fs_head_cv, &fs_head_lock); |
408 | goto recheck; |
408 | goto recheck; |
409 | } |
409 | } |
410 | 410 | ||
411 | fibril_mutex_unlock(&fs_head_lock); |
411 | fibril_mutex_unlock(&fs_head_lock); |
412 | ipc_answer_0(callid, ENOENT); |
412 | ipc_answer_0(callid, ENOENT); |
413 | ipc_answer_0(rid, ENOENT); |
413 | ipc_answer_0(rid, ENOENT); |
414 | free(mp); |
414 | free(mp); |
415 | free(fs_name); |
415 | free(fs_name); |
416 | free(opts); |
416 | free(opts); |
417 | return; |
417 | return; |
418 | } |
418 | } |
419 | fibril_mutex_unlock(&fs_head_lock); |
419 | fibril_mutex_unlock(&fs_head_lock); |
420 | 420 | ||
421 | /* Acknowledge that we know fs_name. */ |
421 | /* Acknowledge that we know fs_name. */ |
422 | ipc_answer_0(callid, EOK); |
422 | ipc_answer_0(callid, EOK); |
423 | 423 | ||
424 | /* Do the mount */ |
424 | /* Do the mount */ |
425 | vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts); |
425 | vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts); |
426 | free(mp); |
426 | free(mp); |
427 | free(fs_name); |
427 | free(fs_name); |
428 | free(opts); |
428 | free(opts); |
429 | } |
429 | } |
430 | 430 | ||
431 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
431 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
432 | { |
432 | { |
433 | if (!vfs_files_init()) { |
433 | if (!vfs_files_init()) { |
434 | ipc_answer_0(rid, ENOMEM); |
434 | ipc_answer_0(rid, ENOMEM); |
435 | return; |
435 | return; |
436 | } |
436 | } |
437 | 437 | ||
438 | /* |
438 | /* |
439 | * The POSIX interface is open(path, oflag, mode). |
439 | * The POSIX interface is open(path, oflag, mode). |
440 | * We can receive oflags and mode along with the VFS_IN_OPEN call; |
440 | * We can receive oflags and mode along with the VFS_IN_OPEN call; |
441 | * the path will need to arrive in another call. |
441 | * the path will need to arrive in another call. |
442 | * |
442 | * |
443 | * We also receive one private, non-POSIX set of flags called lflag |
443 | * We also receive one private, non-POSIX set of flags called lflag |
444 | * used to pass information to vfs_lookup_internal(). |
444 | * used to pass information to vfs_lookup_internal(). |
445 | */ |
445 | */ |
446 | int lflag = IPC_GET_ARG1(*request); |
446 | int lflag = IPC_GET_ARG1(*request); |
447 | int oflag = IPC_GET_ARG2(*request); |
447 | int oflag = IPC_GET_ARG2(*request); |
448 | int mode = IPC_GET_ARG3(*request); |
448 | int mode = IPC_GET_ARG3(*request); |
449 | size_t len; |
449 | size_t len; |
450 | 450 | ||
451 | /* |
451 | /* |
452 | * Make sure that we are called with exactly one of L_FILE and |
452 | * Make sure that we are called with exactly one of L_FILE and |
453 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN. |
453 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN. |
454 | */ |
454 | */ |
455 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
455 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
456 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
456 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
457 | ((lflag & L_OPEN) != 0)) { |
457 | ((lflag & L_OPEN) != 0)) { |
458 | ipc_answer_0(rid, EINVAL); |
458 | ipc_answer_0(rid, EINVAL); |
459 | return; |
459 | return; |
460 | } |
460 | } |
461 | 461 | ||
462 | if (oflag & O_CREAT) |
462 | if (oflag & O_CREAT) |
463 | lflag |= L_CREATE; |
463 | lflag |= L_CREATE; |
464 | if (oflag & O_EXCL) |
464 | if (oflag & O_EXCL) |
465 | lflag |= L_EXCLUSIVE; |
465 | lflag |= L_EXCLUSIVE; |
466 | 466 | ||
467 | ipc_callid_t callid; |
467 | ipc_callid_t callid; |
468 | if (!ipc_data_write_receive(&callid, &len)) { |
468 | if (!ipc_data_write_receive(&callid, &len)) { |
469 | ipc_answer_0(callid, EINVAL); |
469 | ipc_answer_0(callid, EINVAL); |
470 | ipc_answer_0(rid, EINVAL); |
470 | ipc_answer_0(rid, EINVAL); |
471 | return; |
471 | return; |
472 | } |
472 | } |
473 | 473 | ||
474 | char *path = malloc(len + 1); |
474 | char *path = malloc(len + 1); |
475 | if (!path) { |
475 | if (!path) { |
476 | ipc_answer_0(callid, ENOMEM); |
476 | ipc_answer_0(callid, ENOMEM); |
477 | ipc_answer_0(rid, ENOMEM); |
477 | ipc_answer_0(rid, ENOMEM); |
478 | return; |
478 | return; |
479 | } |
479 | } |
480 | 480 | ||
481 | int rc; |
481 | int rc; |
482 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
482 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
483 | ipc_answer_0(rid, rc); |
483 | ipc_answer_0(rid, rc); |
484 | free(path); |
484 | free(path); |
485 | return; |
485 | return; |
486 | } |
486 | } |
487 | path[len] = '\0'; |
487 | path[len] = '\0'; |
488 | 488 | ||
489 | /* |
489 | /* |
490 | * Avoid the race condition in which the file can be deleted before we |
490 | * Avoid the race condition in which the file can be deleted before we |
491 | * find/create-and-lock the VFS node corresponding to the looked-up |
491 | * find/create-and-lock the VFS node corresponding to the looked-up |
492 | * triplet. |
492 | * triplet. |
493 | */ |
493 | */ |
494 | if (lflag & L_CREATE) |
494 | if (lflag & L_CREATE) |
495 | fibril_rwlock_write_lock(&namespace_rwlock); |
495 | fibril_rwlock_write_lock(&namespace_rwlock); |
496 | else |
496 | else |
497 | fibril_rwlock_read_lock(&namespace_rwlock); |
497 | fibril_rwlock_read_lock(&namespace_rwlock); |
498 | 498 | ||
499 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
499 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
500 | vfs_lookup_res_t lr; |
500 | vfs_lookup_res_t lr; |
501 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
501 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
502 | if (rc != EOK) { |
502 | if (rc != EOK) { |
503 | if (lflag & L_CREATE) |
503 | if (lflag & L_CREATE) |
504 | fibril_rwlock_write_unlock(&namespace_rwlock); |
504 | fibril_rwlock_write_unlock(&namespace_rwlock); |
505 | else |
505 | else |
506 | fibril_rwlock_read_unlock(&namespace_rwlock); |
506 | fibril_rwlock_read_unlock(&namespace_rwlock); |
507 | ipc_answer_0(rid, rc); |
507 | ipc_answer_0(rid, rc); |
508 | free(path); |
508 | free(path); |
509 | return; |
509 | return; |
510 | } |
510 | } |
511 | 511 | ||
512 | /* Path is no longer needed. */ |
512 | /* Path is no longer needed. */ |
513 | free(path); |
513 | free(path); |
514 | 514 | ||
515 | vfs_node_t *node = vfs_node_get(&lr); |
515 | vfs_node_t *node = vfs_node_get(&lr); |
516 | if (lflag & L_CREATE) |
516 | if (lflag & L_CREATE) |
517 | fibril_rwlock_write_unlock(&namespace_rwlock); |
517 | fibril_rwlock_write_unlock(&namespace_rwlock); |
518 | else |
518 | else |
519 | fibril_rwlock_read_unlock(&namespace_rwlock); |
519 | fibril_rwlock_read_unlock(&namespace_rwlock); |
520 | 520 | ||
521 | /* Truncate the file if requested and if necessary. */ |
521 | /* Truncate the file if requested and if necessary. */ |
522 | if (oflag & O_TRUNC) { |
522 | if (oflag & O_TRUNC) { |
523 | fibril_rwlock_write_lock(&node->contents_rwlock); |
523 | fibril_rwlock_write_lock(&node->contents_rwlock); |
524 | if (node->size) { |
524 | if (node->size) { |
525 | rc = vfs_truncate_internal(node->fs_handle, |
525 | rc = vfs_truncate_internal(node->fs_handle, |
526 | node->dev_handle, node->index, 0); |
526 | node->dev_handle, node->index, 0); |
527 | if (rc) { |
527 | if (rc) { |
528 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
528 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
529 | vfs_node_put(node); |
529 | vfs_node_put(node); |
530 | ipc_answer_0(rid, rc); |
530 | ipc_answer_0(rid, rc); |
531 | return; |
531 | return; |
532 | } |
532 | } |
533 | node->size = 0; |
533 | node->size = 0; |
534 | } |
534 | } |
535 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
535 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
536 | } |
536 | } |
537 | 537 | ||
538 | /* |
538 | /* |
539 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
539 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
540 | * structure. |
540 | * structure. |
541 | */ |
541 | */ |
542 | int fd = vfs_fd_alloc(); |
542 | int fd = vfs_fd_alloc(); |
543 | if (fd < 0) { |
543 | if (fd < 0) { |
544 | vfs_node_put(node); |
544 | vfs_node_put(node); |
545 | ipc_answer_0(rid, fd); |
545 | ipc_answer_0(rid, fd); |
546 | return; |
546 | return; |
547 | } |
547 | } |
548 | vfs_file_t *file = vfs_file_get(fd); |
548 | vfs_file_t *file = vfs_file_get(fd); |
549 | file->node = node; |
549 | file->node = node; |
550 | if (oflag & O_APPEND) |
550 | if (oflag & O_APPEND) |
551 | file->append = true; |
551 | file->append = true; |
552 | 552 | ||
553 | /* |
553 | /* |
554 | * The following increase in reference count is for the fact that the |
554 | * The following increase in reference count is for the fact that the |
555 | * file is being opened and that a file structure is pointing to it. |
555 | * file is being opened and that a file structure is pointing to it. |
556 | * It is necessary so that the file will not disappear when |
556 | * It is necessary so that the file will not disappear when |
557 | * vfs_node_put() is called. The reference will be dropped by the |
557 | * vfs_node_put() is called. The reference will be dropped by the |
558 | * respective VFS_IN_CLOSE. |
558 | * respective VFS_IN_CLOSE. |
559 | */ |
559 | */ |
560 | vfs_node_addref(node); |
560 | vfs_node_addref(node); |
561 | vfs_node_put(node); |
561 | vfs_node_put(node); |
562 | 562 | ||
563 | /* Success! Return the new file descriptor to the client. */ |
563 | /* Success! Return the new file descriptor to the client. */ |
564 | ipc_answer_1(rid, EOK, fd); |
564 | ipc_answer_1(rid, EOK, fd); |
565 | } |
565 | } |
566 | 566 | ||
567 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
567 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
568 | { |
568 | { |
569 | // FIXME: check for sanity of the supplied fs, dev and index |
569 | // FIXME: check for sanity of the supplied fs, dev and index |
570 | 570 | ||
571 | if (!vfs_files_init()) { |
571 | if (!vfs_files_init()) { |
572 | ipc_answer_0(rid, ENOMEM); |
572 | ipc_answer_0(rid, ENOMEM); |
573 | return; |
573 | return; |
574 | } |
574 | } |
575 | 575 | ||
576 | /* |
576 | /* |
577 | * The interface is open_node(fs, dev, index, oflag). |
577 | * The interface is open_node(fs, dev, index, oflag). |
578 | */ |
578 | */ |
579 | vfs_lookup_res_t lr; |
579 | vfs_lookup_res_t lr; |
580 | 580 | ||
581 | lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
581 | lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
582 | lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
582 | lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
583 | lr.triplet.index = IPC_GET_ARG3(*request); |
583 | lr.triplet.index = IPC_GET_ARG3(*request); |
584 | int oflag = IPC_GET_ARG4(*request); |
584 | int oflag = IPC_GET_ARG4(*request); |
585 | 585 | ||
586 | fibril_rwlock_read_lock(&namespace_rwlock); |
586 | fibril_rwlock_read_lock(&namespace_rwlock); |
587 | 587 | ||
588 | int rc = vfs_open_node_internal(&lr); |
588 | int rc = vfs_open_node_internal(&lr); |
589 | if (rc != EOK) { |
589 | if (rc != EOK) { |
590 | fibril_rwlock_read_unlock(&namespace_rwlock); |
590 | fibril_rwlock_read_unlock(&namespace_rwlock); |
591 | ipc_answer_0(rid, rc); |
591 | ipc_answer_0(rid, rc); |
592 | return; |
592 | return; |
593 | } |
593 | } |
594 | 594 | ||
595 | vfs_node_t *node = vfs_node_get(&lr); |
595 | vfs_node_t *node = vfs_node_get(&lr); |
596 | fibril_rwlock_read_unlock(&namespace_rwlock); |
596 | fibril_rwlock_read_unlock(&namespace_rwlock); |
597 | 597 | ||
598 | /* Truncate the file if requested and if necessary. */ |
598 | /* Truncate the file if requested and if necessary. */ |
599 | if (oflag & O_TRUNC) { |
599 | if (oflag & O_TRUNC) { |
600 | fibril_rwlock_write_lock(&node->contents_rwlock); |
600 | fibril_rwlock_write_lock(&node->contents_rwlock); |
601 | if (node->size) { |
601 | if (node->size) { |
602 | rc = vfs_truncate_internal(node->fs_handle, |
602 | rc = vfs_truncate_internal(node->fs_handle, |
603 | node->dev_handle, node->index, 0); |
603 | node->dev_handle, node->index, 0); |
604 | if (rc) { |
604 | if (rc) { |
605 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
605 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
606 | vfs_node_put(node); |
606 | vfs_node_put(node); |
607 | ipc_answer_0(rid, rc); |
607 | ipc_answer_0(rid, rc); |
608 | return; |
608 | return; |
609 | } |
609 | } |
610 | node->size = 0; |
610 | node->size = 0; |
611 | } |
611 | } |
612 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
612 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
613 | } |
613 | } |
614 | 614 | ||
615 | /* |
615 | /* |
616 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
616 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
617 | * structure. |
617 | * structure. |
618 | */ |
618 | */ |
619 | int fd = vfs_fd_alloc(); |
619 | int fd = vfs_fd_alloc(); |
620 | if (fd < 0) { |
620 | if (fd < 0) { |
621 | vfs_node_put(node); |
621 | vfs_node_put(node); |
622 | ipc_answer_0(rid, fd); |
622 | ipc_answer_0(rid, fd); |
623 | return; |
623 | return; |
624 | } |
624 | } |
625 | vfs_file_t *file = vfs_file_get(fd); |
625 | vfs_file_t *file = vfs_file_get(fd); |
626 | file->node = node; |
626 | file->node = node; |
627 | if (oflag & O_APPEND) |
627 | if (oflag & O_APPEND) |
628 | file->append = true; |
628 | file->append = true; |
629 | 629 | ||
630 | /* |
630 | /* |
631 | * The following increase in reference count is for the fact that the |
631 | * The following increase in reference count is for the fact that the |
632 | * file is being opened and that a file structure is pointing to it. |
632 | * file is being opened and that a file structure is pointing to it. |
633 | * It is necessary so that the file will not disappear when |
633 | * It is necessary so that the file will not disappear when |
634 | * vfs_node_put() is called. The reference will be dropped by the |
634 | * vfs_node_put() is called. The reference will be dropped by the |
635 | * respective VFS_IN_CLOSE. |
635 | * respective VFS_IN_CLOSE. |
636 | */ |
636 | */ |
637 | vfs_node_addref(node); |
637 | vfs_node_addref(node); |
638 | vfs_node_put(node); |
638 | vfs_node_put(node); |
639 | 639 | ||
640 | /* Success! Return the new file descriptor to the client. */ |
640 | /* Success! Return the new file descriptor to the client. */ |
641 | ipc_answer_1(rid, EOK, fd); |
641 | ipc_answer_1(rid, EOK, fd); |
642 | } |
642 | } |
643 | 643 | ||
644 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
644 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
645 | { |
645 | { |
646 | int fd = IPC_GET_ARG1(*request); |
646 | int fd = IPC_GET_ARG1(*request); |
647 | 647 | ||
648 | /* Lookup the file structure corresponding to the file descriptor. */ |
648 | /* Lookup the file structure corresponding to the file descriptor. */ |
649 | vfs_file_t *file = vfs_file_get(fd); |
649 | vfs_file_t *file = vfs_file_get(fd); |
650 | if (!file) { |
650 | if (!file) { |
651 | ipc_answer_0(rid, ENOENT); |
651 | ipc_answer_0(rid, ENOENT); |
652 | return; |
652 | return; |
653 | } |
653 | } |
654 | 654 | ||
655 | /* |
655 | /* |
656 | * Lock the open file structure so that no other thread can manipulate |
656 | * Lock the open file structure so that no other thread can manipulate |
657 | * the same open file at a time. |
657 | * the same open file at a time. |
658 | */ |
658 | */ |
659 | fibril_mutex_lock(&file->lock); |
659 | fibril_mutex_lock(&file->lock); |
660 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
660 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
661 | 661 | ||
662 | /* Make a VFS_OUT_SYMC request at the destination FS server. */ |
662 | /* Make a VFS_OUT_SYMC request at the destination FS server. */ |
663 | aid_t msg; |
663 | aid_t msg; |
664 | ipc_call_t answer; |
664 | ipc_call_t answer; |
665 | msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle, |
665 | msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle, |
666 | file->node->index, &answer); |
666 | file->node->index, &answer); |
667 | 667 | ||
668 | /* Wait for reply from the FS server. */ |
668 | /* Wait for reply from the FS server. */ |
669 | ipcarg_t rc; |
669 | ipcarg_t rc; |
670 | async_wait_for(msg, &rc); |
670 | async_wait_for(msg, &rc); |
671 | 671 | ||
672 | vfs_release_phone(fs_phone); |
672 | vfs_release_phone(fs_phone); |
673 | fibril_mutex_unlock(&file->lock); |
673 | fibril_mutex_unlock(&file->lock); |
674 | 674 | ||
675 | ipc_answer_0(rid, rc); |
675 | ipc_answer_0(rid, rc); |
676 | } |
676 | } |
677 | 677 | ||
678 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
678 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
679 | { |
679 | { |
680 | int fd = IPC_GET_ARG1(*request); |
680 | int fd = IPC_GET_ARG1(*request); |
681 | 681 | ||
682 | /* Lookup the file structure corresponding to the file descriptor. */ |
682 | /* Lookup the file structure corresponding to the file descriptor. */ |
683 | vfs_file_t *file = vfs_file_get(fd); |
683 | vfs_file_t *file = vfs_file_get(fd); |
684 | if (!file) { |
684 | if (!file) { |
685 | ipc_answer_0(rid, ENOENT); |
685 | ipc_answer_0(rid, ENOENT); |
686 | return; |
686 | return; |
687 | } |
687 | } |
688 | 688 | ||
689 | /* |
689 | /* |
690 | * Lock the open file structure so that no other thread can manipulate |
690 | * Lock the open file structure so that no other thread can manipulate |
691 | * the same open file at a time. |
691 | * the same open file at a time. |
692 | */ |
692 | */ |
693 | fibril_mutex_lock(&file->lock); |
693 | fibril_mutex_lock(&file->lock); |
694 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
694 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
695 | 695 | ||
696 | /* Make a VFS_OUT_CLOSE request at the destination FS server. */ |
696 | /* Make a VFS_OUT_CLOSE request at the destination FS server. */ |
697 | aid_t msg; |
697 | aid_t msg; |
698 | ipc_call_t answer; |
698 | ipc_call_t answer; |
699 | msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle, |
699 | msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle, |
700 | file->node->index, &answer); |
700 | file->node->index, &answer); |
701 | 701 | ||
702 | /* Wait for reply from the FS server. */ |
702 | /* Wait for reply from the FS server. */ |
703 | ipcarg_t rc; |
703 | ipcarg_t rc; |
704 | async_wait_for(msg, &rc); |
704 | async_wait_for(msg, &rc); |
705 | 705 | ||
706 | vfs_release_phone(fs_phone); |
706 | vfs_release_phone(fs_phone); |
707 | fibril_mutex_unlock(&file->lock); |
707 | fibril_mutex_unlock(&file->lock); |
708 | 708 | ||
709 | int retval = IPC_GET_ARG1(answer); |
709 | int retval = IPC_GET_ARG1(answer); |
710 | if (retval != EOK) |
710 | if (retval != EOK) |
711 | ipc_answer_0(rid, retval); |
711 | ipc_answer_0(rid, retval); |
712 | 712 | ||
713 | retval = vfs_fd_free(fd); |
713 | retval = vfs_fd_free(fd); |
714 | ipc_answer_0(rid, retval); |
714 | ipc_answer_0(rid, retval); |
715 | } |
715 | } |
716 | 716 | ||
717 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
717 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
718 | { |
718 | { |
719 | 719 | ||
720 | /* |
720 | /* |
721 | * The following code strongly depends on the fact that the files data |
721 | * The following code strongly depends on the fact that the files data |
722 | * structure can be only accessed by a single fibril and all file |
722 | * structure can be only accessed by a single fibril and all file |
723 | * operations are serialized (i.e. the reads and writes cannot |
723 | * operations are serialized (i.e. the reads and writes cannot |
724 | * interleave and a file cannot be closed while it is being read). |
724 | * interleave and a file cannot be closed while it is being read). |
725 | * |
725 | * |
726 | * Additional synchronization needs to be added once the table of |
726 | * Additional synchronization needs to be added once the table of |
727 | * open files supports parallel access! |
727 | * open files supports parallel access! |
728 | */ |
728 | */ |
729 | 729 | ||
730 | int fd = IPC_GET_ARG1(*request); |
730 | int fd = IPC_GET_ARG1(*request); |
731 | 731 | ||
732 | /* Lookup the file structure corresponding to the file descriptor. */ |
732 | /* Lookup the file structure corresponding to the file descriptor. */ |
733 | vfs_file_t *file = vfs_file_get(fd); |
733 | vfs_file_t *file = vfs_file_get(fd); |
734 | if (!file) { |
734 | if (!file) { |
735 | ipc_answer_0(rid, ENOENT); |
735 | ipc_answer_0(rid, ENOENT); |
736 | return; |
736 | return; |
737 | } |
737 | } |
738 | 738 | ||
739 | /* |
739 | /* |
740 | * Now we need to receive a call with client's |
740 | * Now we need to receive a call with client's |
741 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
741 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
742 | */ |
742 | */ |
743 | ipc_callid_t callid; |
743 | ipc_callid_t callid; |
744 | int res; |
744 | int res; |
745 | if (read) |
745 | if (read) |
746 | res = ipc_data_read_receive(&callid, NULL); |
746 | res = ipc_data_read_receive(&callid, NULL); |
747 | else |
747 | else |
748 | res = ipc_data_write_receive(&callid, NULL); |
748 | res = ipc_data_write_receive(&callid, NULL); |
749 | if (!res) { |
749 | if (!res) { |
750 | ipc_answer_0(callid, EINVAL); |
750 | ipc_answer_0(callid, EINVAL); |
751 | ipc_answer_0(rid, EINVAL); |
751 | ipc_answer_0(rid, EINVAL); |
752 | return; |
752 | return; |
753 | } |
753 | } |
754 | 754 | ||
755 | /* |
755 | /* |
756 | * Lock the open file structure so that no other thread can manipulate |
756 | * Lock the open file structure so that no other thread can manipulate |
757 | * the same open file at a time. |
757 | * the same open file at a time. |
758 | */ |
758 | */ |
759 | fibril_mutex_lock(&file->lock); |
759 | fibril_mutex_lock(&file->lock); |
760 | 760 | ||
761 | /* |
761 | /* |
762 | * Lock the file's node so that no other client can read/write to it at |
762 | * Lock the file's node so that no other client can read/write to it at |
763 | * the same time. |
763 | * the same time. |
764 | */ |
764 | */ |
765 | if (read) |
765 | if (read) |
766 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
766 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
767 | else |
767 | else |
768 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
768 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
769 | 769 | ||
770 | if (file->node->type == VFS_NODE_DIRECTORY) { |
770 | if (file->node->type == VFS_NODE_DIRECTORY) { |
771 | /* |
771 | /* |
772 | * Make sure that no one is modifying the namespace |
772 | * Make sure that no one is modifying the namespace |
773 | * while we are in readdir(). |
773 | * while we are in readdir(). |
774 | */ |
774 | */ |
775 | assert(read); |
775 | assert(read); |
776 | fibril_rwlock_read_lock(&namespace_rwlock); |
776 | fibril_rwlock_read_lock(&namespace_rwlock); |
777 | } |
777 | } |
778 | 778 | ||
779 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
779 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
780 | 780 | ||
781 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
781 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
782 | aid_t msg; |
782 | aid_t msg; |
783 | ipc_call_t answer; |
783 | ipc_call_t answer; |
784 | if (!read && file->append) |
784 | if (!read && file->append) |
785 | file->pos = file->node->size; |
785 | file->pos = file->node->size; |
786 | msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE, |
786 | msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE, |
787 | file->node->dev_handle, file->node->index, file->pos, &answer); |
787 | file->node->dev_handle, file->node->index, file->pos, &answer); |
788 | 788 | ||
789 | /* |
789 | /* |
790 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
790 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
791 | * destination FS server. The call will be routed as if sent by |
791 | * destination FS server. The call will be routed as if sent by |
792 | * ourselves. Note that call arguments are immutable in this case so we |
792 | * ourselves. Note that call arguments are immutable in this case so we |
793 | * don't have to bother. |
793 | * don't have to bother. |
794 | */ |
794 | */ |
795 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
795 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
796 | 796 | ||
797 | /* Wait for reply from the FS server. */ |
797 | /* Wait for reply from the FS server. */ |
798 | ipcarg_t rc; |
798 | ipcarg_t rc; |
799 | async_wait_for(msg, &rc); |
799 | async_wait_for(msg, &rc); |
800 | 800 | ||
801 | vfs_release_phone(fs_phone); |
801 | vfs_release_phone(fs_phone); |
802 | 802 | ||
803 | size_t bytes = IPC_GET_ARG1(answer); |
803 | size_t bytes = IPC_GET_ARG1(answer); |
804 | 804 | ||
805 | if (file->node->type == VFS_NODE_DIRECTORY) |
805 | if (file->node->type == VFS_NODE_DIRECTORY) |
806 | fibril_rwlock_read_unlock(&namespace_rwlock); |
806 | fibril_rwlock_read_unlock(&namespace_rwlock); |
807 | 807 | ||
808 | /* Unlock the VFS node. */ |
808 | /* Unlock the VFS node. */ |
809 | if (read) |
809 | if (read) |
810 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
810 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
811 | else { |
811 | else { |
812 | /* Update the cached version of node's size. */ |
812 | /* Update the cached version of node's size. */ |
813 | if (rc == EOK) |
813 | if (rc == EOK) |
814 | file->node->size = IPC_GET_ARG2(answer); |
814 | file->node->size = IPC_GET_ARG2(answer); |
815 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
815 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
816 | } |
816 | } |
817 | 817 | ||
818 | /* Update the position pointer and unlock the open file. */ |
818 | /* Update the position pointer and unlock the open file. */ |
819 | if (rc == EOK) |
819 | if (rc == EOK) |
820 | file->pos += bytes; |
820 | file->pos += bytes; |
821 | fibril_mutex_unlock(&file->lock); |
821 | fibril_mutex_unlock(&file->lock); |
822 | 822 | ||
823 | /* |
823 | /* |
824 | * FS server's reply is the final result of the whole operation we |
824 | * FS server's reply is the final result of the whole operation we |
825 | * return to the client. |
825 | * return to the client. |
826 | */ |
826 | */ |
827 | ipc_answer_1(rid, rc, bytes); |
827 | ipc_answer_1(rid, rc, bytes); |
828 | } |
828 | } |
829 | 829 | ||
830 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
830 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
831 | { |
831 | { |
832 | vfs_rdwr(rid, request, true); |
832 | vfs_rdwr(rid, request, true); |
833 | } |
833 | } |
834 | 834 | ||
835 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
835 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
836 | { |
836 | { |
837 | vfs_rdwr(rid, request, false); |
837 | vfs_rdwr(rid, request, false); |
838 | } |
838 | } |
839 | 839 | ||
840 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
840 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
841 | { |
841 | { |
842 | int fd = (int) IPC_GET_ARG1(*request); |
842 | int fd = (int) IPC_GET_ARG1(*request); |
843 | off_t off = (off_t) IPC_GET_ARG2(*request); |
843 | off_t off = (off_t) IPC_GET_ARG2(*request); |
844 | int whence = (int) IPC_GET_ARG3(*request); |
844 | int whence = (int) IPC_GET_ARG3(*request); |
845 | 845 | ||
846 | 846 | ||
847 | /* Lookup the file structure corresponding to the file descriptor. */ |
847 | /* Lookup the file structure corresponding to the file descriptor. */ |
848 | vfs_file_t *file = vfs_file_get(fd); |
848 | vfs_file_t *file = vfs_file_get(fd); |
849 | if (!file) { |
849 | if (!file) { |
850 | ipc_answer_0(rid, ENOENT); |
850 | ipc_answer_0(rid, ENOENT); |
851 | return; |
851 | return; |
852 | } |
852 | } |
853 | 853 | ||
854 | off_t newpos; |
854 | off_t newpos; |
855 | fibril_mutex_lock(&file->lock); |
855 | fibril_mutex_lock(&file->lock); |
856 | if (whence == SEEK_SET) { |
856 | if (whence == SEEK_SET) { |
857 | file->pos = off; |
857 | file->pos = off; |
858 | fibril_mutex_unlock(&file->lock); |
858 | fibril_mutex_unlock(&file->lock); |
859 | ipc_answer_1(rid, EOK, off); |
859 | ipc_answer_1(rid, EOK, off); |
860 | return; |
860 | return; |
861 | } |
861 | } |
862 | if (whence == SEEK_CUR) { |
862 | if (whence == SEEK_CUR) { |
863 | if (file->pos + off < file->pos) { |
863 | if (file->pos + off < file->pos) { |
864 | fibril_mutex_unlock(&file->lock); |
864 | fibril_mutex_unlock(&file->lock); |
865 | ipc_answer_0(rid, EOVERFLOW); |
865 | ipc_answer_0(rid, EOVERFLOW); |
866 | return; |
866 | return; |
867 | } |
867 | } |
868 | file->pos += off; |
868 | file->pos += off; |
869 | newpos = file->pos; |
869 | newpos = file->pos; |
870 | fibril_mutex_unlock(&file->lock); |
870 | fibril_mutex_unlock(&file->lock); |
871 | ipc_answer_1(rid, EOK, newpos); |
871 | ipc_answer_1(rid, EOK, newpos); |
872 | return; |
872 | return; |
873 | } |
873 | } |
874 | if (whence == SEEK_END) { |
874 | if (whence == SEEK_END) { |
875 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
875 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
876 | size_t size = file->node->size; |
876 | size_t size = file->node->size; |
877 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
877 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
878 | if (size + off < size) { |
878 | if (size + off < size) { |
879 | fibril_mutex_unlock(&file->lock); |
879 | fibril_mutex_unlock(&file->lock); |
880 | ipc_answer_0(rid, EOVERFLOW); |
880 | ipc_answer_0(rid, EOVERFLOW); |
881 | return; |
881 | return; |
882 | } |
882 | } |
883 | newpos = size + off; |
883 | newpos = size + off; |
884 | fibril_mutex_unlock(&file->lock); |
884 | fibril_mutex_unlock(&file->lock); |
885 | ipc_answer_1(rid, EOK, newpos); |
885 | ipc_answer_1(rid, EOK, newpos); |
886 | return; |
886 | return; |
887 | } |
887 | } |
888 | fibril_mutex_unlock(&file->lock); |
888 | fibril_mutex_unlock(&file->lock); |
889 | ipc_answer_0(rid, EINVAL); |
889 | ipc_answer_0(rid, EINVAL); |
890 | } |
890 | } |
891 | 891 | ||
892 | int |
892 | int |
893 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
893 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
894 | fs_index_t index, size_t size) |
894 | fs_index_t index, size_t size) |
895 | { |
895 | { |
896 | ipcarg_t rc; |
896 | ipcarg_t rc; |
897 | int fs_phone; |
897 | int fs_phone; |
898 | 898 | ||
899 | fs_phone = vfs_grab_phone(fs_handle); |
899 | fs_phone = vfs_grab_phone(fs_handle); |
900 | rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle, |
900 | rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle, |
901 | (ipcarg_t)index, (ipcarg_t)size); |
901 | (ipcarg_t)index, (ipcarg_t)size); |
902 | vfs_release_phone(fs_phone); |
902 | vfs_release_phone(fs_phone); |
903 | return (int)rc; |
903 | return (int)rc; |
904 | } |
904 | } |
905 | 905 | ||
906 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
906 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
907 | { |
907 | { |
908 | int fd = IPC_GET_ARG1(*request); |
908 | int fd = IPC_GET_ARG1(*request); |
909 | size_t size = IPC_GET_ARG2(*request); |
909 | size_t size = IPC_GET_ARG2(*request); |
910 | int rc; |
910 | int rc; |
911 | 911 | ||
912 | vfs_file_t *file = vfs_file_get(fd); |
912 | vfs_file_t *file = vfs_file_get(fd); |
913 | if (!file) { |
913 | if (!file) { |
914 | ipc_answer_0(rid, ENOENT); |
914 | ipc_answer_0(rid, ENOENT); |
915 | return; |
915 | return; |
916 | } |
916 | } |
917 | fibril_mutex_lock(&file->lock); |
917 | fibril_mutex_lock(&file->lock); |
918 | 918 | ||
919 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
919 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
920 | rc = vfs_truncate_internal(file->node->fs_handle, |
920 | rc = vfs_truncate_internal(file->node->fs_handle, |
921 | file->node->dev_handle, file->node->index, size); |
921 | file->node->dev_handle, file->node->index, size); |
922 | if (rc == EOK) |
922 | if (rc == EOK) |
923 | file->node->size = size; |
923 | file->node->size = size; |
924 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
924 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
925 | 925 | ||
926 | fibril_mutex_unlock(&file->lock); |
926 | fibril_mutex_unlock(&file->lock); |
927 | ipc_answer_0(rid, (ipcarg_t)rc); |
927 | ipc_answer_0(rid, (ipcarg_t)rc); |
928 | } |
928 | } |
929 | 929 | ||
930 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request) |
930 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request) |
931 | { |
931 | { |
932 | int fd = IPC_GET_ARG1(*request); |
932 | int fd = IPC_GET_ARG1(*request); |
933 | size_t size = IPC_GET_ARG2(*request); |
933 | size_t size = IPC_GET_ARG2(*request); |
934 | ipcarg_t rc; |
934 | ipcarg_t rc; |
935 | 935 | ||
936 | vfs_file_t *file = vfs_file_get(fd); |
936 | vfs_file_t *file = vfs_file_get(fd); |
937 | if (!file) { |
937 | if (!file) { |
938 | ipc_answer_0(rid, ENOENT); |
938 | ipc_answer_0(rid, ENOENT); |
939 | return; |
939 | return; |
940 | } |
940 | } |
941 | 941 | ||
942 | ipc_callid_t callid; |
942 | ipc_callid_t callid; |
943 | if (!ipc_data_read_receive(&callid, NULL)) { |
943 | if (!ipc_data_read_receive(&callid, NULL)) { |
944 | ipc_answer_0(callid, EINVAL); |
944 | ipc_answer_0(callid, EINVAL); |
945 | ipc_answer_0(rid, EINVAL); |
945 | ipc_answer_0(rid, EINVAL); |
946 | return; |
946 | return; |
947 | } |
947 | } |
948 | 948 | ||
949 | fibril_mutex_lock(&file->lock); |
949 | fibril_mutex_lock(&file->lock); |
950 | 950 | ||
951 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
951 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
952 | 952 | ||
953 | aid_t msg; |
953 | aid_t msg; |
954 | msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle, |
954 | msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle, |
955 | file->node->index, true, NULL); |
955 | file->node->index, true, NULL); |
956 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
956 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
957 | async_wait_for(msg, &rc); |
957 | async_wait_for(msg, &rc); |
958 | vfs_release_phone(fs_phone); |
958 | vfs_release_phone(fs_phone); |
959 | 959 | ||
960 | fibril_mutex_unlock(&file->lock); |
960 | fibril_mutex_unlock(&file->lock); |
961 | ipc_answer_0(rid, rc); |
961 | ipc_answer_0(rid, rc); |
962 | } |
962 | } |
963 | 963 | ||
964 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request) |
964 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request) |
965 | { |
965 | { |
- | 966 | size_t len; |
|
- | 967 | ipc_callid_t callid; |
|
- | 968 | ||
- | 969 | if (!ipc_data_write_receive(&callid, &len)) { |
|
- | 970 | ipc_answer_0(callid, EINVAL); |
|
- | 971 | ipc_answer_0(rid, EINVAL); |
|
- | 972 | return; |
|
- | 973 | } |
|
- | 974 | char *path = malloc(len + 1); |
|
- | 975 | if (!path) { |
|
- | 976 | ipc_answer_0(callid, ENOMEM); |
|
- | 977 | ipc_answer_0(rid, ENOMEM); |
|
- | 978 | return; |
|
- | 979 | } |
|
- | 980 | int rc; |
|
- | 981 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
|
- | 982 | ipc_answer_0(rid, rc); |
|
- | 983 | free(path); |
|
- | 984 | return; |
|
- | 985 | } |
|
- | 986 | path[len] = '\0'; |
|
- | 987 | ||
- | 988 | if (!ipc_data_read_receive(&callid, NULL)) { |
|
- | 989 | free(path); |
|
- | 990 | ipc_answer_0(callid, EINVAL); |
|
- | 991 | ipc_answer_0(rid, EINVAL); |
|
- | 992 | return; |
|
- | 993 | } |
|
- | 994 | ||
- | 995 | vfs_lookup_res_t lr; |
|
- | 996 | fibril_rwlock_read_lock(&namespace_rwlock); |
|
- | 997 | rc = vfs_lookup_internal(path, L_NONE, &lr, NULL); |
|
- | 998 | free(path); |
|
- | 999 | if (rc != EOK) { |
|
- | 1000 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
- | 1001 | ipc_answer_0(callid, rc); |
|
- | 1002 | ipc_answer_0(rid, rc); |
|
- | 1003 | return; |
|
- | 1004 | } |
|
- | 1005 | vfs_node_t *node = vfs_node_get(&lr); |
|
- | 1006 | if (!node) { |
|
- | 1007 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
- | 1008 | ipc_answer_0(callid, ENOMEM); |
|
- | 1009 | ipc_answer_0(rid, ENOMEM); |
|
- | 1010 | return; |
|
- | 1011 | } |
|
- | 1012 | ||
- | 1013 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
- | 1014 | ||
- | 1015 | int fs_phone = vfs_grab_phone(node->fs_handle); |
|
- | 1016 | aid_t msg; |
|
- | 1017 | msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle, |
|
- | 1018 | node->index, false, NULL); |
|
- | 1019 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
|
- | 1020 | async_wait_for(msg, &rc); |
|
- | 1021 | vfs_release_phone(fs_phone); |
|
- | 1022 | ||
- | 1023 | ipc_answer_0(rid, rc); |
|
- | 1024 | ||
- | 1025 | vfs_node_put(node); |
|
966 | } |
1026 | } |
967 | 1027 | ||
968 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
1028 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
969 | { |
1029 | { |
970 | int mode = IPC_GET_ARG1(*request); |
1030 | int mode = IPC_GET_ARG1(*request); |
971 | 1031 | ||
972 | size_t len; |
1032 | size_t len; |
973 | ipc_callid_t callid; |
1033 | ipc_callid_t callid; |
974 | 1034 | ||
975 | if (!ipc_data_write_receive(&callid, &len)) { |
1035 | if (!ipc_data_write_receive(&callid, &len)) { |
976 | ipc_answer_0(callid, EINVAL); |
1036 | ipc_answer_0(callid, EINVAL); |
977 | ipc_answer_0(rid, EINVAL); |
1037 | ipc_answer_0(rid, EINVAL); |
978 | return; |
1038 | return; |
979 | } |
1039 | } |
980 | char *path = malloc(len + 1); |
1040 | char *path = malloc(len + 1); |
981 | if (!path) { |
1041 | if (!path) { |
982 | ipc_answer_0(callid, ENOMEM); |
1042 | ipc_answer_0(callid, ENOMEM); |
983 | ipc_answer_0(rid, ENOMEM); |
1043 | ipc_answer_0(rid, ENOMEM); |
984 | return; |
1044 | return; |
985 | } |
1045 | } |
986 | int rc; |
1046 | int rc; |
987 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
1047 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
988 | ipc_answer_0(rid, rc); |
1048 | ipc_answer_0(rid, rc); |
989 | free(path); |
1049 | free(path); |
990 | return; |
1050 | return; |
991 | } |
1051 | } |
992 | path[len] = '\0'; |
1052 | path[len] = '\0'; |
993 | 1053 | ||
994 | fibril_rwlock_write_lock(&namespace_rwlock); |
1054 | fibril_rwlock_write_lock(&namespace_rwlock); |
995 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
1055 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
996 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
1056 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
997 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1057 | fibril_rwlock_write_unlock(&namespace_rwlock); |
998 | free(path); |
1058 | free(path); |
999 | ipc_answer_0(rid, rc); |
1059 | ipc_answer_0(rid, rc); |
1000 | } |
1060 | } |
1001 | 1061 | ||
1002 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
1062 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
1003 | { |
1063 | { |
1004 | int lflag = IPC_GET_ARG1(*request); |
1064 | int lflag = IPC_GET_ARG1(*request); |
1005 | 1065 | ||
1006 | size_t len; |
1066 | size_t len; |
1007 | ipc_callid_t callid; |
1067 | ipc_callid_t callid; |
1008 | 1068 | ||
1009 | if (!ipc_data_write_receive(&callid, &len)) { |
1069 | if (!ipc_data_write_receive(&callid, &len)) { |
1010 | ipc_answer_0(callid, EINVAL); |
1070 | ipc_answer_0(callid, EINVAL); |
1011 | ipc_answer_0(rid, EINVAL); |
1071 | ipc_answer_0(rid, EINVAL); |
1012 | return; |
1072 | return; |
1013 | } |
1073 | } |
1014 | char *path = malloc(len + 1); |
1074 | char *path = malloc(len + 1); |
1015 | if (!path) { |
1075 | if (!path) { |
1016 | ipc_answer_0(callid, ENOMEM); |
1076 | ipc_answer_0(callid, ENOMEM); |
1017 | ipc_answer_0(rid, ENOMEM); |
1077 | ipc_answer_0(rid, ENOMEM); |
1018 | return; |
1078 | return; |
1019 | } |
1079 | } |
1020 | int rc; |
1080 | int rc; |
1021 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
1081 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
1022 | ipc_answer_0(rid, rc); |
1082 | ipc_answer_0(rid, rc); |
1023 | free(path); |
1083 | free(path); |
1024 | return; |
1084 | return; |
1025 | } |
1085 | } |
1026 | path[len] = '\0'; |
1086 | path[len] = '\0'; |
1027 | 1087 | ||
1028 | fibril_rwlock_write_lock(&namespace_rwlock); |
1088 | fibril_rwlock_write_lock(&namespace_rwlock); |
1029 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
1089 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
1030 | vfs_lookup_res_t lr; |
1090 | vfs_lookup_res_t lr; |
1031 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
1091 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
1032 | free(path); |
1092 | free(path); |
1033 | if (rc != EOK) { |
1093 | if (rc != EOK) { |
1034 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1094 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1035 | ipc_answer_0(rid, rc); |
1095 | ipc_answer_0(rid, rc); |
1036 | return; |
1096 | return; |
1037 | } |
1097 | } |
1038 | 1098 | ||
1039 | /* |
1099 | /* |
1040 | * The name has already been unlinked by vfs_lookup_internal(). |
1100 | * The name has already been unlinked by vfs_lookup_internal(). |
1041 | * We have to get and put the VFS node to ensure that it is |
1101 | * We have to get and put the VFS node to ensure that it is |
1042 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped. |
1102 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped. |
1043 | */ |
1103 | */ |
1044 | vfs_node_t *node = vfs_node_get(&lr); |
1104 | vfs_node_t *node = vfs_node_get(&lr); |
1045 | fibril_mutex_lock(&nodes_mutex); |
1105 | fibril_mutex_lock(&nodes_mutex); |
1046 | node->lnkcnt--; |
1106 | node->lnkcnt--; |
1047 | fibril_mutex_unlock(&nodes_mutex); |
1107 | fibril_mutex_unlock(&nodes_mutex); |
1048 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1108 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1049 | vfs_node_put(node); |
1109 | vfs_node_put(node); |
1050 | ipc_answer_0(rid, EOK); |
1110 | ipc_answer_0(rid, EOK); |
1051 | } |
1111 | } |
1052 | 1112 | ||
1053 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
1113 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
1054 | { |
1114 | { |
1055 | size_t olen, nlen; |
1115 | size_t olen, nlen; |
1056 | ipc_callid_t callid; |
1116 | ipc_callid_t callid; |
1057 | int rc; |
1117 | int rc; |
1058 | 1118 | ||
1059 | /* Retrieve the old path. */ |
1119 | /* Retrieve the old path. */ |
1060 | if (!ipc_data_write_receive(&callid, &olen)) { |
1120 | if (!ipc_data_write_receive(&callid, &olen)) { |
1061 | ipc_answer_0(callid, EINVAL); |
1121 | ipc_answer_0(callid, EINVAL); |
1062 | ipc_answer_0(rid, EINVAL); |
1122 | ipc_answer_0(rid, EINVAL); |
1063 | return; |
1123 | return; |
1064 | } |
1124 | } |
1065 | char *old = malloc(olen + 1); |
1125 | char *old = malloc(olen + 1); |
1066 | if (!old) { |
1126 | if (!old) { |
1067 | ipc_answer_0(callid, ENOMEM); |
1127 | ipc_answer_0(callid, ENOMEM); |
1068 | ipc_answer_0(rid, ENOMEM); |
1128 | ipc_answer_0(rid, ENOMEM); |
1069 | return; |
1129 | return; |
1070 | } |
1130 | } |
1071 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
1131 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
1072 | ipc_answer_0(rid, rc); |
1132 | ipc_answer_0(rid, rc); |
1073 | free(old); |
1133 | free(old); |
1074 | return; |
1134 | return; |
1075 | } |
1135 | } |
1076 | old[olen] = '\0'; |
1136 | old[olen] = '\0'; |
1077 | 1137 | ||
1078 | /* Retrieve the new path. */ |
1138 | /* Retrieve the new path. */ |
1079 | if (!ipc_data_write_receive(&callid, &nlen)) { |
1139 | if (!ipc_data_write_receive(&callid, &nlen)) { |
1080 | ipc_answer_0(callid, EINVAL); |
1140 | ipc_answer_0(callid, EINVAL); |
1081 | ipc_answer_0(rid, EINVAL); |
1141 | ipc_answer_0(rid, EINVAL); |
1082 | free(old); |
1142 | free(old); |
1083 | return; |
1143 | return; |
1084 | } |
1144 | } |
1085 | char *new = malloc(nlen + 1); |
1145 | char *new = malloc(nlen + 1); |
1086 | if (!new) { |
1146 | if (!new) { |
1087 | ipc_answer_0(callid, ENOMEM); |
1147 | ipc_answer_0(callid, ENOMEM); |
1088 | ipc_answer_0(rid, ENOMEM); |
1148 | ipc_answer_0(rid, ENOMEM); |
1089 | free(old); |
1149 | free(old); |
1090 | return; |
1150 | return; |
1091 | } |
1151 | } |
1092 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
1152 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
1093 | ipc_answer_0(rid, rc); |
1153 | ipc_answer_0(rid, rc); |
1094 | free(old); |
1154 | free(old); |
1095 | free(new); |
1155 | free(new); |
1096 | return; |
1156 | return; |
1097 | } |
1157 | } |
1098 | new[nlen] = '\0'; |
1158 | new[nlen] = '\0'; |
1099 | 1159 | ||
1100 | char *oldc = canonify(old, &olen); |
1160 | char *oldc = canonify(old, &olen); |
1101 | char *newc = canonify(new, &nlen); |
1161 | char *newc = canonify(new, &nlen); |
1102 | if (!oldc || !newc) { |
1162 | if (!oldc || !newc) { |
1103 | ipc_answer_0(rid, EINVAL); |
1163 | ipc_answer_0(rid, EINVAL); |
1104 | free(old); |
1164 | free(old); |
1105 | free(new); |
1165 | free(new); |
1106 | return; |
1166 | return; |
1107 | } |
1167 | } |
1108 | oldc[olen] = '\0'; |
1168 | oldc[olen] = '\0'; |
1109 | newc[nlen] = '\0'; |
1169 | newc[nlen] = '\0'; |
1110 | if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
1170 | if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
1111 | ((newc[str_length(oldc)] == '/') || |
1171 | ((newc[str_length(oldc)] == '/') || |
1112 | (str_length(oldc) == 1) || |
1172 | (str_length(oldc) == 1) || |
1113 | (str_length(oldc) == str_length(newc)))) { |
1173 | (str_length(oldc) == str_length(newc)))) { |
1114 | /* |
1174 | /* |
1115 | * oldc is a prefix of newc and either |
1175 | * oldc is a prefix of newc and either |
1116 | * - newc continues with a / where oldc ends, or |
1176 | * - newc continues with a / where oldc ends, or |
1117 | * - oldc was / itself, or |
1177 | * - oldc was / itself, or |
1118 | * - oldc and newc are equal. |
1178 | * - oldc and newc are equal. |
1119 | */ |
1179 | */ |
1120 | ipc_answer_0(rid, EINVAL); |
1180 | ipc_answer_0(rid, EINVAL); |
1121 | free(old); |
1181 | free(old); |
1122 | free(new); |
1182 | free(new); |
1123 | return; |
1183 | return; |
1124 | } |
1184 | } |
1125 | 1185 | ||
1126 | vfs_lookup_res_t old_lr; |
1186 | vfs_lookup_res_t old_lr; |
1127 | vfs_lookup_res_t new_lr; |
1187 | vfs_lookup_res_t new_lr; |
1128 | vfs_lookup_res_t new_par_lr; |
1188 | vfs_lookup_res_t new_par_lr; |
1129 | fibril_rwlock_write_lock(&namespace_rwlock); |
1189 | fibril_rwlock_write_lock(&namespace_rwlock); |
1130 | /* Lookup the node belonging to the old file name. */ |
1190 | /* Lookup the node belonging to the old file name. */ |
1131 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
1191 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
1132 | if (rc != EOK) { |
1192 | if (rc != EOK) { |
1133 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1193 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1134 | ipc_answer_0(rid, rc); |
1194 | ipc_answer_0(rid, rc); |
1135 | free(old); |
1195 | free(old); |
1136 | free(new); |
1196 | free(new); |
1137 | return; |
1197 | return; |
1138 | } |
1198 | } |
1139 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
1199 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
1140 | if (!old_node) { |
1200 | if (!old_node) { |
1141 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1201 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1142 | ipc_answer_0(rid, ENOMEM); |
1202 | ipc_answer_0(rid, ENOMEM); |
1143 | free(old); |
1203 | free(old); |
1144 | free(new); |
1204 | free(new); |
1145 | return; |
1205 | return; |
1146 | } |
1206 | } |
1147 | /* Determine the path to the parent of the node with the new name. */ |
1207 | /* Determine the path to the parent of the node with the new name. */ |
1148 | char *parentc = str_dup(newc); |
1208 | char *parentc = str_dup(newc); |
1149 | if (!parentc) { |
1209 | if (!parentc) { |
1150 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1210 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1151 | ipc_answer_0(rid, rc); |
1211 | ipc_answer_0(rid, rc); |
1152 | free(old); |
1212 | free(old); |
1153 | free(new); |
1213 | free(new); |
1154 | return; |
1214 | return; |
1155 | } |
1215 | } |
1156 | char *lastsl = str_rchr(parentc + 1, '/'); |
1216 | char *lastsl = str_rchr(parentc + 1, '/'); |
1157 | if (lastsl) |
1217 | if (lastsl) |
1158 | *lastsl = '\0'; |
1218 | *lastsl = '\0'; |
1159 | else |
1219 | else |
1160 | parentc[1] = '\0'; |
1220 | parentc[1] = '\0'; |
1161 | /* Lookup parent of the new file name. */ |
1221 | /* Lookup parent of the new file name. */ |
1162 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
1222 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
1163 | free(parentc); /* not needed anymore */ |
1223 | free(parentc); /* not needed anymore */ |
1164 | if (rc != EOK) { |
1224 | if (rc != EOK) { |
1165 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1225 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1166 | ipc_answer_0(rid, rc); |
1226 | ipc_answer_0(rid, rc); |
1167 | free(old); |
1227 | free(old); |
1168 | free(new); |
1228 | free(new); |
1169 | return; |
1229 | return; |
1170 | } |
1230 | } |
1171 | /* Check whether linking to the same file system instance. */ |
1231 | /* Check whether linking to the same file system instance. */ |
1172 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
1232 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
1173 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
1233 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
1174 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1234 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1175 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
1235 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
1176 | free(old); |
1236 | free(old); |
1177 | free(new); |
1237 | free(new); |
1178 | return; |
1238 | return; |
1179 | } |
1239 | } |
1180 | /* Destroy the old link for the new name. */ |
1240 | /* Destroy the old link for the new name. */ |
1181 | vfs_node_t *new_node = NULL; |
1241 | vfs_node_t *new_node = NULL; |
1182 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
1242 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
1183 | switch (rc) { |
1243 | switch (rc) { |
1184 | case ENOENT: |
1244 | case ENOENT: |
1185 | /* simply not in our way */ |
1245 | /* simply not in our way */ |
1186 | break; |
1246 | break; |
1187 | case EOK: |
1247 | case EOK: |
1188 | new_node = vfs_node_get(&new_lr); |
1248 | new_node = vfs_node_get(&new_lr); |
1189 | if (!new_node) { |
1249 | if (!new_node) { |
1190 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1250 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1191 | ipc_answer_0(rid, ENOMEM); |
1251 | ipc_answer_0(rid, ENOMEM); |
1192 | free(old); |
1252 | free(old); |
1193 | free(new); |
1253 | free(new); |
1194 | return; |
1254 | return; |
1195 | } |
1255 | } |
1196 | fibril_mutex_lock(&nodes_mutex); |
1256 | fibril_mutex_lock(&nodes_mutex); |
1197 | new_node->lnkcnt--; |
1257 | new_node->lnkcnt--; |
1198 | fibril_mutex_unlock(&nodes_mutex); |
1258 | fibril_mutex_unlock(&nodes_mutex); |
1199 | break; |
1259 | break; |
1200 | default: |
1260 | default: |
1201 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1261 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1202 | ipc_answer_0(rid, ENOTEMPTY); |
1262 | ipc_answer_0(rid, ENOTEMPTY); |
1203 | free(old); |
1263 | free(old); |
1204 | free(new); |
1264 | free(new); |
1205 | return; |
1265 | return; |
1206 | } |
1266 | } |
1207 | /* Create the new link for the new name. */ |
1267 | /* Create the new link for the new name. */ |
1208 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
1268 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
1209 | if (rc != EOK) { |
1269 | if (rc != EOK) { |
1210 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1270 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1211 | if (new_node) |
1271 | if (new_node) |
1212 | vfs_node_put(new_node); |
1272 | vfs_node_put(new_node); |
1213 | ipc_answer_0(rid, rc); |
1273 | ipc_answer_0(rid, rc); |
1214 | free(old); |
1274 | free(old); |
1215 | free(new); |
1275 | free(new); |
1216 | return; |
1276 | return; |
1217 | } |
1277 | } |
1218 | fibril_mutex_lock(&nodes_mutex); |
1278 | fibril_mutex_lock(&nodes_mutex); |
1219 | old_node->lnkcnt++; |
1279 | old_node->lnkcnt++; |
1220 | fibril_mutex_unlock(&nodes_mutex); |
1280 | fibril_mutex_unlock(&nodes_mutex); |
1221 | /* Destroy the link for the old name. */ |
1281 | /* Destroy the link for the old name. */ |
1222 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
1282 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
1223 | if (rc != EOK) { |
1283 | if (rc != EOK) { |
1224 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1284 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1225 | vfs_node_put(old_node); |
1285 | vfs_node_put(old_node); |
1226 | if (new_node) |
1286 | if (new_node) |
1227 | vfs_node_put(new_node); |
1287 | vfs_node_put(new_node); |
1228 | ipc_answer_0(rid, rc); |
1288 | ipc_answer_0(rid, rc); |
1229 | free(old); |
1289 | free(old); |
1230 | free(new); |
1290 | free(new); |
1231 | return; |
1291 | return; |
1232 | } |
1292 | } |
1233 | fibril_mutex_lock(&nodes_mutex); |
1293 | fibril_mutex_lock(&nodes_mutex); |
1234 | old_node->lnkcnt--; |
1294 | old_node->lnkcnt--; |
1235 | fibril_mutex_unlock(&nodes_mutex); |
1295 | fibril_mutex_unlock(&nodes_mutex); |
1236 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1296 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1237 | vfs_node_put(old_node); |
1297 | vfs_node_put(old_node); |
1238 | if (new_node) |
1298 | if (new_node) |
1239 | vfs_node_put(new_node); |
1299 | vfs_node_put(new_node); |
1240 | free(old); |
1300 | free(old); |
1241 | free(new); |
1301 | free(new); |
1242 | ipc_answer_0(rid, EOK); |
1302 | ipc_answer_0(rid, EOK); |
1243 | } |
1303 | } |
1244 | 1304 | ||
1245 | /** |
1305 | /** |
1246 | * @} |
1306 | * @} |
1247 | */ |
1307 | */ |
1248 | 1308 |