fatfs.c 52.0 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
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
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "fatfs.h"
#ifdef LOSCFG_FS_FAT
#include "ff.h"
#include "disk_pri.h"
W
wangchenyang 已提交
36 37 38 39
#include "diskio.h"
#include "fs/fs.h"
#include "fs/dirent_fs.h"
#include "fs/mount.h"
M
mucor 已提交
40 41
#include "vnode.h"
#include "path_cache.h"
W
wenjun 已提交
42 43
#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
#include "virpartff.h"
W
wangchenyang 已提交
44
#include "errcode_fat.h"
W
wenjun 已提交
45
#endif
W
wangchenyang 已提交
46 47
#include "los_tables.h"
#include "user_copy.h"
W
wenjun 已提交
48
#include "los_vm_filemap.h"
W
wangchenyang 已提交
49 50 51 52 53 54 55
#include <time.h>
#include <errno.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
M
mucor 已提交
56
#include "los_hash.h"
W
wenjun 已提交
57 58


W
wangchenyang 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
struct VnodeOps fatfs_vops; /* forward define */
struct file_operations_vfs fatfs_fops;

#define BITMASK4 0x0F
#define BITMASK5 0x1F
#define BITMASK6 0x3F
#define BITMASK7 0x7F
#define FTIME_MIN_OFFSET 6 /* minute offset in word */
#define FTIME_HR_OFFSET 11 /* hour offset in word */
#define FTIME_MTH_OFFSET 5 /* month offset in word */
#define FTIME_YEAR_OFFSET 9 /* year offset in word */
#define FTIME_DATE_OFFSET 16 /* date offset in dword */
#define SEC_MULTIPLIER 2
#define YEAR_OFFSET 80 /* Year start from 1980 in FATFS, while start from 1900 in struct tm */

int fatfs_2_vfs(int result)
W
wenjun 已提交
75
{
W
wangchenyang 已提交
76
    int status = ENOERR;
W
wenjun 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
    if (result < 0 || result >= VIRERR_BASE) {
        return result;
    }
#else
    if (result < 0) {
        return result;
    }
#endif

    /* FatFs errno to Libc errno */
    switch (result) {
        case FR_OK:
            break;

        case FR_NO_FILE:
        case FR_NO_PATH:
W
wangchenyang 已提交
95
            status = ENOENT;
W
wenjun 已提交
96 97
            break;

98 99 100 101
        case FR_NO_FILESYSTEM:
            status = ENOTSUP;
            break;

W
wenjun 已提交
102
        case FR_INVALID_NAME:
W
wangchenyang 已提交
103
            status = EINVAL;
W
wenjun 已提交
104 105 106 107
            break;

        case FR_EXIST:
        case FR_INVALID_OBJECT:
W
wangchenyang 已提交
108
            status = EEXIST;
W
wenjun 已提交
109 110 111 112 113
            break;

        case FR_DISK_ERR:
        case FR_NOT_READY:
        case FR_INT_ERR:
W
wangchenyang 已提交
114
            status = EIO;
W
wenjun 已提交
115 116 117
            break;

        case FR_WRITE_PROTECTED:
W
wangchenyang 已提交
118
            status = EROFS;
W
wenjun 已提交
119 120 121
            break;
        case FR_MKFS_ABORTED:
        case FR_INVALID_PARAMETER:
W
wangchenyang 已提交
122
            status = EINVAL;
W
wenjun 已提交
123 124 125
            break;

        case FR_NO_SPACE_LEFT:
W
wangchenyang 已提交
126
            status = ENOSPC;
W
wenjun 已提交
127 128
            break;
        case FR_NO_DIRENTRY:
W
wangchenyang 已提交
129
            status = ENFILE;
W
wenjun 已提交
130 131
            break;
        case FR_NO_EMPTY_DIR:
W
wangchenyang 已提交
132
            status = ENOTEMPTY;
W
wenjun 已提交
133 134
            break;
        case FR_IS_DIR:
W
wangchenyang 已提交
135
            status = EISDIR;
W
wenjun 已提交
136 137
            break;
        case FR_NO_DIR:
W
wangchenyang 已提交
138
            status = ENOTDIR;
W
wenjun 已提交
139 140 141
            break;
        case FR_NO_EPERM:
        case FR_DENIED:
W
wangchenyang 已提交
142
            status = EPERM;
W
wenjun 已提交
143 144
            break;
        case FR_LOCKED:
W
wangchenyang 已提交
145 146
        case FR_TIMEOUT:
            status = EBUSY;
W
wenjun 已提交
147 148 149
            break;
#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
        case FR_MODIFIED:
W
wangchenyang 已提交
150
            status = -VIRERR_MODIFIED;
W
wenjun 已提交
151 152
            break;
        case FR_CHAIN_ERR:
W
wangchenyang 已提交
153
            status = -VIRERR_CHAIN_ERR;
W
wenjun 已提交
154 155
            break;
        case FR_OCCUPIED:
W
wangchenyang 已提交
156
            status = -VIRERR_OCCUPIED;
W
wenjun 已提交
157 158
            break;
        case FR_NOTCLEAR:
W
wangchenyang 已提交
159
            status = -VIRERR_NOTCLEAR;
W
wenjun 已提交
160 161
            break;
        case FR_NOTFIT:
W
wangchenyang 已提交
162
            status = -VIRERR_NOTFIT;
W
wenjun 已提交
163 164
            break;
        case FR_INVAILD_FATFS:
W
wangchenyang 已提交
165
            status = -VIRERR_INTER_ERR;
W
wenjun 已提交
166 167 168
            break;
#endif
        default:
W
wangchenyang 已提交
169
            status = -FAT_ERROR;
W
wenjun 已提交
170 171 172 173 174 175
            break;
    }

    return status;
}

J
JING 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
static bool fatfs_is_last_cluster(FATFS *fs, DWORD cclust)
{
    switch (fs->fs_type) {
        case FS_FAT12:
            return (cclust == FAT12_END_OF_CLUSTER);
        case FS_FAT16:
            return (cclust == FAT16_END_OF_CLUSTER);
        case FS_FAT32:
        default:
            return (cclust == FAT32_END_OF_CLUSTER);
    }
}

W
wangchenyang 已提交
189
static int fatfs_sync(unsigned long mountflags, FATFS *fs)
W
wenjun 已提交
190
{
W
wangchenyang 已提交
191
#ifdef LOSCFG_FS_FAT_CACHE
W
wenjun 已提交
192
    los_part *part = NULL;
W
wangchenyang 已提交
193 194 195 196 197
    if (mountflags != MS_NOSYNC) {
        part = get_part((INT)fs->pdrv);
        if (part == NULL) {
            return -ENODEV;
        }
W
wenjun 已提交
198

W
wangchenyang 已提交
199
        (void)OsSdSync(part->disk_id);
W
wenjun 已提交
200 201
    }
#endif
W
wangchenyang 已提交
202
    return 0;
W
wenjun 已提交
203
}
W
wangchenyang 已提交
204 205 206 207
int fatfs_hash_cmp(struct Vnode *vp, void *arg)
{
    DIR_FILE *dfp_target = (DIR_FILE *)arg;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
W
wenjun 已提交
208

W
wangchenyang 已提交
209 210 211 212
    return dfp_target->f_dir.sect != dfp->f_dir.sect ||
              dfp_target->f_dir.dptr != dfp->f_dir.dptr ||
              dfp_target->fno.sclst != dfp->fno.sclst;
}
W
wenjun 已提交
213

W
wangchenyang 已提交
214
static DWORD fatfs_hash(QWORD sect, DWORD dptr, DWORD sclst)
W
wenjun 已提交
215
{
M
mucor 已提交
216 217 218 219
    DWORD hash = FNV1_32A_INIT;
    hash = LOS_HashFNV32aBuf(&sect, sizeof(QWORD), hash);
    hash = LOS_HashFNV32aBuf(&dptr, sizeof(DWORD), hash);
    hash = LOS_HashFNV32aBuf(&sclst, sizeof(DWORD), hash);
W
wenjun 已提交
220

W
wangchenyang 已提交
221
    return hash;
W
wenjun 已提交
222 223
}

W
wangchenyang 已提交
224 225 226 227 228
static mode_t fatfs_get_mode(BYTE attribute, mode_t fs_mode)
{
    mode_t mask = 0;
    if (attribute & AM_RDO) {
        mask = S_IWUSR | S_IWGRP | S_IWOTH;
W
wenjun 已提交
229
    }
W
wangchenyang 已提交
230 231 232
    fs_mode &= ~mask;
    if (attribute & AM_DIR) {
        fs_mode |= S_IFDIR;
C
chenjing 已提交
233 234
    } else if (attribute & AM_LNK) {
        fs_mode |= S_IFLNK;
W
wangchenyang 已提交
235 236
    } else {
        fs_mode |= S_IFREG;
W
wenjun 已提交
237
    }
W
wangchenyang 已提交
238
    return fs_mode;
W
wenjun 已提交
239 240
}

C
chenjing 已提交
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
static enum VnodeType fatfstype_2_vnodetype(BYTE type) {
    switch (type) {
        case AM_ARC:
            return VNODE_TYPE_REG;
        case AM_DIR:
            return VNODE_TYPE_DIR;
        case AM_LNK:
            return VNODE_TYPE_LNK;
        default:
            return VNODE_TYPE_UNKNOWN;
    }
}

static FRESULT init_cluster(DIR *dp_new, FATFS *fs, int type, const char *target, DWORD *clust)
{
    FRESULT result;
    BYTE *dir = NULL;
    QWORD sect;
    UINT n;

    /* Allocate a new cluster */
    *clust = create_chain(&(dp_new->obj), 0);
    if (*clust == 0) {
        return FR_NO_SPACE_LEFT;
    }
    if (*clust == 1 || *clust == DISK_ERROR) {
        return FR_DISK_ERR;
    }

    result = sync_window(fs); /* Flush FAT */
    if (result != FR_OK) {
        remove_chain(&(dp_new->obj), *clust, 0);
        return result;
    }

    /* Initialize the new cluster */
#ifndef LOSCFG_FS_FAT_VIRTUAL_PARTITION
    dir = fs->win;
#else
    dir = PARENTFS(fs)->win;
#endif

    sect = clst2sect(fs, *clust);
    mem_set(dir, 0, SS(fs));
    if (type == AM_LNK && target) {
        /* Write target to symlink */
        strcpy_s((char *)dir, SS(fs), target);
    }
    for (n = fs->csize; n > 0; n--) {
#ifndef LOSCFG_FS_FAT_VIRTUAL_PARTITION
        fs->winsect = sect++;
        fs->wflag = 1;
#else
        PARENTFS(fs)->winsect = sect++;
        PARENTFS(fs)->wflag = 1;
296
#endif
C
chenjing 已提交
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
        result = sync_window(fs);
        if (result != FR_OK) {
            remove_chain(&(dp_new->obj), *clust, 0);
            return result;
        }
        if (type == AM_LNK) {
            /* No need to clean the rest sectors of the cluster for symlink */
            break;
        }
    }

    return FR_OK;
}

