Solve the fstat-wants-to-look-at-kernel-data-structures in a nicer

way: don't export the fs internals to innocent userspace programs
which just want to mount the file system.
This commit is contained in:
pooka 2008-07-29 09:10:09 +00:00
parent 340752e10f
commit f00b7c9b12
4 changed files with 190 additions and 177 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: tmpfs.h,v 1.36 2008/07/28 18:00:20 pooka Exp $ */
/* $NetBSD: tmpfs.h,v 1.37 2008/07/29 09:10:09 pooka Exp $ */
/*
* Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
@ -38,11 +38,49 @@
#include <sys/queue.h>
#include <sys/vnode.h>
/* --------------------------------------------------------------------- */
/* For the kernel and anyone who likes peeking into kernel memory */
/* --------------------------------------------------------------------- */
#if defined(_KERNEL)
#include <fs/tmpfs/tmpfs_pool.h>
#include <fs/tmpfs/tmpfs_args.h>
#endif /* defined(_KERNEL) */
/* --------------------------------------------------------------------- */
/*
* Internal representation of a tmpfs directory entry.
*/
struct tmpfs_dirent {
TAILQ_ENTRY(tmpfs_dirent) td_entries;
/* Length of the name stored in this directory entry. This avoids
* the need to recalculate it every time the name is used. */
uint16_t td_namelen;
/* The name of the entry, allocated from a string pool. This
* string is not required to be zero-terminated; therefore, the
* td_namelen field must always be used when accessing its value. */
char * td_name;
/* Pointer to the node this entry refers to. */
struct tmpfs_node * td_node;
};
/* A directory in tmpfs holds a sorted list of directory entries, which in
* turn point to other files (which can be directories themselves).
*
* In tmpfs, this list is managed by a tail queue, whose head is defined by
* the struct tmpfs_dir type.
*
* It is imporant to notice that directories do not have entries for . and
* .. as other file systems do. These can be generated when requested
* based on information available by other means, such as the pointer to
* the node itself in the former case or the pointer to the parent directory
* in the latter case. This is done to simplify tmpfs's code and, more
* importantly, to remove redundancy. */
TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
/* Each entry in a directory has a cookie that identifies it. Cookies
* supersede offsets within directories because, given how tmpfs stores
* directories in memory, there is no such thing as an offset. (Emulating
@ -92,6 +130,7 @@
* if they can happen at all in practice).
*
* XXX A nicer solution shall be attempted. */
#if defined(_KERNEL)
#define TMPFS_DIRCOOKIE_DOT 0
#define TMPFS_DIRCOOKIE_DOTDOT 1
#define TMPFS_DIRCOOKIE_EOF 2
@ -108,9 +147,132 @@ tmpfs_dircookie(struct tmpfs_dirent *de)
return cookie;
}
#endif /* defined(_KERNEL) */
/* --------------------------------------------------------------------- */
/*
* Internal representation of a tmpfs file system node.
*
* This structure is splitted in two parts: one holds attributes common
* to all file types and the other holds data that is only applicable to
* a particular type. The code must be careful to only access those
* attributes that are actually allowed by the node's type.
*/
struct tmpfs_node {
/* Doubly-linked list entry which links all existing nodes for a
* single file system. This is provided to ease the removal of
* all nodes during the unmount operation. */
LIST_ENTRY(tmpfs_node) tn_entries;
/* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
* 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
* types instead of a custom enumeration is to make things simpler
* and faster, as we do not need to convert between two types. */
enum vtype tn_type;
/* Node identifier. */
ino_t tn_id;
/* Node's internal status. This is used by several file system
* operations to do modifications to the node in a delayed
* fashion. */
int tn_status;
#define TMPFS_NODE_ACCESSED (1 << 1)
#define TMPFS_NODE_MODIFIED (1 << 2)
#define TMPFS_NODE_CHANGED (1 << 3)
/* The node size. It does not necessarily match the real amount
* of memory consumed by it. */
off_t tn_size;
/* Generic node attributes. */
uid_t tn_uid;
gid_t tn_gid;
mode_t tn_mode;
int tn_flags;
nlink_t tn_links;
struct timespec tn_atime;
struct timespec tn_mtime;
struct timespec tn_ctime;
struct timespec tn_birthtime;
unsigned long tn_gen;
/* Head of byte-level lock list (used by tmpfs_advlock). */
struct lockf * tn_lockf;
/* As there is a single vnode for each active file within the
* system, care has to be taken to avoid allocating more than one
* vnode per file. In order to do this, a bidirectional association
* is kept between vnodes and nodes.
*
* Whenever a vnode is allocated, its v_data field is updated to
* point to the node it references. At the same time, the node's
* tn_vnode field is modified to point to the new vnode representing
* it. Further attempts to allocate a vnode for this same node will
* result in returning a new reference to the value stored in
* tn_vnode.
*
* May be NULL when the node is unused (that is, no vnode has been
* allocated for it or it has been reclaimed). */
kmutex_t tn_vlock;
struct vnode * tn_vnode;
union {
/* Valid when tn_type == VBLK || tn_type == VCHR. */
struct {
dev_t tn_rdev;
} tn_dev;
/* Valid when tn_type == VDIR. */
struct {
/* Pointer to the parent directory. The root
* directory has a pointer to itself in this field;
* this property identifies the root node. */
struct tmpfs_node * tn_parent;
/* Head of a tail-queue that links the contents of
* the directory together. See above for a
* description of its contents. */
struct tmpfs_dir tn_dir;
/* Number and pointer of the first directory entry
* returned by the readdir operation if it were
* called again to continue reading data from the
* same directory as before. This is used to speed
* up reads of long directories, assuming that no
* more than one read is in progress at a given time.
* Otherwise, these values are discarded and a linear
* scan is performed from the beginning up to the
* point where readdir starts returning values. */
off_t tn_readdir_lastn;
struct tmpfs_dirent * tn_readdir_lastp;
} tn_dir;
/* Valid when tn_type == VLNK. */
struct tn_lnk {
/* The link's target, allocated from a string pool. */
char * tn_link;
} tn_lnk;
/* Valid when tn_type == VREG. */
struct tn_reg {
/* The contents of regular files stored in a tmpfs
* file system are represented by a single anonymous
* memory object (aobj, for short). The aobj provides
* direct access to any position within the file,
* because its contents are always mapped in a
* contiguous region of virtual memory. It is a task
* of the memory management subsystem (see uvm(9)) to
* issue the required page ins or page outs whenever
* a position within the file is accessed. */
struct uvm_object * tn_aobj;
size_t tn_aobj_pages;
} tn_reg;
} tn_spec;
};
#if defined(_KERNEL)
LIST_HEAD(tmpfs_node_list, tmpfs_node);
/* --------------------------------------------------------------------- */
@ -304,6 +466,23 @@ VFS_TO_TMPFS(struct mount *mp)
return tmp;
}
#endif /* defined(_KERNEL) */
static __inline
struct tmpfs_node *
VP_TO_TMPFS_NODE(struct vnode *vp)
{
struct tmpfs_node *node;
#ifdef KASSERT
KASSERT((vp) != NULL && (vp)->v_data != NULL);
#endif
node = (struct tmpfs_node *)vp->v_data;
return node;
}
#if defined(_KERNEL)
static __inline
struct tmpfs_node *
VP_TO_TMPFS_DIR(struct vnode *vp)
@ -316,4 +495,6 @@ VP_TO_TMPFS_DIR(struct vnode *vp)
#endif
return node;
}
#endif /* defined(_KERNEL) */
#endif /* _FS_TMPFS_TMPFS_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: tmpfs_args.h,v 1.2 2008/07/28 18:00:20 pooka Exp $ */
/* $NetBSD: tmpfs_args.h,v 1.3 2008/07/29 09:10:09 pooka Exp $ */
/*
* Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
@ -33,175 +33,6 @@
#ifndef _FS_TMPFS_TMPFS_ARGS_H_
#define _FS_TMPFS_TMPFS_ARGS_H_
#include <sys/vnode.h>
/*
* Internal representation of a tmpfs directory entry.
*/
struct tmpfs_dirent {
TAILQ_ENTRY(tmpfs_dirent) td_entries;
/* Length of the name stored in this directory entry. This avoids
* the need to recalculate it every time the name is used. */
uint16_t td_namelen;
/* The name of the entry, allocated from a string pool. This
* string is not required to be zero-terminated; therefore, the
* td_namelen field must always be used when accessing its value. */
char * td_name;
/* Pointer to the node this entry refers to. */
struct tmpfs_node * td_node;
};
/* A directory in tmpfs holds a sorted list of directory entries, which in
* turn point to other files (which can be directories themselves).
*
* In tmpfs, this list is managed by a tail queue, whose head is defined by
* the struct tmpfs_dir type.
*
* It is imporant to notice that directories do not have entries for . and
* .. as other file systems do. These can be generated when requested
* based on information available by other means, such as the pointer to
* the node itself in the former case or the pointer to the parent directory
* in the latter case. This is done to simplify tmpfs's code and, more
* importantly, to remove redundancy. */
TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
/*
* Internal representation of a tmpfs file system node.
*
* This structure is splitted in two parts: one holds attributes common
* to all file types and the other holds data that is only applicable to
* a particular type. The code must be careful to only access those
* attributes that are actually allowed by the node's type.
*/
struct tmpfs_node {
/* Doubly-linked list entry which links all existing nodes for a
* single file system. This is provided to ease the removal of
* all nodes during the unmount operation. */
LIST_ENTRY(tmpfs_node) tn_entries;
/* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
* 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
* types instead of a custom enumeration is to make things simpler
* and faster, as we do not need to convert between two types. */
enum vtype tn_type;
/* Node identifier. */
ino_t tn_id;
/* Node's internal status. This is used by several file system
* operations to do modifications to the node in a delayed
* fashion. */
int tn_status;
#define TMPFS_NODE_ACCESSED (1 << 1)
#define TMPFS_NODE_MODIFIED (1 << 2)
#define TMPFS_NODE_CHANGED (1 << 3)
/* The node size. It does not necessarily match the real amount
* of memory consumed by it. */
off_t tn_size;
/* Generic node attributes. */
uid_t tn_uid;
gid_t tn_gid;
mode_t tn_mode;
int tn_flags;
nlink_t tn_links;
struct timespec tn_atime;
struct timespec tn_mtime;
struct timespec tn_ctime;
struct timespec tn_birthtime;
unsigned long tn_gen;
/* Head of byte-level lock list (used by tmpfs_advlock). */
struct lockf * tn_lockf;
/* As there is a single vnode for each active file within the
* system, care has to be taken to avoid allocating more than one
* vnode per file. In order to do this, a bidirectional association
* is kept between vnodes and nodes.
*
* Whenever a vnode is allocated, its v_data field is updated to
* point to the node it references. At the same time, the node's
* tn_vnode field is modified to point to the new vnode representing
* it. Further attempts to allocate a vnode for this same node will
* result in returning a new reference to the value stored in
* tn_vnode.
*
* May be NULL when the node is unused (that is, no vnode has been
* allocated for it or it has been reclaimed). */
kmutex_t tn_vlock;
struct vnode * tn_vnode;
union {
/* Valid when tn_type == VBLK || tn_type == VCHR. */
struct {
dev_t tn_rdev;
} tn_dev;
/* Valid when tn_type == VDIR. */
struct {
/* Pointer to the parent directory. The root
* directory has a pointer to itself in this field;
* this property identifies the root node. */
struct tmpfs_node * tn_parent;
/* Head of a tail-queue that links the contents of
* the directory together. See above for a
* description of its contents. */
struct tmpfs_dir tn_dir;
/* Number and pointer of the first directory entry
* returned by the readdir operation if it were
* called again to continue reading data from the
* same directory as before. This is used to speed
* up reads of long directories, assuming that no
* more than one read is in progress at a given time.
* Otherwise, these values are discarded and a linear
* scan is performed from the beginning up to the
* point where readdir starts returning values. */
off_t tn_readdir_lastn;
struct tmpfs_dirent * tn_readdir_lastp;
} tn_dir;
/* Valid when tn_type == VLNK. */
struct tn_lnk {
/* The link's target, allocated from a string pool. */
char * tn_link;
} tn_lnk;
/* Valid when tn_type == VREG. */
struct tn_reg {
/* The contents of regular files stored in a tmpfs
* file system are represented by a single anonymous
* memory object (aobj, for short). The aobj provides
* direct access to any position within the file,
* because its contents are always mapped in a
* contiguous region of virtual memory. It is a task
* of the memory management subsystem (see uvm(9)) to
* issue the required page ins or page outs whenever
* a position within the file is accessed. */
struct uvm_object * tn_aobj;
size_t tn_aobj_pages;
} tn_reg;
} tn_spec;
};
static __inline
struct tmpfs_node *
VP_TO_TMPFS_NODE(struct vnode *vp)
{
struct tmpfs_node *node;
#ifdef KASSERT
KASSERT((vp) != NULL && (vp)->v_data != NULL);
#endif
node = (struct tmpfs_node *)vp->v_data;
return node;
}
/*
* This structure is used to communicate mount parameters between userland
* and kernel space.

View File

@ -1,4 +1,4 @@
/* $NetBSD: tmpfs_vfsops.c,v 1.43 2008/07/28 18:00:20 pooka Exp $ */
/* $NetBSD: tmpfs_vfsops.c,v 1.44 2008/07/29 09:10:09 pooka Exp $ */
/*
* Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
@ -42,7 +42,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.43 2008/07/28 18:00:20 pooka Exp $");
__KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.44 2008/07/29 09:10:09 pooka Exp $");
#include <sys/param.h>
#include <sys/types.h>
@ -56,6 +56,7 @@ __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.43 2008/07/28 18:00:20 pooka Exp
#include <miscfs/genfs/genfs.h>
#include <fs/tmpfs/tmpfs.h>
#include <fs/tmpfs/tmpfs_args.h>
MODULE(MODULE_CLASS_VFS, tmpfs, NULL);

View File

@ -1,4 +1,4 @@
/* $NetBSD: tmpfs.c,v 1.6 2008/07/28 18:05:09 pooka Exp $ */
/* $NetBSD: tmpfs.c,v 1.7 2008/07/29 09:10:09 pooka Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: tmpfs.c,v 1.6 2008/07/28 18:05:09 pooka Exp $");
__RCSID("$NetBSD: tmpfs.c,v 1.7 2008/07/29 09:10:09 pooka Exp $");
#include <sys/types.h>
#include <sys/param.h>
@ -37,7 +37,7 @@ __RCSID("$NetBSD: tmpfs.c,v 1.6 2008/07/28 18:05:09 pooka Exp $");
#include <sys/vnode.h>
#include <sys/mount.h>
#include <fs/tmpfs/tmpfs_args.h>
#include <fs/tmpfs/tmpfs.h>
#include <err.h>
#include <kvm.h>