dfs_fs.c 13.7 KB
Newer Older
1 2 3
/*
 * File      : dfs_fs.c
 * This file is part of Device File System in RT-Thread RTOS
D
dzzxzz@gmail.com 已提交
4
 * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
5
 *
Y
yiyue.fang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22 23 24 25
 *
 * Change Logs:
 * Date           Author       Notes
 * 2005-02-22     Bernard      The first version.
 * 2010-06-30     Bernard      Optimize for RT-Thread RTOS
 * 2011-03-12     Bernard      fix the filesystem lookup issue.
 */
26

27 28 29 30 31 32 33 34 35 36 37 38 39
#include <dfs_fs.h>
#include <dfs_file.h>

/**
 * @addtogroup FsApi
 */
/*@{*/

/**
 * this function will register a file system instance to device file system.
 *
 * @param ops the file system instance to be registered.
 *
D
dzzxzz@gmail.com 已提交
40
 * @return 0 on successful, -1 on failed.
41
 */
42
int dfs_register(const struct dfs_filesystem_operation *ops)
43
{
44
    int index, result;
G
geniusgogo 已提交
45
    int free_index;
46 47

    result = 0;
G
geniusgogo 已提交
48
    free_index = DFS_FILESYSTEM_TYPES_MAX;
49 50 51 52 53 54 55

    /* lock filesystem */
    dfs_lock();

    /* check if this filesystem was already registered */
    for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
    {
G
geniusgogo 已提交
56 57 58 59 60 61 62
        if (filesystem_operation_table[index] == RT_NULL)
        {
            /* find out an empty filesystem type entry */
            if (free_index == DFS_FILESYSTEM_TYPES_MAX)
                free_index = index;
        }
        else if (strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
63 64 65 66 67 68 69
        {
            result = -1;
            goto err;
        }
    }

    /* filesystem type table full */
G
geniusgogo 已提交
70
    if (free_index == DFS_FILESYSTEM_TYPES_MAX)
71 72 73 74 75 76
    {
        result = -1;
        goto err;
    }

    /* save the filesystem's operations */
G
geniusgogo 已提交
77
    filesystem_operation_table[free_index] = ops;
78 79

err:
80 81
    dfs_unlock();
    return result;
82 83 84 85 86 87 88 89 90 91
}

/**
 * this function will return the file system mounted on specified path.
 *
 * @param path the specified path string.
 *
 * @return the found file system or NULL if no file system mounted on
 * specified path
 */
92
struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
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
    struct dfs_filesystem *fs;
    rt_uint32_t index, fspath, prefixlen;

    fs = RT_NULL;
    prefixlen = 0;

    /* lock filesystem */
    dfs_lock();

    /* lookup it in the filesystem table */
    for (index = 0; index < DFS_FILESYSTEMS_MAX; index++)
    {
        if (filesystem_table[index].path == RT_NULL)
            continue;
        else
        {
            fspath = strlen(filesystem_table[index].path);
            if (fspath < prefixlen)
                continue;
        }

        if ((filesystem_table[index].ops != RT_NULL) &&
            (strncmp(filesystem_table[index].path, path, fspath) == 0))
        {
            /* check next path separator */
            if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
                continue;

            fs = &filesystem_table[index];
            prefixlen = fspath;
        }
    }

    dfs_unlock();

    return fs;
130 131 132 133 134 135 136 137 138 139 140
}

/**
 * this function will fetch the partition table on specified buffer.
 *
 * @param part the returned partition structure.
 * @param buf the buffer contains partition table.
 * @param pindex the index of partition table to fetch.
 *
 * @return RT_EOK on successful or -RT_ERROR on failed.
 */
141 142 143
rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part,
                                      rt_uint8_t           *buf,
                                      rt_uint32_t           pindex)
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
#define DPT_ADDRESS     0x1be       /* device partition offset in Boot Sector */
#define DPT_ITEM_SIZE   16          /* partition item size */

    rt_uint8_t *dpt;
    rt_uint8_t type;
    rt_err_t result;

    RT_ASSERT(part != RT_NULL);
    RT_ASSERT(buf != RT_NULL);

    result = RT_EOK;

    dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;

    if ((*dpt != 0x80) && (*dpt != 0x00))
    {
        /* which is not a partition table */
        result = -RT_ERROR;

        return result;
    }

    /* get partition type */
    type = *(dpt+4);

    if (type != 0)
    {
        /* set partition type */
        part->type = type;

        /* get partition offset and size */
        part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24;
        part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24;

        rt_kprintf("found part[%d], begin: %d, size: ",
                   pindex, part->offset*512);
        if ((part->size>>11) > 0) /* MB */
        {
            unsigned int part_size;
            part_size = part->size >> 11;/* MB */
            if ((part_size>>10) > 0) /* GB */
            {
                /* GB */
                rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");
            }
            else
            {
                /* MB */
                rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");
            }
        }
        else
        {
            /* KB */
            rt_kprintf("%d%s",part->size>>1,"KB\r\n");
        }
    }
    else
    {
        result = -RT_ERROR;
    }

    return result;
