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)
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 memset(data->keyb, 0, data->tdb_data_size);
191 data->keys_in_block = 0;
192 data->key_blk_num = data->undo_blk_num;
195 /* Prepare superblock for write */
196 channel = data->real;
197 block_size = channel->block_size;
199 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
200 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
203 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)&super, SUPERBLOCK_SIZE);
204 super.s_magic = ~super.s_magic;
206 /* Write the undo header to disk. */
207 memcpy(data->hdr.magic, E2UNDO_MAGIC, sizeof(data->hdr.magic));
208 data->hdr.num_keys = ext2fs_cpu_to_le64(data->num_keys);
209 data->hdr.super_offset = ext2fs_cpu_to_le64(data->super_blk_num);
210 data->hdr.key_offset = ext2fs_cpu_to_le64(data->first_key_blk);
211 data->hdr.fs_block_size = ext2fs_cpu_to_le32(block_size);
212 data->hdr.sb_crc = ext2fs_cpu_to_le32(sb_crc);
213 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&data->hdr,
215 sizeof(data->hdr.header_crc));
216 data->hdr.header_crc = ext2fs_cpu_to_le32(hdr_crc);
217 retval = io_channel_write_blk64(data->undo_file, 0,
218 -(int)sizeof(data->hdr),
224 * Record the entire superblock (in FS byte order) so that we can't
225 * apply e2undo files to the wrong FS or out of order.
227 dbg_printf("Writing superblock to block %llu\n", data->super_blk_num);
228 retval = io_channel_write_blk64(data->undo_file, data->super_blk_num,
229 -SUPERBLOCK_SIZE, &super);
233 retval = io_channel_flush(data->undo_file);
235 io_channel_set_blksize(channel, block_size);
239 static errcode_t undo_setup_tdb(struct undo_private_data *data)
244 if (data->tdb_written == 1)
247 data->tdb_written = 1;
249 /* Make a bitmap to track what we've written */
250 memset(&data->fake_fs, 0, sizeof(data->fake_fs));
251 data->fake_fs.blocksize = data->tdb_data_size;
252 retval = ext2fs_alloc_generic_bmap(&data->fake_fs,
253 EXT2_ET_MAGIC_BLOCK_BITMAP64,
254 EXT2FS_BMAP64_RBTREE,
256 "undo block map", &data->written_block_map);
260 /* Allocate key block */
261 retval = ext2fs_get_mem(data->tdb_data_size, &data->keyb);
264 data->key_blk_num = data->undo_blk_num;
266 /* Record block size */
267 dbg_printf("Undo block size %llu\n", data->tdb_data_size);
268 dbg_printf("Keys per block %llu\n", KEYS_PER_BLOCK(data));
269 data->hdr.block_size = ext2fs_cpu_to_le32(data->tdb_data_size);
270 io_channel_set_blksize(data->undo_file, data->tdb_data_size);
272 /* Ensure that we have space for header blocks */
273 for (i = 0; i <= 2; i++) {
274 retval = io_channel_read_blk64(data->undo_file, i, 1,
277 memset(data->keyb, 0, data->tdb_data_size);
278 retval = io_channel_write_blk64(data->undo_file, i, 1,
282 retval = io_channel_flush(data->undo_file);
286 memset(data->keyb, 0, data->tdb_data_size);
290 static errcode_t undo_write_tdb(io_channel channel,
291 unsigned long long block, int count)
295 unsigned long long block_num, backing_blk_num;
296 errcode_t retval = 0;
298 struct undo_private_data *data;
299 unsigned char *read_ptr;
300 unsigned long long end_block;
301 unsigned long long data_size;
303 struct undo_key *key;
306 data = (struct undo_private_data *) channel->private_data;
308 if (data->undo_file == NULL) {
310 * Transaction database not initialized
316 size = channel->block_size;
321 size = count * channel->block_size;
324 retval = undo_setup_tdb(data);
328 * Data is stored in tdb database as blocks of tdb_data_size size
329 * This helps in efficient lookup further.
331 * We divide the disk to blocks of tdb_data_size.
333 offset = (block * channel->block_size) + data->offset ;
334 block_num = offset / data->tdb_data_size;
335 end_block = (offset + size - 1) / data->tdb_data_size;
337 while (block_num <= end_block) {
341 * Check if we have the record already
343 if (ext2fs_test_block_bitmap2(data->written_block_map,
345 /* Try the next block */
349 ext2fs_mark_block_bitmap2(data->written_block_map, block_num);
351 /* Spit out a key block */
352 if (data->keys_in_block == KEYS_PER_BLOCK(data)) {
353 retval = write_undo_indexes(data);
356 retval = io_channel_write_blk64(data->undo_file,
357 data->key_blk_num, 1,
363 /* Allocate new key block */
364 if (data->keys_in_block == 0)
365 data->undo_blk_num++;
368 * Read one block using the backing I/O manager
369 * The backing I/O manager block size may be
370 * different from the tdb_data_size.
371 * Also we need to recalcuate the block number with respect
372 * to the backing I/O manager.
374 offset = block_num * data->tdb_data_size;
375 backing_blk_num = (offset - data->offset) / channel->block_size;
377 count = data->tdb_data_size +
378 ((offset - data->offset) % channel->block_size);
379 retval = ext2fs_get_mem(count, &read_ptr);
384 memset(read_ptr, 0, count);
386 if ((count % channel->block_size) == 0)
387 sz = count / channel->block_size;
390 retval = io_channel_read_blk64(data->real, backing_blk_num,
393 if (retval != EXT2_ET_SHORT_READ) {
398 * short read so update the record size
401 data_size = actual_size;
403 data_size = data->tdb_data_size;
405 if (data_size == 0) {
410 dbg_printf("Read %llu bytes from FS block %llu (blk=%llu cnt=%u)\n",
411 data_size, backing_blk_num, block, count);
412 if ((data_size % data->undo_file->block_size) == 0)
413 sz = data_size / data->undo_file->block_size;
416 data_ptr = read_ptr + ((offset - data->offset) %
417 data->undo_file->block_size);
418 /* extend this key? */
419 if (data->keys_in_block) {
420 key = data->keyb->keys + data->keys_in_block - 1;
421 keysz = ext2fs_le32_to_cpu(key->size);
427 ext2fs_le64_to_cpu(key->fsblk) +
428 ((keysz + data->tdb_data_size - 1) /
429 data->tdb_data_size) == backing_blk_num &&
430 E2UNDO_MAX_EXTENT_BLOCKS * data->tdb_data_size >
432 blk_crc = ext2fs_le32_to_cpu(key->blk_crc);
433 blk_crc = ext2fs_crc32c_le(blk_crc,
434 (unsigned char *)data_ptr,
436 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
437 key->size = ext2fs_cpu_to_le32(keysz + data_size);
440 key = data->keyb->keys + data->keys_in_block;
441 data->keys_in_block++;
442 key->fsblk = ext2fs_cpu_to_le64(backing_blk_num);
443 blk_crc = ext2fs_crc32c_le(~0,
444 (unsigned char *)data_ptr,
446 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
447 key->size = ext2fs_cpu_to_le32(data_size);
449 dbg_printf("Writing block %llu to offset %llu size %d key %zu\n",
452 sz, data->num_keys - 1);
453 retval = io_channel_write_blk64(data->undo_file,
454 data->undo_blk_num, sz, data_ptr);
459 data->undo_blk_num++;
468 static errcode_t undo_io_read_error(io_channel channel ATTR((unused)),
469 unsigned long block ATTR((unused)),
470 int count ATTR((unused)),
471 void *data ATTR((unused)),
472 size_t size ATTR((unused)),
474 errcode_t error ATTR((unused)))
476 actual_size = actual;
480 static void undo_err_handler_init(io_channel channel)
482 channel->read_error = undo_io_read_error;
485 static int check_filesystem(struct undo_header *hdr, io_channel undo_file,
486 unsigned int blocksize, blk64_t super_block,
489 struct ext2_super_block super, *sb;
494 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
495 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
500 * Compare the FS and the undo file superblock so that we don't
501 * append to something that doesn't match this FS.
503 retval = ext2fs_get_mem(blocksize, &buf);
506 retval = io_channel_read_blk64(undo_file, super_block,
507 -SUPERBLOCK_SIZE, buf);
510 sb = (struct ext2_super_block *)buf;
511 sb->s_magic = ~sb->s_magic;
512 if (memcmp(&super, buf, sizeof(super))) {
516 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, SUPERBLOCK_SIZE);
517 if (ext2fs_le32_to_cpu(hdr->sb_crc) != sb_crc) {
523 ext2fs_free_mem(&buf);
528 * Try to re-open the undo file, so that we can resume where we left off.
529 * That way, the user can pass the same undo file to various programs as
530 * part of an FS upgrade instead of having to create multiple files and
531 * then apply them in correct order.
533 static errcode_t try_reopen_undo_file(int undo_fd,
534 struct undo_private_data *data)
536 struct undo_header hdr;
537 struct undo_key *dkey;
538 ext2fs_struct_stat statbuf;
539 unsigned int blocksize, fs_blocksize;
540 blk64_t super_block, lblk;
541 size_t num_keys, keys_per_block, i;
542 __u32 hdr_crc, key_crc;
545 /* Zero size already? */
546 retval = ext2fs_fstat(undo_fd, &statbuf);
549 if (statbuf.st_size == 0)
552 /* check the file header */
553 retval = io_channel_read_blk64(data->undo_file, 0, -(int)sizeof(hdr),
558 if (memcmp(hdr.magic, E2UNDO_MAGIC,
561 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&hdr,
562 sizeof(struct undo_header) -
564 if (ext2fs_le32_to_cpu(hdr.header_crc) != hdr_crc)
566 blocksize = ext2fs_le32_to_cpu(hdr.block_size);
567 fs_blocksize = ext2fs_le32_to_cpu(hdr.fs_block_size);
568 if (blocksize > E2UNDO_MAX_BLOCK_SIZE ||
569 blocksize < E2UNDO_MIN_BLOCK_SIZE ||
570 !blocksize || !fs_blocksize)
572 super_block = ext2fs_le64_to_cpu(hdr.super_offset);
573 num_keys = ext2fs_le64_to_cpu(hdr.num_keys);
574 io_channel_set_blksize(data->undo_file, blocksize);
575 if (hdr.f_compat || hdr.f_incompat || hdr.f_rocompat)
578 /* Superblock matches this FS? */
579 if (check_filesystem(&hdr, data->undo_file, blocksize, super_block,
581 retval = EXT2_ET_UNDO_FILE_WRONG;
585 /* Try to set ourselves up */
586 data->tdb_data_size = blocksize;
587 retval = undo_setup_tdb(data);
590 data->num_keys = num_keys;
591 data->super_blk_num = super_block;
592 data->first_key_blk = ext2fs_le64_to_cpu(hdr.key_offset);
594 /* load the written block map */
595 keys_per_block = KEYS_PER_BLOCK(data);
596 lblk = data->first_key_blk;
597 dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n",
598 num_keys, keys_per_block, blocksize);
599 for (i = 0; i < num_keys; i += keys_per_block) {
603 data->key_blk_num = lblk;
604 retval = io_channel_read_blk64(data->undo_file,
605 lblk, 1, data->keyb);
610 if (ext2fs_le32_to_cpu(data->keyb->magic) != KEYBLOCK_MAGIC) {
611 retval = EXT2_ET_UNDO_FILE_CORRUPT;
614 crc = data->keyb->crc;
616 key_crc = ext2fs_crc32c_le(~0, (unsigned char *)data->keyb,
618 if (ext2fs_le32_to_cpu(crc) != key_crc) {
619 retval = EXT2_ET_UNDO_FILE_CORRUPT;
623 /* load keys from key block */
625 max_j = data->num_keys - i;
626 if (max_j > keys_per_block)
627 max_j = keys_per_block;
628 for (j = 0, dkey = data->keyb->keys;
631 blk64_t fsblk = ext2fs_le64_to_cpu(dkey->fsblk);
632 blk64_t undo_blk = fsblk * fs_blocksize / blocksize;
633 size_t size = ext2fs_le32_to_cpu(dkey->size);
635 ext2fs_mark_block_bitmap_range2(data->written_block_map,
637 (size + blocksize - 1) / blocksize);
638 lblk += (size + blocksize - 1) / blocksize;
639 data->undo_blk_num = lblk;
640 data->keys_in_block = j + 1;
643 dbg_printf("Reopen undo, keyblk=%llu undoblk=%llu nrkeys=%zu kib=%zu\n",
644 data->key_blk_num, data->undo_blk_num, data->num_keys,
645 data->keys_in_block);
647 data->hdr.state = hdr.state & ~E2UNDO_STATE_FINISHED;
648 data->hdr.f_compat = hdr.f_compat;
649 data->hdr.f_incompat = hdr.f_incompat;
650 data->hdr.f_rocompat = hdr.f_rocompat;
654 data->key_blk_num = data->undo_blk_num = 0;
655 data->keys_in_block = 0;
656 ext2fs_free_mem(&data->keyb);
657 ext2fs_free_generic_bitmap(data->written_block_map);
658 data->tdb_written = 0;
661 retval = EXT2_ET_UNDO_FILE_CORRUPT;
666 static void undo_atexit(void *p)
668 struct undo_private_data *data = p;
671 err = write_undo_indexes(data);
672 io_channel_close(data->undo_file);
674 com_err(data->tdb_file, err, "while force-closing undo file");
677 static errcode_t undo_open(const char *name, int flags, io_channel *channel)
679 io_channel io = NULL;
680 struct undo_private_data *data = NULL;
685 return EXT2_ET_BAD_DEVICE_NAME;
686 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
689 memset(io, 0, sizeof(struct struct_io_channel));
690 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
691 retval = ext2fs_get_mem(sizeof(struct undo_private_data), &data);
695 io->manager = undo_io_manager;
696 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
700 strcpy(io->name, name);
701 io->private_data = data;
702 io->block_size = 1024;
707 memset(data, 0, sizeof(struct undo_private_data));
708 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
709 data->super_blk_num = 1;
710 data->undo_blk_num = data->first_key_blk = 2;
712 if (undo_io_backing_manager) {
713 retval = undo_io_backing_manager->open(name, flags,
718 data->tdb_file = strdup(tdb_file);
719 if (data->tdb_file == NULL)
721 undo_fd = ext2fs_open_file(data->tdb_file, O_RDWR | O_CREAT,
726 retval = undo_io_backing_manager->open(data->tdb_file,
733 data->undo_file = NULL;
737 io->flags = (io->flags & ~CHANNEL_FLAGS_DISCARD_ZEROES) |
738 (data->real->flags & CHANNEL_FLAGS_DISCARD_ZEROES);
741 * setup err handler for read so that we know
742 * when the backing manager fails do short read
745 undo_err_handler_init(data->real);
747 if (data->undo_file) {
748 retval = try_reopen_undo_file(undo_fd, data);
752 retval = ext2fs_add_exit_fn(undo_atexit, data);
762 ext2fs_remove_exit_fn(undo_atexit, data);
765 if (data && data->undo_file)
766 io_channel_close(data->undo_file);
767 if (data && data->tdb_file)
768 free(data->tdb_file);
769 if (data && data->real)
770 io_channel_close(data->real);
772 ext2fs_free_mem(&data);
774 ext2fs_free_mem(&io);
778 static errcode_t undo_close(io_channel channel)
780 struct undo_private_data *data;
781 errcode_t err, retval = 0;
783 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
784 data = (struct undo_private_data *) channel->private_data;
785 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
787 if (--channel->refcount > 0)
789 /* Before closing write the file system identity */
790 if (!getenv("UNDO_IO_SIMULATE_UNFINISHED"))
791 data->hdr.state = ext2fs_cpu_to_le32(E2UNDO_STATE_FINISHED);
792 err = write_undo_indexes(data);
793 ext2fs_remove_exit_fn(undo_atexit, data);
795 retval = io_channel_close(data->real);
797 free(data->tdb_file);
799 io_channel_close(data->undo_file);
800 ext2fs_free_mem(&data->keyb);
801 if (data->written_block_map)
802 ext2fs_free_generic_bitmap(data->written_block_map);
803 ext2fs_free_mem(&channel->private_data);
805 ext2fs_free_mem(&channel->name);
806 ext2fs_free_mem(&channel);
813 static errcode_t undo_set_blksize(io_channel channel, int blksize)
815 struct undo_private_data *data;
816 errcode_t retval = 0;
818 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
819 data = (struct undo_private_data *) channel->private_data;
820 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
822 if (blksize > E2UNDO_MAX_BLOCK_SIZE || blksize < E2UNDO_MIN_BLOCK_SIZE)
823 return EXT2_ET_INVALID_ARGUMENT;
826 retval = io_channel_set_blksize(data->real, blksize);
828 * Set the block size used for tdb
830 if (!data->tdb_data_size || !data->tdb_written)
831 data->tdb_data_size = blksize;
832 channel->block_size = blksize;
836 static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
837 int count, void *buf)
839 errcode_t retval = 0;
840 struct undo_private_data *data;
842 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
843 data = (struct undo_private_data *) channel->private_data;
844 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
847 retval = io_channel_read_blk64(data->real, block, count, buf);
852 static errcode_t undo_read_blk(io_channel channel, unsigned long block,
853 int count, void *buf)
855 return undo_read_blk64(channel, block, count, buf);
858 static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
859 int count, const void *buf)
861 struct undo_private_data *data;
862 errcode_t retval = 0;
864 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
865 data = (struct undo_private_data *) channel->private_data;
866 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
868 * First write the existing content into database
870 retval = undo_write_tdb(channel, block, count);
874 retval = io_channel_write_blk64(data->real, block, count, buf);
879 static errcode_t undo_write_blk(io_channel channel, unsigned long block,
880 int count, const void *buf)
882 return undo_write_blk64(channel, block, count, buf);
885 static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
886 int size, const void *buf)
888 struct undo_private_data *data;
889 errcode_t retval = 0;
890 ext2_loff_t location;
891 unsigned long blk_num, count;;
893 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
894 data = (struct undo_private_data *) channel->private_data;
895 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
897 location = offset + data->offset;
898 blk_num = location/channel->block_size;
900 * the size specified may spread across multiple blocks
901 * also make sure we account for the fact that block start
902 * offset for tdb is different from the backing I/O manager
903 * due to possible different block size
905 count = (size + (location % channel->block_size) +
906 channel->block_size -1)/channel->block_size;
907 retval = undo_write_tdb(channel, blk_num, count);
910 if (data->real && data->real->manager->write_byte)
911 retval = io_channel_write_byte(data->real, offset, size, buf);
916 static errcode_t undo_discard(io_channel channel, unsigned long long block,
917 unsigned long long count)
919 struct undo_private_data *data;
920 errcode_t retval = 0;
923 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
924 data = (struct undo_private_data *) channel->private_data;
925 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
928 return EXT2_ET_UNIMPLEMENTED;
932 * First write the existing content into database
934 retval = undo_write_tdb(channel, block, icount);
938 retval = io_channel_discard(data->real, block, count);
943 static errcode_t undo_zeroout(io_channel channel, unsigned long long block,
944 unsigned long long count)
946 struct undo_private_data *data;
947 errcode_t retval = 0;
950 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
951 data = (struct undo_private_data *) channel->private_data;
952 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
955 return EXT2_ET_UNIMPLEMENTED;
959 * First write the existing content into database
961 retval = undo_write_tdb(channel, block, icount);
965 retval = io_channel_zeroout(data->real, block, count);
970 static errcode_t undo_cache_readahead(io_channel channel,
971 unsigned long long block,
972 unsigned long long count)
974 struct undo_private_data *data;
975 errcode_t retval = 0;
977 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
978 data = (struct undo_private_data *) channel->private_data;
979 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
982 retval = io_channel_cache_readahead(data->real, block, count);
988 * Flush data buffers to disk.
990 static errcode_t undo_flush(io_channel channel)
992 errcode_t retval = 0;
993 struct undo_private_data *data;
995 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
996 data = (struct undo_private_data *) channel->private_data;
997 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1000 retval = io_channel_flush(data->real);
1005 static errcode_t undo_set_option(io_channel channel, const char *option,
1008 errcode_t retval = 0;
1009 struct undo_private_data *data;
1013 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1014 data = (struct undo_private_data *) channel->private_data;
1015 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1017 if (!strcmp(option, "tdb_data_size")) {
1019 return EXT2_ET_INVALID_ARGUMENT;
1021 tmp = strtoul(arg, &end, 0);
1023 return EXT2_ET_INVALID_ARGUMENT;
1024 if (tmp > E2UNDO_MAX_BLOCK_SIZE || tmp < E2UNDO_MIN_BLOCK_SIZE)
1025 return EXT2_ET_INVALID_ARGUMENT;
1026 if (!data->tdb_data_size || !data->tdb_written) {
1027 data->tdb_written = -1;
1028 data->tdb_data_size = tmp;
1033 * Need to support offset option to work with
1036 if (data->real && data->real->manager->set_option) {
1037 retval = data->real->manager->set_option(data->real,
1040 if (!retval && !strcmp(option, "offset")) {
1042 return EXT2_ET_INVALID_ARGUMENT;
1044 tmp = strtoul(arg, &end, 0);
1046 return EXT2_ET_INVALID_ARGUMENT;
1052 static errcode_t undo_get_stats(io_channel channel, io_stats *stats)
1054 errcode_t retval = 0;
1055 struct undo_private_data *data;
1057 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1058 data = (struct undo_private_data *) channel->private_data;
1059 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1062 retval = (data->real->manager->get_stats)(data->real, stats);
1067 static struct struct_io_manager struct_undo_manager = {
1068 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1069 .name = "Undo I/O Manager",
1071 .close = undo_close,
1072 .set_blksize = undo_set_blksize,
1073 .read_blk = undo_read_blk,
1074 .write_blk = undo_write_blk,
1075 .flush = undo_flush,
1076 .write_byte = undo_write_byte,
1077 .set_option = undo_set_option,
1078 .get_stats = undo_get_stats,
1079 .read_blk64 = undo_read_blk64,
1080 .write_blk64 = undo_write_blk64,
1081 .discard = undo_discard,
1082 .zeroout = undo_zeroout,
1083 .cache_readahead = undo_cache_readahead,
1086 io_manager undo_io_manager = &struct_undo_manager;