Rev 4439 | Rev 4668 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4439 | Rev 4537 | ||
|---|---|---|---|
| Line 42... | Line 42... | ||
| 42 | #include <stdio.h> |
42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
43 | #include <stdlib.h> |
| 44 | #include <string.h> |
44 | #include <string.h> |
| 45 | #include <bool.h> |
45 | #include <bool.h> |
| 46 | #include <futex.h> |
46 | #include <futex.h> |
| 47 | #include <rwlock.h> |
47 | #include <fibril_sync.h> |
| 48 | #include <libadt/list.h> |
48 | #include <adt/list.h> |
| 49 | #include <unistd.h> |
49 | #include <unistd.h> |
| 50 | #include <ctype.h> |
50 | #include <ctype.h> |
| 51 | #include <fcntl.h> |
51 | #include <fcntl.h> |
| 52 | #include <assert.h> |
52 | #include <assert.h> |
| 53 | #include <vfs/canonify.h> |
53 | #include <vfs/canonify.h> |
| Line 64... | Line 64... | ||
| 64 | ipc_callid_t callid; /**< Call ID waiting for the mount */ |
64 | ipc_callid_t callid; /**< Call ID waiting for the mount */ |
| 65 | ipc_callid_t rid; /**< Request ID */ |
65 | ipc_callid_t rid; /**< Request ID */ |
| 66 | dev_handle_t dev_handle; /**< Device handle */ |
66 | dev_handle_t dev_handle; /**< Device handle */ |
| 67 | } pending_req_t; |
67 | } pending_req_t; |
| 68 | 68 | ||
| - | 69 | FIBRIL_MUTEX_INITIALIZE(pending_lock); |
|
| 69 | LIST_INITIALIZE(pending_req); |
70 | LIST_INITIALIZE(pending_req); |
| 70 | 71 | ||
| 71 | /** |
72 | /** |
| 72 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
73 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
| 73 | * concurrent VFS operation which modifies the file system namespace. |
74 | * concurrent VFS operation which modifies the file system namespace. |
| 74 | */ |
75 | */ |
| 75 | RWLOCK_INITIALIZE(namespace_rwlock); |
76 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock); |
| 76 | 77 | ||
| 77 | vfs_pair_t rootfs = { |
78 | vfs_pair_t rootfs = { |
| 78 | .fs_handle = 0, |
79 | .fs_handle = 0, |
| 79 | .dev_handle = 0 |
80 | .dev_handle = 0 |
| 80 | }; |
81 | }; |
| Line 91... | Line 92... | ||
| 91 | unsigned rlnkcnt; |
92 | unsigned rlnkcnt; |
| 92 | ipcarg_t rc; |
93 | ipcarg_t rc; |
| 93 | int phone; |
94 | int phone; |
| 94 | aid_t msg; |
95 | aid_t msg; |
| 95 | ipc_call_t answer; |
96 | ipc_call_t answer; |
| 96 | 97 | ||
| 97 | /* Resolve the path to the mountpoint. */ |
98 | /* Resolve the path to the mountpoint. */ |
| 98 | rwlock_write_lock(&namespace_rwlock); |
99 | fibril_rwlock_write_lock(&namespace_rwlock); |
| 99 | if (rootfs.fs_handle) { |
100 | if (rootfs.fs_handle) { |
| 100 | /* We already have the root FS. */ |
101 | /* We already have the root FS. */ |
| 101 | if (str_cmp(mp, "/") == 0) { |
102 | if (str_cmp(mp, "/") == 0) { |
| 102 | /* Trying to mount root FS over root FS */ |
103 | /* Trying to mount root FS over root FS */ |
| - | 104 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 103 | ipc_answer_0(rid, EBUSY); |
105 | ipc_answer_0(rid, EBUSY); |
| 104 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 105 | return; |
106 | return; |
| 106 | } |
107 | } |
| 107 | 108 | ||
| 108 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
109 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
| 109 | if (rc != EOK) { |
110 | if (rc != EOK) { |
| 110 | /* The lookup failed for some reason. */ |
111 | /* The lookup failed for some reason. */ |
| - | 112 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 111 | ipc_answer_0(rid, rc); |
113 | ipc_answer_0(rid, rc); |
| 112 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 113 | return; |
114 | return; |
| 114 | } |
115 | } |
| 115 | 116 | ||
| 116 | mp_node = vfs_node_get(&mp_res); |
117 | mp_node = vfs_node_get(&mp_res); |
| 117 | if (!mp_node) { |
118 | if (!mp_node) { |
| - | 119 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 118 | ipc_answer_0(rid, ENOMEM); |
120 | ipc_answer_0(rid, ENOMEM); |
| 119 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 120 | return; |
121 | return; |
| 121 | } |
122 | } |
| 122 | 123 | ||
| 123 | /* |
124 | /* |
| 124 | * Now we hold a reference to mp_node. |
125 | * Now we hold a reference to mp_node. |
| Line 139... | Line 140... | ||
| 139 | (ipcarg_t) dev_handle, &answer); |
140 | (ipcarg_t) dev_handle, &answer); |
| 140 | /* send the mount options */ |
141 | /* send the mount options */ |
| 141 | rc = ipc_data_write_start(phone, (void *)opts, |
142 | rc = ipc_data_write_start(phone, (void *)opts, |
| 142 | str_size(opts)); |
143 | str_size(opts)); |
| 143 | if (rc != EOK) { |
144 | if (rc != EOK) { |
| 144 | async_wait_for(msg, NULL); |
- | |
| 145 | vfs_release_phone(phone); |
145 | vfs_release_phone(phone); |
| - | 146 | async_wait_for(msg, NULL); |
|
| - | 147 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 146 | ipc_answer_0(rid, rc); |
148 | ipc_answer_0(rid, rc); |
| 147 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 148 | return; |
149 | return; |
| 149 | } |
150 | } |
| 150 | async_wait_for(msg, &rc); |
- | |
| 151 | vfs_release_phone(phone); |
151 | vfs_release_phone(phone); |
| - | 152 | async_wait_for(msg, &rc); |
|
| 152 | 153 | ||
| 153 | if (rc != EOK) { |
154 | if (rc != EOK) { |
| - | 155 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 154 | ipc_answer_0(rid, rc); |
156 | ipc_answer_0(rid, rc); |
| 155 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 156 | return; |
157 | return; |
| 157 | } |
158 | } |
| 158 | 159 | ||
| 159 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
160 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
| 160 | rsize = (size_t) IPC_GET_ARG2(answer); |
161 | rsize = (size_t) IPC_GET_ARG2(answer); |
| Line 172... | Line 173... | ||
| 172 | 173 | ||
| 173 | /* Add reference to the mounted root. */ |
174 | /* Add reference to the mounted root. */ |
| 174 | mr_node = vfs_node_get(&mr_res); |
175 | mr_node = vfs_node_get(&mr_res); |
| 175 | assert(mr_node); |
176 | assert(mr_node); |
| 176 | 177 | ||
| - | 178 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 177 | ipc_answer_0(rid, rc); |
179 | ipc_answer_0(rid, rc); |
| 178 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 179 | return; |
180 | return; |
| 180 | } else { |
181 | } else { |
| 181 | /* |
182 | /* |
| 182 | * We can't resolve this without the root filesystem |
183 | * We can't resolve this without the root filesystem |
| 183 | * being mounted first. |
184 | * being mounted first. |
| 184 | */ |
185 | */ |
| - | 186 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 185 | ipc_answer_0(rid, ENOENT); |
187 | ipc_answer_0(rid, ENOENT); |
| 186 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 187 | return; |
188 | return; |
| 188 | } |
189 | } |
| 189 | } |
190 | } |
| 190 | 191 | ||
| 191 | /* |
192 | /* |
| Line 205... | Line 206... | ||
| 205 | (ipcarg_t) dev_handle, &answer); |
206 | (ipcarg_t) dev_handle, &answer); |
| 206 | 207 | ||
| 207 | /* send connection */ |
208 | /* send connection */ |
| 208 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
209 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
| 209 | if (rc != EOK) { |
210 | if (rc != EOK) { |
| 210 | async_wait_for(msg, NULL); |
- | |
| 211 | vfs_release_phone(phone); |
211 | vfs_release_phone(phone); |
| - | 212 | async_wait_for(msg, NULL); |
|
| 212 | /* Mount failed, drop reference to mp_node. */ |
213 | /* Mount failed, drop reference to mp_node. */ |
| 213 | if (mp_node) |
214 | if (mp_node) |
| 214 | vfs_node_put(mp_node); |
215 | vfs_node_put(mp_node); |
| 215 | ipc_answer_0(rid, rc); |
216 | ipc_answer_0(rid, rc); |
| 216 | rwlock_write_unlock(&namespace_rwlock); |
217 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 217 | return; |
218 | return; |
| 218 | } |
219 | } |
| 219 | 220 | ||
| 220 | /* send the mount options */ |
221 | /* send the mount options */ |
| 221 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
222 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
| 222 | if (rc != EOK) { |
223 | if (rc != EOK) { |
| 223 | async_wait_for(msg, NULL); |
- | |
| 224 | vfs_release_phone(phone); |
224 | vfs_release_phone(phone); |
| - | 225 | async_wait_for(msg, NULL); |
|
| 225 | /* Mount failed, drop reference to mp_node. */ |
226 | /* Mount failed, drop reference to mp_node. */ |
| 226 | if (mp_node) |
227 | if (mp_node) |
| 227 | vfs_node_put(mp_node); |
228 | vfs_node_put(mp_node); |
| - | 229 | fibril_rwlock_write_unlock(&namespace_rwlock); |
|
| 228 | ipc_answer_0(rid, rc); |
230 | ipc_answer_0(rid, rc); |
| 229 | rwlock_write_unlock(&namespace_rwlock); |
- | |
| 230 | return; |
231 | return; |
| 231 | } |
232 | } |
| 232 | async_wait_for(msg, &rc); |
- | |
| 233 | vfs_release_phone(phone); |
233 | vfs_release_phone(phone); |
| - | 234 | async_wait_for(msg, &rc); |
|
| 234 | 235 | ||
| 235 | if (rc == EOK) { |
236 | if (rc == EOK) { |
| 236 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
237 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
| 237 | rsize = (size_t) IPC_GET_ARG2(answer); |
238 | rsize = (size_t) IPC_GET_ARG2(answer); |
| 238 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
239 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
| Line 252... | Line 253... | ||
| 252 | if (mp_node) |
253 | if (mp_node) |
| 253 | vfs_node_put(mp_node); |
254 | vfs_node_put(mp_node); |
| 254 | } |
255 | } |
| 255 | 256 | ||
| 256 | ipc_answer_0(rid, rc); |
257 | ipc_answer_0(rid, rc); |
| 257 | rwlock_write_unlock(&namespace_rwlock); |
258 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 258 | } |
259 | } |
| 259 | 260 | ||
| 260 | /** Process pending mount requests */ |
261 | /** Process pending mount requests */ |
| 261 | void vfs_process_pending_mount(void) |
262 | void vfs_process_pending_mount(void) |
| 262 | { |
263 | { |
| 263 | link_t *cur; |
264 | link_t *cur; |
| 264 | 265 | ||
| 265 | loop: |
266 | loop: |
| - | 267 | fibril_mutex_lock(&pending_lock); |
|
| 266 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
268 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) { |
| 267 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
269 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link); |
| 268 | 270 | ||
| 269 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true); |
271 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true); |
| 270 | if (!fs_handle) |
272 | if (!fs_handle) |
| 271 | continue; |
273 | continue; |
| 272 | 274 | ||
| 273 | /* Acknowledge that we know fs_name. */ |
275 | /* Acknowledge that we know fs_name. */ |
| Line 280... | Line 282... | ||
| 280 | free(pr->fs_name); |
282 | free(pr->fs_name); |
| 281 | free(pr->mp); |
283 | free(pr->mp); |
| 282 | free(pr->opts); |
284 | free(pr->opts); |
| 283 | list_remove(cur); |
285 | list_remove(cur); |
| 284 | free(pr); |
286 | free(pr); |
| - | 287 | fibril_mutex_unlock(&pending_lock); |
|
| - | 288 | fibril_yield(); |
|
| 285 | goto loop; |
289 | goto loop; |
| 286 | } |
290 | } |
| - | 291 | fibril_mutex_unlock(&pending_lock); |
|
| 287 | } |
292 | } |
| 288 | 293 | ||
| 289 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
294 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
| 290 | { |
295 | { |
| 291 | /* |
296 | /* |
| Line 460... | Line 465... | ||
| 460 | pr->opts = opts; |
465 | pr->opts = opts; |
| 461 | pr->callid = callid; |
466 | pr->callid = callid; |
| 462 | pr->rid = rid; |
467 | pr->rid = rid; |
| 463 | pr->dev_handle = dev_handle; |
468 | pr->dev_handle = dev_handle; |
| 464 | link_initialize(&pr->link); |
469 | link_initialize(&pr->link); |
| - | 470 | fibril_mutex_lock(&pending_lock); |
|
| 465 | list_append(&pr->link, &pending_req); |
471 | list_append(&pr->link, &pending_req); |
| - | 472 | fibril_mutex_unlock(&pending_lock); |
|
| 466 | return; |
473 | return; |
| 467 | } |
474 | } |
| 468 | 475 | ||
| 469 | ipc_answer_0(callid, ENOENT); |
476 | ipc_answer_0(callid, ENOENT); |
| 470 | ipc_answer_0(rid, ENOENT); |
477 | ipc_answer_0(rid, ENOENT); |
| Line 488... | Line 495... | ||
| 488 | { |
495 | { |
| 489 | if (!vfs_files_init()) { |
496 | if (!vfs_files_init()) { |
| 490 | ipc_answer_0(rid, ENOMEM); |
497 | ipc_answer_0(rid, ENOMEM); |
| 491 | return; |
498 | return; |
| 492 | } |
499 | } |
| 493 | 500 | ||
| 494 | /* |
501 | /* |
| 495 | * The POSIX interface is open(path, oflag, mode). |
502 | * The POSIX interface is open(path, oflag, mode). |
| 496 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
503 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
| 497 | * will need to arrive in another call. |
504 | * will need to arrive in another call. |
| 498 | * |
505 | * |
| Line 501... | Line 508... | ||
| 501 | */ |
508 | */ |
| 502 | int lflag = IPC_GET_ARG1(*request); |
509 | int lflag = IPC_GET_ARG1(*request); |
| 503 | int oflag = IPC_GET_ARG2(*request); |
510 | int oflag = IPC_GET_ARG2(*request); |
| 504 | int mode = IPC_GET_ARG3(*request); |
511 | int mode = IPC_GET_ARG3(*request); |
| 505 | size_t len; |
512 | size_t len; |
| 506 | 513 | ||
| 507 | /* |
514 | /* |
| 508 | * Make sure that we are called with exactly one of L_FILE and |
515 | * Make sure that we are called with exactly one of L_FILE and |
| 509 | * L_DIRECTORY. |
516 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN. |
| 510 | */ |
517 | */ |
| 511 | if ((lflag & (L_FILE | L_DIRECTORY)) == 0 || |
518 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
| 512 | (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) { |
519 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
| - | 520 | ((lflag & L_OPEN) != 0)) { |
|
| 513 | ipc_answer_0(rid, EINVAL); |
521 | ipc_answer_0(rid, EINVAL); |
| 514 | return; |
522 | return; |
| 515 | } |
523 | } |
| 516 | 524 | ||
| 517 | if (oflag & O_CREAT) |
525 | if (oflag & O_CREAT) |
| 518 | lflag |= L_CREATE; |
526 | lflag |= L_CREATE; |
| 519 | if (oflag & O_EXCL) |
527 | if (oflag & O_EXCL) |
| 520 | lflag |= L_EXCLUSIVE; |
528 | lflag |= L_EXCLUSIVE; |
| 521 | 529 | ||
| 522 | ipc_callid_t callid; |
530 | ipc_callid_t callid; |
| 523 | - | ||
| 524 | if (!ipc_data_write_receive(&callid, &len)) { |
531 | if (!ipc_data_write_receive(&callid, &len)) { |
| 525 | ipc_answer_0(callid, EINVAL); |
532 | ipc_answer_0(callid, EINVAL); |
| 526 | ipc_answer_0(rid, EINVAL); |
533 | ipc_answer_0(rid, EINVAL); |
| 527 | return; |
534 | return; |
| 528 | } |
535 | } |
| - | 536 | ||
| 529 | char *path = malloc(len + 1); |
537 | char *path = malloc(len + 1); |
| 530 | if (!path) { |
538 | if (!path) { |
| 531 | ipc_answer_0(callid, ENOMEM); |
539 | ipc_answer_0(callid, ENOMEM); |
| 532 | ipc_answer_0(rid, ENOMEM); |
540 | ipc_answer_0(rid, ENOMEM); |
| 533 | return; |
541 | return; |
| 534 | } |
542 | } |
| - | 543 | ||
| 535 | int rc; |
544 | int rc; |
| 536 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
545 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
| 537 | ipc_answer_0(rid, rc); |
546 | ipc_answer_0(rid, rc); |
| 538 | free(path); |
547 | free(path); |
| 539 | return; |
548 | return; |
| Line 544... | Line 553... | ||
| 544 | * Avoid the race condition in which the file can be deleted before we |
553 | * Avoid the race condition in which the file can be deleted before we |
| 545 | * find/create-and-lock the VFS node corresponding to the looked-up |
554 | * find/create-and-lock the VFS node corresponding to the looked-up |
| 546 | * triplet. |
555 | * triplet. |
| 547 | */ |
556 | */ |
| 548 | if (lflag & L_CREATE) |
557 | if (lflag & L_CREATE) |
| 549 | rwlock_write_lock(&namespace_rwlock); |
558 | fibril_rwlock_write_lock(&namespace_rwlock); |
| 550 | else |
559 | else |
| 551 | rwlock_read_lock(&namespace_rwlock); |
560 | fibril_rwlock_read_lock(&namespace_rwlock); |
| 552 | 561 | ||
| 553 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
562 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
| 554 | vfs_lookup_res_t lr; |
563 | vfs_lookup_res_t lr; |
| 555 | rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
564 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
| 556 | if (rc) { |
565 | if (rc != EOK) { |
| 557 | if (lflag & L_CREATE) |
566 | if (lflag & L_CREATE) |
| 558 | rwlock_write_unlock(&namespace_rwlock); |
567 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 559 | else |
568 | else |
| 560 | rwlock_read_unlock(&namespace_rwlock); |
569 | fibril_rwlock_read_unlock(&namespace_rwlock); |
| 561 | ipc_answer_0(rid, rc); |
570 | ipc_answer_0(rid, rc); |
| 562 | free(path); |
571 | free(path); |
| 563 | return; |
572 | return; |
| 564 | } |
573 | } |
| 565 | 574 | ||
| 566 | /* Path is no longer needed. */ |
575 | /* Path is no longer needed. */ |
| 567 | free(path); |
576 | free(path); |
| 568 | 577 | ||
| 569 | vfs_node_t *node = vfs_node_get(&lr); |
578 | vfs_node_t *node = vfs_node_get(&lr); |
| 570 | if (lflag & L_CREATE) |
579 | if (lflag & L_CREATE) |
| 571 | rwlock_write_unlock(&namespace_rwlock); |
580 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 572 | else |
581 | else |
| 573 | rwlock_read_unlock(&namespace_rwlock); |
582 | fibril_rwlock_read_unlock(&namespace_rwlock); |
| 574 | 583 | ||
| 575 | /* Truncate the file if requested and if necessary. */ |
584 | /* Truncate the file if requested and if necessary. */ |
| 576 | if (oflag & O_TRUNC) { |
585 | if (oflag & O_TRUNC) { |
| 577 | rwlock_write_lock(&node->contents_rwlock); |
586 | fibril_rwlock_write_lock(&node->contents_rwlock); |
| 578 | if (node->size) { |
587 | if (node->size) { |
| 579 | rc = vfs_truncate_internal(node->fs_handle, |
588 | rc = vfs_truncate_internal(node->fs_handle, |
| 580 | node->dev_handle, node->index, 0); |
589 | node->dev_handle, node->index, 0); |
| 581 | if (rc) { |
590 | if (rc) { |
| 582 | rwlock_write_unlock(&node->contents_rwlock); |
591 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
| 583 | vfs_node_put(node); |
592 | vfs_node_put(node); |
| 584 | ipc_answer_0(rid, rc); |
593 | ipc_answer_0(rid, rc); |
| 585 | return; |
594 | return; |
| 586 | } |
595 | } |
| 587 | node->size = 0; |
596 | node->size = 0; |
| 588 | } |
597 | } |
| 589 | rwlock_write_unlock(&node->contents_rwlock); |
598 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
| 590 | } |
599 | } |
| 591 | 600 | ||
| 592 | /* |
601 | /* |
| 593 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
602 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
| 594 | * structure. |
603 | * structure. |
| 595 | */ |
604 | */ |
| 596 | int fd = vfs_fd_alloc(); |
605 | int fd = vfs_fd_alloc(); |
| Line 599... | Line 608... | ||
| 599 | ipc_answer_0(rid, fd); |
608 | ipc_answer_0(rid, fd); |
| 600 | return; |
609 | return; |
| 601 | } |
610 | } |
| 602 | vfs_file_t *file = vfs_file_get(fd); |
611 | vfs_file_t *file = vfs_file_get(fd); |
| 603 | file->node = node; |
612 | file->node = node; |
| 604 | if (oflag & O_APPEND) |
613 | if (oflag & O_APPEND) |
| 605 | file->append = true; |
614 | file->append = true; |
| 606 | 615 | ||
| 607 | /* |
616 | /* |
| 608 | * The following increase in reference count is for the fact that the |
617 | * The following increase in reference count is for the fact that the |
| 609 | * file is being opened and that a file structure is pointing to it. |
618 | * file is being opened and that a file structure is pointing to it. |
| 610 | * It is necessary so that the file will not disappear when |
619 | * It is necessary so that the file will not disappear when |
| 611 | * vfs_node_put() is called. The reference will be dropped by the |
620 | * vfs_node_put() is called. The reference will be dropped by the |
| 612 | * respective VFS_CLOSE. |
621 | * respective VFS_CLOSE. |
| 613 | */ |
622 | */ |
| 614 | vfs_node_addref(node); |
623 | vfs_node_addref(node); |
| 615 | vfs_node_put(node); |
624 | vfs_node_put(node); |
| - | 625 | ||
| - | 626 | /* Success! Return the new file descriptor to the client. */ |
|
| - | 627 | ipc_answer_1(rid, EOK, fd); |
|
| - | 628 | } |
|
| 616 | 629 | ||
| - | 630 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
|
| - | 631 | { |
|
| - | 632 | // FIXME: check for sanity of the supplied fs, dev and index |
|
| - | 633 | ||
| - | 634 | if (!vfs_files_init()) { |
|
| - | 635 | ipc_answer_0(rid, ENOMEM); |
|
| - | 636 | return; |
|
| - | 637 | } |
|
| - | 638 | ||
| - | 639 | /* |
|
| - | 640 | * The interface is open_node(fs, dev, index, oflag). |
|
| - | 641 | */ |
|
| - | 642 | vfs_lookup_res_t lr; |
|
| - | 643 | ||
| - | 644 | lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
|
| - | 645 | lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
|
| - | 646 | lr.triplet.index = IPC_GET_ARG3(*request); |
|
| - | 647 | int oflag = IPC_GET_ARG4(*request); |
|
| - | 648 | ||
| - | 649 | fibril_rwlock_read_lock(&namespace_rwlock); |
|
| - | 650 | ||
| - | 651 | int rc = vfs_open_node_internal(&lr); |
|
| - | 652 | if (rc != EOK) { |
|
| - | 653 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
| - | 654 | ipc_answer_0(rid, rc); |
|
| - | 655 | return; |
|
| - | 656 | } |
|
| - | 657 | ||
| - | 658 | vfs_node_t *node = vfs_node_get(&lr); |
|
| - | 659 | fibril_rwlock_read_unlock(&namespace_rwlock); |
|
| - | 660 | ||
| - | 661 | /* Truncate the file if requested and if necessary. */ |
|
| - | 662 | if (oflag & O_TRUNC) { |
|
| - | 663 | fibril_rwlock_write_lock(&node->contents_rwlock); |
|
| - | 664 | if (node->size) { |
|
| - | 665 | rc = vfs_truncate_internal(node->fs_handle, |
|
| - | 666 | node->dev_handle, node->index, 0); |
|
| - | 667 | if (rc) { |
|
| - | 668 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
|
| - | 669 | vfs_node_put(node); |
|
| - | 670 | ipc_answer_0(rid, rc); |
|
| - | 671 | return; |
|
| - | 672 | } |
|
| - | 673 | node->size = 0; |
|
| - | 674 | } |
|
| - | 675 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
|
| - | 676 | } |
|
| - | 677 | ||
| - | 678 | /* |
|
| - | 679 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
|
| - | 680 | * structure. |
|
| - | 681 | */ |
|
| - | 682 | int fd = vfs_fd_alloc(); |
|
| - | 683 | if (fd < 0) { |
|
| - | 684 | vfs_node_put(node); |
|
| - | 685 | ipc_answer_0(rid, fd); |
|
| - | 686 | return; |
|
| - | 687 | } |
|
| - | 688 | vfs_file_t *file = vfs_file_get(fd); |
|
| - | 689 | file->node = node; |
|
| - | 690 | if (oflag & O_APPEND) |
|
| - | 691 | file->append = true; |
|
| - | 692 | ||
| - | 693 | /* |
|
| - | 694 | * The following increase in reference count is for the fact that the |
|
| - | 695 | * file is being opened and that a file structure is pointing to it. |
|
| - | 696 | * It is necessary so that the file will not disappear when |
|
| - | 697 | * vfs_node_put() is called. The reference will be dropped by the |
|
| - | 698 | * respective VFS_CLOSE. |
|
| - | 699 | */ |
|
| - | 700 | vfs_node_addref(node); |
|
| - | 701 | vfs_node_put(node); |
|
| - | 702 | ||
| 617 | /* Success! Return the new file descriptor to the client. */ |
703 | /* Success! Return the new file descriptor to the client. */ |
| 618 | ipc_answer_1(rid, EOK, fd); |
704 | ipc_answer_1(rid, EOK, fd); |
| 619 | } |
705 | } |
| 620 | 706 | ||
| 621 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
707 | void vfs_node(ipc_callid_t rid, ipc_call_t *request) |
| - | 708 | { |
|
| - | 709 | int fd = IPC_GET_ARG1(*request); |
|
| - | 710 | ||
| - | 711 | /* Lookup the file structure corresponding to the file descriptor. */ |
|
| - | 712 | vfs_file_t *file = vfs_file_get(fd); |
|
| - | 713 | if (!file) { |
|
| - | 714 | ipc_answer_0(rid, ENOENT); |
|
| - | 715 | return; |
|
| - | 716 | } |
|
| - | 717 | ||
| - | 718 | ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle, |
|
| - | 719 | file->node->index); |
|
| - | 720 | } |
|
| - | 721 | ||
| - | 722 | void vfs_device(ipc_callid_t rid, ipc_call_t *request) |
|
| - | 723 | { |
|
| - | 724 | int fd = IPC_GET_ARG1(*request); |
|
| - | 725 | ||
| - | 726 | /* Lookup the file structure corresponding to the file descriptor. */ |
|
| - | 727 | vfs_file_t *file = vfs_file_get(fd); |
|
| - | 728 | if (!file) { |
|
| - | 729 | ipc_answer_0(rid, ENOENT); |
|
| - | 730 | return; |
|
| - | 731 | } |
|
| - | 732 | ||
| - | 733 | /* |
|
| - | 734 | * Lock the open file structure so that no other thread can manipulate |
|
| - | 735 | * the same open file at a time. |
|
| - | 736 | */ |
|
| - | 737 | fibril_mutex_lock(&file->lock); |
|
| - | 738 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
| - | 739 | ||
| - | 740 | /* Make a VFS_DEVICE request at the destination FS server. */ |
|
| - | 741 | aid_t msg; |
|
| - | 742 | ipc_call_t answer; |
|
| - | 743 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
|
| - | 744 | file->node->dev_handle, file->node->index, &answer); |
|
| - | 745 | ||
| - | 746 | vfs_release_phone(fs_phone); |
|
| - | 747 | ||
| - | 748 | /* Wait for reply from the FS server. */ |
|
| - | 749 | ipcarg_t rc; |
|
| - | 750 | async_wait_for(msg, &rc); |
|
| - | 751 | ||
| - | 752 | fibril_mutex_unlock(&file->lock); |
|
| - | 753 | ||
| - | 754 | ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer)); |
|
| - | 755 | } |
|
| - | 756 | ||
| - | 757 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
|
| 622 | { |
758 | { |
| 623 | int fd = IPC_GET_ARG1(*request); |
759 | int fd = IPC_GET_ARG1(*request); |
| - | 760 | ||
| - | 761 | /* Lookup the file structure corresponding to the file descriptor. */ |
|
| 624 | int rc = vfs_fd_free(fd); |
762 | vfs_file_t *file = vfs_file_get(fd); |
| - | 763 | if (!file) { |
|
| - | 764 | ipc_answer_0(rid, ENOENT); |
|
| - | 765 | return; |
|
| - | 766 | } |
|
| - | 767 | ||
| - | 768 | /* |
|
| - | 769 | * Lock the open file structure so that no other thread can manipulate |
|
| - | 770 | * the same open file at a time. |
|
| - | 771 | */ |
|
| - | 772 | fibril_mutex_lock(&file->lock); |
|
| - | 773 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
| - | 774 | ||
| - | 775 | /* Make a VFS_SYMC request at the destination FS server. */ |
|
| - | 776 | aid_t msg; |
|
| - | 777 | ipc_call_t answer; |
|
| - | 778 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
|
| - | 779 | file->node->dev_handle, file->node->index, &answer); |
|
| - | 780 | ||
| - | 781 | vfs_release_phone(fs_phone); |
|
| - | 782 | ||
| - | 783 | /* Wait for reply from the FS server. */ |
|
| - | 784 | ipcarg_t rc; |
|
| - | 785 | async_wait_for(msg, &rc); |
|
| - | 786 | ||
| - | 787 | fibril_mutex_unlock(&file->lock); |
|
| - | 788 | ||
| 625 | ipc_answer_0(rid, rc); |
789 | ipc_answer_0(rid, rc); |
| 626 | } |
790 | } |
| 627 | 791 | ||
| - | 792 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
|
| - | 793 | { |
|
| - | 794 | int fd = IPC_GET_ARG1(*request); |
|
| - | 795 | ||
| - | 796 | /* Lookup the file structure corresponding to the file descriptor. */ |
|
| - | 797 | vfs_file_t *file = vfs_file_get(fd); |
|
| - | 798 | if (!file) { |
|
| - | 799 | ipc_answer_0(rid, ENOENT); |
|
| - | 800 | return; |
|
| - | 801 | } |
|
| - | 802 | ||
| - | 803 | /* |
|
| - | 804 | * Lock the open file structure so that no other thread can manipulate |
|
| - | 805 | * the same open file at a time. |
|
| - | 806 | */ |
|
| - | 807 | fibril_mutex_lock(&file->lock); |
|
| - | 808 | ||
| - | 809 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
| - | 810 | ||
| - | 811 | /* Make a VFS_CLOSE request at the destination FS server. */ |
|
| - | 812 | aid_t msg; |
|
| - | 813 | ipc_call_t answer; |
|
| - | 814 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
|
| - | 815 | file->node->dev_handle, file->node->index, &answer); |
|
| - | 816 | ||
| - | 817 | vfs_release_phone(fs_phone); |
|
| - | 818 | ||
| - | 819 | /* Wait for reply from the FS server. */ |
|
| - | 820 | ipcarg_t rc; |
|
| - | 821 | async_wait_for(msg, &rc); |
|
| - | 822 | ||
| - | 823 | fibril_mutex_unlock(&file->lock); |
|
| - | 824 | ||
| - | 825 | int retval = IPC_GET_ARG1(answer); |
|
| - | 826 | if (retval != EOK) |
|
| - | 827 | ipc_answer_0(rid, retval); |
|
| - | 828 | ||
| - | 829 | retval = vfs_fd_free(fd); |
|
| - | 830 | ipc_answer_0(rid, retval); |
|
| - | 831 | } |
|
| - | 832 | ||
| 628 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
833 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
| 629 | { |
834 | { |
| 630 | 835 | ||
| 631 | /* |
836 | /* |
| 632 | * The following code strongly depends on the fact that the files data |
837 | * The following code strongly depends on the fact that the files data |
| Line 665... | Line 870... | ||
| 665 | 870 | ||
| 666 | /* |
871 | /* |
| 667 | * Lock the open file structure so that no other thread can manipulate |
872 | * Lock the open file structure so that no other thread can manipulate |
| 668 | * the same open file at a time. |
873 | * the same open file at a time. |
| 669 | */ |
874 | */ |
| 670 | futex_down(&file->lock); |
875 | fibril_mutex_lock(&file->lock); |
| 671 | 876 | ||
| 672 | /* |
877 | /* |
| 673 | * Lock the file's node so that no other client can read/write to it at |
878 | * Lock the file's node so that no other client can read/write to it at |
| 674 | * the same time. |
879 | * the same time. |
| 675 | */ |
880 | */ |
| 676 | if (read) |
881 | if (read) |
| 677 | rwlock_read_lock(&file->node->contents_rwlock); |
882 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
| 678 | else |
883 | else |
| 679 | rwlock_write_lock(&file->node->contents_rwlock); |
884 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
| 680 | 885 | ||
| 681 | if (file->node->type == VFS_NODE_DIRECTORY) { |
886 | if (file->node->type == VFS_NODE_DIRECTORY) { |
| 682 | /* |
887 | /* |
| 683 | * Make sure that no one is modifying the namespace |
888 | * Make sure that no one is modifying the namespace |
| 684 | * while we are in readdir(). |
889 | * while we are in readdir(). |
| 685 | */ |
890 | */ |
| 686 | assert(read); |
891 | assert(read); |
| 687 | rwlock_read_lock(&namespace_rwlock); |
892 | fibril_rwlock_read_lock(&namespace_rwlock); |
| 688 | } |
893 | } |
| 689 | 894 | ||
| 690 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
895 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
| 691 | 896 | ||
| 692 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
897 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
| Line 702... | Line 907... | ||
| 702 | * destination FS server. The call will be routed as if sent by |
907 | * destination FS server. The call will be routed as if sent by |
| 703 | * ourselves. Note that call arguments are immutable in this case so we |
908 | * ourselves. Note that call arguments are immutable in this case so we |
| 704 | * don't have to bother. |
909 | * don't have to bother. |
| 705 | */ |
910 | */ |
| 706 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
911 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
| 707 | 912 | ||
| 708 | vfs_release_phone(fs_phone); |
913 | vfs_release_phone(fs_phone); |
| 709 | 914 | ||
| 710 | /* Wait for reply from the FS server. */ |
915 | /* Wait for reply from the FS server. */ |
| 711 | ipcarg_t rc; |
916 | ipcarg_t rc; |
| 712 | async_wait_for(msg, &rc); |
917 | async_wait_for(msg, &rc); |
| - | 918 | ||
| 713 | size_t bytes = IPC_GET_ARG1(answer); |
919 | size_t bytes = IPC_GET_ARG1(answer); |
| 714 | 920 | ||
| 715 | if (file->node->type == VFS_NODE_DIRECTORY) |
921 | if (file->node->type == VFS_NODE_DIRECTORY) |
| 716 | rwlock_read_unlock(&namespace_rwlock); |
922 | fibril_rwlock_read_unlock(&namespace_rwlock); |
| 717 | 923 | ||
| 718 | /* Unlock the VFS node. */ |
924 | /* Unlock the VFS node. */ |
| 719 | if (read) |
925 | if (read) |
| 720 | rwlock_read_unlock(&file->node->contents_rwlock); |
926 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
| 721 | else { |
927 | else { |
| 722 | /* Update the cached version of node's size. */ |
928 | /* Update the cached version of node's size. */ |
| 723 | if (rc == EOK) |
929 | if (rc == EOK) |
| 724 | file->node->size = IPC_GET_ARG2(answer); |
930 | file->node->size = IPC_GET_ARG2(answer); |
| 725 | rwlock_write_unlock(&file->node->contents_rwlock); |
931 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
| 726 | } |
932 | } |
| 727 | 933 | ||
| 728 | /* Update the position pointer and unlock the open file. */ |
934 | /* Update the position pointer and unlock the open file. */ |
| 729 | if (rc == EOK) |
935 | if (rc == EOK) |
| 730 | file->pos += bytes; |
936 | file->pos += bytes; |
| 731 | futex_up(&file->lock); |
937 | fibril_mutex_unlock(&file->lock); |
| 732 | 938 | ||
| 733 | /* |
939 | /* |
| 734 | * FS server's reply is the final result of the whole operation we |
940 | * FS server's reply is the final result of the whole operation we |
| 735 | * return to the client. |
941 | * return to the client. |
| 736 | */ |
942 | */ |
| Line 760... | Line 966... | ||
| 760 | ipc_answer_0(rid, ENOENT); |
966 | ipc_answer_0(rid, ENOENT); |
| 761 | return; |
967 | return; |
| 762 | } |
968 | } |
| 763 | 969 | ||
| 764 | off_t newpos; |
970 | off_t newpos; |
| 765 | futex_down(&file->lock); |
971 | fibril_mutex_lock(&file->lock); |
| 766 | if (whence == SEEK_SET) { |
972 | if (whence == SEEK_SET) { |
| 767 | file->pos = off; |
973 | file->pos = off; |
| 768 | futex_up(&file->lock); |
974 | fibril_mutex_unlock(&file->lock); |
| 769 | ipc_answer_1(rid, EOK, off); |
975 | ipc_answer_1(rid, EOK, off); |
| 770 | return; |
976 | return; |
| 771 | } |
977 | } |
| 772 | if (whence == SEEK_CUR) { |
978 | if (whence == SEEK_CUR) { |
| 773 | if (file->pos + off < file->pos) { |
979 | if (file->pos + off < file->pos) { |
| 774 | futex_up(&file->lock); |
980 | fibril_mutex_unlock(&file->lock); |
| 775 | ipc_answer_0(rid, EOVERFLOW); |
981 | ipc_answer_0(rid, EOVERFLOW); |
| 776 | return; |
982 | return; |
| 777 | } |
983 | } |
| 778 | file->pos += off; |
984 | file->pos += off; |
| 779 | newpos = file->pos; |
985 | newpos = file->pos; |
| 780 | futex_up(&file->lock); |
986 | fibril_mutex_unlock(&file->lock); |
| 781 | ipc_answer_1(rid, EOK, newpos); |
987 | ipc_answer_1(rid, EOK, newpos); |
| 782 | return; |
988 | return; |
| 783 | } |
989 | } |
| 784 | if (whence == SEEK_END) { |
990 | if (whence == SEEK_END) { |
| 785 | rwlock_read_lock(&file->node->contents_rwlock); |
991 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
| 786 | size_t size = file->node->size; |
992 | size_t size = file->node->size; |
| 787 | rwlock_read_unlock(&file->node->contents_rwlock); |
993 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
| 788 | if (size + off < size) { |
994 | if (size + off < size) { |
| 789 | futex_up(&file->lock); |
995 | fibril_mutex_unlock(&file->lock); |
| 790 | ipc_answer_0(rid, EOVERFLOW); |
996 | ipc_answer_0(rid, EOVERFLOW); |
| 791 | return; |
997 | return; |
| 792 | } |
998 | } |
| 793 | newpos = size + off; |
999 | newpos = size + off; |
| 794 | futex_up(&file->lock); |
1000 | fibril_mutex_unlock(&file->lock); |
| 795 | ipc_answer_1(rid, EOK, newpos); |
1001 | ipc_answer_1(rid, EOK, newpos); |
| 796 | return; |
1002 | return; |
| 797 | } |
1003 | } |
| 798 | futex_up(&file->lock); |
1004 | fibril_mutex_unlock(&file->lock); |
| 799 | ipc_answer_0(rid, EINVAL); |
1005 | ipc_answer_0(rid, EINVAL); |
| 800 | } |
1006 | } |
| 801 | 1007 | ||
| 802 | int |
1008 | int |
| 803 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
1009 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
| Line 822... | Line 1028... | ||
| 822 | vfs_file_t *file = vfs_file_get(fd); |
1028 | vfs_file_t *file = vfs_file_get(fd); |
| 823 | if (!file) { |
1029 | if (!file) { |
| 824 | ipc_answer_0(rid, ENOENT); |
1030 | ipc_answer_0(rid, ENOENT); |
| 825 | return; |
1031 | return; |
| 826 | } |
1032 | } |
| 827 | futex_down(&file->lock); |
1033 | fibril_mutex_lock(&file->lock); |
| 828 | 1034 | ||
| 829 | rwlock_write_lock(&file->node->contents_rwlock); |
1035 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
| 830 | rc = vfs_truncate_internal(file->node->fs_handle, |
1036 | rc = vfs_truncate_internal(file->node->fs_handle, |
| 831 | file->node->dev_handle, file->node->index, size); |
1037 | file->node->dev_handle, file->node->index, size); |
| 832 | if (rc == EOK) |
1038 | if (rc == EOK) |
| 833 | file->node->size = size; |
1039 | file->node->size = size; |
| 834 | rwlock_write_unlock(&file->node->contents_rwlock); |
1040 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
| 835 | 1041 | ||
| 836 | futex_up(&file->lock); |
1042 | fibril_mutex_unlock(&file->lock); |
| 837 | ipc_answer_0(rid, (ipcarg_t)rc); |
1043 | ipc_answer_0(rid, (ipcarg_t)rc); |
| 838 | } |
1044 | } |
| 839 | 1045 | ||
| 840 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
1046 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
| 841 | { |
1047 | { |
| Line 861... | Line 1067... | ||
| 861 | free(path); |
1067 | free(path); |
| 862 | return; |
1068 | return; |
| 863 | } |
1069 | } |
| 864 | path[len] = '\0'; |
1070 | path[len] = '\0'; |
| 865 | 1071 | ||
| 866 | rwlock_write_lock(&namespace_rwlock); |
1072 | fibril_rwlock_write_lock(&namespace_rwlock); |
| 867 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
1073 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
| 868 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
1074 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
| 869 | rwlock_write_unlock(&namespace_rwlock); |
1075 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 870 | free(path); |
1076 | free(path); |
| 871 | ipc_answer_0(rid, rc); |
1077 | ipc_answer_0(rid, rc); |
| 872 | } |
1078 | } |
| 873 | 1079 | ||
| 874 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
1080 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
| Line 895... | Line 1101... | ||
| 895 | free(path); |
1101 | free(path); |
| 896 | return; |
1102 | return; |
| 897 | } |
1103 | } |
| 898 | path[len] = '\0'; |
1104 | path[len] = '\0'; |
| 899 | 1105 | ||
| 900 | rwlock_write_lock(&namespace_rwlock); |
1106 | fibril_rwlock_write_lock(&namespace_rwlock); |
| 901 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
1107 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
| 902 | vfs_lookup_res_t lr; |
1108 | vfs_lookup_res_t lr; |
| 903 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
1109 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
| 904 | free(path); |
1110 | free(path); |
| 905 | if (rc != EOK) { |
1111 | if (rc != EOK) { |
| 906 | rwlock_write_unlock(&namespace_rwlock); |
1112 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 907 | ipc_answer_0(rid, rc); |
1113 | ipc_answer_0(rid, rc); |
| 908 | return; |
1114 | return; |
| 909 | } |
1115 | } |
| 910 | 1116 | ||
| 911 | /* |
1117 | /* |
| Line 915... | Line 1121... | ||
| 915 | */ |
1121 | */ |
| 916 | vfs_node_t *node = vfs_node_get(&lr); |
1122 | vfs_node_t *node = vfs_node_get(&lr); |
| 917 | futex_down(&nodes_futex); |
1123 | futex_down(&nodes_futex); |
| 918 | node->lnkcnt--; |
1124 | node->lnkcnt--; |
| 919 | futex_up(&nodes_futex); |
1125 | futex_up(&nodes_futex); |
| 920 | rwlock_write_unlock(&namespace_rwlock); |
1126 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 921 | vfs_node_put(node); |
1127 | vfs_node_put(node); |
| 922 | ipc_answer_0(rid, EOK); |
1128 | ipc_answer_0(rid, EOK); |
| 923 | } |
1129 | } |
| 924 | 1130 | ||
| 925 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
1131 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
| Line 996... | Line 1202... | ||
| 996 | } |
1202 | } |
| 997 | 1203 | ||
| 998 | vfs_lookup_res_t old_lr; |
1204 | vfs_lookup_res_t old_lr; |
| 999 | vfs_lookup_res_t new_lr; |
1205 | vfs_lookup_res_t new_lr; |
| 1000 | vfs_lookup_res_t new_par_lr; |
1206 | vfs_lookup_res_t new_par_lr; |
| 1001 | rwlock_write_lock(&namespace_rwlock); |
1207 | fibril_rwlock_write_lock(&namespace_rwlock); |
| 1002 | /* Lookup the node belonging to the old file name. */ |
1208 | /* Lookup the node belonging to the old file name. */ |
| 1003 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
1209 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
| 1004 | if (rc != EOK) { |
1210 | if (rc != EOK) { |
| 1005 | rwlock_write_unlock(&namespace_rwlock); |
1211 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1006 | ipc_answer_0(rid, rc); |
1212 | ipc_answer_0(rid, rc); |
| 1007 | free(old); |
1213 | free(old); |
| 1008 | free(new); |
1214 | free(new); |
| 1009 | return; |
1215 | return; |
| 1010 | } |
1216 | } |
| 1011 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
1217 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
| 1012 | if (!old_node) { |
1218 | if (!old_node) { |
| 1013 | rwlock_write_unlock(&namespace_rwlock); |
1219 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1014 | ipc_answer_0(rid, ENOMEM); |
1220 | ipc_answer_0(rid, ENOMEM); |
| 1015 | free(old); |
1221 | free(old); |
| 1016 | free(new); |
1222 | free(new); |
| 1017 | return; |
1223 | return; |
| 1018 | } |
1224 | } |
| 1019 | /* Determine the path to the parent of the node with the new name. */ |
1225 | /* Determine the path to the parent of the node with the new name. */ |
| 1020 | char *parentc = str_dup(newc); |
1226 | char *parentc = str_dup(newc); |
| 1021 | if (!parentc) { |
1227 | if (!parentc) { |
| 1022 | rwlock_write_unlock(&namespace_rwlock); |
1228 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1023 | ipc_answer_0(rid, rc); |
1229 | ipc_answer_0(rid, rc); |
| 1024 | free(old); |
1230 | free(old); |
| 1025 | free(new); |
1231 | free(new); |
| 1026 | return; |
1232 | return; |
| 1027 | } |
1233 | } |
| Line 1032... | Line 1238... | ||
| 1032 | parentc[1] = '\0'; |
1238 | parentc[1] = '\0'; |
| 1033 | /* Lookup parent of the new file name. */ |
1239 | /* Lookup parent of the new file name. */ |
| 1034 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
1240 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
| 1035 | free(parentc); /* not needed anymore */ |
1241 | free(parentc); /* not needed anymore */ |
| 1036 | if (rc != EOK) { |
1242 | if (rc != EOK) { |
| 1037 | rwlock_write_unlock(&namespace_rwlock); |
1243 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1038 | ipc_answer_0(rid, rc); |
1244 | ipc_answer_0(rid, rc); |
| 1039 | free(old); |
1245 | free(old); |
| 1040 | free(new); |
1246 | free(new); |
| 1041 | return; |
1247 | return; |
| 1042 | } |
1248 | } |
| 1043 | /* Check whether linking to the same file system instance. */ |
1249 | /* Check whether linking to the same file system instance. */ |
| 1044 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
1250 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
| 1045 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
1251 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
| 1046 | rwlock_write_unlock(&namespace_rwlock); |
1252 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1047 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
1253 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
| 1048 | free(old); |
1254 | free(old); |
| 1049 | free(new); |
1255 | free(new); |
| 1050 | return; |
1256 | return; |
| 1051 | } |
1257 | } |
| Line 1057... | Line 1263... | ||
| 1057 | /* simply not in our way */ |
1263 | /* simply not in our way */ |
| 1058 | break; |
1264 | break; |
| 1059 | case EOK: |
1265 | case EOK: |
| 1060 | new_node = vfs_node_get(&new_lr); |
1266 | new_node = vfs_node_get(&new_lr); |
| 1061 | if (!new_node) { |
1267 | if (!new_node) { |
| 1062 | rwlock_write_unlock(&namespace_rwlock); |
1268 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1063 | ipc_answer_0(rid, ENOMEM); |
1269 | ipc_answer_0(rid, ENOMEM); |
| 1064 | free(old); |
1270 | free(old); |
| 1065 | free(new); |
1271 | free(new); |
| 1066 | return; |
1272 | return; |
| 1067 | } |
1273 | } |
| 1068 | futex_down(&nodes_futex); |
1274 | futex_down(&nodes_futex); |
| 1069 | new_node->lnkcnt--; |
1275 | new_node->lnkcnt--; |
| 1070 | futex_up(&nodes_futex); |
1276 | futex_up(&nodes_futex); |
| 1071 | break; |
1277 | break; |
| 1072 | default: |
1278 | default: |
| 1073 | rwlock_write_unlock(&namespace_rwlock); |
1279 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1074 | ipc_answer_0(rid, ENOTEMPTY); |
1280 | ipc_answer_0(rid, ENOTEMPTY); |
| 1075 | free(old); |
1281 | free(old); |
| 1076 | free(new); |
1282 | free(new); |
| 1077 | return; |
1283 | return; |
| 1078 | } |
1284 | } |
| 1079 | /* Create the new link for the new name. */ |
1285 | /* Create the new link for the new name. */ |
| 1080 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
1286 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
| 1081 | if (rc != EOK) { |
1287 | if (rc != EOK) { |
| 1082 | rwlock_write_unlock(&namespace_rwlock); |
1288 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1083 | if (new_node) |
1289 | if (new_node) |
| 1084 | vfs_node_put(new_node); |
1290 | vfs_node_put(new_node); |
| 1085 | ipc_answer_0(rid, rc); |
1291 | ipc_answer_0(rid, rc); |
| 1086 | free(old); |
1292 | free(old); |
| 1087 | free(new); |
1293 | free(new); |
| Line 1091... | Line 1297... | ||
| 1091 | old_node->lnkcnt++; |
1297 | old_node->lnkcnt++; |
| 1092 | futex_up(&nodes_futex); |
1298 | futex_up(&nodes_futex); |
| 1093 | /* Destroy the link for the old name. */ |
1299 | /* Destroy the link for the old name. */ |
| 1094 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
1300 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
| 1095 | if (rc != EOK) { |
1301 | if (rc != EOK) { |
| 1096 | rwlock_write_unlock(&namespace_rwlock); |
1302 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1097 | vfs_node_put(old_node); |
1303 | vfs_node_put(old_node); |
| 1098 | if (new_node) |
1304 | if (new_node) |
| 1099 | vfs_node_put(new_node); |
1305 | vfs_node_put(new_node); |
| 1100 | ipc_answer_0(rid, rc); |
1306 | ipc_answer_0(rid, rc); |
| 1101 | free(old); |
1307 | free(old); |
| Line 1103... | Line 1309... | ||
| 1103 | return; |
1309 | return; |
| 1104 | } |
1310 | } |
| 1105 | futex_down(&nodes_futex); |
1311 | futex_down(&nodes_futex); |
| 1106 | old_node->lnkcnt--; |
1312 | old_node->lnkcnt--; |
| 1107 | futex_up(&nodes_futex); |
1313 | futex_up(&nodes_futex); |
| 1108 | rwlock_write_unlock(&namespace_rwlock); |
1314 | fibril_rwlock_write_unlock(&namespace_rwlock); |
| 1109 | vfs_node_put(old_node); |
1315 | vfs_node_put(old_node); |
| 1110 | if (new_node) |
1316 | if (new_node) |
| 1111 | vfs_node_put(new_node); |
1317 | vfs_node_put(new_node); |
| 1112 | free(old); |
1318 | free(old); |
| 1113 | free(new); |
1319 | free(new); |
| 1114 | ipc_answer_0(rid, EOK); |
1320 | ipc_answer_0(rid, EOK); |
| 1115 | } |
1321 | } |
| 1116 | 1322 | ||
| 1117 | /** |
1323 | /** |
| 1118 | * @} |
1324 | * @} |
| 1119 | */ |
1325 | */ |