dfs_tmpfs.c 15.4 KB
Newer Older
G
guo 已提交
1 2 3 4 5 6 7 8
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-10-24     flybreak     the first version
9
 * 2023-02-01     xqyjlj       fix cannot open the same file repeatedly in 'w' mode
G
guo 已提交
10 11 12 13 14 15 16 17
 */

#include <rthw.h>
#include <rtthread.h>
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>

18
#ifdef RT_USING_SMART
G
guo 已提交
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
#include <lwp.h>
#include <lwp_user_mm.h>
#endif

#include "dfs_tmpfs.h"

#define DBG_TAG              "tmpfs"
#define DBG_LVL              DBG_INFO
#include <rtdbg.h>

static int _path_separate(const char *path, char *parent_path, char *file_name)
{
    const char *path_p, *path_q;

    RT_ASSERT(path[0] == '/');

    file_name[0] = '\0';
    path_p = path_q = &path[1];
__next_dir:
    while (*path_q != '/' && *path_q != '\0')
    {
        path_q++;
    }
    if (path_q != path_p) /*sub dir*/
    {
        if (*path_q != '\0')
        {
            path_q++;
            path_p = path_q;
            goto __next_dir;
        }
        else /* Last level dir */
        {
            rt_memcpy(parent_path, path, path_p - path - 1);
            parent_path[path_p - path - 1] = '\0';
            rt_memcpy(file_name, path_p, path_q - path_p);
            file_name[path_q - path_p] = '\0';
        }
    }
    if (parent_path[0] == 0)
    {
        parent_path[0] = '/';
        parent_path[1] = '\0';
    }
    LOG_D("parent_path: %s", parent_path);
    LOG_D("file_name: %s", file_name);

    return 0;
}

static int _get_subdir(const char *path, char *name)
{
    const char *subpath = path;
    while (*subpath == '/' && *subpath)
        subpath ++;
    while (*subpath != '/' && *subpath)
    {
        *name = *subpath;
        name ++;
        subpath ++;
    }
    return 0;
}

static int _free_subdir(struct tmpfs_file *dfile)
{
    struct tmpfs_file *file;
    rt_list_t *list, *temp_list;
G
geniusgogo 已提交
87
    struct tmpfs_sb *superblock;
G
guo 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

    RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);

    rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
    {
        file = rt_list_entry(list, struct tmpfs_file, sibling);
        if (file->type == TMPFS_TYPE_DIR)
        {
            _free_subdir(file);
        }
        if (file->data != NULL)
        {
            /* TODO: fix for rt-smart */
            rt_free(file->data);
        }
G
geniusgogo 已提交
103 104 105 106 107

        superblock = file->sb;
        RT_ASSERT(superblock != NULL);

        rt_spin_lock(&superblock->lock);
G
guo 已提交
108
        rt_list_remove(&(file->sibling));
G
geniusgogo 已提交
109 110
        rt_spin_unlock(&superblock->lock);

G
guo 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        rt_free(file);
    }
    return 0;
}

int dfs_tmpfs_mount(struct dfs_filesystem *fs,
                    unsigned long          rwflag,
                    const void            *data)
{
    struct tmpfs_sb *superblock;

    superblock = rt_calloc(1, sizeof(struct tmpfs_sb));
    superblock->df_size = sizeof(struct tmpfs_sb);
    superblock->magic = TMPFS_MAGIC;
    rt_list_init(&superblock->sibling);

    superblock->root.name[0] = '/';
    superblock->root.sb = superblock;
    superblock->root.type = TMPFS_TYPE_DIR;
    rt_list_init(&superblock->root.sibling);
    rt_list_init(&superblock->root.subdirs);

G
geniusgogo 已提交
133 134
    rt_spin_lock_init(&superblock->lock);

G
guo 已提交
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
    fs->data = superblock;

    return RT_EOK;
}

int dfs_tmpfs_unmount(struct dfs_filesystem *fs)
{
    struct tmpfs_sb *superblock;

    superblock = (struct tmpfs_sb *)fs->data;
    RT_ASSERT(superblock != NULL);

    _free_subdir(&(superblock->root));
    rt_free(superblock);

    fs->data = NULL;

    return RT_EOK;
}

int dfs_tmpfs_statfs(struct dfs_filesystem *fs, struct statfs *buf)
{
    struct tmpfs_sb *superblock;

    superblock = (struct tmpfs_sb *)fs->data;
    RT_ASSERT(superblock != NULL);
    RT_ASSERT(buf != NULL);

    buf->f_bsize  = 512;
    buf->f_blocks = (superblock->df_size + 511) / 512;
    buf->f_bfree  = 1;
166
    buf->f_bavail = buf->f_bfree;
G
guo 已提交
167 168 169 170

    return RT_EOK;
}