208 209 210 211 212 213 214 215 216 217 218 219 220
}

/**
 * this function will mount a file system on a specified path.
 *
 * @param device_name the name of device which includes a file system.
 * @param path the path to mount a file system
 * @param filesystemtype the file system type
 * @param rwflag the read/write etc. flag.
 * @param data the private data(parameter) for this file system.
 *
 * @return 0 on successful or -1 on failed.
 */
221 222 223 224 225
int dfs_mount(const char   *device_name,
              const char   *path,
              const char   *filesystemtype,
              unsigned long rwflag,
              const void   *data)
226
{
227 228 229 230
    const struct dfs_filesystem_operation *ops;
    struct dfs_filesystem *fs;
    char *fullpath=RT_NULL;
    rt_device_t dev_id;
G
geniusgogo 已提交
231
    int index, free_index;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

    /* open specific device */
    if (device_name != RT_NULL)
    {
        dev_id = rt_device_find(device_name);
        if (dev_id == RT_NULL)
        {
            /* no this device */
            rt_set_errno(-DFS_STATUS_ENODEV);

            return -1;
        }
    }
    else
    {
        /* which is a non-device filesystem mount */
        dev_id = RT_NULL;
    }

    /* find out specific filesystem */
    dfs_lock();
    for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
    {
G
geniusgogo 已提交
255 256 257
        if (filesystem_operation_table[index] == RT_NULL)
            continue;

258 259 260
        if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)
            break;
    }
G
geniusgogo 已提交
261
    dfs_unlock();
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

    /* can't find filesystem */
    if (index == DFS_FILESYSTEM_TYPES_MAX)
    {
        rt_set_errno(-DFS_STATUS_ENODEV);

        return -1;
    }
    ops = filesystem_operation_table[index];

    /* make full path for special file */
    fullpath = dfs_normalize_path(RT_NULL, path);
    if (fullpath == RT_NULL) /* not an abstract path */
    {
        rt_set_errno(-DFS_STATUS_ENOTDIR);

        return -1;
    }

    /* Check if the path exists or not, raw APIs call, fixme */
    if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
    {
        struct dfs_fd fd;

        if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0)
        {
            rt_free(fullpath);
            rt_set_errno(-DFS_STATUS_ENOTDIR);

            return -1;
        }
        dfs_file_close(&fd);
    }

G
geniusgogo 已提交
296
    free_index = DFS_FILESYSTEMS_MAX;
297 298 299 300
    /* check whether the file system mounted or not */
    dfs_lock();
    for (index = 0; index < DFS_FILESYSTEMS_MAX; index ++)
    {
G
geniusgogo 已提交
301 302 303 304 305 306 307
        if (filesystem_table[index].ops == RT_NULL)
        {
            /* find out an empty filesystem table entry */
            if (free_index == DFS_FILESYSTEMS_MAX)
                free_index = index;
        }
        else if (strcmp(filesystem_table[index].path, path) == 0)
308 309 310 311 312 313 314
        {
            rt_set_errno(-DFS_STATUS_EINVAL);
            goto err1;
        }
    }

    /* can't find en empty filesystem table entry */
G
geniusgogo 已提交
315
    if (free_index == DFS_FILESYSTEMS_MAX)
316 317 318 319 320 321
    {
        rt_set_errno(-DFS_STATUS_ENOSPC);
        goto err1;
    }

    /* register file system */
G
geniusgogo 已提交
322
    fs         = &(filesystem_table[free_index]);
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
    fs->path   = fullpath;
    fs->ops    = ops;
    fs->dev_id = dev_id;
    /* release filesystem_table lock */
    dfs_unlock();

    /* open device, but do not check the status of device */
    if (dev_id != RT_NULL)
        rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);

    /* there is no mount implementation */
    if (ops->mount == RT_NULL)
    {
        if (dev_id != RT_NULL)
            rt_device_close(dev_id);
        dfs_lock();
        /* clear filesystem table entry */
        rt_memset(fs, 0, sizeof(struct dfs_filesystem));
        dfs_unlock();

        rt_free(fullpath);
        rt_set_errno(-DFS_STATUS_ENOSYS);

        return -1;
    }
    /* call mount of this filesystem */
    else if (ops->mount(fs, rwflag, data) < 0)
    {
        /* close device */
        if (dev_id != RT_NULL)
            rt_device_close(fs->dev_id);

        /* mount failed */
        dfs_lock();
        /* clear filesystem table entry */
        rt_memset(fs, 0, sizeof(struct dfs_filesystem));
        dfs_unlock();

        rt_free(fullpath);

        return -1;
    }

    return 0;
367 368

err1:
369 370 371
    dfs_unlock();
    if (fullpath != RT_NULL)
        rt_free(fullpath);
372

373
    return -1;
374 375 376
}

/**
D
dzzxzz@gmail.com 已提交
377
 * this function will unmount a file system on specified path.
378 379 380 381 382 383 384
 *
 * @param specialfile the specified path which mounted a file system.
 *
 * @return 0 on successful or -1 on failed.
 */
