/*
 * Copyright (C) 1993, 1994 by Ralf Baechle (linux@uni-koblenz.de)
 *
 * This file is part of Minix-Handler, an AmigaDOS Filehandler to access
 * Minix filesystems via AmigaDOS.
 *
 * Minix-Handler 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, or (at your option)
 * any later version.
 *
 * Minix-Handler 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.
 */

#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <dos/dos.h>
#include <dos/dosextens.h>

#include "minix.h"

/*
 * This is a structure containing all information
 * on a certain volume.
 */

struct VolumeList {
    struct MinList	v_List;
    };

/*
 * Pointers to volume data
 * Although these structures don't fill a whole block,
 * always entire blocks are being read!
 */

struct VolumeNode {
    struct MinNode           Node;
//  struct MinList           MinixList;
    struct MinixNode         *RootLock;
    /*
     * IMPORTANT: VolumeName is longword aligned!
     */
    UBYTE                    VolumeName[12];
    /*
     * IMPORTANT: Device is longword aligned!
     */
    struct DeviceList        Device;
    struct minix_super_block *SuperBlock;
    struct minix_inode       *Inodes;
    ULONG                    InodeSize;
    UBYTE                    *ZoneMap;
    ULONG                    ZoneMapSize;
    UBYTE                    *InodeMap;
    ULONG                    InodeMapSize;
    UWORD                    ZonesUsed;
    struct DeviceList        *VolumeInfo;
    ULONG                    VolumeID;
    };

/*
 * This is a link node used both for locks and filehandles.
 */
#if 0
struct FileLock {
    BPTR		fl_Link;	/* bcpl pointer to next lock */
    LONG		fl_Key;		/* disk block number (== inode in our case)*/
    LONG		fl_Access;	/* exclusive or shared */
    struct MsgPort *	fl_Task;	/* handler task's port */
    BPTR		fl_Volume;	/* bptr to DLT_VOLUME DosList entry */
};
#endif
struct MinixNode {
    /*
     * Must be first element!
     */
    struct FileLock		fl;			// == mn_Lock
    struct MinixNode		*Parent;
    struct FileHandle		*mn_FileHandle;
    /*
     * Wo der gepufferte Block im Speicher liegt.
     */
    APTR			*mn_BufferBlock;
    /*
     * mn_BufferPos:
     * 0xffffffff  == no blocks read yet.
     * other value == position in buffered block.
     */
    ULONG			mn_BufferPos;
    /*
     * ordinal number of the current block in buffer.
     */
    ULONG			mn_BlockNum;
    /*
     * ordinal number of the current block to which we're seeked.
     */
    ULONG			mn_SeekBlock;
    /*
     * Wether we have a block in buffer.
     */
    BOOL			mn_InBuffer;
    UBYTE			Name[MINIX_NAME_LEN];
    };
#define nm_Inode fl.fl_Key

#define LOCK2NODE(x) ((struct MinixNode *) (((UBYTE *)(x)) - ((ULONG)(&((struct MinixNode *) 0)->mn_Lock))))

extern struct VolumeNode	*CVol;

VOID Volume_Init(VOID);
struct VolumeNode *Volume_Open(VOID);
VOID Volume_Close(struct VolumeNode *Vol);
UWORD ZonesFree(struct VolumeNode *Vol);
BOOL WriteInodeMap(struct VolumeNode *Vol);
UWORD AllocInode(struct VolumeNode *Vol);
VOID FreeInode(struct VolumeNode *Vol, UWORD ino);
BOOL WriteZoneMap(struct VolumeNode *Vol);
UWORD AllocZone(struct VolumeNode *Vol);
VOID FreeZone(struct VolumeNode *Vol, ULONG block);
BOOL WriteInodes(struct VolumeNode *Vol);
ULONG Volume_IsInUse(struct VolumeNode *Vol);