171
int dfs_tmpfs_ioctl(struct dfs_file *file, int cmd, void *args)
G
guo 已提交
172 173 174 175 176 177 178 179 180 181 182 183
{
    struct tmpfs_file *d_file;
    struct tmpfs_sb *superblock;

    d_file = (struct tmpfs_file *)file->vnode->data;
    RT_ASSERT(d_file != NULL);

    superblock = d_file->sb;
    RT_ASSERT(superblock != NULL);

    switch (cmd)
    {
184 185
#ifdef RT_USING_SMART
    case RT_FIOMMAP2:
G
guo 已提交
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
    {
        struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)args;
        if (mmap2)
        {
            if (mmap2->length > file->vnode->size)
            {
                return -RT_ENOMEM;
            }

            LOG_D("tmpfile mmap ptr:%x , size:%d\n", d_file->data, mmap2->length);
            mmap2->ret = lwp_map_user_phy(lwp_self(), RT_NULL, d_file->data, mmap2->length, 0);
        }
        return RT_EOK;
        break;
    }
#endif
    default:
        break;
    }
    return -EIO;
}

struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb  *superblock,
                                      const char       *path,
                                      rt_size_t        *size)
{
    const char *subpath, *curpath, *filename = RT_NULL;
    char subdir_name[TMPFS_NAME_MAX];
    struct tmpfs_file *file, *curfile;
    rt_list_t *list;

    subpath = path;
    while (*subpath == '/' && *subpath)
        subpath ++;
    if (! *subpath) /* is root directory */
    {
        *size = 0;
        return &(superblock->root);
    }

    curpath = subpath;
    curfile = &superblock->root;

find_subpath:
    while (*subpath != '/' && *subpath)
        subpath ++;

    if (! *subpath) /* is last directory */
        filename = curpath;
    else
        subpath ++; /* skip '/' */

    memset(subdir_name, 0, TMPFS_NAME_MAX);
    _get_subdir(curpath, subdir_name);

G
geniusgogo 已提交
241
    rt_spin_lock(&superblock->lock);
G
guo 已提交
242 243 244 245 246 247 248 249 250 251

    rt_list_for_each(list, &curfile->subdirs)
    {
        file = rt_list_entry(list, struct tmpfs_file, sibling);
        if (filename) /* find file */
        {
            if (rt_strcmp(file->name, filename) == 0)
            {
                *size = file->size;

G
geniusgogo 已提交
252
                rt_spin_unlock(&superblock->lock);
G
guo 已提交
253 254 255 256 257 258 259 260
                return file;
            }
        }
        else if (rt_strcmp(file->name, subdir_name) == 0)
        {
            *size = file->size;
            curpath = subpath;
            curfile = file;
G
geniusgogo 已提交
261
            rt_spin_unlock(&superblock->lock);
G
guo 已提交
262 263 264
            goto find_subpath;
        }
    }
G
geniusgogo 已提交
265
    rt_spin_unlock(&superblock->lock);
G
guo 已提交
266 267 268 269
    /* not found */
    return NULL;
}

270
int dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count)
G
guo 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
{
    rt_size_t length;
    struct tmpfs_file *d_file;

    d_file = (struct tmpfs_file *)file->vnode->data;
    RT_ASSERT(d_file != NULL);

    if (count < file->vnode->size - file->pos)
        length = count;
    else
        length = file->vnode->size - file->pos;

    if (length > 0)
        memcpy(buf, &(d_file->data[file->pos]), length);

    /* update file current position */
    file->pos += length;

    return length;
}


293
int dfs_tmpfs_write(struct dfs_file *fd, const void *buf, size_t count)
G
guo 已提交
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
{
    struct tmpfs_file *d_file;
    struct tmpfs_sb *superblock;

    d_file = (struct tmpfs_file *)fd->vnode->data;
    RT_ASSERT(d_file != NULL);

    superblock = d_file->sb;
    RT_ASSERT(superblock != NULL);

    if (count + fd->pos > fd->vnode->size)
    {
        rt_uint8_t *ptr;
        ptr = rt_realloc(d_file->data, fd->pos + count);
        if (ptr == NULL)
        {
            rt_set_errno(-ENOMEM);
            return 0;
        }

        superblock->df_size += (fd->pos - d_file->size + count);
        /* update d_file and file size */
        d_file->data = ptr;
        d_file->size = fd->pos + count;
        fd->vnode->size = d_file->size;
        LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
    }

    if (count > 0)
        memcpy(d_file->data + fd->pos, buf, count);

    /* update file current position */
    fd->pos += count;

    return count;
}

