1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

o dm_destroy_all() called on exit - but doesn't touch suspended devices yet.

o 'dmsetup remove_all' calls dm_destroy_all() to provide a quick way to
  prepare for unloading the module
o Ran through indent again.
This commit is contained in:
Alasdair Kergon 2002-03-07 20:56:10 +00:00
parent 11d2da4036
commit e09a7b5787
3 changed files with 66 additions and 68 deletions

View File

@ -43,8 +43,7 @@ void dm_task_destroy(struct dm_task *dmt)
free(dmt);
}
int dm_task_get_driver_version(struct dm_task *dmt, char *version,
size_t size)
int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size)
{
if (!dmt->dmi)
return 0;
@ -97,13 +96,12 @@ int dm_task_set_newname(struct dm_task *dmt, const char *newname)
}
struct target *create_target(uint64_t start,
uint64_t len,
const char *type, const char *params)
uint64_t len, const char *type, const char *params)
{
struct target *t = malloc(sizeof(*t));
if (!t) {
log_error("create_target: malloc(%d) failed", sizeof(*t));
log_error("create_target: malloc(%d) failed", sizeof(*t));
return NULL;
}
@ -123,7 +121,7 @@ struct target *create_target(uint64_t start,
t->length = len;
return t;
bad:
bad:
free(t->params);
free(t->type);
free(t);
@ -241,7 +239,7 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt)
return dmi;
bad:
bad:
free(dmi);
return NULL;
}
@ -278,6 +276,10 @@ int dm_task_run(struct dm_task *dmt)
command = DM_REMOVE;
break;
case DM_DEVICE_REMOVE_ALL:
command = DM_REMOVE_ALL;
break;
case DM_DEVICE_SUSPEND:
command = DM_SUSPEND;
break;
@ -304,13 +306,13 @@ int dm_task_run(struct dm_task *dmt)
default:
log_error("Internal error: unknown device-mapper task %d",
dmt->type);
dmt->type);
goto bad;
}
if (ioctl(fd, command, dmi) < 0) {
log_error("device-mapper ioctl cmd %d failed: %s", dmt->type,
strerror(errno));
strerror(errno));
goto bad;
}
@ -328,10 +330,9 @@ int dm_task_run(struct dm_task *dmt)
close(fd);
return 1;
bad:
bad:
free(dmi);
if (fd >= 0)
close(fd);
return 0;
}

View File

@ -20,9 +20,8 @@
* each ioctl command you want to execute.
*/
typedef void (*dm_log_fn)(int level, const char *file, int line,
const char *f, ...);
typedef void (*dm_log_fn) (int level, const char *file, int line,
const char *f, ...);
/*
* The library user may wish to register their own
@ -35,6 +34,7 @@ enum {
DM_DEVICE_CREATE,
DM_DEVICE_RELOAD,
DM_DEVICE_REMOVE,
DM_DEVICE_REMOVE_ALL,
DM_DEVICE_SUSPEND,
DM_DEVICE_RESUME,
@ -46,7 +46,6 @@ enum {
DM_DEVICE_VERSION,
};
struct dm_task;
struct dm_task *dm_task_create(int type);
@ -74,8 +73,7 @@ struct dm_deps {
};
int dm_get_library_version(char *version, size_t size);
int dm_task_get_driver_version(struct dm_task *dmt, char *version,
size_t size);
int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size);
int dm_task_get_info(struct dm_task *dmt, struct dm_info *dmi);
struct dm_deps *dm_task_get_deps(struct dm_task *dmt);
@ -89,9 +87,7 @@ int dm_task_set_minor(struct dm_task *dmt, int minor);
*/
int dm_task_add_target(struct dm_task *dmt,
uint64_t start,
uint64_t size,
const char *ttype,
const char *params);
uint64_t size, const char *ttype, const char *params);
/*
* Call this to actually run the ioctl.
@ -104,4 +100,4 @@ int dm_task_run(struct dm_task *dmt);
int dm_set_dev_dir(const char *dir);
const char *dm_dir(void);
#endif /* LIB_DEVICE_MAPPER_H */
#endif /* LIB_DEVICE_MAPPER_H */

View File

