/*
 *               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 contains the public declarations for the SCFS
 *
 */


#ifndef __FS_H__
#define __FS_H__


#include <sktypes.h>


/* File system return type */
typedef s32 FSError;

/* File system callback for async functions */
typedef void (*FSCallBack) (FSError);

/* File system statistics */
typedef struct {
    u32     blockSize;      /* File system block size */
    u32     freeBlocks;     /* Free FS blocks */
    u32     occupiedBlocks; /* FS blocks in use */
    u32     badBlocks;      /* Bad FS blocks */
    u32     reservedBlocks; /* Reserved FS blocks */
    u32     freeInodes;     /* Free inodes */
    u32     occupiedInodes; /* Inodes in use */
} FSStats;

/* File statistics */
typedef struct {
    u32     size;           /* Size of file in bytes */
    u32     offset;         /* Current position indicator */
} FSFileStats;


/* File system error codes */
#define FS_ERROR_OK                 0       /* Success */
#define FS_ERROR_INVALID            -101    /* Invalid parameters */
#define FS_ERROR_ACCESS             -102    /* Access denied */
#define FS_ERROR_CORRUPT            -103    /* File system corrupted */
#define FS_ERROR_NOTREADY           -104    /* File system not ready */
#define FS_ERROR_EXISTS             -105    /* Already exists */
#define FS_ERROR_NOEXISTS           -106    /* Does not exist */
#define FS_ERROR_MAXFILES           -107    /* Ran out of inode entries */
#define FS_ERROR_MAXBLOCKS          -108    /* Ran out of FS blocks */
#define FS_ERROR_MAXFD              -109    /* Ran out of file descriptors */
#define FS_ERROR_MAXMEM             -110    /* Ran out of memory */
#define FS_ERROR_MAXDEPTH           -111    /* Maximum directory tree depth */
#define FS_ERROR_BADFD              -112    /* Invalid file descriptor */
#define FS_ERROR_BADBLOCK           -113    /* Bad block */
#define FS_ERROR_ECC                -114    /* ECC error */
#define FS_ERROR_ECC_CRIT           -115    /* Critical ECC error */
#define FS_ERROR_NOTEMPTY           -116    /* File is not empty */
#define FS_ERROR_HMAC               -117    /* HMAC verification failed */
#define FS_ERROR_BUSY               -118    /* Busy handling another request */
#define FS_ERROR_DEV                -119    /* Unrelated device request */
#define FS_ERROR_UNKNOWN            -120    /* Unknown error */

/* File system block size - 16KB */
#define FS_BLOCK_SIZE               (16 * 1024)
#define FS_BLOCK_SIZE_SHIFT         14

/*
 * Max # of characters in a directory name, not including the NULL
 * character
 */
#define FS_INODE_NAMELEN            12

/* Max # of characters in a path name including the NULL character */
#define FS_PATH_MAXLEN              128

/* Max # of open file descriptors */
#define FS_FD_ENTRIES               8

/*
 * Access control permission bits
 */
#define FS_READ_ACCESS              0x1
#define FS_WRITE_ACCESS             0x2
#define FS_RW_ACCESS                (FS_READ_ACCESS | FS_WRITE_ACCESS)


/* TODO: Move to a more generic place */
#define SK_ROOT_ID                  0
#define SK_TITLE_0_ID               1
#define SK_TITLE_1_ID               2

#define SK_ROOT_GROUP               0
#define SK_TITLE_GROUP              1


/* Structures used by the FS resource manager */
typedef struct {
    u8          path1[FS_PATH_MAXLEN];      /* NULL terminated string */
    u8          path2[FS_PATH_MAXLEN];      /* NULL terminated string */
} FSPathsInput;

typedef struct {
    SKUid       ownerId;                    /* File or directory owner */
    SKGid       groupId;                    /* Group ID of the owner */
    u8          path[FS_PATH_MAXLEN];       /* NULL terminated string */
    u8          ownerAccess;                /* Least significant 2 bits */
    u8          groupAccess;                /* Least significant 2 bits */
    u8          othersAccess;               /* Least significant 2 bits */
    u8          attr;                       /* Attribute bits */
} __attribute__ ((packed)) FSPathAttrInput;

/* FS resource manager commands */
#define FS_FORMAT                   1
#define FS_STATS                    2
#define FS_DCREATE                  3
#define FS_DREAD                    4
#define FS_SETATTR                  5
#define FS_GETATTR                  6
#define FS_DEL                      7
#define FS_RENAME                   8
#define FS_FCREATE                  9
#define FS_FSETVCTRL                10
#define FS_FSTATS                   11


/* File system APIs */
FSError FS_InitLib(void *buf);
FSError FS_CloseLib(void);
FSError FS_Format(void);
FSError FS_FormatAsync(FSCallBack cb);

FSError FS_GetStats(FSStats *stats);

FSError FS_CreateDir(const u8 *dname, SKUid ownerId, SKGid groupId,
    u32 dirAttr, u32 ownerAcc, u32 groupAcc, u32 othersAcc);
FSError FS_CreateDirAsync(const u8 *dname, SKUid ownerId, SKGid groupId,
    u32 dirAttr, u32 ownerAcc, u32 groupAcc, u32 othersAcc, FSCallBack cb);

FSError FS_ReadDir(const u8 *dname, u8 *nameList, u32 *num);

FSError FS_SetAttr(const u8 *name, SKGid groupId, u32 attr, u32 ownerAcc,
    u32 groupAcc, u32 othersAcc);
FSError FS_SetAttrAsync(const u8 *name, SKGid groupId, u32 attr,
    u32 ownerAcc, u32 groupAcc, u32 othersAcc, FSCallBack cb);

FSError FS_GetAttr(const u8 *name, SKUid *ownerId, SKGid *groupId,
    u32 *attr, u32 *ownerAcc, u32 *groupAcc, u32 *othersAcc);

FSError FS_Delete(const u8 *name);
FSError FS_DeleteAsync(const u8 *name, FSCallBack cb);

FSError FS_Rename(const u8 *oldName, const u8 *newName);
FSError FS_RenameAsync(const u8 *oldName, const u8 *newName, FSCallBack cb);

FSError FS_CreateFile(const u8 *fname, SKUid ownerId, SKGid groupId,
    u32 fileAttr, u32 ownerAcc, u32 groupAcc, u32 othersAcc);
FSError FS_CreateFileAsync(const u8 *fname, SKUid ownerId,
    SKGid groupId, u32 fileAttr, u32 ownerAcc, u32 groupAcc, u32 othersAcc,
    FSCallBack cb);

FSError FS_SetFileVersionControl(const u8 *fname, u32 enable);
FSError FS_SetFileVersionControlAsync(const u8 *fname, u32 enable,
    FSCallBack cb);

SKFd    FS_Open(const u8 *fname, u32 access);

FSError FS_GetFileStats(SKFd fd, FSFileStats *stats);

FSError FS_Seek(SKFd fd, s32 offset, u32 whence);

FSError FS_Read(SKFd fd, u8 *buf, u32 size);
FSError FS_ReadAsync(SKFd fd, u8 *buf, u32 size, FSCallBack cb);

FSError FS_Write(SKFd fd, const u8 *buf, u32 size);
FSError FS_WriteAsync(SKFd fd, const u8 *buf, u32 size, FSCallBack cb);

FSError FS_Close(SKFd fd);
FSError FS_CloseAsync(SKFd fd, FSCallBack cb);


#endif /* __FS_H__ */
