aboutsummaryrefslogtreecommitdiffstats
path: root/sbin/geom/misc
diff options
context:
space:
mode:
authorPawel Jakub Dawidek <pjd@FreeBSD.org>2010-09-23 12:04:12 +0000
committerPawel Jakub Dawidek <pjd@FreeBSD.org>2010-09-23 12:04:12 +0000
commit7157a8ad7d7530ff57e13c95c581218f1dca4212 (patch)
treedeb94a01b1305b30e040db2b8fb2665958ab554a /sbin/geom/misc
parentcdf8ab6198b3d67b46ae7f65c2537e5462692d7c (diff)
downloadsrc-7157a8ad7d7530ff57e13c95c581218f1dca4212.tar.gz
src-7157a8ad7d7530ff57e13c95c581218f1dca4212.zip
- Simplify code by using g_*() API.
- Don't use u_char and u_int in userland. - Change 'unsigned' to 'unsigned int'. - Update copyright years. MFC after: 1 week
Notes
Notes: svn path=/head/; revision=213074
Diffstat (limited to 'sbin/geom/misc')
-rw-r--r--sbin/geom/misc/subr.c133
-rw-r--r--sbin/geom/misc/subr.h12
2 files changed, 62 insertions, 83 deletions
diff --git a/sbin/geom/misc/subr.c b/sbin/geom/misc/subr.c
index 5c6afe649265..1409eba751cb 100644
--- a/sbin/geom/misc/subr.c
+++ b/sbin/geom/misc/subr.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
+ * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -54,30 +54,20 @@ struct std_metadata {
};
static void
-std_metadata_decode(const u_char *data, struct std_metadata *md)
+std_metadata_decode(const unsigned char *data, struct std_metadata *md)
{
bcopy(data, md->md_magic, sizeof(md->md_magic));
md->md_version = le32dec(data + 16);
}
-static void
-pathgen(const char *name, char *path, size_t size)
-{
-
- if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) != 0)
- snprintf(path, size, "%s%s", _PATH_DEV, name);
- else
- strlcpy(path, name, size);
-}
-
/*
* Greatest Common Divisor.
*/
-static unsigned
-gcd(unsigned a, unsigned b)
+static unsigned int
+gcd(unsigned int a, unsigned int b)
{
- u_int c;
+ unsigned int c;
while (b != 0) {
c = a;
@@ -90,8 +80,8 @@ gcd(unsigned a, unsigned b)
/*
* Least Common Multiple.
*/
-unsigned
-g_lcm(unsigned a, unsigned b)
+unsigned int
+g_lcm(unsigned int a, unsigned int b)
{
return ((a * b) / gcd(a, b));
@@ -129,7 +119,7 @@ bitcount32(uint32_t x)
*
*/
int
-g_parse_lba(const char *lbastr, unsigned sectorsize, off_t *sectors)
+g_parse_lba(const char *lbastr, unsigned int sectorsize, off_t *sectors)
{
off_t number, mult, unit;
char *s;
@@ -200,76 +190,69 @@ done:
off_t
g_get_mediasize(const char *name)
{
- char path[MAXPATHLEN];
off_t mediasize;
int fd;
- pathgen(name, path, sizeof(path));
- fd = open(path, O_RDONLY);
+ fd = g_open(name, 0);
if (fd == -1)
return (0);
- if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
- close(fd);
- return (0);
- }
- close(fd);
+ mediasize = g_mediasize(fd);
+ if (mediasize == -1)
+ mediasize = 0;
+ (void)g_close(fd);
return (mediasize);
}
-unsigned
+unsigned int
g_get_sectorsize(const char *name)
{
- char path[MAXPATHLEN];
- unsigned sectorsize;
+ ssize_t sectorsize;
int fd;
- pathgen(name, path, sizeof(path));
- fd = open(path, O_RDONLY);
+ fd = g_open(name, 0);
if (fd == -1)
return (0);
- if (ioctl(fd, DIOCGSECTORSIZE, &sectorsize) < 0) {
- close(fd);
- return (0);
- }
- close(fd);
- return (sectorsize);
+ sectorsize = g_sectorsize(fd);
+ if (sectorsize == -1)
+ sectorsize = 0;
+ (void)g_close(fd);
+ return ((unsigned int)sectorsize);
}
int
-g_metadata_read(const char *name, u_char *md, size_t size, const char *magic)
+g_metadata_read(const char *name, unsigned char *md, size_t size,
+ const char *magic)
{
struct std_metadata stdmd;
- char path[MAXPATHLEN];
- unsigned sectorsize;
+ unsigned char *sector;
+ ssize_t sectorsize;
off_t mediasize;
- u_char *sector;
int error, fd;
- pathgen(name, path, sizeof(path));
sector = NULL;
error = 0;
- fd = open(path, O_RDONLY);
+ fd = g_open(name, 0);
if (fd == -1)
return (errno);
- mediasize = g_get_mediasize(name);
- if (mediasize == 0) {
+ mediasize = g_mediasize(fd);
+ if (mediasize == -1) {
error = errno;
goto out;
}
- sectorsize = g_get_sectorsize(name);
- if (sectorsize == 0) {
+ sectorsize = g_sectorsize(fd);
+ if (sectorsize == -1) {
error = errno;
goto out;
}
- assert(sectorsize >= size);
+ assert(sectorsize >= (ssize_t)size);
sector = malloc(sectorsize);
if (sector == NULL) {
error = ENOMEM;
goto out;
}
if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
- (ssize_t)sectorsize) {
+ sectorsize) {
error = errno;
goto out;
}
@@ -284,37 +267,35 @@ g_metadata_read(const char *name, u_char *md, size_t size, const char *magic)
out:
if (sector != NULL)
free(sector);
- close(fd);
+ g_close(fd);
return (error);
}
int
-g_metadata_store(const char *name, u_char *md, size_t size)
+g_metadata_store(const char *name, const unsigned char *md, size_t size)
{
- char path[MAXPATHLEN];
- unsigned sectorsize;
+ unsigned char *sector;
+ ssize_t sectorsize;
off_t mediasize;
- u_char *sector;
int error, fd;
- pathgen(name, path, sizeof(path));
sector = NULL;
error = 0;
- fd = open(path, O_WRONLY);
+ fd = g_open(name, 1);
if (fd == -1)
return (errno);
- mediasize = g_get_mediasize(name);
- if (mediasize == 0) {
+ mediasize = g_mediasize(fd);
+ if (mediasize == -1) {
error = errno;
goto out;
}
- sectorsize = g_get_sectorsize(name);
- if (sectorsize == 0) {
+ sectorsize = g_sectorsize(fd);
+ if (sectorsize == -1) {
error = errno;
goto out;
}
- assert(sectorsize >= size);
+ assert(sectorsize >= (ssize_t)size);
sector = malloc(sectorsize);
if (sector == NULL) {
error = ENOMEM;
@@ -322,15 +303,15 @@ g_metadata_store(const char *name, u_char *md, size_t size)
}
bcopy(md, sector, size);
if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
- (ssize_t)sectorsize) {
+ sectorsize) {
error = errno;
goto out;
}
- (void)ioctl(fd, DIOCGFLUSH, NULL);
+ (void)g_flush(fd);
out:
if (sector != NULL)
free(sector);
- close(fd);
+ (void)g_close(fd);
return (error);
}
@@ -338,25 +319,23 @@ int
g_metadata_clear(const char *name, const char *magic)
{
struct std_metadata md;
- char path[MAXPATHLEN];
- unsigned sectorsize;
+ unsigned char *sector;
+ ssize_t sectorsize;
off_t mediasize;
- u_char *sector;
int error, fd;
- pathgen(name, path, sizeof(path));
sector = NULL;
error = 0;
- fd = open(path, O_RDWR);
+ fd = g_open(name, 1);
if (fd == -1)
return (errno);
- mediasize = g_get_mediasize(name);
+ mediasize = g_mediasize(fd);
if (mediasize == 0) {
error = errno;
goto out;
}
- sectorsize = g_get_sectorsize(name);
+ sectorsize = g_sectorsize(fd);
if (sectorsize == 0) {
error = errno;
goto out;
@@ -368,7 +347,7 @@ g_metadata_clear(const char *name, const char *magic)
}
if (magic != NULL) {
if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
- (ssize_t)sectorsize) {
+ sectorsize) {
error = errno;
goto out;
}
@@ -380,15 +359,15 @@ g_metadata_clear(const char *name, const char *magic)
}
bzero(sector, sectorsize);
if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
- (ssize_t)sectorsize) {
+ sectorsize) {
error = errno;
goto out;
}
- (void)ioctl(fd, DIOCGFLUSH, NULL);
+ (void)g_flush(fd);
out:
if (sector != NULL)
free(sector);
- close(fd);
+ g_close(fd);
return (error);
}
@@ -412,8 +391,8 @@ gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
{
struct gctl_req_arg *argp;
char param[256];
+ unsigned int i;
void *p;
- unsigned i;
vsnprintf(param, sizeof(param), pfmt, ap);
for (i = 0; i < req->narg; i++) {
@@ -486,7 +465,7 @@ gctl_change_param(struct gctl_req *req, const char *name, int len,
const void *value)
{
struct gctl_req_arg *ap;
- unsigned i;
+ unsigned int i;
if (req == NULL || req->error != NULL)
return (EDOOFUS);
diff --git a/sbin/geom/misc/subr.h b/sbin/geom/misc/subr.h
index 2b43bdb60129..5ad1269f84d9 100644
--- a/sbin/geom/misc/subr.h
+++ b/sbin/geom/misc/subr.h
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
+ * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -30,16 +30,16 @@
#define _SUBR_H_
#include <stdint.h>
-unsigned g_lcm(unsigned a, unsigned b);
+unsigned int g_lcm(unsigned int a, unsigned int b);
uint32_t bitcount32(uint32_t x);
-int g_parse_lba(const char *lbastr, unsigned sectorsize, off_t *sectors);
+int g_parse_lba(const char *lbastr, unsigned int sectorsize, off_t *sectors);
off_t g_get_mediasize(const char *name);
-unsigned g_get_sectorsize(const char *name);
+unsigned int g_get_sectorsize(const char *name);
-int g_metadata_read(const char *name, u_char *md, size_t size,
+int g_metadata_read(const char *name, unsigned char *md, size_t size,
const char *magic);
-int g_metadata_store(const char *name, u_char *md, size_t size);
+int g_metadata_store(const char *name, const unsigned char *md, size_t size);
int g_metadata_clear(const char *name, const char *magic);
void gctl_error(struct gctl_req *req, const char *error, ...) __printflike(2, 3);