static int fatfs_create_obj(struct Vnode *parent, const char *name, int mode, struct Vnode **vpp, BYTE type, const char *target)
{
    struct Vnode *vp = NULL;
    FATFS *fs = (FATFS *)parent->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)parent->data;
    FILINFO *finfo = &(dfp->fno);
    DIR_FILE *dfp_new = NULL;
    FILINFO *finfo_new = NULL;
    DIR *dp_new = NULL;
    QWORD time;
    DWORD hash;
    DWORD clust = 0;
    FRESULT result;
    int ret;

    if ((type != AM_ARC) && (type != AM_DIR) && (type != AM_LNK)) {
        result = FR_INVALID_NAME;
        goto ERROR_EXIT;
    }

    dfp_new = (DIR_FILE *)zalloc(sizeof(DIR_FILE));
    if (dfp_new == NULL) {
        result = FR_NOT_ENOUGH_CORE;
        goto ERROR_EXIT;
    }

    ret = lock_fs(fs);
    if (ret == FALSE) { /* lock failed */
        result = FR_TIMEOUT;
        goto ERROR_FREE;
    }

    if (finfo->fattrib & AM_ARC || finfo->fattrib & AM_LNK) {
        result = FR_NO_DIR;
        goto ERROR_UNLOCK;
    }

    finfo_new = &(dfp_new->fno);
    LOS_ListInit(&finfo_new->fp_list);
    dp_new = &(dfp_new->f_dir);
    dp_new->obj.fs = fs;
    dp_new->obj.sclust = finfo->sclst;

    DEF_NAMBUF;
    INIT_NAMBUF(fs);

    result = create_name(dp_new, &name);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
    }

    result = dir_find(dp_new);
    if (result == FR_OK) {
        result = FR_EXIST;
        goto ERROR_UNLOCK;
    }

    if (type == AM_DIR || type == AM_LNK) {
        result = init_cluster(dp_new, fs, type, target, &clust);
        if (result != FR_OK) {
            goto ERROR_UNLOCK;
        }
    }

    result = dir_register(dp_new);
    if (result != FR_OK) {
        goto ERROR_REMOVE_CHAIN;
    }

    /* Set the directory entry attribute */
    if (time_status == SYSTEM_TIME_ENABLE) {
        time = GET_FATTIME();
    } else {
        time = 0;
    }
    st_dword(dp_new->dir + DIR_CrtTime, time);
    st_dword(dp_new->dir + DIR_ModTime, time);
    st_word(dp_new->dir + DIR_LstAccDate, time >> FTIME_DATE_OFFSET);
    dp_new->dir[DIR_Attr] = type;
    if (((DWORD)mode & S_IWUSR) == 0) {
        dp_new->dir[DIR_Attr] |= AM_RDO;
    }
    st_clust(fs, dp_new->dir, clust);
    if (type == AM_ARC) {
        st_dword(dp_new->dir + DIR_FileSize, 0);
    } else if (type == AM_LNK) {
        st_dword(dp_new->dir + DIR_FileSize, strlen(target));
    }

#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
    PARENTFS(fs)->wflag = 1;
#else
    fs->wflag = 1;
#endif
    result = sync_fs(fs);
    if (result != FR_OK) {
        goto ERROR_REMOVE_CHAIN;
    }
    result = dir_read(dp_new, 0);
    if (result != FR_OK) {
        goto ERROR_REMOVE_CHAIN;
    }
    dp_new->blk_ofs = dir_ofs(dp_new);
    get_fileinfo(dp_new, finfo_new);
    if (type == AM_ARC) {
        dp_new->obj.objsize = 0;
    } else if (type == AM_LNK) {
        dp_new->obj.objsize = strlen(target);
    }

    ret = VnodeAlloc(&fatfs_vops, &vp);
    if (ret != 0) {
        result = FR_NOT_ENOUGH_CORE;
        goto ERROR_REMOVE_CHAIN;
    }

    vp->parent = parent;
    vp->fop = &fatfs_fops;
    vp->data = dfp_new;
    vp->originMount = parent->originMount;
    vp->uid = fs->fs_uid;
    vp->gid = fs->fs_gid;
    vp->mode = fatfs_get_mode(finfo_new->fattrib, fs->fs_mode);
    vp->type = fatfstype_2_vnodetype(type);

    hash = fatfs_hash(dp_new->sect, dp_new->dptr, finfo_new->sclst);
    ret = VfsHashInsert(vp, hash);
    if (ret != 0) {
        result = FR_NOT_ENOUGH_CORE;
        goto ERROR_REMOVE_CHAIN;
    }
    *vpp = vp;

    unlock_fs(fs, FR_OK);
    FREE_NAMBUF();
    return fatfs_sync(parent->originMount->mountFlags, fs);

ERROR_REMOVE_CHAIN:
    remove_chain(&(dp_new->obj), clust, 0);
ERROR_UNLOCK:
    unlock_fs(fs, result);
    FREE_NAMBUF();
ERROR_FREE:
    free(dfp_new);
ERROR_EXIT:
    return -fatfs_2_vfs(result);
}

W
wangchenyang 已提交
459
int fatfs_lookup(struct Vnode *parent, const char *path, int len, struct Vnode **vpp)
W
wenjun 已提交
460
{
W
wangchenyang 已提交
461 462 463 464 465 466 467 468
    struct Vnode *vp = NULL;
    FATFS *fs = (FATFS *)(parent->originMount->data);
    DIR_FILE *dfp;
    DIR *dp = NULL;
    FILINFO *finfo = NULL;
    DWORD hash;
    FRESULT result;
    int ret;
W
wenjun 已提交
469

W
wangchenyang 已提交
470 471 472 473
    dfp = (DIR_FILE *)zalloc(sizeof(DIR_FILE));
    if (dfp == NULL) {
        ret = ENOMEM;
        goto ERROR_EXIT;
W
wenjun 已提交
474 475
    }

W
wangchenyang 已提交
476 477 478 479
    ret = lock_fs(fs);
    if (ret == FALSE) {
        ret = EBUSY;
        goto ERROR_FREE;
W
wenjun 已提交
480
    }
W
wangchenyang 已提交
481 482 483 484 485 486 487 488 489 490 491 492
    finfo = &(dfp->fno);
    LOS_ListInit(&finfo->fp_list);
    dp = &(dfp->f_dir);
    dp->obj.fs = fs;
    dp->obj.sclust = ((DIR_FILE *)(parent->data))->fno.sclst;

    DEF_NAMBUF;
    INIT_NAMBUF(fs);
    result = create_name(dp, &path);
    if (result != FR_OK) {
        ret = fatfs_2_vfs(result);
        goto ERROR_UNLOCK;
W
wenjun 已提交
493 494
    }

W
wangchenyang 已提交
495 496 497 498 499
    result = dir_find(dp);
    if (result != FR_OK) {
        ret = fatfs_2_vfs(result);
        goto ERROR_UNLOCK;
    }
W
wenjun 已提交
500

W
wangchenyang 已提交
501 502 503 504 505
    if (dp->fn[NSFLAG] & NS_NONAME) {
        result = FR_INVALID_NAME;
        ret = fatfs_2_vfs(result);
        goto ERROR_UNLOCK;
    }
W
wenjun 已提交
506

W
wangchenyang 已提交
507 508
    get_fileinfo(dp, finfo);
    dp->obj.objsize = 0;
W
wenjun 已提交
509

W
wangchenyang 已提交
510 511 512 513 514 515 516 517
    hash = fatfs_hash(dp->sect, dp->dptr, finfo->sclst);
    ret = VfsHashGet(parent->originMount, hash, &vp, fatfs_hash_cmp, dfp);
    if (ret != 0) {
        ret = VnodeAlloc(&fatfs_vops, &vp);
        if (ret != 0) {
            ret = ENOMEM;
            result = FR_NOT_ENOUGH_CORE;
            goto ERROR_UNLOCK;
W
wenjun 已提交
518
        }
W
wangchenyang 已提交
519 520 521 522 523 524 525 526 527 528 529
        vp->parent = parent;
        vp->fop = &fatfs_fops;
        vp->data = dfp;
        vp->originMount = parent->originMount;
        vp->uid = fs->fs_uid;
        vp->gid = fs->fs_gid;
        vp->mode = fatfs_get_mode(finfo->fattrib, fs->fs_mode);
        if (finfo->fattrib & AM_DIR) {
            vp->type = VNODE_TYPE_DIR;
        } else {
            vp->type = VNODE_TYPE_REG;
W
wenjun 已提交
530
        }
W
wangchenyang 已提交
531 532 533 534 535

        ret = VfsHashInsert(vp, hash);
        if (ret != 0) {
            result = FR_INVALID_PARAMETER;
            goto ERROR_UNLOCK;
W
wenjun 已提交
536 537
        }
    } else {
538
        vp->parent = parent;
W
wangchenyang 已提交
539
        free(dfp); /* hash hit dfp is no needed */
W
wenjun 已提交
540 541
    }

W
wangchenyang 已提交
542 543 544 545
    unlock_fs(fs, FR_OK);
    FREE_NAMBUF();
    *vpp = vp;
    return 0;
W
wenjun 已提交
546

W
wangchenyang 已提交
547 548 549 550 551 552 553
ERROR_UNLOCK:
    unlock_fs(fs, result);
    FREE_NAMBUF();
ERROR_FREE:
    free(dfp);
ERROR_EXIT:
    return -ret;
W
wenjun 已提交
554 555
}

W
wangchenyang 已提交
556
int fatfs_create(struct Vnode *parent, const char *name, int mode, struct Vnode **vpp)
W
wenjun 已提交
557
{
C
chenjing 已提交
558
    return fatfs_create_obj(parent, name, mode, vpp, AM_ARC, NULL);
W
wangchenyang 已提交
559
}
W
wenjun 已提交
560

W
wangchenyang 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574
int fatfs_open(struct file *filep)
{
    struct Vnode *vp = filep->f_vnode;
    FATFS *fs = (FATFS *)vp->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    DIR *dp = &(dfp->f_dir);
    FILINFO *finfo = &(dfp->fno);
    FIL *fp;
    int ret;

    fp = (FIL *)zalloc(sizeof(FIL));
    if (fp == NULL) {
        ret = ENOMEM;
        goto ERROR_EXIT;
W
wenjun 已提交
575
    }
W
wangchenyang 已提交
576 577 578 579
    ret = lock_fs(fs);
    if (ret == FALSE) {
        ret = EBUSY;
        goto ERROR_EXIT;
W
wenjun 已提交
580 581
    }

W
wangchenyang 已提交
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 610
    fp->dir_sect = dp->sect;
    fp->dir_ptr = dp->dir;
    fp->obj.sclust = finfo->sclst;
    fp->obj.objsize = finfo->fsize;
#if FF_USE_FASTSEEK
    fp->cltbl = 0; /* Disable fast seek mode */
#endif
    fp->obj.fs = fs;
    fp->obj.id = fs->id;
    fp->flag = FA_READ | FA_WRITE;
    fp->err = 0;
    fp->sect = 0;
    fp->fptr = 0;
    fp->buf = (BYTE*) ff_memalloc(SS(fs));
    if (fp->buf == NULL) {
        ret = ENOMEM;
        goto ERROR_FREE;
    }
    LOS_ListAdd(&finfo->fp_list, &fp->fp_entry);
    unlock_fs(fs, FR_OK);

    filep->f_priv = fp;
    return fatfs_sync(vp->originMount->mountFlags, fs);

ERROR_FREE:
    unlock_fs(fs, FR_OK);
    free(fp);
ERROR_EXIT:
    return -ret;
W
wenjun 已提交
611 612
}

