- Remove unnecessary NULL checks after M_WAITOK allocations.
 - Use VOP_ACCESS instead of hand-rolled suser_cred()
   calls. [1]
 - Use malloc(9) KPI to allocate memory for string.  The
   optimization taken from NetBSD is not valid for FreeBSD
   because our malloc(9) already act that way. [2]

Requested by:	rwatson [1]
Submitted by:	Howard Su [2]
Approved by:	re (tmpfs blanket)
This commit is contained in:
Xin LI 2007-06-29 05:23:15 +00:00
parent 56083486d5
commit 9b258fca27
8 changed files with 21 additions and 190 deletions

View File

@ -1206,7 +1206,6 @@ fs/tmpfs/tmpfs_vnops.c optional tmpfs
fs/tmpfs/tmpfs_fifoops.c optional tmpfs
fs/tmpfs/tmpfs_vfsops.c optional tmpfs
fs/tmpfs/tmpfs_subr.c optional tmpfs
fs/tmpfs/tmpfs_uma.c optional tmpfs
gdb/gdb_cons.c optional gdb
gdb/gdb_main.c optional gdb
gdb/gdb_packet.c optional gdb

View File

@ -60,8 +60,7 @@
#include <vm/swap_pager.h>
MALLOC_DECLARE(M_TMPFSMNT);
#include <fs/tmpfs/tmpfs_uma.h>
MALLOC_DECLARE(M_TMPFSNAME);
/* --------------------------------------------------------------------- */
@ -323,7 +322,6 @@ struct tmpfs_mount {
* tmpfs_pool.c. */
uma_zone_t tm_dirent_pool;
uma_zone_t tm_node_pool;
struct tmpfs_str_zone tm_str_pool;
};
#define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock)
#define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock)

View File

@ -105,8 +105,6 @@ tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
nnode = (struct tmpfs_node *)uma_zalloc_arg(
tmp->tm_node_pool, tmp, M_WAITOK);
if (nnode == NULL)
return (ENOSPC);
/* Generic initialization. */
nnode->tn_type = type;
@ -141,13 +139,8 @@ tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
case VLNK:
MPASS(strlen(target) < MAXPATHLEN);
nnode->tn_size = strlen(target);
nnode->tn_link = tmpfs_str_zone_alloc(&tmp->tm_str_pool,
M_WAITOK, nnode->tn_size);
if (nnode->tn_link == NULL) {
nnode->tn_type = VNON;
uma_zfree(tmp->tm_node_pool, nnode);
return ENOSPC;
}
nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
M_WAITOK);
memcpy(nnode->tn_link, target, nnode->tn_size);
break;
@ -217,8 +210,7 @@ tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
break;
case VLNK:
tmpfs_str_zone_free(&tmp->tm_str_pool, node->tn_link,
node->tn_size);
free(node->tn_link, M_TMPFSNAME);
break;
case VREG:
@ -259,14 +251,7 @@ tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
nde = (struct tmpfs_dirent *)uma_zalloc(
tmp->tm_dirent_pool, M_WAITOK);
if (nde == NULL)
return ENOSPC;
nde->td_name = tmpfs_str_zone_alloc(&tmp->tm_str_pool, M_WAITOK, len);
if (nde->td_name == NULL) {
uma_zfree(tmp->tm_dirent_pool, nde);
return ENOSPC;
}
nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
nde->td_namelen = len;
memcpy(nde->td_name, name, len);
@ -302,7 +287,7 @@ tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
node->tn_links--;
}
tmpfs_str_zone_free(&tmp->tm_str_pool, de->td_name, de->td_namelen);
free(de->td_name, M_TMPFSNAME);
uma_zfree(tmp->tm_dirent_pool, de);
}
@ -1174,14 +1159,15 @@ tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
if (node->tn_flags & (IMMUTABLE | APPEND))
return EPERM;
/* XXX: The following comes from UFS code, and can be found in
* several other file systems. Shouldn't this be centralized
* somewhere? */
if (cred->cr_uid != node->tn_uid &&
(error = suser_cred(cred, 0)) &&
((vaflags & VA_UTIMES_NULL) == 0 ||
(error = VOP_ACCESS(vp, VWRITE, cred, l))))
return error;
/* Determine if the user have proper privilege to update time. */
if (vaflags & VA_UTIMES_NULL) {
error = VOP_ACCESS(vp, VADMIN, cred, l);
if (error)
error = VOP_ACCESS(vp, VWRITE, cred, l);
} else
error = VOP_ACCESS(vp, VADMIN, cred, l);
if (error)
return (error);
if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
node->tn_status |= TMPFS_NODE_ACCESSED;

View File

