Rev 3675 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3675 | Rev 4377 | ||
|---|---|---|---|
| Line 26... | Line 26... | ||
| 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> |
| Line 53... | Line 53... | ||
| 53 | #include <vfs/canonify.h> |
53 | #include <vfs/canonify.h> |
| 54 | 54 | ||
| 55 | /* Forward declarations of static functions. */ |
55 | /* Forward declarations of static functions. */ |
| 56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
| 57 | 57 | ||
| - | 58 | /** Pending mount structure. */ |
|
| - | 59 | typedef struct { |
|
| - | 60 | link_t link; |
|
| - | 61 | char *fs_name; /**< File system name */ |
|
| - | 62 | char *mp; /**< Mount point */ |
|
| - | 63 | char *opts; /**< Mount options. */ |
|
| - | 64 | ipc_callid_t callid; /**< Call ID waiting for the mount */ |
|
| - | 65 | ipc_callid_t rid; /**< Request ID */ |
|
| - | 66 | dev_handle_t dev_handle; /**< Device handle */ |
|
| - | 67 | } pending_req_t; |
|
| - | 68 | ||
| - | 69 | LIST_INITIALIZE(pending_req); |
|
| - | 70 | ||
| 58 | /** |
71 | /** |
| 59 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
72 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
| 60 | * concurrent VFS operation which modifies the file system namespace. |
73 | * concurrent VFS operation which modifies the file system namespace. |
| 61 | */ |
74 | */ |
| 62 | RWLOCK_INITIALIZE(namespace_rwlock); |
75 | RWLOCK_INITIALIZE(namespace_rwlock); |
| Line 65... | Line 78... | ||
| 65 | vfs_pair_t rootfs = { |
78 | vfs_pair_t rootfs = { |
| 66 | .fs_handle = 0, |
79 | .fs_handle = 0, |
| 67 | .dev_handle = 0 |
80 | .dev_handle = 0 |
| 68 | }; |
81 | }; |
| 69 | 82 | ||
| 70 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
83 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle, |
| - | 84 | fs_handle_t fs_handle, char *mp, char *opts) |
|
| 71 | { |
85 | { |
| 72 | dev_handle_t dev_handle; |
86 | vfs_lookup_res_t mp_res; |
| 73 | vfs_node_t *mp_node = NULL; |
87 | vfs_node_t *mp_node = NULL; |
| 74 | ipc_callid_t callid; |
- | |
| 75 | ipc_call_t data; |
88 | ipcarg_t rc; |
| 76 | int rc; |
- | |
| 77 | int phone; |
89 | int phone; |
| 78 | size_t size; |
90 | aid_t msg; |
| 79 | - | ||
| 80 | /* |
- | |
| 81 | * We expect the library to do the device-name to device-handle |
- | |
| 82 | * translation for us, thus the device handle will arrive as ARG1 |
- | |
| 83 | * in the request. |
- | |
| 84 | */ |
- | |
| 85 | dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
- | |
| 86 | - | ||
| 87 | /* |
- | |
| 88 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
- | |
| 89 | * carry mount options in the future. |
- | |
| 90 | */ |
- | |
| 91 | - | ||
| 92 | /* |
- | |
| 93 | * Now, we expect the client to send us data with the name of the file |
- | |
| 94 | * system. |
- | |
| 95 | */ |
- | |
| 96 | if (!ipc_data_write_receive(&callid, &size)) { |
- | |
| 97 | ipc_answer_0(callid, EINVAL); |
- | |
| 98 | ipc_answer_0(rid, EINVAL); |
- | |
| 99 | return; |
- | |
| 100 | } |
- | |
| 101 | - | ||
| 102 | /* |
- | |
| 103 | * Don't receive more than is necessary for storing a full file system |
- | |
| 104 | * name. |
- | |
| 105 | */ |
- | |
| 106 | if (size < 1 || size > FS_NAME_MAXLEN) { |
- | |
| 107 | ipc_answer_0(callid, EINVAL); |
- | |
| 108 | ipc_answer_0(rid, EINVAL); |
- | |
| 109 | return; |
- | |
| 110 | } |
- | |
| 111 | - | ||
| 112 | /* Deliver the file system name. */ |
- | |
| 113 | char fs_name[FS_NAME_MAXLEN + 1]; |
- | |
| 114 | (void) ipc_data_write_finalize(callid, fs_name, size); |
- | |
| 115 | fs_name[size] = '\0'; |
- | |
| 116 | - | ||
| 117 | /* |
- | |
| 118 | * Wait for IPC_M_PING so that we can return an error if we don't know |
- | |
| 119 | * fs_name. |
- | |
| 120 | */ |
- | |
| 121 | callid = async_get_call(&data); |
- | |
| 122 | if (IPC_GET_METHOD(data) != IPC_M_PING) { |
- | |
| 123 | ipc_answer_0(callid, ENOTSUP); |
- | |
| 124 | ipc_answer_0(rid, ENOTSUP); |
- | |
| 125 | return; |
- | |
| 126 | } |
- | |
| 127 | - | ||
| 128 | /* |
- | |
| 129 | * Check if we know a file system with the same name as is in fs_name. |
- | |
| 130 | * This will also give us its file system handle. |
- | |
| 131 | */ |
- | |
| 132 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
- | |
| 133 | if (!fs_handle) { |
91 | ipc_call_t answer; |
| 134 | ipc_answer_0(callid, ENOENT); |
- | |
| 135 | ipc_answer_0(rid, ENOENT); |
- | |
| 136 | return; |
- | |
| 137 | } |
- | |
| 138 | - | ||
| 139 | /* Acknowledge that we know fs_name. */ |
- | |
| 140 | ipc_answer_0(callid, EOK); |
- | |
| 141 | - | ||
| 142 | /* Now, we want the client to send us the mount point. */ |
- | |
| 143 | if (!ipc_data_write_receive(&callid, &size)) { |
- | |
| 144 | ipc_answer_0(callid, EINVAL); |
- | |
| 145 | ipc_answer_0(rid, EINVAL); |
- | |
| 146 | return; |
- | |
| 147 | } |
- | |
| 148 | - | ||
| 149 | /* Check whether size is reasonable wrt. the mount point. */ |
- | |
| 150 | if (size < 1 || size > MAX_PATH_LEN) { |
- | |
| 151 | ipc_answer_0(callid, EINVAL); |
- | |
| 152 | ipc_answer_0(rid, EINVAL); |
- | |
| 153 | return; |
- | |
| 154 | } |
- | |
| 155 | /* Allocate buffer for the mount point data being received. */ |
- | |
| 156 | char *buf; |
- | |
| 157 | buf = malloc(size + 1); |
- | |
| 158 | if (!buf) { |
- | |
| 159 | ipc_answer_0(callid, ENOMEM); |
- | |
| 160 | ipc_answer_0(rid, ENOMEM); |
- | |
| 161 | return; |
- | |
| 162 | } |
- | |
| 163 | - | ||
| 164 | /* Deliver the mount point. */ |
- | |
| 165 | (void) ipc_data_write_finalize(callid, buf, size); |
- | |
| 166 | buf[size] = '\0'; |
- | |
| 167 | 92 | ||
| 168 | /* Resolve the path to the mountpoint. */ |
93 | /* Resolve the path to the mountpoint. */ |
| 169 | vfs_lookup_res_t mp_res; |
- | |
| 170 | futex_down(&rootfs_futex); |
94 | futex_down(&rootfs_futex); |
| 171 | if (rootfs.fs_handle) { |
95 | if (rootfs.fs_handle) { |
| 172 | /* We already have the root FS. */ |
96 | /* We already have the root FS. */ |
| 173 | rwlock_write_lock(&namespace_rwlock); |
97 | rwlock_write_lock(&namespace_rwlock); |
| 174 | if ((size == 1) && (buf[0] == '/')) { |
98 | if (str_cmp(mp, "/") == 0) { |
| 175 | /* Trying to mount root FS over root FS */ |
99 | /* Trying to mount root FS over root FS */ |
| 176 | rwlock_write_unlock(&namespace_rwlock); |
100 | rwlock_write_unlock(&namespace_rwlock); |
| 177 | futex_up(&rootfs_futex); |
101 | futex_up(&rootfs_futex); |
| 178 | free(buf); |
- | |
| 179 | ipc_answer_0(rid, EBUSY); |
102 | ipc_answer_0(rid, EBUSY); |
| 180 | return; |
103 | return; |
| 181 | } |
104 | } |
| - | 105 | ||
| 182 | rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL); |
106 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
| 183 | if (rc != EOK) { |
107 | if (rc != EOK) { |
| 184 | /* The lookup failed for some reason. */ |
108 | /* The lookup failed for some reason. */ |
| 185 | rwlock_write_unlock(&namespace_rwlock); |
109 | rwlock_write_unlock(&namespace_rwlock); |
| 186 | futex_up(&rootfs_futex); |
110 | futex_up(&rootfs_futex); |
| 187 | free(buf); |
- | |
| 188 | ipc_answer_0(rid, rc); |
111 | ipc_answer_0(rid, rc); |
| 189 | return; |
112 | return; |
| 190 | } |
113 | } |
| - | 114 | ||
| 191 | mp_node = vfs_node_get(&mp_res); |
115 | mp_node = vfs_node_get(&mp_res); |
| 192 | if (!mp_node) { |
116 | if (!mp_node) { |
| 193 | rwlock_write_unlock(&namespace_rwlock); |
117 | rwlock_write_unlock(&namespace_rwlock); |
| 194 | futex_up(&rootfs_futex); |
118 | futex_up(&rootfs_futex); |
| 195 | free(buf); |
- | |
| 196 | ipc_answer_0(rid, ENOMEM); |
119 | ipc_answer_0(rid, ENOMEM); |
| 197 | return; |
120 | return; |
| 198 | } |
121 | } |
| - | 122 | ||
| 199 | /* |
123 | /* |
| 200 | * Now we hold a reference to mp_node. |
124 | * Now we hold a reference to mp_node. |
| 201 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
125 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
| 202 | * This prevents the mount point from being deleted. |
126 | * This prevents the mount point from being deleted. |
| 203 | */ |
127 | */ |
| 204 | rwlock_write_unlock(&namespace_rwlock); |
128 | rwlock_write_unlock(&namespace_rwlock); |
| 205 | } else { |
129 | } else { |
| 206 | /* We still don't have the root file system mounted. */ |
130 | /* We still don't have the root file system mounted. */ |
| 207 | if ((size == 1) && (buf[0] == '/')) { |
131 | if (str_cmp(mp, "/") == 0) { |
| 208 | vfs_lookup_res_t mr_res; |
132 | vfs_lookup_res_t mr_res; |
| 209 | vfs_node_t *mr_node; |
133 | vfs_node_t *mr_node; |
| 210 | ipcarg_t rindex; |
134 | fs_index_t rindex; |
| 211 | ipcarg_t rsize; |
135 | size_t rsize; |
| 212 | ipcarg_t rlnkcnt; |
136 | unsigned rlnkcnt; |
| 213 | 137 | ||
| 214 | /* |
138 | /* |
| 215 | * For this simple, but important case, |
139 | * For this simple, but important case, |
| 216 | * we are almost done. |
140 | * we are almost done. |
| 217 | */ |
141 | */ |
| 218 | free(buf); |
- | |
| 219 | 142 | ||
| 220 | /* Tell the mountee that it is being mounted. */ |
143 | /* Tell the mountee that it is being mounted. */ |
| 221 | phone = vfs_grab_phone(fs_handle); |
144 | phone = vfs_grab_phone(fs_handle); |
| 222 | rc = async_req_1_3(phone, VFS_MOUNTED, |
145 | msg = async_send_1(phone, VFS_MOUNTED, |
| 223 | (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt); |
146 | (ipcarg_t) dev_handle, &answer); |
| - | 147 | /* send the mount options */ |
|
| - | 148 | rc = ipc_data_write_start(phone, (void *)opts, |
|
| - | 149 | str_size(opts)); |
|
| - | 150 | if (rc != EOK) { |
|
| - | 151 | async_wait_for(msg, NULL); |
|
| - | 152 | vfs_release_phone(phone); |
|
| - | 153 | futex_up(&rootfs_futex); |
|
| - | 154 | ipc_answer_0(rid, rc); |
|
| - | 155 | return; |
|
| - | 156 | } |
|
| - | 157 | async_wait_for(msg, &rc); |
|
| 224 | vfs_release_phone(phone); |
158 | vfs_release_phone(phone); |
| 225 | 159 | ||
| 226 | if (rc != EOK) { |
160 | if (rc != EOK) { |
| 227 | futex_up(&rootfs_futex); |
161 | futex_up(&rootfs_futex); |
| 228 | ipc_answer_0(rid, rc); |
162 | ipc_answer_0(rid, rc); |
| 229 | return; |
163 | return; |
| 230 | } |
164 | } |
| 231 | 165 | ||
| - | 166 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
|
| - | 167 | rsize = (size_t) IPC_GET_ARG2(answer); |
|
| - | 168 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
|
| - | 169 | ||
| 232 | mr_res.triplet.fs_handle = fs_handle; |
170 | mr_res.triplet.fs_handle = fs_handle; |
| 233 | mr_res.triplet.dev_handle = dev_handle; |
171 | mr_res.triplet.dev_handle = dev_handle; |
| 234 | mr_res.triplet.index = (fs_index_t) rindex; |
172 | mr_res.triplet.index = rindex; |
| 235 | mr_res.size = (size_t) rsize; |
173 | mr_res.size = rsize; |
| 236 | mr_res.lnkcnt = (unsigned) rlnkcnt; |
174 | mr_res.lnkcnt = rlnkcnt; |
| 237 | mr_res.type = VFS_NODE_DIRECTORY; |
175 | mr_res.type = VFS_NODE_DIRECTORY; |
| 238 | 176 | ||
| 239 | rootfs.fs_handle = fs_handle; |
177 | rootfs.fs_handle = fs_handle; |
| 240 | rootfs.dev_handle = dev_handle; |
178 | rootfs.dev_handle = dev_handle; |
| 241 | futex_up(&rootfs_futex); |
179 | futex_up(&rootfs_futex); |
| 242 | 180 | ||
| 243 | /* Add reference to the mounted root. */ |
181 | /* Add reference to the mounted root. */ |
| 244 | mr_node = vfs_node_get(&mr_res); |
182 | mr_node = vfs_node_get(&mr_res); |
| 245 | assert(mr_node); |
183 | assert(mr_node); |
| 246 | 184 | ||
| 247 | ipc_answer_0(rid, rc); |
185 | ipc_answer_0(rid, rc); |
| 248 | return; |
186 | return; |
| 249 | } else { |
187 | } else { |
| 250 | /* |
188 | /* |
| 251 | * We can't resolve this without the root filesystem |
189 | * We can't resolve this without the root filesystem |
| 252 | * being mounted first. |
190 | * being mounted first. |
| 253 | */ |
191 | */ |
| 254 | futex_up(&rootfs_futex); |
192 | futex_up(&rootfs_futex); |
| 255 | free(buf); |
- | |
| 256 | ipc_answer_0(rid, ENOENT); |
193 | ipc_answer_0(rid, ENOENT); |
| 257 | return; |
194 | return; |
| 258 | } |
195 | } |
| 259 | } |
196 | } |
| 260 | futex_up(&rootfs_futex); |
197 | futex_up(&rootfs_futex); |
| 261 | 198 | ||
| 262 | free(buf); /* The buffer is not needed anymore. */ |
- | |
| 263 | - | ||
| 264 | /* |
199 | /* |
| 265 | * At this point, we have all necessary pieces: file system and device |
200 | * At this point, we have all necessary pieces: file system and device |
| 266 | * handles, and we know the mount point VFS node. |
201 | * handles, and we know the mount point VFS node. |
| 267 | */ |
202 | */ |
| 268 | 203 | ||
| 269 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
204 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
| 270 | rc = async_req_4_0(phone, VFS_MOUNT, |
205 | msg = async_send_4(phone, VFS_MOUNT, |
| 271 | (ipcarg_t) mp_res.triplet.dev_handle, |
206 | (ipcarg_t) mp_res.triplet.dev_handle, |
| 272 | (ipcarg_t) mp_res.triplet.index, |
207 | (ipcarg_t) mp_res.triplet.index, |
| 273 | (ipcarg_t) fs_handle, |
208 | (ipcarg_t) fs_handle, |
| 274 | (ipcarg_t) dev_handle); |
209 | (ipcarg_t) dev_handle, &answer); |
| - | 210 | /* send the mount options */ |
|
| - | 211 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
|
| - | 212 | if (rc != EOK) { |
|
| - | 213 | async_wait_for(msg, NULL); |
|
| - | 214 | vfs_release_phone(phone); |
|
| - | 215 | /* Mount failed, drop reference to mp_node. */ |
|
| - | 216 | if (mp_node) |
|
| - | 217 | vfs_node_put(mp_node); |
|
| - | 218 | ipc_answer_0(rid, rc); |
|
| - | 219 | return; |
|
| - | 220 | } |
|
| - | 221 | async_wait_for(msg, &rc); |
|
| 275 | vfs_release_phone(phone); |
222 | vfs_release_phone(phone); |
| 276 | 223 | ||
| 277 | if (rc != EOK) { |
224 | if (rc != EOK) { |
| 278 | /* Mount failed, drop reference to mp_node. */ |
225 | /* Mount failed, drop reference to mp_node. */ |
| 279 | if (mp_node) |
226 | if (mp_node) |
| 280 | vfs_node_put(mp_node); |
227 | vfs_node_put(mp_node); |
| 281 | } |
228 | } |
| 282 | 229 | ||
| 283 | ipc_answer_0(rid, rc); |
230 | ipc_answer_0(rid, rc); |
| 284 | } |
231 | } |
| 285 | 232 | ||
| - | 233 | /** Process pending mount requests */ |
|
| - | 234 | void vfs_process_pending_mount() |
|
| - | 235 | { |
|
| - | 236 | link_t *cur; |
|
| - | 237 | ||
| - | 238 | loop: |
|
| - | 239 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
|
| - | 240 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
|
| - | 241 | ||
| - | 242 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true); |
|
| - | 243 | if (!fs_handle) |
|
| - | 244 | continue; |
|
| - | 245 | ||
| - | 246 | /* Acknowledge that we know fs_name. */ |
|
| - | 247 | ipc_answer_0(pr->callid, EOK); |
|
| - | 248 | ||
| - | 249 | /* Do the mount */ |
|
| - | 250 | vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp, |
|
| - | 251 | pr->opts); |
|
| - | 252 | ||
| - | 253 | free(pr->fs_name); |
|
| - | 254 | free(pr->mp); |
|
| - | 255 | free(pr->opts); |
|
| - | 256 | list_remove(cur); |
|
| - | 257 | free(pr); |
|
| - | 258 | goto loop; |
|
| - | 259 | } |
|
| - | 260 | } |
|
| - | 261 | ||
| - | 262 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
|
| - | 263 | { |
|
| - | 264 | /* |
|
| - | 265 | * We expect the library to do the device-name to device-handle |
|
| - | 266 | * translation for us, thus the device handle will arrive as ARG1 |
|
| - | 267 | * in the request. |
|
| - | 268 | */ |
|
| - | 269 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
|
| - | 270 | ||
| - | 271 | /* |
|
| - | 272 | * Mount flags are passed as ARG2. |
|
| - | 273 | */ |
|
| - | 274 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request); |
|
| - | 275 | ||
| - | 276 | /* |
|
| - | 277 | * For now, don't make use of ARG3, but it can be used to |
|
| - | 278 | * carry mount options in the future. |
|
| - | 279 | */ |
|
| - | 280 | ||
| - | 281 | /* We want the client to send us the mount point. */ |
|
| - | 282 | ipc_callid_t callid; |
|
| - | 283 | size_t size; |
|
| - | 284 | if (!ipc_data_write_receive(&callid, &size)) { |
|
| - | 285 | ipc_answer_0(callid, EINVAL); |
|
| - | 286 | ipc_answer_0(rid, EINVAL); |
|
| - | 287 | return; |
|
| - | 288 | } |
|
| - | 289 | ||
| - | 290 | /* Check whether size is reasonable wrt. the mount point. */ |
|
| - | 291 | if ((size < 1) || (size > MAX_PATH_LEN)) { |
|
| - | 292 | ipc_answer_0(callid, EINVAL); |
|
| - | 293 | ipc_answer_0(rid, EINVAL); |
|
| - | 294 | return; |
|
| - | 295 | } |
|
| - | 296 | ||
| - | 297 | /* Allocate buffer for the mount point data being received. */ |
|
| - | 298 | char *mp = malloc(size + 1); |
|
| - | 299 | if (!mp) { |
|
| - | 300 | ipc_answer_0(callid, ENOMEM); |
|
| - | 301 | ipc_answer_0(rid, ENOMEM); |
|
| - | 302 | return; |
|
| - | 303 | } |
|
| - | 304 | ||
| - | 305 | /* Deliver the mount point. */ |
|
| - | 306 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size); |
|
| - | 307 | if (retval != EOK) { |
|
| - | 308 | ipc_answer_0(rid, retval); |
|
| - | 309 | free(mp); |
|
| - | 310 | return; |
|
| - | 311 | } |
|
| - | 312 | mp[size] = '\0'; |
|
| - | 313 | ||
| - | 314 | /* Now we expect to receive the mount options. */ |
|
| - | 315 | if (!ipc_data_write_receive(&callid, &size)) { |
|
| - | 316 | ipc_answer_0(callid, EINVAL); |
|
| - | 317 | ipc_answer_0(rid, EINVAL); |
|
| - | 318 | free(mp); |
|
| - | 319 | return; |
|
| - | 320 | } |
|
| - | 321 | ||
| - | 322 | /* Check the offered options size. */ |
|
| - | 323 | if (size < 0 || size > MAX_MNTOPTS_LEN) { |
|
| - | 324 | ipc_answer_0(callid, EINVAL); |
|
| - | 325 | ipc_answer_0(rid, EINVAL); |
|
| - | 326 | free(mp); |
|
| - | 327 | return; |
|
| - | 328 | } |
|
| - | 329 | ||
| - | 330 | /* Allocate buffer for the mount options. */ |
|
| - | 331 | char *opts = (char *) malloc(size + 1); |
|
| - | 332 | if (!opts) { |
|
| - | 333 | ipc_answer_0(callid, ENOMEM); |
|
| - | 334 | ipc_answer_0(rid, ENOMEM); |
|
| - | 335 | free(mp); |
|
| - | 336 | return; |
|
| - | 337 | } |
|
| - | 338 | ||
| - | 339 | /* Deliver the mount options. */ |
|
| - | 340 | retval = ipc_data_write_finalize(callid, opts, size); |
|
| - | 341 | if (retval != EOK) { |
|
| - | 342 | ipc_answer_0(rid, retval); |
|
| - | 343 | free(mp); |
|
| - | 344 | free(opts); |
|
| - | 345 | return; |
|
| - | 346 | } |
|
| - | 347 | opts[size] = '\0'; |
|
| - | 348 | ||
| - | 349 | /* |
|
| - | 350 | * Now, we expect the client to send us data with the name of the file |
|
| - | 351 | * system. |
|
| - | 352 | */ |
|
| - | 353 | if (!ipc_data_write_receive(&callid, &size)) { |
|
| - | 354 | ipc_answer_0(callid, EINVAL); |
|
| - | 355 | ipc_answer_0(rid, EINVAL); |
|
| - | 356 | free(mp); |
|
| - | 357 | free(opts); |
|
| - | 358 | return; |
|
| - | 359 | } |
|
| - | 360 | ||
| - | 361 | /* |
|
| - | 362 | * Don't receive more than is necessary for storing a full file system |
|
| - | 363 | * name. |
|
| - | 364 | */ |
|
| - | 365 | if ((size < 1) || (size > FS_NAME_MAXLEN)) { |
|
| - | 366 | ipc_answer_0(callid, EINVAL); |
|
| - | 367 | ipc_answer_0(rid, EINVAL); |
|
| - | 368 | free(mp); |
|
| - | 369 | free(opts); |
|
| - | 370 | return; |
|
| - | 371 | } |
|
| - | 372 | ||
| - | 373 | /* |
|
| - | 374 | * Allocate buffer for file system name. |
|
| - | 375 | */ |
|
| - | 376 | char *fs_name = (char *) malloc(size + 1); |
|
| - | 377 | if (fs_name == NULL) { |
|
| - | 378 | ipc_answer_0(callid, ENOMEM); |
|
| - | 379 | ipc_answer_0(rid, ENOMEM); |
|
| - | 380 | free(mp); |
|
| - | 381 | free(opts); |
|
| - | 382 | return; |
|
| - | 383 | } |
|
| - | 384 | ||
| - | 385 | /* Deliver the file system name. */ |
|
| - | 386 | retval = ipc_data_write_finalize(callid, fs_name, size); |
|
| - | 387 | if (retval != EOK) { |
|
| - | 388 | ipc_answer_0(rid, retval); |
|
| - | 389 | free(mp); |
|
| - | 390 | free(opts); |
|
| - | 391 | free(fs_name); |
|
| - | 392 | return; |
|
| - | 393 | } |
|
| - | 394 | fs_name[size] = '\0'; |
|
| - | 395 | ||
| - | 396 | /* |
|
| - | 397 | * Wait for IPC_M_PING so that we can return an error if we don't know |
|
| - | 398 | * fs_name. |
|
| - | 399 | */ |
|
| - | 400 | ipc_call_t data; |
|
| - | 401 | callid = async_get_call(&data); |
|
| - | 402 | if (IPC_GET_METHOD(data) != IPC_M_PING) { |
|
| - | 403 | ipc_answer_0(callid, ENOTSUP); |
|
| - | 404 | ipc_answer_0(rid, ENOTSUP); |
|
| - | 405 | free(mp); |
|
| - | 406 | free(opts); |
|
| - | 407 | free(fs_name); |
|
| - | 408 | return; |
|
| - | 409 | } |
|
| - | 410 | ||
| - | 411 | /* |
|
| - | 412 | * Check if we know a file system with the same name as is in fs_name. |
|
| - | 413 | * This will also give us its file system handle. |
|
| - | 414 | */ |
|
| - | 415 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true); |
|
| - | 416 | if (!fs_handle) { |
|
| - | 417 | if (flags & IPC_FLAG_BLOCKING) { |
|
| - | 418 | pending_req_t *pr; |
|
| - | 419 | ||
| - | 420 | /* Blocking mount, add to pending list */ |
|
| - | 421 | pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
|
| - | 422 | if (!pr) { |
|
| - | 423 | ipc_answer_0(callid, ENOMEM); |
|
| - | 424 | ipc_answer_0(rid, ENOMEM); |
|
| - | 425 | free(mp); |
|
| - | 426 | free(fs_name); |
|
| - | 427 | free(opts); |
|
| - | 428 | return; |
|
| - | 429 | } |
|
| - | 430 | ||
| - | 431 | pr->fs_name = fs_name; |
|
| - | 432 | pr->mp = mp; |
|
| - | 433 | pr->opts = opts; |
|
| - | 434 | pr->callid = callid; |
|
| - | 435 | pr->rid = rid; |
|
| - | 436 | pr->dev_handle = dev_handle; |
|
| - | 437 | link_initialize(&pr->link); |
|
| - | 438 | list_append(&pr->link, &pending_req); |
|
| - | 439 | return; |
|
| - | 440 | } |
|
| - | 441 | ||
| - | 442 | ipc_answer_0(callid, ENOENT); |
|
| - | 443 | ipc_answer_0(rid, ENOENT); |
|
| - | 444 | free(mp); |
|
| - | 445 | free(fs_name); |
|
| - | 446 | free(opts); |
|
| - | 447 | return; |
|
| - | 448 | } |
|
| - | 449 | ||
| - | 450 | /* Acknowledge that we know fs_name. */ |
|
| - | 451 | ipc_answer_0(callid, EOK); |
|
| - | 452 | ||
| - | 453 | /* Do the mount */ |
|
| - | 454 | vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts); |
|
| - | 455 | free(mp); |
|
| - | 456 | free(fs_name); |
|
| - | 457 | free(opts); |
|
| - | 458 | } |
|
| - | 459 | ||
| 286 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
460 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
| 287 | { |
461 | { |
| 288 | if (!vfs_files_init()) { |
462 | if (!vfs_files_init()) { |
| 289 | ipc_answer_0(rid, ENOMEM); |
463 | ipc_answer_0(rid, ENOMEM); |
| 290 | return; |
464 | return; |
| Line 721... | Line 895... | ||
| 721 | ipc_answer_0(rid, EOK); |
895 | ipc_answer_0(rid, EOK); |
| 722 | } |
896 | } |
| 723 | 897 | ||
| 724 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
898 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
| 725 | { |
899 | { |
| 726 | size_t len; |
900 | size_t olen, nlen; |
| 727 | ipc_callid_t callid; |
901 | ipc_callid_t callid; |
| 728 | int rc; |
902 | int rc; |
| 729 | 903 | ||
| 730 | /* Retrieve the old path. */ |
904 | /* Retrieve the old path. */ |
| 731 | if (!ipc_data_write_receive(&callid, &len)) { |
905 | if (!ipc_data_write_receive(&callid, &olen)) { |
| 732 | ipc_answer_0(callid, EINVAL); |
906 | ipc_answer_0(callid, EINVAL); |
| 733 | ipc_answer_0(rid, EINVAL); |
907 | ipc_answer_0(rid, EINVAL); |
| 734 | return; |
908 | return; |
| 735 | } |
909 | } |
| 736 | char *old = malloc(len + 1); |
910 | char *old = malloc(olen + 1); |
| 737 | if (!old) { |
911 | if (!old) { |
| 738 | ipc_answer_0(callid, ENOMEM); |
912 | ipc_answer_0(callid, ENOMEM); |
| 739 | ipc_answer_0(rid, ENOMEM); |
913 | ipc_answer_0(rid, ENOMEM); |
| 740 | return; |
914 | return; |
| 741 | } |
915 | } |
| 742 | if ((rc = ipc_data_write_finalize(callid, old, len))) { |
916 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
| 743 | ipc_answer_0(rid, rc); |
917 | ipc_answer_0(rid, rc); |
| 744 | free(old); |
918 | free(old); |
| 745 | return; |
919 | return; |
| 746 | } |
920 | } |
| 747 | old[len] = '\0'; |
921 | old[olen] = '\0'; |
| 748 | 922 | ||
| 749 | /* Retrieve the new path. */ |
923 | /* Retrieve the new path. */ |
| 750 | if (!ipc_data_write_receive(&callid, &len)) { |
924 | if (!ipc_data_write_receive(&callid, &nlen)) { |
| 751 | ipc_answer_0(callid, EINVAL); |
925 | ipc_answer_0(callid, EINVAL); |
| 752 | ipc_answer_0(rid, EINVAL); |
926 | ipc_answer_0(rid, EINVAL); |
| 753 | free(old); |
927 | free(old); |
| 754 | return; |
928 | return; |
| 755 | } |
929 | } |
| 756 | char *new = malloc(len + 1); |
930 | char *new = malloc(nlen + 1); |
| 757 | if (!new) { |
931 | if (!new) { |
| 758 | ipc_answer_0(callid, ENOMEM); |
932 | ipc_answer_0(callid, ENOMEM); |
| 759 | ipc_answer_0(rid, ENOMEM); |
933 | ipc_answer_0(rid, ENOMEM); |
| 760 | free(old); |
934 | free(old); |
| 761 | return; |
935 | return; |
| 762 | } |
936 | } |
| 763 | if ((rc = ipc_data_write_finalize(callid, new, len))) { |
937 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
| 764 | ipc_answer_0(rid, rc); |
938 | ipc_answer_0(rid, rc); |
| 765 | free(old); |
939 | free(old); |
| 766 | free(new); |
940 | free(new); |
| 767 | return; |
941 | return; |
| 768 | } |
942 | } |
| 769 | new[len] = '\0'; |
943 | new[nlen] = '\0'; |
| 770 | 944 | ||
| 771 | char *oldc = canonify(old, &len); |
945 | char *oldc = canonify(old, &olen); |
| 772 | char *newc = canonify(new, NULL); |
946 | char *newc = canonify(new, &nlen); |
| 773 | if (!oldc || !newc) { |
947 | if (!oldc || !newc) { |
| 774 | ipc_answer_0(rid, EINVAL); |
948 | ipc_answer_0(rid, EINVAL); |
| 775 | free(old); |
949 | free(old); |
| 776 | free(new); |
950 | free(new); |
| 777 | return; |
951 | return; |
| 778 | } |
952 | } |
| - | 953 | oldc[olen] = '\0'; |
|
| - | 954 | newc[nlen] = '\0'; |
|
| 779 | if (!strncmp(newc, oldc, len)) { |
955 | if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
| - | 956 | ((newc[str_length(oldc)] == '/') || |
|
| - | 957 | (str_length(oldc) == 1) || |
|
| - | 958 | (str_length(oldc) == str_length(newc)))) { |
|
| - | 959 | /* |
|
| 780 | /* oldc is a prefix of newc */ |
960 | * oldc is a prefix of newc and either |
| - | 961 | * - newc continues with a / where oldc ends, or |
|
| - | 962 | * - oldc was / itself, or |
|
| - | 963 | * - oldc and newc are equal. |
|
| - | 964 | */ |
|
| 781 | ipc_answer_0(rid, EINVAL); |
965 | ipc_answer_0(rid, EINVAL); |
| 782 | free(old); |
966 | free(old); |
| 783 | free(new); |
967 | free(new); |
| 784 | return; |
968 | return; |
| 785 | } |
969 | } |
| Line 803... | Line 987... | ||
| 803 | ipc_answer_0(rid, ENOMEM); |
987 | ipc_answer_0(rid, ENOMEM); |
| 804 | free(old); |
988 | free(old); |
| 805 | free(new); |
989 | free(new); |
| 806 | return; |
990 | return; |
| 807 | } |
991 | } |
| - | 992 | /* Determine the path to the parent of the node with the new name. */ |
|
| - | 993 | char *parentc = str_dup(newc); |
|
| - | 994 | if (!parentc) { |
|
| - | 995 | rwlock_write_unlock(&namespace_rwlock); |
|
| - | 996 | ipc_answer_0(rid, rc); |
|
| - | 997 | free(old); |
|
| - | 998 | free(new); |
|
| - | 999 | return; |
|
| - | 1000 | } |
|
| - | 1001 | char *lastsl = str_rchr(parentc + 1, L'/'); |
|
| - | 1002 | if (lastsl) |
|
| - | 1003 | *lastsl = '\0'; |
|
| - | 1004 | else |
|
| - | 1005 | parentc[1] = '\0'; |
|
| 808 | /* Lookup parent of the new file name. */ |
1006 | /* Lookup parent of the new file name. */ |
| 809 | rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL); |
1007 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
| - | 1008 | free(parentc); /* not needed anymore */ |
|
| 810 | if (rc != EOK) { |
1009 | if (rc != EOK) { |
| 811 | rwlock_write_unlock(&namespace_rwlock); |
1010 | rwlock_write_unlock(&namespace_rwlock); |
| 812 | ipc_answer_0(rid, rc); |
1011 | ipc_answer_0(rid, rc); |
| 813 | free(old); |
1012 | free(old); |
| 814 | free(new); |
1013 | free(new); |