W
wangchenyang 已提交
613
int fatfs_close(struct file *filep)
W
wenjun 已提交
614
{
W
wangchenyang 已提交
615 616 617 618
    FIL *fp = (FIL *)filep->f_priv;
    FATFS *fs = fp->obj.fs;
    FRESULT result;
    int ret;
W
wenjun 已提交
619

W
wangchenyang 已提交
620 621 622
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
623
    }
W
wangchenyang 已提交
624 625 626 627
#if !FF_FS_READONLY
    result = f_sync(fp); /* Flush cached data */
    if (result != FR_OK) {
        goto EXIT;
W
wenjun 已提交
628
    }
W
wangchenyang 已提交
629 630 631 632
    ret = fatfs_sync(filep->f_vnode->originMount->mountFlags, fs);
    if (ret != 0) {
        unlock_fs(fs, FR_OK);
        return ret;
W
wenjun 已提交
633
    }
W
wangchenyang 已提交
634 635 636 637 638 639 640 641
#endif
    LOS_ListDelete(&fp->fp_entry);
    ff_memfree(fp->buf);
    free(fp);
    filep->f_priv = NULL;
EXIT:
    unlock_fs(fs, result);
    return -fatfs_2_vfs(result);
W
wenjun 已提交
642 643
}

W
wangchenyang 已提交
644
int fatfs_read(struct file *filep, char *buff, size_t count)
W
wenjun 已提交
645
{
W
wangchenyang 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    FIL *fp = (FIL *)filep->f_priv;
    FATFS *fs = fp->obj.fs;
    struct Vnode *vp = filep->f_vnode;
    FILINFO *finfo = &((DIR_FILE *)(vp->data))->fno;
    size_t rcount;
    FRESULT result;
    int ret;

    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
    }
    fp->obj.objsize = finfo->fsize;
    fp->obj.sclust = finfo->sclst;
    result = f_read(fp, buff, count, &rcount);
    if (result != FR_OK) {
        goto EXIT;
W
wenjun 已提交
663
    }
W
wangchenyang 已提交
664 665 666 667 668
    filep->f_pos = fp->fptr;
EXIT:
    unlock_fs(fs, result);
    return rcount;
}
W
wenjun 已提交
669

W
wangchenyang 已提交
670 671 672 673 674 675
static FRESULT update_dir(DIR *dp, FILINFO *finfo)
{
    FATFS *fs = dp->obj.fs;
    DWORD tm;
    BYTE *dbuff = NULL;
    FRESULT result;
W
wenjun 已提交
676

W
wangchenyang 已提交
677 678 679 680 681 682 683 684 685 686 687 688
    result = move_window(fs, dp->sect);
    if (result != FR_OK) {
        return result;
    }
    dbuff = fs->win + dp->dptr % SS(fs);
    dbuff[DIR_Attr] = finfo->fattrib;
    st_clust(fs, dbuff, finfo->sclst); /* Update start cluster */
    st_dword(dbuff + DIR_FileSize, (DWORD)finfo->fsize); /* Update file size */
    if (time_status == SYSTEM_TIME_ENABLE) {
        tm = GET_FATTIME();
    } else {
        tm = 0;
W
wenjun 已提交
689
    }
W
wangchenyang 已提交
690 691 692 693 694 695 696 697 698
    st_dword(dbuff + DIR_ModTime, tm); /* Update mtime */
    st_word(dbuff + DIR_LstAccDate, tm >> FTIME_DATE_OFFSET); /* Update access date */
#ifndef LOSCFG_FS_FAT_VIRTUAL_PARTITION
    fs->wflag = 1;
#else
    PARENTFS(fs)->wflag = 1;
#endif
    return sync_fs(fs);
}
W
wenjun 已提交
699

W
wangchenyang 已提交
700 701 702 703 704 705 706 707 708 709
off64_t fatfs_lseek64(struct file *filep, off64_t offset, int whence)
{
    FIL *fp = (FIL *)filep->f_priv;
    FATFS *fs = fp->obj.fs;
    struct Vnode *vp = filep->f_vnode;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    FILINFO *finfo = &(dfp->fno);
    FSIZE_t fpos;
    FRESULT result;
    int ret;
W
wenjun 已提交
710

W
wangchenyang 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    switch (whence) {
        case SEEK_CUR:
            offset = filep->f_pos + offset;
            if (offset < 0) {
                return -EINVAL;
            }
            fpos = offset;
            break;
        case SEEK_SET:
            if (offset < 0) {
                return -EINVAL;
            }
            fpos = offset;
            break;
        case SEEK_END:
            offset = (off_t)((long long)finfo->fsize + offset);
            if (offset < 0) {
                return -EINVAL;
            }
            fpos = offset;
            break;
        default:
            return -EINVAL;
W
wenjun 已提交
734 735
    }

W
wangchenyang 已提交
736 737
    if (offset >= FAT32_MAXSIZE) {
        return -EINVAL;
W
wenjun 已提交
738 739
    }

W
wangchenyang 已提交
740 741 742
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
743
    }
W
wangchenyang 已提交
744 745
    fp->obj.sclust = finfo->sclst;
    fp->obj.objsize = finfo->fsize;
W
wenjun 已提交
746

W
wangchenyang 已提交
747 748 749 750 751
    result = f_lseek(fp, fpos);
    finfo->fsize = fp->obj.objsize;
    finfo->sclst = fp->obj.sclust;
    if (result != FR_OK) {
        goto ERROR_EXIT;
W
wenjun 已提交
752 753
    }

W
wangchenyang 已提交
754 755 756
    result = f_sync(fp);
    if (result != FR_OK) {
        goto ERROR_EXIT;
W
wenjun 已提交
757
    }
W
wangchenyang 已提交
758
    filep->f_pos = fpos;
W
wenjun 已提交
759

W
wangchenyang 已提交
760 761 762 763 764 765
    unlock_fs(fs, FR_OK);
    return fpos;
ERROR_EXIT:
    unlock_fs(fs, result);
    return -fatfs_2_vfs(result);
}
W
wenjun 已提交
766

W
wangchenyang 已提交
767 768 769
off_t fatfs_lseek(struct file *filep, off_t offset, int whence)
{
    return (off_t)fatfs_lseek64(filep, offset, whence);
W
wenjun 已提交
770 771
}

W
wangchenyang 已提交
772
static int update_filbuff(FILINFO *finfo, FIL *wfp, const char  *data)
W
wenjun 已提交
773
{
W
wangchenyang 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    LOS_DL_LIST *list = &finfo->fp_list;
    FATFS *fs = wfp->obj.fs;
    FIL *entry = NULL;
    int ret = 0;

    LOS_DL_LIST_FOR_EACH_ENTRY(entry, list, FIL, fp_entry) {
        if (entry == wfp) {
            continue;
        }
        if (entry->sect != 0) {
            if (disk_read(fs->pdrv, entry->buf, entry->sect, 1) != RES_OK) {
                ret = -1;
            }
        }
    }

    return ret;
W
wenjun 已提交
791 792
}

W
wangchenyang 已提交
793
int fatfs_write(struct file *filep, const char *buff, size_t count)
W
wenjun 已提交
794
{
W
wangchenyang 已提交
795 796 797 798 799
    FIL *fp = (FIL *)filep->f_priv;
    FATFS *fs = fp->obj.fs;
    struct Vnode *vp = filep->f_vnode;
    FILINFO *finfo = &(((DIR_FILE *)vp->data)->fno);
    size_t wcount;
W
wenjun 已提交
800
    FRESULT result;
W
wangchenyang 已提交
801
    int ret;
W
wenjun 已提交
802

W
wangchenyang 已提交
803 804 805
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
806
    }
W
wangchenyang 已提交
807 808 809 810 811
    fp->obj.objsize = finfo->fsize;
    fp->obj.sclust = finfo->sclst;
    result = f_write(fp, buff, count, &wcount);
    if (result != FR_OK) {
        goto ERROR_EXIT;
W
wenjun 已提交
812 813
    }

W
wangchenyang 已提交
814 815 816 817 818
    finfo->fsize = fp->obj.objsize;
    finfo->sclst = fp->obj.sclust;
    result = f_sync(fp);
    if (result != FR_OK) {
        goto ERROR_EXIT;
W
wenjun 已提交
819
    }
W
wangchenyang 已提交
820
    update_filbuff(finfo, fp, buff);
W
wenjun 已提交
821

W
wangchenyang 已提交
822
    filep->f_pos = fp->fptr;
W
wenjun 已提交
823

W
wangchenyang 已提交
824 825 826 827 828 829
    unlock_fs(fs, FR_OK);
    return wcount;
ERROR_EXIT:
    unlock_fs(fs, result);
    return -fatfs_2_vfs(result);
}
W
wenjun 已提交
830

W
wangchenyang 已提交
831 832 833 834 835 836
int fatfs_fsync(struct file *filep)
{
    FIL *fp = filep->f_priv;
    FATFS *fs = fp->obj.fs;
    FRESULT result;
    int ret;
W
wenjun 已提交
837

W
wangchenyang 已提交
838 839 840
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
841 842
    }

W
wangchenyang 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
    result = f_sync(fp);
    unlock_fs(fs, result);
    return -fatfs_2_vfs(result);
}

int fatfs_fallocate64(struct file *filep, int mode, off64_t offset, off64_t len)
{
    FIL *fp = (FIL *)filep->f_priv;
    FATFS *fs = fp->obj.fs;
    struct Vnode *vp = filep->f_vnode;
    FILINFO *finfo = &((DIR_FILE *)(vp->data))->fno;
    FRESULT result;
    int ret;

    if (offset < 0 || len <= 0) {
W
wenjun 已提交
858 859 860
        return -EINVAL;
    }

W
wangchenyang 已提交
861 862 863
    if (len >= FAT32_MAXSIZE || offset >= FAT32_MAXSIZE ||
        len + offset >= FAT32_MAXSIZE) {
        return -EINVAL;
W
wenjun 已提交
864 865
    }

W
wangchenyang 已提交
866 867
    if (mode != FALLOC_FL_KEEP_SIZE) {
        return -EINVAL;
W
wenjun 已提交
868 869
    }

W
wangchenyang 已提交
870 871 872
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
873
    }
W
wangchenyang 已提交
874 875 876 877 878 879
    result = f_expand(fp, (FSIZE_t)offset, (FSIZE_t)len, 1);
    if (result == FR_OK && finfo->sclst == 0) {
        finfo->sclst = fp->obj.sclust;
    }
    result = f_sync(fp);
    unlock_fs(fs, FR_OK);
W
wenjun 已提交
880

W
wangchenyang 已提交
881
    return -fatfs_2_vfs(result);
