forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.c
562 lines (457 loc) · 10.7 KB
/
fs.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* Copyright (c) 2018 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include <zephyr/types.h>
#include <errno.h>
#include <init.h>
#include <fs.h>
#define LOG_LEVEL CONFIG_FS_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(fs);
/* list of mounted file systems */
static sys_dlist_t fs_mnt_list;
/* lock to protect mount list operations */
static struct k_mutex mutex;
/* file system map table */
static struct fs_file_system_t *fs_map[FS_TYPE_END];
int fs_get_mnt_point(struct fs_mount_t **mnt_pntp,
const char *name, size_t *match_len)
{
struct fs_mount_t *mnt_p = NULL, *itr;
size_t longest_match = 0;
size_t len, name_len = strlen(name);
sys_dnode_t *node;
k_mutex_lock(&mutex, K_FOREVER);
SYS_DLIST_FOR_EACH_NODE(&fs_mnt_list, node) {
itr = CONTAINER_OF(node, struct fs_mount_t, node);
len = itr->mountp_len;
/*
* Move to next node if mount point length is
* shorter than longest_match match or if path
* name is shorter than the mount point name.
*/
if ((len < longest_match) || (len > name_len)) {
continue;
}
/*
* Move to next node if name does not have a directory
* separator where mount point name ends.
*/
if ((len > 1) && (name[len] != '/') && (name[len] != '\0')) {
continue;
}
/* Check for mount point match */
if (strncmp(name, itr->mnt_point, len) == 0) {
mnt_p = itr;
longest_match = len;
}
}
k_mutex_unlock(&mutex);
if (mnt_p == NULL) {
return -ENOENT;
}
*mnt_pntp = mnt_p;
if (match_len)
*match_len = mnt_p->mountp_len;
return 0;
}
/* File operations */
int fs_open(struct fs_file_t *zfp, const char *file_name)
{
struct fs_mount_t *mp;
int rc = -EINVAL;
if ((file_name == NULL) ||
(strlen(file_name) <= 1) || (file_name[0] != '/')) {
LOG_ERR("invalid file name!!");
return -EINVAL;
}
rc = fs_get_mnt_point(&mp, file_name, NULL);
if (rc < 0) {
LOG_ERR("%s:mount point not found!!", __func__);
return rc;
}
zfp->mp = mp;
if (zfp->mp->fs->open != NULL) {
rc = zfp->mp->fs->open(zfp, file_name);
if (rc < 0) {
LOG_ERR("file open error (%d)", rc);
return rc;
}
}
return rc;
}
int fs_close(struct fs_file_t *zfp)
{
int rc = -EINVAL;
if (zfp->mp->fs->close != NULL) {
rc = zfp->mp->fs->close(zfp);
if (rc < 0) {
LOG_ERR("file close error (%d)", rc);
return rc;
}
}
zfp->mp = NULL;
return rc;
}
ssize_t fs_read(struct fs_file_t *zfp, void *ptr, size_t size)
{
int rc = -EINVAL;
if (zfp->mp->fs->read != NULL) {
rc = zfp->mp->fs->read(zfp, ptr, size);
if (rc < 0) {
LOG_ERR("file read error (%d)", rc);
}
}
return rc;
}
ssize_t fs_write(struct fs_file_t *zfp, const void *ptr, size_t size)
{
int rc = -EINVAL;
if (zfp->mp->fs->write != NULL) {
rc = zfp->mp->fs->write(zfp, ptr, size);
if (rc < 0) {
LOG_ERR("file write error (%d)", rc);
}
}
return rc;
}
int fs_seek(struct fs_file_t *zfp, off_t offset, int whence)
{
int rc = -EINVAL;
if (zfp->mp->fs->lseek != NULL) {
rc = zfp->mp->fs->lseek(zfp, offset, whence);
if (rc < 0) {
LOG_ERR("file seek error (%d)", rc);
}
}
return rc;
}
off_t fs_tell(struct fs_file_t *zfp)
{
int rc = -EINVAL;
if (zfp->mp->fs->tell != NULL) {
rc = zfp->mp->fs->tell(zfp);
if (rc < 0) {
LOG_ERR("file tell error (%d)", rc);
}
}
return rc;
}
int fs_truncate(struct fs_file_t *zfp, off_t length)
{
int rc = -EINVAL;
if (zfp->mp->fs->truncate != NULL) {
rc = zfp->mp->fs->truncate(zfp, length);
if (rc < 0) {
LOG_ERR("file truncate error (%d)", rc);
}
}
return rc;
}
int fs_sync(struct fs_file_t *zfp)
{
int rc = -EINVAL;
if (zfp->mp->fs->sync != NULL) {
rc = zfp->mp->fs->sync(zfp);
if (rc < 0) {
LOG_ERR("file sync error (%d)", rc);
}
}
return rc;
}
/* Directory operations */
int fs_opendir(struct fs_dir_t *zdp, const char *abs_path)
{
struct fs_mount_t *mp;
int rc = -EINVAL;
if ((abs_path == NULL) ||
(strlen(abs_path) <= 1) || (abs_path[0] != '/')) {
LOG_ERR("invalid file name!!");
return -EINVAL;
}
rc = fs_get_mnt_point(&mp, abs_path, NULL);
if (rc < 0) {
LOG_ERR("%s:mount point not found!!", __func__);
return rc;
}
zdp->mp = mp;
if (zdp->mp->fs->opendir != NULL) {
rc = zdp->mp->fs->opendir(zdp, abs_path);
if (rc < 0) {
LOG_ERR("directory open error (%d)", rc);
}
}
return rc;
}
int fs_readdir(struct fs_dir_t *zdp, struct fs_dirent *entry)
{
int rc = -EINVAL;
if (zdp->mp->fs->readdir != NULL) {
rc = zdp->mp->fs->readdir(zdp, entry);
if (rc < 0) {
LOG_ERR("directory read error (%d)", rc);
}
}
return rc;
}
int fs_closedir(struct fs_dir_t *zdp)
{
int rc = -EINVAL;
if (zdp->mp->fs->closedir != NULL) {
rc = zdp->mp->fs->closedir(zdp);
if (rc < 0) {
LOG_ERR("directory close error (%d)", rc);
return rc;
}
}
zdp->mp = NULL;
return rc;
}
/* Filesystem operations */
int fs_mkdir(const char *abs_path)
{
struct fs_mount_t *mp;
int rc = -EINVAL;
if ((abs_path == NULL) ||
(strlen(abs_path) <= 1) || (abs_path[0] != '/')) {
LOG_ERR("invalid file name!!");
return -EINVAL;
}
rc = fs_get_mnt_point(&mp, abs_path, NULL);
if (rc < 0) {
LOG_ERR("%s:mount point not found!!", __func__);
return rc;
}
if (mp->fs->mkdir != NULL) {
rc = mp->fs->mkdir(mp, abs_path);
if (rc < 0) {
LOG_ERR("failed to create directory (%d)", rc);
}
}
return rc;
}
int fs_unlink(const char *abs_path)
{
struct fs_mount_t *mp;
int rc = -EINVAL;
if ((abs_path == NULL) ||
(strlen(abs_path) <= 1) || (abs_path[0] != '/')) {
LOG_ERR("invalid file name!!");
return -EINVAL;
}
rc = fs_get_mnt_point(&mp, abs_path, NULL);
if (rc < 0) {
LOG_ERR("%s:mount point not found!!", __func__);
return rc;
}
if (mp->fs->unlink != NULL) {
rc = mp->fs->unlink(mp, abs_path);
if (rc < 0) {
LOG_ERR("failed to unlink path (%d)", rc);
}
}
return rc;
}
int fs_rename(const char *from, const char *to)
{
struct fs_mount_t *mp;
size_t match_len;
int rc = -EINVAL;
if ((from == NULL) || (strlen(from) <= 1) || (from[0] != '/') ||
(to == NULL) || (strlen(to) <= 1) || (to[0] != '/')) {
LOG_ERR("invalid file name!!");
return -EINVAL;
}
rc = fs_get_mnt_point(&mp, from, &match_len);
if (rc < 0) {
LOG_ERR("%s:mount point not found!!", __func__);
return rc;
}
/* Make sure both files are mounted on the same path */
if (strncmp(from, to, match_len) != 0) {
LOG_ERR("mount point not same!!");
return -EINVAL;
}
if (mp->fs->rename != NULL) {
rc = mp->fs->rename(mp, from, to);
if (rc < 0) {
LOG_ERR("failed to rename file or dir (%d)", rc);
}
}
return rc;
}
int fs_stat(const char *abs_path, struct fs_dirent *entry)
{
struct fs_mount_t *mp;
int rc = -EINVAL;
if ((abs_path == NULL) ||
(strlen(abs_path) <= 1) || (abs_path[0] != '/')) {
LOG_ERR("invalid file name!!");
return -EINVAL;
}
rc = fs_get_mnt_point(&mp, abs_path, NULL);
if (rc < 0) {
LOG_ERR("%s:mount point not found!!", __func__);
return rc;
}
if (mp->fs->stat != NULL) {
rc = mp->fs->stat(mp, abs_path, entry);
if (rc < 0) {
LOG_ERR("failed get file or dir stat (%d)", rc);
}
}
return rc;
}
int fs_statvfs(const char *abs_path, struct fs_statvfs *stat)
{
struct fs_mount_t *mp;
int rc;
if ((abs_path == NULL) ||
(strlen(abs_path) <= 1) || (abs_path[0] != '/')) {
LOG_ERR("invalid file name!!");
return -EINVAL;
}
rc = fs_get_mnt_point(&mp, abs_path, NULL);
if (rc < 0) {
LOG_ERR("%s:mount point not found!!", __func__);
return rc;
}
if (mp->fs->statvfs != NULL) {
rc = mp->fs->statvfs(mp, abs_path, stat);
if (rc < 0) {
LOG_ERR("failed get file or dir stat (%d)", rc);
}
}
return rc;
}
int fs_mount(struct fs_mount_t *mp)
{
struct fs_mount_t *itr;
struct fs_file_system_t *fs;
sys_dnode_t *node;
int rc = -EINVAL;
if ((mp == NULL) || (mp->mnt_point == NULL)) {
LOG_ERR("mount point not initialized!!");
return -EINVAL;
}
k_mutex_lock(&mutex, K_FOREVER);
/* Check if requested file system is registered */
if (mp->type >= FS_TYPE_END || fs_map[mp->type] == NULL) {
LOG_ERR("requested file system not registered!!");
rc = -ENOENT;
goto mount_err;
}
/* Get fs interface from file system map */
fs = fs_map[mp->type];
mp->mountp_len = strlen(mp->mnt_point);
if ((mp->mnt_point[0] != '/') ||
(strlen(mp->mnt_point) <= 1)) {
LOG_ERR("invalid mount point!!");
rc = -EINVAL;
goto mount_err;
}
if (fs->mount == NULL) {
LOG_ERR("fs ops functions not set!!");
rc = -EINVAL;
goto mount_err;
}
/* Check if mount point already exists */
SYS_DLIST_FOR_EACH_NODE(&fs_mnt_list, node) {
itr = CONTAINER_OF(node, struct fs_mount_t, node);
/* continue if length does not match */
if (mp->mountp_len != itr->mountp_len) {
continue;
}
if (strncmp(mp->mnt_point, itr->mnt_point,
mp->mountp_len) == 0) {
LOG_ERR("mount Point already exists!!");
rc = -EBUSY;
goto mount_err;
}
}
rc = fs->mount(mp);
if (rc < 0) {
LOG_ERR("fs mount error (%d)", rc);
goto mount_err;
}
/* set mount point fs interface */
mp->fs = fs;
/* append to the mount list */
sys_dlist_append(&fs_mnt_list, &mp->node);
LOG_DBG("fs mouted, mount point:%s", mp->mnt_point);
mount_err:
k_mutex_unlock(&mutex);
return rc;
}
int fs_unmount(struct fs_mount_t *mp)
{
int rc = -EINVAL;
if ((mp == NULL) || (mp->mnt_point == NULL) ||
(strlen(mp->mnt_point) <= 1)) {
LOG_ERR("invalid mount point!!");
return -EINVAL;
}
k_mutex_lock(&mutex, K_FOREVER);
if ((mp->fs == NULL) || mp->fs->unmount == NULL) {
LOG_ERR("fs ops functions not set!!");
rc = -EINVAL;
goto unmount_err;
}
rc = mp->fs->unmount(mp);
if (rc < 0) {
LOG_ERR("fs unmount error (%d)", rc);
goto unmount_err;
}
/* clear file system interface */
mp->fs = NULL;
/* remove mount node from the list */
sys_dlist_remove(&mp->node);
LOG_DBG("fs unmouted, mount point:%s", mp->mnt_point);
unmount_err:
k_mutex_unlock(&mutex);
return rc;
}
/* Register File system */
int fs_register(enum fs_type type, struct fs_file_system_t *fs)
{
int rc = 0;
k_mutex_lock(&mutex, K_FOREVER);
if (type >= FS_TYPE_END) {
LOG_ERR("failed to register File system!!");
rc = -EINVAL;
goto reg_err;
}
fs_map[type] = fs;
LOG_DBG("fs registered of type(%u)", type);
reg_err:
k_mutex_unlock(&mutex);
return rc;
}
/* Unregister File system */
int fs_unregister(enum fs_type type, struct fs_file_system_t *fs)
{
int rc = 0;
k_mutex_lock(&mutex, K_FOREVER);
if ((type >= FS_TYPE_END) ||
(fs_map[type] != fs)) {
LOG_ERR("failed to unregister File system!!");
rc = -EINVAL;
goto unreg_err;
}
fs_map[type] = NULL;
LOG_DBG("fs unregistered of type(%u)", type);
unreg_err:
k_mutex_unlock(&mutex);
return rc;
}
static int fs_init(struct device *dev)
{
k_mutex_init(&mutex);
sys_dlist_init(&fs_mnt_list);
return 0;
}
SYS_INIT(fs_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);