2 * undo_io.c --- This is the undo io manager that copies the old data that
3 * copies the old data being overwritten into a tdb database
5 * Copyright IBM Corporation, 2007
6 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 * This file may be redistributed under the terms of the GNU Library
10 * General Public License, version 2.
14 #define _LARGEFILE_SOURCE
15 #define _LARGEFILE64_SOURCE
29 #include <sys/utsname.h>
35 #include <sys/types.h>
37 #if HAVE_SYS_RESOURCE_H
38 #include <sys/resource.h>
47 #define ATTR(x) __attribute__(x)
55 # define dbg_printf(f, a...) do {printf(f, ## a); fflush(stdout); } while (0)
57 # define dbg_printf(f, a...)
61 * For checking structure magic numbers...
64 #define EXT2_CHECK_MAGIC(struct, code) \
65 if ((struct)->magic != (code)) return (code)
67 * Undo file format: The file is cut up into undo_header.block_size blocks.
68 * The first block contains the header.
69 * The second block contains the superblock.
70 * There is then a repeating series of blocks as follows:
71 * A key block, which contains undo_keys to map the following data blocks.
73 * (Note that there are pointers to the first key block and the sb, so this
74 * order isn't strictly necessary.)
76 #define E2UNDO_MAGIC "E2UNDO02"
77 #define KEYBLOCK_MAGIC 0xCADECADE
79 #define E2UNDO_STATE_FINISHED 0x1 /* undo file is complete */
81 #define E2UNDO_MIN_BLOCK_SIZE 1024 /* undo blocks are no less than 1KB */
82 #define E2UNDO_MAX_BLOCK_SIZE 1048576 /* undo blocks are no more than 1MB */
85 char magic[8]; /* "E2UNDO02" */
86 __le64 num_keys; /* how many keys? */
87 __le64 super_offset; /* where in the file is the superblock copy? */
88 __le64 key_offset; /* where do the key/data block chunks start? */
89 __le32 block_size; /* block size of the undo file */
90 __le32 fs_block_size; /* block size of the target device */
91 __le32 sb_crc; /* crc32c of the superblock */
92 __le32 state; /* e2undo state flags */
93 __le32 f_compat; /* compatible features (none so far) */
94 __le32 f_incompat; /* incompatible features (none so far) */
95 __le32 f_rocompat; /* ro compatible features (none so far) */
96 __u8 padding[448]; /* padding */
97 __le32 header_crc; /* crc32c of this header (but not this field) */
100 #define E2UNDO_MAX_EXTENT_BLOCKS 512 /* max extent size, in blocks */
103 __le64 fsblk; /* where in the fs does the block go */
104 __le32 blk_crc; /* crc32c of the block */
105 __le32 size; /* how many bytes in this block? */
108 struct undo_key_block {
109 __le32 magic; /* KEYBLOCK_MAGIC number */
110 __le32 crc; /* block checksum */
111 __le64 reserved; /* zero */
113 struct undo_key keys[0]; /* keys, which come immediately after */
116 struct undo_private_data {
119 /* the undo file io channel */
120 io_channel undo_file;
121 blk64_t undo_blk_num; /* next free block */
122 blk64_t key_blk_num; /* current key block location */
123 blk64_t super_blk_num; /* superblock location */
124 blk64_t first_key_blk; /* first key block location */
125 struct undo_key_block *keyb;
126 size_t num_keys, keys_in_block;
128 /* The backing io channel */
131 unsigned long long tdb_data_size;
134 /* to support offset in unix I/O manager */
137 ext2fs_block_bitmap written_block_map;
138 struct struct_ext2_filsys fake_fs;
140 struct undo_header hdr;
142 #define KEYS_PER_BLOCK(d) (((d)->tdb_data_size / sizeof(struct undo_key)) - 1)
144 static io_manager undo_io_backing_manager;
145 static char *tdb_file;
146 static int actual_size;
148 errcode_t set_undo_io_backing_manager(io_manager manager)
151 * We may want to do some validation later
153 undo_io_backing_manager = manager;
157 errcode_t set_undo_io_backup_file(char *file_name)
159 tdb_file = strdup(file_name);
161 if (tdb_file == NULL) {
162 return EXT2_ET_NO_MEMORY;
168 static errcode_t write_undo_indexes(struct undo_private_data *data, int flush)
171 struct ext2_super_block super;
174 __u32 sb_crc, hdr_crc;
176 /* Spit out a key block, if there's any data */
177 if (data->keys_in_block) {
178 data->keyb->magic = ext2fs_cpu_to_le32(KEYBLOCK_MAGIC);
180 data->keyb->crc = ext2fs_cpu_to_le32(
182 (unsigned char *)data->keyb,
183 data->tdb_data_size));
184 dbg_printf("Writing keyblock to blk %llu\n", data->key_blk_num);
185 retval = io_channel_write_blk64(data->undo_file,
190 /* Move on to the next key block if it's full. */
191 if (data->keys_in_block == KEYS_PER_BLOCK(data)) {
192 memset(data->keyb, 0, data->tdb_data_size);
193 data->keys_in_block = 0;
194 data->key_blk_num = data->undo_blk_num;
195 data->undo_blk_num++;
199 /* Prepare superblock for write */
200 channel = data->real;
201 block_size = channel->block_size;
203 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
204 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
207 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)&super, SUPERBLOCK_SIZE);
208 super.s_magic = ~super.s_magic;
210 /* Write the undo header to disk. */
211 memcpy(data->hdr.magic, E2UNDO_MAGIC, sizeof(data->hdr.magic));
212 data->hdr.num_keys = ext2fs_cpu_to_le64(data->num_keys);
213 data->hdr.super_offset = ext2fs_cpu_to_le64(data->super_blk_num);
214 data->hdr.key_offset = ext2fs_cpu_to_le64(data->first_key_blk);
215 data->hdr.fs_block_size = ext2fs_cpu_to_le32(block_size);
216 data->hdr.sb_crc = ext2fs_cpu_to_le32(sb_crc);
217 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&data->hdr,
219 sizeof(data->hdr.header_crc));
220 data->hdr.header_crc = ext2fs_cpu_to_le32(hdr_crc);
221 retval = io_channel_write_blk64(data->undo_file, 0,
222 -(int)sizeof(data->hdr),
228 * Record the entire superblock (in FS byte order) so that we can't
229 * apply e2undo files to the wrong FS or out of order.
231 dbg_printf("Writing superblock to block %llu\n", data->super_blk_num);
232 retval = io_channel_write_blk64(data->undo_file, data->super_blk_num,
233 -SUPERBLOCK_SIZE, &super);
238 retval = io_channel_flush(data->undo_file);
240 io_channel_set_blksize(channel, block_size);
244 static errcode_t undo_setup_tdb(struct undo_private_data *data)
249 if (data->tdb_written == 1)
252 data->tdb_written = 1;
254 /* Make a bitmap to track what we've written */
255 memset(&data->fake_fs, 0, sizeof(data->fake_fs));
256 data->fake_fs.blocksize = data->tdb_data_size;
257 retval = ext2fs_alloc_generic_bmap(&data->fake_fs,
258 EXT2_ET_MAGIC_BLOCK_BITMAP64,
259 EXT2FS_BMAP64_RBTREE,
261 "undo block map", &data->written_block_map);
265 /* Allocate key block */
266 retval = ext2fs_get_mem(data->tdb_data_size, &data->keyb);
269 data->key_blk_num = data->first_key_blk;
271 /* Record block size */
272 dbg_printf("Undo block size %llu\n", data->tdb_data_size);
273 dbg_printf("Keys per block %llu\n", KEYS_PER_BLOCK(data));
274 data->hdr.block_size = ext2fs_cpu_to_le32(data->tdb_data_size);
275 io_channel_set_blksize(data->undo_file, data->tdb_data_size);
277 /* Ensure that we have space for header blocks */
278 for (i = 0; i <= 2; i++) {
279 retval = io_channel_read_blk64(data->undo_file, i, 1,
282 memset(data->keyb, 0, data->tdb_data_size);
283 retval = io_channel_write_blk64(data->undo_file, i, 1,
287 retval = io_channel_flush(data->undo_file);
291 memset(data->keyb, 0, data->tdb_data_size);
295 static errcode_t undo_write_tdb(io_channel channel,
296 unsigned long long block, int count)
300 unsigned long long block_num, backing_blk_num;
301 errcode_t retval = 0;
303 struct undo_private_data *data;
304 unsigned char *read_ptr;
305 unsigned long long end_block;
306 unsigned long long data_size;
308 struct undo_key *key;
311 data = (struct undo_private_data *) channel->private_data;
313 if (data->undo_file == NULL) {
315 * Transaction database not initialized
321 size = channel->block_size;
326 size = count * channel->block_size;
329 retval = undo_setup_tdb(data);
333 * Data is stored in tdb database as blocks of tdb_data_size size
334 * This helps in efficient lookup further.
336 * We divide the disk to blocks of tdb_data_size.
338 offset = (block * channel->block_size) + data->offset ;
339 block_num = offset / data->tdb_data_size;
340 end_block = (offset + size - 1) / data->tdb_data_size;
342 while (block_num <= end_block) {
346 * Check if we have the record already
348 if (ext2fs_test_block_bitmap2(data->written_block_map,
350 /* Try the next block */
354 ext2fs_mark_block_bitmap2(data->written_block_map, block_num);
357 * Read one block using the backing I/O manager
358 * The backing I/O manager block size may be
359 * different from the tdb_data_size.
360 * Also we need to recalcuate the block number with respect
361 * to the backing I/O manager.
363 offset = block_num * data->tdb_data_size;
364 backing_blk_num = (offset - data->offset) / channel->block_size;
366 count = data->tdb_data_size +
367 ((offset - data->offset) % channel->block_size);
368 retval = ext2fs_get_mem(count, &read_ptr);
373 memset(read_ptr, 0, count);
375 if ((count % channel->block_size) == 0)
376 sz = count / channel->block_size;
379 retval = io_channel_read_blk64(data->real, backing_blk_num,
382 if (retval != EXT2_ET_SHORT_READ) {
387 * short read so update the record size
390 data_size = actual_size;
392 data_size = data->tdb_data_size;
394 if (data_size == 0) {
399 dbg_printf("Read %llu bytes from FS block %llu (blk=%llu cnt=%u)\n",
400 data_size, backing_blk_num, block, count);
401 if ((data_size % data->undo_file->block_size) == 0)
402 sz = data_size / data->undo_file->block_size;
405 data_ptr = read_ptr + ((offset - data->offset) %
406 data->undo_file->block_size);
407 /* extend this key? */
408 if (data->keys_in_block) {
409 key = data->keyb->keys + data->keys_in_block - 1;
410 keysz = ext2fs_le32_to_cpu(key->size);
416 ext2fs_le64_to_cpu(key->fsblk) +
417 ((keysz + data->tdb_data_size - 1) /
418 data->tdb_data_size) == backing_blk_num &&
419 E2UNDO_MAX_EXTENT_BLOCKS * data->tdb_data_size >
421 blk_crc = ext2fs_le32_to_cpu(key->blk_crc);
422 blk_crc = ext2fs_crc32c_le(blk_crc,
423 (unsigned char *)data_ptr,
425 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
426 key->size = ext2fs_cpu_to_le32(keysz + data_size);
429 key = data->keyb->keys + data->keys_in_block;
430 data->keys_in_block++;
431 key->fsblk = ext2fs_cpu_to_le64(backing_blk_num);
432 blk_crc = ext2fs_crc32c_le(~0,
433 (unsigned char *)data_ptr,
435 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
436 key->size = ext2fs_cpu_to_le32(data_size);
438 dbg_printf("Writing block %llu to offset %llu size %d key %zu\n",
441 sz, data->num_keys - 1);
442 retval = io_channel_write_blk64(data->undo_file,
443 data->undo_blk_num, sz, data_ptr);
448 data->undo_blk_num++;
451 /* Write out the key block */
452 retval = write_undo_indexes(data, 0);
463 static errcode_t undo_io_read_error(io_channel channel ATTR((unused)),
464 unsigned long block ATTR((unused)),
465 int count ATTR((unused)),
466 void *data ATTR((unused)),
467 size_t size ATTR((unused)),
469 errcode_t error ATTR((unused)))
471 actual_size = actual;
475 static void undo_err_handler_init(io_channel channel)
477 channel->read_error = undo_io_read_error;
480 static int check_filesystem(struct undo_header *hdr, io_channel undo_file,
481 unsigned int blocksize, blk64_t super_block,
484 struct ext2_super_block super, *sb;
489 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
490 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
495 * Compare the FS and the undo file superblock so that we don't
496 * append to something that doesn't match this FS.
498 retval = ext2fs_get_mem(blocksize, &buf);
501 retval = io_channel_read_blk64(undo_file, super_block,
502 -SUPERBLOCK_SIZE, buf);
505 sb = (struct ext2_super_block *)buf;
506 sb->s_magic = ~sb->s_magic;
507 if (memcmp(&super, buf, sizeof(super))) {
511 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, SUPERBLOCK_SIZE);
512 if (ext2fs_le32_to_cpu(hdr->sb_crc) != sb_crc) {
518 ext2fs_free_mem(&buf);
523 * Try to re-open the undo file, so that we can resume where we left off.
524 * That way, the user can pass the same undo file to various programs as
525 * part of an FS upgrade instead of having to create multiple files and
526 * then apply them in correct order.
528 static errcode_t try_reopen_undo_file(int undo_fd,
529 struct undo_private_data *data)
531 struct undo_header hdr;
532 struct undo_key *dkey;
533 ext2fs_struct_stat statbuf;
534 unsigned int blocksize, fs_blocksize;
535 blk64_t super_block, lblk;
536 size_t num_keys, keys_per_block, i;
537 __u32 hdr_crc, key_crc;
540 /* Zero size already? */
541 retval = ext2fs_fstat(undo_fd, &statbuf);
544 if (statbuf.st_size == 0)
547 /* check the file header */
548 retval = io_channel_read_blk64(data->undo_file, 0, -(int)sizeof(hdr),
553 if (memcmp(hdr.magic, E2UNDO_MAGIC,
556 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&hdr,
557 sizeof(struct undo_header) -
559 if (ext2fs_le32_to_cpu(hdr.header_crc) != hdr_crc)
561 blocksize = ext2fs_le32_to_cpu(hdr.block_size);
562 fs_blocksize = ext2fs_le32_to_cpu(hdr.fs_block_size);
563 if (blocksize > E2UNDO_MAX_BLOCK_SIZE ||
564 blocksize < E2UNDO_MIN_BLOCK_SIZE ||
565 !blocksize || !fs_blocksize)
567 super_block = ext2fs_le64_to_cpu(hdr.super_offset);
568 num_keys = ext2fs_le64_to_cpu(hdr.num_keys);
569 io_channel_set_blksize(data->undo_file, blocksize);
570 if (hdr.f_compat || hdr.f_incompat || hdr.f_rocompat)
573 /* Superblock matches this FS? */
574 if (check_filesystem(&hdr, data->undo_file, blocksize, super_block,
576 retval = EXT2_ET_UNDO_FILE_WRONG;
580 /* Try to set ourselves up */
581 data->tdb_data_size = blocksize;
582 retval = undo_setup_tdb(data);
585 data->num_keys = num_keys;
586 data->super_blk_num = super_block;
587 data->first_key_blk = ext2fs_le64_to_cpu(hdr.key_offset);
589 /* load the written block map */
590 keys_per_block = KEYS_PER_BLOCK(data);
591 lblk = data->first_key_blk;
592 dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n",
593 num_keys, keys_per_block, blocksize);
594 for (i = 0; i < num_keys; i += keys_per_block) {
598 data->key_blk_num = lblk;
599 retval = io_channel_read_blk64(data->undo_file,
600 lblk, 1, data->keyb);
605 if (ext2fs_le32_to_cpu(data->keyb->magic) != KEYBLOCK_MAGIC) {
606 retval = EXT2_ET_UNDO_FILE_CORRUPT;
609 crc = data->keyb->crc;
611 key_crc = ext2fs_crc32c_le(~0, (unsigned char *)data->keyb,
613 if (ext2fs_le32_to_cpu(crc) != key_crc) {
614 retval = EXT2_ET_UNDO_FILE_CORRUPT;
618 /* load keys from key block */
620 max_j = data->num_keys - i;
621 if (max_j > keys_per_block)
622 max_j = keys_per_block;
623 for (j = 0, dkey = data->keyb->keys;
626 blk64_t fsblk = ext2fs_le64_to_cpu(dkey->fsblk);
627 blk64_t undo_blk = fsblk * fs_blocksize / blocksize;
628 size_t size = ext2fs_le32_to_cpu(dkey->size);
630 ext2fs_mark_block_bitmap_range2(data->written_block_map,
632 (size + blocksize - 1) / blocksize);
633 lblk += (size + blocksize - 1) / blocksize;
634 data->undo_blk_num = lblk;
635 data->keys_in_block = j + 1;
638 dbg_printf("Reopen undo, keyblk=%llu undoblk=%llu nrkeys=%zu kib=%zu\n",
639 data->key_blk_num, data->undo_blk_num, data->num_keys,
640 data->keys_in_block);
642 data->hdr.state = hdr.state & ~E2UNDO_STATE_FINISHED;
643 data->hdr.f_compat = hdr.f_compat;
644 data->hdr.f_incompat = hdr.f_incompat;
645 data->hdr.f_rocompat = hdr.f_rocompat;
649 data->key_blk_num = data->undo_blk_num = 0;
650 data->keys_in_block = 0;
651 ext2fs_free_mem(&data->keyb);
652 ext2fs_free_generic_bitmap(data->written_block_map);
653 data->tdb_written = 0;
656 retval = EXT2_ET_UNDO_FILE_CORRUPT;
661 static void undo_atexit(void *p)
663 struct undo_private_data *data = p;
666 err = write_undo_indexes(data, 1);
667 io_channel_close(data->undo_file);
669 com_err(data->tdb_file, err, "while force-closing undo file");
672 static errcode_t undo_open(const char *name, int flags, io_channel *channel)
674 io_channel io = NULL;
675 struct undo_private_data *data = NULL;
680 return EXT2_ET_BAD_DEVICE_NAME;
681 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
684 memset(io, 0, sizeof(struct struct_io_channel));
685 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
686 retval = ext2fs_get_mem(sizeof(struct undo_private_data), &data);
690 io->manager = undo_io_manager;
691 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
695 strcpy(io->name, name);
696 io->private_data = data;
697 io->block_size = 1024;
702 memset(data, 0, sizeof(struct undo_private_data));
703 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
704 data->super_blk_num = 1;
705 data->first_key_blk = 2;
706 data->undo_blk_num = 3;
708 if (undo_io_backing_manager) {
709 retval = undo_io_backing_manager->open(name, flags,
714 data->tdb_file = strdup(tdb_file);
715 if (data->tdb_file == NULL)
717 undo_fd = ext2fs_open_file(data->tdb_file, O_RDWR | O_CREAT,
722 retval = undo_io_backing_manager->open(data->tdb_file,
729 data->undo_file = NULL;
733 io->flags = (io->flags & ~CHANNEL_FLAGS_DISCARD_ZEROES) |
734 (data->real->flags & CHANNEL_FLAGS_DISCARD_ZEROES);
737 * setup err handler for read so that we know
738 * when the backing manager fails do short read
741 undo_err_handler_init(data->real);
743 if (data->undo_file) {
744 retval = try_reopen_undo_file(undo_fd, data);
748 retval = ext2fs_add_exit_fn(undo_atexit, data);
758 ext2fs_remove_exit_fn(undo_atexit, data);
761 if (data && data->undo_file)
762 io_channel_close(data->undo_file);
763 if (data && data->tdb_file)
764 free(data->tdb_file);
765 if (data && data->real)
766 io_channel_close(data->real);
768 ext2fs_free_mem(&data);
770 ext2fs_free_mem(&io);
774 static errcode_t undo_close(io_channel channel)
776 struct undo_private_data *data;
777 errcode_t err, retval = 0;
779 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
780 data = (struct undo_private_data *) channel->private_data;
781 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
783 if (--channel->refcount > 0)
785 /* Before closing write the file system identity */
786 if (!getenv("UNDO_IO_SIMULATE_UNFINISHED"))
787 data->hdr.state = ext2fs_cpu_to_le32(E2UNDO_STATE_FINISHED);
788 err = write_undo_indexes(data, 1);
789 ext2fs_remove_exit_fn(undo_atexit, data);
791 retval = io_channel_close(data->real);
793 free(data->tdb_file);
795 io_channel_close(data->undo_file);
796 ext2fs_free_mem(&data->keyb);
797 if (data->written_block_map)
798 ext2fs_free_generic_bitmap(data->written_block_map);
799 ext2fs_free_mem(&channel->private_data);
801 ext2fs_free_mem(&channel->name);
802 ext2fs_free_mem(&channel);
809 static errcode_t undo_set_blksize(io_channel channel, int blksize)
811 struct undo_private_data *data;
812 errcode_t retval = 0;
814 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
815 data = (struct undo_private_data *) channel->private_data;
816 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
818 if (blksize > E2UNDO_MAX_BLOCK_SIZE || blksize < E2UNDO_MIN_BLOCK_SIZE)
819 return EXT2_ET_INVALID_ARGUMENT;
822 retval = io_channel_set_blksize(data->real, blksize);
824 * Set the block size used for tdb
826 if (!data->tdb_data_size || !data->tdb_written)
827 data->tdb_data_size = blksize;
828 channel->block_size = blksize;
832 static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
833 int count, void *buf)
835 errcode_t retval = 0;
836 struct undo_private_data *data;
838 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
839 data = (struct undo_private_data *) channel->private_data;
840 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
843 retval = io_channel_read_blk64(data->real, block, count, buf);
848 static errcode_t undo_read_blk(io_channel channel, unsigned long block,
849 int count, void *buf)
851 return undo_read_blk64(channel, block, count, buf);
854 static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
855 int count, const void *buf)
857 struct undo_private_data *data;
858 errcode_t retval = 0;
860 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
861 data = (struct undo_private_data *) channel->private_data;
862 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
864 * First write the existing content into database
866 retval = undo_write_tdb(channel, block, count);
870 retval = io_channel_write_blk64(data->real, block, count, buf);
875 static errcode_t undo_write_blk(io_channel channel, unsigned long block,
876 int count, const void *buf)
878 return undo_write_blk64(channel, block, count, buf);
881 static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
882 int size, const void *buf)
884 struct undo_private_data *data;
885 errcode_t retval = 0;
886 ext2_loff_t location;
887 unsigned long blk_num, count;;
889 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
890 data = (struct undo_private_data *) channel->private_data;
891 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
893 location = offset + data->offset;
894 blk_num = location/channel->block_size;
896 * the size specified may spread across multiple blocks
897 * also make sure we account for the fact that block start
898 * offset for tdb is different from the backing I/O manager
899 * due to possible different block size
901 count = (size + (location % channel->block_size) +
902 channel->block_size -1)/channel->block_size;
903 retval = undo_write_tdb(channel, blk_num, count);
906 if (data->real && data->real->manager->write_byte)
907 retval = io_channel_write_byte(data->real, offset, size, buf);
912 static errcode_t undo_discard(io_channel channel, unsigned long long block,
913 unsigned long long count)
915 struct undo_private_data *data;
916 errcode_t retval = 0;
919 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
920 data = (struct undo_private_data *) channel->private_data;
921 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
924 return EXT2_ET_UNIMPLEMENTED;
928 * First write the existing content into database
930 retval = undo_write_tdb(channel, block, icount);
934 retval = io_channel_discard(data->real, block, count);
939 static errcode_t undo_zeroout(io_channel channel, unsigned long long block,
940 unsigned long long count)
942 struct undo_private_data *data;
943 errcode_t retval = 0;
946 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
947 data = (struct undo_private_data *) channel->private_data;
948 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
951 return EXT2_ET_UNIMPLEMENTED;
955 * First write the existing content into database
957 retval = undo_write_tdb(channel, block, icount);
961 retval = io_channel_zeroout(data->real, block, count);
966 static errcode_t undo_cache_readahead(io_channel channel,
967 unsigned long long block,
968 unsigned long long count)
970 struct undo_private_data *data;
971 errcode_t retval = 0;
973 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
974 data = (struct undo_private_data *) channel->private_data;
975 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
978 retval = io_channel_cache_readahead(data->real, block, count);
984 * Flush data buffers to disk.
986 static errcode_t undo_flush(io_channel channel)
988 errcode_t retval = 0;
989 struct undo_private_data *data;
991 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
992 data = (struct undo_private_data *) channel->private_data;
993 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
996 retval = io_channel_flush(data->real);
1001 static errcode_t undo_set_option(io_channel channel, const char *option,
1004 errcode_t retval = 0;
1005 struct undo_private_data *data;
1009 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1010 data = (struct undo_private_data *) channel->private_data;
1011 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1013 if (!strcmp(option, "tdb_data_size")) {
1015 return EXT2_ET_INVALID_ARGUMENT;
1017 tmp = strtoul(arg, &end, 0);
1019 return EXT2_ET_INVALID_ARGUMENT;
1020 if (tmp > E2UNDO_MAX_BLOCK_SIZE || tmp < E2UNDO_MIN_BLOCK_SIZE)
1021 return EXT2_ET_INVALID_ARGUMENT;
1022 if (!data->tdb_data_size || !data->tdb_written) {
1023 data->tdb_written = -1;
1024 data->tdb_data_size = tmp;
1029 * Need to support offset option to work with
1032 if (data->real && data->real->manager->set_option) {
1033 retval = data->real->manager->set_option(data->real,
1036 if (!retval && !strcmp(option, "offset")) {
1038 return EXT2_ET_INVALID_ARGUMENT;
1040 tmp = strtoul(arg, &end, 0);
1042 return EXT2_ET_INVALID_ARGUMENT;
1048 static errcode_t undo_get_stats(io_channel channel, io_stats *stats)
1050 errcode_t retval = 0;
1051 struct undo_private_data *data;
1053 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1054 data = (struct undo_private_data *) channel->private_data;
1055 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1058 retval = (data->real->manager->get_stats)(data->real, stats);
1063 static struct struct_io_manager struct_undo_manager = {
1064 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1065 .name = "Undo I/O Manager",
1067 .close = undo_close,
1068 .set_blksize = undo_set_blksize,
1069 .read_blk = undo_read_blk,
1070 .write_blk = undo_write_blk,
1071 .flush = undo_flush,
1072 .write_byte = undo_write_byte,
1073 .set_option = undo_set_option,
1074 .get_stats = undo_get_stats,
1075 .read_blk64 = undo_read_blk64,
1076 .write_blk64 = undo_write_blk64,
1077 .discard = undo_discard,
1078 .zeroout = undo_zeroout,
1079 .cache_readahead = undo_cache_readahead,
1082 io_manager undo_io_manager = &struct_undo_manager;