W
wenjun 已提交
882 883
}

W
wangchenyang 已提交
884
static FRESULT realloc_cluster(FILINFO *finfo, FFOBJID *obj, FSIZE_t size)
W
wenjun 已提交
885
{
W
wangchenyang 已提交
886 887 888 889 890
    FATFS *fs = obj->fs;
    off64_t remain;
    DWORD cclust;
    DWORD pclust;
    QWORD csize;
W
wenjun 已提交
891 892
    FRESULT result;

W
wangchenyang 已提交
893 894 895 896 897 898 899 900 901
    if (size == 0) { /* Remove cluster chain */
        if (finfo->sclst != 0) {
            result = remove_chain(obj, finfo->sclst, 0);
            if (result != FR_OK) {
                return result;
            }
            finfo->sclst = 0;
        }
        return FR_OK;
W
wenjun 已提交
902 903
    }

W
wangchenyang 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    remain = size;
    csize = SS(fs) * fs->csize;
    if (finfo->sclst == 0) { /* Allocate one cluster if file doesn't have any cluster */
        cclust = create_chain(obj, 0);
        if (cclust == 0) {
            return FR_NO_SPACE_LEFT;
        }
        if (cclust == 1 || cclust == DISK_ERROR) {
            return FR_DISK_ERR;
        }
        finfo->sclst = cclust;
    }
    cclust = finfo->sclst;
    while (remain > csize) { /* Follow or strentch the cluster chain */
        pclust = cclust;
        cclust = create_chain(obj, pclust);
        if (cclust == 0) {
            return FR_NO_SPACE_LEFT;
        }
        if (cclust == 1 || cclust == DISK_ERROR) {
            return FR_DISK_ERR;
        }
        remain -= csize;
W
wenjun 已提交
927
    }
W
wangchenyang 已提交
928 929 930 931 932
    pclust = cclust;
    cclust = get_fat(obj, pclust);
    if ((cclust == BAD_CLUSTER) || (cclust == DISK_ERROR)) {
        return FR_DISK_ERR;
    }
J
JING 已提交
933
    if (!fatfs_is_last_cluster(obj->fs, cclust)) { /* Remove extra cluster if existing */
W
wangchenyang 已提交
934 935 936 937
        result = remove_chain(obj, cclust, pclust);
        if (result != FR_OK) {
            return result;
        }
W
wenjun 已提交
938 939
    }

W
wangchenyang 已提交
940
    return FR_OK;
W
wenjun 已提交
941 942
}

W
wangchenyang 已提交
943
int fatfs_fallocate(struct file *filep, int mode, off_t offset, off_t len)
W
wenjun 已提交
944
{
W
wangchenyang 已提交
945
    return fatfs_fallocate64(filep, mode, offset, len);
W
wenjun 已提交
946 947
}

W
wangchenyang 已提交
948
int fatfs_truncate64(struct Vnode *vp, off64_t len)
W
wenjun 已提交
949
{
W
wangchenyang 已提交
950 951 952 953 954 955 956 957 958 959 960
    FATFS *fs = (FATFS *)vp->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    DIR *dp = &(dfp->f_dir);
    FILINFO *finfo = &(dfp->fno);
    FFOBJID object;
    FRESULT result = FR_OK;
    int ret;

    if (len < 0 || len >= FAT32_MAXSIZE) {
        return -EINVAL;
    }
W
wenjun 已提交
961

W
wangchenyang 已提交
962 963 964 965
    ret = lock_fs(fs);
    if (ret == FALSE) {
        result = FR_TIMEOUT;
        goto ERROR_OUT;
W
wenjun 已提交
966
    }
W
wangchenyang 已提交
967 968 969
    if (len == finfo->fsize) {
        unlock_fs(fs, FR_OK);
        return 0;
W
wenjun 已提交
970 971
    }

W
wangchenyang 已提交
972 973 974 975
    object.fs = fs;
    result = realloc_cluster(finfo, &object, (FSIZE_t)len);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
W
wenjun 已提交
976
    }
W
wangchenyang 已提交
977
    finfo->fsize = (FSIZE_t)len;
W
wenjun 已提交
978

W
wangchenyang 已提交
979 980 981 982 983 984 985 986 987 988
    result = update_dir(dp, finfo);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
    }
    unlock_fs(fs, FR_OK);
    return fatfs_sync(vp->originMount->mountFlags, fs);
ERROR_UNLOCK:
    unlock_fs(fs, result);
ERROR_OUT:
    return -fatfs_2_vfs(result);
W
wenjun 已提交
989 990
}

W
wangchenyang 已提交
991
int fatfs_truncate(struct Vnode *vp, off_t len)
W
wenjun 已提交
992
{
W
wangchenyang 已提交
993
    return fatfs_truncate64(vp, len);
W
wenjun 已提交
994 995
}

W
wangchenyang 已提交
996
static int fat_bind_check(struct Vnode *blk_driver, los_part **partition)
W
wenjun 已提交
997
{
W
wangchenyang 已提交
998
    los_part *part = NULL;
W
wenjun 已提交
999

W
wangchenyang 已提交
1000 1001
    if (blk_driver == NULL || blk_driver->data == NULL) {
        return ENODEV;
W
wenjun 已提交
1002 1003
    }

W
wangchenyang 已提交
1004 1005 1006
    struct drv_data *dd = blk_driver->data;
    if (dd->ops == NULL) {
        return ENODEV;
W
wenjun 已提交
1007
    }
W
wangchenyang 已提交
1008 1009 1010
    const struct block_operations *bops = dd->ops;
    if (bops->open == NULL) {
        return EINVAL;
W
wenjun 已提交
1011
    }
W
wangchenyang 已提交
1012 1013
    if (bops->open(blk_driver) < 0) {
        return EBUSY;
W
wenjun 已提交
1014 1015
    }

W
wangchenyang 已提交
1016
    part = los_part_find(blk_driver);
Y
YOUR_NAME 已提交
1017 1018 1019
    if (part == NULL) {
        return ENODEV;
    }
W
wangchenyang 已提交
1020 1021 1022
    if (part->part_name != NULL) {
        bops->close(blk_driver);
        return EBUSY;
W
wenjun 已提交
1023 1024
    }

W
wangchenyang 已提交
1025 1026 1027 1028
#ifndef FF_MULTI_PARTITION
    if (part->part_no_mbr > 1) {
        bops->close(blk_driver);
        return EPERM;
W
wenjun 已提交
1029 1030 1031
    }
#endif

W
wangchenyang 已提交
1032 1033
    *partition = part;
    return 0;
W
wenjun 已提交
1034 1035
}

W
wangchenyang 已提交
1036
int fatfs_mount(struct Mount *mnt, struct Vnode *blk_device, const void *data)
W
wenjun 已提交
1037
{
W
wangchenyang 已提交
1038 1039 1040 1041 1042 1043 1044
    struct Vnode *vp = NULL;
    FATFS *fs = NULL;
    DIR_FILE *dfp = NULL;
    los_part *part = NULL;
    QWORD start_sector;
    BYTE fmt;
    DWORD hash;
W
wenjun 已提交
1045
    FRESULT result;
W
wangchenyang 已提交
1046
    int ret;
W
wenjun 已提交
1047

W
wangchenyang 已提交
1048 1049 1050
    ret = fat_bind_check(blk_device, &part);
    if (ret != 0) {
        goto ERROR_EXIT;
W
wenjun 已提交
1051 1052
    }

W
wangchenyang 已提交
1053 1054 1055 1056
    ret = SetDiskPartName(part, "vfat");
    if (ret != 0) {
        ret = EIO;
        goto ERROR_EXIT;
W
wenjun 已提交
1057 1058
    }

W
wangchenyang 已提交
1059 1060 1061 1062
    fs = (FATFS *)zalloc(sizeof(FATFS));
    if (fs == NULL) {
        ret = ENOMEM;
        goto ERROR_PARTNAME;
W
wenjun 已提交
1063 1064
    }

W
wangchenyang 已提交
1065 1066 1067 1068 1069 1070
#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
    fs->vir_flag = FS_PARENT;
    fs->parent_fs = fs;
    fs->vir_amount = DISK_ERROR;
    fs->vir_avail = FS_VIRDISABLE;
#endif
W
wenjun 已提交
1071

W
wangchenyang 已提交
1072 1073 1074 1075
    ret = ff_cre_syncobj(0, &fs->sobj);
    if (ret == 0) { /* create sync object failed */
        ret = EINVAL;
        goto ERROR_WITH_FS;
W
wenjun 已提交
1076 1077
    }

W
wangchenyang 已提交
1078 1079 1080 1081
    ret = lock_fs(fs);
    if (ret == FALSE) {
        ret = EBUSY;
        goto ERROR_WITH_MUX;
W
wenjun 已提交
1082 1083
    }

W
wangchenyang 已提交
1084 1085
    fs->fs_type = 0;
    fs->pdrv = part->part_id;
W
wenjun 已提交
1086

W
wangchenyang 已提交
1087 1088 1089 1090
#if FF_MAX_SS != FF_MIN_SS  /* Get sector size (multiple sector size cfg only) */
    if (disk_ioctl(fs->pdrv, GET_SECTOR_SIZE, &(fs->ssize)) != RES_OK) {
        ret = EIO;
        goto ERROR_WITH_LOCK;
W
wenjun 已提交
1091
    }
W
wangchenyang 已提交
1092 1093 1094
    if (fs->ssize > FF_MAX_SS || fs->ssize < FF_MIN_SS || (fs->ssize & (fs->ssize - 1))) {
        ret = EIO;
        goto ERROR_WITH_LOCK;
W
wenjun 已提交
1095
    }
W
wangchenyang 已提交
1096
#endif
W
wenjun 已提交
1097

W
wangchenyang 已提交
1098 1099 1100 1101
    fs->win = (BYTE *)ff_memalloc(SS(fs));
    if (fs->win == NULL) {
        ret = ENOMEM;
        goto ERROR_WITH_LOCK;
W
wenjun 已提交
1102 1103
    }

W
wangchenyang 已提交
1104
    result = find_fat_partition(fs, part, &fmt, &start_sector);
W
wenjun 已提交
1105
    if (result != FR_OK) {
W
wangchenyang 已提交
1106 1107
        ret = fatfs_2_vfs(result);
        goto ERROR_WITH_FSWIN;
W
wenjun 已提交
1108 1109
    }

W
wangchenyang 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
    result = init_fatobj(fs, fmt, start_sector);
    if (result != FR_OK) {
        ret = fatfs_2_vfs(result);
        goto ERROR_WITH_FSWIN;
    }

    fs->fs_uid = mnt->vnodeBeCovered->uid;
    fs->fs_gid = mnt->vnodeBeCovered->gid;
    fs->fs_dmask = GetUmask();
    fs->fs_fmask = GetUmask();
    fs->fs_mode = mnt->vnodeBeCovered->mode & (S_IRWXU | S_IRWXG | S_IRWXO);

    dfp = (DIR_FILE *)zalloc(sizeof(DIR_FILE));
    if (dfp == NULL) {
        ret = ENOMEM;
        goto ERROR_WITH_FSWIN;
    }

    dfp->f_dir.obj.fs = fs;
    dfp->f_dir.obj.sclust = 0; /* set start clust 0, root */
    dfp->f_dir.obj.attr = AM_DIR;
    dfp->f_dir.obj.objsize = 0; /* dir size is 0 */
    dfp->fno.fsize = 0;
    dfp->fno.fdate = 0;
    dfp->fno.ftime = 0;
    dfp->fno.fattrib = AM_DIR;
    dfp->fno.sclst = 0;
    dfp->fno.fname[0] = '/'; /* Mark as root dir */
    dfp->fno.fname[1] = '\0';
    LOS_ListInit(&(dfp->fno.fp_list));

    ret = VnodeAlloc(&fatfs_vops, &vp);
    if (ret != 0) {
        ret = ENOMEM;
        goto ERROR_WITH_FSWIN;
    }

    mnt->data = fs;
    mnt->vnodeCovered = vp;

    vp->parent = mnt->vnodeBeCovered;
    vp->fop = &fatfs_fops;
    vp->data = dfp;
    vp->originMount = mnt;
    vp->uid = fs->fs_uid;
    vp->gid = fs->fs_gid;
    vp->mode = mnt->vnodeBeCovered->mode;
    vp->type = VNODE_TYPE_DIR;

    hash = fatfs_hash(0, 0, 0);
    ret = VfsHashInsert(vp, hash);
    if (ret != 0) {
        ret = -ret;
        goto ERROR_WITH_LOCK;
    }
    unlock_fs(fs, FR_OK);
W
wenjun 已提交
1166 1167

    return 0;
W
wangchenyang 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

ERROR_WITH_FSWIN:
    ff_memfree(fs->win);
ERROR_WITH_LOCK:
    unlock_fs(fs, FR_OK);
ERROR_WITH_MUX:
    ff_del_syncobj(&fs->sobj);
ERROR_WITH_FS:
    free(fs);
ERROR_PARTNAME:
    if (part->part_name) {
        free(part->part_name);
        part->part_name = NULL;
    }
ERROR_EXIT:
    return -ret;
W
wenjun 已提交
1184 1185
}

