File: libdevmapper.c

package info (click to toggle)
devmapper 2%3A1.01.00-4sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,044 kB
  • ctags: 2,336
  • sloc: ansic: 11,777; sh: 2,695; makefile: 291; perl: 16
file content (414 lines) | stat: -rw-r--r-- 8,276 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
 *
 * This file is part of the device-mapper userspace tools.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "libdevmapper.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <linux/kdev_t.h>
#include <linux/device-mapper.h>

#include "libdm-targets.h"
#include "libdm-common.h"

typedef enum {
	DIR_CREATE,
	DIR_REMOVE
} do_newold_t;

/*
 * Join n path components together with /'s.
 */
static char *mkpath(int n, ...)
{
	va_list va;
	int len = 0, i;
	char *str, *r;

	va_start(va, n);
	for (i = 0; i < n; i++)
		len += strlen(va_arg(va, char *)) + 1;

	va_end(va);

	if (!(r = str = malloc(len))) {
		log_error("mkpath: malloc(%d) failed", len);
		return NULL;
	}

	va_start(va, n);
	for (i = 0; i < n; i++)
		str += sprintf(str, "%s%s", i ? "/" : "", va_arg(va, char *));
	va_end(va);

	return r;
}

void dm_task_destroy(struct dm_task *dmt)
{
	struct target *t, *n;

	for (t = dmt->head; t; t = n) {
		n = t->next;
		free(t);
	}

	if (dmt->dev_name)
		free(dmt->dev_name);

	free(dmt);
}

int dm_task_get_info(struct dm_task *dmt, struct dm_info *info)
{
	memcpy(info, &dmt->info, sizeof(struct dm_info));
	return 1;
}

int dm_task_set_ro(struct dm_task *dmt)
{
	log_error("Read-only attribute ignored by filesystem interface");
	return 1;
}

int dm_task_set_newname(struct dm_task *dmt, const char *newname)
{
	log_error("Renaming is not yet supported by the filesystem interface");
	return 0;
}

int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size)
{
	log_error("The filesystem interface cannot return its version yet");
	return 0;
}

struct target *create_target(uint64_t start,
			     uint64_t len, const char *type, const char *params)
{
	struct target *t;
	int size = strlen(params) + strlen(type);
	int ret;

	size += 64;		/* Guess at max size of start and len */

	t = malloc(size + sizeof(struct target));
	if (!t) {
		log_error("create_target: malloc(%d) failed",
		    size + sizeof(struct target));
		return NULL;
	}

	memset(t, 0, size + sizeof(struct target));
	t->str = (char *) (t + 1);

	ret = sprintf(t->str, "%" PRIu64 " %" PRIu64 " %s %s\n", start, len,
		      type, params);
	if (ret > size) {
		/* This should be impossible, but check anyway */
		log_error("create_target internal error: Ran out of buffer space");
		free(t);
		return NULL;
	}

	return t;
}

static int do_suspend(char *mnt, char *name, int on)
{
	char *path;
	FILE *fp;
	int ret = 0;
	char c;

	if (!(path = mkpath(3, mnt, name, "suspend")))
		return 0;

	if ((fp = fopen(path, "w"))) {
		c = on ? '1' : '0';
		if (fputc(c, fp) == (int)c)
			ret = 1;
		else
			log_error("%s: fputc failed: %s", path, strerror(errno));
		fclose(fp);
	} else
		log_error("%s: fopen failed: %s", path, strerror(errno));

	free(path);

	return ret;
}

static int do_newold(char *mnt, char *name, do_newold_t create)
{
	char *path = mkpath(2, mnt, name);
	int ret;

	if (!path)
		return 0;

	if (create == DIR_CREATE) {
		if ((ret = mkdir(path, 0750)) < 0) {
			struct stat st;
			if (errno == EEXIST && !stat(path, &st) &&
			    S_ISDIR(st.st_mode)) 
				ret = 1;
			log_error("%s: mkdir failed: %s", path, strerror(errno));
		}
	} else if ((ret = rmdir(path)) < 0)
		log_error("%s: rmdir failed: %s", path, strerror(errno));

	free(path);

	return (ret < 0) ? 0 : 1;
}

static int do_device(char *mnt, char *name, struct dm_info *info)
{
	char *path;
	struct stat st;

	if (!(path = mkpath(3, mnt, name, "device")))
		return 0;

	if (!stat(path, &st)) {
		info->major = MAJOR(st.st_rdev);
		info->minor = MINOR(st.st_rdev);
		info->exists = 1;
	} else
		info->exists = 0;

	free(path);
	return 1;
}

static int do_suspend_state(char *mnt, char *name, struct dm_info *info)
{
	char *path;
	FILE *fp;
	int ret = 0;

	if (!(path = mkpath(3, mnt, name, "suspend")))
		return 0;

	if ((fp = fopen(path, "r"))) {
		if (fscanf(fp, "%d", &info->suspended) == 1)
			ret = 1;
		else
			log_error("%s fscanf failed: %s", path, strerror(errno));
		fclose(fp);
	} else
		log_error("%s: fopen failed: %s", path, strerror(errno));

	free(path);

	return ret;
}

