Subversion Repositories HelenOS

Rev

Rev 4377 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4377 Rev 4692
Line 36... Line 36...
36
#include <vfs/canonify.h>
36
#include <vfs/canonify.h>
37
#include <stdlib.h>
37
#include <stdlib.h>
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <dirent.h>
39
#include <dirent.h>
40
#include <fcntl.h>
40
#include <fcntl.h>
41
#include <sys/stat.h>
-
 
42
#include <stdio.h>
41
#include <stdio.h>
-
 
42
#include <sys/stat.h>
43
#include <sys/types.h>
43
#include <sys/types.h>
44
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
45
#include <ipc/services.h>
46
#include <async.h>
46
#include <async.h>
47
#include <atomic.h>
47
#include <atomic.h>
48
#include <futex.h>
48
#include <futex.h>
49
#include <errno.h>
49
#include <errno.h>
50
#include <string.h>
50
#include <string.h>
-
 
51
#include <devmap.h>
-
 
52
#include <ipc/vfs.h>
51
#include <ipc/devmap.h>
53
#include <ipc/devmap.h>
52
#include "../../../srv/vfs/vfs.h"
-
 
53
 
54
 
54
int vfs_phone = -1;
55
static int vfs_phone = -1;
55
futex_t vfs_phone_futex = FUTEX_INITIALIZER;
56
static futex_t vfs_phone_futex = FUTEX_INITIALIZER;
-
 
57
static futex_t cwd_futex = FUTEX_INITIALIZER;
56
 
58
 
57
futex_t cwd_futex = FUTEX_INITIALIZER;
-
 
58
DIR *cwd_dir = NULL;
59
DIR *cwd_dir = NULL;
59
char *cwd_path = NULL;
60
char *cwd_path = NULL;
60
size_t cwd_size = 0;
61
size_t cwd_size = 0;
61
 
62
 
62
char *absolutize(const char *path, size_t *retlen)
63
char *absolutize(const char *path, size_t *retlen)
Line 113... Line 114...
113
{
114
{
114
    while (vfs_phone < 0)
115
    while (vfs_phone < 0)
115
        vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0);
116
        vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0);
116
}
117
}
117
 
118
 
118
static int device_get_handle(const char *name, dev_handle_t *handle,
-
 
119
    const unsigned int flags)
-
 
120
{
-
 
121
    int phone;
-
 
122
   
-
 
123
    if (flags & IPC_FLAG_BLOCKING)
-
 
124
        phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
-
 
125
    else
-
 
126
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
-
 
127
   
-
 
128
    if (phone < 0)
-
 
129
        return phone;
-
 
130
   
-
 
131
    ipc_call_t answer;
-
 
132
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0,
-
 
133
        &answer);
-
 
134
   
-
 
135
    ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1);
-
 
136
   
-
 
137
    if (retval != EOK) {
-
 
138
        async_wait_for(req, NULL);
-
 
139
        ipc_hangup(phone);
-
 
140
        return retval;
-
 
141
    }
-
 
142
   
-
 
143
    async_wait_for(req, &retval);
-
 
144
   
-
 
145
    if (handle != NULL)
-
 
146
        *handle = -1;
-
 
147
   
-
 
148
    if (retval == EOK) {
-
 
149
        if (handle != NULL)
-
 
150
            *handle = (dev_handle_t) IPC_GET_ARG1(answer);
-
 
151
    }
-
 
152
   
-
 
153
    ipc_hangup(phone);
-
 
154
    return retval;
-
 
155
}
-
 
156
 
-
 
157
int mount(const char *fs_name, const char *mp, const char *dev,
119
int mount(const char *fs_name, const char *mp, const char *dev,
158
    const char *opts, const unsigned int flags)
120
    const char *opts, unsigned int flags)
