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++;
200 /* Prepare superblock for write */
201 channel = data->real;
202 block_size = channel->block_size;
204 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
205 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
208 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)&super, SUPERBLOCK_SIZE);
209 super.s_magic = ~super.s_magic;
211 /* Write the undo header to disk. */
212 memcpy(data->hdr.magic, E2UNDO_MAGIC, sizeof(data->hdr.magic));
213 data->hdr.num_keys = ext2fs_cpu_to_le64(data->num_keys);
214 data->hdr.super_offset = ext2fs_cpu_to_le64(data->super_blk_num);
215 data->hdr.key_offset = ext2fs_cpu_to_le64(data->first_key_blk);
216 data->hdr.fs_block_size = ext2fs_cpu_to_le32(block_size);
217 data->hdr.sb_crc = ext2fs_cpu_to_le32(sb_crc);
218 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&data->hdr,
220 sizeof(data->hdr.header_crc));
221 data->hdr.header_crc = ext2fs_cpu_to_le32(hdr_crc);
222 retval = io_channel_write_blk64(data->undo_file, 0,
223 -(int)sizeof(data->hdr),
229 * Record the entire superblock (in FS byte order) so that we can't
230 * apply e2undo files to the wrong FS or out of order.
232 dbg_printf("Writing superblock to block %llu\n", data->super_blk_num);
233 retval = io_channel_write_blk64(data->undo_file, data->super_blk_num,
234 -SUPERBLOCK_SIZE, &super);
239 retval = io_channel_flush(data->undo_file);
241 io_channel_set_blksize(channel, block_size);
245 static errcode_t undo_setup_tdb(struct undo_private_data *data)
250 if (data->tdb_written == 1)
253 data->tdb_written = 1;
255 /* Make a bitmap to track what we've written */
256 memset(&data->fake_fs, 0, sizeof(data->fake_fs));
257 data->fake_fs.blocksize = data->tdb_data_size;
258 retval = ext2fs_alloc_generic_bmap(&data->fake_fs,
259 EXT2_ET_MAGIC_BLOCK_BITMAP64,
260 EXT2FS_BMAP64_RBTREE,
262 "undo block map", &data->written_block_map);
266 /* Allocate key block */
267 retval = ext2fs_get_mem(data->tdb_data_size, &data->keyb);
270 data->key_blk_num = data->first_key_blk;
272 /* Record block size */
273 dbg_printf("Undo block size %llu\n", data->tdb_data_size);
274 dbg_printf("Keys per block %llu\n", KEYS_PER_BLOCK(data));
275 data->hdr.block_size = ext2fs_cpu_to_le32(data->tdb_data_size);
276 io_channel_set_blksize(data->undo_file, data->tdb_data_size);
278 /* Ensure that we have space for header blocks */
279 for (i = 0; i <= 2; i++) {
280 retval = io_channel_read_blk64(data->undo_file, i, 1,
283 memset(data->keyb, 0, data->tdb_data_size);
284 retval = io_channel_write_blk64(data->undo_file, i, 1,
288 retval = io_channel_flush(data->undo_file);
292 memset(data->keyb, 0, data->tdb_data_size);
296 static errcode_t undo_write_tdb(io_channel channel,
297 unsigned long long block, int count)
301 unsigned long long block_num, backing_blk_num;
302 errcode_t retval = 0;
304 struct undo_private_data *data;
305 unsigned char *read_ptr;
306 unsigned long long end_block;
307 unsigned long long data_size;
309 struct undo_key *key;
312 data = (struct undo_private_data *) channel->private_data;
314 if (data->undo_file == NULL) {
316 * Transaction database not initialized
322 size = channel->block_size;
327 size = count * channel->block_size;
330 retval = undo_setup_tdb(data);
334 * Data is stored in tdb database as blocks of tdb_data_size size
335 * This helps in efficient lookup further.
337 * We divide the disk to blocks of tdb_data_size.
339 offset = (block * channel->block_size) + data->offset ;
340 block_num = offset / data->tdb_data_size;
341 end_block = (offset + size - 1) / data->tdb_data_size;
343 while (block_num <= end_block) {
347 * Check if we have the record already
349 if (ext2fs_test_block_bitmap2(data->written_block_map,
351 /* Try the next block */
355 ext2fs_mark_block_bitmap2(data->written_block_map, block_num);
358 * Read one block using the backing I/O manager
359 * The backing I/O manager block size may be
360 * different from the tdb_data_size.
361 * Also we need to recalcuate the block number with respect
362 * to the backing I/O manager.
364 offset = block_num * data->tdb_data_size;
365 backing_blk_num = (offset - data->offset) / channel->block_size;
367 count = data->tdb_data_size +
368 ((offset - data->offset) % channel->block_size);
369 retval = ext2fs_get_mem(count, &read_ptr);
374 memset(read_ptr, 0, count);
376 if ((count % channel->block_size) == 0)
377 sz = count / channel->block_size;
380 retval = io_channel_read_blk64(data->real, backing_blk_num,
383 if (retval != EXT2_ET_SHORT_READ) {
388 * short read so update the record size
391 data_size = actual_size;
393 data_size = data->tdb_data_size;
395 if (data_size == 0) {
400 dbg_printf("Read %llu bytes from FS block %llu (blk=%llu cnt=%u)\n",
401 data_size, backing_blk_num, block, count);
402 if ((data_size % data->undo_file->block_size) == 0)
403 sz = data_size / data->undo_file->block_size;
406 data_ptr = read_ptr + ((offset - data->offset) %
407 data->undo_file->block_size);
408 /* extend this key? */
409 if (data->keys_in_block) {
410 key = data->keyb->keys + data->keys_in_block - 1;
411 keysz = ext2fs_le32_to_cpu(key->size);
417 ext2fs_le64_to_cpu(key->fsblk) +
418 ((keysz + data->tdb_data_size - 1) /
419 data->tdb_data_size) == backing_blk_num &&
420 E2UNDO_MAX_EXTENT_BLOCKS * data->tdb_data_size >
422 blk_crc = ext2fs_le32_to_cpu(key->blk_crc);
423 blk_crc = ext2fs_crc32c_le(blk_crc,
424 (unsigned char *)data_ptr,
426 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
427 key->size = ext2fs_cpu_to_le32(keysz + data_size);
430 key = data->keyb->keys + data->keys_in_block;
431 data->keys_in_block++;
432 key->fsblk = ext2fs_cpu_to_le64(backing_blk_num);
433 blk_crc = ext2fs_crc32c_le(~0,
434 (unsigned char *)data_ptr,
436 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
437 key->size = ext2fs_cpu_to_le32(data_size);
439 dbg_printf("Writing block %llu to offset %llu size %d key %zu\n",
442 sz, data->num_keys - 1);
443 retval = io_channel_write_blk64(data->undo_file,
444 data->undo_blk_num, sz, data_ptr);
449 data->undo_blk_num++;
452 /* Write out the key block */
453 retval = write_undo_indexes(data, 0);
464 static errcode_t undo_io_read_error(io_channel channel ATTR((unused)),
465 unsigned long block ATTR((unused)),
466 int count ATTR((unused)),
467 void *data ATTR((unused)),
468 size_t size ATTR((unused)),
470 errcode_t error ATTR((unused)))
472 actual_size = actual;
476 static void undo_err_handler_init(io_channel channel)
478 channel->read_error = undo_io_read_error;
481 static int check_filesystem(struct undo_header *hdr, io_channel undo_file,
482 unsigned int blocksize, blk64_t super_block,
485 struct ext2_super_block super, *sb;
490 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
491 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
496 * Compare the FS and the undo file superblock so that we don't
497 * append to something that doesn't match this FS.
499 retval = ext2fs_get_mem(blocksize, &buf);
502 retval = io_channel_read_blk64(undo_file, super_block,
503 -SUPERBLOCK_SIZE, buf);
506 sb = (struct ext2_super_block *)buf;
507 sb->s_magic = ~sb->s_magic;
508 if (memcmp(&super, buf, sizeof(super))) {
512 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, SUPERBLOCK_SIZE);
513 if (ext2fs_le32_to_cpu(hdr->sb_crc) != sb_crc) {
519 ext2fs_free_mem(&buf);
524 * Try to re-open the undo file, so that we can resume where we left off.
525 * That way, the user can pass the same undo file to various programs as
526 * part of an FS upgrade instead of having to create multiple files and
527 * then apply them in correct order.
529 static errcode_t try_reopen_undo_file(int undo_fd,
530 struct undo_private_data *data)
532 struct undo_header hdr;
533 struct undo_key *dkey;
534 ext2fs_struct_stat statbuf;
535 unsigned int blocksize, fs_blocksize;
536 blk64_t super_block, lblk;
537 size_t num_keys, keys_per_block, i;
538 __u32 hdr_crc, key_crc;
541 /* Zero size already? */
542 retval = ext2fs_fstat(undo_fd, &statbuf);
545 if (statbuf.st_size == 0)
548 /* check the file header */
549 retval = io_channel_read_blk64(data->undo_file, 0, -(int)sizeof(hdr),
554 if (memcmp(hdr.magic, E2UNDO_MAGIC,
557 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&hdr,
558 sizeof(struct undo_header) -
560 if (ext2fs_le32_to_cpu(hdr.header_crc) != hdr_crc)
562 blocksize = ext2fs_le32_to_cpu(hdr.block_size);
563 fs_blocksize = ext2fs_le32_to_cpu(hdr.fs_block_size);
564 if (blocksize > E2UNDO_MAX_BLOCK_SIZE ||
565 blocksize < E2UNDO_MIN_BLOCK_SIZE ||
566 !blocksize || !fs_blocksize)
568 super_block = ext2fs_le64_to_cpu(hdr.super_offset);
569 num_keys = ext2fs_le64_to_cpu(hdr.num_keys);
570 io_channel_set_blksize(data->undo_file, blocksize);
571 if (hdr.f_compat || hdr.f_incompat || hdr.f_rocompat)
574 /* Superblock matches this FS? */
575 if (check_filesystem(&hdr, data->undo_file, blocksize, super_block,
577 retval = EXT2_ET_UNDO_FILE_WRONG;
581 /* Try to set ourselves up */
582 data->tdb_data_size = blocksize;
583 retval = undo_setup_tdb(data);
586 data->num_keys = num_keys;
587 data->super_blk_num = super_block;
588 data->first_key_blk = ext2fs_le64_to_cpu(hdr.key_offset);
590 /* load the written block map */
591 keys_per_block = KEYS_PER_BLOCK(data);
592 lblk = data->first_key_blk;
593 dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n",
594 num_keys, keys_per_block, blocksize);
595 for (i = 0; i < num_keys; i += keys_per_block) {
599 data->key_blk_num = lblk;
600 retval = io_channel_read_blk64(data->undo_file,
601 lblk, 1, data->keyb);
606 if (ext2fs_le32_to_cpu(data->keyb->magic) != KEYBLOCK_MAGIC) {
607 retval = EXT2_ET_UNDO_FILE_CORRUPT;
610 crc = data->keyb->crc;
612 key_crc = ext2fs_crc32c_le(~0, (unsigned char *)data->keyb,
614 if (ext2fs_le32_to_cpu(crc) != key_crc) {
615 retval = EXT2_ET_UNDO_FILE_CORRUPT;
619 /* load keys from key block */
621 max_j = data->num_keys - i;
622 if (max_j > keys_per_block)
623 max_j = keys_per_block;
624 for (j = 0, dkey = data->keyb->keys;
627 blk64_t fsblk = ext2fs_le64_to_cpu(dkey->fsblk);
628 blk64_t undo_blk = fsblk * fs_blocksize / blocksize;
629 size_t size = ext2fs_le32_to_cpu(dkey->size);
631 ext2fs_mark_block_bitmap_range2(data->written_block_map,
633 (size + blocksize - 1) / blocksize);
634 lblk += (size + blocksize - 1) / blocksize;
635 data->undo_blk_num = lblk;
636 data->keys_in_block = j + 1;
639 dbg_printf("Reopen undo, keyblk=%llu undoblk=%llu nrkeys=%zu kib=%zu\n",
640 data->key_blk_num, data->undo_blk_num, data->num_keys,
641 data->keys_in_block);
643 data->hdr.state = hdr.state & ~E2UNDO_STATE_FINISHED;
644 data->hdr.f_compat = hdr.f_compat;
645 data->hdr.f_incompat = hdr.f_incompat;
646 data->hdr.f_rocompat = hdr.f_rocompat;
650 data->key_blk_num = data->undo_blk_num = 0;
651 data->keys_in_block = 0;
652 ext2fs_free_mem(&data->keyb);
653 ext2fs_free_generic_bitmap(data->written_block_map);
654 data->tdb_written = 0;
657 retval = EXT2_ET_UNDO_FILE_CORRUPT;
662 static void undo_atexit(void *p)
664 struct undo_private_data *data = p;
667 err = write_undo_indexes(data, 1);
668 io_channel_close(data->undo_file);
670 com_err(data->tdb_file, err, "while force-closing undo file");
673 static errcode_t undo_open(const char *name, int flags, io_channel *channel)
675 io_channel io = NULL;
676 struct undo_private_data *data = NULL;
681 return EXT2_ET_BAD_DEVICE_NAME;
682 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
685 memset(io, 0, sizeof(struct struct_io_channel));
686 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
687 retval = ext2fs_get_mem(sizeof(struct undo_private_data), &data);
691 io->manager = undo_io_manager;
692 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
696 strcpy(io->name, name);
697 io->private_data = data;
698 io->block_size = 1024;
703 memset(data, 0, sizeof(struct undo_private_data));
704 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
705 data->super_blk_num = 1;
706 data->first_key_blk = 2;
707 data->undo_blk_num = 3;
709 if (undo_io_backing_manager) {
710 retval = undo_io_backing_manager->open(name, flags,
715 data->tdb_file = strdup(tdb_file);
716 if (data->tdb_file == NULL)
718 undo_fd = ext2fs_open_file(data->tdb_file, O_RDWR | O_CREAT,
723 retval = undo_io_backing_manager->open(data->tdb_file,
730 data->undo_file = NULL;
734 io->flags = (io->flags & ~CHANNEL_FLAGS_DISCARD_ZEROES) |
735 (data->real->flags & CHANNEL_FLAGS_DISCARD_ZEROES);
738 * setup err handler for read so that we know
739 * when the backing manager fails do short read
742 undo_err_handler_init(data->real);
744 if (data->undo_file) {
745 retval = try_reopen_undo_file(undo_fd, data);
749 retval = ext2fs_add_exit_fn(undo_atexit, data);
759 ext2fs_remove_exit_fn(undo_atexit, data);
762 if (data && data->undo_file)
763 io_channel_close(data->undo_file);
764 if (data && data->tdb_file)
765 free(data->tdb_file);
766 if (data && data->real)
767 io_channel_close(data->real);
769 ext2fs_free_mem(&data);
771 ext2fs_free_mem(&io);
775 static errcode_t undo_close(io_channel channel)
777 struct undo_private_data *data;
778 errcode_t err, retval = 0;
780 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
781 data = (struct undo_private_data *) channel->private_data;
782 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
784 if (--channel->refcount > 0)
786 /* Before closing write the file system identity */
787 if (!getenv("UNDO_IO_SIMULATE_UNFINISHED"))
788 data->hdr.state = ext2fs_cpu_to_le32(E2UNDO_STATE_FINISHED);
789 err = write_undo_indexes(data, 1);
790 ext2fs_remove_exit_fn(undo_atexit, data);
792 retval = io_channel_close(data->real);
794 free(data->tdb_file);
796 io_channel_close(data->undo_file);
797 ext2fs_free_mem(&data->keyb);
798 if (data->written_block_map)
799 ext2fs_free_generic_bitmap(data->written_block_map);
800 ext2fs_free_mem(&channel->private_data);
802 ext2fs_free_mem(&channel->name);
803 ext2fs_free_mem(&channel);
810 static errcode_t undo_set_blksize(io_channel channel, int blksize)
812 struct undo_private_data *data;
813 errcode_t retval = 0;
815 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
816 data = (struct undo_private_data *) channel->private_data;
817 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
819 if (blksize > E2UNDO_MAX_BLOCK_SIZE || blksize < E2UNDO_MIN_BLOCK_SIZE)
820 return EXT2_ET_INVALID_ARGUMENT;
823 retval = io_channel_set_blksize(data->real, blksize);
825 * Set the block size used for tdb
827 if (!data->tdb_data_size || !data->tdb_written)
828 data->tdb_data_size = blksize;
829 channel->block_size = blksize;
833 static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
834 int count, void *buf)
836 errcode_t retval = 0;
837 struct undo_private_data *data;
839 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
840 data = (struct undo_private_data *) channel->private_data;
841 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
844 retval = io_channel_read_blk64(data->real, block, count, buf);
849 static errcode_t undo_read_blk(io_channel channel, unsigned long block,
850 int count, void *buf)
852 return undo_read_blk64(channel, block, count, buf);
855 static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
856 int count, const void *buf)
858 struct undo_private_data *data;
859 errcode_t retval = 0;
861 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
862 data = (struct undo_private_data *) channel->private_data;
863 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
865 * First write the existing content into database
867 retval = undo_write_tdb(channel, block, count);
871 retval = io_channel_write_blk64(data->real, block, count, buf);
876 static errcode_t undo_write_blk(io_channel channel, unsigned long block,
877 int count, const void *buf)
879 return undo_write_blk64(channel, block, count, buf);
882 static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
883 int size, const void *buf)
885 struct undo_private_data *data;
886 errcode_t retval = 0;
887 ext2_loff_t location;
888 unsigned long blk_num, count;;
890 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
891 data = (struct undo_private_data *) channel->private_data;
892 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
894 location = offset + data->offset;
895 blk_num = location/channel->block_size;
897 * the size specified may spread across multiple blocks
898 * also make sure we account for the fact that block start
899 * offset for tdb is different from the backing I/O manager
900 * due to possible different block size
902 count = (size + (location % channel->block_size) +
903 channel->block_size -1)/channel->block_size;
904 retval = undo_write_tdb(channel, blk_num, count);
907 if (data->real && data->real->manager->write_byte)
908 retval = io_channel_write_byte(data->real, offset, size, buf);
913 static errcode_t undo_discard(io_channel channel, unsigned long long block,
914 unsigned long long count)
916 struct undo_private_data *data;
917 errcode_t retval = 0;
920 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
921 data = (struct undo_private_data *) channel->private_data;
922 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
925 return EXT2_ET_UNIMPLEMENTED;
929 * First write the existing content into database
931 retval = undo_write_tdb(channel, block, icount);
935 retval = io_channel_discard(data->real, block, count);
940 static errcode_t undo_zeroout(io_channel channel, unsigned long long block,
941 unsigned long long count)
943 struct undo_private_data *data;
944 errcode_t retval = 0;
947 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
948 data = (struct undo_private_data *) channel->private_data;
949 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
952 return EXT2_ET_UNIMPLEMENTED;
956 * First write the existing content into database
958 retval = undo_write_tdb(channel, block, icount);
962 retval = io_channel_zeroout(data->real, block, count);
967 static errcode_t undo_cache_readahead(io_channel channel,
968 unsigned long long block,
969 unsigned long long count)
971 struct undo_private_data *data;
972 errcode_t retval = 0;
974 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
975 data = (struct undo_private_data *) channel->private_data;
976 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
979 retval = io_channel_cache_readahead(data->real, block, count);
985 * Flush data buffers to disk.
987 static errcode_t undo_flush(io_channel channel)
989 errcode_t retval = 0;
990 struct undo_private_data *data;
992 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
993 data = (struct undo_private_data *) channel->private_data;
994 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
997 retval = io_channel_flush(data->real);
1002 static errcode_t undo_set_option(io_channel channel, const char *option,
1005 errcode_t retval = 0;
1006 struct undo_private_data *data;
1010 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1011 data = (struct undo_private_data *) channel->private_data;
1012 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1014 if (!strcmp(option, "tdb_data_size")) {
1016 return EXT2_ET_INVALID_ARGUMENT;
1018 tmp = strtoul(arg, &end, 0);
1020 return EXT2_ET_INVALID_ARGUMENT;
1021 if (tmp > E2UNDO_MAX_BLOCK_SIZE || tmp < E2UNDO_MIN_BLOCK_SIZE)
1022 return EXT2_ET_INVALID_ARGUMENT;
1023 if (!data->tdb_data_size || !data->tdb_written) {
1024 data->tdb_written = -1;
1025 data->tdb_data_size = tmp;
1030 * Need to support offset option to work with
1033 if (data->real && data->real->manager->set_option) {
1034 retval = data->real->manager->set_option(data->real,
1037 if (!retval && !strcmp(option, "offset")) {
1039 return EXT2_ET_INVALID_ARGUMENT;
1041 tmp = strtoul(arg, &end, 0);
1043 return EXT2_ET_INVALID_ARGUMENT;
1049 static errcode_t undo_get_stats(io_channel channel, io_stats *stats)
1051 errcode_t retval = 0;
1052 struct undo_private_data *data;
1054 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1055 data = (struct undo_private_data *) channel->private_data;
1056 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1059 retval = (data->real->manager->get_stats)(data->real, stats);
1064 static struct struct_io_manager struct_undo_manager = {
1065 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1066 .name = "Undo I/O Manager",
1068 .close = undo_close,
1069 .set_blksize = undo_set_blksize,
1070 .read_blk = undo_read_blk,
1071 .write_blk = undo_write_blk,
1072 .flush = undo_flush,
1073 .write_byte = undo_write_byte,
1074 .set_option = undo_set_option,
1075 .get_stats = undo_get_stats,
1076 .read_blk64 = undo_read_blk64,
1077 .write_blk64 = undo_write_blk64,
1078 .discard = undo_discard,
1079 .zeroout = undo_zeroout,
1080 .cache_readahead = undo_cache_readahead,
1083 io_manager undo_io_manager = &struct_undo_manager;