W
wangchenyang 已提交
1186
int fatfs_umount(struct Mount *mnt, struct Vnode **blkdriver)
W
wenjun 已提交
1187
{
W
wangchenyang 已提交
1188 1189 1190 1191
    struct Vnode *device;
    FATFS *fs = (FATFS *)mnt->data;
    los_part *part;
    int ret;
W
wenjun 已提交
1192

W
wangchenyang 已提交
1193 1194 1195
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
1196 1197
    }

W
wangchenyang 已提交
1198 1199 1200 1201
    part = get_part(fs->pdrv);
    if (part == NULL) {
        unlock_fs(fs, FR_OK);
        return -ENODEV;
W
wenjun 已提交
1202
    }
W
wangchenyang 已提交
1203 1204 1205 1206
    device = part->dev;
    if (device == NULL) {
        unlock_fs(fs, FR_OK);
        return -ENODEV;
W
wenjun 已提交
1207
    }
W
wangchenyang 已提交
1208 1209 1210 1211 1212
#ifdef LOSCFG_FS_FAT_CACHE
    ret = OsSdSync(part->disk_id);
    if (ret != 0) {
        unlock_fs(fs, FR_DISK_ERR);
        return -EIO;
W
wenjun 已提交
1213
    }
W
wangchenyang 已提交
1214 1215 1216 1217
#endif
    if (part->part_name != NULL) {
        free(part->part_name);
        part->part_name = NULL;
W
wenjun 已提交
1218 1219
    }

W
wangchenyang 已提交
1220 1221 1222 1223
    struct drv_data *dd = device->data;
    if (dd->ops == NULL) {
        unlock_fs(fs, FR_OK);
        return ENODEV;
W
wenjun 已提交
1224 1225
    }

W
wangchenyang 已提交
1226 1227 1228
    const struct block_operations *bops = dd->ops;
    if (bops != NULL && bops->close != NULL) {
        bops->close(*blkdriver);
W
wenjun 已提交
1229 1230
    }

W
wangchenyang 已提交
1231 1232
    if (fs->win != NULL) {
        ff_memfree(fs->win);
W
wenjun 已提交
1233 1234
    }

W
wangchenyang 已提交
1235
    unlock_fs(fs, FR_OK);
W
wenjun 已提交
1236

W
wangchenyang 已提交
1237 1238
    ret = ff_del_syncobj(&fs->sobj);
    if (ret == FALSE) {
W
wenjun 已提交
1239 1240
        return -EINVAL;
    }
W
wangchenyang 已提交
1241
    free(fs);
W
wenjun 已提交
1242

W
wangchenyang 已提交
1243
    *blkdriver = device;
W
wenjun 已提交
1244

W
wangchenyang 已提交
1245
    return 0;
W
wenjun 已提交
1246 1247
}

W
wangchenyang 已提交
1248
int fatfs_statfs(struct Mount *mnt, struct statfs *info)
W
wenjun 已提交
1249
{
W
wangchenyang 已提交
1250
    FATFS *fs = (FATFS *)mnt->data;
1251 1252 1253
    DWORD nclst = 0;
    FRESULT result = FR_OK;
    int ret;
W
wenjun 已提交
1254

W
wangchenyang 已提交
1255 1256 1257
    info->f_type = MSDOS_SUPER_MAGIC;
#if FF_MAX_SS != FF_MIN_SS
    info->f_bsize = fs->ssize * fs->csize;
W
wenjun 已提交
1258
#else
W
wangchenyang 已提交
1259
    info->f_bsize = FF_MIN_SS * fs->csize;
W
wenjun 已提交
1260
#endif
W
wangchenyang 已提交
1261
    info->f_blocks = fs->n_fatent;
1262 1263 1264 1265 1266 1267 1268 1269
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
    }
    /* free cluster is unavailable, update it */
    if (fs->free_clst == DISK_ERROR) {
        result = fat_count_free_entries(&nclst, fs);
    }
W
wangchenyang 已提交
1270 1271
    info->f_bfree = fs->free_clst;
    info->f_bavail = fs->free_clst;
1272
    unlock_fs(fs, result);
W
wenjun 已提交
1273

W
wangchenyang 已提交
1274 1275 1276
#if FF_USE_LFN
    /* Maximum length of filenames */
    info->f_namelen = FF_MAX_LFN;
W
wenjun 已提交
1277
#else
W
wangchenyang 已提交
1278 1279
    /* Maximum length of filenames: 8 is the basename length, 1 is the dot, 3 is the extension length */
    info->f_namelen = (8 + 1 + 3);
W
wenjun 已提交
1280
#endif
W
wangchenyang 已提交
1281 1282 1283 1284 1285 1286
    info->f_fsid.__val[0] = MSDOS_SUPER_MAGIC;
    info->f_fsid.__val[1] = 1;
    info->f_frsize = SS(fs) * fs->csize;
    info->f_files = 0;
    info->f_ffree = 0;
    info->f_flags = mnt->mountFlags;
W
wenjun 已提交
1287

1288
    return -fatfs_2_vfs(result);
W
wenjun 已提交
1289 1290
}

W
wangchenyang 已提交
1291
static inline int GET_SECONDS(WORD ftime)
W
wenjun 已提交
1292
{
W
wangchenyang 已提交
1293
    return (ftime & BITMASK5) * SEC_MULTIPLIER;
W
wenjun 已提交
1294
}
W
wangchenyang 已提交
1295
static inline int GET_MINUTES(WORD ftime)
W
wenjun 已提交
1296
{
W
wangchenyang 已提交
1297
    return (ftime >> FTIME_MIN_OFFSET) & BITMASK6;
W
wenjun 已提交
1298
}
W
wangchenyang 已提交
1299
static inline int GET_HOURS(WORD ftime)
W
wenjun 已提交
1300
{
W
wangchenyang 已提交
1301
    return (ftime >> FTIME_HR_OFFSET) & BITMASK5;
W
wenjun 已提交
1302
}
W
wangchenyang 已提交
1303
static inline int GET_DAY(WORD fdate)
W
wenjun 已提交
1304
{
W
wangchenyang 已提交
1305
    return fdate & BITMASK5;
W
wenjun 已提交
1306
}
W
wangchenyang 已提交
1307
static inline int GET_MONTH(WORD fdate)
W
wenjun 已提交
1308
{
W
wangchenyang 已提交
1309
    return (fdate >> FTIME_MTH_OFFSET) & BITMASK4;
W
wenjun 已提交
1310
}
W
wangchenyang 已提交
1311
static inline int GET_YEAR(WORD fdate)
W
wenjun 已提交
1312
{
W
wangchenyang 已提交
1313
    return (fdate >> FTIME_YEAR_OFFSET) & BITMASK7;
W
wenjun 已提交
1314 1315
}

W
wangchenyang 已提交
1316
static time_t fattime_transfer(WORD fdate, WORD ftime)
W
wenjun 已提交
1317
{
Y
YOUR_NAME 已提交
1318
    struct tm time = { 0 };
W
wangchenyang 已提交
1319 1320 1321 1322 1323 1324 1325 1326
    time.tm_sec = GET_SECONDS(ftime);
    time.tm_min = GET_MINUTES(ftime);
    time.tm_hour = GET_HOURS(ftime);
    time.tm_mday = GET_DAY(fdate);
    time.tm_mon = GET_MONTH(fdate);
    time.tm_year = GET_YEAR(fdate) + YEAR_OFFSET; /* Year start from 1980 in FATFS */
    time_t ret = mktime(&time);
    return ret;
W
wenjun 已提交
1327 1328
}

W
wangchenyang 已提交
1329
DWORD fattime_format(time_t time)
W
wenjun 已提交
1330
{
W
wangchenyang 已提交
1331 1332
    struct tm st;
    DWORD ftime;
W
wenjun 已提交
1333

W
wangchenyang 已提交
1334
    localtime_r(&time, &st);
W
wenjun 已提交
1335

W
wangchenyang 已提交
1336 1337 1338 1339
    ftime = (DWORD)st.tm_mday;
    ftime |= ((DWORD)st.tm_mon) << FTIME_MTH_OFFSET;
    ftime |= ((DWORD)((st.tm_year > YEAR_OFFSET) ? (st.tm_year - YEAR_OFFSET) : 0)) << FTIME_YEAR_OFFSET;
    ftime <<= FTIME_DATE_OFFSET;
W
wenjun 已提交
1340

W
wangchenyang 已提交
1341 1342 1343
    ftime = (DWORD)st.tm_sec / SEC_MULTIPLIER;
    ftime |= ((DWORD)st.tm_min) << FTIME_MIN_OFFSET;
    ftime |= ((DWORD)st.tm_hour) << FTIME_HR_OFFSET;
W
wenjun 已提交
1344

W
wangchenyang 已提交
1345
    return ftime;
W
wenjun 已提交
1346 1347
}

