Style fixes and comment updates.

Edit comments which explain no longer relevant details, and add
locking annotations to the struct tmpfs_node members.

Tested by:	pho (as part of the larger patch)
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
This commit is contained in:
Konstantin Belousov 2017-01-19 14:27:37 +00:00
parent b328ce000e
commit bba7ed2054
4 changed files with 180 additions and 163 deletions

View File

@ -80,8 +80,10 @@ struct tmpfs_dirent {
uint32_t td_hash;
u_int td_namelen;
/* Pointer to the node this entry refers to. In case this field
* is NULL, the node is a whiteout. */
/*
* Pointer to the node this entry refers to. In case this field
* is NULL, the node is a whiteout.
*/
struct tmpfs_node * td_node;
union {
@ -94,21 +96,24 @@ struct tmpfs_dirent {
} ud;
};
/* A directory in tmpfs holds a list of directory entries, which in
* turn point to other files (which can be directories themselves).
/*
* A directory in tmpfs holds a collection of directory entries, which
* in turn point to other files (which can be directories themselves).
*
* In tmpfs, this list is managed by a RB-Tree, whose head is defined by
* the struct tmpfs_dir type.
* In tmpfs, this collection is managed by a RB-Tree, whose head is
* defined by the struct tmpfs_dir type.
*
* It is important 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. */
* importantly, to remove redundancy.
*/
RB_HEAD(tmpfs_dir, tmpfs_dirent);
/* Each entry in a directory has a cookie that identifies it. Cookies
/*
* 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.
*
@ -139,51 +144,65 @@ RB_HEAD(tmpfs_dir, tmpfs_dirent);
* a particular type. The code must be careful to only access those
* attributes that are actually allowed by the node's type.
*
*
* Below is the key of locks used to protected the fields in the following
* structures.
*
* (v) vnode lock in exclusive mode
* (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and
* tn_interlock
* (i) tn_interlock
* (m) tmpfs_mount allnode_lock
* (c) stable after creation
*/
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;
/*
* 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, and to support
* the implementation of VOP_VNTOCNP().
*/
LIST_ENTRY(tmpfs_node) tn_entries; /* (m) */
/* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
/*
* 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;
* and faster, as we do not need to convert between two types.
*/
enum vtype tn_type; /* (c) */
/* Node identifier. */
ino_t tn_id;
ino_t tn_id; /* (c) */
/* Node's internal status. This is used by several file system
/*
* 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;
* fashion.
*/
int tn_status; /* (vi) */
#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;
/*
* The node size. It does not necessarily match the real amount
* of memory consumed by it.
*/
off_t tn_size; /* (v) */
/* Generic node attributes. */
uid_t tn_uid;
gid_t tn_gid;
mode_t tn_mode;
u_long 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;
uid_t tn_uid; /* (v) */
gid_t tn_gid; /* (v) */
mode_t tn_mode; /* (v) */
u_long tn_flags; /* (v) */
nlink_t tn_links; /* (v) */
struct timespec tn_atime; /* (vi) */
struct timespec tn_mtime; /* (vi) */
struct timespec tn_ctime; /* (vi) */
struct timespec tn_birthtime; /* (v) */
unsigned long tn_gen; /* (c) */
/* As there is a single vnode for each active file within the
/*
* 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.
@ -196,70 +215,81 @@ struct tmpfs_node {
* tn_vnode.
*
* May be NULL when the node is unused (that is, no vnode has been
* allocated for it or it has been reclaimed). */
struct vnode * tn_vnode;
* allocated for it or it has been reclaimed).
*/
struct vnode * tn_vnode; /* (i) */
/* Interlock to protect tn_vpstate, and tn_status under shared
/*
* Interlock to protect tn_vpstate, and tn_status under shared
* vnode lock.
*/
struct mtx tn_interlock;
/* Identify if current node has vnode assiocate with
/*
* Identify if current node has vnode assiocate with
* or allocating vnode.
*/
int tn_vpstate;
int tn_vpstate; /* (i) */
/* misc data field for different tn_type node */
union {
/* Valid when tn_type == VBLK || tn_type == VCHR. */
dev_t tn_rdev;
dev_t tn_rdev; /* (c) */
/* Valid when tn_type == VDIR. */
struct tn_dir {
/* Pointer to the parent directory. The root
/*
* Pointer to the parent directory. The root
* directory has a pointer to itself in this field;
* this property identifies the root node. */
* this property identifies the root node.
*/
struct tmpfs_node * tn_parent;
/* Head of a tree that links the contents of
* the directory together. */
/*
* Head of a tree that links the contents of
* the directory together.
*/
struct tmpfs_dir tn_dirhead;
/* Head of a list the contains fake directory entries
/*
* Head of a list the contains fake directory entries
* heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
* flag. */
* flag.
*/
struct tmpfs_dir_duphead tn_dupindex;
/* Number and pointer of the first directory entry
/*
* 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. */
* Otherwise, these values are discarded.
*/
off_t tn_readdir_lastn;
struct tmpfs_dirent * tn_readdir_lastp;
} tn_dir;
/* Valid when tn_type == VLNK. */
/* The link's target, allocated from a string pool. */
char * tn_link;
char * tn_link; /* (c) */
/* 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. */
vm_object_t tn_aobj;
}tn_reg;
}tn_spec;
/*
* 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. It is a task
* of the memory management subsystem to issue
* the required page ins or page outs whenever
* a position within the file is accessed.
*/
vm_object_t tn_aobj; /* (c) */
} tn_reg;
} tn_spec; /* (v) */
};
LIST_HEAD(tmpfs_node_list, tmpfs_node);
@ -303,26 +333,34 @@ LIST_HEAD(tmpfs_node_list, tmpfs_node);
* Internal representation of a tmpfs mount point.
*/
struct tmpfs_mount {
/* Maximum number of memory pages available for use by the file
/*
* Maximum number of memory pages available for use by the file
* system, set during mount time. This variable must never be
* used directly as it may be bigger than the current amount of
* free memory; in the extreme case, it will hold the ULONG_MAX
* value. */
* value.
*/
u_long tm_pages_max;
/* Number of pages in use by the file system. */
u_long tm_pages_used;
/* Pointer to the node representing the root directory of this
* file system. */
/*
* Pointer to the node representing the root directory of this
* file system.
*/
struct tmpfs_node * tm_root;
/* Maximum number of possible nodes for this file system; set
struct mount * tm_mnt;
/*
* Maximum number of possible nodes for this file system; set
* during mount time. We need a hard limit on the maximum number
* of nodes to avoid allocating too much of them; their objects
* cannot be released until the file system is unmounted.
* Otherwise, we could easily run out of memory by creating lots
* of empty files and then simply removing them. */
* of empty files and then simply removing them.
*/
ino_t tm_nodes_max;
/* unrhdr used to allocate inode numbers */
@ -334,27 +372,16 @@ struct tmpfs_mount {
/* maximum representable file size */
u_int64_t tm_maxfilesize;
/* Nodes are organized in two different lists. The used list
* contains all nodes that are currently used by the file system;
* i.e., they refer to existing files. The available list contains
* all nodes that are currently available for use by new files.
* Nodes must be kept in this list (instead of deleting them)
* because we need to keep track of their generation number (tn_gen
* field).
*
* Note that nodes are lazily allocated: if the available list is
* empty and we have enough space to create more nodes, they will be
* created and inserted in the used list. Once these are released,
* they will go into the available list, remaining alive until the
* file system is unmounted. */
/*
* The used list contains all nodes that are currently used by
* the file system; i.e., they refer to existing files.
*/
struct tmpfs_node_list tm_nodes_used;
/* All node lock to protect the node list and tmp_pages_used */
/* All node lock to protect the node list and tmp_pages_used. */
struct mtx allnode_lock;
/* Pools used to store file system meta data. These are not shared
* across several instances of tmpfs for the reasons described in
* tmpfs_pool.c. */
/* Zones used to store file system meta data, per tmpfs mount. */
uma_zone_t tm_dirent_pool;
uma_zone_t tm_node_pool;
@ -443,10 +470,6 @@ int tmpfs_truncate(struct vnode *, off_t);
MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
} while (0)
/*
* Memory management stuff.
*/
/*
* Amount of memory pages to reserve for the system (e.g., to not use by
* tmpfs).
@ -464,37 +487,34 @@ size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
* specific ones.
*/
static inline
struct tmpfs_mount *
static inline struct tmpfs_mount *
VFS_TO_TMPFS(struct mount *mp)
{
struct tmpfs_mount *tmp;
MPASS((mp) != NULL && (mp)->mnt_data != NULL);
tmp = (struct tmpfs_mount *)(mp)->mnt_data;
return tmp;
MPASS(mp != NULL && mp->mnt_data != NULL);
tmp = (struct tmpfs_mount *)mp->mnt_data;
return (tmp);
}
static inline
struct tmpfs_node *
static inline struct tmpfs_node *
VP_TO_TMPFS_NODE(struct vnode *vp)
{
struct tmpfs_node *node;
MPASS((vp) != NULL && (vp)->v_data != NULL);
MPASS(vp != NULL && vp->v_data != NULL);
node = (struct tmpfs_node *)vp->v_data;
return node;
return (node);
}
static inline
struct tmpfs_node *
static inline struct tmpfs_node *
VP_TO_TMPFS_DIR(struct vnode *vp)
{
struct tmpfs_node *node;
node = VP_TO_TMPFS_NODE(vp);
TMPFS_VALIDATE_DIR(node);
return node;
return (node);
}
#endif /* _FS_TMPFS_TMPFS_H_ */

View File

@ -198,8 +198,8 @@ tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
return (EBUSY);
}
nnode = (struct tmpfs_node *)uma_zalloc_arg(
tmp->tm_node_pool, tmp, M_WAITOK);
nnode = (struct tmpfs_node *)uma_zalloc_arg(tmp->tm_node_pool, tmp,
M_WAITOK);
/* Generic initialization. */
nnode->tn_type = type;
@ -257,7 +257,8 @@ tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
break;
default:
panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
panic("tmpfs_alloc_node: type %p %d", nnode,
(int)nnode->tn_type);
}
TMPFS_LOCK(tmp);
@ -266,25 +267,12 @@ tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
TMPFS_UNLOCK(tmp);
*node = nnode;
return 0;
return (0);
}
/*
* Destroys the node pointed to by node from the file system 'tmp'.
* If the node does not belong to the given mount point, the results are
* unpredicted.
*
* If the node references a directory; no entries are allowed because
* their removal could need a recursive algorithm, something forbidden in
* kernel space. Furthermore, there is not need to provide such
* functionality (recursive removal) because the only primitives offered
* to the user are the removal of empty directories and the deletion of
* individual files.
*
* Note that nodes are not really deleted; in fact, when a node has been
* allocated, it cannot be deleted during the whole life of the file
* system. Instead, they are moved to the available list and remain there
* until reused.
* If the node references a directory, no entries are allowed.
*/
void
tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
@ -609,7 +597,7 @@ loop1:
VN_LOCK_ASHARE(vp);
error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
if (error)
if (error != 0)
vp = NULL;
unlock:
@ -638,7 +626,7 @@ out:
}
#endif
return error;
return (error);
}
/*
@ -706,8 +694,8 @@ tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
/* Allocate a node that represents the new file. */
error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type,
cnp->cn_cred->cr_uid,
dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent,
target, vap->va_rdev, &node);
if (error != 0)
return (error);
@ -1115,9 +1103,8 @@ tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
* Return ENOENT if the current node is already removed.
*/
TMPFS_ASSERT_LOCKED(node);
if (node->tn_dir.tn_parent == NULL) {
if (node->tn_dir.tn_parent == NULL)
return (ENOENT);
}
TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
dent.d_fileno = node->tn_dir.tn_parent->tn_id;

