/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished proprietary information of BroadOn Communications Corp.,
 *  and are protected by federal copyright law. They may not be disclosed
 *  to third parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 *  Description:
 *      This file implements the core file system operations for the SCFS
 *
 */


#ifndef DS
#include <ioslibc.h>
#endif

#include <fs.h>

#ifdef NET_CARD
#include <sc/ios.h>
#endif

#ifdef NEXT_GEN
#include <rvl/ios.h>
#endif

#ifdef DS

#include <ios.h>
#include <string.h>             /* Needed for memcpy() */
#define ATTR_SECTION_DATA
#if defined(__arm) && defined(__MWERKS__)
extern size_t strnlen(const unsigned char *str, size_t n);
#endif

#else
#define ATTR_SECTION_DATA __attribute__ ((section(".data")))
#endif /* !NITRO_SDK */

#ifdef NEXT_GEN
#define CACHE_LINE_SIZE     32
#else
#define CACHE_LINE_SIZE     16
#endif

#define CACHE_LINE_MASK     (CACHE_LINE_SIZE - 1)
#define MULT_CACHE_LINE(sz) ((sz + CACHE_LINE_MASK) & ~CACHE_LINE_MASK)
#define CACHE_LINE_ALIGN    __attribute__ ((aligned(CACHE_LINE_SIZE)))

#define ISFS_GETFD()                                                        \
    do {                                                                    \
        if (__isfsFd < 0) {                                                 \
            __isfsFd = IOS_Open("/dev/fs", 0);                              \
        }                                                                   \
    } while (0)

#define ISFS_RELFD()                                                        \
    do {                                                                    \
        if (__isfsFd >= 0) {                                                \
            IOS_Close(__isfsFd);                                            \
            __isfsFd = -1;                                                  \
        }                                                                   \
    } while (0)


/* FS library connection to the file system */
static IOSFd    __isfsFd = -1;

/* Buffer readable/writable by the FS, passed to the library through init */
static u8       __isfsInternalBuf[ISFS_INITLIB_BUFSIZE] CACHE_LINE_ALIGN ATTR_SECTION_DATA;
void            *__isfsBuf = __isfsInternalBuf;


ISFSError
ISFS_Format(void)
{
    ISFS_GETFD();

    return (IOS_Ioctl(__isfsFd, ISFS_FORMAT, NULL, 0, NULL, 0));

    ISFS_RELFD();
}


ISFSError
ISFS_GetStats(ISFSStats *stats)
{
    ISFS_GETFD();

    return (IOS_Ioctl(__isfsFd, ISFS_STATS, NULL, 0, stats, sizeof(ISFSStats)));

    ISFS_RELFD();
}


