dfs_fs.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
+------------------------------------------------------------------------------
| Project   : Device Filesystem
+------------------------------------------------------------------------------
| Copyright 2004, 2005  www.fayfayspace.org.
| All rights reserved.
|------------------------------------------------------------------------------
| File      : dfs_fs.c, the filesystem related implementations of Device FileSystem
|------------------------------------------------------------------------------
| Chang Logs:
| Date           Author       Notes
wuyangyong's avatar
wuyangyong 已提交
12
| 2005-02-22     ffxz         The first version.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
+------------------------------------------------------------------------------
*/
#include <dfs_fs.h>
#include <dfs_raw.h>
#include <dfs_util.h>

#include <string.h>

/*
+------------------------------------------------------------------------------
| Function    : dfs_register
+------------------------------------------------------------------------------
| Description : registers a filesystem
|
| Parameters  : ops, the implementation of filesystem
| Returns     : the status of register
|               0 , successful
|               -1, failed
+------------------------------------------------------------------------------
*/
int dfs_register(struct dfs_filesystem_operation* ops)
{
wuyangyong's avatar
wuyangyong 已提交
35
    int index;
36

wuyangyong's avatar
wuyangyong 已提交
37
    rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
38

wuyangyong's avatar
wuyangyong 已提交
39 40 41 42 43 44 45
    /* check if this filesystem was already registered */
    for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
    {
        if (filesystem_operation_table[index] != RT_NULL &&
                strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
            goto err;
    }
46

wuyangyong's avatar
wuyangyong 已提交
47 48 49
    /* find out an empty filesystem type entry */
    for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL;
            index++) ;
50

wuyangyong's avatar
wuyangyong 已提交
51 52
    /* filesystem type table full */
    if (index == DFS_FILESYSTEM_TYPES_MAX) goto err;
53

wuyangyong's avatar
wuyangyong 已提交
54 55 56 57 58
    /* save the filesystem's operations */
    filesystem_operation_table[index] = ops;

    rt_sem_release(filesystem_operation_table_lock);
    return 0;
59 60

err:
wuyangyong's avatar
wuyangyong 已提交
61 62
    rt_sem_release(filesystem_operation_table_lock);
    return -1;
63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

/*
+------------------------------------------------------------------------------
| Function    : dfs_filesystem_lookup
+------------------------------------------------------------------------------
| Description : lookup the mounted filesystem on the specified path
|
| Parameters  : path, the specified path string
| Returns     : the found filesystem
+------------------------------------------------------------------------------
*/
struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
{
wuyangyong's avatar
wuyangyong 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    struct dfs_filesystem* fs;
    rt_uint32_t index, fspath, prefixlen;

    fs = RT_NULL;
    prefixlen = 0;

    /* lock filesystem table */
    rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);

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

        if ((filesystem_table[index].ops != RT_NULL) &&
                str_is_prefix(filesystem_table[index].path, path) == 0)
        {
            fs = &filesystem_table[index];
            prefixlen = fspath;
        }
    }

    rt_sem_release(filesystem_table_lock);
    return fs;
102 103 104 105 106 107 108
}

rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex)
{
#define DPT_ADDRESS		0x1be		/* device partition offset in Boot Sector */
#define DPT_ITEM_SIZE	16			/* partition item size */

wuyangyong's avatar
wuyangyong 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    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;
140 141

#ifdef RT_USING_FINSH
wuyangyong's avatar
wuyangyong 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        rt_kprintf("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 */
            {
                rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */
            }
            else
            {
                rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */
            }
        }
        else
        {
            rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */
        }
161
#endif
wuyangyong's avatar
wuyangyong 已提交
162 163 164 165 166
    }
    else
    {
        result = -RT_ERROR;
    }
167

wuyangyong's avatar
wuyangyong 已提交
168
    return result;
169 170 171 172 173 174 175 176 177
}

/*
+------------------------------------------------------------------------------
| Function    : dfs_mount
+------------------------------------------------------------------------------
| Description : mount a filesystem to specified path
|
| Parameters  : device_name, the implementation of filesystem
wuyangyong's avatar
wuyangyong 已提交
178
|               path,
179 180 181 182 183 184 185 186 187
|               filesystemtype,
|               rwflag,
|               data,
| Returns     : the status of register
|               0 , successful
|               -1, failed
+------------------------------------------------------------------------------
*/
int dfs_mount(const char* device_name, const char* path,
wuyangyong's avatar
wuyangyong 已提交
188 189
              const char* filesystemtype, unsigned long rwflag, const
              void* data)
