File: persistent_inode.c

package info (click to toggle)
unionfs 1.4%2Bdebian-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 924 kB
  • ctags: 929
  • sloc: ansic: 9,905; sh: 2,787; makefile: 253; perl: 200
file content (658 lines) | stat: -rw-r--r-- 15,528 bytes parent folder | download
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
 * Copyright (c) 2003-2006 Erez Zadok
 * Copyright (c) 2003-2006 Charles P. Wright
 * Copyright (c) 2005-2006 Josef Sipek
 * Copyright (c) 2005      Arun M. Krishnakumar
 * Copyright (c) 2005-2006 David P. Quigley
 * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
 * Copyright (c) 2003      Puja Gupta
 * Copyright (c) 2003      Harikesavan Krishnan
 * Copyright (c) 2003-2006 Stony Brook University
 * Copyright (c) 2003-2006 The Research Foundation of State University of New York
 *
 * For specific licensing information, see the COPYING file distributed with
 * this package.
 *
 * This Copyright notice must be kept intact and distributed with all sources.
 */
/*
 *  $Id: persistent_inode.c,v 1.36 2006/07/08 17:58:31 ezk Exp $
 */
#ifdef UNIONFS_IMAP

#include "unionfs.h"

static ssize_t __fread(struct file *filp, void *buf, size_t size, loff_t * pos)
{
	int err;
	mm_segment_t oldfs;
	ssize_t(*func) (struct file *, char __user *, size_t, loff_t *);

	func = do_sync_read;
	if (filp->f_op && filp->f_op->read)
		func = filp->f_op->read;

	oldfs = get_fs();
	set_fs(KERNEL_DS);
	do {
		err = func(filp, (char __user *)buf, size, pos);
	} while (err == -EAGAIN || err == -EINTR);
	set_fs(oldfs);
	return err;
}

static ssize_t __fwrite(struct file *filp, void *buf, size_t size, loff_t * pos)
{
	int err;
	mm_segment_t oldfs;
	unsigned long flim;
	struct rlimit *rl;
	ssize_t(*func) (struct file *, const char __user *, size_t, loff_t *);

	func = do_sync_write;
	if (filp->f_op && filp->f_op->write)
		func = filp->f_op->write;

	/*
	 * it breaks RLIMIT_FSIZE,
	 * but users should be careful to quota.
	 */
	rl = current->signal->rlim + RLIMIT_FSIZE;
	flim = rl->rlim_cur;
	rl->rlim_cur = RLIM_INFINITY;
	oldfs = get_fs();
	set_fs(KERNEL_DS);
	do {
		err = func(filp, (const char __user *)buf, size, pos);
	} while (err == -EAGAIN || err == -EINTR);
	set_fs(oldfs);
	rl->rlim_cur = flim;
	return err;
}

/*
 * verify_forwardmap(super_block *sb)
 * sb: pointer to a superblock containing the forwardmap.
 * returns: 0 on success EINVAL or ENOMEM on failure;
 */
static int verify_forwardmap(struct super_block *sb)
{
	int err = 0, bytesread = 0, bindex = 0, mallocsize = 0;
	loff_t readpos = 0;
	struct file *forwardmap = NULL;
	struct fmaphdr header;
	struct unionfs_sb_info *spd = NULL;
	print_entry_location();

	spd = stopd(sb);
	BUG_ON(!spd);

	forwardmap = spd->usi_forwardmap;
	if (!forwardmap) {
		err = -EINVAL;
		goto out;
	}
	bytesread = __fread(forwardmap, &header, sizeof(struct fmaphdr),
			    &readpos);
	if (bytesread < sizeof(struct fmaphdr)) {
		err = -EINVAL;
		goto out;
	}
	if (header.magic != FORWARDMAP_MAGIC
	    || header.version != FORWARDMAP_VERSION) {
		err = -EINVAL;
		goto out;
	}
	spd->usi_bmap =
	    KMALLOC(sizeof(struct bmapent) * header.usedbranches, GFP_KERNEL);

	if (!spd->usi_bmap) {
		err = -ENOMEM;
		goto out;
	}

	while (bindex < header.usedbranches) {
		bytesread = __fread(forwardmap, &stopd(sb)->usi_bmap[bindex],
				    sizeof(struct bmapent), &readpos);
		if (bytesread < sizeof(struct bmapent)) {
			err = -EINVAL;
			goto out_err;
		}
		bindex++;
	}

	mallocsize = sizeof(int) * header.usedbranches;
	goto out;
      out_err:
	if (spd->usi_bmap)
		KFREE(spd->usi_bmap);
      out:
	print_exit_status(err);
	return err;
}