@ -36,23 +36,22 @@ enum {
static int _switches[NUM_SWITCHES];
static int _values[NUM_SWITCHES];
/*
* Commands
*/
static int _parse_file(struct dm_task *dmt, const char *file)
{
char buffer[LINE_SIZE], *ttype, *ptr, *comment;
FILE *fp = fopen(file, "r");
unsigned long long start, size;
int r = 0, n, line = 0;
char buffer[LINE_SIZE], *ttype, *ptr, *comment;
FILE *fp = fopen(file, "r");
unsigned long long start, size;
int r = 0, n, line = 0;
if (!fp) {
err("Couldn't open '%s' for reading", file);
return 0;
}
if (!fp) {
err("Couldn't open '%s' for reading", file);
return 0;
}
while (fgets(buffer, sizeof(buffer), fp)) {
while (fgets(buffer, sizeof(buffer), fp)) {
line++;
/* trim trailing space */
@ -63,32 +62,31 @@ static int _parse_file(struct dm_task *dmt, const char *file)
*ptr = '\0';
/* trim leading space */
for (ptr = buffer; *ptr && isspace((int) *ptr); ptr++)
;
for (ptr = buffer; *ptr && isspace((int) *ptr); ptr++) ;
if (!*ptr || *ptr == '#')
continue;
if (!*ptr || *ptr == '#')
continue;
if (sscanf(ptr, "%llu %llu %as %n",
&start, &size, &ttype, &n) < 3) {
err("%s:%d Invalid format", file, line);
goto out;
}
if (sscanf(ptr, "%llu %llu %as %n",
&start, &size, &ttype, &n) < 3) {
err("%s:%d Invalid format", file, line);
goto out;
}
ptr += n;
if ((comment = strchr(ptr, (int) '#')))
*comment = '\0';
if (!dm_task_add_target(dmt, start, size, ttype, ptr))
goto out;
if (!dm_task_add_target(dmt, start, size, ttype, ptr))
goto out;
free(ttype);
}
r = 1;
}
r = 1;
out:
fclose(fp);
return r;
out:
fclose(fp);
return r;
}
static int _load(int task, const char *name, const char *file)
@ -116,7 +114,7 @@ static int _load(int task, const char *name, const char *file)
r = 1;
out:
out:
dm_task_destroy(dmt);
return r;
@ -137,24 +135,24 @@ static int _rename(int argc, char **argv)
int r = 0;
struct dm_task *dmt;
if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
return 0;
if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
return 0;
if (!dm_task_set_name(dmt, argv[1]))
goto out;
if (!dm_task_set_name(dmt, argv[1]))
goto out;
if (!dm_task_set_newname(dmt, argv[2]))
goto out;
if (!dm_task_run(dmt))
goto out;
if (!dm_task_run(dmt))
goto out;
r = 1;
r = 1;
out:
dm_task_destroy(dmt);
out:
dm_task_destroy(dmt);
return r;
return r;
}
static int _version(int argc, char **argv)
@ -172,7 +170,7 @@ static int _version(int argc, char **argv)
if (!dm_task_run(dmt))
goto out;
if (!dm_task_get_driver_version(dmt, (char *)&version,
if (!dm_task_get_driver_version(dmt, (char *) &version,
sizeof(version)))
goto out;
@ -201,11 +199,16 @@ static int _simple(int task, const char *name)
r = dm_task_run(dmt);
out:
out:
dm_task_destroy(dmt);
return r;
}
static int _remove_all(int argc, char **argv)
{
return _simple(DM_DEVICE_REMOVE_ALL, "");
}
static int _remove(int argc, char **argv)
{
return _simple(DM_DEVICE_REMOVE, argv[1]);
@ -260,7 +263,7 @@ static int _info(int argc, char **argv)
r = 1;
out:
out:
dm_task_destroy(dmt);
return r;
}
@ -305,16 +308,15 @@ static int _deps(int argc, char **argv)
r = 1;
out:
out:
dm_task_destroy(dmt);
return r;
}
/*
* dispatch table
*/
typedef int (*command_fn)(int argc, char **argv);
typedef int (*command_fn) (int argc, char **argv);
struct command {
char *name;
@ -326,6 +328,7 @@ struct command {
static struct command _commands[] = {
{"create", "<dev_name> <table_file>", 2, _create},
{"remove", "<dev_name>", 1, _remove},
{"remove_all", "", 0, _remove_all},
{"suspend", "<dev_name>", 1, _suspend},
{"resume", "<dev_name>", 1, _resume},
{"reload", "<dev_name> <table_file>", 2, _reload},
@ -336,14 +339,13 @@ static struct command _commands[] = {
{NULL, NULL, 0, NULL}
};
static void _usage(FILE *out)
static void _usage(FILE * out)
{
int i;
fprintf(out, "usage:\n");
for (i = 0; _commands[i].name; i++)
fprintf(out, "\t%s %s\n",
_commands[i].name, _commands[i].help);
fprintf(out, "\t%s %s\n", _commands[i].name, _commands[i].help);
return;
}
@ -423,4 +425,3 @@ int main(int argc, char **argv)
return 0;
}