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 #ifndef _LARGEFILE_SOURCE
15 #define _LARGEFILE_SOURCE
17 #ifndef _LARGEFILE64_SOURCE
18 #define _LARGEFILE64_SOURCE
33 #include <sys/utsname.h>
39 #include <sys/types.h>
41 #if HAVE_SYS_RESOURCE_H
42 #include <sys/resource.h>
51 #define ATTR(x) __attribute__(x)
59 # define dbg_printf(f, a...) do {printf(f, ## a); fflush(stdout); } while (0)
61 # define dbg_printf(f, a...)
65 * For checking structure magic numbers...
68 #define EXT2_CHECK_MAGIC(struct, code) \
69 if ((struct)->magic != (code)) return (code)
71 * Undo file format: The file is cut up into undo_header.block_size blocks.
72 * The first block contains the header.
73 * The second block contains the superblock.
74 * There is then a repeating series of blocks as follows:
75 * A key block, which contains undo_keys to map the following data blocks.
77 * (Note that there are pointers to the first key block and the sb, so this
78 * order isn't strictly necessary.)
80 #define E2UNDO_MAGIC "E2UNDO02"
81 #define KEYBLOCK_MAGIC 0xCADECADE
83 #define E2UNDO_STATE_FINISHED 0x1 /* undo file is complete */
85 #define E2UNDO_MIN_BLOCK_SIZE 1024 /* undo blocks are no less than 1KB */
86 #define E2UNDO_MAX_BLOCK_SIZE 1048576 /* undo blocks are no more than 1MB */
89 char magic[8]; /* "E2UNDO02" */
90 __le64 num_keys; /* how many keys? */
91 __le64 super_offset; /* where in the file is the superblock copy? */
92 __le64 key_offset; /* where do the key/data block chunks start? */
93 __le32 block_size; /* block size of the undo file */
94 __le32 fs_block_size; /* block size of the target device */
95 __le32 sb_crc; /* crc32c of the superblock */
96 __le32 state; /* e2undo state flags */
97 __le32 f_compat; /* compatible features */
98 __le32 f_incompat; /* incompatible features (none so far) */
99 __le32 f_rocompat; /* ro compatible features (none so far) */
100 __le32 pad32; /* padding for fs_offset */
101 __le64 fs_offset; /* filesystem offset */
102 __u8 padding[436]; /* padding */
103 __le32 header_crc; /* crc32c of this header (but not this field) */
106 #define E2UNDO_MAX_EXTENT_BLOCKS 512 /* max extent size, in blocks */
109 __le64 fsblk; /* where in the fs does the block go */
110 __le32 blk_crc; /* crc32c of the block */
111 __le32 size; /* how many bytes in this block? */
114 struct undo_key_block {
115 __le32 magic; /* KEYBLOCK_MAGIC number */
116 __le32 crc; /* block checksum */
117 __le64 reserved; /* zero */
119 #if __STDC_VERSION__ >= 199901L
120 struct undo_key keys[]; /* keys, which come immediately after */
122 struct undo_key keys[0]; /* keys, which come immediately after */
126 struct undo_private_data {
129 /* the undo file io channel */
130 io_channel undo_file;
131 blk64_t undo_blk_num; /* next free block */
132 blk64_t key_blk_num; /* current key block location */
133 blk64_t super_blk_num; /* superblock location */
134 blk64_t first_key_blk; /* first key block location */
135 struct undo_key_block *keyb;
136 size_t num_keys, keys_in_block;
138 /* The backing io channel */
141 unsigned long long tdb_data_size;
144 /* to support offset in unix I/O manager */
147 ext2fs_block_bitmap written_block_map;
148 struct struct_ext2_filsys fake_fs;
150 struct undo_header hdr;
152 #define KEYS_PER_BLOCK(d) (((d)->tdb_data_size / sizeof(struct undo_key)) - 1)
154 #define E2UNDO_FEATURE_COMPAT_FS_OFFSET 0x1 /* the filesystem offset */
156 static inline void e2undo_set_feature_fs_offset(struct undo_header *header) {
157 header->f_compat |= ext2fs_le32_to_cpu(E2UNDO_FEATURE_COMPAT_FS_OFFSET);
160 static inline void e2undo_clear_feature_fs_offset(struct undo_header *header) {
161 header->f_compat &= ~ext2fs_le32_to_cpu(E2UNDO_FEATURE_COMPAT_FS_OFFSET);
164 static io_manager undo_io_backing_manager;
165 static char *tdb_file;
166 static int actual_size;
168 errcode_t set_undo_io_backing_manager(io_manager manager)
171 * We may want to do some validation later
173 undo_io_backing_manager = manager;
177 errcode_t set_undo_io_backup_file(char *file_name)
179 tdb_file = strdup(file_name);
181 if (tdb_file == NULL) {
182 return EXT2_ET_NO_MEMORY;
188 static errcode_t write_undo_indexes(struct undo_private_data *data, int flush)
191 struct ext2_super_block super;
194 __u32 sb_crc, hdr_crc;
196 /* Spit out a key block, if there's any data */
197 if (data->keys_in_block) {
198 data->keyb->magic = ext2fs_cpu_to_le32(KEYBLOCK_MAGIC);
200 data->keyb->crc = ext2fs_cpu_to_le32(
202 (unsigned char *)data->keyb,
203 data->tdb_data_size));
204 dbg_printf("Writing keyblock to blk %llu\n", data->key_blk_num);
205 retval = io_channel_write_blk64(data->undo_file,
210 /* Move on to the next key block if it's full. */
211 if (data->keys_in_block == KEYS_PER_BLOCK(data)) {
212 memset(data->keyb, 0, data->tdb_data_size);
213 data->keys_in_block = 0;
214 data->key_blk_num = data->undo_blk_num;
215 data->undo_blk_num++;
219 /* Prepare superblock for write */
220 channel = data->real;
221 block_size = channel->block_size;
223 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
224 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
227 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)&super, SUPERBLOCK_SIZE);
228 super.s_magic = ~super.s_magic;
230 /* Write the undo header to disk. */
231 memcpy(data->hdr.magic, E2UNDO_MAGIC, sizeof(data->hdr.magic));
232 data->hdr.num_keys = ext2fs_cpu_to_le64(data->num_keys);
233 data->hdr.super_offset = ext2fs_cpu_to_le64(data->super_blk_num);
234 data->hdr.key_offset = ext2fs_cpu_to_le64(data->first_key_blk);
235 data->hdr.fs_block_size = ext2fs_cpu_to_le32(block_size);
236 data->hdr.sb_crc = ext2fs_cpu_to_le32(sb_crc);
237 data->hdr.fs_offset = ext2fs_cpu_to_le64(data->offset);
239 e2undo_set_feature_fs_offset(&data->hdr);
241 e2undo_clear_feature_fs_offset(&data->hdr);
242 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&data->hdr,
244 sizeof(data->hdr.header_crc));
245 data->hdr.header_crc = ext2fs_cpu_to_le32(hdr_crc);
246 retval = io_channel_write_blk64(data->undo_file, 0,
247 -(int)sizeof(data->hdr),
253 * Record the entire superblock (in FS byte order) so that we can't
254 * apply e2undo files to the wrong FS or out of order.
256 dbg_printf("Writing superblock to block %llu\n", data->super_blk_num);
257 retval = io_channel_write_blk64(data->undo_file, data->super_blk_num,
258 -SUPERBLOCK_SIZE, &super);
263 retval = io_channel_flush(data->undo_file);
265 io_channel_set_blksize(channel, block_size);
269 static errcode_t undo_setup_tdb(struct undo_private_data *data)
274 if (data->tdb_written == 1)
277 data->tdb_written = 1;
279 /* Make a bitmap to track what we've written */
280 memset(&data->fake_fs, 0, sizeof(data->fake_fs));
281 data->fake_fs.blocksize = data->tdb_data_size;
282 retval = ext2fs_alloc_generic_bmap(&data->fake_fs,
283 EXT2_ET_MAGIC_BLOCK_BITMAP64,
284 EXT2FS_BMAP64_RBTREE,
286 "undo block map", &data->written_block_map);
290 /* Allocate key block */
291 retval = ext2fs_get_mem(data->tdb_data_size, &data->keyb);
294 data->key_blk_num = data->first_key_blk;
296 /* Record block size */
297 dbg_printf("Undo block size %llu\n", data->tdb_data_size);
298 dbg_printf("Keys per block %llu\n", KEYS_PER_BLOCK(data));
299 data->hdr.block_size = ext2fs_cpu_to_le32(data->tdb_data_size);
300 io_channel_set_blksize(data->undo_file, data->tdb_data_size);
302 /* Ensure that we have space for header blocks */
303 for (i = 0; i <= 2; i++) {
304 retval = io_channel_read_blk64(data->undo_file, i, 1,
307 memset(data->keyb, 0, data->tdb_data_size);
308 retval = io_channel_write_blk64(data->undo_file, i, 1,
312 retval = io_channel_flush(data->undo_file);
316 memset(data->keyb, 0, data->tdb_data_size);
320 static errcode_t undo_write_tdb(io_channel channel,
321 unsigned long long block, int count)
325 unsigned long long block_num, backing_blk_num;
326 errcode_t retval = 0;
328 struct undo_private_data *data;
329 unsigned char *read_ptr;
330 unsigned long long end_block;
331 unsigned long long data_size;
332 struct undo_key *key;
335 data = (struct undo_private_data *) channel->private_data;
337 if (data->undo_file == NULL) {
339 * Transaction database not initialized
345 size = channel->block_size;
350 size = count * channel->block_size;
353 retval = undo_setup_tdb(data);
357 * Data is stored in tdb database as blocks of tdb_data_size size
358 * This helps in efficient lookup further.
360 * We divide the disk to blocks of tdb_data_size.
362 offset = (block * channel->block_size) + data->offset ;
363 block_num = offset / data->tdb_data_size;
364 end_block = (offset + size - 1) / data->tdb_data_size;
366 while (block_num <= end_block) {
370 * Check if we have the record already
372 if (ext2fs_test_block_bitmap2(data->written_block_map,
374 /* Try the next block */
378 ext2fs_mark_block_bitmap2(data->written_block_map, block_num);
381 * Read one block using the backing I/O manager
382 * The backing I/O manager block size may be
383 * different from the tdb_data_size.
384 * Also we need to recalculate the block number with respect
385 * to the backing I/O manager.
387 offset = block_num * data->tdb_data_size +
388 (data->offset % data->tdb_data_size);
389 backing_blk_num = (offset - data->offset) / channel->block_size;
391 retval = ext2fs_get_mem(data->tdb_data_size, &read_ptr);
396 memset(read_ptr, 0, data->tdb_data_size);
398 if ((data->tdb_data_size % channel->block_size) == 0)
399 sz = data->tdb_data_size / channel->block_size;
401 sz = -data->tdb_data_size;
402 retval = io_channel_read_blk64(data->real, backing_blk_num,
405 if (retval != EXT2_ET_SHORT_READ) {
410 * short read so update the record size
413 data_size = actual_size;
415 data_size = data->tdb_data_size;
417 if (data_size == 0) {
422 dbg_printf("Read %llu bytes from FS block %llu (blk=%llu cnt=%llu)\n",
423 data_size, backing_blk_num, block, data->tdb_data_size);
424 if ((data_size % data->undo_file->block_size) == 0)
425 sz = data_size / data->undo_file->block_size;
428 /* extend this key? */
429 if (data->keys_in_block) {
430 key = data->keyb->keys + data->keys_in_block - 1;
431 keysz = ext2fs_le32_to_cpu(key->size);
437 (ext2fs_le64_to_cpu(key->fsblk) * channel->block_size +
438 channel->block_size - 1 +
439 keysz) / channel->block_size == backing_blk_num &&
440 E2UNDO_MAX_EXTENT_BLOCKS * data->tdb_data_size >
442 blk_crc = ext2fs_le32_to_cpu(key->blk_crc);
443 blk_crc = ext2fs_crc32c_le(blk_crc, read_ptr, data_size);
444 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
445 key->size = ext2fs_cpu_to_le32(keysz + data_size);
448 key = data->keyb->keys + data->keys_in_block;
449 data->keys_in_block++;
450 key->fsblk = ext2fs_cpu_to_le64(backing_blk_num);
451 blk_crc = ext2fs_crc32c_le(~0, read_ptr, data_size);
452 key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
453 key->size = ext2fs_cpu_to_le32(data_size);
455 dbg_printf("Writing block %llu to offset %llu size %d key %zu\n",
458 sz, data->num_keys - 1);
459 retval = io_channel_write_blk64(data->undo_file,
460 data->undo_blk_num, sz, read_ptr);
465 data->undo_blk_num++;
468 /* Write out the key block */
469 retval = write_undo_indexes(data, 0);
480 static errcode_t undo_io_read_error(io_channel channel ATTR((unused)),
481 unsigned long block ATTR((unused)),
482 int count ATTR((unused)),
483 void *data ATTR((unused)),
484 size_t size ATTR((unused)),
486 errcode_t error ATTR((unused)))
488 actual_size = actual;
492 static void undo_err_handler_init(io_channel channel)
494 channel->read_error = undo_io_read_error;
497 static int check_filesystem(struct undo_header *hdr, io_channel undo_file,
498 unsigned int blocksize, blk64_t super_block,
501 struct ext2_super_block super, *sb;
506 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
507 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
512 * Compare the FS and the undo file superblock so that we don't
513 * append to something that doesn't match this FS.
515 retval = ext2fs_get_mem(blocksize, &buf);
518 retval = io_channel_read_blk64(undo_file, super_block,
519 -SUPERBLOCK_SIZE, buf);
522 sb = (struct ext2_super_block *)buf;
523 sb->s_magic = ~sb->s_magic;
524 if (memcmp(&super, buf, sizeof(super))) {
528 sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, SUPERBLOCK_SIZE);
529 if (ext2fs_le32_to_cpu(hdr->sb_crc) != sb_crc) {
535 ext2fs_free_mem(&buf);
540 * Try to re-open the undo file, so that we can resume where we left off.
541 * That way, the user can pass the same undo file to various programs as
542 * part of an FS upgrade instead of having to create multiple files and
543 * then apply them in correct order.
545 static errcode_t try_reopen_undo_file(int undo_fd,
546 struct undo_private_data *data)
548 struct undo_header hdr;
549 struct undo_key *dkey;
550 ext2fs_struct_stat statbuf;
551 unsigned int blocksize, fs_blocksize;
552 blk64_t super_block, lblk;
553 size_t num_keys, keys_per_block, i;
554 __u32 hdr_crc, key_crc;
557 /* Zero size already? */
558 retval = ext2fs_fstat(undo_fd, &statbuf);
561 if (statbuf.st_size == 0)
564 /* check the file header */
565 retval = io_channel_read_blk64(data->undo_file, 0, -(int)sizeof(hdr),
570 if (memcmp(hdr.magic, E2UNDO_MAGIC,
573 hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&hdr,
574 sizeof(struct undo_header) -
576 if (ext2fs_le32_to_cpu(hdr.header_crc) != hdr_crc)
578 blocksize = ext2fs_le32_to_cpu(hdr.block_size);
579 fs_blocksize = ext2fs_le32_to_cpu(hdr.fs_block_size);
580 if (blocksize > E2UNDO_MAX_BLOCK_SIZE ||
581 blocksize < E2UNDO_MIN_BLOCK_SIZE ||
582 !blocksize || !fs_blocksize)
584 super_block = ext2fs_le64_to_cpu(hdr.super_offset);
585 num_keys = ext2fs_le64_to_cpu(hdr.num_keys);
586 io_channel_set_blksize(data->undo_file, blocksize);
588 * Do not compare hdr.f_compat with the available compatible
589 * features set, because a "missing" compatible feature should
590 * not cause any problems.
592 if (hdr.f_incompat || hdr.f_rocompat)
595 /* Superblock matches this FS? */
596 if (check_filesystem(&hdr, data->undo_file, blocksize, super_block,
598 retval = EXT2_ET_UNDO_FILE_WRONG;
602 /* Try to set ourselves up */
603 data->tdb_data_size = blocksize;
604 retval = undo_setup_tdb(data);
607 data->num_keys = num_keys;
608 data->super_blk_num = super_block;
609 data->first_key_blk = ext2fs_le64_to_cpu(hdr.key_offset);
611 /* load the written block map */
612 keys_per_block = KEYS_PER_BLOCK(data);
613 lblk = data->first_key_blk;
614 dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n",
615 num_keys, keys_per_block, blocksize);
616 for (i = 0; i < num_keys; i += keys_per_block) {
620 data->key_blk_num = lblk;
621 retval = io_channel_read_blk64(data->undo_file,
622 lblk, 1, data->keyb);
627 if (ext2fs_le32_to_cpu(data->keyb->magic) != KEYBLOCK_MAGIC) {
628 retval = EXT2_ET_UNDO_FILE_CORRUPT;
631 crc = data->keyb->crc;
633 key_crc = ext2fs_crc32c_le(~0, (unsigned char *)data->keyb,
635 if (ext2fs_le32_to_cpu(crc) != key_crc) {
636 retval = EXT2_ET_UNDO_FILE_CORRUPT;
640 /* load keys from key block */
642 max_j = data->num_keys - i;
643 if (max_j > keys_per_block)
644 max_j = keys_per_block;
645 for (j = 0, dkey = data->keyb->keys;
648 blk64_t fsblk = ext2fs_le64_to_cpu(dkey->fsblk);
649 blk64_t undo_blk = fsblk * fs_blocksize / blocksize;
650 size_t size = ext2fs_le32_to_cpu(dkey->size);
652 ext2fs_mark_block_bitmap_range2(data->written_block_map,
654 (size + blocksize - 1) / blocksize);
655 lblk += (size + blocksize - 1) / blocksize;
656 data->undo_blk_num = lblk;
657 data->keys_in_block = j + 1;
660 dbg_printf("Reopen undo, keyblk=%llu undoblk=%llu nrkeys=%zu kib=%zu\n",
661 data->key_blk_num, data->undo_blk_num, data->num_keys,
662 data->keys_in_block);
664 data->hdr.state = hdr.state & ~E2UNDO_STATE_FINISHED;
665 data->hdr.f_compat = hdr.f_compat;
666 data->hdr.f_incompat = hdr.f_incompat;
667 data->hdr.f_rocompat = hdr.f_rocompat;
671 data->key_blk_num = data->undo_blk_num = 0;
672 data->keys_in_block = 0;
673 ext2fs_free_mem(&data->keyb);
674 ext2fs_free_generic_bitmap(data->written_block_map);
675 data->tdb_written = 0;
678 retval = EXT2_ET_UNDO_FILE_CORRUPT;
683 static void undo_atexit(void *p)
685 struct undo_private_data *data = p;
688 err = write_undo_indexes(data, 1);
689 io_channel_close(data->undo_file);
691 com_err(data->tdb_file, err, "while force-closing undo file");
694 static errcode_t undo_open(const char *name, int flags, io_channel *channel)
696 io_channel io = NULL;
697 struct undo_private_data *data = NULL;
702 return EXT2_ET_BAD_DEVICE_NAME;
703 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
706 memset(io, 0, sizeof(struct struct_io_channel));
707 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
708 retval = ext2fs_get_mem(sizeof(struct undo_private_data), &data);
712 io->manager = undo_io_manager;
713 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
717 strcpy(io->name, name);
718 io->private_data = data;
719 io->block_size = 1024;
724 memset(data, 0, sizeof(struct undo_private_data));
725 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
726 data->super_blk_num = 1;
727 data->first_key_blk = 2;
728 data->undo_blk_num = 3;
730 if (undo_io_backing_manager) {
731 retval = undo_io_backing_manager->open(name, flags,
736 data->tdb_file = strdup(tdb_file);
737 if (data->tdb_file == NULL)
739 undo_fd = ext2fs_open_file(data->tdb_file, O_RDWR | O_CREAT,
744 retval = undo_io_backing_manager->open(data->tdb_file,
751 data->undo_file = NULL;
755 io->flags = (io->flags & ~CHANNEL_FLAGS_DISCARD_ZEROES) |
756 (data->real->flags & CHANNEL_FLAGS_DISCARD_ZEROES);
759 * setup err handler for read so that we know
760 * when the backing manager fails do short read
763 undo_err_handler_init(data->real);
765 if (data->undo_file) {
766 retval = try_reopen_undo_file(undo_fd, data);
770 retval = ext2fs_add_exit_fn(undo_atexit, data);
780 ext2fs_remove_exit_fn(undo_atexit, data);
783 if (data && data->undo_file)
784 io_channel_close(data->undo_file);
785 if (data && data->tdb_file)
786 free(data->tdb_file);
787 if (data && data->real)
788 io_channel_close(data->real);
790 ext2fs_free_mem(&data);
792 ext2fs_free_mem(&io);
796 static errcode_t undo_close(io_channel channel)
798 struct undo_private_data *data;
799 errcode_t err, retval = 0;
801 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
802 data = (struct undo_private_data *) channel->private_data;
803 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
805 if (--channel->refcount > 0)
807 /* Before closing write the file system identity */
808 if (!getenv("UNDO_IO_SIMULATE_UNFINISHED"))
809 data->hdr.state = ext2fs_cpu_to_le32(E2UNDO_STATE_FINISHED);
810 err = write_undo_indexes(data, 1);
811 ext2fs_remove_exit_fn(undo_atexit, data);
813 retval = io_channel_close(data->real);
815 free(data->tdb_file);
817 io_channel_close(data->undo_file);
818 ext2fs_free_mem(&data->keyb);
819 if (data->written_block_map)
820 ext2fs_free_generic_bitmap(data->written_block_map);
821 ext2fs_free_mem(&channel->private_data);
823 ext2fs_free_mem(&channel->name);
824 ext2fs_free_mem(&channel);
831 static errcode_t undo_set_blksize(io_channel channel, int blksize)
833 struct undo_private_data *data;
834 errcode_t retval = 0;
836 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
837 data = (struct undo_private_data *) channel->private_data;
838 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
840 if (blksize > E2UNDO_MAX_BLOCK_SIZE || blksize < E2UNDO_MIN_BLOCK_SIZE)
841 return EXT2_ET_INVALID_ARGUMENT;
844 retval = io_channel_set_blksize(data->real, blksize);
846 * Set the block size used for tdb
848 if (!data->tdb_data_size || !data->tdb_written)
849 data->tdb_data_size = blksize;
850 channel->block_size = blksize;
854 static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
855 int count, void *buf)
857 errcode_t retval = 0;
858 struct undo_private_data *data;
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);
865 retval = io_channel_read_blk64(data->real, block, count, buf);
870 static errcode_t undo_read_blk(io_channel channel, unsigned long block,
871 int count, void *buf)
873 return undo_read_blk64(channel, block, count, buf);
876 static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
877 int count, const void *buf)
879 struct undo_private_data *data;
880 errcode_t retval = 0;
882 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
883 data = (struct undo_private_data *) channel->private_data;
884 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
886 * First write the existing content into database
888 retval = undo_write_tdb(channel, block, count);
892 retval = io_channel_write_blk64(data->real, block, count, buf);
897 static errcode_t undo_write_blk(io_channel channel, unsigned long block,
898 int count, const void *buf)
900 return undo_write_blk64(channel, block, count, buf);
903 static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
904 int size, const void *buf)
906 struct undo_private_data *data;
907 errcode_t retval = 0;
908 ext2_loff_t location;
909 unsigned long blk_num, count;;
911 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
912 data = (struct undo_private_data *) channel->private_data;
913 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
915 location = offset + data->offset;
916 blk_num = location/channel->block_size;
918 * the size specified may spread across multiple blocks
919 * also make sure we account for the fact that block start
920 * offset for tdb is different from the backing I/O manager
921 * due to possible different block size
923 count = (size + (location % channel->block_size) +
924 channel->block_size -1)/channel->block_size;
925 retval = undo_write_tdb(channel, blk_num, count);
928 if (data->real && data->real->manager->write_byte)
929 retval = io_channel_write_byte(data->real, offset, size, buf);
934 static errcode_t undo_discard(io_channel channel, unsigned long long block,
935 unsigned long long count)
937 struct undo_private_data *data;
938 errcode_t retval = 0;
941 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
942 data = (struct undo_private_data *) channel->private_data;
943 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
946 return EXT2_ET_UNIMPLEMENTED;
950 * First write the existing content into database
952 retval = undo_write_tdb(channel, block, icount);
956 retval = io_channel_discard(data->real, block, count);
961 static errcode_t undo_zeroout(io_channel channel, unsigned long long block,
962 unsigned long long count)
964 struct undo_private_data *data;
965 errcode_t retval = 0;
968 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
969 data = (struct undo_private_data *) channel->private_data;
970 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
973 return EXT2_ET_UNIMPLEMENTED;
977 * First write the existing content into database
979 retval = undo_write_tdb(channel, block, icount);
983 retval = io_channel_zeroout(data->real, block, count);
988 static errcode_t undo_cache_readahead(io_channel channel,
989 unsigned long long block,
990 unsigned long long count)
992 struct undo_private_data *data;
993 errcode_t retval = 0;
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_cache_readahead(data->real, block, count);
1006 * Flush data buffers to disk.
1008 static errcode_t undo_flush(io_channel channel)
1010 errcode_t retval = 0;
1011 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);
1018 retval = io_channel_flush(data->real);
1023 static errcode_t undo_set_option(io_channel channel, const char *option,
1026 errcode_t retval = 0;
1027 struct undo_private_data *data;
1031 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1032 data = (struct undo_private_data *) channel->private_data;
1033 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1035 if (!strcmp(option, "tdb_data_size")) {
1037 return EXT2_ET_INVALID_ARGUMENT;
1039 tmp = strtoul(arg, &end, 0);
1041 return EXT2_ET_INVALID_ARGUMENT;
1042 if (tmp > E2UNDO_MAX_BLOCK_SIZE || tmp < E2UNDO_MIN_BLOCK_SIZE)
1043 return EXT2_ET_INVALID_ARGUMENT;
1044 if (!data->tdb_data_size || !data->tdb_written) {
1045 data->tdb_written = -1;
1046 data->tdb_data_size = tmp;
1051 * Need to support offset option to work with
1054 if (data->real && data->real->manager->set_option) {
1055 retval = data->real->manager->set_option(data->real,
1058 if (!retval && !strcmp(option, "offset")) {
1060 return EXT2_ET_INVALID_ARGUMENT;
1062 tmp = strtoul(arg, &end, 0);
1064 return EXT2_ET_INVALID_ARGUMENT;
1070 static errcode_t undo_get_stats(io_channel channel, io_stats *stats)
1072 errcode_t retval = 0;
1073 struct undo_private_data *data;
1075 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1076 data = (struct undo_private_data *) channel->private_data;
1077 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1080 retval = (data->real->manager->get_stats)(data->real, stats);
1085 static struct struct_io_manager struct_undo_manager = {
1086 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1087 .name = "Undo I/O Manager",
1089 .close = undo_close,
1090 .set_blksize = undo_set_blksize,
1091 .read_blk = undo_read_blk,
1092 .write_blk = undo_write_blk,
1093 .flush = undo_flush,
1094 .write_byte = undo_write_byte,
1095 .set_option = undo_set_option,
1096 .get_stats = undo_get_stats,
1097 .read_blk64 = undo_read_blk64,
1098 .write_blk64 = undo_write_blk64,
1099 .discard = undo_discard,
1100 .zeroout = undo_zeroout,
1101 .cache_readahead = undo_cache_readahead,
1104 io_manager undo_io_manager = &struct_undo_manager;