/*
 * verify_reversemap(struct super_block sb, int rmapindex)
 *
 * sb: The unionfs superblock containing all of the current imap info
 * rmapindex: the index in the usi_reversemaps array that we wish to
 * verify
 *
 * Assumes the reverse maps less than rmapindex are valid.
 *
 * returns: 0 if the opperation succeds
 * 	-EINVAL if the map file does not belong to the forward map
 *
 */
static int verify_reversemap(struct super_block *sb, int rmapindex,
			     struct unionfs_dentry_info *hidden_root_info)
{
	int err = 0, i = 0, bindex = 0, found = 0, bytesread;
	loff_t readpos = 0;
	struct file *forwardmap, *reversemap;
	struct fmaphdr fheader;
	struct rmaphdr rheader;
	struct kstatfs st;
	struct unionfs_sb_info *spd = NULL;

	print_entry_location();

	spd = stopd(sb);
	BUG_ON(!spd);

	forwardmap = spd->usi_forwardmap;
	if (!forwardmap) {
		err = -EINVAL;
		goto out;
	}
	reversemap = spd->usi_reversemaps[rmapindex];
	if (!reversemap) {
		err = -EINVAL;
		goto out;
	}
	bytesread = __fread(forwardmap, &fheader, sizeof(struct fmaphdr),
			    &readpos);
	if (bytesread < sizeof(struct fmaphdr)) {
		err = -EINVAL;
		goto out;
	}
	readpos = 0;
	bytesread = __fread(reversemap, &rheader, sizeof(struct rmaphdr),
			    &readpos);
	if (bytesread < sizeof(struct rmaphdr)) {
		err = -EINVAL;
		goto out;
	}
	if (rheader.magic != REVERSEMAP_MAGIC
	    || rheader.version != REVERSEMAP_VERSION) {
		err = -EINVAL;
		goto out;
	}
	if (memcmp(fheader.uuid, rheader.fwduuid, sizeof(fheader.uuid))) {
		err = -EINVAL;
		goto out;
	}

	/* XXX: Ok so here we take the new map and read the fsid from it. Then
	 * we go through all the branches in the union and see which ones it
	 * matches with*/
	for (i = 0; i < spd->usi_num_bmapents && !found; i++) {
		if (memcmp
		    (rheader.revuuid, spd->usi_bmap[i].uuid,
		     sizeof(rheader.revuuid)))
			continue;

		found = 1;
		for (bindex = 0; bindex <= hidden_root_info->udi_bend; bindex++) {
			struct dentry *d;
			fsid_t fsid;
			dev_t dev;
			memset(&st, 0, sizeof(struct kstatfs));

			d = hidden_root_info->udi_dentry[bindex];

			err = d->d_sb->s_op->statfs(d->d_sb, &st);
			if (err)
				goto out;

			if (st.f_fsid.val[0] || st.f_fsid.val[1]) {
				fsid = st.f_fsid;
			} else {

				dev = d->d_sb->s_dev;
				fsid.val[0] = MAJOR(dev);
				fsid.val[1] = MINOR(dev);
			}

			if (memcmp(&fsid, &rheader.fsid, sizeof(fsid)))
				continue;

			if (spd->usi_bnum_table[bindex] == -1)
				spd->usi_bnum_table[bindex] = i;
			if (spd->usi_map_table[bindex]) {
				printk(KERN_WARNING
				       "Two reverse maps share fsid %u%u!\n",
				       rheader.fsid.val[0],
				       rheader.fsid.val[1]);
				err = -EINVAL;
				goto out;
			} else {
				spd->usi_map_table[bindex] = reversemap;
			}
		}
	}
	if (!found) {
		printk(KERN_WARNING
		       "Could not match the reversemap uuid with an entry in the forwardmap table\n");
		err = -EINVAL;
	}
      out:
	print_exit_status(err);
	return err;
}

