-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrap_lkl.c
523 lines (417 loc) · 11.4 KB
/
wrap_lkl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* Copyright 2010 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Lucian Adrian Grijincu, lucian.grijincu@gmail.com
*/
#include <asm/lkl.h>
#include <asm/disk.h>
#include <asm/env.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include "util.h"
#define BRIDGE_LKL
#include "lkl-haiku-bridge.h"
#include "lh-fcntl.h"
#include "lh-error.h"
// we can't include Haiku's headers here because they define types
// already defined in lkl's headers. Because of this we have to
// manually declare functions defined by Haiku that we use here.
extern void* memcpy(void* dst, const void* src, int size);
extern int dprintf(const char* fmt, ...);
extern char* strdup(const char* str);
extern char* strncat(char*dest, const char*src, lh_size_t n);
extern void* malloc(int len);
extern void free(void* p);
#define LKL_MOUNT_PATH_TEMPLATE "/mnt/xxxxxxxxxxxxxxxx"
#define LKL_MOUNT_PATH_TEMPLATE_LEN sizeof(LKL_MOUNT_PATH_TEMPLATE)
typedef struct lklfs_fs_volume {
int fd;
int readonly;
__kernel_dev_t dev;
char mnt_path[LKL_MOUNT_PATH_TEMPLATE_LEN];
} lklfs_fs_volume;
static char*
vnode_to_abs_path(struct lklfs_fs_volume * vol, lklfs_vnode* vnode)
{
char* rel_path;
char* abs_path;
rel_path = vnode_full_path(vnode);
if (rel_path == NULL)
return NULL;
abs_path = pathJoin(vol->mnt_path, rel_path);
free(rel_path);
return abs_path;
}
/*! Create a Linux device and mount it
Returns the Linux path to the mounted device in @mntpath.
return 0 - in case of error
return a non-zero dev_t on success
*/
static __kernel_dev_t
mount_disk(int fd, __u64 size, unsigned long flags,
char * mntpath, unsigned long mntpath_size)
{
int rc;
__kernel_dev_t dev;
dev = lkl_disk_add_disk((void*) fd, size / 512);
dprintf("mount_disk> dev=%d\n", (int) dev);
if (dev == 0)
return 0;
rc = lkl_mount_dev(dev, NULL, flags, NULL, mntpath, mntpath_size);
if (rc != 0) {
dprintf("mount_disk> lkl_mount_dev err rc=%d\n", rc);
lkl_disk_del_disk(dev);
return 0;
}
dprintf("mount_disk> success\n");
return dev;
}
static int
umount_disk(__kernel_dev_t dev)
{
int rc;
rc = lkl_umount_dev(dev, 0);
if (rc != 0) {
lkl_disk_del_disk(dev);
return rc;
}
return lkl_disk_del_disk(dev);
}
static void
list_files(const char * path)
{
int fd;
dprintf("[list_files] -------- printing contents of [%s]\n", path);
fd = lkl_sys_open(path, O_RDONLY | O_LARGEFILE | O_DIRECTORY, 0);
if (fd >= 0) {
char x[4096];
int count, reclen;
struct __kernel_dirent * de;
count = lkl_sys_getdents(fd, (struct __kernel_dirent*) x, sizeof(x));
de = (struct __kernel_dirent *) x;
while (count > 0) {
reclen = de->d_reclen;
dprintf("[list_files] %s %ld\n", de->d_name, de->d_ino);
de = (struct __kernel_dirent *) ((char *) de + reclen);
count -= reclen;
}
lkl_sys_close(fd);
}
dprintf("[list_files] ++++++++ done printing contents of [%s]\n", path);
}
static int
fill_partition_id(const char * mnt_path, lklfs_partition_id * part)
{
struct __kernel_statfs stat;
int rc;
rc = lkl_sys_statfs(mnt_path, &stat);
if (rc < 0)
return rc;
part->content_size = stat.f_blocks * stat.f_bsize;
part->block_size = stat.f_bsize;
part->content_name = strdup("FIXME: LKL: label retrieval not implemented ");
if (part->content_name == NULL)
return -ENOMEM;
return 0;
}
int
lklfs_identify_partition_impl(int fd, lh_off_t size, void** _cookie)
{
int rc = 0;
__kernel_dev_t dev;
char mnt_str[] = { LKL_MOUNT_PATH_TEMPLATE };
struct lklfs_partition_id part;
// we only want some info about the partition, mount it read-only.
dev = mount_disk(fd, size, MS_RDONLY, mnt_str, sizeof(mnt_str));
if (dev == 0)
return -1;
// sone debug routines, just checking if stuff works correctly.
dprintf("wrap_lkl_identify_partition:: mnt_str=%s\n", mnt_str);
list_files(mnt_str);
list_files("."); // should be equal to '/'
// save some information for a later call for lklfs_scan_partition
rc = fill_partition_id(mnt_str, &part);
if (rc != 0) {
umount_disk(dev);
return rc;
}
// umount the partition as we've gathered all needed info and this.
// This fd won't be valid in other fs calls (e.g. scan_partition) anyway.
rc = umount_disk(dev);
if (rc != 0)
return rc;
*_cookie = malloc(sizeof(struct lklfs_partition_id));
if (*_cookie == NULL)
return -ENOMEM;
memcpy(*_cookie, &part, sizeof(part));
return 0;
}
void *
lklfs_mount_impl(int fd, lh_off_t size, int readonly)
{
lklfs_fs_volume * vol = (lklfs_fs_volume *) malloc(sizeof(lklfs_fs_volume));
if (vol == NULL)
return NULL;
int flags = (readonly ? MS_RDONLY : 0);
vol->fd = fd;
vol->readonly = readonly;
snprintf(vol->mnt_path, sizeof(vol->mnt_path), LKL_MOUNT_PATH_TEMPLATE);
vol->dev = mount_disk(fd, size, flags, vol->mnt_path, sizeof(vol->mnt_path));
if (vol->dev == 0) {
free(vol);
return NULL;
}
dprintf("wrap_lkl_mount:: success\n");
return vol;
}
int
lklfs_umount_impl(void * vol_)
{
lklfs_fs_volume * vol = (lklfs_fs_volume *) vol_;
__kernel_dev_t dev = vol->dev;
free(vol);
return umount_disk(dev);
}
// Returns the inode corresponding to @path, relative to @vol_'s mount point.
// Returns -1 on error.
lh_ino_t
lklfs_get_ino(void * vol_, lklfs_vnode* vnode)
{
int rc;
struct __kernel_stat64 stat;
char * abs_path = vnode_to_abs_path(vol_, vnode);
if (abs_path == NULL)
return -ENOMEM;
rc = lkl_sys_stat64(abs_path, &stat);
free(abs_path);
if (rc < 0) {
dprintf("lklfs_get_ino:: cannot stat [%s], rc=%d\n", abs_path, rc);
return rc;
}
return stat.st_ino;
}
int
lklfs_read_fs_info_impl(void * vol_, struct lh_fs_info * fi)
{
lklfs_fs_volume * vol = (lklfs_fs_volume *) vol_;
struct __kernel_statfs stat;
int rc;
rc = lkl_sys_statfs(vol->mnt_path, &stat);
if (rc < 0)
return rc;
fi->flags = vol->readonly ? LH_FS_READONLY : 0;
fi->block_size = stat.f_bsize;
fi->io_size = stat.f_bsize;
// TODO: is this the optimal I/O size?
fi->total_blocks = stat.f_blocks;
fi->free_blocks = stat.f_bfree;
fi->total_nodes = stat.f_files;
fi->free_nodes = stat.f_ffree;
snprintf(fi->volume_name, sizeof(fi->volume_name),
"FIXME: lklfs_read_fs_info_impl fi->volume_name");
return 0;
}
int
lklfs_read_stat_impl(void * vol_, lklfs_vnode* vnode, struct lh_stat * ls)
{
int rc;
struct __kernel_stat64 stat;
char * abs_path = vnode_to_abs_path(vol_, vnode);
if (abs_path == NULL)
return -ENOMEM;
rc = lkl_sys_stat64(abs_path, &stat);
free(abs_path);
if (rc != 0) {
dprintf("lkl_sys_stat64:: rc=%d\n");
return rc;
}
ls->st_mode = stat.st_mode;
ls->st_nlink = stat.st_nlink;
ls->st_uid = stat.st_uid;
ls->st_gid = stat.st_gid;
ls->st_size = stat.st_size;
ls->st_blksize = stat.st_blksize;
ls->st_blocks = stat.st_blocks;
ls->st_atim = stat.st_atime;
ls->st_atim_nsec = stat.st_atime_nsec;
ls->st_mtim = stat.st_mtime;
ls->st_mtim_nsec = stat.st_mtime_nsec;
ls->st_ctim = stat.st_ctime;
ls->st_ctim_nsec = stat.st_ctime_nsec;
// FIXME: POSIX stat does not have creation time
ls->st_crtim = stat.st_ctime;
ls->st_crtim_nsec = stat.st_ctime_nsec;
return 0;
}
int
lklfs_open_dir_impl(void * vol_, lklfs_vnode* vnode, void ** _cookie)
{
char * abs_path = vnode_to_abs_path(vol_, vnode);
if (abs_path == NULL)
return -1;
int fd = lkl_sys_open(abs_path, O_RDONLY | O_LARGEFILE | O_DIRECTORY, 0);
free(abs_path);
*_cookie = (void *) fd;
if (fd < 0)
return fd;
return 0;
}
int
lklfs_close_dir_impl(void * _cookie)
{
int fd = (int) _cookie;
return lkl_sys_close(fd);
}
// Returns the number of entries successfully read or -1 on error.
int
lklfs_read_dir_impl(void * _cookie, struct lh_dirent * ld, int bufferSize)
{
int bytesRead;
int fd = (int) _cookie;
int i = 0;
int total_reclen = 0;
struct __kernel_dirent * de, * de_;
// save a backup copy of the pointer for free()
de_ = de = (struct __kernel_dirent *) malloc(bufferSize);
if (de == NULL)
return -ENOMEM;
bytesRead = lkl_sys_getdents(fd, de, bufferSize);
if (bytesRead < 0) {
free(de);
return bytesRead;
}
total_reclen = 0;
while (bytesRead > 0 && total_reclen < bufferSize) {
unsigned short lh_reclen = sizeof(struct lh_dirent) + strlen(de->d_name) + 1;
total_reclen += lh_reclen;
bytesRead -= de->d_reclen;
ld->d_ino = de->d_ino;
ld->d_reclen = lh_reclen;
memcpy(ld->d_name, de->d_name, strlen(de->d_name) + 1);
de = (struct __kernel_dirent *) ((char *) de + de->d_reclen);
ld = (struct lh_dirent*) ((char*)ld + ld->d_reclen);
i++;
}
// always remember to delete the pointer that was received from malloc()
free(de_);
return i;
}
int
lklfs_rewind_dir_impl(void * cookie)
{
int fd = (int) cookie;
return lkl_sys_lseek(fd, 0, SEEK_SET);
}
int
lklfs_open_impl(void * vol_, lklfs_vnode* vnode, int lhOpenMode, int mode, void ** cookie_)
{
int fd;
int lklOpenMode = lh_to_lkl_openMode(lhOpenMode);
char * abs_path = vnode_to_abs_path(vol_, vnode);
if (abs_path == NULL)
return -ENOMEM;
fd = lkl_sys_open(abs_path, lklOpenMode, mode);
// don't need to worry about the permission bits as this
// will never create new files.
free(abs_path); // TODO: FIXME
if (fd < 0) {
//dprintf("lklfs_open_impl(%s):: fd=%d\n", abs_path, fd);
return fd;
}
*cookie_ = (void*) fd;
return 0;
}
int
lklfs_create_impl(void * vol_, lklfs_vnode* dir, const char* name,
int lhOpenMode, int perms, void ** cookie_)
{
int fd;
int lklOpenMode = lh_to_lkl_openMode(lhOpenMode) | O_CREAT;
char* dir_path = vnode_to_abs_path(vol_, dir);
if (dir_path == NULL)
return -ENOMEM;
lh_size_t len = strlen(dir_path) + strlen(name) + 2;
char* file_path = malloc(len);
if (file_path == NULL) {
free(dir_path);
return -ENOMEM;
}
snprintf(file_path, len, "%s/%s", dir_path, name);
free(dir_path);
//dprintf("lklfs_create_impl(%s) lklOpenMode=%d perms=%d\n", file_path, lklOpenMode, perms);
fd = lkl_sys_open(file_path, lklOpenMode, perms);
// don't need to worry about the permission bits as this
// will never create new files.
free(file_path); // TODO: FIXME
if (fd < 0) {
//dprintf("lklfs_create_impl(%s):: fd=%d\n", file_path, fd);
return fd;
}
*cookie_ = (void*) fd;
//dprintf("lklfs_create_impl(%s) fd=%d\n", file_path, fd);
return 0;
}
int
lklfs_access_impl(void * vol_, lklfs_vnode* vnode, int accessMode)
{
int fd;
char * abs_path = vnode_to_abs_path(vol_, vnode);
if (abs_path == NULL)
return -ENOMEM;
fd = lkl_sys_access(abs_path, accessMode);
free(abs_path);
if (fd < 0)
return fd;
return 0;
}
int
lklfs_close_impl(void * cookie)
{
// the cookie's value is the Linux file descriptor corresponding
// to this opened file. It's not treated as a pointer.
return lkl_sys_close((int) cookie);
}
int
lklfs_read_impl(void * cookie, lh_off_t pos, void * buffer, lh_size_t * length)
{
int rc;
int fd = (int) cookie;
rc = lkl_sys_pread64(fd, buffer, *length, pos);
if (rc < 0)
return rc;
*length = rc;
return 0;
}
int
lklfs_write_impl(void * cookie, lh_off_t pos, const void * buffer, lh_size_t * length)
{
int rc;
int fd = (int) cookie;
rc = lkl_sys_pwrite64(fd, buffer, *length, pos);
if (rc < 0)
return rc;
*length = rc;
return 0;
}
int
lklfs_sync_impl(void)
{
return lkl_sys_sync();
}
int
lklfs_get_mode(void* vol_, lklfs_vnode* vnode, int* _type)
{
int rc;
struct __kernel_stat64 stat;
char* abs_path = vnode_to_abs_path((struct lklfs_fs_volume*) vol_, vnode);
if (abs_path == NULL)
return -ENOMEM;
rc = lkl_sys_stat64(abs_path, &stat);
free(abs_path);
if (rc == 0)
*_type = stat.st_mode;
return 0;
}