/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Portions Copyright 2007 Shivakumar GN */
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/sysmacros.h>
/*
* Generic pseudo-filesystem routines.
*
* There are significant similarities between the implementation of certain file
* system entry points across different filesystems. While one could attempt to
* "choke up on the bat" and incorporate common functionality into a VOP
* preamble or postamble, such an approach is limited in the benefit it can
* provide. In this file we instead define a toolkit of routines which can be
* called from a filesystem (with in-kernel pseudo-filesystems being the focus
* of the exercise) in a more component-like fashion.
*
* There are three basic classes of routines:
*
* 1) Lowlevel support routines
*
* These routines are designed to play a support role for existing
* pseudo-filesystems (such as procfs). They simplify common tasks,
* without forcing the filesystem to hand over management to GFS. The
* routines covered are:
*
* gfs_readdir_init()
* gfs_readdir_emit()
* gfs_readdir_emitn()
* gfs_readdir_pred()
* gfs_readdir_fini()
* gfs_lookup_dot()
*
* 2) Complete GFS management
*
* These routines take a more active role in management of the
* pseudo-filesystem. They handle the relationship between vnode private
* data and VFS data, as well as the relationship between vnodes in the
* directory hierarchy.
*
* In order to use these interfaces, the first member of every private
* v_data must be a gfs_file_t or a gfs_dir_t. This hands over all control
* to GFS.
*
* gfs_file_create()
* gfs_dir_create()
* gfs_root_create()
*
* gfs_file_inactive()
* gfs_dir_inactive()
* gfs_dir_lookup()
* gfs_dir_readdir()
*
* gfs_vop_inactive()
* gfs_vop_lookup()
* gfs_vop_readdir()
* gfs_vop_map()
*
* 3) Single File pseudo-filesystems
*
* This routine creates a rooted file to be overlayed ontop of another
* file in the physical filespace.
*
* Note that the parent is NULL (actually the vfs), but there is nothing
* technically keeping such a file from utilizing the "Complete GFS
* management" set of routines.
*
* gfs_root_create_file()
*/
/*
* gfs_make_opsvec: take an array of vnode type definitions and create
* their vnodeops_t structures
*
* This routine takes an array of gfs_opsvec_t's. It could
* alternatively take an array of gfs_opsvec_t*'s, which would allow
* vnode types to be completely defined in files external to the caller
* of gfs_make_opsvec(). As it stands, much more sharing takes place --
* both the caller and the vnode type provider need to access gfsv_ops
* and gfsv_template, and the caller also needs to know gfsv_name.
*/
int
{
int error, i;
for (i = 0; ; i++) {
return (0);
if (error)
break;
}
for (i--; i >= 0; i--) {
}
return (error);
}
/*
* Low level directory routines
*
* These routines provide some simple abstractions for reading directories.
* They are designed to be used by existing pseudo filesystems (namely procfs)
* that already have a complicated management infrastructure.
*/
/*
* gfs_get_parent_ino: used to obtain a parent inode number and the
* inode number of the given vnode in preparation for calling gfs_readdir_init.
*/
int
{
int error;
if (error)
return (error);
} else {
}
return (0);
}
/*
* gfs_readdir_init: initiate a generic readdir
* st - a pointer to an uninitialized gfs_readdir_state_t structure
* name_max - the directory's maximum file name length
* ureclen - the exported file-space record length (1 for non-legacy FSs)
* uiop - the uiop passed to readdir
* parent - the parent directory's inode
* self - this directory's inode
* flags - flags from VOP_READDIR
*
* Returns 0 or a non-zero errno.
*
* Typical VOP_READDIR usage of gfs_readdir_*:
*
* if ((error = gfs_readdir_init(...)) != 0)
* return (error);
* eof = 0;
* while ((error = gfs_readdir_pred(..., &voffset)) != 0) {
* if (!consumer_entry_at(voffset))
* voffset = consumer_next_entry(voffset);
* if (consumer_eof(voffset)) {
* eof = 1
* break;
* }
* if ((error = gfs_readdir_emit(..., voffset,
* consumer_ino(voffset), consumer_name(voffset))) != 0)
* break;
* }
* return (gfs_readdir_fini(..., error, eofp, eof));
*
* As you can see, a zero result from gfs_readdir_pred() or
* gfs_readdir_emit() indicates that processing should continue,
* whereas a non-zero result indicates that the loop should terminate.
* Most consumers need do nothing more than let gfs_readdir_fini()
* determine what the cause of failure was and return the appropriate
* value.
*/
int
{
return (EINVAL);
if (flags & V_RDDIR_ENTFLAGS)
else
return (0);
}
/*
* gfs_readdir_emit_int: internal routine to emit directory entry
*
* uiop - caller-supplied uio pointer
* next - the offset of the next entry
*/
static int
{
int reclen;
} else {
}
/*
* Error if no entries were returned yet
*/
return (EINVAL);
return (-1);
}
} else {
}
return (EFAULT);
return (0);
}
/*
* gfs_readdir_emit: emit a directory entry
* voff - the virtual offset (obtained from gfs_readdir_pred)
* ino - the entry's inode
* name - the entry's name
* eflags - value for ed_eflags (if processing edirent_t)
*
* Returns a 0 on success, a non-zero errno on failure, or -1 if the
* readdir loop should terminate. A non-zero result (either errno or
* -1) from this function is typically passed directly to
* gfs_readdir_fini().
*/
int
{
} else {
}
/*
* Inter-entry offsets are invalid, so we assume a record size of
* grd_ureclen and explicitly set the offset appropriately.
*/
}
/*
* gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer
* instead of a string for the entry's name.
*/
int
{
}
/*
* gfs_readdir_pred: readdir loop predicate
* voffp - a pointer in which the next virtual offset should be stored
*
* Returns a 0 on success, a non-zero errno on failure, or -1 if the
* readdir loop should terminate. A non-zero result (either errno or
* -1) from this function is typically passed directly to
* gfs_readdir_fini().
*/
int
{
int error;
top:
return (-1);
if (off == 0) {
".", 0)) == 0)
goto top;
} else if (off == 1) {
"..", 0)) == 0)
goto top;
} else {
return (0);
}
return (error);
}
/*
* gfs_readdir_fini: generic readdir cleanup
* error - if positive, an error to return
* eofp - the eofp passed to readdir
* eof - the eof value
*
* Returns a 0 on success, a non-zero errno on failure. This result
* should be returned from readdir.
*/
int
{
else
if (error > 0)
return (error);
if (eofp)
return (0);
}
/*
* gfs_lookup_dot
*
* Performs a basic check for "." and ".." directory entries.
*/
int
{
return (0);
} else {
}
return (0);
}
return (-1);
}
/*
* gfs_file_create(): create a new GFS file
*
* size - size of private data structure (v_data)
* pvp - parent vnode (GFS directory)
* ops - vnode operations vector
*
* In order to use this interface, the parent vnode must have been created by
* gfs_dir_create(), and the private data stored in v_data must have a
* 'gfs_file_t' as its first field.
*
* Given these constraints, this routine will automatically:
*
* - Allocate v_data for the vnode
* - Initialize necessary fields in the vnode
* - Hold the parent
*/
vnode_t *
{
/*
* Allocate vnode and internal data structure
*/
/*
* Set up various pointers
*/
/*
* Initialize vnode and hold parent.
*/
if (pvp) {
}
return (vp);
}
/*
* gfs_dir_create: creates a new directory in the parent
*
* size - size of private data structure (v_data)
* pvp - parent vnode (GFS directory)
* ops - vnode operations vector
* entries - NULL-terminated list of static entries (if any)
* maxlen - maximum length of a directory entry
* readdir_cb - readdir callback (see gfs_dir_readdir)
* inode_cb - inode callback (see gfs_dir_readdir)
* lookup_cb - lookup callback (see gfs_dir_lookup)
*
* In order to use this function, the first member of the private vnode
* structure (v_data) must be a gfs_dir_t. For each directory, there are
* static entries, defined when the structure is initialized, and dynamic
* entries, retrieved through callbacks.
*
* If a directory has static entries, then it must supply a inode callback,
* which will compute the inode number based on the parent and the index.
* For a directory with dynamic entries, the caller must supply a readdir
* callback and a lookup callback. If a static lookup fails, we fall back to
* the supplied lookup callback, if any.
*
* This function also performs the same initialization as gfs_file_create().
*/
vnode_t *
{
dp->gfsd_nstatic++;
}
return (vp);
}
/*
* gfs_root_create(): create a root vnode for a GFS filesystem
*
* Similar to gfs_dir_create(), this creates a root vnode for a filesystem. The
* only difference is that it takes a vfs_t instead of a vnode_t as its parent.
*/
vnode_t *
{
/* Manually set the inode */
return (vp);
}
/*
* gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
*
* Similar to gfs_root_create(), this creates a root vnode for a file to
* be the pseudo-filesystem.
*/
vnode_t *
{
return (vp);
}
/*
* gfs_file_inactive()
*
* Called from the VOP_INACTIVE() routine. If necessary, this routine will
* remove the given vnode from the parent directory and clean up any references
* in the VFS layer.
*
* If the vnode was not removed (due to a race with vget), then NULL is
* returned. Otherwise, a pointer to the private data is returned.
*/
void *
{
int i;
void *data;
goto found;
/*
* First, see if this vnode is cached in the parent.
*/
/*
* Find it in the set of static entries.
*/
for (i = 0; i < dp->gfsd_nstatic; i++) {
goto found;
}
/*
* If 'ge' is NULL, then it is a dynamic entry.
*/
}
/*
* Really remove this vnode
*/
/*
* If this was a statically cached entry, simply set the
* cached vnode to NULL.
*/
}
}
/*
* Free vnode and release parent
*/
if (fp->gfs_parent) {
if (dp) {
}
} else {
}
} else {
}
if (dp)
}
return (data);
}
/*
* gfs_dir_inactive()
*
* Same as above, but for directories.
*/
void *
{
if (dp->gfsd_nstatic)
}
return (dp);
}
/*
* gfs_dir_lookup_dynamic()
*
* This routine looks up the provided name amongst the dynamic entries
* in the gfs directory and returns the corresponding vnode, if found.
*
* The gfs directory is expected to be locked by the caller prior to
* calling this function. The directory will be unlocked during the
* execution of this function, but will be locked upon return from the
* function. This function returns 0 on success, non-zero on error.
*
* The dynamic lookups are performed by invoking the lookup
* callback, which is passed to this function as the first argument.
* The arguments to the callback are:
*
* int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr,
* int flags, int *deflgs, pathname_t *rpnp);
*
* pvp - parent vnode
* nm - name of entry
* vpp - pointer to resulting vnode
* cr - pointer to cred
* flags - flags value from lookup request
* ignored here; currently only used to request
* insensitive lookups
* direntflgs - output parameter, directory entry flags
* ignored here; currently only used to indicate a lookup
* has more than one possible match when case is not considered
* realpnp - output parameter, real pathname
* ignored here; when lookup was performed case-insensitively,
* this field contains the "real" name of the file.
*
* Returns 0 on success, non-zero on error.
*/
static int
{
int ret;
/*
* Drop the directory lock, as the lookup routine
* will need to allocate memory, or otherwise deadlock on this
* directory.
*/
/*
* The callback for extended attributes returns a vnode
* with v_data from an underlying fs.
*/
}
return (ret);
}
/*
* gfs_dir_lookup_static()
*
* This routine looks up the provided name amongst the static entries
* in the gfs directory and returns the corresponding vnode, if found.
* The first argument to the function is a pointer to the comparison
* function this function should use to decide if names are a match.
*
* If a match is found, and GFS_CACHE_VNODE is set and the vnode
* exists, we simply return the existing vnode. Otherwise, we call
* the static entry's callback routine, caching the result if
* necessary. If the idx pointer argument is non-NULL, we use it to
* return the index of the matching static entry.
*
* The gfs directory is expected to be locked by the caller prior to calling
* this function. The directory may be unlocked during the execution of
* this function, but will be locked upon return from the function.
*
* This function returns 0 if a match is found, ENOENT if not.
*/
static int
{
int i;
/*
* Search static entries.
*/
for (i = 0; i < dp->gfsd_nstatic; i++) {
if (rpnp)
rpnp->pn_bufsize);
if (ge->gfse_vnode) {
break;
}
/*
* We drop the directory lock, as the constructor will
* need to do KM_SLEEP allocations. If we return from
* the constructor only to find that a parallel
* operation has completed, and GFS_CACHE_VNODE is set
* for this entry, we discard the result in favor of
* the cached vnode.
*/
/* Set the inode according to the callback. */
} else {
/*
* A parallel constructor beat us to it;
* return existing vnode. We have to be
* careful because we can't release the
* current vnode while holding the
* directory lock; its inactive routine
* will try to lock this directory.
*/
}
}
break;
}
}
return (ENOENT);
else if (idx)
*idx = i;
return (0);
}
/*
* gfs_dir_lookup()
*
* Looks up the given name in the directory and returns the corresponding
* vnode, if found.
*
* First, we search statically defined entries, if any, with a call to
* gfs_dir_lookup_static(). If no static entry is found, and we have
* a callback function we try a dynamic lookup via gfs_dir_lookup_dynamic().
*
* This function returns 0 on success, non-zero on error.
*/
int
{
int (*compare)(const char *, const char *);
return (0);
(flags & FIGNORECASE))
else
int i;
goto out;
}
}
}
/* static and dynamic entries are case-insensitive conflict */
error = 0;
} else if (error) {
}
out:
return (error);
}
/*
* gfs_dir_readdir: does a readdir() on the given directory
*
* dvp - directory vnode
* uiop - uio structure
* eofp - eof pointer
* data - arbitrary data passed to readdir callback
*
* This routine does all the readdir() dirty work. Even so, the caller must
* supply two callbacks in order to get full compatibility.
*
* If the directory contains static entries, an inode callback must be
* specified. This avoids having to create every vnode and call VOP_GETATTR()
* when reading the directory. This function has the following arguments:
*
* ino_t gfs_inode_cb(vnode_t *vp, int index);
*
* vp - vnode for the directory
* index - index in original gfs_dirent_t array
*
* Returns the inode number for the given entry.
*
* For directories with dynamic entries, a readdir callback must be provided.
* This is significantly more complex, thanks to the particulars of
* VOP_READDIR().
*
* int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
* offset_t *off, offset_t *nextoff, void *data, int flags)
*
* vp - directory vnode
* dp - directory entry, sized according to maxlen given to
* gfs_dir_create(). callback must fill in d_name and
* d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
* (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
* is set in 'flags'.
* eofp - callback must set to 1 when EOF has been reached
* off - on entry, the last offset read from the directory. Callback
* must set to the offset of the current entry, typically left
* untouched.
* nextoff - callback must set to offset of next entry. Typically
* (off + 1)
* data - caller-supplied data
* flags - VOP_READDIR flags
*
* Return 0 on success, or error on failure.
*/
int
{
if (error)
return (error);
return (error);
!eof) {
!= 0)
break;
} else if (dp->gfsd_readdir) {
break;
next)) != 0)
break;
} else {
/*
* Offset is beyond the end of the static entries, and
* we have no dynamic entries. Set EOF.
*/
eof = 1;
}
}
}
/*
* gfs_vop_lookup: VOP_LOOKUP() entry point
*
* For use directly in vnode ops table. Given a GFS directory, calls
* gfs_dir_lookup() as necessary.
*/
/* ARGSUSED */
int
{
}
/*
* gfs_vop_readdir: VOP_READDIR() entry point
*
* For use directly in vnode ops table. Given a GFS directory, calls
* gfs_dir_readdir() as necessary.
*/
/* ARGSUSED */
int
{
}
/*
* gfs_vop_map: VOP_MAP() entry point
*
* Convenient routine for handling pseudo-files that wish to allow mmap() calls.
* This function only works for readonly files, and uses the read function for
* the vnode to fill in the data. The mapped data is immediately faulted in and
* filled with the necessary data during this call; there are no getpage() or
* putpage() routines.
*/
/* ARGSUSED */
int
{
int rv;
/*
* Check for bad parameters
*/
#ifdef _ILP32
return (ENOMEM);
#endif
return (ENOTSUP);
return (EFBIG);
return (EINVAL);
return (ENODEV);
return (EACCES);
/*
* Find appropriate address if needed, otherwise clear address range.
*/
if (rv != 0) {
return (rv);
}
/*
* Create mapping
*/
if (rv != 0)
return (rv);
/*
* Fill with data from read()
*/
if (rv != 0) {
}
return (rv);
}
/*
* gfs_vop_inactive: VOP_INACTIVE() entry point
*
* Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
* gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
*/
/* ARGSUSED */
void
{
void *data;
else
}