File: dmfs-table.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 (386 lines) | stat: -rw-r--r-- 8,305 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
/*
 * dmfs-table.c
 *
 * Copyright (C) 2001 Sistina Software
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU CC; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "dm.h"
#include "dmfs.h"

#include <linux/mm.h>

static offset_t start_of_next_range(struct dm_table *t)
{
	offset_t n = 0;

	if (t->num_targets) {
		n = t->highs[t->num_targets - 1] + 1;
	}
	
	return n;
}

static char *dmfs_parse_line(struct dm_table *t, char *str)
{
	offset_t start, size, high;
	void *context;
	struct target_type *ttype;
	int rv = 0;
	char *msg;
	int pos = 0;
	char target[33];
	char *argv[MAX_ARGS];
	int argc;

	static char *err_table[] = {
		"Missing/Invalid start argument",
		"Missing/Invalid size argument",
		"Missing target type"
	};

	rv = sscanf(str, "%d %d %32s%n", &start, &size, target, &pos);
	if (rv < 3) {
		msg = err_table[rv];
		goto out;
	}
	str += pos;
	while (*str && isspace(*str))
		str++;

	msg = "Gap in table";
	if (start != start_of_next_range(t))
		goto out;

	msg = "Target type unknown";
	ttype = dm_get_target_type(target);
	if (ttype) {
		msg = "Too many arguments";
		rv = split_args(MAX_ARGS, &argc, argv, str);
		if (rv < 0)
			goto out;

		msg = "This message should never appear (constructor error)";
		rv = ttype->ctr(t, start, size, argc, argv, &context);
		msg = context;
		if (rv == 0) {
			msg = "Error adding target to table";
			high = start + (size - 1);
			if (dm_table_add_target(t, high, ttype, context) == 0)
				return NULL;
			ttype->dtr(t, context);
		}
		dm_put_target_type(ttype);
	}

      out:
	return msg;
}

static int dmfs_copy(char *dst, int dstlen, char *src, int srclen, int *flag)
{
	int len = min(dstlen, srclen);
	char *start = dst;

	while (len) {
		*dst = *src++;
		if (*dst == '\n')
			goto end_of_line;
		dst++;
		len--;
	}
      out:
	return (dst - start);
      end_of_line:
	dst++;
	*flag = 1;
	goto out;
}

static int dmfs_line_is_not_comment(char *str)
{
	while (*str) {
		if (*str == '#')
			break;
		if (!isspace(*str))
			return 1;
		str++;
	}
	return 0;
}

struct dmfs_desc {
	struct dm_table *table;
	struct inode *inode;
	char *tmp;
	loff_t tmpl;
	unsigned long lnum;
};

static int dmfs_read_actor(read_descriptor_t *desc, struct page *page,
			   unsigned long offset, unsigned long size)
{
	char *buf, *msg;
	unsigned long count = desc->count, len, copied;
	struct dmfs_desc *d = (struct dmfs_desc *) desc->buf;

	if (size > count)
		size = count;

	len = size;
	buf = kmap(page);
	do {
		int flag = 0;
		copied = dmfs_copy(d->tmp + d->tmpl, PAGE_SIZE - d->tmpl - 1,
				   buf + offset, len, &flag);
		offset += copied;
		len -= copied;
		if (d->tmpl + copied == PAGE_SIZE - 1)
			goto line_too_long;
		d->tmpl += copied;
		if (flag || (len == 0 && count == size)) {
			*(d->tmp + d->tmpl) = 0;
			if (dmfs_line_is_not_comment(d->tmp)) {
				msg = dmfs_parse_line(d->table, d->tmp);
				if (msg) {
					dmfs_add_error(d->inode, d->lnum, msg);
				}
			}
			d->lnum++;
			d->tmpl = 0;
		}
	} while (len > 0);
	kunmap(page);

	desc->count = count - size;
	desc->written += size;

	return size;

      line_too_long:
	printk(KERN_INFO "dmfs_read_actor: Line %lu too long\n", d->lnum);
	kunmap(page);
	return 0;
}

static struct dm_table *dmfs_parse(struct inode *inode, struct file *filp)
{
	struct dm_table *t = NULL;
	unsigned long page;
	struct dmfs_desc d;
	loff_t pos = 0;
	int r;

	if (inode->i_size == 0)
		return NULL;

	page = __get_free_page(GFP_NOFS);
	if (page) {
		r = dm_table_create(&t);
		if (!r) {
			read_descriptor_t desc;

			desc.written = 0;
			desc.count = inode->i_size;
			desc.buf = (char *) &d;
			d.table = t;
			d.inode = inode;
			d.tmp = (char *) page;
			d.tmpl = 0;
			d.lnum = 1;

			do_generic_file_read(filp, &pos, &desc,
					     dmfs_read_actor);
			if (desc.written != inode->i_size) {
				dm_table_destroy(t);
				t = NULL;
			} 
			if (!t || (t && !t->num_targets))
				dmfs_add_error(d.inode, 0, 
					       "No valid targets found");
		}
		free_page(page);
	}

	if (!list_empty(&DMFS_I(inode)->errors)) {
		dm_table_destroy(t);
		t = NULL;
	}

	return t;
}

static int dmfs_table_release(struct inode *inode, struct file *f)
{
	struct dentry *dentry = f->f_dentry;
	struct inode *parent = dentry->d_parent->d_inode;
	struct dmfs_i *dmi = DMFS_I(parent);
	struct dm_table *table;

	if (f->f_mode & FMODE_WRITE) {
		down(&dmi->sem);
		dmfs_zap_errors(dentry->d_parent->d_inode);
		table = dmfs_parse(dentry->d_parent->d_inode, f);

		if (table) {
			struct mapped_device *md = dmi->md;
			int need_resume = 0;

			if (md->suspended == 0) {
				dm_suspend(md);
				need_resume = 1;
			}
			dm_swap_table(md, table);
			if (need_resume) {
				dm_resume(md);
			}
		}
		up(&dmi->sem);

		put_write_access(parent);
	}

	return 0;
}

static int dmfs_readpage(struct file *file, struct page *page)
{
	if (!Page_Uptodate(page)) {
		memset(kmap(page), 0, PAGE_CACHE_SIZE);
		kunmap(page);
		flush_dcache_page(page);
		SetPageUptodate(page);
	}

	UnlockPage(page);
	return 0;
}

static int dmfs_prepare_write(struct file *file, struct page *page,
			      unsigned offset, unsigned to)
{
	void *addr = kmap(page);

	if (!Page_Uptodate(page)) {
		memset(addr, 0, PAGE_CACHE_SIZE);
		flush_dcache_page(page);
		SetPageUptodate(page);
	}

	SetPageDirty(page);
	return 0;
}

static int dmfs_commit_write(struct file *file, struct page *page,
			     unsigned offset, unsigned to)
{
	struct inode *inode = page->mapping->host;
	loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + to;

	kunmap(page);
	if (pos > inode->i_size)
		inode->i_size = pos;

	return 0;
}

/*
 * There is a small race here in that two processes might call this at
 * the same time and both fail. So its a fail safe race :-) This should
 * move into namei.c (and thus use the spinlock and do this properly)
 * at some stage if we continue to use this set of functions for ensuring
 * exclusive write access to the file
 */