int init_imap_data(struct super_block *sb,
		   struct unionfs_dentry_info *hidden_root_info)
{
	int i, err = 0, mallocsize = 0;
	struct unionfs_sb_info *spd;

	print_entry_location();

	spd = stopd(sb);

	spd->usi_forwardmap = NULL;
	spd->usi_reversemaps = NULL;
	spd->usi_bnum_table = NULL;

	mallocsize = sizeof(struct file *) * (hidden_root_info->udi_bend + 1);
	spd->usi_reversemaps = KZALLOC(mallocsize, GFP_KERNEL);
	if (!spd->usi_reversemaps) {
		err = -ENOMEM;
		goto out_error;
	}

	spd->usi_map_table = KZALLOC(mallocsize, GFP_KERNEL);
	if (!spd->usi_map_table) {
		err = -ENOMEM;
		goto out_error;
	}

	mallocsize = sizeof(int) * (hidden_root_info->udi_bend + 1);
	spd->usi_bnum_table = KMALLOC(mallocsize, GFP_KERNEL);
	if (!spd->usi_bnum_table) {
		err = -ENOMEM;
		goto out_error;
	}

	for (i = 0; i <= hidden_root_info->udi_bend; i++) {
		spd->usi_bnum_table[i] = -1;
	}

	if (!err)
		goto out;
      out_error:

	if (spd->usi_reversemaps) {
		KFREE(spd->usi_reversemaps);
		spd->usi_reversemaps = NULL;
	}

	if (spd->usi_map_table) {
		KFREE(spd->usi_map_table);
		spd->usi_map_table = NULL;
	}

	if (spd->usi_bnum_table) {
		KFREE(spd->usi_bnum_table);
		spd->usi_bnum_table = NULL;

	}

      out:
	print_exit_status(err);
	return err;

}

void cleanup_imap_data(struct super_block *sb)
{
	int count = 0;
	struct unionfs_sb_info *spd;

	print_entry_location();

	spd = stopd(sb);

	spd->usi_persistent = 0;
	count = spd->usi_num_bmapents;
	while (count - 1 >= 0) {
		if (spd->usi_reversemaps[count - 1]) {
			filp_close(spd->usi_reversemaps[count - 1], NULL);
			spd->usi_reversemaps[count - 1] = NULL;
		}
		count--;
	}
	if (spd->usi_reversemaps) {
		KFREE(spd->usi_reversemaps);
		spd->usi_reversemaps = NULL;
	}

	if (spd->usi_map_table) {
		KFREE(spd->usi_map_table);
		spd->usi_map_table = NULL;
	}

	if (spd->usi_bnum_table) {
		KFREE(spd->usi_bnum_table);
		spd->usi_bnum_table = NULL;
	}
	if (spd->usi_forwardmap) {
		filp_close(spd->usi_forwardmap, NULL);
		spd->usi_forwardmap = NULL;
	}
	print_exit_location();
}