159
{
121
{
160
    int res;
122
    int res;
161
    ipcarg_t rc;
123
    ipcarg_t rc;
162
    aid_t req;
124
    aid_t req;
163
    dev_handle_t dev_handle;
125
    dev_handle_t dev_handle;
164
   
126
   
165
    res = device_get_handle(dev, &dev_handle, flags);
127
    res = devmap_device_get_handle(dev, &dev_handle, flags);
166
    if (res != EOK)
128
    if (res != EOK)
167
        return res;
129
        return res;
168
   
130
   
169
    size_t mpa_size;
131
    size_t mpa_size;
170
    char *mpa = absolutize(mp, &mpa_size);
132
    char *mpa = absolutize(mp, &mpa_size);
Line 173... Line 135...
173
   
135
   
174
    futex_down(&vfs_phone_futex);
136
    futex_down(&vfs_phone_futex);
175
    async_serialize_start();
137
    async_serialize_start();
176
    vfs_connect();
138
    vfs_connect();
177
   
139
   
178
    req = async_send_2(vfs_phone, VFS_MOUNT, dev_handle, flags, NULL);
140
    req = async_send_2(vfs_phone, VFS_IN_MOUNT, dev_handle, flags, NULL);
179
    rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_size);
141
    rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_size);
180
    if (rc != EOK) {
142
    if (rc != EOK) {
181
        async_wait_for(req, NULL);
143
        async_wait_for(req, NULL);
182
        async_serialize_end();
144
        async_serialize_end();
183
        futex_up(&vfs_phone_futex);
145
        futex_up(&vfs_phone_futex);
Line 234... Line 196...
234
   
196
   
235
    futex_down(&vfs_phone_futex);
197
    futex_down(&vfs_phone_futex);
236
    async_serialize_start();
198
    async_serialize_start();
237
    vfs_connect();
199
    vfs_connect();
238
   
200
   
239
    req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
201
    req = async_send_3(vfs_phone, VFS_IN_OPEN, lflag, oflag, 0, &answer);
240
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
202
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
241
    if (rc != EOK) {
203
    if (rc != EOK) {
242
        async_wait_for(req, NULL);
204
        async_wait_for(req, NULL);
243
        async_serialize_end();
205
        async_serialize_end();
244
        futex_up(&vfs_phone_futex);
206
        futex_up(&vfs_phone_futex);
Line 247... Line 209...
247
    }
209
    }
248
    async_wait_for(req, &rc);
210
    async_wait_for(req, &rc);
249
    async_serialize_end();
211
    async_serialize_end();
250
    futex_up(&vfs_phone_futex);
212
    futex_up(&vfs_phone_futex);
251
    free(pa);
213
    free(pa);
252
 
214
   
253
    if (rc != EOK)
215
    if (rc != EOK)
254
        return (int) rc;
216
        return (int) rc;
-
 
217
   
255
    return (int) IPC_GET_ARG1(answer);
218
    return (int) IPC_GET_ARG1(answer);
256
}
219
}
257
 
220
 
258
int open(const char *path, int oflag, ...)
221
int open(const char *path, int oflag, ...)
259
{
222
{
260
    return _open(path, L_FILE, oflag);
223
    return _open(path, L_FILE, oflag);
261
}
224
}
262
 
225
 
-
 
226
int open_node(fdi_node_t *node, int oflag)
-
 
227
{
-
 
228
    futex_down(&vfs_phone_futex);
-
 
229
    async_serialize_start();
-
 
230
    vfs_connect();
-
 
231
   
-
 
232
    ipc_call_t answer;
-
 
233
    aid_t req = async_send_4(vfs_phone, VFS_IN_OPEN_NODE, node->fs_handle,
-
 
234
        node->dev_handle, node->index, oflag, &answer);
-
 
235
   
-
 
236
    ipcarg_t rc;
-
 
237
    async_wait_for(req, &rc);
-
 
238
    async_serialize_end();
-
 
239
    futex_up(&vfs_phone_futex);
-
 
240
   
-
 
241
    if (rc != EOK)
-
 
242
        return (int) rc;
-
 
243
   
-
 
244
    return (int) IPC_GET_ARG1(answer);
-
 
245
}
-
 
246
 