331
int dfs_tmpfs_lseek(struct dfs_file *file, off_t offset)
G
guo 已提交
332 333 334 335 336 337 338 339 340 341 342
{
    if (offset <= (off_t)file->vnode->size)
    {
        file->pos = offset;

        return file->pos;
    }

    return -EIO;
}

343
int dfs_tmpfs_close(struct dfs_file *file)
G
guo 已提交
344 345 346 347 348 349 350 351 352 353 354 355
{
    RT_ASSERT(file->vnode->ref_count > 0);
    if (file->vnode->ref_count > 1)
    {
        return 0;
    }

    file->vnode->data = NULL;

    return RT_EOK;
}

356
int dfs_tmpfs_open(struct dfs_file *file)
G
guo 已提交
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
{
    rt_size_t size;
    struct tmpfs_sb *superblock;
    struct tmpfs_file *d_file, *p_file;
    struct dfs_filesystem *fs;
    char parent_path[DFS_PATH_MAX],file_name[TMPFS_NAME_MAX];

    RT_ASSERT(file->vnode->ref_count > 0);
    if (file->vnode->ref_count > 1)
    {
        if (file->vnode->type == FT_DIRECTORY
                && !(file->flags & O_DIRECTORY))
        {
            return -ENOENT;
        }
        file->pos = 0;
        return 0;
    }

    fs = file->vnode->fs;

    superblock = (struct tmpfs_sb *)fs->data;
    RT_ASSERT(superblock != NULL);

    /* find file */
    d_file = dfs_tmpfs_lookup(superblock, file->vnode->path, &size);
    if (d_file == NULL && !(file->flags & O_CREAT))
        return -ENOENT;

    /* Creates a new file. */
    if (file->flags & O_CREAT)
    {
389 390
        if (d_file == NULL)
        {
391 392 393 394 395 396 397 398 399 400 401 402
            /* find parent file */
            _path_separate(file->vnode->path, parent_path, file_name);
            if (file_name[0] == '\0') /* it's root dir */
                return -ENOENT;

            /* open parent directory */
            p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
            if (p_file == NULL)
                return -ENOENT;

            /* create a file entry */
            d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
403 404
            if (d_file == NULL)
            {
405 406 407
                return -ENOMEM;
            }
            superblock->df_size += sizeof(struct tmpfs_file);
G
guo 已提交
408

409
            strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
G
guo 已提交
410

411 412 413 414 415
            rt_list_init(&(d_file->subdirs));
            rt_list_init(&(d_file->sibling));
            d_file->data = NULL;
            d_file->size = 0;
            d_file->sb   = superblock;
416 417
            if (file->flags & O_DIRECTORY)
            {
418 419
                d_file->type = TMPFS_TYPE_DIR;
            }
420 421
            else
            {
422 423
                d_file->type = TMPFS_TYPE_FILE;
            }
G
geniusgogo 已提交
424
            rt_spin_lock(&superblock->lock);
425
            rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
G
geniusgogo 已提交
426
            rt_spin_unlock(&superblock->lock);
G
guo 已提交
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
        }
    }
    /* Creates a new file.
        * If the file is existing, it is truncated and overwritten.
        */
    if (file->flags & O_TRUNC)
    {
        d_file->size = 0;
        if (d_file->data != NULL)
        {
            /* ToDo: fix for rt-smart. */
            rt_free(d_file->data);
            d_file->data = NULL;
        }
    }

    /* fill file */
    if (d_file->type == TMPFS_TYPE_DIR)
    {
        if (file->flags & O_DIRECTORY)
            file->vnode->type = FT_DIRECTORY;
        else
            return -ENOMEM;
    }
    else
    {
        if (file->flags & O_DIRECTORY)
        {
            return -ENOMEM;
        }
        file->vnode->type = FT_DEVICE;
    }

    file->vnode->data = d_file;
    file->vnode->size = d_file->size;
    if (file->flags & O_APPEND)
    {
        file->pos = file->vnode->size;
    }
    else
    {
        file->pos = 0;
    }

    return 0;
}

int dfs_tmpfs_stat(struct dfs_filesystem *fs,
                   const char            *path,
                   struct stat           *st)
{
    rt_size_t size;
    struct tmpfs_file *d_file;
    struct tmpfs_sb *superblock;

    superblock = (struct tmpfs_sb *)fs->data;
    d_file = dfs_tmpfs_lookup(superblock, path, &size);

    if (d_file == NULL)
        return -ENOENT;

    st->st_dev = 0;
    st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
                  S_IWUSR | S_IWGRP | S_IWOTH;
    if (d_file->type == TMPFS_TYPE_DIR)
    {
        st->st_mode &= ~S_IFREG;
        st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
    }

    st->st_size = d_file->size;
    st->st_mtime = 0;

    return RT_EOK;
}

