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

Extract some common functions.

This commit is contained in:
Alasdair Kergon 2003-09-17 20:35:57 +00:00
parent 188b5fa337
commit aa37899857
10 changed files with 101 additions and 85 deletions

View File

@ -9,6 +9,7 @@
../lib/datastruct/hash.h ../lib/datastruct/hash.h
../lib/datastruct/list.h ../lib/datastruct/list.h
../lib/datastruct/lvm-types.h ../lib/datastruct/lvm-types.h
../lib/datastruct/str_list.h
../lib/device/dev-cache.h ../lib/device/dev-cache.h
../lib/device/device.h ../lib/device/device.h
../lib/display/display.h ../lib/display/display.h
@ -21,6 +22,7 @@
../lib/label/label.h ../lib/label/label.h
../lib/locking/locking.h ../lib/locking/locking.h
../lib/log/log.h ../lib/log/log.h
../lib/metadata/lv_alloc.h
../lib/metadata/metadata.h ../lib/metadata/metadata.h
../lib/mm/dbg_malloc.h ../lib/mm/dbg_malloc.h
../lib/mm/memlock.h ../lib/mm/memlock.h

View File

@ -20,6 +20,7 @@ SOURCES=\
datastruct/bitset.c \ datastruct/bitset.c \
datastruct/btree.c \ datastruct/btree.c \
datastruct/hash.c \ datastruct/hash.c \
datastruct/str_list.c \
device/dev-cache.c \ device/dev-cache.c \
device/dev-io.c \ device/dev-io.c \
device/device.c \ device/device.c \

View File

@ -5,6 +5,7 @@
*/ */
#include "lib.h" #include "lib.h"
#include "str_list.h"
#include "dev_manager.h" #include "dev_manager.h"
#include "pool.h" #include "pool.h"
#include "hash.h" #include "hash.h"
@ -155,33 +156,6 @@ static inline void _clear_flag(struct dev_layer *dl, int bit)
dl->flags &= ~(1 << bit); dl->flags &= ~(1 << bit);
} }
static int _pre_list_add(struct pool *mem, struct list *pl, const char *str)
{
struct str_list *sl;
struct list *plh;
if (!str) {
stack;
return 0;
}
/* Already in list? */
list_iterate(plh, pl) {
if (!strcmp(str, list_item(plh, struct str_list)->str))
return 1;
}
if (!(sl = pool_alloc(mem, sizeof(*sl)))) {
stack;
return 0;
}
sl->str = str;
list_add(pl, &sl->list);
return 1;
}
/* /*
* Device layer names are all of the form <vg>-<lv>-<layer>, any * Device layer names are all of the form <vg>-<lv>-<layer>, any
* other hyphens that appear in these names are quoted with yet * other hyphens that appear in these names are quoted with yet
@ -1016,7 +990,7 @@ static int _populate_snapshot(struct dev_manager *dm,
return 0; return 0;
} }
if (lvm_snprintf(params, sizeof(params), "%s %s P %d", if (lvm_snprintf(params, sizeof(params), "%s %s P %d",
devbufo, devbufc, s->chunk_size) == -1) { devbufo, devbufc, s->chunk_size) == -1) {
stack; stack;
return 0; return 0;
@ -1301,10 +1275,10 @@ static int _expand_vanilla(struct dev_manager *dm, struct logical_volume *lv,
for (s = 0; s < seg->area_count; s++) { for (s = 0; s < seg->area_count; s++) {
if (seg->area[s].type != AREA_LV) if (seg->area[s].type != AREA_LV)
continue; continue;
if (!_pre_list_add(dm->mem, &dl->pre_create, if (!str_list_add(dm->mem, &dl->pre_create,
_build_dlid(dm->mem, _build_dlid(dm->mem,
seg->area[s].u.lv. seg->area[s].u.lv.
lv->lvid.s, NULL))) { lv->lvid.s, NULL))) {
stack; stack;
return 0; return 0;
} }
@ -1327,8 +1301,8 @@ static int _expand_vanilla(struct dev_manager *dm, struct logical_volume *lv,
_set_flag(dlr, REMOVE); _set_flag(dlr, REMOVE);
/* add the dependency on the real device */ /* add the dependency on the real device */
if (!_pre_list_add(dm->mem, &dl->pre_create, if (!str_list_add(dm->mem, &dl->pre_create,
pool_strdup(dm->mem, dlr->dlid))) { pool_strdup(dm->mem, dlr->dlid))) {
stack; stack;
return 0; return 0;
} }
@ -1361,8 +1335,8 @@ static int _expand_origin_real(struct dev_manager *dm,
_set_flag(dl, TOPLEVEL); _set_flag(dl, TOPLEVEL);
/* add the dependency on the real device */ /* add the dependency on the real device */
if (!_pre_list_add(dm->mem, &dl->pre_create, if (!str_list_add(dm->mem, &dl->pre_create,
pool_strdup(dm->mem, real_dlid))) { pool_strdup(dm->mem, real_dlid))) {
stack; stack;
return 0; return 0;
} }
@ -1422,21 +1396,21 @@ static int _expand_snapshot(struct dev_manager *dm, struct logical_volume *lv,
_set_flag(dl, TOPLEVEL); _set_flag(dl, TOPLEVEL);
/* add the dependency on the cow device */ /* add the dependency on the cow device */
if (!_pre_list_add(dm->mem, &dl->pre_create, if (!str_list_add(dm->mem, &dl->pre_create,
pool_strdup(dm->mem, cow_dlid))) { pool_strdup(dm->mem, cow_dlid))) {
stack; stack;
return 0; return 0;
} }
/* add the dependency on the real origin device */ /* add the dependency on the real origin device */
if (!_pre_list_add(dm->mem, &dl->pre_create, if (!str_list_add(dm->mem, &dl->pre_create,
_build_dlid(dm->mem, s->origin->lvid.s, "real"))) { _build_dlid(dm->mem, s->origin->lvid.s, "real"))) {
stack; stack;
return 0; return 0;
} }
/* add the dependency on the visible origin device */ /* add the dependency on the visible origin device */
if (!_pre_list_add(dm->mem, &dl->pre_suspend, s->origin->lvid.s)) { if (!str_list_add(dm->mem, &dl->pre_suspend, s->origin->lvid.s)) {
stack; stack;
return 0; return 0;
} }
@ -1770,8 +1744,8 @@ static int _populate_pre_suspend_lists(struct dev_manager *dm)
continue; continue;
} }
if (!_pre_list_add(dm->mem, &dep->pre_create, if (!str_list_add(dm->mem, &dep->pre_create,
dl->dlid)) { dl->dlid)) {
stack; stack;
return 0; return 0;
} }
@ -1787,8 +1761,8 @@ static int _populate_pre_suspend_lists(struct dev_manager *dm)
continue; continue;
} }
if (!_pre_list_add(dm->mem, &dep->pre_suspend, if (!str_list_add(dm->mem, &dep->pre_suspend,
dl->dlid)) { dl->dlid)) {
stack; stack;
return 0; return 0;
} }