int dfs_unmount(const char *specialfile)
{
385 386
    char *fullpath;
    struct dfs_filesystem *fs = RT_NULL;
387

388 389 390 391
    fullpath = dfs_normalize_path(RT_NULL, specialfile);
    if (fullpath == RT_NULL)
    {
        rt_set_errno(-DFS_STATUS_ENOTDIR);
392

393 394
        return -1;
    }
395

396 397
    /* lock filesystem */
    dfs_lock();
398

399 400 401 402 403 404 405
    fs = dfs_filesystem_lookup(fullpath);
    if (fs == RT_NULL ||
        fs->ops->unmount == RT_NULL ||
        fs->ops->unmount(fs) < 0)
    {
        goto err1;
    }
406

407 408 409
    /* close device, but do not check the status of device */
    if (fs->dev_id != RT_NULL)
        rt_device_close(fs->dev_id);
410

411 412
    if (fs->path != RT_NULL)
        rt_free(fs->path);
413

414 415
    /* clear this filesystem table entry */
    rt_memset(fs, 0, sizeof(struct dfs_filesystem));
416

417 418 419 420
    dfs_unlock();
    rt_free(fullpath);

    return 0;
421 422

err1:
423 424
    dfs_unlock();
    rt_free(fullpath);
425

426
    return -1;
427 428 429 430 431 432 433 434 435 436
}

/**
 * make a file system on the special device
 *
 * @param fs_name the file system name
 * @param device_name the special device name
 *
 * @return 0 on successful, otherwise failed.
 */
437
int dfs_mkfs(const char *fs_name, const char *device_name)
438
{
439
    int index;
440 441 442 443 444 445 446 447 448 449 450 451 452
    rt_device_t dev_id;

    /* check device name, and it should not be NULL */
    if (device_name == RT_NULL)
        dev_id = RT_NULL;
    else
        dev_id = rt_device_find(device_name);

    if (dev_id == RT_NULL)
    {
        rt_set_errno(-DFS_STATUS_ENODEV);
        return -1;
    }
453 454 455 456 457 458 459 460 461 462 463 464 465 466

    /* lock file system */
    dfs_lock();
    /* find the file system operations */
    for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++)
    {
        if (filesystem_operation_table[index] != RT_NULL &&
            strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
        {
            /* find file system operation */
            const struct dfs_filesystem_operation *ops = filesystem_operation_table[index];
            dfs_unlock();

            if (ops->mkfs != RT_NULL)
467
                return ops->mkfs(dev_id);
468 469 470 471 472 473 474 475

            break;
        }
    }
    dfs_unlock();

    rt_kprintf("Can not find the file system which named as %s.\n", fs_name);
    return -1;
476 477 478 479 480 481 482 483 484 485
}

/**
 * this function will return the information about a mounted file system.
 *
 * @param path the path which mounted file system.
 * @param buffer the buffer to save the returned information.
 *
 * @return 0 on successful, others on failed.
 */
486
int dfs_statfs(const char *path, struct statfs *buffer)
487
{
488
    struct dfs_filesystem *fs;
489

490 491 492 493 494 495
    fs = dfs_filesystem_lookup(path);
    if (fs != RT_NULL)
    {
        if (fs->ops->statfs != RT_NULL)
            return fs->ops->statfs(fs, buffer);
    }
496

497
    return -1;
498 499
}

B
bernard 已提交
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
#ifdef RT_USING_DFS_MNTTABLE
int dfs_mount_table(void)
{
	int index;

	while (1)
	{
		if (mount_table[index].path == RT_NULL) break;
		
		if (dfs_mount(mount_table[index].device_name,
				mount_table[index].path,
				mount_table[index].filesystemtype,
				mount_table[index].rwflag,
				mount_table[index].data) != 0)
		{
			rt_kprintf("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype, 
				mount_table[index].path);
			return -RT_ERROR;
		}

		index ++;
	}
	return 0;
}
INIT_ENV_EXPORT(dfs_mount_table);
#endif

527 528
#ifdef RT_USING_FINSH
#include <finsh.h>
529
void mkfs(const char *fs_name, const char *device_name)
530
{
531
    dfs_mkfs(fs_name, device_name);
532 533 534
}
FINSH_FUNCTION_EXPORT(mkfs, make a file system);

P
prife 已提交
535
int df(const char *path)
536
{
537
    int result;
P
prife 已提交
538
    long long cap;
539 540 541 542 543 544 545
    struct statfs buffer;

    if (path == RT_NULL)
        result = dfs_statfs("/", &buffer);
    else
        result = dfs_statfs(path, &buffer);

P
prife 已提交
546
    if (result != 0)
547
    {
P
prife 已提交
548 549
        rt_kprintf("dfs_statfs failed.\n");
        return -1;
550
    }
P
prife 已提交
551 552 553 554 555

    cap = buffer.f_bsize * buffer.f_bfree / 1024;
    rt_kprintf("disk free: %d KB [ %d block, %d bytes per block ]\n",
    (unsigned long)cap, buffer.f_bfree, buffer.f_bsize);
    return 0;
556 557 558 559 560
}
FINSH_FUNCTION_EXPORT(df, get disk free);
#endif

/* @} */