503
int dfs_tmpfs_getdents(struct dfs_file *file,
G
guo 已提交
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 563 564 565 566 567 568 569 570
                       struct dirent *dirp,
                       uint32_t    count)
{
    rt_size_t index, end;
    struct dirent *d;
    struct tmpfs_file *d_file, *n_file;
    rt_list_t *list;
    struct tmpfs_sb *superblock;

    d_file = (struct tmpfs_file *)file->vnode->data;

    superblock  = d_file->sb;
    RT_ASSERT(superblock != RT_NULL);

    /* make integer count */
    count = (count / sizeof(struct dirent));
    if (count == 0)
        return -EINVAL;

    end = file->pos + count;
    index = 0;
    count = 0;

    rt_list_for_each(list, &d_file->subdirs)
    {
        n_file = rt_list_entry(list, struct tmpfs_file, sibling);
        if (index >= (rt_size_t)file->pos)
        {
            d = dirp + count;
            if (d_file->type == TMPFS_TYPE_FILE)
            {
                d->d_type = DT_REG;
            }
            if (d_file->type == TMPFS_TYPE_DIR)
            {
                d->d_type = DT_DIR;
            }
            d->d_namlen = RT_NAME_MAX;
            d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
            rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);

            count += 1;
            file->pos += 1;
        }
        index += 1;
        if (index >= end)
        {
            break;
        }
    }

    return count * sizeof(struct dirent);
}

int dfs_tmpfs_unlink(struct dfs_filesystem *fs, const char *path)
{
    rt_size_t size;
    struct tmpfs_sb *superblock;
    struct tmpfs_file *d_file;

    superblock = (struct tmpfs_sb *)fs->data;
    RT_ASSERT(superblock != NULL);

    d_file = dfs_tmpfs_lookup(superblock, path, &size);
    if (d_file == NULL)
        return -ENOENT;

G
geniusgogo 已提交
571
    rt_spin_lock(&superblock->lock);
G
guo 已提交
572
    rt_list_remove(&(d_file->sibling));
G
geniusgogo 已提交
573
    rt_spin_unlock(&superblock->lock);
G
guo 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

    if (d_file->data != NULL)
        rt_free(d_file->data);
    rt_free(d_file);

    return RT_EOK;
}

int dfs_tmpfs_rename(struct dfs_filesystem *fs,
                     const char            *oldpath,
                     const char            *newpath)
{
    struct tmpfs_file *d_file, *p_file;
    struct tmpfs_sb *superblock;
    rt_size_t size;
    char parent_path[DFS_PATH_MAX],file_name[TMPFS_NAME_MAX];

    superblock = (struct tmpfs_sb *)fs->data;
    RT_ASSERT(superblock != NULL);

    d_file = dfs_tmpfs_lookup(superblock, newpath, &size);
    if (d_file != NULL)
        return -EEXIST;

    d_file = dfs_tmpfs_lookup(superblock, oldpath, &size);
    if (d_file == NULL)
        return -ENOENT;

    /* find parent file */
    _path_separate(newpath, parent_path, file_name);
    if (file_name[0] == '\0') /* it's root dir */
        return -ENOENT;
    /* open parent directory */
    p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
    RT_ASSERT(p_file != NULL);

G
geniusgogo 已提交
610
    rt_spin_lock(&superblock->lock);
G
guo 已提交
611
    rt_list_remove(&(d_file->sibling));
G
geniusgogo 已提交
612
    rt_spin_unlock(&superblock->lock);
G
guo 已提交
613 614 615

    strncpy(d_file->name, file_name, TMPFS_NAME_MAX);

G
geniusgogo 已提交
616
    rt_spin_lock(&superblock->lock);
G
guo 已提交
617
    rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
G
geniusgogo 已提交
618
    rt_spin_unlock(&superblock->lock);
G
guo 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

    return RT_EOK;
}

static const struct dfs_file_ops _tmp_fops =
{
    dfs_tmpfs_open,
    dfs_tmpfs_close,
    dfs_tmpfs_ioctl,
    dfs_tmpfs_read,
    dfs_tmpfs_write,
    NULL, /* flush */
    dfs_tmpfs_lseek,
    dfs_tmpfs_getdents,
};

static const struct dfs_filesystem_ops _tmpfs =
{
    "tmp",
    DFS_FS_FLAG_DEFAULT,
    &_tmp_fops,

    dfs_tmpfs_mount,
    dfs_tmpfs_unmount,
    NULL, /* mkfs */
    dfs_tmpfs_statfs,

    dfs_tmpfs_unlink,
    dfs_tmpfs_stat,
    dfs_tmpfs_rename,
};

int dfs_tmpfs_init(void)
{
    /* register tmp file system */
    dfs_register(&_tmpfs);

    return 0;
}