W
wangchenyang 已提交
1348
int fatfs_stat(struct Vnode *vp, struct stat* sp)
W
wenjun 已提交
1349
{
W
wangchenyang 已提交
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
    FATFS *fs = (FATFS *)vp->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    FILINFO *finfo = &(dfp->fno);
    time_t time;
    int ret;

    ret = lock_fs(fs);
    if (ret == FALSE) {
        return EBUSY;
    }

    sp->st_dev = fs->pdrv;
    sp->st_mode = vp->mode;
    sp->st_nlink = 1;
    sp->st_uid = fs->fs_uid;
    sp->st_gid = fs->fs_gid;
    sp->st_size = finfo->fsize;
    sp->st_blksize = fs->csize * SS(fs);
    sp->st_blocks = finfo->fsize ? ((finfo->fsize - 1) / SS(fs) / fs->csize + 1) : 0;
    time = fattime_transfer(finfo->fdate, finfo->ftime);
    sp->st_mtime = time;

    unlock_fs(fs, FR_OK);
    return 0;
}
W
wenjun 已提交
1375

W
wangchenyang 已提交
1376 1377 1378 1379 1380 1381 1382 1383
void fatfs_chtime(DIR *dp, struct IATTR *attr)
{
    BYTE *dir = dp->dir;
    DWORD ftime;
    if (attr->attr_chg_valid & CHG_ATIME) {
        ftime = fattime_format(attr->attr_chg_atime);
        st_word(dir + DIR_LstAccDate, (ftime >> FTIME_DATE_OFFSET));
    }
W
wenjun 已提交
1384

W
wangchenyang 已提交
1385 1386 1387 1388
    if (attr->attr_chg_valid & CHG_CTIME) {
        ftime = fattime_format(attr->attr_chg_ctime);
        st_dword(dir + DIR_CrtTime, ftime);
    }
W
wenjun 已提交
1389

W
wangchenyang 已提交
1390 1391 1392
    if (attr->attr_chg_valid & CHG_MTIME) {
        ftime = fattime_format(attr->attr_chg_mtime);
        st_dword(dir + DIR_ModTime, ftime);
W
wenjun 已提交
1393
    }
W
wangchenyang 已提交
1394
}
W
wenjun 已提交
1395

W
wangchenyang 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
int fatfs_chattr(struct Vnode *vp, struct IATTR *attr)
{
    FATFS *fs = (FATFS *)vp->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    DIR *dp = &(dfp->f_dir);
    FILINFO *finfo = &(dfp->fno);
    BYTE *dir = dp->dir;
    DWORD ftime;
    FRESULT result;
    int ret;
W
wenjun 已提交
1406

W
wangchenyang 已提交
1407 1408
    if (finfo->fname[0] == '/') { /* Is root dir of fatfs ? */
        return 0;
W
wenjun 已提交
1409 1410
    }

W
wangchenyang 已提交
1411 1412 1413 1414
    ret = lock_fs(fs);
    if (ret == FALSE) {
        result = FR_TIMEOUT;
        goto ERROR_OUT;
W
wenjun 已提交
1415 1416
    }

W
wangchenyang 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    result = move_window(fs, dp->sect);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
    }

    if (attr->attr_chg_valid & CHG_MODE) {
        /* FAT only support readonly flag */
        if ((attr->attr_chg_mode & S_IWUSR) == 0 && (finfo->fattrib & AM_RDO) == 0) {
            dir[DIR_Attr] |= AM_RDO;
            finfo->fattrib |= AM_RDO;
            fs->wflag = 1;
        } else if ((attr->attr_chg_mode & S_IWUSR) != 0 && (finfo->fattrib & AM_RDO) != 0) {
            dir[DIR_Attr] &= ~AM_RDO;
            finfo->fattrib &= ~AM_RDO;
            fs->wflag = 1;
W
wenjun 已提交
1432
        }
W
wangchenyang 已提交
1433
        vp->mode = fatfs_get_mode(finfo->fattrib, fs->fs_mode);
W
wenjun 已提交
1434 1435
    }

W
wangchenyang 已提交
1436 1437 1438 1439 1440 1441
    if (attr->attr_chg_valid & (CHG_ATIME | CHG_CTIME | CHG_MTIME)) {
        fatfs_chtime(dp, attr);
        ftime = ld_dword(dp->dir + DIR_ModTime);
        finfo->fdate = (WORD)(ftime >> FTIME_DATE_OFFSET);
        finfo->ftime = (WORD)ftime;
    }
W
wenjun 已提交
1442

W
wangchenyang 已提交
1443 1444 1445
    result = sync_window(fs);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
W
wenjun 已提交
1446 1447
    }

W
wangchenyang 已提交
1448 1449 1450 1451 1452 1453
    unlock_fs(fs, FR_OK);
    return fatfs_sync(vp->originMount->mountFlags, fs);
ERROR_UNLOCK:
    unlock_fs(fs, result);
ERROR_OUT:
    return -fatfs_2_vfs(result);
W
wenjun 已提交
1454 1455
}

W
wangchenyang 已提交
1456
int fatfs_opendir(struct Vnode *vp, struct fs_dirent_s *idir)
W
wenjun 已提交
1457
{
W
wangchenyang 已提交
1458 1459 1460 1461 1462
    FATFS *fs = vp->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    FILINFO *finfo = &(dfp->fno);
    DIR *dp;
    DWORD clst;
W
wenjun 已提交
1463
    FRESULT result;
W
wangchenyang 已提交
1464
    int ret;
W
wenjun 已提交
1465

W
wangchenyang 已提交
1466 1467 1468
    dp = (DIR*)zalloc(sizeof(DIR));
    if (dp == NULL) {
        return -ENOMEM;
W
wenjun 已提交
1469 1470
    }

W
wangchenyang 已提交
1471 1472 1473
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
1474
    }
W
wangchenyang 已提交
1475 1476 1477
    clst = finfo->sclst;
    dp->obj.fs = fs;
    dp->obj.sclust = clst;
W
wenjun 已提交
1478

W
wangchenyang 已提交
1479 1480 1481 1482 1483
    result = dir_sdi(dp, 0);
    if (result != FR_OK) {
        free(dp);
        unlock_fs(fs, result);
        return -fatfs_2_vfs(result);
W
wenjun 已提交
1484
    }
W
wangchenyang 已提交
1485 1486
    unlock_fs(fs, result);
    idir->u.fs_dir = dp;
W
wenjun 已提交
1487

W
wangchenyang 已提交
1488 1489
    return 0;
}
W
wenjun 已提交
1490

W
wangchenyang 已提交
1491 1492 1493 1494 1495 1496 1497 1498
int fatfs_readdir(struct Vnode *vp, struct fs_dirent_s *idir)
{
    FATFS *fs = vp->originMount->data;
    FILINFO fno;
    DIR* dp = (DIR*)idir->u.fs_dir;
    struct dirent *dirp = NULL;
    FRESULT result;
    int ret, i;
W
wenjun 已提交
1499

W
wangchenyang 已提交
1500 1501 1502
    ret = lock_fs(fs);
    if (ret == FALSE) { /* Lock fs failed */
        return -EBUSY;
W
wenjun 已提交
1503
    }
W
wangchenyang 已提交
1504 1505
    DEF_NAMBUF;
    INIT_NAMBUF(fs);
M
mucor 已提交
1506
    for (i = 0; i < idir->read_cnt; i++) {
1507 1508
        /* using dir_read_massive to promote performance */
        result = dir_read_massive(dp, 0);
W
wangchenyang 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
        if (result == FR_NO_FILE) {
            break;
        } else if (result != FR_OK) {
            goto ERROR_UNLOCK;
        }
        get_fileinfo(dp, &fno);
        /* 0x00 for end of directory and 0xFF for directory is empty */
        if (fno.fname[0] == 0x00 || fno.fname[0] == (TCHAR)0xFF) {
            break;
        }
W
wenjun 已提交
1519

W
wangchenyang 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
        dirp = &(idir->fd_dir[i]);
        if (fno.fattrib & AM_DIR) { /* is dir */
            dirp->d_type = DT_DIR;
        } else {
            dirp->d_type = DT_REG;
        }
        if (strncpy_s(dirp->d_name, sizeof(dirp->d_name), fno.fname, strlen(fno.fname)) != EOK) {
            result = FR_NOT_ENOUGH_CORE;
            goto ERROR_UNLOCK;
        }
        result = dir_next(dp, 0);
        if (result != FR_OK && result != FR_NO_FILE) {
            goto ERROR_UNLOCK;
        }
        idir->fd_position++;
        idir->fd_dir[i].d_off = idir->fd_position;
        idir->fd_dir[i].d_reclen = (uint16_t)sizeof(struct dirent);
W
wenjun 已提交
1537
    }
W
wangchenyang 已提交
1538 1539 1540 1541 1542 1543 1544 1545
    unlock_fs(fs, FR_OK);
    FREE_NAMBUF();
    return i;
ERROR_UNLOCK:
    unlock_fs(fs, result);
    FREE_NAMBUF();
    return -fatfs_2_vfs(result);
}
W
wenjun 已提交
1546

W
wangchenyang 已提交
1547 1548 1549 1550 1551 1552
int fatfs_rewinddir(struct Vnode *vp, struct fs_dirent_s *dir)
{
    DIR *dp = (DIR *)dir->u.fs_dir;
    FATFS *fs = dp->obj.fs;
    FRESULT result;
    int ret;
W
wenjun 已提交
1553

W
wangchenyang 已提交
1554 1555 1556
    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
W
wenjun 已提交
1557 1558
    }

W
wangchenyang 已提交
1559 1560 1561 1562
    result = dir_sdi(dp, 0);
    unlock_fs(fs, result);
    return -fatfs_2_vfs(result);
}
W
wenjun 已提交
1563

W
wangchenyang 已提交
1564 1565 1566 1567 1568 1569
int fatfs_closedir(struct Vnode *vp, struct fs_dirent_s *dir)
{
    DIR *dp = (DIR *)dir->u.fs_dir;
    free(dp);
    dir->u.fs_dir = NULL;
    return 0;
W
wenjun 已提交
1570 1571
}

W
wangchenyang 已提交
1572
static FRESULT rename_check(DIR *dp_new, FILINFO *finfo_new, DIR *dp_old, FILINFO *finfo_old)
W
wenjun 已提交
1573
{
W
wangchenyang 已提交
1574
    DIR dir_sub;
W
wenjun 已提交
1575
    FRESULT result;
W
wangchenyang 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
    if (finfo_new->fattrib & AM_ARC) { /* new path is file */
        if (finfo_old->fattrib & AM_DIR) { /* but old path is dir */
            return FR_NO_DIR;
        }
    } else if (finfo_new->fattrib & AM_DIR) { /* new path is dir */
        if (finfo_old->fattrib & AM_ARC) { /* old path is file */
            return FR_IS_DIR;
        }
        dir_sub.obj.fs = dp_old->obj.fs;
        dir_sub.obj.sclust = finfo_new->sclst;
        result = dir_sdi(&dir_sub, 0);
        if (result != FR_OK) {
            return result;
        }
        result = dir_read(&dir_sub, 0);
        if (result == FR_OK) { /* new path isn't empty file */
            return FR_NO_EMPTY_DIR;
        }
    } else { /* System file or volume label */
        return FR_DENIED;
W
wenjun 已提交
1596
    }
W
wangchenyang 已提交
1597 1598
    return FR_OK;
}
W
wenjun 已提交
1599