190
{
wuyangyong's avatar
wuyangyong 已提交
191 192 193 194 195
    struct dfs_filesystem_operation* ops;
    struct dfs_filesystem* fs;
    char *fullpath=RT_NULL;
#ifdef RT_USING_WORKDIR
    char full_path[DFS_PATH_MAX + 1];
196
#endif
wuyangyong's avatar
wuyangyong 已提交
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
    rt_device_t dev_id;
    int index;

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

    /* find out specific filesystem */
    rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
    for ( index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++ )
    {
        if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)break;
    }

    /* can't find filesystem */
    if ( index == DFS_FILESYSTEM_TYPES_MAX )
    {
        rt_set_errno(-DFS_STATUS_ENODEV);
        rt_sem_release(filesystem_operation_table_lock);
        return -1;
    }
    ops = filesystem_operation_table[index];
    rt_sem_release(filesystem_operation_table_lock);

    /* make full path for special file */
    fullpath = (char*)path;
    if ( fullpath[0] != '/') /* not an abstract path */
    {
#ifdef RT_USING_WORKDIR
        /* build full path */
        fullpath = full_path;
        rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
        build_fullpath(working_directory, path, fullpath);
        rt_sem_release(working_directory_lock);
236
#else
wuyangyong's avatar
wuyangyong 已提交
237 238
        rt_set_errno(-DFS_STATUS_ENOTDIR);
        return -1;
239
#endif
wuyangyong's avatar
wuyangyong 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    }

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

        if ( dfile_raw_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0 )
        {
            rt_set_errno(-DFS_STATUS_ENOTDIR);
            return -1;
        }
        dfile_raw_close(&fd);
    }

    /* check whether the file system mounted or not */
    rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
    for (index =0; index < DFS_FILESYSTEMS_MAX; index++)
    {
        if ( filesystem_table[index].ops != RT_NULL &&
                strcmp(filesystem_table[index].path, path) == 0 )
        {
            rt_set_errno(-DFS_STATUS_EINVAL);
            goto err1;
        }
    }

    /* find out en empty filesystem table entry */
    for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL;
            index++) ;
    if ( index == DFS_FILESYSTEMS_MAX )	/* can't find en empty filesystem table entry */
    {
        rt_set_errno(-DFS_STATUS_EMMOUNT);
        goto err1;
    }

    /* register file system */
    fs = &(filesystem_table[index]);
    strncpy(fs->path, fullpath, strlen(fullpath));
    fs->ops = ops;
    fs->dev_id = dev_id;
    /* release filesystem_table lock */
    rt_sem_release(filesystem_table_lock);

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

    if ( ops->mount == RT_NULL ) /* there is no mount implementation */
    {
        rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);

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

        rt_sem_release(filesystem_table_lock);

        rt_set_errno(-DFS_STATUS_ENOSYS);
        return -1;
    }
    /* call mount of this filesystem */
    else if ( ops->mount(fs) < 0 )
    {
        /* mount failed */
        rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);

        /* close device */
        rt_device_close(fs->dev_id);

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

        rt_sem_release(filesystem_table_lock);
        return -1;
    }

    return 0;
316 317

err1:
wuyangyong's avatar
wuyangyong 已提交
318 319
    rt_sem_release(filesystem_table_lock);
    return -1;
320 321 322 323 324 325 326 327
}

/*
+------------------------------------------------------------------------------
| Function    : dfs_unmount
+------------------------------------------------------------------------------
| Description : unmount a filesystem
|
wuyangyong's avatar
wuyangyong 已提交
328
| Parameters  :
329 330 331 332 333 334 335
| Returns     : the status of register
|               0 , successful
|               -1, failed
+------------------------------------------------------------------------------
*/
int dfs_unmount(const char *specialfile)
{
wuyangyong's avatar
wuyangyong 已提交
336 337 338
    char *fullpath;
#ifdef RT_USING_WORKDIR
    char full_path[DFS_PATH_MAX + 1];
339
#endif
wuyangyong's avatar
wuyangyong 已提交
340 341 342 343 344 345 346 347 348 349 350
    struct dfs_filesystem* fs = RT_NULL;

    fullpath = (char*)specialfile;
    if ( fullpath[0] != '/')
    {
#ifdef RT_USING_WORKDIR
        /* build full path */
        fullpath = full_path;
        rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
        build_fullpath(working_directory, specialfile, fullpath);
        rt_sem_release(working_directory_lock);
351
#else
wuyangyong's avatar
wuyangyong 已提交
352 353
        rt_set_errno(-DFS_STATUS_ENOTDIR);
        return -1;
354
#endif
wuyangyong's avatar
wuyangyong 已提交
355
    }
356

wuyangyong's avatar
wuyangyong 已提交
357 358
    /* lock filesystem table */
    rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
359

wuyangyong's avatar
wuyangyong 已提交
360 361 362 363 364
    fs = dfs_filesystem_lookup(fullpath);
    if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
    {
        goto err1;
    }
365

wuyangyong's avatar
wuyangyong 已提交
366 367
    /* close device, but do not check the status of device */
    rt_device_close(fs->dev_id);
368

wuyangyong's avatar
wuyangyong 已提交
369 370
    /* clear this filesystem table entry */
    rt_memset(fs, 0, sizeof(struct dfs_filesystem));
371

wuyangyong's avatar
wuyangyong 已提交
372
    rt_sem_release(filesystem_table_lock);
373

wuyangyong's avatar
wuyangyong 已提交
374
    return 0;
375 376

err1:
wuyangyong's avatar
wuyangyong 已提交
377 378
    rt_sem_release(filesystem_table_lock);
    return -1;
379
}