Rev 4581 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4581 | Rev 4718 | ||
---|---|---|---|
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_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_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_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 > 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_OPEN call; the path |
440 | * We can receive oflags and mode along with the VFS_IN_OPEN call; |
441 | * 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 | ||
- | 451 | /* Ignore mode for now. */ |
|
- | 452 | (void) mode; |
|
450 | 453 | ||
451 | /* |
454 | /* |
452 | * Make sure that we are called with exactly one of L_FILE and |
455 | * 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. |
456 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN. |
454 | */ |
457 | */ |
455 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
458 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
456 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
459 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
457 | ((lflag & L_OPEN) != 0)) { |
460 | ((lflag & L_OPEN) != 0)) { |
458 | ipc_answer_0(rid, EINVAL); |
461 | ipc_answer_0(rid, EINVAL); |
459 | return; |
462 | return; |
460 | } |
463 | } |
461 | 464 | ||
462 | if (oflag & O_CREAT) |
465 | if (oflag & O_CREAT) |
463 | lflag |= L_CREATE; |
466 | lflag |= L_CREATE; |
464 | if (oflag & O_EXCL) |
467 | if (oflag & O_EXCL) |
465 | lflag |= L_EXCLUSIVE; |
468 | lflag |= L_EXCLUSIVE; |
466 | 469 | ||
467 | ipc_callid_t callid; |
470 | ipc_callid_t callid; |
468 | if (!ipc_data_write_receive(&callid, &len)) { |
471 | if (!ipc_data_write_receive(&callid, &len)) { |
469 | ipc_answer_0(callid, EINVAL); |
472 | ipc_answer_0(callid, EINVAL); |
470 | ipc_answer_0(rid, EINVAL); |
473 | ipc_answer_0(rid, EINVAL); |
471 | return; |
474 | return; |
472 | } |
475 | } |
473 | 476 | ||
474 | char *path = malloc(len + 1); |
477 | char *path = malloc(len + 1); |
475 | if (!path) { |
478 | if (!path) { |
476 | ipc_answer_0(callid, ENOMEM); |
479 | ipc_answer_0(callid, ENOMEM); |
477 | ipc_answer_0(rid, ENOMEM); |
480 | ipc_answer_0(rid, ENOMEM); |
478 | return; |
481 | return; |
479 | } |
482 | } |
480 | 483 | ||
481 | int rc; |
484 | int rc; |
482 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
485 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
483 | ipc_answer_0(rid, rc); |
486 | ipc_answer_0(rid, rc); |
484 | free(path); |
487 | free(path); |
485 | return; |
488 | return; |
486 | } |
489 | } |
487 | path[len] = '\0'; |
490 | path[len] = '\0'; |
488 | 491 | ||
489 | /* |
492 | /* |
490 | * Avoid the race condition in which the file can be deleted before we |
493 | * 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 |
494 | * find/create-and-lock the VFS node corresponding to the looked-up |
492 | * triplet. |
495 | * triplet. |
493 | */ |
496 | */ |
494 | if (lflag & L_CREATE) |
497 | if (lflag & L_CREATE) |
495 | fibril_rwlock_write_lock(&namespace_rwlock); |
498 | fibril_rwlock_write_lock(&namespace_rwlock); |
496 | else |
499 | else |
497 | fibril_rwlock_read_lock(&namespace_rwlock); |
500 | fibril_rwlock_read_lock(&namespace_rwlock); |
498 | 501 | ||
499 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
502 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
500 | vfs_lookup_res_t lr; |
503 | vfs_lookup_res_t lr; |
501 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
504 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
502 | if (rc != EOK) { |
505 | if (rc != EOK) { |
503 | if (lflag & L_CREATE) |
506 | if (lflag & L_CREATE) |
504 | fibril_rwlock_write_unlock(&namespace_rwlock); |
507 | fibril_rwlock_write_unlock(&namespace_rwlock); |
505 | else |
508 | else |
506 | fibril_rwlock_read_unlock(&namespace_rwlock); |
509 | fibril_rwlock_read_unlock(&namespace_rwlock); |
507 | ipc_answer_0(rid, rc); |
510 | ipc_answer_0(rid, rc); |
508 | free(path); |
511 | free(path); |
509 | return; |
512 | return; |
510 | } |
513 | } |
511 | 514 | ||
512 | /* Path is no longer needed. */ |
515 | /* Path is no longer needed. */ |
513 | free(path); |
516 | free(path); |
514 | 517 | ||
515 | vfs_node_t *node = vfs_node_get(&lr); |
518 | vfs_node_t *node = vfs_node_get(&lr); |
516 | if (lflag & L_CREATE) |
519 | if (lflag & L_CREATE) |
517 | fibril_rwlock_write_unlock(&namespace_rwlock); |
520 | fibril_rwlock_write_unlock(&namespace_rwlock); |
518 | else |
521 | else |
519 | fibril_rwlock_read_unlock(&namespace_rwlock); |
522 | fibril_rwlock_read_unlock(&namespace_rwlock); |
520 | 523 | ||
521 | /* Truncate the file if requested and if necessary. */ |
524 | /* Truncate the file if requested and if necessary. */ |
522 | if (oflag & O_TRUNC) { |
525 | if (oflag & O_TRUNC) { |
523 | fibril_rwlock_write_lock(&node->contents_rwlock); |
526 | fibril_rwlock_write_lock(&node->contents_rwlock); |
524 | if (node->size) { |
527 | if (node->size) { |
525 | rc = vfs_truncate_internal(node->fs_handle, |
528 | rc = vfs_truncate_internal(node->fs_handle, |
526 | node->dev_handle, node->index, 0); |
529 | node->dev_handle, node->index, 0); |
527 | if (rc) { |
530 | if (rc) { |
528 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
531 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
529 | vfs_node_put(node); |
532 | vfs_node_put(node); |
530 | ipc_answer_0(rid, rc); |
533 | ipc_answer_0(rid, rc); |
531 | return; |
534 | return; |
532 | } |
535 | } |
533 | node->size = 0; |
536 | node->size = 0; |
534 | } |
537 | } |
535 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
538 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
536 | } |
539 | } |
537 | 540 | ||
538 | /* |
541 | /* |
539 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
542 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
540 | * structure. |
543 | * structure. |
541 | */ |
544 | */ |
542 | int fd = vfs_fd_alloc(); |
545 | int fd = vfs_fd_alloc(); |
543 | if (fd < 0) { |
546 | if (fd < 0) { |
544 | vfs_node_put(node); |
547 | vfs_node_put(node); |
545 | ipc_answer_0(rid, fd); |
548 | ipc_answer_0(rid, fd); |
546 | return; |
549 | return; |
547 | } |
550 | } |
548 | vfs_file_t *file = vfs_file_get(fd); |
551 | vfs_file_t *file = vfs_file_get(fd); |
549 | file->node = node; |
552 | file->node = node; |
550 | if (oflag & O_APPEND) |
553 | if (oflag & O_APPEND) |
551 | file->append = true; |
554 | file->append = true; |
552 | 555 | ||
553 | /* |
556 | /* |
554 | * The following increase in reference count is for the fact that the |
557 | * 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. |
558 | * 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 |
559 | * It is necessary so that the file will not disappear when |
557 | * vfs_node_put() is called. The reference will be dropped by the |
560 | * vfs_node_put() is called. The reference will be dropped by the |
558 | * respective VFS_CLOSE. |
561 | * respective VFS_IN_CLOSE. |
559 | */ |
562 | */ |
560 | vfs_node_addref(node); |
563 | vfs_node_addref(node); |
561 | vfs_node_put(node); |
564 | vfs_node_put(node); |
562 | 565 | ||
563 | /* Success! Return the new file descriptor to the client. */ |
566 | /* Success! Return the new file descriptor to the client. */ |
564 | ipc_answer_1(rid, EOK, fd); |
567 | ipc_answer_1(rid, EOK, fd); |
565 | } |
568 | } |
566 | 569 | ||
567 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
570 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
568 | { |
571 | { |
569 | // FIXME: check for sanity of the supplied fs, dev and index |
572 | // FIXME: check for sanity of the supplied fs, dev and index |
570 | 573 | ||
571 | if (!vfs_files_init()) { |
574 | if (!vfs_files_init()) { |
572 | ipc_answer_0(rid, ENOMEM); |
575 | ipc_answer_0(rid, ENOMEM); |
573 | return; |
576 | return; |
574 | } |
577 | } |
575 | 578 | ||
576 | /* |
579 | /* |
577 | * The interface is open_node(fs, dev, index, oflag). |
580 | * The interface is open_node(fs, dev, index, oflag). |
578 | */ |
581 | */ |
579 | vfs_lookup_res_t lr; |
582 | vfs_lookup_res_t lr; |
580 | 583 | ||
581 | lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
584 | lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
582 | lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
585 | lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
583 | lr.triplet.index = IPC_GET_ARG3(*request); |
586 | lr.triplet.index = IPC_GET_ARG3(*request); |
584 | int oflag = IPC_GET_ARG4(*request); |
587 | int oflag = IPC_GET_ARG4(*request); |
585 | 588 | ||
586 | fibril_rwlock_read_lock(&namespace_rwlock); |
589 | fibril_rwlock_read_lock(&namespace_rwlock); |
587 | 590 | ||
588 | int rc = vfs_open_node_internal(&lr); |
591 | int rc = vfs_open_node_internal(&lr); |
589 | if (rc != EOK) { |
592 | if (rc != EOK) { |
590 | fibril_rwlock_read_unlock(&namespace_rwlock); |
593 | fibril_rwlock_read_unlock(&namespace_rwlock); |
591 | ipc_answer_0(rid, rc); |
594 | ipc_answer_0(rid, rc); |
592 | return; |
595 | return; |
593 | } |
596 | } |
594 | 597 | ||
595 | vfs_node_t *node = vfs_node_get(&lr); |
598 | vfs_node_t *node = vfs_node_get(&lr); |
596 | fibril_rwlock_read_unlock(&namespace_rwlock); |
599 | fibril_rwlock_read_unlock(&namespace_rwlock); |
597 | 600 | ||
598 | /* Truncate the file if requested and if necessary. */ |
601 | /* Truncate the file if requested and if necessary. */ |
599 | if (oflag & O_TRUNC) { |
602 | if (oflag & O_TRUNC) { |
600 | fibril_rwlock_write_lock(&node->contents_rwlock); |
603 | fibril_rwlock_write_lock(&node->contents_rwlock); |
601 | if (node->size) { |
604 | if (node->size) { |
602 | rc = vfs_truncate_internal(node->fs_handle, |
605 | rc = vfs_truncate_internal(node->fs_handle, |
603 | node->dev_handle, node->index, 0); |
606 | node->dev_handle, node->index, 0); |
604 | if (rc) { |
607 | if (rc) { |
605 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
608 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
606 | vfs_node_put(node); |
609 | vfs_node_put(node); |
607 | ipc_answer_0(rid, rc); |
610 | ipc_answer_0(rid, rc); |
608 | return; |
611 | return; |
609 | } |
612 | } |
610 | node->size = 0; |
613 | node->size = 0; |
611 | } |
614 | } |
612 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
615 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
613 | } |
616 | } |
614 | 617 | ||
615 | /* |
618 | /* |
616 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
619 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
617 | * structure. |
620 | * structure. |
618 | */ |
621 | */ |
619 | int fd = vfs_fd_alloc(); |
622 | int fd = vfs_fd_alloc(); |
620 | if (fd < 0) { |
623 | if (fd < 0) { |
621 | vfs_node_put(node); |
624 | vfs_node_put(node); |
622 | ipc_answer_0(rid, fd); |
625 | ipc_answer_0(rid, fd); |
623 | return; |
626 | return; |
624 | } |
627 | } |
625 | vfs_file_t *file = vfs_file_get(fd); |
628 | vfs_file_t *file = vfs_file_get(fd); |
626 | file->node = node; |
629 | file->node = node; |
627 | if (oflag & O_APPEND) |
630 | if (oflag & O_APPEND) |
628 | file->append = true; |
631 | file->append = true; |
629 | 632 | ||
630 | /* |
633 | /* |
631 | * The following increase in reference count is for the fact that the |
634 | * 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. |
635 | * 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 |
636 | * It is necessary so that the file will not disappear when |
634 | * vfs_node_put() is called. The reference will be dropped by the |
637 | * vfs_node_put() is called. The reference will be dropped by the |
635 | * respective VFS_CLOSE. |
638 | * respective VFS_IN_CLOSE. |
636 | */ |
639 | */ |
637 | vfs_node_addref(node); |
640 | vfs_node_addref(node); |
638 | vfs_node_put(node); |
641 | vfs_node_put(node); |
639 | 642 | ||
640 | /* Success! Return the new file descriptor to the client. */ |
643 | /* Success! Return the new file descriptor to the client. */ |
641 | ipc_answer_1(rid, EOK, fd); |
644 | ipc_answer_1(rid, EOK, fd); |
642 | } |
645 | } |
643 | 646 | ||
644 | void vfs_node(ipc_callid_t rid, ipc_call_t *request) |
- | |
645 | { |
- | |
646 | int fd = IPC_GET_ARG1(*request); |
- | |
647 | - | ||
648 | /* Lookup the file structure corresponding to the file descriptor. */ |
- | |
649 | vfs_file_t *file = vfs_file_get(fd); |
- | |
650 | if (!file) { |
- | |
651 | ipc_answer_0(rid, ENOENT); |
- | |
652 | return; |
- | |
653 | } |
- | |
654 | - | ||
655 | ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle, |
- | |
656 | file->node->index); |
- | |
657 | } |
- | |
658 | - | ||
659 | void vfs_device(ipc_callid_t rid, ipc_call_t *request) |
- | |
660 | { |
- | |
661 | int fd = IPC_GET_ARG1(*request); |
- | |
662 | - | ||
663 | /* Lookup the file structure corresponding to the file descriptor. */ |
- | |
664 | vfs_file_t *file = vfs_file_get(fd); |
- | |
665 | if (!file) { |
- | |
666 | ipc_answer_0(rid, ENOENT); |
- | |
667 | return; |
- | |
668 | } |
- | |
669 | - | ||
670 | /* |
- | |
671 | * Lock the open file structure so that no other thread can manipulate |
- | |
672 | * the same open file at a time. |
- | |
673 | */ |
- | |
674 | fibril_mutex_lock(&file->lock); |
- | |
675 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
- | |
676 | - | ||
677 | /* Make a VFS_DEVICE request at the destination FS server. */ |
- | |
678 | aid_t msg; |
- | |
679 | ipc_call_t answer; |
- | |
680 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
- | |
681 | file->node->dev_handle, file->node->index, &answer); |
- | |
682 | - | ||
683 | /* Wait for reply from the FS server. */ |
- | |
684 | ipcarg_t rc; |
- | |
685 | async_wait_for(msg, &rc); |
- | |
686 | - | ||
687 | vfs_release_phone(fs_phone); |
- | |
688 | fibril_mutex_unlock(&file->lock); |
- | |
689 | - | ||
690 | ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer)); |
- | |
691 | } |
- | |
692 | - | ||
693 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
647 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
694 | { |
648 | { |
695 | int fd = IPC_GET_ARG1(*request); |
649 | int fd = IPC_GET_ARG1(*request); |
696 | 650 | ||
697 | /* Lookup the file structure corresponding to the file descriptor. */ |
651 | /* Lookup the file structure corresponding to the file descriptor. */ |
698 | vfs_file_t *file = vfs_file_get(fd); |
652 | vfs_file_t *file = vfs_file_get(fd); |
699 | if (!file) { |
653 | if (!file) { |
700 | ipc_answer_0(rid, ENOENT); |
654 | ipc_answer_0(rid, ENOENT); |
701 | return; |
655 | return; |
702 | } |
656 | } |
703 | 657 | ||
704 | /* |
658 | /* |
705 | * Lock the open file structure so that no other thread can manipulate |
659 | * Lock the open file structure so that no other thread can manipulate |
706 | * the same open file at a time. |
660 | * the same open file at a time. |
707 | */ |
661 | */ |
708 | fibril_mutex_lock(&file->lock); |
662 | fibril_mutex_lock(&file->lock); |
709 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
663 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
710 | 664 | ||
711 | /* Make a VFS_SYMC request at the destination FS server. */ |
665 | /* Make a VFS_OUT_SYMC request at the destination FS server. */ |
712 | aid_t msg; |
666 | aid_t msg; |
713 | ipc_call_t answer; |
667 | ipc_call_t answer; |
714 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
668 | msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle, |
715 | file->node->dev_handle, file->node->index, &answer); |
669 | file->node->index, &answer); |
716 | 670 | ||
717 | /* Wait for reply from the FS server. */ |
671 | /* Wait for reply from the FS server. */ |
718 | ipcarg_t rc; |
672 | ipcarg_t rc; |
719 | async_wait_for(msg, &rc); |
673 | async_wait_for(msg, &rc); |
720 | 674 | ||
721 | vfs_release_phone(fs_phone); |
675 | vfs_release_phone(fs_phone); |
722 | fibril_mutex_unlock(&file->lock); |
676 | fibril_mutex_unlock(&file->lock); |
723 | 677 | ||
724 | ipc_answer_0(rid, rc); |
678 | ipc_answer_0(rid, rc); |
725 | } |
679 | } |
726 | 680 | ||
727 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
681 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
728 | { |
682 | { |
729 | int fd = IPC_GET_ARG1(*request); |
683 | int fd = IPC_GET_ARG1(*request); |
730 | 684 | ||
731 | /* Lookup the file structure corresponding to the file descriptor. */ |
685 | /* Lookup the file structure corresponding to the file descriptor. */ |
732 | vfs_file_t *file = vfs_file_get(fd); |
686 | vfs_file_t *file = vfs_file_get(fd); |
733 | if (!file) { |
687 | if (!file) { |
734 | ipc_answer_0(rid, ENOENT); |
688 | ipc_answer_0(rid, ENOENT); |
735 | return; |
689 | return; |
736 | } |
690 | } |
737 | 691 | ||
738 | /* |
692 | /* |
739 | * Lock the open file structure so that no other thread can manipulate |
693 | * Lock the open file structure so that no other thread can manipulate |
740 | * the same open file at a time. |
694 | * the same open file at a time. |
741 | */ |
695 | */ |
742 | fibril_mutex_lock(&file->lock); |
696 | fibril_mutex_lock(&file->lock); |
743 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
697 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
744 | 698 | ||
745 | /* Make a VFS_CLOSE request at the destination FS server. */ |
699 | /* Make a VFS_OUT_CLOSE request at the destination FS server. */ |
746 | aid_t msg; |
700 | aid_t msg; |
747 | ipc_call_t answer; |
701 | ipc_call_t answer; |
748 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
702 | msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle, |
749 | file->node->dev_handle, file->node->index, &answer); |
703 | file->node->index, &answer); |
750 | 704 | ||
751 | /* Wait for reply from the FS server. */ |
705 | /* Wait for reply from the FS server. */ |
752 | ipcarg_t rc; |
706 | ipcarg_t rc; |
753 | async_wait_for(msg, &rc); |
707 | async_wait_for(msg, &rc); |
754 | 708 | ||
755 | vfs_release_phone(fs_phone); |
709 | vfs_release_phone(fs_phone); |
756 | fibril_mutex_unlock(&file->lock); |
710 | fibril_mutex_unlock(&file->lock); |
757 | 711 | ||
758 | int retval = IPC_GET_ARG1(answer); |
712 | int retval = IPC_GET_ARG1(answer); |
759 | if (retval != EOK) |
713 | if (retval != EOK) |
760 | ipc_answer_0(rid, retval); |
714 | ipc_answer_0(rid, retval); |
761 | 715 | ||
762 | retval = vfs_fd_free(fd); |
716 | retval = vfs_fd_free(fd); |
763 | ipc_answer_0(rid, retval); |
717 | ipc_answer_0(rid, retval); |
764 | } |
718 | } |
765 | 719 | ||
766 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
720 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
767 | { |
721 | { |
768 | 722 | ||
769 | /* |
723 | /* |
770 | * The following code strongly depends on the fact that the files data |
724 | * The following code strongly depends on the fact that the files data |
771 | * structure can be only accessed by a single fibril and all file |
725 | * structure can be only accessed by a single fibril and all file |
772 | * operations are serialized (i.e. the reads and writes cannot |
726 | * operations are serialized (i.e. the reads and writes cannot |
773 | * interleave and a file cannot be closed while it is being read). |
727 | * interleave and a file cannot be closed while it is being read). |
774 | * |
728 | * |
775 | * Additional synchronization needs to be added once the table of |
729 | * Additional synchronization needs to be added once the table of |
776 | * open files supports parallel access! |
730 | * open files supports parallel access! |
777 | */ |
731 | */ |
778 | 732 | ||
779 | int fd = IPC_GET_ARG1(*request); |
733 | int fd = IPC_GET_ARG1(*request); |
780 | 734 | ||
781 | /* Lookup the file structure corresponding to the file descriptor. */ |
735 | /* Lookup the file structure corresponding to the file descriptor. */ |
782 | vfs_file_t *file = vfs_file_get(fd); |
736 | vfs_file_t *file = vfs_file_get(fd); |
783 | if (!file) { |
737 | if (!file) { |
784 | ipc_answer_0(rid, ENOENT); |
738 | ipc_answer_0(rid, ENOENT); |
785 | return; |
739 | return; |
786 | } |
740 | } |
787 | 741 | ||
788 | /* |
742 | /* |
789 | * Now we need to receive a call with client's |
743 | * Now we need to receive a call with client's |
790 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
744 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
791 | */ |
745 | */ |
792 | ipc_callid_t callid; |
746 | ipc_callid_t callid; |
793 | int res; |
747 | int res; |
794 | if (read) |
748 | if (read) |
795 | res = ipc_data_read_receive(&callid, NULL); |
749 | res = ipc_data_read_receive(&callid, NULL); |
796 | else |
750 | else |
797 | res = ipc_data_write_receive(&callid, NULL); |
751 | res = ipc_data_write_receive(&callid, NULL); |
798 | if (!res) { |
752 | if (!res) { |
799 | ipc_answer_0(callid, EINVAL); |
753 | ipc_answer_0(callid, EINVAL); |
800 | ipc_answer_0(rid, EINVAL); |
754 | ipc_answer_0(rid, EINVAL); |
801 | return; |
755 | return; |
802 | } |
756 | } |
803 | 757 | ||
804 | /* |
758 | /* |
805 | * Lock the open file structure so that no other thread can manipulate |
759 | * Lock the open file structure so that no other thread can manipulate |
806 | * the same open file at a time. |
760 | * the same open file at a time. |
807 | */ |
761 | */ |
808 | fibril_mutex_lock(&file->lock); |
762 | fibril_mutex_lock(&file->lock); |
809 | 763 | ||
810 | /* |
764 | /* |
811 | * Lock the file's node so that no other client can read/write to it at |
765 | * Lock the file's node so that no other client can read/write to it at |
812 | * the same time. |
766 | * the same time. |
813 | */ |
767 | */ |
814 | if (read) |
768 | if (read) |
815 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
769 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
816 | else |
770 | else |
817 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
771 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
818 | 772 | ||
819 | if (file->node->type == VFS_NODE_DIRECTORY) { |
773 | if (file->node->type == VFS_NODE_DIRECTORY) { |
820 | /* |
774 | /* |
821 | * Make sure that no one is modifying the namespace |
775 | * Make sure that no one is modifying the namespace |
822 | * while we are in readdir(). |
776 | * while we are in readdir(). |
823 | */ |
777 | */ |
824 | assert(read); |
778 | assert(read); |
825 | fibril_rwlock_read_lock(&namespace_rwlock); |
779 | fibril_rwlock_read_lock(&namespace_rwlock); |
826 | } |
780 | } |
827 | 781 | ||
828 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
782 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
829 | 783 | ||
830 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
784 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
831 | aid_t msg; |
785 | aid_t msg; |
832 | ipc_call_t answer; |
786 | ipc_call_t answer; |
833 | if (!read && file->append) |
787 | if (!read && file->append) |
834 | file->pos = file->node->size; |
788 | file->pos = file->node->size; |
835 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
789 | msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE, |
836 | file->node->dev_handle, file->node->index, file->pos, &answer); |
790 | file->node->dev_handle, file->node->index, file->pos, &answer); |
837 | 791 | ||
838 | /* |
792 | /* |
839 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
793 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
840 | * destination FS server. The call will be routed as if sent by |
794 | * destination FS server. The call will be routed as if sent by |
841 | * ourselves. Note that call arguments are immutable in this case so we |
795 | * ourselves. Note that call arguments are immutable in this case so we |
842 | * don't have to bother. |
796 | * don't have to bother. |
843 | */ |
797 | */ |
844 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
798 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
845 | 799 | ||
846 | /* Wait for reply from the FS server. */ |
800 | /* Wait for reply from the FS server. */ |
847 | ipcarg_t rc; |
801 | ipcarg_t rc; |
848 | async_wait_for(msg, &rc); |
802 | async_wait_for(msg, &rc); |
849 | 803 | ||
850 | vfs_release_phone(fs_phone); |
804 | vfs_release_phone(fs_phone); |
851 | 805 | ||
852 | size_t bytes = IPC_GET_ARG1(answer); |
806 | size_t bytes = IPC_GET_ARG1(answer); |
853 | 807 | ||
854 | if (file->node->type == VFS_NODE_DIRECTORY) |
808 | if (file->node->type == VFS_NODE_DIRECTORY) |
855 | fibril_rwlock_read_unlock(&namespace_rwlock); |
809 | fibril_rwlock_read_unlock(&namespace_rwlock); |
856 | 810 | ||
857 | /* Unlock the VFS node. */ |
811 | /* Unlock the VFS node. */ |
858 | if (read) |
812 | if (read) |
859 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
813 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
860 | else { |
814 | else { |
861 | /* Update the cached version of node's size. */ |
815 | /* Update the cached version of node's size. */ |
862 | if (rc == EOK) |
816 | if (rc == EOK) |
863 | file->node->size = IPC_GET_ARG2(answer); |
817 | file->node->size = IPC_GET_ARG2(answer); |
864 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
818 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
865 | } |
819 | } |
866 | 820 | ||
867 | /* Update the position pointer and unlock the open file. */ |
821 | /* Update the position pointer and unlock the open file. */ |
868 | if (rc == EOK) |
822 | if (rc == EOK) |
869 | file->pos += bytes; |
823 | file->pos += bytes; |
870 | fibril_mutex_unlock(&file->lock); |
824 | fibril_mutex_unlock(&file->lock); |
871 | 825 | ||
872 | /* |
826 | /* |
873 | * FS server's reply is the final result of the whole operation we |
827 | * FS server's reply is the final result of the whole operation we |
874 | * return to the client. |
828 | * return to the client. |
875 | */ |
829 | */ |
876 | ipc_answer_1(rid, rc, bytes); |
830 | ipc_answer_1(rid, rc, bytes); |
877 | } |
831 | } |
878 | 832 | ||
879 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
833 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
880 | { |
834 | { |
881 | vfs_rdwr(rid, request, true); |
835 | vfs_rdwr(rid, request, true); |
882 | } |
836 | } |
883 | 837 | ||
884 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
838 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
885 | { |
839 | { |
886 | vfs_rdwr(rid, request, false); |
840 | vfs_rdwr(rid, request, false); |
887 | } |
841 | } |
888 | 842 | ||
889 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
843 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
890 | { |
844 | { |
891 | int fd = (int) IPC_GET_ARG1(*request); |
845 | int fd = (int) IPC_GET_ARG1(*request); |
892 | off_t off = (off_t) IPC_GET_ARG2(*request); |
846 | off_t off = (off_t) IPC_GET_ARG2(*request); |
893 | int whence = (int) IPC_GET_ARG3(*request); |
847 | int whence = (int) IPC_GET_ARG3(*request); |
894 | 848 | ||
895 | 849 | ||
896 | /* Lookup the file structure corresponding to the file descriptor. */ |
850 | /* Lookup the file structure corresponding to the file descriptor. */ |
897 | vfs_file_t *file = vfs_file_get(fd); |
851 | vfs_file_t *file = vfs_file_get(fd); |
898 | if (!file) { |
852 | if (!file) { |
899 | ipc_answer_0(rid, ENOENT); |
853 | ipc_answer_0(rid, ENOENT); |
900 | return; |
854 | return; |
901 | } |
855 | } |
902 | 856 | ||
903 | off_t newpos; |
857 | off_t newpos; |
904 | fibril_mutex_lock(&file->lock); |
858 | fibril_mutex_lock(&file->lock); |
905 | if (whence == SEEK_SET) { |
859 | if (whence == SEEK_SET) { |
906 | file->pos = off; |
860 | file->pos = off; |
907 | fibril_mutex_unlock(&file->lock); |
861 | fibril_mutex_unlock(&file->lock); |
908 | ipc_answer_1(rid, EOK, off); |
862 | ipc_answer_1(rid, EOK, off); |
909 | return; |
863 | return; |
910 | } |
864 | } |
911 | if (whence == SEEK_CUR) { |
865 | if (whence == SEEK_CUR) { |
912 | if (file->pos + off < file->pos) { |
866 | if (file->pos + off < file->pos) { |
913 | fibril_mutex_unlock(&file->lock); |
867 | fibril_mutex_unlock(&file->lock); |
914 | ipc_answer_0(rid, EOVERFLOW); |
868 | ipc_answer_0(rid, EOVERFLOW); |
915 | return; |
869 | return; |
916 | } |
870 | } |
917 | file->pos += off; |
871 | file->pos += off; |
918 | newpos = file->pos; |
872 | newpos = file->pos; |
919 | fibril_mutex_unlock(&file->lock); |
873 | fibril_mutex_unlock(&file->lock); |
920 | ipc_answer_1(rid, EOK, newpos); |
874 | ipc_answer_1(rid, EOK, newpos); |
921 | return; |
875 | return; |
922 | } |
876 | } |
923 | if (whence == SEEK_END) { |
877 | if (whence == SEEK_END) { |
924 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
878 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
925 | size_t size = file->node->size; |
879 | size_t size = file->node->size; |
926 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
880 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
927 | if (size + off < size) { |
881 | if (size + off < size) { |
928 | fibril_mutex_unlock(&file->lock); |
882 | fibril_mutex_unlock(&file->lock); |
929 | ipc_answer_0(rid, EOVERFLOW); |
883 | ipc_answer_0(rid, EOVERFLOW); |
930 | return; |
884 | return; |
931 | } |
885 | } |
932 | newpos = size + off; |
886 | newpos = size + off; |
933 | fibril_mutex_unlock(&file->lock); |
887 | fibril_mutex_unlock(&file->lock); |
934 | ipc_answer_1(rid, EOK, newpos); |
888 | ipc_answer_1(rid, EOK, newpos); |
935 | return; |
889 | return; |
936 | } |
890 | } |
937 | fibril_mutex_unlock(&file->lock); |
891 | fibril_mutex_unlock(&file->lock); |
938 | ipc_answer_0(rid, EINVAL); |
892 | ipc_answer_0(rid, EINVAL); |
939 | } |
893 | } |
940 | 894 | ||
941 | int |
895 | int |
942 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
896 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
943 | fs_index_t index, size_t size) |
897 | fs_index_t index, size_t size) |
944 | { |
898 | { |
945 | ipcarg_t rc; |
899 | ipcarg_t rc; |
946 | int fs_phone; |
900 | int fs_phone; |
947 | 901 | ||
948 | fs_phone = vfs_grab_phone(fs_handle); |
902 | fs_phone = vfs_grab_phone(fs_handle); |
949 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
903 | rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle, |
950 | (ipcarg_t)index, (ipcarg_t)size); |
904 | (ipcarg_t)index, (ipcarg_t)size); |
951 | vfs_release_phone(fs_phone); |
905 | vfs_release_phone(fs_phone); |
952 | return (int)rc; |
906 | return (int)rc; |
953 | } |
907 | } |
954 | 908 | ||
955 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
909 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
956 | { |
910 | { |
957 | int fd = IPC_GET_ARG1(*request); |
911 | int fd = IPC_GET_ARG1(*request); |
958 | size_t size = IPC_GET_ARG2(*request); |
912 | size_t size = IPC_GET_ARG2(*request); |
959 | int rc; |
913 | int rc; |
960 | 914 | ||
961 | vfs_file_t *file = vfs_file_get(fd); |
915 | vfs_file_t *file = vfs_file_get(fd); |
962 | if (!file) { |
916 | if (!file) { |
963 | ipc_answer_0(rid, ENOENT); |
917 | ipc_answer_0(rid, ENOENT); |
964 | return; |
918 | return; |
965 | } |
919 | } |
966 | fibril_mutex_lock(&file->lock); |
920 | fibril_mutex_lock(&file->lock); |
967 | 921 | ||
968 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
922 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
969 | rc = vfs_truncate_internal(file->node->fs_handle, |
923 | rc = vfs_truncate_internal(file->node->fs_handle, |
970 | file->node->dev_handle, file->node->index, size); |
924 | file->node->dev_handle, file->node->index, size); |
971 | if (rc == EOK) |
925 | if (rc == EOK) |
972 | file->node->size = size; |
926 | file->node->size = size; |
973 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
927 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
974 | 928 | ||
975 | fibril_mutex_unlock(&file->lock); |
929 | fibril_mutex_unlock(&file->lock); |
976 | ipc_answer_0(rid, (ipcarg_t)rc); |
930 | ipc_answer_0(rid, (ipcarg_t)rc); |
977 | } |
931 | } |
978 | 932 | ||
- | 933 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request) |
|
- | 934 | { |
|
- | 935 | int fd = IPC_GET_ARG1(*request); |
|
- | 936 | size_t size = IPC_GET_ARG2(*request); |
|
- | 937 | ipcarg_t rc; |
|
- | 938 | ||
- | 939 | vfs_file_t *file = vfs_file_get(fd); |
|
- | 940 | if (!file) { |
|
- | 941 | ipc_answer_0(rid, ENOENT); |
|
- | 942 | return; |
|
- | 943 | } |
|
- | 944 | ||
- | 945 | ipc_callid_t callid; |
|
- | 946 | if (!ipc_data_read_receive(&callid, NULL)) { |
|
- | 947 | ipc_answer_0(callid, EINVAL); |
|
- | 948 | ipc_answer_0(rid, EINVAL); |
|
- | 949 | return; |
|
- | 950 | } |
|
- | 951 | ||
- | 952 | fibril_mutex_lock(&file->lock); |
|
- | 953 | ||
- | 954 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
- | 955 | ||
- | 956 | aid_t msg; |
|
- | 957 | msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle, |
|
- | 958 | file->node->index, true, NULL); |
|
- | 959 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
|
- | 960 | async_wait_for(msg, &rc); |
|
- | 961 | vfs_release_phone(fs_phone); |
|
- | 962 | ||
- | 963 | fibril_mutex_unlock(&file->lock); |
|
- | 964 | ipc_answer_0(rid, rc); |
|
- | 965 | } |
|
- | 966 | ||
- | 967 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request) |
|
- | 968 | { |
|
- | 969 | size_t len; |
|
- | 970 | ipc_callid_t callid; |
|
- | 971 | ||
- | 972 | if (!ipc_data_write_receive(&callid, &len)) { |
|
- | 973 | ipc_answer_0(callid, EINVAL); |
|
- | 974 | ipc_answer_0(rid, EINVAL); |
|
- | 975 | return; |
|
- | 976 | } |
|
- | 977 | char *path = malloc(len + 1); |
|
- | 978 | if (!path) { |
|
- | 979 | ipc_answer_0(callid, ENOMEM); |
|
- | 980 | ipc_answer_0(rid, ENOMEM); |
|
- | 981 | return; |
|
- | 982 | } |
|
- | 983 | int rc; |
|
- | 984 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
|
- | 985 | ipc_answer_0(rid, rc); |
|
- | 986 | free(path); |
|
- | 987 | return; |
|
- | 988 | } |
|
- | 989 | path[len] = '\0'; |
|
- | 990 | ||
- | 991 | if (!ipc_data_read_receive(&callid, NULL)) { |
|
- | 992 | free(path); |
|
- | 993 | ipc_answer_0(callid, EINVAL); |
|
- | 994 | ipc_answer_0(rid, EINVAL); |
|
- | 995 | return; |
|
- | 996 | } |
|
- | 997 | ||
- | 998 | vfs_lookup_res_t lr; |
|
- | 999 | fibril_rwlock_read_lock(&namespace_rwlock); |
|
- | 1000 | rc = vfs_lookup_internal(path, L_NONE, &lr, NULL); |
|
- | 1001 | free(path); |
|
- | 1002 | if (rc != EOK) { |
|
- | 1003 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
- | 1004 | ipc_answer_0(callid, rc); |
|
- | 1005 | ipc_answer_0(rid, rc); |
|
- | 1006 | return; |
|
- | 1007 | } |
|
- | 1008 | vfs_node_t *node = vfs_node_get(&lr); |
|
- | 1009 | if (!node) { |
|
- | 1010 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
- | 1011 | ipc_answer_0(callid, ENOMEM); |
|
- | 1012 | ipc_answer_0(rid, ENOMEM); |
|
- | 1013 | return; |
|
- | 1014 | } |
|
- | 1015 | ||
- | 1016 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
- | 1017 | ||
- | 1018 | int fs_phone = vfs_grab_phone(node->fs_handle); |
|
- | 1019 | aid_t msg; |
|
- | 1020 | msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle, |
|
- | 1021 | node->index, false, NULL); |
|
- | 1022 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
|
- | 1023 | ||
- | 1024 | ipcarg_t rv; |
|
- | 1025 | async_wait_for(msg, &rv); |
|
- | 1026 | vfs_release_phone(fs_phone); |
|
- | 1027 | ||
- | 1028 | ipc_answer_0(rid, rv); |
|
- | 1029 | ||
- | 1030 | vfs_node_put(node); |
|
- | 1031 | } |
|
- | 1032 | ||
979 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
1033 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
980 | { |
1034 | { |
981 | int mode = IPC_GET_ARG1(*request); |
1035 | int mode = IPC_GET_ARG1(*request); |
982 | 1036 | ||
983 | size_t len; |
1037 | size_t len; |
984 | ipc_callid_t callid; |
1038 | ipc_callid_t callid; |
985 | 1039 | ||
986 | if (!ipc_data_write_receive(&callid, &len)) { |
1040 | if (!ipc_data_write_receive(&callid, &len)) { |
987 | ipc_answer_0(callid, EINVAL); |
1041 | ipc_answer_0(callid, EINVAL); |
988 | ipc_answer_0(rid, EINVAL); |
1042 | ipc_answer_0(rid, EINVAL); |
989 | return; |
1043 | return; |
990 | } |
1044 | } |
991 | char *path = malloc(len + 1); |
1045 | char *path = malloc(len + 1); |
992 | if (!path) { |
1046 | if (!path) { |
993 | ipc_answer_0(callid, ENOMEM); |
1047 | ipc_answer_0(callid, ENOMEM); |
994 | ipc_answer_0(rid, ENOMEM); |
1048 | ipc_answer_0(rid, ENOMEM); |
995 | return; |
1049 | return; |
996 | } |
1050 | } |
997 | int rc; |
1051 | int rc; |
998 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
1052 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
999 | ipc_answer_0(rid, rc); |
1053 | ipc_answer_0(rid, rc); |
1000 | free(path); |
1054 | free(path); |
1001 | return; |
1055 | return; |
1002 | } |
1056 | } |
1003 | path[len] = '\0'; |
1057 | path[len] = '\0'; |
- | 1058 | ||
- | 1059 | /* Ignore mode for now. */ |
|
- | 1060 | (void) mode; |
|
1004 | 1061 | ||
1005 | fibril_rwlock_write_lock(&namespace_rwlock); |
1062 | fibril_rwlock_write_lock(&namespace_rwlock); |
1006 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
1063 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
1007 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
1064 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
1008 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1065 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1009 | free(path); |
1066 | free(path); |
1010 | ipc_answer_0(rid, rc); |
1067 | ipc_answer_0(rid, rc); |
1011 | } |
1068 | } |
1012 | 1069 | ||
1013 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
1070 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
1014 | { |
1071 | { |
1015 | int lflag = IPC_GET_ARG1(*request); |
1072 | int lflag = IPC_GET_ARG1(*request); |
1016 | 1073 | ||
1017 | size_t len; |
1074 | size_t len; |
1018 | ipc_callid_t callid; |
1075 | ipc_callid_t callid; |
1019 | 1076 | ||
1020 | if (!ipc_data_write_receive(&callid, &len)) { |
1077 | if (!ipc_data_write_receive(&callid, &len)) { |
1021 | ipc_answer_0(callid, EINVAL); |
1078 | ipc_answer_0(callid, EINVAL); |
1022 | ipc_answer_0(rid, EINVAL); |
1079 | ipc_answer_0(rid, EINVAL); |
1023 | return; |
1080 | return; |
1024 | } |
1081 | } |
1025 | char *path = malloc(len + 1); |
1082 | char *path = malloc(len + 1); |
1026 | if (!path) { |
1083 | if (!path) { |
1027 | ipc_answer_0(callid, ENOMEM); |
1084 | ipc_answer_0(callid, ENOMEM); |
1028 | ipc_answer_0(rid, ENOMEM); |
1085 | ipc_answer_0(rid, ENOMEM); |
1029 | return; |
1086 | return; |
1030 | } |
1087 | } |
1031 | int rc; |
1088 | int rc; |
1032 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
1089 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
1033 | ipc_answer_0(rid, rc); |
1090 | ipc_answer_0(rid, rc); |
1034 | free(path); |
1091 | free(path); |
1035 | return; |
1092 | return; |
1036 | } |
1093 | } |
1037 | path[len] = '\0'; |
1094 | path[len] = '\0'; |
1038 | 1095 | ||
1039 | fibril_rwlock_write_lock(&namespace_rwlock); |
1096 | fibril_rwlock_write_lock(&namespace_rwlock); |
1040 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
1097 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
1041 | vfs_lookup_res_t lr; |
1098 | vfs_lookup_res_t lr; |
1042 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
1099 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
1043 | free(path); |
1100 | free(path); |
1044 | if (rc != EOK) { |
1101 | if (rc != EOK) { |
1045 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1102 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1046 | ipc_answer_0(rid, rc); |
1103 | ipc_answer_0(rid, rc); |
1047 | return; |
1104 | return; |
1048 | } |
1105 | } |
1049 | 1106 | ||
1050 | /* |
1107 | /* |
1051 | * The name has already been unlinked by vfs_lookup_internal(). |
1108 | * The name has already been unlinked by vfs_lookup_internal(). |
1052 | * We have to get and put the VFS node to ensure that it is |
1109 | * We have to get and put the VFS node to ensure that it is |
1053 | * VFS_DESTROY'ed after the last reference to it is dropped. |
1110 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped. |
1054 | */ |
1111 | */ |
1055 | vfs_node_t *node = vfs_node_get(&lr); |
1112 | vfs_node_t *node = vfs_node_get(&lr); |
1056 | fibril_mutex_lock(&nodes_mutex); |
1113 | fibril_mutex_lock(&nodes_mutex); |
1057 | node->lnkcnt--; |
1114 | node->lnkcnt--; |
1058 | fibril_mutex_unlock(&nodes_mutex); |
1115 | fibril_mutex_unlock(&nodes_mutex); |
1059 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1116 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1060 | vfs_node_put(node); |
1117 | vfs_node_put(node); |
1061 | ipc_answer_0(rid, EOK); |
1118 | ipc_answer_0(rid, EOK); |
1062 | } |
1119 | } |
1063 | 1120 | ||
1064 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
1121 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
1065 | { |
1122 | { |
1066 | size_t olen, nlen; |
1123 | size_t olen, nlen; |
1067 | ipc_callid_t callid; |
1124 | ipc_callid_t callid; |
1068 | int rc; |
1125 | int rc; |
1069 | 1126 | ||
1070 | /* Retrieve the old path. */ |
1127 | /* Retrieve the old path. */ |
1071 | if (!ipc_data_write_receive(&callid, &olen)) { |
1128 | if (!ipc_data_write_receive(&callid, &olen)) { |
1072 | ipc_answer_0(callid, EINVAL); |
1129 | ipc_answer_0(callid, EINVAL); |
1073 | ipc_answer_0(rid, EINVAL); |
1130 | ipc_answer_0(rid, EINVAL); |
1074 | return; |
1131 | return; |
1075 | } |
1132 | } |
1076 | char *old = malloc(olen + 1); |
1133 | char *old = malloc(olen + 1); |
1077 | if (!old) { |
1134 | if (!old) { |
1078 | ipc_answer_0(callid, ENOMEM); |
1135 | ipc_answer_0(callid, ENOMEM); |
1079 | ipc_answer_0(rid, ENOMEM); |
1136 | ipc_answer_0(rid, ENOMEM); |
1080 | return; |
1137 | return; |
1081 | } |
1138 | } |
1082 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
1139 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
1083 | ipc_answer_0(rid, rc); |
1140 | ipc_answer_0(rid, rc); |
1084 | free(old); |
1141 | free(old); |
1085 | return; |
1142 | return; |
1086 | } |
1143 | } |
1087 | old[olen] = '\0'; |
1144 | old[olen] = '\0'; |
1088 | 1145 | ||
1089 | /* Retrieve the new path. */ |
1146 | /* Retrieve the new path. */ |
1090 | if (!ipc_data_write_receive(&callid, &nlen)) { |
1147 | if (!ipc_data_write_receive(&callid, &nlen)) { |
1091 | ipc_answer_0(callid, EINVAL); |
1148 | ipc_answer_0(callid, EINVAL); |
1092 | ipc_answer_0(rid, EINVAL); |
1149 | ipc_answer_0(rid, EINVAL); |
1093 | free(old); |
1150 | free(old); |
1094 | return; |
1151 | return; |
1095 | } |
1152 | } |
1096 | char *new = malloc(nlen + 1); |
1153 | char *new = malloc(nlen + 1); |
1097 | if (!new) { |
1154 | if (!new) { |
1098 | ipc_answer_0(callid, ENOMEM); |
1155 | ipc_answer_0(callid, ENOMEM); |
1099 | ipc_answer_0(rid, ENOMEM); |
1156 | ipc_answer_0(rid, ENOMEM); |
1100 | free(old); |
1157 | free(old); |
1101 | return; |
1158 | return; |
1102 | } |
1159 | } |
1103 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
1160 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
1104 | ipc_answer_0(rid, rc); |
1161 | ipc_answer_0(rid, rc); |
1105 | free(old); |
1162 | free(old); |
1106 | free(new); |
1163 | free(new); |
1107 | return; |
1164 | return; |
1108 | } |
1165 | } |
1109 | new[nlen] = '\0'; |
1166 | new[nlen] = '\0'; |
1110 | 1167 | ||
1111 | char *oldc = canonify(old, &olen); |
1168 | char *oldc = canonify(old, &olen); |
1112 | char *newc = canonify(new, &nlen); |
1169 | char *newc = canonify(new, &nlen); |
1113 | if (!oldc || !newc) { |
1170 | if (!oldc || !newc) { |
1114 | ipc_answer_0(rid, EINVAL); |
1171 | ipc_answer_0(rid, EINVAL); |
1115 | free(old); |
1172 | free(old); |
1116 | free(new); |
1173 | free(new); |
1117 | return; |
1174 | return; |
1118 | } |
1175 | } |
1119 | oldc[olen] = '\0'; |
1176 | oldc[olen] = '\0'; |
1120 | newc[nlen] = '\0'; |
1177 | newc[nlen] = '\0'; |
1121 | if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
1178 | if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
1122 | ((newc[str_length(oldc)] == '/') || |
1179 | ((newc[str_length(oldc)] == '/') || |
1123 | (str_length(oldc) == 1) || |
1180 | (str_length(oldc) == 1) || |
1124 | (str_length(oldc) == str_length(newc)))) { |
1181 | (str_length(oldc) == str_length(newc)))) { |
1125 | /* |
1182 | /* |
1126 | * oldc is a prefix of newc and either |
1183 | * oldc is a prefix of newc and either |
1127 | * - newc continues with a / where oldc ends, or |
1184 | * - newc continues with a / where oldc ends, or |
1128 | * - oldc was / itself, or |
1185 | * - oldc was / itself, or |
1129 | * - oldc and newc are equal. |
1186 | * - oldc and newc are equal. |
1130 | */ |
1187 | */ |
1131 | ipc_answer_0(rid, EINVAL); |
1188 | ipc_answer_0(rid, EINVAL); |
1132 | free(old); |
1189 | free(old); |
1133 | free(new); |
1190 | free(new); |
1134 | return; |
1191 | return; |
1135 | } |
1192 | } |
1136 | 1193 | ||
1137 | vfs_lookup_res_t old_lr; |
1194 | vfs_lookup_res_t old_lr; |
1138 | vfs_lookup_res_t new_lr; |
1195 | vfs_lookup_res_t new_lr; |
1139 | vfs_lookup_res_t new_par_lr; |
1196 | vfs_lookup_res_t new_par_lr; |
1140 | fibril_rwlock_write_lock(&namespace_rwlock); |
1197 | fibril_rwlock_write_lock(&namespace_rwlock); |
1141 | /* Lookup the node belonging to the old file name. */ |
1198 | /* Lookup the node belonging to the old file name. */ |
1142 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
1199 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
1143 | if (rc != EOK) { |
1200 | if (rc != EOK) { |
1144 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1201 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1145 | ipc_answer_0(rid, rc); |
1202 | ipc_answer_0(rid, rc); |
1146 | free(old); |
1203 | free(old); |
1147 | free(new); |
1204 | free(new); |
1148 | return; |
1205 | return; |
1149 | } |
1206 | } |
1150 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
1207 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
1151 | if (!old_node) { |
1208 | if (!old_node) { |
1152 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1209 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1153 | ipc_answer_0(rid, ENOMEM); |
1210 | ipc_answer_0(rid, ENOMEM); |
1154 | free(old); |
1211 | free(old); |
1155 | free(new); |
1212 | free(new); |
1156 | return; |
1213 | return; |
1157 | } |
1214 | } |
1158 | /* Determine the path to the parent of the node with the new name. */ |
1215 | /* Determine the path to the parent of the node with the new name. */ |
1159 | char *parentc = str_dup(newc); |
1216 | char *parentc = str_dup(newc); |
1160 | if (!parentc) { |
1217 | if (!parentc) { |
1161 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1218 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1162 | ipc_answer_0(rid, rc); |
1219 | ipc_answer_0(rid, rc); |
1163 | free(old); |
1220 | free(old); |
1164 | free(new); |
1221 | free(new); |
1165 | return; |
1222 | return; |
1166 | } |
1223 | } |
1167 | char *lastsl = str_rchr(parentc + 1, '/'); |
1224 | char *lastsl = str_rchr(parentc + 1, '/'); |
1168 | if (lastsl) |
1225 | if (lastsl) |
1169 | *lastsl = '\0'; |
1226 | *lastsl = '\0'; |
1170 | else |
1227 | else |
1171 | parentc[1] = '\0'; |
1228 | parentc[1] = '\0'; |
1172 | /* Lookup parent of the new file name. */ |
1229 | /* Lookup parent of the new file name. */ |
1173 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
1230 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
1174 | free(parentc); /* not needed anymore */ |
1231 | free(parentc); /* not needed anymore */ |
1175 | if (rc != EOK) { |
1232 | if (rc != EOK) { |
1176 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1233 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1177 | ipc_answer_0(rid, rc); |
1234 | ipc_answer_0(rid, rc); |
1178 | free(old); |
1235 | free(old); |
1179 | free(new); |
1236 | free(new); |
1180 | return; |
1237 | return; |
1181 | } |
1238 | } |
1182 | /* Check whether linking to the same file system instance. */ |
1239 | /* Check whether linking to the same file system instance. */ |
1183 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
1240 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
1184 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
1241 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
1185 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1242 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1186 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
1243 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
1187 | free(old); |
1244 | free(old); |
1188 | free(new); |
1245 | free(new); |
1189 | return; |
1246 | return; |
1190 | } |
1247 | } |
1191 | /* Destroy the old link for the new name. */ |
1248 | /* Destroy the old link for the new name. */ |
1192 | vfs_node_t *new_node = NULL; |
1249 | vfs_node_t *new_node = NULL; |
1193 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
1250 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
1194 | switch (rc) { |
1251 | switch (rc) { |
1195 | case ENOENT: |
1252 | case ENOENT: |
1196 | /* simply not in our way */ |
1253 | /* simply not in our way */ |
1197 | break; |
1254 | break; |
1198 | case EOK: |
1255 | case EOK: |
1199 | new_node = vfs_node_get(&new_lr); |
1256 | new_node = vfs_node_get(&new_lr); |
1200 | if (!new_node) { |
1257 | if (!new_node) { |
1201 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1258 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1202 | ipc_answer_0(rid, ENOMEM); |
1259 | ipc_answer_0(rid, ENOMEM); |
1203 | free(old); |
1260 | free(old); |
1204 | free(new); |
1261 | free(new); |
1205 | return; |
1262 | return; |
1206 | } |
1263 | } |
1207 | fibril_mutex_lock(&nodes_mutex); |
1264 | fibril_mutex_lock(&nodes_mutex); |
1208 | new_node->lnkcnt--; |
1265 | new_node->lnkcnt--; |
1209 | fibril_mutex_unlock(&nodes_mutex); |
1266 | fibril_mutex_unlock(&nodes_mutex); |
1210 | break; |
1267 | break; |
1211 | default: |
1268 | default: |
1212 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1269 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1213 | ipc_answer_0(rid, ENOTEMPTY); |
1270 | ipc_answer_0(rid, ENOTEMPTY); |
1214 | free(old); |
1271 | free(old); |
1215 | free(new); |
1272 | free(new); |
1216 | return; |
1273 | return; |
1217 | } |
1274 | } |
1218 | /* Create the new link for the new name. */ |
1275 | /* Create the new link for the new name. */ |
1219 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
1276 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
1220 | if (rc != EOK) { |
1277 | if (rc != EOK) { |
1221 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1278 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1222 | if (new_node) |
1279 | if (new_node) |
1223 | vfs_node_put(new_node); |
1280 | vfs_node_put(new_node); |
1224 | ipc_answer_0(rid, rc); |
1281 | ipc_answer_0(rid, rc); |
1225 | free(old); |
1282 | free(old); |
1226 | free(new); |
1283 | free(new); |
1227 | return; |
1284 | return; |
1228 | } |
1285 | } |
1229 | fibril_mutex_lock(&nodes_mutex); |
1286 | fibril_mutex_lock(&nodes_mutex); |
1230 | old_node->lnkcnt++; |
1287 | old_node->lnkcnt++; |
1231 | fibril_mutex_unlock(&nodes_mutex); |
1288 | fibril_mutex_unlock(&nodes_mutex); |
1232 | /* Destroy the link for the old name. */ |
1289 | /* Destroy the link for the old name. */ |
1233 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
1290 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
1234 | if (rc != EOK) { |
1291 | if (rc != EOK) { |
1235 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1292 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1236 | vfs_node_put(old_node); |
1293 | vfs_node_put(old_node); |
1237 | if (new_node) |
1294 | if (new_node) |
1238 | vfs_node_put(new_node); |
1295 | vfs_node_put(new_node); |
1239 | ipc_answer_0(rid, rc); |
1296 | ipc_answer_0(rid, rc); |
1240 | free(old); |
1297 | free(old); |
1241 | free(new); |
1298 | free(new); |
1242 | return; |
1299 | return; |
1243 | } |
1300 | } |
1244 | fibril_mutex_lock(&nodes_mutex); |
1301 | fibril_mutex_lock(&nodes_mutex); |
1245 | old_node->lnkcnt--; |
1302 | old_node->lnkcnt--; |
1246 | fibril_mutex_unlock(&nodes_mutex); |
1303 | fibril_mutex_unlock(&nodes_mutex); |
1247 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1304 | fibril_rwlock_write_unlock(&namespace_rwlock); |
1248 | vfs_node_put(old_node); |
1305 | vfs_node_put(old_node); |
1249 | if (new_node) |
1306 | if (new_node) |
1250 | vfs_node_put(new_node); |
1307 | vfs_node_put(new_node); |
1251 | free(old); |
1308 | free(old); |
1252 | free(new); |
1309 | free(new); |
1253 | ipc_answer_0(rid, EOK); |
1310 | ipc_answer_0(rid, EOK); |
1254 | } |
1311 | } |
1255 | 1312 | ||
1256 | /** |
1313 | /** |
1257 | * @} |
1314 | * @} |
1258 | */ |
1315 | */ |
1259 | 1316 |