View File

@ -190,7 +190,7 @@ tmpfs_mount(struct mount *mp)
/* Do not allow mounts if we do not have enough memory to preserve
* the minimum reserved pages. */
if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
return ENOSPC;
return (ENOSPC);
/* Get the maximum number of memory pages this file system is
* allowed to use, based on the maximum size the user passed in
@ -229,27 +229,23 @@ tmpfs_mount(struct mount *mp)
tmp->tm_pages_used = 0;
tmp->tm_ino_unr = new_unrhdr(2, INT_MAX, &tmp->allnode_lock);
tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
sizeof(struct tmpfs_dirent),
NULL, NULL, NULL, NULL,
sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
UMA_ALIGN_PTR, 0);
tmp->tm_node_pool = uma_zcreate("TMPFS node",
sizeof(struct tmpfs_node),
tmpfs_node_ctor, tmpfs_node_dtor,
tmpfs_node_init, tmpfs_node_fini,
UMA_ALIGN_PTR, 0);
sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
/* Allocate the root node. */
error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid,
root_gid, root_mode & ALLPERMS, NULL, NULL,
VNOVAL, &root);
error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid,
root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root);
if (error != 0 || root == NULL) {
uma_zdestroy(tmp->tm_node_pool);
uma_zdestroy(tmp->tm_dirent_pool);
delete_unrhdr(tmp->tm_ino_unr);
free(tmp, M_TMPFSMNT);
return error;
uma_zdestroy(tmp->tm_node_pool);
uma_zdestroy(tmp->tm_dirent_pool);
delete_unrhdr(tmp->tm_ino_unr);
free(tmp, M_TMPFSMNT);
return (error);
}
KASSERT(root->tn_id == 2,
("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
@ -340,12 +336,11 @@ static int
tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
{
int error;
error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
if (!error)
if (error == 0)
(*vpp)->v_vflag |= VV_ROOT;
return error;
return (error);
}
static int

View File

@ -117,10 +117,12 @@ tmpfs_lookup(struct vop_cachedlookup_args *v)
if (de != NULL && de->td_node == NULL)
cnp->cn_flags |= ISWHITEOUT;
if (de == NULL || de->td_node == NULL) {
/* The entry was not found in the directory.
/*
* The entry was not found in the directory.
* This is OK if we are creating or renaming an
* entry and are working on the last component of
* the path name. */
* the path name.
*/
if ((cnp->cn_flags & ISLASTCN) &&
(cnp->cn_nameiop == CREATE || \
cnp->cn_nameiop == RENAME ||
@ -132,8 +134,10 @@ tmpfs_lookup(struct vop_cachedlookup_args *v)
if (error != 0)
goto out;
/* Keep the component name in the buffer for
* future uses. */
/*
* Keep the component name in the buffer for
* future uses.
*/
cnp->cn_flags |= SAVENAME;
error = EJUSTRETURN;
@ -142,14 +146,18 @@ tmpfs_lookup(struct vop_cachedlookup_args *v)
} else {
struct tmpfs_node *tnode;
/* The entry was found, so get its associated
* tmpfs_node. */
/*
* The entry was found, so get its associated
* tmpfs_node.
*/
tnode = de->td_node;
/* If we are not at the last path component and
/*
* If we are not at the last path component and
* found a non-directory or non-link entry (which
* may itself be pointing to a directory), raise
* an error. */
* an error.
*/
if ((tnode->tn_type != VDIR &&
tnode->tn_type != VLNK) &&
!(cnp->cn_flags & ISLASTCN)) {
@ -157,9 +165,11 @@ tmpfs_lookup(struct vop_cachedlookup_args *v)
goto out;
}
/* If we are deleting or renaming the entry, keep
/*
* If we are deleting or renaming the entry, keep
* track of its tmpfs_dirent so that it can be
* easily deleted later. */
* easily deleted later.
*/
if ((cnp->cn_flags & ISLASTCN) &&
(cnp->cn_nameiop == DELETE ||
cnp->cn_nameiop == RENAME)) {
@ -175,8 +185,9 @@ tmpfs_lookup(struct vop_cachedlookup_args *v)
goto out;
if ((dnode->tn_mode & S_ISTXT) &&
VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
cnp->cn_cred, cnp->cn_thread)) {
error = EPERM;
vput(*vpp);
*vpp = NULL;
@ -192,18 +203,22 @@ tmpfs_lookup(struct vop_cachedlookup_args *v)
}
}
/* Store the result of this lookup in the cache. Avoid this if the
/*
* Store the result of this lookup in the cache. Avoid this if the
* request was for creation, as it does not improve timings on
* emprical tests. */
* emprical tests.
*/
if ((cnp->cn_flags & MAKEENTRY) != 0)
cache_enter(dvp, *vpp, cnp);
out:
/* If there were no errors, *vpp cannot be null and it must be
* locked. */
/*
* If there were no errors, *vpp cannot be null and it must be
* locked.
*/
MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
return error;
return (error);
}
static int
@ -1390,7 +1405,7 @@ tmpfs_whiteout(struct vop_whiteout_args *ap)
}
/*
* vnode operations vector used for files stored in a tmpfs file system.
* Vnode operations vector used for files stored in a tmpfs file system.
*/
struct vop_vector tmpfs_vnodeop_entries = {
.vop_default = &default_vnodeops,