W
wangchenyang 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
int fatfs_rename(struct Vnode *old_vnode, struct Vnode *new_parent, const char *oldname, const char *newname)
{
    FATFS *fs = (FATFS *)(old_vnode->originMount->data);
    DIR_FILE *dfp_old = (DIR_FILE *)old_vnode->data;
    DIR *dp_old = &(dfp_old->f_dir);
    FILINFO *finfo_old = &(dfp_old->fno);
    DIR_FILE *dfp_new = NULL;
    DIR* dp_new = NULL;
    FILINFO* finfo_new = NULL;
    DWORD clust;
    FRESULT result;
    int ret;
W
wenjun 已提交
1612

W
wangchenyang 已提交
1613 1614 1615
    ret = lock_fs(fs);
    if (ret == FALSE) { /* Lock fs failed */
        return -EBUSY;
W
wenjun 已提交
1616 1617
    }

W
wangchenyang 已提交
1618 1619 1620 1621
    dfp_new = (DIR_FILE *)zalloc(sizeof(DIR_FILE));
    if (dfp_new == NULL) {
        result = FR_NOT_ENOUGH_CORE;
        goto ERROR_UNLOCK;
W
wenjun 已提交
1622
    }
W
wangchenyang 已提交
1623 1624
    dp_new = &(dfp_new->f_dir);
    finfo_new = &(dfp_new->fno);
W
wenjun 已提交
1625

W
wangchenyang 已提交
1626 1627
    dp_new->obj.sclust = ((DIR_FILE *)(new_parent->data))->fno.sclst;
    dp_new->obj.fs = fs;
W
wenjun 已提交
1628

W
wangchenyang 已提交
1629 1630 1631 1632
    /* Find new path */
    DEF_NAMBUF;
    INIT_NAMBUF(fs);
    result = create_name(dp_new, &newname);
W
wenjun 已提交
1633
    if (result != FR_OK) {
W
wangchenyang 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
        goto ERROR_FREE;
    }
    result = dir_find(dp_new);
    if (result == FR_OK) { /* new path name exist */
        get_fileinfo(dp_new, finfo_new);
        result = rename_check(dp_new, finfo_new, dp_old, finfo_old);
        if (result != FR_OK) {
            goto ERROR_FREE;
        }
        result = dir_remove(dp_old);
        if (result != FR_OK) {
            goto ERROR_FREE;
        }
        clust = finfo_new->sclst;
        if (clust != 0) { /* remove the new path cluster chain if exists */
            result = remove_chain(&(dp_new->obj), clust, 0);
            if (result != FR_OK) {
                goto ERROR_FREE;
            }
        }
    } else { /* new path name not exist */
        result = dir_remove(dp_old);
        if (result != FR_OK) {
            goto ERROR_FREE;
        }
        result = dir_register(dp_new);
        if (result != FR_OK) {
            goto ERROR_FREE;
        }
W
wenjun 已提交
1663 1664
    }

W
wangchenyang 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673
    /* update new dir entry with old info */
    result = update_dir(dp_new, finfo_old);
    if (result != FR_OK) {
        goto ERROR_FREE;
    }
    result = dir_read(dp_new, 0);
    if (result != FR_OK) {
        goto ERROR_FREE;
    }
C
chenjing 已提交
1674
    dp_new->blk_ofs = dir_ofs(dp_new);
W
wangchenyang 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
    get_fileinfo(dp_new, finfo_new);

    dfp_new->fno.fp_list.pstNext = dfp_old->fno.fp_list.pstNext;
    dfp_new->fno.fp_list.pstPrev = dfp_old->fno.fp_list.pstPrev;
    ret = memcpy_s(dfp_old, sizeof(DIR_FILE), dfp_new, sizeof(DIR_FILE));
    if (ret != 0) {
        result = FR_NOT_ENOUGH_CORE;
        goto ERROR_FREE;
    }
    free(dfp_new);
    unlock_fs(fs, FR_OK);
    FREE_NAMBUF();
    return fatfs_sync(old_vnode->originMount->mountFlags, fs);

ERROR_FREE:
    free(dfp_new);
ERROR_UNLOCK:
    unlock_fs(fs, result);
    FREE_NAMBUF();
    return -fatfs_2_vfs(result);
W
wenjun 已提交
1695 1696 1697
}


W
wangchenyang 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706
static int fatfs_erase(los_part *part, int option)
{
    int opt = option;
    if ((UINT)opt & FMT_ERASE) {
        opt = (UINT)opt & (~FMT_ERASE);
        if (EraseDiskByID(part->disk_id, part->sector_start, part->sector_count) != LOS_OK) {
            PRINTK("Disk erase error.\n");
            return -1;
        }
W
wenjun 已提交
1707 1708
    }

W
wangchenyang 已提交
1709 1710
    if (opt != FM_FAT && opt != FM_FAT32) {
        opt = FM_ANY;
W
wenjun 已提交
1711 1712
    }

W
wangchenyang 已提交
1713 1714 1715 1716 1717 1718 1719 1720
    return opt;
}

static int fatfs_set_part_info(los_part *part)
{
    los_disk *disk = NULL;
    char *buf = NULL;
    int ret;
W
wenjun 已提交
1721

W
wangchenyang 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
    /* If there is no MBR before, the partition info needs to be changed after mkfs */
    if (part->type != EMMC && part->part_no_mbr == 0) {
        disk = get_disk(part->disk_id);
        if (disk == NULL) {
            return -EIO;
        }
        buf = (char *)zalloc(disk->sector_size);
        if (buf == NULL) {
            return -ENOMEM;
        }
        (void)memset_s(buf, disk->sector_size, 0, disk->sector_size);
1733
        ret = los_disk_read(part->disk_id, buf, 0, 1, TRUE); /* TRUE when not reading large data */
W
wangchenyang 已提交
1734 1735 1736 1737 1738 1739 1740 1741
        if (ret < 0) {
            free(buf);
            return -EIO;
        }
        part->sector_start = LD_DWORD_DISK(&buf[PAR_OFFSET + PAR_START_OFFSET]);
        part->sector_count = LD_DWORD_DISK(&buf[PAR_OFFSET + PAR_COUNT_OFFSET]);
        part->part_no_mbr = 1;
        part->filesystem_type = buf[PAR_OFFSET + PAR_TYPE_OFFSET];
W
wenjun 已提交
1742

W
wangchenyang 已提交
1743
        free(buf);
W
wenjun 已提交
1744
    }
W
wangchenyang 已提交
1745
    return 0;
W
wenjun 已提交
1746 1747
}

W
wangchenyang 已提交
1748
int fatfs_mkfs (struct Vnode *device, int sectors, int option)
W
wenjun 已提交
1749
{
W
wangchenyang 已提交
1750 1751 1752 1753
    BYTE *work_buff = NULL;
    los_part *part = NULL;
    FRESULT result;
    int ret;
W
wenjun 已提交
1754

W
wangchenyang 已提交
1755 1756 1757 1758
    part = los_part_find(device);
    if (part == NULL || device->data == NULL) {
        return -ENODEV;
    }
W
wenjun 已提交
1759

Y
YOUR_NAME 已提交
1760
    if (sectors < 0 || sectors > FAT32_MAX_CLUSTER_SIZE || ((DWORD)sectors & ((DWORD)sectors - 1))) {
W
wenjun 已提交
1761 1762 1763
        return -EINVAL;
    }

W
wangchenyang 已提交
1764
    if (option != FMT_FAT && option != FMT_FAT32 && option != FMT_ANY && option != FMT_ERASE) {
W
wenjun 已提交
1765 1766 1767
        return -EINVAL;
    }

W
wangchenyang 已提交
1768 1769 1770 1771 1772 1773 1774
    if (part->part_name != NULL) { /* The part is mounted */
        return -EBUSY;
    }
    option = fatfs_erase(part, option);
    work_buff = (BYTE *)zalloc(FF_MAX_SS);
    if (work_buff == NULL) {
        return -ENOMEM;
W
wenjun 已提交
1775 1776
    }

W
wangchenyang 已提交
1777 1778 1779 1780
    result = _mkfs(part, sectors, option, work_buff, FF_MAX_SS);
    free(work_buff);
    if (result != FR_OK) {
        return -fatfs_2_vfs(result);
W
wenjun 已提交
1781 1782
    }

W
wangchenyang 已提交
1783 1784 1785 1786 1787
#ifdef LOSCFG_FS_FAT_CACHE
    ret = OsSdSync(part->disk_id);
    if (ret != 0) {
        return -EIO;
    }
W
wenjun 已提交
1788 1789
#endif

W
wangchenyang 已提交
1790 1791 1792
    ret = fatfs_set_part_info(part);
    if (ret != 0) {
        return -EIO;
W
wenjun 已提交
1793 1794
    }

W
wangchenyang 已提交
1795
    return 0;
W
wenjun 已提交
1796 1797
}

W
wangchenyang 已提交
1798
int fatfs_mkdir(struct Vnode *parent, const char *name, mode_t mode, struct Vnode **vpp)
W
wenjun 已提交
1799
{
C
chenjing 已提交
1800
    return fatfs_create_obj(parent, name, mode, vpp, AM_DIR, NULL);
W
wenjun 已提交
1801 1802
}

C
chenwei 已提交
1803
int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name)
W
wenjun 已提交
1804
{
W
wangchenyang 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
    FATFS *fs = (FATFS *)vp->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    FILINFO *finfo = &(dfp->fno);
    DIR *dp = &(dfp->f_dir);
    DIR dir_sub;
    FRESULT result = FR_OK;
    int ret;

    if (finfo->fattrib & AM_ARC) {
        result = FR_NO_DIR;
        goto ERROR_OUT;
    }

    DEF_NAMBUF;
    INIT_NAMBUF(fs);

    ret = lock_fs(fs);
    if (ret == FALSE) {
        result = FR_TIMEOUT;
        goto ERROR_OUT;
    }
    dir_sub.obj.fs = fs;
    dir_sub.obj.sclust = finfo->sclst;
    result = dir_sdi(&dir_sub, 0);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
W
wenjun 已提交
1831
    }
W
wangchenyang 已提交
1832 1833 1834 1835
    result = dir_read(&dir_sub, 0);
    if (result == FR_OK) {
        result = FR_NO_EMPTY_DIR;
        goto ERROR_UNLOCK;
W
wenjun 已提交
1836
    }
W
wangchenyang 已提交
1837 1838 1839
    result = dir_remove(dp); /* remove directory entry */
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
W
wenjun 已提交
1840
    }
W
wangchenyang 已提交
1841 1842 1843 1844
    /* Directory entry contains at least one cluster */
    result = remove_chain(&(dp->obj), finfo->sclst, 0);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
W
wenjun 已提交
1845 1846
    }

W
wangchenyang 已提交
1847 1848 1849
    unlock_fs(fs, FR_OK);
    FREE_NAMBUF();
    return fatfs_sync(vp->originMount->mountFlags, fs);
W
wenjun 已提交
1850

