dfs_nfs.c 27.7 KB
Newer Older
1 2 3 4 5
/*
 * File      : dfs_nfs.c
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
 *
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
 *
 * Change Logs:
 * Date           Author       Notes
 */
 
24 25 26 27 28 29 30 31 32 33
#include <stdio.h>
#include <rtthread.h>
#include <dfs_fs.h>
#include <dfs_def.h>

#include <rpc/rpc.h>

#include "mount.h"
#include "nfs.h"

Y
yiyue.fang 已提交
34
#define NAME_MAX    64
35 36 37 38 39
#define DFS_NFS_MAX_MTU  1024

#ifdef _WIN32
#define strtok_r strtok_s
#endif
40 41 42

struct nfs_file
{
Y
yiyue.fang 已提交
43 44
    nfs_fh3 handle;     /* handle */
    size_t offset;      /* current offset */
45

Y
yiyue.fang 已提交
46 47
    size_t size;        /* total size */
    bool_t eof;         /* end of file */
48 49 50 51
};

struct nfs_dir
{
Y
yiyue.fang 已提交
52 53 54 55 56 57
    nfs_fh3 handle;
    cookie3 cookie;
    cookieverf3 cookieverf;
    entry3 *entry;
    bool_t eof;
    READDIR3res res;
58 59
};

Y
yiyue.fang 已提交
60 61
#define HOST_LENGTH         32
#define EXPORT_PATH_LENGTH  32
62 63
struct nfs_filesystem
{
Y
yiyue.fang 已提交
64 65 66 67
    nfs_fh3 root_handle;
    nfs_fh3 current_handle;
    CLIENT *nfs_client;
    CLIENT *mount_client;
68

Y
yiyue.fang 已提交
69 70
    char host[HOST_LENGTH];
    char export[EXPORT_PATH_LENGTH];
71 72 73
};
typedef struct nfs_file nfs_file;
typedef struct nfs_dir nfs_dir;
74
nfs_dir *nfs_opendir(struct nfs_filesystem *nfs, const char *path);
75

76
static int nfs_parse_host_export(const char *host_export, 
Y
yiyue.fang 已提交
77 78 79 80
                                 char       *host,
                                 size_t      host_len, 
                                 char       *export,
                                 size_t      export_len)
81
{
Y
yiyue.fang 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    int index;

    for (index = 0; index < host_len; index ++)
    {
        /* it's end of string, failed */
        if (host_export[index] == 0)
            return -1;

        /* copy to host buffer */
        if (host_export[index] != ':')
            host[index] = host_export[index];
        else
            break;
    }

    /* host buffer is not enough, failed */
    if (index == host_len)
        return -1;

    /* make RT_NULL */
    host_len = index;
    host[host_len] = '\0';

    host_len ++;

    /* copy export path */
    for (index = host_len; index < host_len + export_len; index ++)
    {
        if (host_export[index] == 0) 
        {
            export[index - host_len] = '\0';

            return 0;
        }

        export[index - host_len] = host_export[index];
    }

    return -1;
121 122 123 124
}

static void copy_handle(nfs_fh3 *dest, const nfs_fh3 *source)
{
Y
yiyue.fang 已提交
125 126 127 128 129 130 131 132 133 134
    dest->data.data_len = source->data.data_len;
    dest->data.data_val = rt_malloc(dest->data.data_len);
    if (dest->data.data_val == RT_NULL)
    {
        dest->data.data_len = 0;

        return;
    }

    memcpy(dest->data.data_val, source->data.data_val, dest->data.data_len);
135 136
}

137
static nfs_fh3 *get_handle(struct nfs_filesystem *nfs, const char *name)
138
{
Y
yiyue.fang 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    nfs_fh3 *handle = RT_NULL;
    char *file;
    char *path;
    char *init;

    init = path = rt_malloc(strlen(name)+1);
    if (init == RT_NULL)
        return RT_NULL;

    memcpy(init, name, strlen(name)+1);

    handle = rt_malloc(sizeof(nfs_fh3));
    if (handle == RT_NULL)
    {
        rt_free(init);

        return RT_NULL;
    }

    if (path[0] == '/')
    {
        path ++;
        copy_handle(handle, &nfs->root_handle);
    }
    else
    {
        copy_handle(handle, &nfs->current_handle);
    }

    while ((file = strtok_r(RT_NULL, "/", &path)) != RT_NULL)
    {
        LOOKUP3args args;
        LOOKUP3res res;
        memset(&res, 0, sizeof(res));
        copy_handle(&args.what.dir, handle);
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
        args.what.name = file;

        if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
        {
            rt_kprintf("Lookup failed\n");
            rt_free(init);
            rt_free(handle);
            xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);

            return RT_NULL;
        }
        else if (res.status != NFS3_OK)
        {
            rt_kprintf("Lookup failed: %d\n", res.status);
            rt_free(init);
            rt_free(handle);
            xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
            xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);

            return RT_NULL;
        }
        copy_handle(handle, &res.LOOKUP3res_u.resok.object);
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
        xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
    }

    rt_free(init);

    return handle;
