diff --git a/lib/activate/fs.c b/lib/activate/fs.c new file mode 100644 index 000000000..80480c06d --- /dev/null +++ b/lib/activate/fs.c @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited. + * + * This file is released under the LGPL. + */ + +#include + +/* + * FIXME: copied straight from LVM1, we need to + * check awkward cases like the guy who was + * running devfs in parallel, mounted on + * /devfs/ + * + * This should run through /proc/mounts once only, + * storing devfs mount points in a hash table. + */ +static int _check_devfs(const char *dev_prefix) +{ + int r = 0, len; + char dir[NAME_LEN], line[512]; + char type[32]; + FILE *mounts = NULL; + + if (!(mounts = fopen("/proc/mounts", "r"))) + goto out; + + /* trim the trailing slash off the dir prefix, yuck */ + len = strlen(dev_prefix) - 1; + while(len && dev_prefix[len] == '/') + len--; + + while (!feof(mounts)) { + fgets(line, sizeof(line) - 1, mounts); + if (sscanf(line, "%*s %s %s %*s", dir, type) != 2) + continue; + + if (!strcmp(type, "devfs") && !strncmp(dir, dev_prefix, len)) { + ret = 1; + break; + } + } + fclose(mounts); + + out: + return r; +} + +static inline int _running_devfs(struct io_space *ios) +{ + static int _using_devfs = -1; + + if (_using_devfs < 0) + _using_devfs = _check_devfs(ios->prefix); + + return _using_devfs; +} + +static int _mk_node(struct io_space *ios, struct logical_volume *lv) +{ + char lv_path[PATH_MAX]; + dev_t dev; + + if (_running_devfs(ios)) + return 1; + + snprintf(lv_path, sizeof(lv_path), "%s/device-mapper/%s", + ios->prefix, lv->name); + + if (mknod(lv_path, S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP, dev) < 0) { + log_sys_err("mknod", "creating lv device node"); + return 0; + } + + return 1; +} + +static int _rm_node(struct io_space *ios, struct logical_volume *lv) +{ + char lv_path[PATH_MAX]; + + if (_running_devfs(ios)) + return 1; + + snprintf(lv_path, sizeof(lv_path), "%s/device-mapper/%s", + ios->prefix, lv->name); + + if (unlink(lv_path) < 0) { + log_sys_err("unlink", "removing lv device node"); + return 0; + } + + return 1; +} + +/* + * Lazy programmer: I'm just going to always try + * and create/remove the vg directory, and not say + * anything if it fails. + */ +static int _mk_dir(struct io_space *ios, const char *vg_name) +{ + char vg_path[PATH_MAX]; + + snprintf(vg_path, sizeof(vg_path), "%s/%s", ios->prefix, vg_name); + mkdir(vg_path, 0555); + return 1; +} + +static int _rm_dir(struct io_space *ios, const char *vg_name) +{ + char vg_path[PATH_MAX]; + + snprintf(vg_path, sizeof(vg_path), "%s/%s", ios->prefix, vg_name); + rmdir(vg_path); + return 1; +} + +static int _mk_link(struct io_space *ios, struct logical_volume *lv) +{ + char lv_path[PATH_MAX], link_path[PATH_MAX]; + + snprintf(lv_path, sizeof(lv_path), ); +} + +int fs_add_lv(struct io_space *ios, struct logical_volume *lv) +{ + if (!_mk_node(ios, lv)) { + stack; + return 0; + } + + if (!_mk_dir(ios, lv->vg->name)) { + stack; + return 0; + } + + if (!_mk_link(ios, lv)) { + stack; + return 0; + } + + return 1; +} + +int fs_del_lv(struct logical_volume *lv) +{ + if (!_rm_link(ios, lv)) { + stack; + return 0; + } + + if (!_rm_dir(ios, lv->vg->name)) { + stack; + return 0; + } + + if (!_rm_node(ios, lv)) { + stack; + return 0; + } + + return 1; +} + diff --git a/lib/activate/fs.h b/lib/activate/fs.h new file mode 100644 index 000000000..7e17325a9 --- /dev/null +++ b/lib/activate/fs.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited. + * + * This file is released under the LGPL. + */ + +#ifndef _LVM_FS_H +#define _LVM_FS_H + +#include "metadata.h" + +int fs_add_lv(struct io_space *ios, struct logical_volume *lv); +int fs_del_lv(struct io_space *ios, struct logical_volume *lv); + +#endif diff --git a/old-tests/device/fill_device.c b/old-tests/device/fill_device.c new file mode 100644 index 000000000..65ad966eb --- /dev/null +++ b/old-tests/device/fill_device.c @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited + * + * This file is released under the GPL. + */ + +#include "device.h" +#include "random.h" + +#include + + + + +int main(int argc, char **argv) +{ + +} diff --git a/old-tests/device/random.c b/old-tests/device/random.c new file mode 100644 index 000000000..04594c624 --- /dev/null +++ b/old-tests/device/random.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited + * + * This file is released under the GPL. + */ + +#include "random.h" +#include "log.h" + +int32_t _a[56]; +int32_t *_r; + +static inline int32_t _mod_diff(int32_t x, int32_t y) +{ + return (x - y) & 0x7fffffff; +} + +static int32_t _flip_cycle(void) +{ + int32_t *ii, *jj; + for (ii = _a + 1, jj = _a + 32; jj <= _a + 55; ii++, jj++) + *ii = _mod_diff(*ii, *jj); + + for (jj = _a + 1; ii <= _a + 55; ii++, jj++) + *ii = _mod_diff(*ii, *jj); + + _r = _a + 54; + return _a[55]; +} + +static void rand_init(int32_t seed) +{ + int64_t i; + int64_t prev = seed, next = 1; + + seed = prev = _mod_diff(prev, 0); /* strip the sign */ + _a[55] = prev; + for (i = 21; i; i = (i + 21) % 55) { + _a[i] = next; + next = _mod_diff(prev, next); + if(seed & 1) + seed = 0x40000000L + (seed >> 1); + else + seed >>= 1; + + next = _mod_diff(next, seed); + prev = _a[i]; + } + + _flip_cycle(); + _flip_cycle(); + _flip_cycle(); + _flip_cycle(); + _flip_cycle(); +} + +/* + * FIXME: move this to be an inline in the + * header. + */ +int32_t rand_get(void) +{ + return (*_r >= 0) ? *_r-- : _flip_cycle(); +} + + +/* + * just used by rand_check + */ +#define t31 0x80000000 +static int32_t _uniform(int32_t m) +{ + uint32_t t = t31 - (t31 % m); + int32_t r; + + do + r = next_rand(sc); + + while (t <= (uint32_t) r); + + return r % m; +} + +/* + * Checks I've copied the code correctly. + */ +int rand_check(void) +{ + int j; + + rand_init(-314159L); + + if (next_rand(sc) != 119318998) { + log_err("Random number generator failed check 1"); + return 0; + } + + for(j = 1; j <= 133; j++) + rand_get(); + + if (_uniform(0x55555555L) != 748103812) { + log_err("Random number generator failed check 2"); + return 0; + } + + return 1; +} diff --git a/old-tests/device/random.h b/old-tests/device/random.h new file mode 100644 index 000000000..5be2c48e7 --- /dev/null +++ b/old-tests/device/random.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited + * + * This file is released under the GPL. + */ + +/* + * Random number generator snarfed from the + * Stanford Graphbase. + */ + +#ifndef _LVM_RANDOM_H +#define _LVM_RANDOM_H + +#include "lvm-types.h" + +void rand_init(int32_t seed); +int32_t rand_get(void); + +/* + * Note this will reset the seed. + */ +int rand_check(void); + +#endif