W
wangchenyang 已提交
1851 1852 1853 1854 1855 1856
ERROR_UNLOCK:
    unlock_fs(fs, result);
    FREE_NAMBUF();
ERROR_OUT:
    return -fatfs_2_vfs(result);
}
W
wenjun 已提交
1857

W
wangchenyang 已提交
1858 1859 1860 1861 1862 1863
int fatfs_reclaim(struct Vnode *vp)
{
    free(vp->data);
    vp->data = NULL;
    return 0;
}
W
wenjun 已提交
1864

C
chenwei 已提交
1865
int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name)
W
wangchenyang 已提交
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
{
    FATFS *fs = (FATFS *)vp->originMount->data;
    DIR_FILE *dfp = (DIR_FILE *)vp->data;
    FILINFO *finfo = &(dfp->fno);
    DIR *dp = &(dfp->f_dir);
    FRESULT result = FR_OK;
    int ret;

    if (finfo->fattrib & AM_DIR) {
        result = FR_IS_DIR;
        goto ERROR_OUT;
    }
    ret = lock_fs(fs);
    if (ret == FALSE) {
        result = FR_TIMEOUT;
        goto ERROR_OUT;
    }
    result = dir_remove(dp); /* remove directory entry */
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
W
wenjun 已提交
1886
    }
W
wangchenyang 已提交
1887 1888 1889 1890 1891
    if (finfo->sclst != 0) { /* if cluster chain exists */
        result = remove_chain(&(dp->obj), finfo->sclst, 0);
        if (result != FR_OK) {
            goto ERROR_UNLOCK;
        }
W
wenjun 已提交
1892
    }
W
wangchenyang 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
    result = sync_fs(fs);
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
    }
    unlock_fs(fs, FR_OK);
    return fatfs_sync(vp->originMount->mountFlags, fs);

ERROR_UNLOCK:
    unlock_fs(fs, result);
ERROR_OUT:
    return -fatfs_2_vfs(result);
}

int fatfs_ioctl(struct file *filep, int req, unsigned long arg)
{
W
wenjun 已提交
1908 1909 1910 1911
    return -ENOSYS;
}

#define CHECK_FILE_NUM 3
W
wangchenyang 已提交
1912
static inline DWORD combine_time(FILINFO *finfo)
W
wenjun 已提交
1913
{
W
wangchenyang 已提交
1914 1915
    return (finfo->fdate << FTIME_DATE_OFFSET) | finfo->ftime;
}
W
wenjun 已提交
1916

W
wangchenyang 已提交
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
static UINT get_oldest_time(DIR_FILE df[], DWORD *oldest_time, UINT len)
{
    int i;
    DWORD old_time = combine_time(&(df[0].fno));
    DWORD time;
    UINT index = 0;
    for (i = 1; i < len; i++) {
        time = combine_time(&(df[i].fno));
        if (time < old_time) {
            old_time = time;
            index = i;
        }
W
wenjun 已提交
1929
    }
W
wangchenyang 已提交
1930 1931
    *oldest_time = old_time;
    return index;
W
wenjun 已提交
1932 1933
}

W
wangchenyang 已提交
1934
int fatfs_fscheck(struct Vnode* vp, struct fs_dirent_s *dir)
W
wenjun 已提交
1935
{
W
wangchenyang 已提交
1936 1937 1938 1939
    FATFS *fs = (FATFS *)vp->originMount->data;
    DIR_FILE df[CHECK_FILE_NUM] = {0};
    DIR *dp = NULL;
    FILINFO *finfo = &(((DIR_FILE *)(vp->data))->fno);
W
wenjun 已提交
1940
    FILINFO fno;
W
wangchenyang 已提交
1941 1942 1943 1944
    DWORD old_time = -1;
    DWORD time;
    UINT count;
    UINT index = 0;
W
wenjun 已提交
1945
    los_part *part = NULL;
W
wangchenyang 已提交
1946 1947
    FRESULT result;
    int ret;
W
wenjun 已提交
1948

W
wangchenyang 已提交
1949 1950
    if (fs->fs_type != FS_FAT32) {
        return -EINVAL;
W
wenjun 已提交
1951 1952
    }

W
wangchenyang 已提交
1953 1954 1955
    if ((finfo->fattrib & AM_DIR) == 0) {
        return -ENOTDIR;
    }
W
wenjun 已提交
1956

W
wangchenyang 已提交
1957 1958
    ret = fatfs_opendir(vp, dir);
    if (ret < 0) {
W
wenjun 已提交
1959 1960 1961
        return ret;
    }

W
wangchenyang 已提交
1962 1963 1964 1965
    ret = lock_fs(fs);
    if (ret == FALSE) {
        result = FR_TIMEOUT;
        goto ERROR_WITH_DIR;
W
wenjun 已提交
1966 1967
    }

W
wangchenyang 已提交
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
    dp = (DIR *)dir->u.fs_dir;
    dp->obj.id = fs->id;
    for (count = 0; count < CHECK_FILE_NUM; count++) {
        if ((result = f_readdir(dp, &fno)) != FR_OK) {
            goto ERROR_UNLOCK;
        } else {
            if (fno.fname[0] == 0 || fno.fname[0] == (TCHAR)0xFF) {
                break;
            }
            (void)memcpy_s(&df[count].f_dir, sizeof(DIR), dp, sizeof(DIR));
            (void)memcpy_s(&df[count].fno, sizeof(FILINFO), &fno, sizeof(FILINFO));
            time = combine_time(&(df[count].fno));
            if (time < old_time) {
                old_time = time;
                index = count;
            }
W
wenjun 已提交
1984
        }
W
wangchenyang 已提交
1985 1986 1987
    }
    while ((result = f_readdir(dp, &fno)) == FR_OK) {
        if (fno.fname[0] == 0 || fno.fname[0] == (TCHAR)0xFF) {
W
wenjun 已提交
1988 1989
            break;
        }
W
wangchenyang 已提交
1990 1991 1992 1993 1994
        time = combine_time(&fno);
        if (time < old_time) {
            (void)memcpy_s(&df[index].f_dir, sizeof(DIR), dp, sizeof(DIR));
            (void)memcpy_s(&df[index].fno, sizeof(FILINFO), &fno, sizeof(FILINFO));
            index = get_oldest_time(df, &old_time, CHECK_FILE_NUM);
W
wenjun 已提交
1995
        }
W
wangchenyang 已提交
1996 1997 1998 1999
    }
    if (result != FR_OK) {
        goto ERROR_UNLOCK;
    }
W
wenjun 已提交
2000

W
wangchenyang 已提交
2001 2002 2003 2004
    for (index = 0; index < count; index++) {
        result = f_fcheckfat(&df[index]);
        if (result != FR_OK) {
            goto ERROR_UNLOCK;
W
wenjun 已提交
2005 2006 2007
        }
    }

W
wangchenyang 已提交
2008 2009 2010 2011
    unlock_fs(fs, FR_OK);

    ret = fatfs_closedir(vp, dir);
    if (ret < 0) {
W
wenjun 已提交
2012 2013 2014 2015
        return ret;
    }

#ifdef LOSCFG_FS_FAT_CACHE
W
wangchenyang 已提交
2016
    part = get_part((INT)fs->pdrv);
W
wenjun 已提交
2017 2018 2019 2020 2021 2022
    if (part != NULL) {
        (void)OsSdSync(part->disk_id);
    }
#endif

    return 0;
W
wangchenyang 已提交
2023 2024 2025 2026 2027 2028

ERROR_UNLOCK:
    unlock_fs(fs, result);
ERROR_WITH_DIR:
    fatfs_closedir(vp, dir);
    return -fatfs_2_vfs(result);
W
wenjun 已提交
2029 2030
}

C
chenjing 已提交
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
int fatfs_symlink(struct Vnode *parentVnode, struct Vnode **newVnode, const char *path, const char *target)
{
    return fatfs_create_obj(parentVnode, path, 0, newVnode, AM_LNK, target);
}

ssize_t fatfs_readlink(struct Vnode *vnode, char *buffer, size_t bufLen)
{
    int ret;
    FRESULT res = FR_OK;
    DWORD clust;
    QWORD sect;
    DIR_FILE *dfp = (DIR_FILE *)(vnode->data);
    DIR *dp = &(dfp->f_dir);
    FATFS *fs = dp->obj.fs;
    FILINFO *finfo = &(dfp->fno);
    size_t targetLen = finfo->fsize;
    size_t cnt;

    ret = lock_fs(fs);
    if (ret == FALSE) {
        return -EBUSY;
    }

    clust = finfo->sclst;
    sect = clst2sect(fs, clust);    /* Get current sector */
    if (sect == 0) {
        res = FR_DISK_ERR;
        goto ERROUT;
    }

    if (move_window(fs, sect) != FR_OK) {
        res = FR_DISK_ERR;
        goto ERROUT;
    }

    cnt = (bufLen - 1) < targetLen ? (bufLen - 1) : targetLen;
    ret = LOS_CopyFromKernel(buffer, bufLen, fs->win, cnt);
    if (ret != EOK) {
        res = FR_INVALID_PARAMETER;
        goto ERROUT;
    }
    buffer[cnt] = '\0';

    unlock_fs(fs, FR_OK);
    return cnt;

ERROUT:
    unlock_fs(fs, res);
    return -fatfs_2_vfs(res);
}

W
wangchenyang 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
struct VnodeOps fatfs_vops = {
    /* file ops */
    .Getattr = fatfs_stat,
    .Chattr = fatfs_chattr,
    .Lookup = fatfs_lookup,
    .Rename = fatfs_rename,
    .Create = fatfs_create,
    .Unlink = fatfs_unlink,
    .Reclaim = fatfs_reclaim,
    .Truncate = fatfs_truncate,
    .Truncate64 = fatfs_truncate64,
    /* dir ops */
    .Opendir = fatfs_opendir,
    .Readdir = fatfs_readdir,
    .Rewinddir = fatfs_rewinddir,
    .Closedir = fatfs_closedir,
    .Mkdir = fatfs_mkdir,
    .Rmdir = fatfs_rmdir,
    .Fscheck = fatfs_fscheck,
C
chenjing 已提交
2101 2102
    .Symlink = fatfs_symlink,
    .Readlink = fatfs_readlink,
W
wangchenyang 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
};

struct MountOps fatfs_mops = {
    .Mount = fatfs_mount,
    .Unmount = fatfs_umount,
    .Statfs = fatfs_statfs,
};

struct file_operations_vfs fatfs_fops = {
    .open = fatfs_open,
    .read = fatfs_read,
    .write = fatfs_write,
    .seek = fatfs_lseek,
    .close = fatfs_close,
    .mmap = OsVfsFileMmap,
    .fallocate = fatfs_fallocate,
    .fallocate64 = fatfs_fallocate64,
    .fsync = fatfs_fsync,
    .ioctl = fatfs_ioctl,
W
wenjun 已提交
2122 2123
};

W
wangchenyang 已提交
2124
FSMAP_ENTRY(fat_fsmap, "vfat", fatfs_mops, FALSE, TRUE);
W
wenjun 已提交
2125 2126

#endif /* LOSCFG_FS_FAT */