static int do_info(char *mnt, char *name, struct dm_info *info)
{
	memset(info, 0, sizeof(struct dm_info));

	if (!do_device(mnt, name, info))
		return 0;

	if (info->exists && !do_suspend_state(mnt, name, info))
		return 0;

	/* Unsupported */
	info->target_count = -1;
	info->open_count = -1;
	info->read_only = 0;

	return 1;
}

/*
 * Writes a buffer out to a file, returns 0 on failure.
 */
static int write_buffer(int fd, const void *buf, size_t count)
{
	size_t n = 0;
	int tot = 0;

	while (tot < count) {
		do
			n = write(fd, buf, count - tot);
		while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));

		if (n <= 0)
			return 0;

		tot += n;
		buf += n;
	}

	return 1;
}

static int write_data(int fd, struct dm_task *dmt)
{
	struct target *t;

	for (t = dmt->head; t; t = t->next)
		if (!write_buffer(fd, t->str, strlen(t->str)))
			return 0;

	return 1;
}

static int do_load(char *mnt, char *name, struct dm_task *dmt)
{
	char *path;
	int fd, ret = 0;

	if (!(path = mkpath(3, mnt, name, "table")))
		return 0;

	if ((fd = open(path, O_RDWR)) != -1) {
		if (!(ret = write_data(fd, dmt)))
			log_error("%s: write failed: %s", path, strerror(errno));
		close(fd);
	}

	free(path);

	return ret;
}

static void strip_nl(char *str)
{
	while (*str && *str != '\n' && *str != '\r')
		str++;
	*str = 0;
}

static int do_error_check(char *mnt, char *name)
{
	char *path;
	FILE *fp;
	int ret = 1;
	char buf[1024];

	if (!(path = mkpath(3, mnt, name, "error")))
		return 0;

	if (!(fp = fopen(path, "r"))) {
		log_error("%s: fopen failed: %s", path, strerror(errno));
		free(path);
		return 0;
	}

	while (fgets(buf, sizeof(buf), fp)) {
		strip_nl(buf);
		log_error(buf);
		ret = 0;
	}

	fclose(fp);
	free(path);
	return ret;
}

static char *find_mount_point(void)
{
	FILE *fp;
	static char mpoint[4096];
	char fstype[30];

	if (!(fp = fopen("/proc/mounts", "r"))) {
		log_error("/proc/mounts: fopen failed: %s", strerror(errno));
		return NULL;
	}

	while (fscanf(fp, "%*s%4096s%30s%*s%*d%*d", mpoint, fstype) == 2) {
		if (!strcmp(fstype, "dmfs")) {
			fclose(fp);
			return mpoint;
		}
	}
	fclose(fp);
	return NULL;
}

int dm_task_run(struct dm_task *dmt)
{
	char *mnt = find_mount_point();

	if (mnt == NULL) {
		/* FIXME Mount it temporarily if not mounted */
		log_error("Cannot find mount point for dmfs or dmfs not mounted");
		return 0;
	}

	if (!dmt->dev_name || !*dmt->dev_name) {
		log_error("dm_task_run: Device name not supplied");
		return 0;
	}

	switch (dmt->type) {
	case DM_DEVICE_CREATE:
		if (!do_newold(mnt, dmt->dev_name, DIR_CREATE) ||
		    !do_load(mnt, dmt->dev_name, dmt) ||
		    !do_error_check(mnt, dmt->dev_name) ||
		    !do_suspend(mnt, dmt->dev_name, 0) ||
		    !do_info(mnt, dmt->dev_name, &dmt->info))
			return 0;
		add_dev_node(dmt->dev_name,
			     MKDEV(dmt->info.major, dmt->info.minor));
		break;

	case DM_DEVICE_RELOAD:
		if (!do_load(mnt, dmt->dev_name, dmt) ||
		    !do_error_check(mnt, dmt->dev_name)) return 0;
		break;

	case DM_DEVICE_REMOVE:
		if (!do_newold(mnt, dmt->dev_name, DIR_REMOVE) ||
		    !do_info(mnt, dmt->dev_name, &dmt->info))
			return 0;
		rm_dev_node(dmt->dev_name);
		break;

	case DM_DEVICE_SUSPEND:
		if (!do_suspend(mnt, dmt->dev_name, 1))
			return 0;
		break;

	case DM_DEVICE_RESUME:
		if (!do_suspend(mnt, dmt->dev_name, 0))
			return 0;
		break;

	case DM_DEVICE_INFO:
		if (!do_info(mnt, dmt->dev_name, &dmt->info))
			return 0;
		break;

	default:
		log_error("Internal error: unknown device-mapper task %d", dmt->type);
		return 0;
	}

	return 1;
}