ISFSError
ISFS_CreateDir(const u8 *dname, u32 dirAttr, u32 ownerAcc, u32 groupAcc,
    u32 othersAcc)
{
    ISFSError           rc = ISFS_ERROR_OK;
    ISFSPathAttrArgs    *pathAttrArgs;
    u32                 len;
    u8                  array[4];

    ISFS_GETFD();

    if ((dname == NULL) ||
            ((len = strnlen(dname, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_CreateDir_out;
    }

    /* Issue the create dir command */
    pathAttrArgs = (ISFSPathAttrArgs *) __isfsBuf;
    memcpy(pathAttrArgs->path, dname, len+1);
    array[0] = ownerAcc;
    array[1] = groupAcc;
    array[2] = othersAcc;
    array[3] = dirAttr;
    memcpy(&pathAttrArgs->ownerAccess, array, 4);

    rc = IOS_Ioctl(__isfsFd,  ISFS_DCREATE, pathAttrArgs,
                sizeof (ISFSPathAttrArgs), NULL, 0);

ISFS_CreateDir_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_ReadDir(const u8 *dname, u8 *nameList, u32 *num)
{
    ISFSError   rc = ISFS_ERROR_OK;
    IOSIoVector *v = __isfsBuf;
    u32         len, numInputs, numOutputs, *numPtr;
    u8          *dnPtr;

    ISFS_GETFD();

    if ((dname == NULL) || (num == NULL) ||
            ((len = strnlen(dname, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_ReadDir_out;
    }

    /*
     * Issue the read dir command. There can be at most two inputs:
     *  1) The name of the directory
     *  2) The number of elements (only if nameList is not NULL)
     *
     * There can also be at most two outputs:
     *  1) The name list buffer (only if nameList is not NULL)
     *  2) The actual number of elements copied
     */

    /* Copy the directory name onto the global buffer */
    dnPtr = (u8 *) (((u32) __isfsBuf) + MULT_CACHE_LINE(4 * sizeof (IOSIoVector)));
    memcpy(dnPtr, dname, len + 1);
    v[0].base = dnPtr;
    v[0].length = ISFS_PATH_MAXLEN;

    /* Set the pointer for num input/output */
    numPtr = (u32 *) (((u32) dnPtr) + ISFS_PATH_MAXLEN);

    if (nameList) {
        numInputs = 2;
        numOutputs = 2;

        *numPtr = *num;
        v[1].base = (u8 *) numPtr;
        v[1].length = 4;
        v[2].base = nameList;
        v[2].length = *num * (ISFS_INODE_NAMELEN + 1);
        v[3].base = (u8 *) numPtr;
        v[3].length = 4;
    } else {
        numInputs = 1;
        numOutputs = 1;

        v[1].base = (u8 *) numPtr;
        v[1].length = 4;
    }

    rc = IOS_Ioctlv(__isfsFd,  ISFS_DREAD, numInputs, numOutputs, v);
    if (rc != ISFS_ERROR_OK) {
        goto ISFS_ReadDir_out;
    }

    *num = *numPtr;

ISFS_ReadDir_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_SetAttr(const u8 *name, IOSUid ownerId, IOSGid groupId, u32 attr,
    u32 ownerAcc, u32 groupAcc, u32 othersAcc)
{
    ISFSError           rc = ISFS_ERROR_OK;
    ISFSPathAttrArgs    *pathAttrArgs;
    u32                 len;
    u8                  array[4];

    ISFS_GETFD();

    if ((name == NULL) ||
            ((len = strnlen(name, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_SetAttr_out;
    }

    /* Issue the set attr command */
    pathAttrArgs = (ISFSPathAttrArgs *) __isfsBuf;
    memcpy(pathAttrArgs->path, name, len+1);
    memcpy(&pathAttrArgs->ownerId, &ownerId, sizeof(pathAttrArgs->ownerId));
    memcpy(&pathAttrArgs->groupId, &groupId, sizeof(pathAttrArgs->groupId));
    array[0] = ownerAcc;
    array[1] = groupAcc;
    array[2] = othersAcc;
    array[3] = attr;
    memcpy(&pathAttrArgs->ownerAccess, array, 4);

    rc = IOS_Ioctl(__isfsFd, ISFS_SETATTR, (u8 *) pathAttrArgs,
                sizeof (ISFSPathAttrArgs), NULL, 0);

ISFS_SetAttr_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_GetAttr(const u8 *name, IOSUid *ownerId, IOSGid *groupId, u32 *attr,
    u32 *ownerAcc, u32 *groupAcc, u32 *othersAcc)
{
    ISFSError           rc = ISFS_ERROR_OK;
    ISFSPathAttrArgs    *pathAttrArgs;
    u8                  *ptr, array[4];
    u32                 len;

    ISFS_GETFD();

    if ((name == NULL) ||
            ((len = strnlen(name, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN) ||
            (ownerId == NULL) || (groupId == NULL) || (attr == NULL) ||
            (ownerAcc == NULL) || (groupAcc == NULL) || (othersAcc == NULL)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_GetAttr_out;
    }

    /* Issue the get attr command */
    ptr = (u8 *) __isfsBuf;
    memcpy(ptr, name, len+1);
    pathAttrArgs = (ISFSPathAttrArgs *) &ptr[ISFS_PATH_MAXLEN];

    rc = IOS_Ioctl(__isfsFd, ISFS_GETATTR, (u8 *) __isfsBuf, ISFS_PATH_MAXLEN,
                (u8 *) pathAttrArgs, sizeof (ISFSPathAttrArgs));
    if (rc == ISFS_ERROR_OK) {
        memcpy(ownerId, &pathAttrArgs->ownerId, sizeof(*ownerId));
        memcpy(groupId, &pathAttrArgs->groupId, sizeof(*groupId));
        memcpy(array, &pathAttrArgs->ownerAccess, sizeof(array));
        *ownerAcc = array[0];
        *groupAcc = array[1];
        *othersAcc = array[2];
        *attr = array[3];
    }

ISFS_GetAttr_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_GetUsage(const u8 *dname, u32 *nblocks, u32 *ninodes)
{
    ISFSError   rc = ISFS_ERROR_OK;
    IOSIoVector *v = __isfsBuf;
    u32         len, *blkPtr, *inodePtr;
    u8          *dnPtr;

    ISFS_GETFD();

    if ((dname == NULL) || (nblocks == NULL) || (ninodes == NULL) ||
            ((len = strnlen(dname, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_GetUsage_out;
    }

    /* Copy the directory name onto the global buffer */
    dnPtr = (u8 *) (((u32) __isfsBuf) + MULT_CACHE_LINE(3 * sizeof (IOSIoVector)));
    memcpy(dnPtr, dname, len + 1);
    v[0].base = dnPtr;
    v[0].length = ISFS_PATH_MAXLEN;

    /* Set the pointers for nblocks and ninodes */
    blkPtr = (u32 *) (((u32) dnPtr) + ISFS_PATH_MAXLEN);
    inodePtr = (u32 *) (((u32) blkPtr) + CACHE_LINE_SIZE);

    v[1].base = (u8 *) blkPtr;
    v[1].length = 4;
    v[2].base = (u8 *) inodePtr;
    v[2].length = 4;

    rc = IOS_Ioctlv(__isfsFd,  ISFS_GETUSAGE, 1, 2, v);
    if (rc != ISFS_ERROR_OK) {
        goto ISFS_GetUsage_out;
    }

    *nblocks = *blkPtr;
    *ninodes = *inodePtr;

ISFS_GetUsage_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_Delete(const u8 *name)
{
    ISFSError   rc = ISFS_ERROR_OK;
    u32         len;

    ISFS_GETFD();

    if ((name == NULL) ||
            ((len = strnlen(name, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_Delete_out;
    }

    /* Issue the delete command */
    memcpy(__isfsBuf, name, len+1);
    rc = IOS_Ioctl(__isfsFd, ISFS_DEL, (u8 *) __isfsBuf, ISFS_PATH_MAXLEN, NULL, 0);

ISFS_Delete_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_Rename(const u8 *oldName, const u8 *newName)
{
    ISFSError       rc = ISFS_ERROR_OK;
    ISFSPathsArgs   *pathsArgs;
    u32             oldLen, newLen;

    ISFS_GETFD();

    if ((oldName == NULL) || (newName == NULL) ||
            ((oldLen = strnlen(oldName, ISFS_PATH_MAXLEN)) ==
                                                        ISFS_PATH_MAXLEN) ||
            ((newLen = strnlen(newName, ISFS_PATH_MAXLEN)) ==
                                                        ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_Rename_out;
    }

    /* Issue the rename command */
    pathsArgs = (ISFSPathsArgs *) __isfsBuf;
    memcpy(pathsArgs->path1, oldName, oldLen+1);
    memcpy(pathsArgs->path2, newName, newLen+1);

    rc = IOS_Ioctl(__isfsFd, ISFS_RENAME, (u8 *) pathsArgs,
                sizeof (ISFSPathsArgs), NULL, 0);

ISFS_Rename_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_CreateFile(const u8 *fname, u32 fileAttr, u32 ownerAcc, u32 groupAcc,
    u32 othersAcc)
{
    ISFSError           rc = ISFS_ERROR_OK;
    ISFSPathAttrArgs    *pathAttrArgs;
    u32                 len;
    u8                  array[4];

    ISFS_GETFD();

    if ((fname == NULL) ||
            ((len = strnlen(fname, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_CreateFile_out;
    }

    /* Issue the file create command */
    pathAttrArgs = (ISFSPathAttrArgs *) __isfsBuf;
    memcpy(pathAttrArgs->path, fname, len+1);
    array[0] = ownerAcc;
    array[1] = groupAcc;
    array[2] = othersAcc;
    array[3] = fileAttr;
    memcpy(&pathAttrArgs->ownerAccess, array, 4);

    rc = IOS_Ioctl(__isfsFd, ISFS_FCREATE, (u8 *) pathAttrArgs,
                sizeof (ISFSPathAttrArgs), NULL, 0);

ISFS_CreateFile_out:

    ISFS_RELFD();

    return (rc);
}


ISFSError
ISFS_SetFileVersionControl(const u8 *fname, u32 enable)
{
    ISFSError           rc = ISFS_ERROR_OK;
    ISFSPathAttrArgs    *pathAttrArgs;
    u32                 len;
    u8                  array[1];

    ISFS_GETFD();

    if ((fname == NULL) ||
            ((len = strnlen(fname, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_SetFileVersionControl_out;
    }

    /* Issue the set attr command */
    pathAttrArgs = (ISFSPathAttrArgs *) __isfsBuf;
    memcpy(pathAttrArgs->path, fname, len+1);
    array[0] = enable;
    memcpy(&pathAttrArgs->attr, array, 1);

    rc = IOS_Ioctl(__isfsFd, ISFS_FSETVCTRL, (u8 *) pathAttrArgs,
                sizeof (ISFSPathAttrArgs), NULL, 0);

ISFS_SetFileVersionControl_out:

    ISFS_RELFD();

    return (rc);
}


IOSFd
ISFS_Open(const u8 *fname, u32 access)
{
    ISFSError   rc = ISFS_ERROR_OK;
    u32         len;

    if ((fname == NULL) ||
            ((len = strnlen(fname, ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_Open_out;
    }

    /*
     * For consistency with the other FS APIs, the file name is
     * copied to the global buffer here so that the caller doesn't
     * need to put it into the data section
     */
    memcpy(__isfsBuf, fname, len+1);

    rc = IOS_Open(__isfsBuf, access);

ISFS_Open_out:

    return (rc);
}


ISFSError
ISFS_GetFileStats(IOSFd fd, ISFSFileStats *stats)
{
    return (IOS_Ioctl(fd, ISFS_FSTATS, NULL, 0, stats, sizeof (ISFSFileStats)));
}


ISFSError
ISFS_Seek(IOSFd fd, s32 offset, u32 whence)
{
    return (IOS_Seek(fd, offset, whence));
}


ISFSError
ISFS_Read(IOSFd fd, u8 *buf, u32 size)
{
    return (IOS_Read(fd, buf, size));
}


ISFSError
ISFS_Write(IOSFd fd, const u8 *buf, u32 size)
{
    return (IOS_Write(fd, (u8 *) buf, size));
}


ISFSError
ISFS_Close(IOSFd fd)
{
    return (IOS_Close(fd));
}


ISFSError
ISFS_Shutdown(void)
{
    ISFS_GETFD();

    return (IOS_Ioctl(__isfsFd, ISFS_SHUTDOWN, NULL, 0, NULL, 0));

    ISFS_RELFD();
}


ISFSError
ISFS_ReserveSpace(const u8 **fnames, const u32 *sizes, u32 nFiles)
{
    ISFSError           rc = ISFS_ERROR_OK;
    IOSIoVector         *v = __isfsBuf;
    u32                 numInputs, i, len;

    ISFS_GETFD();

    if ((fnames == NULL) || (sizes == NULL) || (nFiles == 0)) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_ReserveSpace_out;
    }

    /* Inputs include 1 for each file name and 1 for the size array */
    numInputs = 1 + nFiles;

    /* Check to make sure __isfsBuf is large enough */
    if ((numInputs * sizeof (IOSIoVector)) > ISFS_INITLIB_BUFSIZE) {
        rc = ISFS_ERROR_INVALID;
        goto ISFS_ReserveSpace_out;
    }

    /* Set the reserve space arguments */
    for (i = 0; i < nFiles; i++) {
        if ((len = strnlen(fnames[i], ISFS_PATH_MAXLEN)) == ISFS_PATH_MAXLEN) {
            rc = ISFS_ERROR_INVALID;
            goto ISFS_ReserveSpace_out;
        }

        v[i].base = (u8 *) fnames[i];
        v[i].length = len + 1;
    }
    v[nFiles].base = (u8 *) sizes;
    v[nFiles].length = nFiles * sizeof (sizes[0]);

    /* Issue the reserve space command */
    rc = IOS_Ioctlv(__isfsFd,  ISFS_RESERVESPACE, numInputs, 0, v);

ISFS_ReserveSpace_out:

    ISFS_RELFD();

    return (rc);
}