@ -1,61 +0,0 @@
/*-
* Copyright (c) 2007 Rohit Jalan (rohitj@purpe.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <vm/vm.h>
#include <fs/tmpfs/tmpfs.h>
void
tmpfs_str_zone_create(struct tmpfs_str_zone *tsz)
{
int i, len;
len = TMPFS_STRZONE_STARTLEN;
for (i = 0; i < TMPFS_STRZONE_ZONECOUNT; ++i) {
tsz->tsz_zone[i] = uma_zcreate(
"TMPFS str", len,
NULL, NULL, NULL, NULL,
UMA_ALIGN_PTR, 0);
len <<= 1;
}
}
void
tmpfs_str_zone_destroy(struct tmpfs_str_zone *tsz)
{
int i, len;
len = TMPFS_STRZONE_STARTLEN;
for (i = 0; i < TMPFS_STRZONE_ZONECOUNT; ++i) {
uma_zdestroy(tsz->tsz_zone[i]);
len <<= 1;
}
}

View File

@ -1,81 +0,0 @@
/*-
* Copyright (c) 2007 Rohit Jalan (rohitj@purpe.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _TMPFS_UMA_H
#define _TMPFS_UMA_H
#include <vm/uma.h>
#define TMPFS_STRZONE_ZONECOUNT 7
#define TMPFS_STRZONE_STARTLEN (1 << 4)
struct tmpfs_mount;
struct tmpfs_str_zone {
uma_zone_t tsz_zone[TMPFS_STRZONE_ZONECOUNT];
};
void tmpfs_str_zone_create(struct tmpfs_str_zone *);
void tmpfs_str_zone_destroy(struct tmpfs_str_zone *);
static __inline char*
tmpfs_str_zone_alloc(struct tmpfs_str_zone *tsz, int flags, size_t len)
{
size_t i, zlen;
char *ptr;
MPASS(len <= (TMPFS_STRZONE_STARTLEN << (TMPFS_STRZONE_ZONECOUNT-1)));
i = 0;
zlen = TMPFS_STRZONE_STARTLEN;
while (len > zlen) {
++i;
zlen <<= 1;
}
ptr = (char *)uma_zalloc(tsz->tsz_zone[i], flags);
return ptr;
}
static __inline void
tmpfs_str_zone_free(struct tmpfs_str_zone *tsz, char *item, size_t len)
{
size_t i, zlen;
MPASS(len <= (TMPFS_STRZONE_STARTLEN << (TMPFS_STRZONE_ZONECOUNT-1)));
i = 0;
zlen = TMPFS_STRZONE_STARTLEN;
while (len > zlen) {
++i;
zlen <<= 1;
}
uma_zfree(tsz->tsz_zone[i], item);
}
#endif

View File

@ -70,6 +70,7 @@ __FBSDID("$FreeBSD$");
#define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
/* --------------------------------------------------------------------- */
@ -276,8 +277,7 @@ tmpfs_mount(struct mount *mp, struct thread *l)
tmpfs_node_ctor, tmpfs_node_dtor,
tmpfs_node_init, tmpfs_node_fini,
UMA_ALIGN_PTR,
0);
tmpfs_str_zone_create(&tmp->tm_str_pool);
UMA_ZONE_NOFREE);
/* Allocate the root node. */
error = tmpfs_alloc_node(tmp, VDIR, args.ta_root_uid,
@ -285,7 +285,6 @@ tmpfs_mount(struct mount *mp, struct thread *l)
VNOVAL, l, &root);
if (error != 0 || root == NULL) {
tmpfs_str_zone_destroy(&tmp->tm_str_pool);
uma_zdestroy(tmp->tm_node_pool);
uma_zdestroy(tmp->tm_dirent_pool);
free(tmp, M_TMPFSMNT);
@ -358,7 +357,6 @@ tmpfs_unmount(struct mount *mp, int mntflags, struct thread *l)
uma_zdestroy(tmp->tm_dirent_pool);
uma_zdestroy(tmp->tm_node_pool);
tmpfs_str_zone_destroy(&tmp->tm_str_pool);
mtx_destroy(&tmp->allnode_lock);
MPASS(tmp->tm_pages_used == 0);

View File

@ -830,12 +830,7 @@ tmpfs_rename(struct vop_rename_args *v)
* has to be changed. */
if (fcnp->cn_namelen != tcnp->cn_namelen ||
memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
newname = tmpfs_str_zone_alloc(&tmp->tm_str_pool, M_WAITOK,
tcnp->cn_namelen);
if (newname == NULL) {
error = ENOSPC;
goto out_locked;
}
newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
} else
newname = NULL;
@ -855,8 +850,7 @@ tmpfs_rename(struct vop_rename_args *v)
if (n == fnode) {
error = EINVAL;
if (newname != NULL)
tmpfs_str_zone_free(&tmp->tm_str_pool,
newname, tcnp->cn_namelen);
free(newname, M_TMPFSNAME);
goto out_locked;
}
n = n->tn_dir.tn_parent;
@ -884,8 +878,7 @@ tmpfs_rename(struct vop_rename_args *v)
if (newname != NULL) {
MPASS(tcnp->cn_namelen <= MAXNAMLEN);
tmpfs_str_zone_free(&tmp->tm_str_pool, de->td_name,
de->td_namelen);
free(de->td_name, M_TMPFSNAME);
de->td_namelen = (uint16_t)tcnp->cn_namelen;
memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
de->td_name = newname;

View File

@ -4,7 +4,6 @@
KMOD= tmpfs
SRCS= vnode_if.h \
tmpfs_vnops.c tmpfs_fifoops.c tmpfs_vfsops.c tmpfs_subr.c \
tmpfs_uma.c
tmpfs_vnops.c tmpfs_fifoops.c tmpfs_vfsops.c tmpfs_subr.c
.include <bsd.kmod.mk>