204 205
}

206
static nfs_fh3 *get_dir_handle(struct nfs_filesystem *nfs, const char *name)
207
{
Y
yiyue.fang 已提交
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
    nfs_fh3 *handle = RT_NULL;
    char *file;
    char *path;
    char *init;

    init = path = rt_malloc(strlen(name)+1);
    if (init == RT_NULL)
        return RT_NULL;
    memcpy(init, name, strlen(name)+1);

    handle = rt_malloc(sizeof(nfs_fh3));
    if (handle == RT_NULL)
    {
        rt_free(init);

        return RT_NULL;
    }

    if (path[0] == '/')
    {
        path ++;
        copy_handle(handle, &nfs->root_handle);
    }
    else
    {
        copy_handle(handle, &nfs->current_handle);
    }

P
prife 已提交
236
    while ((file = strtok_r(RT_NULL, "/", &path)) != RT_NULL && path[0] != 0)
Y
yiyue.fang 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    {
        LOOKUP3args args;
        LOOKUP3res res;
        memset(&res, 0, sizeof(res));
        copy_handle(&args.what.dir, handle);
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
        args.what.name = file;

        if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
        {
            rt_kprintf("Lookup failed\n");
            rt_free(init);
            rt_free(handle);
            xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);

            return RT_NULL;
        }
        else if (res.status != NFS3_OK)
        {
            rt_kprintf("Lookup failed: %d\n", res.status);
            rt_free(init);
            rt_free(handle);
            xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
            xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);

            return RT_NULL;
        }
        copy_handle(handle, &res.LOOKUP3res_u.resok.object);
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
        xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
    }

    rt_free(init);

    return handle;
272 273
}

274
static size_t nfs_get_filesize(struct nfs_filesystem *nfs, nfs_fh3 *handle)
275
{
Y
yiyue.fang 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    GETATTR3args args;
    GETATTR3res res;
    fattr3 *info;
    size_t size;

    args.object = *handle;

    memset(&res, '\0', sizeof(res));

    if ((nfsproc3_getattr_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS) ||
        res.status != NFS3_OK)
    {
        rt_kprintf("GetAttr failed: %d\n", res.status);

        return 0;
    }

    info = &res.GETATTR3res_u.resok.obj_attributes;
    size = info->size;
    xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
    
    return size;
298 299
}

300
rt_bool_t nfs_is_directory(struct nfs_filesystem *nfs, const char *name)
B
bernard.xiong@gmail.com 已提交
301
{
Y
yiyue.fang 已提交
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
    GETATTR3args args;
    GETATTR3res res;
    fattr3 *info;
    nfs_fh3 *handle;
    rt_bool_t result;

    result = RT_FALSE;
    handle = get_handle(nfs, name);
    if (handle == RT_NULL)
        return RT_FALSE;

    args.object = *handle;

    memset(&res, '\0', sizeof(res));

    if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
    {
        rt_kprintf("GetAttr failed\n");

        return RT_FALSE;
    }
    else if (res.status != NFS3_OK)
    {
        rt_kprintf("Getattr failed: %d\n", res.status);

        return RT_FALSE;
    }

    info=&res.GETATTR3res_u.resok.obj_attributes;

    if (info->type == NFS3DIR)
        result = RT_TRUE;

    xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
    xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
    rt_free(handle);
    
    return result;
B
bernard.xiong@gmail.com 已提交
340 341
}