int parse_imap_option(struct super_block *sb,
		      struct unionfs_dentry_info *hidden_root_info,
		      char *options)
{
	int count = 0, err = 0;
	char *name;
	struct unionfs_sb_info *spd = NULL;

	print_entry_location();
	spd = stopd(sb);
	BUG_ON(!spd);

	err = init_imap_data(sb, hidden_root_info);
	if (err)
		goto out_error;
	while ((name = strsep(&options, ":")) != NULL) {
		if (!*name)
			continue;
		if (!spd->usi_forwardmap) {
			spd->usi_forwardmap = filp_open(name, O_RDWR, 0);
			if (IS_ERR(spd->usi_forwardmap)) {
				err = PTR_ERR(spd->usi_forwardmap);
				spd->usi_forwardmap = NULL;
				goto out_error;
			}
		} else {
			spd->usi_reversemaps[count] =
			    filp_open(name, O_RDWR, 0);
			if (IS_ERR(spd->usi_reversemaps[count])) {
				err = PTR_ERR(spd->usi_reversemaps[count]);
				spd->usi_reversemaps[count] = NULL;
				goto out_error;

			}
			count++;
		}
	}
	if (count <= 0) {
		printk(KERN_WARNING "unionfs: no reverse maps specified.\n");
		err = -EINVAL;
	}
	if (err)
		goto out_error;

	/* Initialize the super block's next_avail field */
	/* Dave, you can't use 64-bit division here because the i386 doesn't
	 * support it natively.  Instead you need to punt if the size is
	 * greater than unsigned long, and then cast it down.  Then you should
	 * be able to assign to this value, without having these problems. */

	if (spd->usi_forwardmap->f_dentry->d_inode->i_size > ULONG_MAX) {
		err = -EFBIG;
		goto out_error;
	}
	spd->usi_next_avail =
	    ((unsigned long)(spd->usi_forwardmap->f_dentry->d_inode->
			     i_size - (sizeof(struct fmaphdr) +
				       sizeof(struct bmapent[256])))
	     / sizeof(struct fmapent));

	if (spd->usi_next_avail < FIRST_VALID_INODE)
		spd->usi_next_avail = FIRST_VALID_INODE;

	spd->usi_num_bmapents = count;
	err = verify_forwardmap(sb);
	if (err)
		goto out_error;
	while (count > 0) {
		err = verify_reversemap(sb, --count, hidden_root_info);
		if (err)
			goto out_error;
	}
	spd->usi_persistent = 1;

	goto out;

      out_error:
	spd->usi_num_bmapents = count;
	cleanup_imap_data(sb);

      out:
	print_exit_status(err);
	return err;
}

 /*
  * get @ino from @hidden_ino.
  */
static int __read_uin(struct unionfs_sb_info *sbi, ino_t hidden_ino, int bindex,
		      ino_t * ino)
{
	int err;
	struct file *rev;
	loff_t pos;
	ssize_t sz;
	uint64_t ino64;
	const int elmnt = sizeof(ino64);

	rev = sbi->usi_map_table[bindex];
	pos = sizeof(struct rmaphdr) + elmnt * hidden_ino;
	*ino = 0;
	err = 0;
	if (pos + elmnt > rev->f_dentry->d_inode->i_size)
		goto out;

	sz = __fread(rev, &ino64, elmnt, &pos);
	err = sz;
	if (err < 0)
		goto out;
	err = 0;
	*ino = -1;
	if (sz != elmnt || ino64 > *ino)
		err = -EIO;
	*ino = ino64;
      out:
	print_exit_status(err);
	return err;
}

/*
 * put unionfs @ino for @hidden_ino on @bindex.
 */
static int __write_uin(struct unionfs_sb_info *sbi, ino_t ino, int bindex,
		       ino_t hidden_ino)
{
	struct file *fwd, *rev;
	struct fmapent ent;
	loff_t pos;
	ssize_t sz;
	int err;
	uint64_t ino64;
	const int fwdhdr = sizeof(struct fmaphdr) + sizeof(struct bmapent[256]);
	const int fwd_elmnt = sizeof(ent);
	const int rev_elmnt = sizeof(ino64);

	err = -ENOSPC;
	if (ino < FIRST_VALID_INODE)
		goto out;

	fwd = sbi->usi_forwardmap;
	ent.fsnum = sbi->usi_bnum_table[bindex];
	ent.inode = hidden_ino;
	pos = fwdhdr + fwd_elmnt * ino;
	sz = __fwrite(fwd, &ent, fwd_elmnt, &pos);
	err = sz;
	if (err < 0)
		goto out;
	err = -EIO;
	if (sz != fwd_elmnt)
		goto out;

	rev = sbi->usi_map_table[bindex];
	pos = sizeof(struct rmaphdr) + rev_elmnt * hidden_ino;
	ino64 = ino;
	sz = __fwrite(rev, &ino64, rev_elmnt, &pos);
	err = sz;
	if (err < 0)
		goto out;
	err = 0;
	if (sz != rev_elmnt)
		err = -EIO;
      out:
	print_exit_status(err);
	return err;
}

