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
|
/*
* undo_io.c --- This is the undo io manager that copies the old data that
* copies the old data being overwritten into a tdb database
*
* Copyright IBM Corporation, 2007
* Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Library
* General Public License, version 2.
* %End-Header%
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include <fcntl.h>
#include <time.h>
#ifdef __linux__
#include <sys/utsname.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include "tdb.h"
#include "ext2_fs.h"
#include "ext2fs.h"
#ifdef __GNUC__
#define ATTR(x) __attribute__(x)
#else
#define ATTR(x)
#endif
/*
* For checking structure magic numbers...
*/
#define EXT2_CHECK_MAGIC(struct, code) \
if ((struct)->magic != (code)) return (code)
struct undo_private_data {
int magic;
TDB_CONTEXT *tdb;
char *tdb_file;
/* The backing io channel */
io_channel real;
int tdb_data_size;
int tdb_written;
/* to support offset in unix I/O manager */
ext2_loff_t offset;
};
static errcode_t undo_open(const char *name, int flags, io_channel *channel);
static errcode_t undo_close(io_channel channel);
static errcode_t undo_set_blksize(io_channel channel, int blksize);
static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
int count, void *data);
static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
int count, const void *data);
static errcode_t undo_read_blk(io_channel channel, unsigned long block,
int count, void *data);
static errcode_t undo_write_blk(io_channel channel, unsigned long block,
int count, const void *data);
static errcode_t undo_flush(io_channel channel);
static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
int size, const void *data);
static errcode_t undo_set_option(io_channel channel, const char *option,
const char *arg);
static errcode_t undo_get_stats(io_channel channel, io_stats *stats);
static struct struct_io_manager struct_undo_manager = {
EXT2_ET_MAGIC_IO_MANAGER,
"Undo I/O Manager",
undo_open,
undo_close,
undo_set_blksize,
undo_read_blk,
undo_write_blk,
undo_flush,
undo_write_byte,
undo_set_option,
undo_get_stats,
undo_read_blk64,
undo_write_blk64,
};
io_manager undo_io_manager = &struct_undo_manager;
static io_manager undo_io_backing_manager ;
static char *tdb_file;
static int actual_size;
static unsigned char mtime_key[] = "filesystem MTIME";
static unsigned char blksize_key[] = "filesystem BLKSIZE";
static unsigned char uuid_key[] = "filesystem UUID";
errcode_t set_undo_io_backing_manager(io_manager manager)
{
/*
* We may want to do some validation later
*/
undo_io_backing_manager = manager;
return 0;
}
errcode_t set_undo_io_backup_file(char *file_name)
{
tdb_file = strdup(file_name);
if (tdb_file == NULL) {
return EXT2_ET_NO_MEMORY;
}
return 0;
}
static errcode_t write_file_system_identity(io_channel undo_channel,
TDB_CONTEXT *tdb)
{
errcode_t retval;
struct ext2_super_block super;
TDB_DATA tdb_key, tdb_data;
struct undo_private_data *data;
io_channel channel;
int block_size ;
data = (struct undo_private_data *) undo_channel->private_data;
channel = data->real;
block_size = channel->block_size;
io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
if (retval)
goto err_out;
/* Write to tdb file in the file system byte order */
tdb_key.dptr = mtime_key;
tdb_key.dsize = sizeof(mtime_key);
tdb_data.dptr = (unsigned char *) &(super.s_mtime);
tdb_data.dsize = sizeof(super.s_mtime);
retval = tdb_store(tdb, tdb_key, tdb_data, TDB_INSERT);
if (retval == -1) {
retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
goto err_out;
}
tdb_key.dptr = uuid_key;
tdb_key.dsize = sizeof(uuid_key);
tdb_data.dptr = (unsigned char *)&(super.s_uuid);
tdb_data.dsize = sizeof(super.s_uuid);
retval = tdb_store(tdb, tdb_key, tdb_data, TDB_INSERT);
if (retval == -1) {
retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
}
err_out:
io_channel_set_blksize(channel, block_size);
return retval;
}
static errcode_t write_block_size(TDB_CONTEXT *tdb, int block_size)
{
errcode_t retval;
TDB_DATA tdb_key, tdb_data;
tdb_key.dptr = blksize_key;
tdb_key.dsize = sizeof(blksize_key);
tdb_data.dptr = (unsigned char *)&(block_size);
tdb_data.dsize = sizeof(block_size);
retval = tdb_store(tdb, tdb_key, tdb_data, TDB_INSERT);
if (retval == -1) {
retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
}
return retval;
}
static errcode_t undo_write_tdb(io_channel channel,
unsigned long long block, int count)
{
int size, sz;
unsigned long long block_num, backing_blk_num;
errcode_t retval = 0;
ext2_loff_t offset;
struct undo_private_data *data;
TDB_DATA tdb_key, tdb_data;
unsigned char *read_ptr;
unsigned long long end_block;
data = (struct undo_private_data *) channel->private_data;
if (data->tdb == NULL) {
/*
* Transaction database not initialized
*/
return 0;
}
if (count == 1)
size = channel->block_size;
else {
if (count < 0)
size = -count;
else
size = count * channel->block_size;
}
/*
* Data is stored in tdb database as blocks of tdb_data_size size
* This helps in efficient lookup further.
*
* We divide the disk to blocks of tdb_data_size.
*/
offset = (block * channel->block_size) + data->offset ;
block_num = offset / data->tdb_data_size;
end_block = (offset + size) / data->tdb_data_size;
tdb_transaction_start(data->tdb);
while (block_num <= end_block ) {
tdb_key.dptr = (unsigned char *)&block_num;
tdb_key.dsize = sizeof(block_num);
/*
* Check if we have the record already
*/
if (tdb_exists(data->tdb, tdb_key)) {
/* Try the next block */
block_num++;
continue;
}
/*
* Read one block using the backing I/O manager
* The backing I/O manager block size may be
* different from the tdb_data_size.
* Also we need to recalcuate the block number with respect
* to the backing I/O manager.
*/
offset = block_num * data->tdb_data_size;
backing_blk_num = (offset - data->offset) / channel->block_size;
count = data->tdb_data_size +
((offset - data->offset) % channel->block_size);
retval = ext2fs_get_mem(count, &read_ptr);
if (retval) {
tdb_transaction_cancel(data->tdb);
return retval;
}
memset(read_ptr, 0, count);
actual_size = 0;
if ((count % channel->block_size) == 0)
sz = count / channel->block_size;
else
sz = -count;
retval = io_channel_read_blk64(data->real, backing_blk_num,
sz, read_ptr);
if (retval) {
if (retval != EXT2_ET_SHORT_READ) {
free(read_ptr);
tdb_transaction_cancel(data->tdb);
return retval;
}
/*
* short read so update the record size
* accordingly
*/
tdb_data.dsize = actual_size;
} else {
tdb_data.dsize = data->tdb_data_size;
}
tdb_data.dptr = read_ptr +
((offset - data->offset) % channel->block_size);
#ifdef DEBUG
printf("Printing with key %lld data %x and size %d\n",
block_num,
tdb_data.dptr,
tdb_data.dsize);
#endif
if (!data->tdb_written) {
data->tdb_written = 1;
/* Write the blocksize to tdb file */
retval = write_block_size(data->tdb,
data->tdb_data_size);
if (retval) {
tdb_transaction_cancel(data->tdb);
retval = EXT2_ET_TDB_ERR_IO;
free(read_ptr);
return retval;
}
}
retval = tdb_store(data->tdb, tdb_key, tdb_data, TDB_INSERT);
if (retval == -1) {
/*
* TDB_ERR_EXISTS cannot happen because we
* have already verified it doesn't exist
*/
tdb_transaction_cancel(data->tdb);
retval = EXT2_ET_TDB_ERR_IO;
free(read_ptr);
return retval;
}
free(read_ptr);
/* Next block */
block_num++;
}
tdb_transaction_commit(data->tdb);
return retval;
}
static errcode_t undo_io_read_error(io_channel channel ATTR((unused)),
unsigned long block ATTR((unused)),
int count ATTR((unused)),
void *data ATTR((unused)),
size_t size ATTR((unused)),
int actual,
errcode_t error ATTR((unused)))
{
actual_size = actual;
return error;
}
static void undo_err_handler_init(io_channel channel)
{
channel->read_error = undo_io_read_error;
}
static errcode_t undo_open(const char *name, int flags, io_channel *channel)
{
io_channel io = NULL;
struct undo_private_data *data = NULL;
errcode_t retval;
if (name == 0)
return EXT2_ET_BAD_DEVICE_NAME;
retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
if (retval)
goto cleanup;
memset(io, 0, sizeof(struct struct_io_channel));
io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
retval = ext2fs_get_mem(sizeof(struct undo_private_data), &data);
if (retval)
goto cleanup;
io->manager = undo_io_manager;
retval = ext2fs_get_mem(strlen(name)+1, &io->name);
if (retval)
goto cleanup;
strcpy(io->name, name);
io->private_data = data;
io->block_size = 1024;
io->read_error = 0;
io->write_error = 0;
io->refcount = 1;
memset(data, 0, sizeof(struct undo_private_data));
data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
if (undo_io_backing_manager) {
retval = undo_io_backing_manager->open(name, flags,
&data->real);
if (retval)
goto cleanup;
} else {
data->real = 0;
}
/* setup the tdb file */
data->tdb = tdb_open(tdb_file, 0, TDB_CLEAR_IF_FIRST,
O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
if (!data->tdb) {
retval = errno;
goto cleanup;
}
/*
* setup err handler for read so that we know
* when the backing manager fails do short read
*/
if (data->real)
undo_err_handler_init(data->real);
*channel = io;
return 0;
cleanup:
if (data && data->real)
io_channel_close(data->real);
if (data)
ext2fs_free_mem(&data);
if (io)
ext2fs_free_mem(&io);
return retval;
}
static errcode_t undo_close(io_channel channel)
{
struct undo_private_data *data;
errcode_t retval = 0;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (--channel->refcount > 0)
return 0;
/* Before closing write the file system identity */
retval = write_file_system_identity(channel, data->tdb);
if (retval)
return retval;
if (data->real)
retval = io_channel_close(data->real);
if (data->tdb)
tdb_close(data->tdb);
ext2fs_free_mem(&channel->private_data);
if (channel->name)
ext2fs_free_mem(&channel->name);
ext2fs_free_mem(&channel);
return retval;
}
static errcode_t undo_set_blksize(io_channel channel, int blksize)
{
struct undo_private_data *data;
errcode_t retval = 0;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (data->real)
retval = io_channel_set_blksize(data->real, blksize);
/*
* Set the block size used for tdb
*/
if (!data->tdb_data_size) {
data->tdb_data_size = blksize;
}
channel->block_size = blksize;
return retval;
}
static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
int count, void *buf)
{
errcode_t retval = 0;
struct undo_private_data *data;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (data->real)
retval = io_channel_read_blk64(data->real, block, count, buf);
return retval;
}
static errcode_t undo_read_blk(io_channel channel, unsigned long block,
int count, void *buf)
{
return undo_read_blk64(channel, block, count, buf);
}
static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
int count, const void *buf)
{
struct undo_private_data *data;
errcode_t retval = 0;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
/*
* First write the existing content into database
*/
retval = undo_write_tdb(channel, block, count);
if (retval)
return retval;
if (data->real)
retval = io_channel_write_blk64(data->real, block, count, buf);
return retval;
}
static errcode_t undo_write_blk(io_channel channel, unsigned long block,
int count, const void *buf)
{
return undo_write_blk64(channel, block, count, buf);
}
static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
int size, const void *buf)
{
struct undo_private_data *data;
errcode_t retval = 0;
ext2_loff_t location;
unsigned long blk_num, count;;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
location = offset + data->offset;
blk_num = location/channel->block_size;
/*
* the size specified may spread across multiple blocks
* also make sure we account for the fact that block start
* offset for tdb is different from the backing I/O manager
* due to possible different block size
*/
count = (size + (location % channel->block_size) +
channel->block_size -1)/channel->block_size;
retval = undo_write_tdb(channel, blk_num, count);
if (retval)
return retval;
if (data->real && data->real->manager->write_byte)
retval = io_channel_write_byte(data->real, offset, size, buf);
return retval;
}
/*
* Flush data buffers to disk.
*/
static errcode_t undo_flush(io_channel channel)
{
errcode_t retval = 0;
struct undo_private_data *data;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (data->real)
retval = io_channel_flush(data->real);
return retval;
}
static errcode_t undo_set_option(io_channel channel, const char *option,
const char *arg)
{
errcode_t retval = 0;
struct undo_private_data *data;
unsigned long tmp;
char *end;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (!strcmp(option, "tdb_data_size")) {
if (!arg)
return EXT2_ET_INVALID_ARGUMENT;
tmp = strtoul(arg, &end, 0);
if (*end)
return EXT2_ET_INVALID_ARGUMENT;
if (!data->tdb_data_size || !data->tdb_written) {
data->tdb_data_size = tmp;
}
return 0;
}
/*
* Need to support offset option to work with
* Unix I/O manager
*/
if (data->real && data->real->manager->set_option) {
retval = data->real->manager->set_option(data->real,
option, arg);
}
if (!retval && !strcmp(option, "offset")) {
if (!arg)
return EXT2_ET_INVALID_ARGUMENT;
tmp = strtoul(arg, &end, 0);
if (*end)
return EXT2_ET_INVALID_ARGUMENT;
data->offset = tmp;
}
return retval;
}
static errcode_t undo_get_stats(io_channel channel, io_stats *stats)
{
errcode_t retval = 0;
struct undo_private_data *data;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct undo_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (data->real)
retval = (data->real->manager->get_stats)(data->real, stats);
return retval;
}
|