342
int nfs_create(struct nfs_filesystem *nfs, const char *name, mode_t mode)
343
{
Y
yiyue.fang 已提交
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
    CREATE3args args;
    CREATE3res res;
    int ret = 0;
    nfs_fh3 *handle;

    if (nfs->nfs_client == RT_NULL)
    {
        return -1;
    }

    handle = get_dir_handle(nfs, name);
    if (handle == RT_NULL)
    {
        return -1;
    }
    args.where.dir = *handle;
    args.where.name = strrchr(name, '/') + 1;
    if (args.where.name == RT_NULL)
    {
        args.where.name = (char *)name;
    }
    args.how.mode = GUARDED;

    args.how.createhow3_u.obj_attributes.mode.set_it = TRUE;
    args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = mode;
    args.how.createhow3_u.obj_attributes.uid.set_it = FALSE;
    args.how.createhow3_u.obj_attributes.gid.set_it = FALSE;
    args.how.createhow3_u.obj_attributes.size.set_it = FALSE;
    args.how.createhow3_u.obj_attributes.atime.set_it = DONT_CHANGE;
    args.how.createhow3_u.obj_attributes.mtime.set_it = DONT_CHANGE;

    memset(&res, 0, sizeof(res));

    if (nfsproc3_create_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
    {
        rt_kprintf("Create failed\n");
        ret = -1;
    }
    else if (res.status != NFS3_OK)
    {
        rt_kprintf("Create failed: %d\n", res.status);
        ret = -1;
    }
    xdr_free((xdrproc_t)xdr_CREATE3res, (char *)&res);
    xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
    rt_free(handle);

    return ret;
392 393
}

394
int nfs_mkdir(struct nfs_filesystem *nfs, const char *name, mode_t mode)
395
{
Y
yiyue.fang 已提交
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
    MKDIR3args args;
    MKDIR3res res;
    int ret = 0;
    nfs_fh3 *handle;

    if (nfs->nfs_client == RT_NULL)
        return -1;

    handle = get_dir_handle(nfs, name);
    if (handle == RT_NULL)
        return -1;

    args.where.dir = *handle;
    args.where.name = strrchr(name, '/') + 1;
    if (args.where.name == RT_NULL)
    {
        args.where.name = (char *)name;
    }

    args.attributes.mode.set_it = TRUE;
    args.attributes.mode.set_mode3_u.mode = mode;
    args.attributes.uid.set_it = FALSE;
    args.attributes.gid.set_it = FALSE;
    args.attributes.size.set_it = FALSE;
    args.attributes.atime.set_it = DONT_CHANGE;
    args.attributes.mtime.set_it = DONT_CHANGE;

    memset(&res, 0, sizeof(res));

    if (nfsproc3_mkdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
    {
        rt_kprintf("Mkdir failed\n");
        ret = -1;
    }
    else if (res.status != NFS3_OK)
    {
        rt_kprintf("Mkdir failed: %d\n", res.status);
        ret = -1;
    }
    xdr_free((xdrproc_t)xdr_MKDIR3res, (char *)&res);
    xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
    rt_free(handle);
    
    return ret;
440 441 442
}

/* mount(RT_NULL, "/mnt", "nfs", 0, "192.168.1.1:/export") */
443
int nfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
444
{
Y
yiyue.fang 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    mountres3 res;
    struct nfs_filesystem *nfs;

    nfs = (struct nfs_filesystem *)rt_malloc(sizeof(struct nfs_filesystem));
    memset(nfs, 0, sizeof(struct nfs_filesystem));

    if (nfs_parse_host_export((const char *)data, nfs->host, HOST_LENGTH,
        nfs->export, EXPORT_PATH_LENGTH) < 0)
    {
        rt_kprintf("host or export path error\n");
        goto __return;
    }

    nfs->mount_client=clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp");
    if (nfs->mount_client == RT_NULL)
    {
        rt_kprintf("create mount client failed\n");
        goto __return;
    }

    memset(&res, '\0', sizeof(mountres3));
    if (mountproc3_mnt_3((char *)nfs->export, &res, nfs->mount_client) != RPC_SUCCESS)
    {
        rt_kprintf("nfs mount failed\n");
        goto __return;
    }
    else if (res.fhs_status != MNT3_OK)
    {
        rt_kprintf("nfs mount failed\n");
        goto __return;
    }
    nfs->nfs_client=clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp");
    if (nfs->nfs_client == RT_NULL)
    {
        rt_kprintf("creat nfs client failed\n");
        goto __return;
    }
    copy_handle(&nfs->root_handle, (nfs_fh3 *)&res.mountres3_u.mountinfo.fhandle);
    copy_handle(&nfs->current_handle, &nfs->root_handle);

    nfs->nfs_client->cl_auth = authnone_create();
    fs->data = nfs;

    return 0;
489 490

__return:
Y
yiyue.fang 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    if (nfs != RT_NULL)
    {
        if (nfs->mount_client != RT_NULL)
        {
            clnt_destroy(nfs->mount_client);
        }
        if (nfs->nfs_client != RT_NULL)
        {
            if (nfs->nfs_client->cl_auth != RT_NULL)
            {
                auth_destroy(nfs->nfs_client->cl_auth);
            }
            clnt_destroy(nfs->nfs_client);
        }
        rt_free(nfs);
    }

    return -1;
509 510
}

511
int nfs_unmount(struct dfs_filesystem *fs)
512
{
Y
yiyue.fang 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    struct nfs_filesystem *nfs;

    RT_ASSERT(fs != RT_NULL);
    RT_ASSERT(fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)fs->data;

    if (nfs->mount_client != RT_NULL && 
        mountproc3_umnt_3((char *)nfs->export, RT_NULL, nfs->mount_client) != RPC_SUCCESS)
    {
        rt_kprintf("umount failed\n");

        return -1;
    }

    /* destroy nfs client */
    if (nfs->nfs_client != RT_NULL)
    {
        if (nfs->nfs_client->cl_auth != RT_NULL)
        {
            auth_destroy(nfs->nfs_client->cl_auth);
            nfs->nfs_client->cl_auth = RT_NULL;
        }
        clnt_destroy(nfs->nfs_client);
        nfs->nfs_client = RT_NULL;
    }

    /* destroy mount client */
    if (nfs->mount_client != RT_NULL)
    {
        if (nfs->mount_client->cl_auth != RT_NULL)
        {
            auth_destroy(nfs->mount_client->cl_auth);
            nfs->mount_client->cl_auth = RT_NULL;
        }
        clnt_destroy(nfs->mount_client);
        nfs->mount_client = RT_NULL;
    }

    rt_free(nfs);
    fs->data = RT_NULL;

    return 0;
555 556
}

Y
yiyue.fang 已提交
557
int nfs_ioctl(struct dfs_fd *file, int cmd, void *args)
558
{
Y
yiyue.fang 已提交
559
    return -DFS_STATUS_ENOSYS;
560 561
}

562
int nfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
563
{
Y
yiyue.fang 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    READ3args args;
    READ3res res;
    ssize_t bytes, total=0;
    nfs_file *fd;
    struct nfs_filesystem *nfs;

    if (file->type == FT_DIRECTORY)
        return -DFS_STATUS_EISDIR;

    fd = (nfs_file *)(file->data);
    RT_ASSERT(fd != RT_NULL);
    RT_ASSERT(file->fs != RT_NULL);
    RT_ASSERT(file->fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)file->fs->data;

    if (nfs->nfs_client == RT_NULL)
        return -1;

    /* end of file */
    if (fd->eof == TRUE)
        return 0;

    args.file = fd->handle;
    do {
        args.offset = fd->offset;
        args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
        count -= args.count;

        memset(&res, 0, sizeof(res));
        if (nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
        {
            rt_kprintf("Read failed\n");
            total = 0;
            break;
        }
        else if (res.status != NFS3_OK)
        {
            rt_kprintf("Read failed: %d\n", res.status);
            total = 0;
            break;
        }
        else
        {
            bytes = res.READ3res_u.resok.count;
            total += bytes;
            fd->offset += bytes;
            /* update current position */
            file->pos = fd->offset;
            memcpy(buf, res.READ3res_u.resok.data.data_val, bytes);
            buf = (void *)((char *)buf + args.count);
            if (res.READ3res_u.resok.eof)
            {
                /* something should probably be here */
                fd->eof = TRUE;
                break;
            }
        }
        xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
    } while(count > 0);

    xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);

    return total;
627 628
}

629
int nfs_write(struct dfs_fd *file, const void *buf, rt_size_t count)
630
{
Y
yiyue.fang 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    WRITE3args args;
    WRITE3res res;
    ssize_t bytes, total=0;
    nfs_file *fd;
    struct nfs_filesystem *nfs;

    if (file->type == FT_DIRECTORY)
        return -DFS_STATUS_EISDIR;

    fd = (nfs_file *)(file->data);
    RT_ASSERT(fd != RT_NULL);
    RT_ASSERT(file->fs != RT_NULL);
    RT_ASSERT(file->fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)file->fs->data;

    if (nfs->nfs_client == RT_NULL)
        return -1;

    args.file = fd->handle;
    args.stable = FILE_SYNC;

    do {
        args.offset = fd->offset;

        memset(&res, 0, sizeof(res));
        args.data.data_val=(void *)buf;
        args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
        args.data.data_len = args.count;
        count -= args.count;
        buf = (const void *)((char *)buf + args.count);

        if (nfsproc3_write_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
        {
            rt_kprintf("Write failed\n");
            total = 0;
            break;
        }
        else if (res.status != NFS3_OK)
        {
            rt_kprintf("Write failed: %d\n", res.status);
            total = 0;
            break;
        }
        else
        {
            bytes = res.WRITE3res_u.resok.count;
            fd->offset += bytes;
            total += bytes;
            /* update current position */
            file->pos = fd->offset;
B
bernard 已提交
681 682 683
            /* update file size */
			if (fd->size < fd->offset) fd->size = fd->offset;
			file->size = fd->size;
Y
yiyue.fang 已提交
684 685 686 687 688 689 690
        }
        xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
    } while (count > 0);

    xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);

    return total;
691 692
}

693
int nfs_lseek(struct dfs_fd *file, rt_off_t offset)
694
{
Y
yiyue.fang 已提交
695
    nfs_file *fd;
696

Y
yiyue.fang 已提交
697 698
    if (file->type == FT_DIRECTORY)
        return -DFS_STATUS_EISDIR;
699

Y
yiyue.fang 已提交
700 701
    fd = (nfs_file *)(file->data);
    RT_ASSERT(fd != RT_NULL);
702

Y
yiyue.fang 已提交
703 704 705
    if (offset <= fd->size)
    {
        fd->offset = offset;
706

Y
yiyue.fang 已提交
707 708 709 710
        return offset;
    }

    return -DFS_STATUS_EIO;
711 712
}

713
int nfs_close(struct dfs_fd *file)
714
{
Y
yiyue.fang 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    if (file->type == FT_DIRECTORY)
    {
        struct nfs_dir *dir;

        dir = (struct nfs_dir *)file->data;
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
        xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
        rt_free(dir);
    }
    else if (file->type == FT_REGULAR)
    {
        struct nfs_file *fd;

        fd = (struct nfs_file *)file->data;

        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
        rt_free(fd);
    }

    file->data = RT_NULL;
    return 0;
736 737
}

738
int nfs_open(struct dfs_fd *file)
739
{
Y
yiyue.fang 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752
    struct nfs_filesystem *nfs;

    RT_ASSERT(file->fs != RT_NULL);
    RT_ASSERT(file->fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)file->fs->data;

    if (file->flags & DFS_O_DIRECTORY)
    {
        nfs_dir *dir;
    
        if (file->flags & DFS_O_CREAT)
        {
            if (nfs_mkdir(nfs, file->path, 0755) < 0)
B
bernard 已提交
753
                return -DFS_STATUS_EAGAIN;
Y
yiyue.fang 已提交
754 755 756 757
        }

        /* open directory */
        dir = nfs_opendir(nfs, file->path);
B
bernard 已提交
758
        if (dir == RT_NULL) return -DFS_STATUS_ENOENT;
Y
yiyue.fang 已提交
759 760 761 762 763 764 765 766 767 768 769
        file->data = dir;
    }
    else
    {
        nfs_file *fp;
        nfs_fh3 *handle;

        /* create file */
        if (file->flags & DFS_O_CREAT)
        {
            if (nfs_create(nfs, file->path, 0664) < 0)
B
bernard 已提交
770
                return -DFS_STATUS_EAGAIN;
Y
yiyue.fang 已提交
771 772 773 774 775
        }

        /* open file (get file handle ) */
        fp = rt_malloc(sizeof(nfs_file));
        if (fp == RT_NULL)
B
bernard 已提交
776
            return -DFS_STATUS_ENOMEM;
Y
yiyue.fang 已提交
777 778 779 780 781 782

        handle = get_handle(nfs, file->path);
        if (handle == RT_NULL)
        {
            rt_free(fp);

B
bernard 已提交
783
            return -DFS_STATUS_ENOENT;
Y
yiyue.fang 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
        }

        /* get size of file */
        fp->size = nfs_get_filesize(nfs, handle);
        fp->offset = 0;
        fp->eof = FALSE;

        copy_handle(&fp->handle, handle);
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
        rt_free(handle);

        if (file->flags & DFS_O_APPEND)
        {
            fp->offset = fp->size;
        }

        /* set private file */
        file->data = fp;
B
bernard 已提交
802
        file->size = fp->size;
Y
yiyue.fang 已提交
803 804 805
    }

    return 0;
806 807
}

808
int nfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
809
{
Y
yiyue.fang 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    GETATTR3args args;
    GETATTR3res res;
    fattr3 *info;
    nfs_fh3 *handle;
    struct nfs_filesystem *nfs;

    RT_ASSERT(fs != RT_NULL);
    RT_ASSERT(fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)fs->data;

    handle = get_handle(nfs, path);
    if (handle == RT_NULL)
        return -1;

    args.object = *handle;

    memset(&res, '\0', sizeof(res));

    if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
    {
        rt_kprintf("GetAttr failed\n");
        return -1;
    }
    else if (res.status != NFS3_OK)
    {
        rt_kprintf("Getattr failed: %d\n", res.status);
        return -1;
    }

    info = &res.GETATTR3res_u.resok.obj_attributes;

    st->st_dev = 0;

    st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
    DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
    if (info->type == NFS3DIR)
    {
        st->st_mode &= ~DFS_S_IFREG;
        st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
    }

    st->st_size  = info->size;
    st->st_mtime = info->mtime.seconds;
    st->st_blksize = 512;

    xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
    xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
    rt_free(handle);

    return 0;
860 861
}

862
nfs_dir *nfs_opendir(struct nfs_filesystem *nfs, const char *path)
863
{
Y
yiyue.fang 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
    nfs_dir *dir;
    nfs_fh3 *handle;

    dir = rt_malloc(sizeof(nfs_dir));
    if (dir == RT_NULL)
    {
        return RT_NULL;
    }

    handle = get_handle(nfs, path);
    if (handle == RT_NULL)
    {
        rt_free(dir);

        return RT_NULL;
    }

    copy_handle(&dir->handle, handle);
    xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
    rt_free(handle);

    dir->cookie = 0;
    memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
    dir->entry = RT_NULL;
    dir->eof = FALSE;
    memset(&dir->res, '\0', sizeof(dir->res));

    return dir;
892 893
}

894
char *nfs_readdir(struct nfs_filesystem *nfs, nfs_dir *dir)
895
{
Y
yiyue.fang 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    static char name[NAME_MAX];

    if (nfs->nfs_client == RT_NULL || dir == RT_NULL)
        return RT_NULL;

    if (dir->entry == RT_NULL)
    {
        READDIR3args args;

        xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
        memset(&dir->res, '\0', sizeof(dir->res));

        args.dir = dir->handle;
        args.cookie = dir->cookie;
        memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
        args.count = 1024;

        if (nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client) != RPC_SUCCESS)
        {
            rt_kprintf("Readdir failed\n");

            return RT_NULL;
        }
        else if (dir->res.status != NFS3_OK)
        {
            rt_kprintf("Readdir failed: %d\n", dir->res.status);

            return RT_NULL;
        }

        memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
        dir->eof = dir->res.READDIR3res_u.resok.reply.eof;
        dir->entry = dir->res.READDIR3res_u.resok.reply.entries;
    }
    if (dir->eof == TRUE && dir->entry == RT_NULL)
        return RT_NULL;

    dir->cookie = dir->entry->cookie;
    strncpy(name, dir->entry->name, NAME_MAX-1);
    dir->entry = dir->entry->nextentry;
    name[NAME_MAX - 1] = '\0';

    return name;
939 940
}

941
int nfs_unlink(struct dfs_filesystem *fs, const char *path)
942
{
Y
yiyue.fang 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    int ret = 0;
    struct nfs_filesystem *nfs;

    RT_ASSERT(fs != RT_NULL);
    RT_ASSERT(fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)fs->data;

    if (nfs_is_directory(nfs, path) == RT_FALSE)
    {
        /* remove file */
        REMOVE3args args;
        REMOVE3res res;
        nfs_fh3 *handle;

        handle = get_dir_handle(nfs, path);
        if (handle == RT_NULL)
            return -1;

        args.object.dir = *handle;
        args.object.name = strrchr(path, '/') + 1;
        if (args.object.name == RT_NULL)
        {
            args.object.name = (char *)path;
        }

        memset(&res, 0, sizeof(res));

        if (nfsproc3_remove_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
        {
            rt_kprintf("Remove failed\n");
            ret = -1;
        }
        else if (res.status != NFS3_OK)
        {
            rt_kprintf("Remove failed: %d\n", res.status);
            ret = -1;
        }
        xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
        rt_free(handle);
    }
    else
    {
        /* remove directory */
        RMDIR3args args;
        RMDIR3res res;
        nfs_fh3 *handle;

        handle = get_dir_handle(nfs, path);
        if (handle == RT_NULL)
            return -1;

        args.object.dir = *handle;
        args.object.name = strrchr(path, '/') + 1;
        if (args.object.name == RT_NULL)
        {
            args.object.name = (char *)path;
        }

        memset(&res, 0, sizeof(res));

        if (nfsproc3_rmdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
        {
            rt_kprintf("Rmdir failed\n");
            ret = -1;
        }
        else if (res.status != NFS3_OK)
        {
            rt_kprintf("Rmdir failed: %d\n", res.status);
            ret = -1;
        }

        xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
        xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
        rt_free(handle);    
    }

    return ret;
1021 1022
}

1023
int nfs_rename(struct dfs_filesystem *fs, const char *src, const char *dest)
1024
{
Y
yiyue.fang 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    RENAME3args args;
    RENAME3res res;
    nfs_fh3 *sHandle;
    nfs_fh3 *dHandle;
    int ret = 0;
    struct nfs_filesystem *nfs;

    RT_ASSERT(fs != RT_NULL);
    RT_ASSERT(fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)fs->data;

    if (nfs->nfs_client == RT_NULL)
        return -1;

    sHandle = get_dir_handle(nfs, src);
    if (sHandle == RT_NULL)
        return -1;

    dHandle = get_dir_handle(nfs, dest);
    if (dHandle == RT_NULL)
        return -1;

    args.from.dir = *sHandle;
    args.from.name = strrchr(src, '/') + 1;
    if (args.from.name == RT_NULL)
        args.from.name = (char *)src;

    args.to.dir = *dHandle;
    args.to.name = strrchr(src, '/') + 1;
    if (args.to.name == RT_NULL)
        args.to.name = (char *)dest;

    memset(&res, '\0', sizeof(res));

    if (nfsproc3_rename_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
    {
        rt_kprintf("Rename failed\n");
        ret = -1;
    }
    else if (res.status != NFS3_OK)
    {
        rt_kprintf("Rename failed: %d\n", res.status);
        ret = -1;
    }

    xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
    xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
    xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);

    return ret;
1075 1076
}

1077
int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
1078
{
Y
yiyue.fang 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    nfs_dir *dir;
    rt_uint32_t index;
    struct dirent *d;
    struct nfs_filesystem *nfs;
    char *name;

    dir = (nfs_dir *)(file->data);
    RT_ASSERT(dir != RT_NULL);
    RT_ASSERT(file->fs != RT_NULL);
    RT_ASSERT(file->fs->data != RT_NULL);
    nfs = (struct nfs_filesystem *)file->fs->data;

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

    index = 0;
    while (1)
    {
        d = dirp + index;

        name = nfs_readdir(nfs, dir);
        if (name == RT_NULL)
            break;

        d->d_type = DFS_DT_REG;

        d->d_namlen = rt_strlen(name);
        d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
        rt_strncpy(d->d_name, name, rt_strlen(name) + 1);

        index ++;
        if (index * sizeof(struct dirent) >= count)
            break;
    }

    return index * sizeof(struct dirent);
1117 1118
}

1119
static const struct dfs_filesystem_operation _nfs = 
1120
{
Y
yiyue.fang 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
    "nfs",
    DFS_FS_FLAG_DEFAULT,    
    nfs_mount,
    nfs_unmount,
    RT_NULL, /* mkfs */
    RT_NULL, /* statfs */
    nfs_open,
    nfs_close,
    nfs_ioctl,
    nfs_read,
    nfs_write,
    RT_NULL, /* flush */
    nfs_lseek,
    nfs_getdents,
    nfs_unlink, 
    nfs_stat,
    nfs_rename,
1138
};
1139

1140 1141
int nfs_init(void)
{
Y
yiyue.fang 已提交
1142 1143
    /* register fatfs file system */
    dfs_register(&_nfs);
1144

Y
yiyue.fang 已提交
1145
    return RT_EOK;
1146
}
B
Bernard Xiong 已提交
1147
INIT_FS_EXPORT(nfs_init);