/*
 * read_uin(struct super_block *sb, uint8_t branchnum, ino_t inode_number, int flag, ino_t *uino)
 * fsnum: branch to reference when getting the inode number
 * inode_number: lower level inode number use to reference the proper inode.
 * flag: if set to O_CREAT it will creat the entry if it doesent exist
 * 		 otherwise it will return the existing one.
 * returns: the unionfs inode number either created or retrieved based on
 * 			the information.
 */
int read_uin(struct super_block *sb, uint8_t branchnum, ino_t inode_number,
	     int flag, ino_t * uino)
{
	int err = 0;
	struct unionfs_sb_info *spd;

	print_entry_location();

	spd = stopd(sb);
	BUG_ON(!spd);

	/* Find appropriate reverse map and then read from the required position */
	/* get it from the array. */
	err = __read_uin(spd, inode_number, branchnum, uino);
	if (err || *uino)
		goto out;

	err = -EIO;
	if (!(flag & O_CREAT))
		goto out;

	/* If we haven't found an entry and we have the O_CREAT flag set we want to
	 * create a new entry write it out to the file and return its index
	 */
	mutex_lock(&sb->s_lock);
	*uino = spd->usi_next_avail++;
	err = __write_uin(spd, *uino, branchnum, inode_number);
	if (err)
		spd->usi_next_avail--;
	mutex_unlock(&sb->s_lock);
      out:
	print_exit_status(err);
	return err;
}

int write_uin(struct super_block *sb, ino_t ino, int bindex, ino_t hidden_ino)
{
	int err;

	print_entry_location();
	err = __write_uin(stopd(sb), ino, bindex, hidden_ino);
	print_exit_status(err);
	return err;
}

/*
 * get_lin(ino_t inode_number)
 * inode_number : inode number for the unionfs inode
 * returns: the lower level inode# and branch#
 */
/* entry should use a poiner on the stack. should be staticly allocated one
 * level up*/
int get_lin(struct super_block *sb, ino_t inode_number, struct fmapent *entry)
{
	struct file *forwardmap;
	loff_t seek_size;
	mm_segment_t oldfs;
	int err = 0, bytesread = 0;

	print_entry_location();

	if (!entry) {
		entry = ERR_PTR(-ENOMEM);
		goto out;
	}
	forwardmap = stopd(sb)->usi_forwardmap;
	seek_size =
	    sizeof(struct fmaphdr) + sizeof(struct bmapent[256]) +
	    (sizeof(struct fmapent) * inode_number);
	oldfs = get_fs();
	set_fs(KERNEL_DS);
	bytesread = __fread(forwardmap, entry, sizeof(*entry), &seek_size);
	set_fs(oldfs);
	if (bytesread != sizeof(*entry))
		err = -EINVAL;

      out:
	print_exit_location();
	return err;
}

/*
 * remove_map(struct super_block *sb,int bindex)
 *
 * sb: The super block containing all the current imap info
 * bindex: the index of the branch that is being removed.
 *
 * This assumes that end hasen't been decremented yet.
 *
 * Returns: This function really can't fail. The only thing
 * that could possibly happen is that it will oops but that
 * requires unionfs to be in an inconsistant state which
 * shoulden't happen.
 */
int remove_map(struct super_block *sb, int bindex)
{
	int i;
	struct unionfs_sb_info *spd;

	print_entry_location();

	spd = stopd(sb);
	BUG_ON(!spd);

	for (i = bindex; i < sbend(sb); i++) {
		spd->usi_map_table[i] = spd->usi_map_table[i + 1];
		spd->usi_bnum_table[i] = spd->usi_bnum_table[i + 1];
	}
	return 0;
}

#endif
/*
 *
 * vim:shiftwidth=8
 * vim:tabstop=8
 *
 * For Emacs:
 * Local variables:
 * c-basic-offset: 8
 * c-comment-only-line-offset: 0
 * c-offsets-alist: ((statement-block-intro . +) (knr-argdecl-intro . 0)
 *              (substatement-open . 0) (label . 0) (statement-cont . +))
 * indent-tabs-mode: t
 * tab-width: 8
 * End:
 */