263
int close(int fildes)
247
int close(int fildes)
264
{
248
{
265
    ipcarg_t rc;
249
    ipcarg_t rc;
266
   
250
   
267
    futex_down(&vfs_phone_futex);
251
    futex_down(&vfs_phone_futex);
268
    async_serialize_start();
252
    async_serialize_start();
269
    vfs_connect();
253
    vfs_connect();
270
   
254
   
271
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
255
    rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes);
272
   
256
   
273
    async_serialize_end();
257
    async_serialize_end();
274
    futex_up(&vfs_phone_futex);
258
    futex_up(&vfs_phone_futex);
275
   
259
   
276
    return (int)rc;
260
    return (int)rc;
Line 284... Line 268...
284
 
268
 
285
    futex_down(&vfs_phone_futex);
269
    futex_down(&vfs_phone_futex);
286
    async_serialize_start();
270
    async_serialize_start();
287
    vfs_connect();
271
    vfs_connect();
288
   
272
   
289
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
273
    req = async_send_1(vfs_phone, VFS_IN_READ, fildes, &answer);
290
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
274
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
291
    if (rc != EOK) {
275
    if (rc != EOK) {
292
        async_wait_for(req, NULL);
276
        async_wait_for(req, NULL);
293
        async_serialize_end();
277
        async_serialize_end();
294
        futex_up(&vfs_phone_futex);
278
        futex_up(&vfs_phone_futex);
Line 311... Line 295...
311
 
295
 
312
    futex_down(&vfs_phone_futex);
296
    futex_down(&vfs_phone_futex);
313
    async_serialize_start();
297
    async_serialize_start();
314
    vfs_connect();
298
    vfs_connect();
315
   
299
   
316
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
300
    req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer);
317
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
301
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
318
    if (rc != EOK) {
302
    if (rc != EOK) {
319
        async_wait_for(req, NULL);
303
        async_wait_for(req, NULL);
320
        async_serialize_end();
304
        async_serialize_end();
321
        futex_up(&vfs_phone_futex);
305
        futex_up(&vfs_phone_futex);
Line 328... Line 312...
328
        return (ssize_t) IPC_GET_ARG1(answer);
312
        return (ssize_t) IPC_GET_ARG1(answer);
329
    else
313
    else
330
        return -1;
314
        return -1;
331
}
315
}
332
 
316
 
-
 
317
int fsync(int fildes)
-
 
318
{
-
 
319
    futex_down(&vfs_phone_futex);
-
 
320
    async_serialize_start();
-
 
321
    vfs_connect();
-
 
322
   
-
 
323
    ipcarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes);
-
 
324
   
-
 
325
    async_serialize_end();
-
 
326
    futex_up(&vfs_phone_futex);
-
 
327
   
-
 
328
    return (int) rc;
-
 
329
}
-
 
330
 
333
off_t lseek(int fildes, off_t offset, int whence)
331
off_t lseek(int fildes, off_t offset, int whence)
334
{
332
{
335
    ipcarg_t rc;
333
    ipcarg_t rc;
336
 
334
 
337
    futex_down(&vfs_phone_futex);
335
    futex_down(&vfs_phone_futex);
338
    async_serialize_start();
336
    async_serialize_start();
339
    vfs_connect();
337
    vfs_connect();
340
   
338
   
341
    ipcarg_t newoffs;
339
    ipcarg_t newoffs;
342
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
340
    rc = async_req_3_1(vfs_phone, VFS_IN_SEEK, fildes, offset, whence,
343
        &newoffs);
341
        &newoffs);
344
 
342
 
345
    async_serialize_end();
343
    async_serialize_end();
346
    futex_up(&vfs_phone_futex);
344
    futex_up(&vfs_phone_futex);
347
 
345
 
Line 357... Line 355...
357
   
355
   
358
    futex_down(&vfs_phone_futex);
356
    futex_down(&vfs_phone_futex);
359
    async_serialize_start();
357
    async_serialize_start();
360
    vfs_connect();
358
    vfs_connect();
361
   
359
   
362
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
360
    rc = async_req_2_0(vfs_phone, VFS_IN_TRUNCATE, fildes, length);
363
    async_serialize_end();
361
    async_serialize_end();
364
    futex_up(&vfs_phone_futex);
362
    futex_up(&vfs_phone_futex);
365
    return (int) rc;
363
    return (int) rc;
366
}
364
}
367
 