35
lib/datastruct/str_list.c Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2003 Sistina Software
*
* This file is released under the LGPL.
*/
#include "lib.h"
#include "str_list.h"
int str_list_add(struct pool *mem, struct list *sl, const char *str)
{
struct str_list *sln;
struct list *slh;
if (!str) {
stack;
return 0;
}
/* Already in list? */
list_iterate(slh, sl) {
if (!strcmp(str, list_item(slh, struct str_list)->str))
return 1;
}
if (!(sln = pool_alloc(mem, sizeof(*sln)))) {
stack;
return 0;
}
sln->str = str;
list_add(sl, &sln->list);
return 1;
}

14
lib/datastruct/str_list.h Normal file
View File

@ -0,0 +1,14 @@
/*
* Copyright (C) 2003 Sistina Software (UK) Limited.
*
* This file is released under the GPL.
*/
#ifndef _LVM_STR_LIST_H
#define _LVM_STR_LIST_H
#include "pool.h"
int str_list_add(struct pool *mem, struct list *sl, const char *str);
#endif

View File

@ -9,6 +9,7 @@
#include "hash.h" #include "hash.h"
#include "pool.h" #include "pool.h"
#include "disk-rep.h" #include "disk-rep.h"
#include "lv_alloc.h"
/* /*
* After much thought I have decided it is easier, * After much thought I have decided it is easier,
@ -191,26 +192,13 @@ static int _check_maps_are_complete(struct hash_table *maps)
return 1; return 1;
} }
static struct lv_segment *_alloc_seg(struct pool *mem, uint32_t stripes)
{
struct lv_segment *seg;
uint32_t len = sizeof(*seg) + (stripes * sizeof(seg->area[0]));
if (!(seg = pool_zalloc(mem, len))) {
stack;
return NULL;
}
return seg;
}
static int _read_linear(struct pool *mem, struct lv_map *lvm) static int _read_linear(struct pool *mem, struct lv_map *lvm)
{ {
uint32_t le = 0; uint32_t le = 0;
struct lv_segment *seg; struct lv_segment *seg;
while (le < lvm->lv->le_count) { while (le < lvm->lv->le_count) {
seg = _alloc_seg(mem, 1); seg = alloc_lv_segment(mem, 1);
seg->lv = lvm->lv; seg->lv = lvm->lv;
seg->type = SEG_STRIPED; seg->type = SEG_STRIPED;
@ -276,7 +264,7 @@ static int _read_stripes(struct pool *mem, struct lv_map *lvm)
len = lvm->lv->le_count / lvm->stripes; len = lvm->lv->le_count / lvm->stripes;
while (le < len) { while (le < len) {
if (!(seg = _alloc_seg(mem, lvm->stripes))) { if (!(seg = alloc_lv_segment(mem, lvm->stripes))) {
stack; stack;
return 0; return 0;
} }

View File

@ -63,23 +63,6 @@ static struct flag *_get_flags(int type)
return NULL; return NULL;
} }
static int _emit(char **buffer, size_t *size, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vsnprintf(*buffer, *size, fmt, ap);
va_end(ap);
if (n < 0 || (n == *size))
return 0;
*buffer += n;
*size -= n;
return 1;
}
/* /*
* Converts a bitset to an array of string values, * Converts a bitset to an array of string values,
* using one of the tables defined at the top of * using one of the tables defined at the top of
@ -95,27 +78,27 @@ int print_flags(uint32_t status, int type, char *buffer, size_t size)
return 0; return 0;
} }
if (!_emit(&buffer, &size, "[")) if (!emit_to_buffer(&buffer, &size, "["))
return 0; return 0;
for (f = 0; flags[f].mask; f++) { for (f = 0; flags[f].mask; f++) {
if (status & flags[f].mask) { if (status & flags[f].mask) {
if (!first) { if (!first) {
if (!_emit(&buffer, &size, ", ")) if (!emit_to_buffer(&buffer, &size, ", "))
return 0; return 0;
} else } else
first = 0; first = 0;
if (!_emit(&buffer, &size, "\"%s\"", if (!emit_to_buffer(&buffer, &size, "\"%s\"",
flags[f].description)) flags[f].description))
return 0; return 0;
status &= ~flags[f].mask; status &= ~flags[f].mask;
} }
} }
if (!_emit(&buffer, &size, "]")) if (!emit_to_buffer(&buffer, &size, "]"))
return 0; return 0;
if (status) if (status)

View File

@ -12,6 +12,7 @@
#include "hash.h" #include "hash.h"
#include "toolcontext.h" #include "toolcontext.h"
#include "lvmcache.h" #include "lvmcache.h"
#include "lv_alloc.h"
typedef int (*section_fn) (struct format_instance * fid, struct pool * mem, typedef int (*section_fn) (struct format_instance * fid, struct pool * mem,
struct volume_group * vg, struct config_node * pvn, struct volume_group * vg, struct config_node * pvn,
@ -279,9 +280,8 @@ static int _read_segment(struct pool *mem, struct volume_group *vg,
} }
} }
if (!(seg = pool_zalloc(mem, sizeof(*seg) + if (!(seg = alloc_lv_segment(mem, area_count))) {
(sizeof(seg->area[0]) * area_count)))) { log_error("Segment allocation failed");
stack;
return 0; return 0;
} }

View File

@ -29,3 +29,20 @@ int lvm_snprintf(char *buf, size_t bufsize, const char *format, ...)
return n; return n;
} }
int emit_to_buffer(char **buffer, size_t *size, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vsnprintf(*buffer, *size, fmt, ap);
va_end(ap);
if (n < 0 || (n == *size))
return 0;
*buffer += n;
*size -= n;
return 1;
}

View File

@ -19,4 +19,6 @@
*/ */
int lvm_snprintf(char *buf, size_t bufsize, const char *format, ...); int lvm_snprintf(char *buf, size_t bufsize, const char *format, ...);
int emit_to_buffer(char **buffer, size_t *size, const char *fmt, ...);
#endif #endif