int get_exclusive_write_access(struct inode *inode)
{
	if (get_write_access(inode))
		return -1;
	if (atomic_read(&inode->i_writecount) != 1) {
		put_write_access(inode);
		return -1;
	}
	return 0;
}

static int dmfs_table_open(struct inode *inode, struct file *file)
{
	struct dentry *dentry = file->f_dentry;
	struct inode *parent = dentry->d_parent->d_inode;
	struct dmfs_i *dmi = DMFS_I(parent);

	if (file->f_mode & FMODE_WRITE) {
		if (get_exclusive_write_access(parent))
			return -EPERM;

		if (!dmi->md->suspended) {
			put_write_access(parent);
			return -EPERM;
		}
	}

	return 0;
}

static int dmfs_table_sync(struct file *file, struct dentry *dentry,
			   int datasync)
{
	return 0;
}

static int dmfs_table_revalidate(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	struct inode *parent = dentry->d_parent->d_inode;

	inode->i_size = parent->i_size;

	return 0;
}

struct address_space_operations dmfs_address_space_operations = {
	readpage:	dmfs_readpage,
	writepage:	fail_writepage,
	prepare_write:	dmfs_prepare_write,
	commit_write:	dmfs_commit_write
};

static struct file_operations dmfs_table_file_operations = {
	llseek:		generic_file_llseek,
	read:		generic_file_read,
	write:		generic_file_write,
	open:		dmfs_table_open,
	release:	dmfs_table_release,
	fsync:		dmfs_table_sync
};

static struct inode_operations dmfs_table_inode_operations = {
	revalidate:	dmfs_table_revalidate
};

struct inode *dmfs_create_table(struct inode *dir, int mode)
{
	struct inode *inode = dmfs_new_inode(dir->i_sb, mode | S_IFREG);

	if (inode) {
		inode->i_mapping = dir->i_mapping;
		inode->i_mapping->a_ops = &dmfs_address_space_operations;
		inode->i_fop = &dmfs_table_file_operations;
		inode->i_op = &dmfs_table_inode_operations;
	}

	return inode;
}