365
 
-
 
366
int fstat(int fildes, struct stat *stat)
-
 
367
{
-
 
368
    ipcarg_t rc;
-
 
369
    aid_t req;
-
 
370
 
-
 
371
    futex_down(&vfs_phone_futex);
-
 
372
    async_serialize_start();
-
 
373
    vfs_connect();
-
 
374
   
-
 
375
    req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL);
-
 
376
    rc = ipc_data_read_start(vfs_phone, (void *)stat, sizeof(struct stat));
-
 
377
    if (rc != EOK) {
-
 
378
        async_wait_for(req, NULL);
-
 
379
        async_serialize_end();
-
 
380
        futex_up(&vfs_phone_futex);
-
 
381
        return (ssize_t) rc;
-
 
382
    }
-
 
383
    async_wait_for(req, &rc);
-
 
384
    async_serialize_end();
-
 
385
    futex_up(&vfs_phone_futex);
-
 
386
 
-
 
387
    return rc;
-
 
388
}
-
 
389
 
-
 
390
int stat(const char *path, struct stat *stat)
-
 
391
{
-
 
392
    ipcarg_t rc;
-
 
393
    aid_t req;
-
 
394
   
-
 
395
    size_t pa_size;
-
 
396
    char *pa = absolutize(path, &pa_size);
-
 
397
    if (!pa)
-
 
398
        return ENOMEM;
-
 
399
   
-
 
400
    futex_down(&vfs_phone_futex);
-
 
401
    async_serialize_start();
-
 
402
    vfs_connect();
-
 
403
   
-
 
404
    req = async_send_0(vfs_phone, VFS_IN_STAT, NULL);
-
 
405
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
-
 
406
    if (rc != EOK) {
-
 
407
        async_wait_for(req, NULL);
-
 
408
        async_serialize_end();
-
 
409
        futex_up(&vfs_phone_futex);
-
 
410
        free(pa);
-
 
411
        return (int) rc;
-
 
412
    }
-
 
413
    rc = ipc_data_read_start(vfs_phone, stat, sizeof(struct stat));
-
 
414
    if (rc != EOK) {
-
 
415
        async_wait_for(req, NULL);
-
 
416
        async_serialize_end();
-
 
417
        futex_up(&vfs_phone_futex);
-
 
418
        free(pa);
-
 
419
        return (int) rc;
-
 
420
    }
-
 
421
    async_wait_for(req, &rc);
-
 
422
    async_serialize_end();
-
 
423
    futex_up(&vfs_phone_futex);
-
 
424
    free(pa);
-
 
425
    return rc;
-
 
426
}
-
 
427
 
368
DIR *opendir(const char *dirname)
428
DIR *opendir(const char *dirname)
369
{
429
{
370
    DIR *dirp = malloc(sizeof(DIR));
430
    DIR *dirp = malloc(sizeof(DIR));
371
    if (!dirp)
431
    if (!dirp)
372
        return NULL;
432
        return NULL;
Line 410... Line 470...
410
   
470
   
411
    futex_down(&vfs_phone_futex);
471
    futex_down(&vfs_phone_futex);
412
    async_serialize_start();
472
    async_serialize_start();
413
    vfs_connect();
473
    vfs_connect();
414
   
474
   
415
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
475
    req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL);
416
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
476
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
417
    if (rc != EOK) {
477
    if (rc != EOK) {
418
        async_wait_for(req, NULL);
478
        async_wait_for(req, NULL);
419
        async_serialize_end();
479
        async_serialize_end();
420
        futex_up(&vfs_phone_futex);
480
        futex_up(&vfs_phone_futex);
Line 423... Line 483...
423
    }
483
    }
424
    async_wait_for(req, &rc);
484
    async_wait_for(req, &rc);
425
    async_serialize_end();
485
    async_serialize_end();
426
    futex_up(&vfs_phone_futex);
486
    futex_up(&vfs_phone_futex);
427
    free(pa);
487
    free(pa);
428
    return rc;
488
    return rc;
429
}
489
}
430
 
