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:
parent
188b5fa337
commit
aa37899857
@ -9,6 +9,7 @@
|
||||
../lib/datastruct/hash.h
|
||||
../lib/datastruct/list.h
|
||||
../lib/datastruct/lvm-types.h
|
||||
../lib/datastruct/str_list.h
|
||||
../lib/device/dev-cache.h
|
||||
../lib/device/device.h
|
||||
../lib/display/display.h
|
||||
@ -21,6 +22,7 @@
|
||||
../lib/label/label.h
|
||||
../lib/locking/locking.h
|
||||
../lib/log/log.h
|
||||
../lib/metadata/lv_alloc.h
|
||||
../lib/metadata/metadata.h
|
||||
../lib/mm/dbg_malloc.h
|
||||
../lib/mm/memlock.h
|
||||
|
@ -20,6 +20,7 @@ SOURCES=\
|
||||
datastruct/bitset.c \
|
||||
datastruct/btree.c \
|
||||
datastruct/hash.c \
|
||||
datastruct/str_list.c \
|
||||
device/dev-cache.c \
|
||||
device/dev-io.c \
|
||||
device/device.c \
|
||||
|
@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "lib.h"
|
||||
#include "str_list.h"
|
||||
#include "dev_manager.h"
|
||||
#include "pool.h"
|
||||
#include "hash.h"
|
||||
@ -155,33 +156,6 @@ static inline void _clear_flag(struct dev_layer *dl, int 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
|
||||
* other hyphens that appear in these names are quoted with yet
|
||||
@ -1301,7 +1275,7 @@ static int _expand_vanilla(struct dev_manager *dm, struct logical_volume *lv,
|
||||
for (s = 0; s < seg->area_count; s++) {
|
||||
if (seg->area[s].type != AREA_LV)
|
||||
continue;
|
||||
if (!_pre_list_add(dm->mem, &dl->pre_create,
|
||||
if (!str_list_add(dm->mem, &dl->pre_create,
|
||||
_build_dlid(dm->mem,
|
||||
seg->area[s].u.lv.
|
||||
lv->lvid.s, NULL))) {
|
||||
@ -1327,7 +1301,7 @@ static int _expand_vanilla(struct dev_manager *dm, struct logical_volume *lv,
|
||||
_set_flag(dlr, REMOVE);
|
||||
|
||||
/* 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))) {
|
||||
stack;
|
||||
return 0;
|
||||
@ -1361,7 +1335,7 @@ static int _expand_origin_real(struct dev_manager *dm,
|
||||
_set_flag(dl, TOPLEVEL);
|
||||
|
||||
/* 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))) {
|
||||
stack;
|
||||
return 0;
|
||||
@ -1422,21 +1396,21 @@ static int _expand_snapshot(struct dev_manager *dm, struct logical_volume *lv,
|
||||
_set_flag(dl, TOPLEVEL);
|
||||
|
||||
/* 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))) {
|
||||
stack;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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"))) {
|
||||
stack;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
return 0;
|
||||
}
|
||||
@ -1770,7 +1744,7 @@ static int _populate_pre_suspend_lists(struct dev_manager *dm)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_pre_list_add(dm->mem, &dep->pre_create,
|
||||
if (!str_list_add(dm->mem, &dep->pre_create,
|
||||
dl->dlid)) {
|
||||
stack;
|
||||
return 0;
|
||||
@ -1787,7 +1761,7 @@ static int _populate_pre_suspend_lists(struct dev_manager *dm)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_pre_list_add(dm->mem, &dep->pre_suspend,
|
||||
if (!str_list_add(dm->mem, &dep->pre_suspend,
|
||||
dl->dlid)) {
|
||||
stack;
|
||||
return 0;
|
||||
|
35
lib/datastruct/str_list.c
Normal file
35
lib/datastruct/str_list.c
Normal 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
14
lib/datastruct/str_list.h
Normal 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
|
@ -9,6 +9,7 @@
|
||||
#include "hash.h"
|
||||
#include "pool.h"
|
||||
#include "disk-rep.h"
|
||||
#include "lv_alloc.h"
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
uint32_t le = 0;
|
||||
struct lv_segment *seg;
|
||||
|
||||
while (le < lvm->lv->le_count) {
|
||||
seg = _alloc_seg(mem, 1);
|
||||
seg = alloc_lv_segment(mem, 1);
|
||||
|
||||
seg->lv = lvm->lv;
|
||||
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;
|
||||
|
||||
while (le < len) {
|
||||
if (!(seg = _alloc_seg(mem, lvm->stripes))) {
|
||||
if (!(seg = alloc_lv_segment(mem, lvm->stripes))) {
|
||||
stack;
|
||||
return 0;
|
||||
}
|
||||
|
@ -63,23 +63,6 @@ static struct flag *_get_flags(int type)
|
||||
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,
|
||||
* using one of the tables defined at the top of
|
||||
@ -95,19 +78,19 @@ int print_flags(uint32_t status, int type, char *buffer, size_t size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!_emit(&buffer, &size, "["))
|
||||
if (!emit_to_buffer(&buffer, &size, "["))
|
||||
return 0;
|
||||
|
||||
for (f = 0; flags[f].mask; f++) {
|
||||
if (status & flags[f].mask) {
|
||||
if (!first) {
|
||||
if (!_emit(&buffer, &size, ", "))
|
||||
if (!emit_to_buffer(&buffer, &size, ", "))
|
||||
return 0;
|
||||
|
||||
} else
|
||||
first = 0;
|
||||
|
||||
if (!_emit(&buffer, &size, "\"%s\"",
|
||||
if (!emit_to_buffer(&buffer, &size, "\"%s\"",
|
||||
flags[f].description))
|
||||
return 0;
|
||||
|
||||
@ -115,7 +98,7 @@ int print_flags(uint32_t status, int type, char *buffer, size_t size)
|
||||
}
|
||||
}
|
||||
|
||||
if (!_emit(&buffer, &size, "]"))
|
||||
if (!emit_to_buffer(&buffer, &size, "]"))
|
||||
return 0;
|
||||
|
||||
if (status)
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include "hash.h"
|
||||
#include "toolcontext.h"
|
||||
#include "lvmcache.h"
|
||||
#include "lv_alloc.h"
|
||||
|
||||
typedef int (*section_fn) (struct format_instance * fid, struct pool * mem,
|
||||
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) +
|
||||
(sizeof(seg->area[0]) * area_count)))) {
|
||||
stack;
|
||||
if (!(seg = alloc_lv_segment(mem, area_count))) {
|
||||
log_error("Segment allocation failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -29,3 +29,20 @@ int lvm_snprintf(char *buf, size_t bufsize, const char *format, ...)
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -19,4 +19,6 @@
|
||||
*/
|
||||
int lvm_snprintf(char *buf, size_t bufsize, const char *format, ...);
|
||||
|
||||
int emit_to_buffer(char **buffer, size_t *size, const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user