/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _SYS_GFS_H
#define	_SYS_GFS_H

#pragma ident	"@(#)gfs.h	1.4	06/03/10 SMI"

#include <sys/types.h>
#include <sys/vnode.h>
#include <sys/mutex.h>
#include <sys/dirent.h>
#include <sys/uio.h>
#include <sys/list.h>
#include <sys/pathname.h>

#ifdef	__cplusplus
extern "C" {
#endif

typedef struct gfs_opsvec {
	const char		*gfsv_name;	/* vnode description */
	const fs_operation_def_t *gfsv_template; /* ops template */
	vnodeops_t		**gfsv_ops;	/* ptr to result */
} gfs_opsvec_t;

int gfs_make_opsvec(gfs_opsvec_t *);

#define	GFS_CACHE_VNODE		0x1

typedef struct gfs_dirent {
	char			*gfse_name;	/* entry name */
	vnode_t *(*gfse_ctor)(vnode_t *);	/* constructor */
	int			gfse_flags;	/* flags */
	list_node_t		gfse_link;	/* dynamic list */
	vnode_t			*gfse_vnode;	/* cached vnode */
} gfs_dirent_t;

typedef enum gfs_type {
	GFS_DIR,
	GFS_FILE
} gfs_type_t;

typedef struct gfs_file {
	vnode_t		*gfs_vnode;	/* current vnode */
	vnode_t		*gfs_parent;	/* parent vnode */
	size_t		gfs_size;	/* size of private data structure */
	gfs_type_t	gfs_type;	/* type of vnode */
	int		gfs_index;	/* index in parent dir */
	ino64_t		gfs_ino;	/* inode for this vnode */
} gfs_file_t;

typedef int (*gfs_readdir_cb)(vnode_t *, struct dirent64 *, int *, offset_t *,
    offset_t *, void *);
typedef int (*gfs_lookup_cb)(vnode_t *, const char *, vnode_t **, ino64_t *);
typedef ino64_t (*gfs_inode_cb)(vnode_t *, int);

typedef struct gfs_dir {
	gfs_file_t	gfsd_file;	/* generic file attributes */
	gfs_dirent_t	*gfsd_static;	/* statically defined entries */
	int		gfsd_nstatic;	/* # static entries */
	kmutex_t	gfsd_lock;	/* protects entries */
	int		gfsd_maxlen;	/* maximum name length */
	gfs_readdir_cb	gfsd_readdir;	/* readdir() callback */
	gfs_lookup_cb	gfsd_lookup;	/* lookup() callback */
	gfs_inode_cb	gfsd_inode;	/* get an inode number */
} gfs_dir_t;

struct vfs;

extern vnode_t *gfs_file_create(size_t, vnode_t *, vnodeops_t *);
extern vnode_t *gfs_dir_create(size_t, vnode_t *, vnodeops_t *,
    gfs_dirent_t *, gfs_inode_cb, int, gfs_readdir_cb, gfs_lookup_cb);
extern vnode_t *gfs_root_create(size_t, struct vfs *, vnodeops_t *, ino64_t,
    gfs_dirent_t *, gfs_inode_cb, int, gfs_readdir_cb, gfs_lookup_cb);

extern void *gfs_file_inactive(vnode_t *);
extern void *gfs_dir_inactive(vnode_t *);

extern int gfs_dir_lookup(vnode_t *, const char *, vnode_t **);
extern int gfs_dir_readdir(vnode_t *, uio_t *, int *, void *);

#define	gfs_dir_lock(gd)	mutex_enter(&(gd)->gfsd_lock)
#define	gfs_dir_unlock(gd)	mutex_exit(&(gd)->gfsd_lock)

#define	gfs_file_parent(vp)	(((gfs_file_t *)(vp)->v_data)->gfs_parent)

#define	gfs_file_index(vp)	(((gfs_file_t *)(vp)->v_data)->gfs_index)
#define	gfs_file_set_index(vp, idx)	\
	(((gfs_file_t *)(vp)->v_data)->gfs_index = (idx))

#define	gfs_file_inode(vp)	(((gfs_file_t *)(vp)->v_data)->gfs_ino)
#define	gfs_file_set_inode(vp, ino)	\
	(((gfs_file_t *)(vp)->v_data)->gfs_ino = (ino))

typedef struct gfs_readdir_state {
	struct dirent64 *grd_dirent;	/* directory entry buffer */
	size_t		grd_namlen;	/* max file name length */
	size_t		grd_ureclen;	/* exported record size */
	ssize_t		grd_oresid;	/* original uio_resid */
	ino64_t		grd_parent;	/* inode of parent */
	ino64_t		grd_self;	/* inode of self */
} gfs_readdir_state_t;

extern int gfs_readdir_init(gfs_readdir_state_t *, int, int, uio_t *, ino64_t,
    ino64_t);
extern int gfs_readdir_emit(gfs_readdir_state_t *, uio_t *, offset_t, ino64_t,
    const char *);
extern int gfs_readdir_emitn(gfs_readdir_state_t *, uio_t *, offset_t, ino64_t,
    unsigned long);
extern int gfs_readdir_pred(gfs_readdir_state_t *, uio_t *, offset_t *);
extern int gfs_readdir_fini(gfs_readdir_state_t *, int, int *, int);

extern int gfs_lookup_dot(vnode_t **, vnode_t *, vnode_t *, const char *);

extern int gfs_vop_lookup(vnode_t *, char *, vnode_t **, pathname_t *,
    int, vnode_t *, cred_t *);
extern int gfs_vop_readdir(vnode_t *, uio_t *, cred_t *, int *);
extern int gfs_vop_map(vnode_t *, offset_t, struct as *, caddr_t *,
    size_t, uchar_t, uchar_t, uint_t, cred_t *);
extern void gfs_vop_inactive(vnode_t *, cred_t *);


#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_GFS_H */