490
 
431
static int _unlink(const char *path, int lflag)
491
static int _unlink(const char *path, int lflag)
432
{
492
{
433
    ipcarg_t rc;
493
    ipcarg_t rc;
Line 440... Line 500...
440
 
500
 
441
    futex_down(&vfs_phone_futex);
501
    futex_down(&vfs_phone_futex);
442
    async_serialize_start();
502
    async_serialize_start();
443
    vfs_connect();
503
    vfs_connect();
444
   
504
   
445
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
505
    req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL);
446
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
506
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
447
    if (rc != EOK) {
507
    if (rc != EOK) {
448
        async_wait_for(req, NULL);
508
        async_wait_for(req, NULL);
449
        async_serialize_end();
509
        async_serialize_end();
450
        futex_up(&vfs_phone_futex);
510
        futex_up(&vfs_phone_futex);
Line 453... Line 513...
453
    }
513
    }
454
    async_wait_for(req, &rc);
514
    async_wait_for(req, &rc);
455
    async_serialize_end();
515
    async_serialize_end();
456
    futex_up(&vfs_phone_futex);
516
    futex_up(&vfs_phone_futex);
457
    free(pa);
517
    free(pa);
458
    return rc;
518
    return rc;
459
}
519
}
460
 
520
 
461
int unlink(const char *path)
521
int unlink(const char *path)
462
{
522
{
463
    return _unlink(path, L_NONE);
523
    return _unlink(path, L_NONE);
Line 487... Line 547...
487
 
547
 
488
    futex_down(&vfs_phone_futex);
548
    futex_down(&vfs_phone_futex);
489
    async_serialize_start();
549
    async_serialize_start();
490
    vfs_connect();
550
    vfs_connect();
491
   
551
   
492
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
552
    req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL);
493
    rc = ipc_data_write_start(vfs_phone, olda, olda_size);
553
    rc = ipc_data_write_start(vfs_phone, olda, olda_size);
494
    if (rc != EOK) {
554
    if (rc != EOK) {
495
        async_wait_for(req, NULL);
555
        async_wait_for(req, NULL);
496
        async_serialize_end();
556
        async_serialize_end();
497
        futex_up(&vfs_phone_futex);
557
        futex_up(&vfs_phone_futex);
Line 556... Line 616...
556
    str_cpy(buf, size, cwd_path);
616
    str_cpy(buf, size, cwd_path);
557
    futex_up(&cwd_futex);
617
    futex_up(&cwd_futex);
558
    return buf;
618
    return buf;
559
}
619
}
560
 
620
 
-
 
621
int fd_phone(int fildes)
-
 
622
{
-
 
623
    struct stat stat;
-
 
624
    int rc;
-
 
625
 
-
 
626
    rc = fstat(fildes, &stat);
-
 
627
 
-
 
628
    if (!stat.devfs_stat.device)
-
 
629
        return -1;
-
 
630
   
-
 
631
    return devmap_device_connect(stat.devfs_stat.device, 0);
-
 
632
}
-
 
633
 
-
 
634
int fd_node(int fildes, fdi_node_t *node)
-
 
635
{
-
 
636
    struct stat stat;
-
 
637
    int rc;
-
 
638
 
-
 
639
    rc = fstat(fildes, &stat);
-
 
640
   
-
 
641
    if (rc == EOK) {
-
 
642
        node->fs_handle = stat.fs_handle;
-
 
643
        node->dev_handle = stat.dev_handle;
-
 
644
        node->index = stat.index;
-
 
645
    }
-
 
646
   
-
 
647
    return rc;
-
 
648
}
-
 
649
 
561
/** @}
650
/** @}
562
 */
651
 */