xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/undo_io.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * undo_io.c --- This is the undo io manager that copies the old data that
3*6a54128fSAndroid Build Coastguard Worker  * copies the old data being overwritten into a tdb database
4*6a54128fSAndroid Build Coastguard Worker  *
5*6a54128fSAndroid Build Coastguard Worker  * Copyright IBM Corporation, 2007
6*6a54128fSAndroid Build Coastguard Worker  * Author Aneesh Kumar K.V <[email protected]>
7*6a54128fSAndroid Build Coastguard Worker  *
8*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
9*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
10*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
11*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
12*6a54128fSAndroid Build Coastguard Worker  */
13*6a54128fSAndroid Build Coastguard Worker 
14*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE_SOURCE
15*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE_SOURCE
16*6a54128fSAndroid Build Coastguard Worker #endif
17*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE64_SOURCE
18*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE64_SOURCE
19*6a54128fSAndroid Build Coastguard Worker #endif
20*6a54128fSAndroid Build Coastguard Worker 
21*6a54128fSAndroid Build Coastguard Worker #include "config.h"
22*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
23*6a54128fSAndroid Build Coastguard Worker #include <string.h>
24*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
25*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
26*6a54128fSAndroid Build Coastguard Worker #endif
27*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
28*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
29*6a54128fSAndroid Build Coastguard Worker #endif
30*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
31*6a54128fSAndroid Build Coastguard Worker #include <time.h>
32*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
33*6a54128fSAndroid Build Coastguard Worker #include <sys/utsname.h>
34*6a54128fSAndroid Build Coastguard Worker #endif
35*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_STAT_H
36*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
37*6a54128fSAndroid Build Coastguard Worker #endif
38*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
39*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
40*6a54128fSAndroid Build Coastguard Worker #endif
41*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_RESOURCE_H
42*6a54128fSAndroid Build Coastguard Worker #include <sys/resource.h>
43*6a54128fSAndroid Build Coastguard Worker #endif
44*6a54128fSAndroid Build Coastguard Worker #include <limits.h>
45*6a54128fSAndroid Build Coastguard Worker 
46*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
47*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
48*6a54128fSAndroid Build Coastguard Worker #include "ext2fsP.h"
49*6a54128fSAndroid Build Coastguard Worker 
50*6a54128fSAndroid Build Coastguard Worker #ifdef __GNUC__
51*6a54128fSAndroid Build Coastguard Worker #define ATTR(x) __attribute__(x)
52*6a54128fSAndroid Build Coastguard Worker #else
53*6a54128fSAndroid Build Coastguard Worker #define ATTR(x)
54*6a54128fSAndroid Build Coastguard Worker #endif
55*6a54128fSAndroid Build Coastguard Worker 
56*6a54128fSAndroid Build Coastguard Worker #undef DEBUG
57*6a54128fSAndroid Build Coastguard Worker 
58*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
59*6a54128fSAndroid Build Coastguard Worker # define dbg_printf(f, a...)  do {printf(f, ## a); fflush(stdout); } while (0)
60*6a54128fSAndroid Build Coastguard Worker #else
61*6a54128fSAndroid Build Coastguard Worker # define dbg_printf(f, a...)
62*6a54128fSAndroid Build Coastguard Worker #endif
63*6a54128fSAndroid Build Coastguard Worker 
64*6a54128fSAndroid Build Coastguard Worker /*
65*6a54128fSAndroid Build Coastguard Worker  * For checking structure magic numbers...
66*6a54128fSAndroid Build Coastguard Worker  */
67*6a54128fSAndroid Build Coastguard Worker 
68*6a54128fSAndroid Build Coastguard Worker #define EXT2_CHECK_MAGIC(struct, code) \
69*6a54128fSAndroid Build Coastguard Worker 	  if ((struct)->magic != (code)) return (code)
70*6a54128fSAndroid Build Coastguard Worker /*
71*6a54128fSAndroid Build Coastguard Worker  * Undo file format: The file is cut up into undo_header.block_size blocks.
72*6a54128fSAndroid Build Coastguard Worker  * The first block contains the header.
73*6a54128fSAndroid Build Coastguard Worker  * The second block contains the superblock.
74*6a54128fSAndroid Build Coastguard Worker  * There is then a repeating series of blocks as follows:
75*6a54128fSAndroid Build Coastguard Worker  *   A key block, which contains undo_keys to map the following data blocks.
76*6a54128fSAndroid Build Coastguard Worker  *   Data blocks
77*6a54128fSAndroid Build Coastguard Worker  * (Note that there are pointers to the first key block and the sb, so this
78*6a54128fSAndroid Build Coastguard Worker  * order isn't strictly necessary.)
79*6a54128fSAndroid Build Coastguard Worker  */
80*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MAGIC "E2UNDO02"
81*6a54128fSAndroid Build Coastguard Worker #define KEYBLOCK_MAGIC 0xCADECADE
82*6a54128fSAndroid Build Coastguard Worker 
83*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_STATE_FINISHED	0x1	/* undo file is complete */
84*6a54128fSAndroid Build Coastguard Worker 
85*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MIN_BLOCK_SIZE	1024	/* undo blocks are no less than 1KB */
86*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MAX_BLOCK_SIZE	1048576	/* undo blocks are no more than 1MB */
87*6a54128fSAndroid Build Coastguard Worker 
88*6a54128fSAndroid Build Coastguard Worker struct undo_header {
89*6a54128fSAndroid Build Coastguard Worker 	char magic[8];		/* "E2UNDO02" */
90*6a54128fSAndroid Build Coastguard Worker 	__le64 num_keys;	/* how many keys? */
91*6a54128fSAndroid Build Coastguard Worker 	__le64 super_offset;	/* where in the file is the superblock copy? */
92*6a54128fSAndroid Build Coastguard Worker 	__le64 key_offset;	/* where do the key/data block chunks start? */
93*6a54128fSAndroid Build Coastguard Worker 	__le32 block_size;	/* block size of the undo file */
94*6a54128fSAndroid Build Coastguard Worker 	__le32 fs_block_size;	/* block size of the target device */
95*6a54128fSAndroid Build Coastguard Worker 	__le32 sb_crc;		/* crc32c of the superblock */
96*6a54128fSAndroid Build Coastguard Worker 	__le32 state;		/* e2undo state flags */
97*6a54128fSAndroid Build Coastguard Worker 	__le32 f_compat;	/* compatible features */
98*6a54128fSAndroid Build Coastguard Worker 	__le32 f_incompat;	/* incompatible features (none so far) */
99*6a54128fSAndroid Build Coastguard Worker 	__le32 f_rocompat;	/* ro compatible features (none so far) */
100*6a54128fSAndroid Build Coastguard Worker 	__le32 pad32;		/* padding for fs_offset */
101*6a54128fSAndroid Build Coastguard Worker 	__le64 fs_offset;	/* filesystem offset */
102*6a54128fSAndroid Build Coastguard Worker 	__u8 padding[436];	/* padding */
103*6a54128fSAndroid Build Coastguard Worker 	__le32 header_crc;	/* crc32c of this header (but not this field) */
104*6a54128fSAndroid Build Coastguard Worker };
105*6a54128fSAndroid Build Coastguard Worker 
106*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MAX_EXTENT_BLOCKS	512	/* max extent size, in blocks */
107*6a54128fSAndroid Build Coastguard Worker 
108*6a54128fSAndroid Build Coastguard Worker struct undo_key {
109*6a54128fSAndroid Build Coastguard Worker 	__le64 fsblk;		/* where in the fs does the block go */
110*6a54128fSAndroid Build Coastguard Worker 	__le32 blk_crc;		/* crc32c of the block */
111*6a54128fSAndroid Build Coastguard Worker 	__le32 size;		/* how many bytes in this block? */
112*6a54128fSAndroid Build Coastguard Worker };
113*6a54128fSAndroid Build Coastguard Worker 
114*6a54128fSAndroid Build Coastguard Worker struct undo_key_block {
115*6a54128fSAndroid Build Coastguard Worker 	__le32 magic;		/* KEYBLOCK_MAGIC number */
116*6a54128fSAndroid Build Coastguard Worker 	__le32 crc;		/* block checksum */
117*6a54128fSAndroid Build Coastguard Worker 	__le64 reserved;	/* zero */
118*6a54128fSAndroid Build Coastguard Worker 
119*6a54128fSAndroid Build Coastguard Worker #if __STDC_VERSION__ >= 199901L
120*6a54128fSAndroid Build Coastguard Worker 	struct undo_key keys[];		/* keys, which come immediately after */
121*6a54128fSAndroid Build Coastguard Worker #else
122*6a54128fSAndroid Build Coastguard Worker 	struct undo_key keys[0];	/* keys, which come immediately after */
123*6a54128fSAndroid Build Coastguard Worker #endif
124*6a54128fSAndroid Build Coastguard Worker };
125*6a54128fSAndroid Build Coastguard Worker 
126*6a54128fSAndroid Build Coastguard Worker struct undo_private_data {
127*6a54128fSAndroid Build Coastguard Worker 	int	magic;
128*6a54128fSAndroid Build Coastguard Worker 
129*6a54128fSAndroid Build Coastguard Worker 	/* the undo file io channel */
130*6a54128fSAndroid Build Coastguard Worker 	io_channel undo_file;
131*6a54128fSAndroid Build Coastguard Worker 	blk64_t undo_blk_num;			/* next free block */
132*6a54128fSAndroid Build Coastguard Worker 	blk64_t key_blk_num;			/* current key block location */
133*6a54128fSAndroid Build Coastguard Worker 	blk64_t super_blk_num;			/* superblock location */
134*6a54128fSAndroid Build Coastguard Worker 	blk64_t first_key_blk;			/* first key block location */
135*6a54128fSAndroid Build Coastguard Worker 	struct undo_key_block *keyb;
136*6a54128fSAndroid Build Coastguard Worker 	size_t num_keys, keys_in_block;
137*6a54128fSAndroid Build Coastguard Worker 
138*6a54128fSAndroid Build Coastguard Worker 	/* The backing io channel */
139*6a54128fSAndroid Build Coastguard Worker 	io_channel real;
140*6a54128fSAndroid Build Coastguard Worker 
141*6a54128fSAndroid Build Coastguard Worker 	unsigned long long tdb_data_size;
142*6a54128fSAndroid Build Coastguard Worker 	int tdb_written;
143*6a54128fSAndroid Build Coastguard Worker 
144*6a54128fSAndroid Build Coastguard Worker 	/* to support offset in unix I/O manager */
145*6a54128fSAndroid Build Coastguard Worker 	ext2_loff_t offset;
146*6a54128fSAndroid Build Coastguard Worker 
147*6a54128fSAndroid Build Coastguard Worker 	ext2fs_block_bitmap written_block_map;
148*6a54128fSAndroid Build Coastguard Worker 	struct struct_ext2_filsys fake_fs;
149*6a54128fSAndroid Build Coastguard Worker 	char *tdb_file;
150*6a54128fSAndroid Build Coastguard Worker 	struct undo_header hdr;
151*6a54128fSAndroid Build Coastguard Worker };
152*6a54128fSAndroid Build Coastguard Worker #define KEYS_PER_BLOCK(d) (((d)->tdb_data_size / sizeof(struct undo_key)) - 1)
153*6a54128fSAndroid Build Coastguard Worker 
154*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_FEATURE_COMPAT_FS_OFFSET 0x1	/* the filesystem offset */
155*6a54128fSAndroid Build Coastguard Worker 
e2undo_set_feature_fs_offset(struct undo_header * header)156*6a54128fSAndroid Build Coastguard Worker static inline void e2undo_set_feature_fs_offset(struct undo_header *header) {
157*6a54128fSAndroid Build Coastguard Worker 	header->f_compat |= ext2fs_le32_to_cpu(E2UNDO_FEATURE_COMPAT_FS_OFFSET);
158*6a54128fSAndroid Build Coastguard Worker }
159*6a54128fSAndroid Build Coastguard Worker 
e2undo_clear_feature_fs_offset(struct undo_header * header)160*6a54128fSAndroid Build Coastguard Worker static inline void e2undo_clear_feature_fs_offset(struct undo_header *header) {
161*6a54128fSAndroid Build Coastguard Worker 	header->f_compat &= ~ext2fs_le32_to_cpu(E2UNDO_FEATURE_COMPAT_FS_OFFSET);
162*6a54128fSAndroid Build Coastguard Worker }
163*6a54128fSAndroid Build Coastguard Worker 
164*6a54128fSAndroid Build Coastguard Worker static io_manager undo_io_backing_manager;
165*6a54128fSAndroid Build Coastguard Worker static char *tdb_file;
166*6a54128fSAndroid Build Coastguard Worker static int actual_size;
167*6a54128fSAndroid Build Coastguard Worker 
set_undo_io_backing_manager(io_manager manager)168*6a54128fSAndroid Build Coastguard Worker errcode_t set_undo_io_backing_manager(io_manager manager)
169*6a54128fSAndroid Build Coastguard Worker {
170*6a54128fSAndroid Build Coastguard Worker 	/*
171*6a54128fSAndroid Build Coastguard Worker 	 * We may want to do some validation later
172*6a54128fSAndroid Build Coastguard Worker 	 */
173*6a54128fSAndroid Build Coastguard Worker 	undo_io_backing_manager = manager;
174*6a54128fSAndroid Build Coastguard Worker 	return 0;
175*6a54128fSAndroid Build Coastguard Worker }
176*6a54128fSAndroid Build Coastguard Worker 
set_undo_io_backup_file(char * file_name)177*6a54128fSAndroid Build Coastguard Worker errcode_t set_undo_io_backup_file(char *file_name)
178*6a54128fSAndroid Build Coastguard Worker {
179*6a54128fSAndroid Build Coastguard Worker 	tdb_file = strdup(file_name);
180*6a54128fSAndroid Build Coastguard Worker 
181*6a54128fSAndroid Build Coastguard Worker 	if (tdb_file == NULL) {
182*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_NO_MEMORY;
183*6a54128fSAndroid Build Coastguard Worker 	}
184*6a54128fSAndroid Build Coastguard Worker 
185*6a54128fSAndroid Build Coastguard Worker 	return 0;
186*6a54128fSAndroid Build Coastguard Worker }
187*6a54128fSAndroid Build Coastguard Worker 
write_undo_indexes(struct undo_private_data * data,int flush)188*6a54128fSAndroid Build Coastguard Worker static errcode_t write_undo_indexes(struct undo_private_data *data, int flush)
189*6a54128fSAndroid Build Coastguard Worker {
190*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
191*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block super;
192*6a54128fSAndroid Build Coastguard Worker 	io_channel channel;
193*6a54128fSAndroid Build Coastguard Worker 	int block_size;
194*6a54128fSAndroid Build Coastguard Worker 	__u32 sb_crc, hdr_crc;
195*6a54128fSAndroid Build Coastguard Worker 
196*6a54128fSAndroid Build Coastguard Worker 	/* Spit out a key block, if there's any data */
197*6a54128fSAndroid Build Coastguard Worker 	if (data->keys_in_block) {
198*6a54128fSAndroid Build Coastguard Worker 		data->keyb->magic = ext2fs_cpu_to_le32(KEYBLOCK_MAGIC);
199*6a54128fSAndroid Build Coastguard Worker 		data->keyb->crc = 0;
200*6a54128fSAndroid Build Coastguard Worker 		data->keyb->crc = ext2fs_cpu_to_le32(
201*6a54128fSAndroid Build Coastguard Worker 					 ext2fs_crc32c_le(~0,
202*6a54128fSAndroid Build Coastguard Worker 					 (unsigned char *)data->keyb,
203*6a54128fSAndroid Build Coastguard Worker 					 data->tdb_data_size));
204*6a54128fSAndroid Build Coastguard Worker 		dbg_printf("Writing keyblock to blk %llu\n", data->key_blk_num);
205*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(data->undo_file,
206*6a54128fSAndroid Build Coastguard Worker 						data->key_blk_num,
207*6a54128fSAndroid Build Coastguard Worker 						1, data->keyb);
208*6a54128fSAndroid Build Coastguard Worker 		if (retval)
209*6a54128fSAndroid Build Coastguard Worker 			return retval;
210*6a54128fSAndroid Build Coastguard Worker 		/* Move on to the next key block if it's full. */
211*6a54128fSAndroid Build Coastguard Worker 		if (data->keys_in_block == KEYS_PER_BLOCK(data)) {
212*6a54128fSAndroid Build Coastguard Worker 			memset(data->keyb, 0, data->tdb_data_size);
213*6a54128fSAndroid Build Coastguard Worker 			data->keys_in_block = 0;
214*6a54128fSAndroid Build Coastguard Worker 			data->key_blk_num = data->undo_blk_num;
215*6a54128fSAndroid Build Coastguard Worker 			data->undo_blk_num++;
216*6a54128fSAndroid Build Coastguard Worker 		}
217*6a54128fSAndroid Build Coastguard Worker 	}
218*6a54128fSAndroid Build Coastguard Worker 
219*6a54128fSAndroid Build Coastguard Worker 	/* Prepare superblock for write */
220*6a54128fSAndroid Build Coastguard Worker 	channel = data->real;
221*6a54128fSAndroid Build Coastguard Worker 	block_size = channel->block_size;
222*6a54128fSAndroid Build Coastguard Worker 
223*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
224*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
225*6a54128fSAndroid Build Coastguard Worker 	if (retval)
226*6a54128fSAndroid Build Coastguard Worker 		goto err_out;
227*6a54128fSAndroid Build Coastguard Worker 	sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)&super, SUPERBLOCK_SIZE);
228*6a54128fSAndroid Build Coastguard Worker 	super.s_magic = ~super.s_magic;
229*6a54128fSAndroid Build Coastguard Worker 
230*6a54128fSAndroid Build Coastguard Worker 	/* Write the undo header to disk. */
231*6a54128fSAndroid Build Coastguard Worker 	memcpy(data->hdr.magic, E2UNDO_MAGIC, sizeof(data->hdr.magic));
232*6a54128fSAndroid Build Coastguard Worker 	data->hdr.num_keys = ext2fs_cpu_to_le64(data->num_keys);
233*6a54128fSAndroid Build Coastguard Worker 	data->hdr.super_offset = ext2fs_cpu_to_le64(data->super_blk_num);
234*6a54128fSAndroid Build Coastguard Worker 	data->hdr.key_offset = ext2fs_cpu_to_le64(data->first_key_blk);
235*6a54128fSAndroid Build Coastguard Worker 	data->hdr.fs_block_size = ext2fs_cpu_to_le32(block_size);
236*6a54128fSAndroid Build Coastguard Worker 	data->hdr.sb_crc = ext2fs_cpu_to_le32(sb_crc);
237*6a54128fSAndroid Build Coastguard Worker 	data->hdr.fs_offset = ext2fs_cpu_to_le64(data->offset);
238*6a54128fSAndroid Build Coastguard Worker 	if (data->offset)
239*6a54128fSAndroid Build Coastguard Worker 		e2undo_set_feature_fs_offset(&data->hdr);
240*6a54128fSAndroid Build Coastguard Worker 	else
241*6a54128fSAndroid Build Coastguard Worker 		e2undo_clear_feature_fs_offset(&data->hdr);
242*6a54128fSAndroid Build Coastguard Worker 	hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&data->hdr,
243*6a54128fSAndroid Build Coastguard Worker 				   sizeof(data->hdr) -
244*6a54128fSAndroid Build Coastguard Worker 				   sizeof(data->hdr.header_crc));
245*6a54128fSAndroid Build Coastguard Worker 	data->hdr.header_crc = ext2fs_cpu_to_le32(hdr_crc);
246*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_write_blk64(data->undo_file, 0,
247*6a54128fSAndroid Build Coastguard Worker 					-(int)sizeof(data->hdr),
248*6a54128fSAndroid Build Coastguard Worker 					&data->hdr);
249*6a54128fSAndroid Build Coastguard Worker 	if (retval)
250*6a54128fSAndroid Build Coastguard Worker 		goto err_out;
251*6a54128fSAndroid Build Coastguard Worker 
252*6a54128fSAndroid Build Coastguard Worker 	/*
253*6a54128fSAndroid Build Coastguard Worker 	 * Record the entire superblock (in FS byte order) so that we can't
254*6a54128fSAndroid Build Coastguard Worker 	 * apply e2undo files to the wrong FS or out of order.
255*6a54128fSAndroid Build Coastguard Worker 	 */
256*6a54128fSAndroid Build Coastguard Worker 	dbg_printf("Writing superblock to block %llu\n", data->super_blk_num);
257*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_write_blk64(data->undo_file, data->super_blk_num,
258*6a54128fSAndroid Build Coastguard Worker 					-SUPERBLOCK_SIZE, &super);
259*6a54128fSAndroid Build Coastguard Worker 	if (retval)
260*6a54128fSAndroid Build Coastguard Worker 		goto err_out;
261*6a54128fSAndroid Build Coastguard Worker 
262*6a54128fSAndroid Build Coastguard Worker 	if (flush)
263*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_flush(data->undo_file);
264*6a54128fSAndroid Build Coastguard Worker err_out:
265*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(channel, block_size);
266*6a54128fSAndroid Build Coastguard Worker 	return retval;
267*6a54128fSAndroid Build Coastguard Worker }
268*6a54128fSAndroid Build Coastguard Worker 
undo_setup_tdb(struct undo_private_data * data)269*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_setup_tdb(struct undo_private_data *data)
270*6a54128fSAndroid Build Coastguard Worker {
271*6a54128fSAndroid Build Coastguard Worker 	int i;
272*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
273*6a54128fSAndroid Build Coastguard Worker 
274*6a54128fSAndroid Build Coastguard Worker 	if (data->tdb_written == 1)
275*6a54128fSAndroid Build Coastguard Worker 		return 0;
276*6a54128fSAndroid Build Coastguard Worker 
277*6a54128fSAndroid Build Coastguard Worker 	data->tdb_written = 1;
278*6a54128fSAndroid Build Coastguard Worker 
279*6a54128fSAndroid Build Coastguard Worker 	/* Make a bitmap to track what we've written */
280*6a54128fSAndroid Build Coastguard Worker 	memset(&data->fake_fs, 0, sizeof(data->fake_fs));
281*6a54128fSAndroid Build Coastguard Worker 	data->fake_fs.blocksize = data->tdb_data_size;
282*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_alloc_generic_bmap(&data->fake_fs,
283*6a54128fSAndroid Build Coastguard Worker 				EXT2_ET_MAGIC_BLOCK_BITMAP64,
284*6a54128fSAndroid Build Coastguard Worker 				EXT2FS_BMAP64_RBTREE,
285*6a54128fSAndroid Build Coastguard Worker 				0, ~1ULL, ~1ULL,
286*6a54128fSAndroid Build Coastguard Worker 				"undo block map", &data->written_block_map);
287*6a54128fSAndroid Build Coastguard Worker 	if (retval)
288*6a54128fSAndroid Build Coastguard Worker 		return retval;
289*6a54128fSAndroid Build Coastguard Worker 
290*6a54128fSAndroid Build Coastguard Worker 	/* Allocate key block */
291*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(data->tdb_data_size, &data->keyb);
292*6a54128fSAndroid Build Coastguard Worker 	if (retval)
293*6a54128fSAndroid Build Coastguard Worker 		return retval;
294*6a54128fSAndroid Build Coastguard Worker 	data->key_blk_num = data->first_key_blk;
295*6a54128fSAndroid Build Coastguard Worker 
296*6a54128fSAndroid Build Coastguard Worker 	/* Record block size */
297*6a54128fSAndroid Build Coastguard Worker 	dbg_printf("Undo block size %llu\n", data->tdb_data_size);
298*6a54128fSAndroid Build Coastguard Worker 	dbg_printf("Keys per block %llu\n", KEYS_PER_BLOCK(data));
299*6a54128fSAndroid Build Coastguard Worker 	data->hdr.block_size = ext2fs_cpu_to_le32(data->tdb_data_size);
300*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(data->undo_file, data->tdb_data_size);
301*6a54128fSAndroid Build Coastguard Worker 
302*6a54128fSAndroid Build Coastguard Worker 	/* Ensure that we have space for header blocks */
303*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i <= 2; i++) {
304*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(data->undo_file, i, 1,
305*6a54128fSAndroid Build Coastguard Worker 					       data->keyb);
306*6a54128fSAndroid Build Coastguard Worker 		if (retval)
307*6a54128fSAndroid Build Coastguard Worker 			memset(data->keyb, 0, data->tdb_data_size);
308*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(data->undo_file, i, 1,
309*6a54128fSAndroid Build Coastguard Worker 						data->keyb);
310*6a54128fSAndroid Build Coastguard Worker 		if (retval)
311*6a54128fSAndroid Build Coastguard Worker 			return retval;
312*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_flush(data->undo_file);
313*6a54128fSAndroid Build Coastguard Worker 		if (retval)
314*6a54128fSAndroid Build Coastguard Worker 			return retval;
315*6a54128fSAndroid Build Coastguard Worker 	}
316*6a54128fSAndroid Build Coastguard Worker 	memset(data->keyb, 0, data->tdb_data_size);
317*6a54128fSAndroid Build Coastguard Worker 	return 0;
318*6a54128fSAndroid Build Coastguard Worker }
319*6a54128fSAndroid Build Coastguard Worker 
undo_write_tdb(io_channel channel,unsigned long long block,int count)320*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_write_tdb(io_channel channel,
321*6a54128fSAndroid Build Coastguard Worker 				unsigned long long block, int count)
322*6a54128fSAndroid Build Coastguard Worker 
323*6a54128fSAndroid Build Coastguard Worker {
324*6a54128fSAndroid Build Coastguard Worker 	int size, sz;
325*6a54128fSAndroid Build Coastguard Worker 	unsigned long long block_num, backing_blk_num;
326*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = 0;
327*6a54128fSAndroid Build Coastguard Worker 	ext2_loff_t offset;
328*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
329*6a54128fSAndroid Build Coastguard Worker 	unsigned char *read_ptr;
330*6a54128fSAndroid Build Coastguard Worker 	unsigned long long end_block;
331*6a54128fSAndroid Build Coastguard Worker 	unsigned long long data_size;
332*6a54128fSAndroid Build Coastguard Worker 	struct undo_key *key;
333*6a54128fSAndroid Build Coastguard Worker 	__u32 blk_crc;
334*6a54128fSAndroid Build Coastguard Worker 
335*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
336*6a54128fSAndroid Build Coastguard Worker 
337*6a54128fSAndroid Build Coastguard Worker 	if (data->undo_file == NULL) {
338*6a54128fSAndroid Build Coastguard Worker 		/*
339*6a54128fSAndroid Build Coastguard Worker 		 * Transaction database not initialized
340*6a54128fSAndroid Build Coastguard Worker 		 */
341*6a54128fSAndroid Build Coastguard Worker 		return 0;
342*6a54128fSAndroid Build Coastguard Worker 	}
343*6a54128fSAndroid Build Coastguard Worker 
344*6a54128fSAndroid Build Coastguard Worker 	if (count == 1)
345*6a54128fSAndroid Build Coastguard Worker 		size = channel->block_size;
346*6a54128fSAndroid Build Coastguard Worker 	else {
347*6a54128fSAndroid Build Coastguard Worker 		if (count < 0)
348*6a54128fSAndroid Build Coastguard Worker 			size = -count;
349*6a54128fSAndroid Build Coastguard Worker 		else
350*6a54128fSAndroid Build Coastguard Worker 			size = count * channel->block_size;
351*6a54128fSAndroid Build Coastguard Worker 	}
352*6a54128fSAndroid Build Coastguard Worker 
353*6a54128fSAndroid Build Coastguard Worker 	retval = undo_setup_tdb(data);
354*6a54128fSAndroid Build Coastguard Worker 	if (retval)
355*6a54128fSAndroid Build Coastguard Worker 		return retval;
356*6a54128fSAndroid Build Coastguard Worker 	/*
357*6a54128fSAndroid Build Coastguard Worker 	 * Data is stored in tdb database as blocks of tdb_data_size size
358*6a54128fSAndroid Build Coastguard Worker 	 * This helps in efficient lookup further.
359*6a54128fSAndroid Build Coastguard Worker 	 *
360*6a54128fSAndroid Build Coastguard Worker 	 * We divide the disk to blocks of tdb_data_size.
361*6a54128fSAndroid Build Coastguard Worker 	 */
362*6a54128fSAndroid Build Coastguard Worker 	offset = (block * channel->block_size) + data->offset ;
363*6a54128fSAndroid Build Coastguard Worker 	block_num = offset / data->tdb_data_size;
364*6a54128fSAndroid Build Coastguard Worker 	end_block = (offset + size - 1) / data->tdb_data_size;
365*6a54128fSAndroid Build Coastguard Worker 
366*6a54128fSAndroid Build Coastguard Worker 	while (block_num <= end_block) {
367*6a54128fSAndroid Build Coastguard Worker 		__u32 keysz;
368*6a54128fSAndroid Build Coastguard Worker 
369*6a54128fSAndroid Build Coastguard Worker 		/*
370*6a54128fSAndroid Build Coastguard Worker 		 * Check if we have the record already
371*6a54128fSAndroid Build Coastguard Worker 		 */
372*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_test_block_bitmap2(data->written_block_map,
373*6a54128fSAndroid Build Coastguard Worker 						   block_num)) {
374*6a54128fSAndroid Build Coastguard Worker 			/* Try the next block */
375*6a54128fSAndroid Build Coastguard Worker 			block_num++;
376*6a54128fSAndroid Build Coastguard Worker 			continue;
377*6a54128fSAndroid Build Coastguard Worker 		}
378*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(data->written_block_map, block_num);
379*6a54128fSAndroid Build Coastguard Worker 
380*6a54128fSAndroid Build Coastguard Worker 		/*
381*6a54128fSAndroid Build Coastguard Worker 		 * Read one block using the backing I/O manager
382*6a54128fSAndroid Build Coastguard Worker 		 * The backing I/O manager block size may be
383*6a54128fSAndroid Build Coastguard Worker 		 * different from the tdb_data_size.
384*6a54128fSAndroid Build Coastguard Worker 		 * Also we need to recalculate the block number with respect
385*6a54128fSAndroid Build Coastguard Worker 		 * to the backing I/O manager.
386*6a54128fSAndroid Build Coastguard Worker 		 */
387*6a54128fSAndroid Build Coastguard Worker 		offset = block_num * data->tdb_data_size +
388*6a54128fSAndroid Build Coastguard Worker 				(data->offset % data->tdb_data_size);
389*6a54128fSAndroid Build Coastguard Worker 		backing_blk_num = (offset - data->offset) / channel->block_size;
390*6a54128fSAndroid Build Coastguard Worker 
391*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_get_mem(data->tdb_data_size, &read_ptr);
392*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
393*6a54128fSAndroid Build Coastguard Worker 			return retval;
394*6a54128fSAndroid Build Coastguard Worker 		}
395*6a54128fSAndroid Build Coastguard Worker 
396*6a54128fSAndroid Build Coastguard Worker 		memset(read_ptr, 0, data->tdb_data_size);
397*6a54128fSAndroid Build Coastguard Worker 		actual_size = 0;
398*6a54128fSAndroid Build Coastguard Worker 		if ((data->tdb_data_size % channel->block_size) == 0)
399*6a54128fSAndroid Build Coastguard Worker 			sz = data->tdb_data_size / channel->block_size;
400*6a54128fSAndroid Build Coastguard Worker 		else
401*6a54128fSAndroid Build Coastguard Worker 			sz = -data->tdb_data_size;
402*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(data->real, backing_blk_num,
403*6a54128fSAndroid Build Coastguard Worker 					     sz, read_ptr);
404*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
405*6a54128fSAndroid Build Coastguard Worker 			if (retval != EXT2_ET_SHORT_READ) {
406*6a54128fSAndroid Build Coastguard Worker 				free(read_ptr);
407*6a54128fSAndroid Build Coastguard Worker 				return retval;
408*6a54128fSAndroid Build Coastguard Worker 			}
409*6a54128fSAndroid Build Coastguard Worker 			/*
410*6a54128fSAndroid Build Coastguard Worker 			 * short read so update the record size
411*6a54128fSAndroid Build Coastguard Worker 			 * accordingly
412*6a54128fSAndroid Build Coastguard Worker 			 */
413*6a54128fSAndroid Build Coastguard Worker 			data_size = actual_size;
414*6a54128fSAndroid Build Coastguard Worker 		} else {
415*6a54128fSAndroid Build Coastguard Worker 			data_size = data->tdb_data_size;
416*6a54128fSAndroid Build Coastguard Worker 		}
417*6a54128fSAndroid Build Coastguard Worker 		if (data_size == 0) {
418*6a54128fSAndroid Build Coastguard Worker 			free(read_ptr);
419*6a54128fSAndroid Build Coastguard Worker 			block_num++;
420*6a54128fSAndroid Build Coastguard Worker 			continue;
421*6a54128fSAndroid Build Coastguard Worker 		}
422*6a54128fSAndroid Build Coastguard Worker 		dbg_printf("Read %llu bytes from FS block %llu (blk=%llu cnt=%llu)\n",
423*6a54128fSAndroid Build Coastguard Worker 		       data_size, backing_blk_num, block, data->tdb_data_size);
424*6a54128fSAndroid Build Coastguard Worker 		if ((data_size % data->undo_file->block_size) == 0)
425*6a54128fSAndroid Build Coastguard Worker 			sz = data_size / data->undo_file->block_size;
426*6a54128fSAndroid Build Coastguard Worker 		else
427*6a54128fSAndroid Build Coastguard Worker 			sz = -data_size;;
428*6a54128fSAndroid Build Coastguard Worker 		/* extend this key? */
429*6a54128fSAndroid Build Coastguard Worker 		if (data->keys_in_block) {
430*6a54128fSAndroid Build Coastguard Worker 			key = data->keyb->keys + data->keys_in_block - 1;
431*6a54128fSAndroid Build Coastguard Worker 			keysz = ext2fs_le32_to_cpu(key->size);
432*6a54128fSAndroid Build Coastguard Worker 		} else {
433*6a54128fSAndroid Build Coastguard Worker 			key = NULL;
434*6a54128fSAndroid Build Coastguard Worker 			keysz = 0;
435*6a54128fSAndroid Build Coastguard Worker 		}
436*6a54128fSAndroid Build Coastguard Worker 		if (key != NULL &&
437*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_le64_to_cpu(key->fsblk) * channel->block_size +
438*6a54128fSAndroid Build Coastguard Worker 		     channel->block_size - 1 +
439*6a54128fSAndroid Build Coastguard Worker 		     keysz) / channel->block_size == backing_blk_num &&
440*6a54128fSAndroid Build Coastguard Worker 		    E2UNDO_MAX_EXTENT_BLOCKS * data->tdb_data_size >
441*6a54128fSAndroid Build Coastguard Worker 		    keysz + data_size) {
442*6a54128fSAndroid Build Coastguard Worker 			blk_crc = ext2fs_le32_to_cpu(key->blk_crc);
443*6a54128fSAndroid Build Coastguard Worker 			blk_crc = ext2fs_crc32c_le(blk_crc, read_ptr, data_size);
444*6a54128fSAndroid Build Coastguard Worker 			key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
445*6a54128fSAndroid Build Coastguard Worker 			key->size = ext2fs_cpu_to_le32(keysz + data_size);
446*6a54128fSAndroid Build Coastguard Worker 		} else {
447*6a54128fSAndroid Build Coastguard Worker 			data->num_keys++;
448*6a54128fSAndroid Build Coastguard Worker 			key = data->keyb->keys + data->keys_in_block;
449*6a54128fSAndroid Build Coastguard Worker 			data->keys_in_block++;
450*6a54128fSAndroid Build Coastguard Worker 			key->fsblk = ext2fs_cpu_to_le64(backing_blk_num);
451*6a54128fSAndroid Build Coastguard Worker 			blk_crc = ext2fs_crc32c_le(~0, read_ptr, data_size);
452*6a54128fSAndroid Build Coastguard Worker 			key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
453*6a54128fSAndroid Build Coastguard Worker 			key->size = ext2fs_cpu_to_le32(data_size);
454*6a54128fSAndroid Build Coastguard Worker 		}
455*6a54128fSAndroid Build Coastguard Worker 		dbg_printf("Writing block %llu to offset %llu size %d key %zu\n",
456*6a54128fSAndroid Build Coastguard Worker 		       block_num,
457*6a54128fSAndroid Build Coastguard Worker 		       data->undo_blk_num,
458*6a54128fSAndroid Build Coastguard Worker 		       sz, data->num_keys - 1);
459*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(data->undo_file,
460*6a54128fSAndroid Build Coastguard Worker 					data->undo_blk_num, sz, read_ptr);
461*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
462*6a54128fSAndroid Build Coastguard Worker 			free(read_ptr);
463*6a54128fSAndroid Build Coastguard Worker 			return retval;
464*6a54128fSAndroid Build Coastguard Worker 		}
465*6a54128fSAndroid Build Coastguard Worker 		data->undo_blk_num++;
466*6a54128fSAndroid Build Coastguard Worker 		free(read_ptr);
467*6a54128fSAndroid Build Coastguard Worker 
468*6a54128fSAndroid Build Coastguard Worker 		/* Write out the key block */
469*6a54128fSAndroid Build Coastguard Worker 		retval = write_undo_indexes(data, 0);
470*6a54128fSAndroid Build Coastguard Worker 		if (retval)
471*6a54128fSAndroid Build Coastguard Worker 			return retval;
472*6a54128fSAndroid Build Coastguard Worker 
473*6a54128fSAndroid Build Coastguard Worker 		/* Next block */
474*6a54128fSAndroid Build Coastguard Worker 		block_num++;
475*6a54128fSAndroid Build Coastguard Worker 	}
476*6a54128fSAndroid Build Coastguard Worker 
477*6a54128fSAndroid Build Coastguard Worker 	return retval;
478*6a54128fSAndroid Build Coastguard Worker }
479*6a54128fSAndroid Build Coastguard Worker 
undo_io_read_error(io_channel channel ATTR ((unused)),unsigned long block ATTR ((unused)),int count ATTR ((unused)),void * data ATTR ((unused)),size_t size ATTR ((unused)),int actual,errcode_t error ATTR ((unused)))480*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_io_read_error(io_channel channel ATTR((unused)),
481*6a54128fSAndroid Build Coastguard Worker 				    unsigned long block ATTR((unused)),
482*6a54128fSAndroid Build Coastguard Worker 				    int count ATTR((unused)),
483*6a54128fSAndroid Build Coastguard Worker 				    void *data ATTR((unused)),
484*6a54128fSAndroid Build Coastguard Worker 				    size_t size ATTR((unused)),
485*6a54128fSAndroid Build Coastguard Worker 				    int actual,
486*6a54128fSAndroid Build Coastguard Worker 				    errcode_t error ATTR((unused)))
487*6a54128fSAndroid Build Coastguard Worker {
488*6a54128fSAndroid Build Coastguard Worker 	actual_size = actual;
489*6a54128fSAndroid Build Coastguard Worker 	return error;
490*6a54128fSAndroid Build Coastguard Worker }
491*6a54128fSAndroid Build Coastguard Worker 
undo_err_handler_init(io_channel channel)492*6a54128fSAndroid Build Coastguard Worker static void undo_err_handler_init(io_channel channel)
493*6a54128fSAndroid Build Coastguard Worker {
494*6a54128fSAndroid Build Coastguard Worker 	channel->read_error = undo_io_read_error;
495*6a54128fSAndroid Build Coastguard Worker }
496*6a54128fSAndroid Build Coastguard Worker 
check_filesystem(struct undo_header * hdr,io_channel undo_file,unsigned int blocksize,blk64_t super_block,io_channel channel)497*6a54128fSAndroid Build Coastguard Worker static int check_filesystem(struct undo_header *hdr, io_channel undo_file,
498*6a54128fSAndroid Build Coastguard Worker 			    unsigned int blocksize, blk64_t super_block,
499*6a54128fSAndroid Build Coastguard Worker 			    io_channel channel)
500*6a54128fSAndroid Build Coastguard Worker {
501*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block super, *sb;
502*6a54128fSAndroid Build Coastguard Worker 	char *buf;
503*6a54128fSAndroid Build Coastguard Worker 	__u32 sb_crc;
504*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
505*6a54128fSAndroid Build Coastguard Worker 
506*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
507*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
508*6a54128fSAndroid Build Coastguard Worker 	if (retval)
509*6a54128fSAndroid Build Coastguard Worker 		return retval;
510*6a54128fSAndroid Build Coastguard Worker 
511*6a54128fSAndroid Build Coastguard Worker 	/*
512*6a54128fSAndroid Build Coastguard Worker 	 * Compare the FS and the undo file superblock so that we don't
513*6a54128fSAndroid Build Coastguard Worker 	 * append to something that doesn't match this FS.
514*6a54128fSAndroid Build Coastguard Worker 	 */
515*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(blocksize, &buf);
516*6a54128fSAndroid Build Coastguard Worker 	if (retval)
517*6a54128fSAndroid Build Coastguard Worker 		return retval;
518*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_read_blk64(undo_file, super_block,
519*6a54128fSAndroid Build Coastguard Worker 				       -SUPERBLOCK_SIZE, buf);
520*6a54128fSAndroid Build Coastguard Worker 	if (retval)
521*6a54128fSAndroid Build Coastguard Worker 		goto out;
522*6a54128fSAndroid Build Coastguard Worker 	sb = (struct ext2_super_block *)buf;
523*6a54128fSAndroid Build Coastguard Worker 	sb->s_magic = ~sb->s_magic;
524*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(&super, buf, sizeof(super))) {
525*6a54128fSAndroid Build Coastguard Worker 		retval = -1;
526*6a54128fSAndroid Build Coastguard Worker 		goto out;
527*6a54128fSAndroid Build Coastguard Worker 	}
528*6a54128fSAndroid Build Coastguard Worker 	sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, SUPERBLOCK_SIZE);
529*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_le32_to_cpu(hdr->sb_crc) != sb_crc) {
530*6a54128fSAndroid Build Coastguard Worker 		retval = -1;
531*6a54128fSAndroid Build Coastguard Worker 		goto out;
532*6a54128fSAndroid Build Coastguard Worker 	}
533*6a54128fSAndroid Build Coastguard Worker 
534*6a54128fSAndroid Build Coastguard Worker out:
535*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&buf);
536*6a54128fSAndroid Build Coastguard Worker 	return retval;
537*6a54128fSAndroid Build Coastguard Worker }
538*6a54128fSAndroid Build Coastguard Worker 
539*6a54128fSAndroid Build Coastguard Worker /*
540*6a54128fSAndroid Build Coastguard Worker  * Try to re-open the undo file, so that we can resume where we left off.
541*6a54128fSAndroid Build Coastguard Worker  * That way, the user can pass the same undo file to various programs as
542*6a54128fSAndroid Build Coastguard Worker  * part of an FS upgrade instead of having to create multiple files and
543*6a54128fSAndroid Build Coastguard Worker  * then apply them in correct order.
544*6a54128fSAndroid Build Coastguard Worker  */
try_reopen_undo_file(int undo_fd,struct undo_private_data * data)545*6a54128fSAndroid Build Coastguard Worker static errcode_t try_reopen_undo_file(int undo_fd,
546*6a54128fSAndroid Build Coastguard Worker 				      struct undo_private_data *data)
547*6a54128fSAndroid Build Coastguard Worker {
548*6a54128fSAndroid Build Coastguard Worker 	struct undo_header hdr;
549*6a54128fSAndroid Build Coastguard Worker 	struct undo_key *dkey;
550*6a54128fSAndroid Build Coastguard Worker 	ext2fs_struct_stat statbuf;
551*6a54128fSAndroid Build Coastguard Worker 	unsigned int blocksize, fs_blocksize;
552*6a54128fSAndroid Build Coastguard Worker 	blk64_t super_block, lblk;
553*6a54128fSAndroid Build Coastguard Worker 	size_t num_keys, keys_per_block, i;
554*6a54128fSAndroid Build Coastguard Worker 	__u32 hdr_crc, key_crc;
555*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
556*6a54128fSAndroid Build Coastguard Worker 
557*6a54128fSAndroid Build Coastguard Worker 	/* Zero size already? */
558*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_fstat(undo_fd, &statbuf);
559*6a54128fSAndroid Build Coastguard Worker 	if (retval)
560*6a54128fSAndroid Build Coastguard Worker 		goto bad_file;
561*6a54128fSAndroid Build Coastguard Worker 	if (statbuf.st_size == 0)
562*6a54128fSAndroid Build Coastguard Worker 		goto out;
563*6a54128fSAndroid Build Coastguard Worker 
564*6a54128fSAndroid Build Coastguard Worker 	/* check the file header */
565*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_read_blk64(data->undo_file, 0, -(int)sizeof(hdr),
566*6a54128fSAndroid Build Coastguard Worker 				       &hdr);
567*6a54128fSAndroid Build Coastguard Worker 	if (retval)
568*6a54128fSAndroid Build Coastguard Worker 		goto bad_file;
569*6a54128fSAndroid Build Coastguard Worker 
570*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(hdr.magic, E2UNDO_MAGIC,
571*6a54128fSAndroid Build Coastguard Worker 		    sizeof(hdr.magic)))
572*6a54128fSAndroid Build Coastguard Worker 		goto bad_file;
573*6a54128fSAndroid Build Coastguard Worker 	hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&hdr,
574*6a54128fSAndroid Build Coastguard Worker 				   sizeof(struct undo_header) -
575*6a54128fSAndroid Build Coastguard Worker 				   sizeof(__u32));
576*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_le32_to_cpu(hdr.header_crc) != hdr_crc)
577*6a54128fSAndroid Build Coastguard Worker 		goto bad_file;
578*6a54128fSAndroid Build Coastguard Worker 	blocksize = ext2fs_le32_to_cpu(hdr.block_size);
579*6a54128fSAndroid Build Coastguard Worker 	fs_blocksize = ext2fs_le32_to_cpu(hdr.fs_block_size);
580*6a54128fSAndroid Build Coastguard Worker 	if (blocksize > E2UNDO_MAX_BLOCK_SIZE ||
581*6a54128fSAndroid Build Coastguard Worker 	    blocksize < E2UNDO_MIN_BLOCK_SIZE ||
582*6a54128fSAndroid Build Coastguard Worker 	    !blocksize || !fs_blocksize)
583*6a54128fSAndroid Build Coastguard Worker 		goto bad_file;
584*6a54128fSAndroid Build Coastguard Worker 	super_block = ext2fs_le64_to_cpu(hdr.super_offset);
585*6a54128fSAndroid Build Coastguard Worker 	num_keys = ext2fs_le64_to_cpu(hdr.num_keys);
586*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(data->undo_file, blocksize);
587*6a54128fSAndroid Build Coastguard Worker 	/*
588*6a54128fSAndroid Build Coastguard Worker 	 * Do not compare hdr.f_compat with the available compatible
589*6a54128fSAndroid Build Coastguard Worker 	 * features set, because a "missing" compatible feature should
590*6a54128fSAndroid Build Coastguard Worker 	 * not cause any problems.
591*6a54128fSAndroid Build Coastguard Worker 	 */
592*6a54128fSAndroid Build Coastguard Worker 	if (hdr.f_incompat || hdr.f_rocompat)
593*6a54128fSAndroid Build Coastguard Worker 		goto bad_file;
594*6a54128fSAndroid Build Coastguard Worker 
595*6a54128fSAndroid Build Coastguard Worker 	/* Superblock matches this FS? */
596*6a54128fSAndroid Build Coastguard Worker 	if (check_filesystem(&hdr, data->undo_file, blocksize, super_block,
597*6a54128fSAndroid Build Coastguard Worker 			     data->real) != 0) {
598*6a54128fSAndroid Build Coastguard Worker 		retval = EXT2_ET_UNDO_FILE_WRONG;
599*6a54128fSAndroid Build Coastguard Worker 		goto out;
600*6a54128fSAndroid Build Coastguard Worker 	}
601*6a54128fSAndroid Build Coastguard Worker 
602*6a54128fSAndroid Build Coastguard Worker 	/* Try to set ourselves up */
603*6a54128fSAndroid Build Coastguard Worker 	data->tdb_data_size = blocksize;
604*6a54128fSAndroid Build Coastguard Worker 	retval = undo_setup_tdb(data);
605*6a54128fSAndroid Build Coastguard Worker 	if (retval)
606*6a54128fSAndroid Build Coastguard Worker 		goto bad_file;
607*6a54128fSAndroid Build Coastguard Worker 	data->num_keys = num_keys;
608*6a54128fSAndroid Build Coastguard Worker 	data->super_blk_num = super_block;
609*6a54128fSAndroid Build Coastguard Worker 	data->first_key_blk = ext2fs_le64_to_cpu(hdr.key_offset);
610*6a54128fSAndroid Build Coastguard Worker 
611*6a54128fSAndroid Build Coastguard Worker 	/* load the written block map */
612*6a54128fSAndroid Build Coastguard Worker 	keys_per_block = KEYS_PER_BLOCK(data);
613*6a54128fSAndroid Build Coastguard Worker 	lblk = data->first_key_blk;
614*6a54128fSAndroid Build Coastguard Worker 	dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n",
615*6a54128fSAndroid Build Coastguard Worker 		   num_keys, keys_per_block, blocksize);
616*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < num_keys; i += keys_per_block) {
617*6a54128fSAndroid Build Coastguard Worker 		size_t j, max_j;
618*6a54128fSAndroid Build Coastguard Worker 		__le32 crc;
619*6a54128fSAndroid Build Coastguard Worker 
620*6a54128fSAndroid Build Coastguard Worker 		data->key_blk_num = lblk;
621*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(data->undo_file,
622*6a54128fSAndroid Build Coastguard Worker 					       lblk, 1, data->keyb);
623*6a54128fSAndroid Build Coastguard Worker 		if (retval)
624*6a54128fSAndroid Build Coastguard Worker 			goto bad_key_replay;
625*6a54128fSAndroid Build Coastguard Worker 
626*6a54128fSAndroid Build Coastguard Worker 		/* check keys */
627*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_le32_to_cpu(data->keyb->magic) != KEYBLOCK_MAGIC) {
628*6a54128fSAndroid Build Coastguard Worker 			retval = EXT2_ET_UNDO_FILE_CORRUPT;
629*6a54128fSAndroid Build Coastguard Worker 			goto bad_key_replay;
630*6a54128fSAndroid Build Coastguard Worker 		}
631*6a54128fSAndroid Build Coastguard Worker 		crc = data->keyb->crc;
632*6a54128fSAndroid Build Coastguard Worker 		data->keyb->crc = 0;
633*6a54128fSAndroid Build Coastguard Worker 		key_crc = ext2fs_crc32c_le(~0, (unsigned char *)data->keyb,
634*6a54128fSAndroid Build Coastguard Worker 					   blocksize);
635*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_le32_to_cpu(crc) != key_crc) {
636*6a54128fSAndroid Build Coastguard Worker 			retval = EXT2_ET_UNDO_FILE_CORRUPT;
637*6a54128fSAndroid Build Coastguard Worker 			goto bad_key_replay;
638*6a54128fSAndroid Build Coastguard Worker 		}
639*6a54128fSAndroid Build Coastguard Worker 
640*6a54128fSAndroid Build Coastguard Worker 		/* load keys from key block */
641*6a54128fSAndroid Build Coastguard Worker 		lblk++;
642*6a54128fSAndroid Build Coastguard Worker 		max_j = data->num_keys - i;
643*6a54128fSAndroid Build Coastguard Worker 		if (max_j > keys_per_block)
644*6a54128fSAndroid Build Coastguard Worker 			max_j = keys_per_block;
645*6a54128fSAndroid Build Coastguard Worker 		for (j = 0, dkey = data->keyb->keys;
646*6a54128fSAndroid Build Coastguard Worker 		     j < max_j;
647*6a54128fSAndroid Build Coastguard Worker 		     j++, dkey++) {
648*6a54128fSAndroid Build Coastguard Worker 			blk64_t fsblk = ext2fs_le64_to_cpu(dkey->fsblk);
649*6a54128fSAndroid Build Coastguard Worker 			blk64_t undo_blk = fsblk * fs_blocksize / blocksize;
650*6a54128fSAndroid Build Coastguard Worker 			size_t size = ext2fs_le32_to_cpu(dkey->size);
651*6a54128fSAndroid Build Coastguard Worker 
652*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_block_bitmap_range2(data->written_block_map,
653*6a54128fSAndroid Build Coastguard Worker 					 undo_blk,
654*6a54128fSAndroid Build Coastguard Worker 					(size + blocksize - 1) / blocksize);
655*6a54128fSAndroid Build Coastguard Worker 			lblk += (size + blocksize - 1) / blocksize;
656*6a54128fSAndroid Build Coastguard Worker 			data->undo_blk_num = lblk;
657*6a54128fSAndroid Build Coastguard Worker 			data->keys_in_block = j + 1;
658*6a54128fSAndroid Build Coastguard Worker 		}
659*6a54128fSAndroid Build Coastguard Worker 	}
660*6a54128fSAndroid Build Coastguard Worker 	dbg_printf("Reopen undo, keyblk=%llu undoblk=%llu nrkeys=%zu kib=%zu\n",
661*6a54128fSAndroid Build Coastguard Worker 		   data->key_blk_num, data->undo_blk_num, data->num_keys,
662*6a54128fSAndroid Build Coastguard Worker 		   data->keys_in_block);
663*6a54128fSAndroid Build Coastguard Worker 
664*6a54128fSAndroid Build Coastguard Worker 	data->hdr.state = hdr.state & ~E2UNDO_STATE_FINISHED;
665*6a54128fSAndroid Build Coastguard Worker 	data->hdr.f_compat = hdr.f_compat;
666*6a54128fSAndroid Build Coastguard Worker 	data->hdr.f_incompat = hdr.f_incompat;
667*6a54128fSAndroid Build Coastguard Worker 	data->hdr.f_rocompat = hdr.f_rocompat;
668*6a54128fSAndroid Build Coastguard Worker 	return retval;
669*6a54128fSAndroid Build Coastguard Worker 
670*6a54128fSAndroid Build Coastguard Worker bad_key_replay:
671*6a54128fSAndroid Build Coastguard Worker 	data->key_blk_num = data->undo_blk_num = 0;
672*6a54128fSAndroid Build Coastguard Worker 	data->keys_in_block = 0;
673*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&data->keyb);
674*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_generic_bitmap(data->written_block_map);
675*6a54128fSAndroid Build Coastguard Worker 	data->tdb_written = 0;
676*6a54128fSAndroid Build Coastguard Worker 	goto out;
677*6a54128fSAndroid Build Coastguard Worker bad_file:
678*6a54128fSAndroid Build Coastguard Worker 	retval = EXT2_ET_UNDO_FILE_CORRUPT;
679*6a54128fSAndroid Build Coastguard Worker out:
680*6a54128fSAndroid Build Coastguard Worker 	return retval;
681*6a54128fSAndroid Build Coastguard Worker }
682*6a54128fSAndroid Build Coastguard Worker 
undo_atexit(void * p)683*6a54128fSAndroid Build Coastguard Worker static void undo_atexit(void *p)
684*6a54128fSAndroid Build Coastguard Worker {
685*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data = p;
686*6a54128fSAndroid Build Coastguard Worker 	errcode_t err;
687*6a54128fSAndroid Build Coastguard Worker 
688*6a54128fSAndroid Build Coastguard Worker 	err = write_undo_indexes(data, 1);
689*6a54128fSAndroid Build Coastguard Worker 	io_channel_close(data->undo_file);
690*6a54128fSAndroid Build Coastguard Worker 
691*6a54128fSAndroid Build Coastguard Worker 	com_err(data->tdb_file, err, "while force-closing undo file");
692*6a54128fSAndroid Build Coastguard Worker }
693*6a54128fSAndroid Build Coastguard Worker 
undo_open(const char * name,int flags,io_channel * channel)694*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_open(const char *name, int flags, io_channel *channel)
695*6a54128fSAndroid Build Coastguard Worker {
696*6a54128fSAndroid Build Coastguard Worker 	io_channel	io = NULL;
697*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data = NULL;
698*6a54128fSAndroid Build Coastguard Worker 	int		undo_fd = -1;
699*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
700*6a54128fSAndroid Build Coastguard Worker 
701*6a54128fSAndroid Build Coastguard Worker 	/* We don't support multi-threading, at least for now */
702*6a54128fSAndroid Build Coastguard Worker 	flags &= ~IO_FLAG_THREADS;
703*6a54128fSAndroid Build Coastguard Worker 	if (name == 0)
704*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_BAD_DEVICE_NAME;
705*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
706*6a54128fSAndroid Build Coastguard Worker 	if (retval)
707*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
708*6a54128fSAndroid Build Coastguard Worker 	memset(io, 0, sizeof(struct struct_io_channel));
709*6a54128fSAndroid Build Coastguard Worker 	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
710*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(sizeof(struct undo_private_data), &data);
711*6a54128fSAndroid Build Coastguard Worker 	if (retval)
712*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
713*6a54128fSAndroid Build Coastguard Worker 
714*6a54128fSAndroid Build Coastguard Worker 	io->manager = undo_io_manager;
715*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(strlen(name)+1, &io->name);
716*6a54128fSAndroid Build Coastguard Worker 	if (retval)
717*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
718*6a54128fSAndroid Build Coastguard Worker 
719*6a54128fSAndroid Build Coastguard Worker 	strcpy(io->name, name);
720*6a54128fSAndroid Build Coastguard Worker 	io->private_data = data;
721*6a54128fSAndroid Build Coastguard Worker 	io->block_size = 1024;
722*6a54128fSAndroid Build Coastguard Worker 	io->read_error = 0;
723*6a54128fSAndroid Build Coastguard Worker 	io->write_error = 0;
724*6a54128fSAndroid Build Coastguard Worker 	io->refcount = 1;
725*6a54128fSAndroid Build Coastguard Worker 
726*6a54128fSAndroid Build Coastguard Worker 	memset(data, 0, sizeof(struct undo_private_data));
727*6a54128fSAndroid Build Coastguard Worker 	data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
728*6a54128fSAndroid Build Coastguard Worker 	data->super_blk_num = 1;
729*6a54128fSAndroid Build Coastguard Worker 	data->first_key_blk = 2;
730*6a54128fSAndroid Build Coastguard Worker 	data->undo_blk_num = 3;
731*6a54128fSAndroid Build Coastguard Worker 
732*6a54128fSAndroid Build Coastguard Worker 	if (undo_io_backing_manager) {
733*6a54128fSAndroid Build Coastguard Worker 		retval = undo_io_backing_manager->open(name, flags,
734*6a54128fSAndroid Build Coastguard Worker 						       &data->real);
735*6a54128fSAndroid Build Coastguard Worker 		if (retval)
736*6a54128fSAndroid Build Coastguard Worker 			goto cleanup;
737*6a54128fSAndroid Build Coastguard Worker 
738*6a54128fSAndroid Build Coastguard Worker 		data->tdb_file = strdup(tdb_file);
739*6a54128fSAndroid Build Coastguard Worker 		if (data->tdb_file == NULL)
740*6a54128fSAndroid Build Coastguard Worker 			goto cleanup;
741*6a54128fSAndroid Build Coastguard Worker 		undo_fd = ext2fs_open_file(data->tdb_file, O_RDWR | O_CREAT,
742*6a54128fSAndroid Build Coastguard Worker 					   0600);
743*6a54128fSAndroid Build Coastguard Worker 		if (undo_fd < 0)
744*6a54128fSAndroid Build Coastguard Worker 			goto cleanup;
745*6a54128fSAndroid Build Coastguard Worker 
746*6a54128fSAndroid Build Coastguard Worker 		retval = undo_io_backing_manager->open(data->tdb_file,
747*6a54128fSAndroid Build Coastguard Worker 						       IO_FLAG_RW,
748*6a54128fSAndroid Build Coastguard Worker 						       &data->undo_file);
749*6a54128fSAndroid Build Coastguard Worker 		if (retval)
750*6a54128fSAndroid Build Coastguard Worker 			goto cleanup;
751*6a54128fSAndroid Build Coastguard Worker 	} else {
752*6a54128fSAndroid Build Coastguard Worker 		data->real = NULL;
753*6a54128fSAndroid Build Coastguard Worker 		data->undo_file = NULL;
754*6a54128fSAndroid Build Coastguard Worker 	}
755*6a54128fSAndroid Build Coastguard Worker 
756*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
757*6a54128fSAndroid Build Coastguard Worker 		io->flags = (io->flags & ~CHANNEL_FLAGS_DISCARD_ZEROES) |
758*6a54128fSAndroid Build Coastguard Worker 			    (data->real->flags & CHANNEL_FLAGS_DISCARD_ZEROES);
759*6a54128fSAndroid Build Coastguard Worker 
760*6a54128fSAndroid Build Coastguard Worker 	/*
761*6a54128fSAndroid Build Coastguard Worker 	 * setup err handler for read so that we know
762*6a54128fSAndroid Build Coastguard Worker 	 * when the backing manager fails do short read
763*6a54128fSAndroid Build Coastguard Worker 	 */
764*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
765*6a54128fSAndroid Build Coastguard Worker 		undo_err_handler_init(data->real);
766*6a54128fSAndroid Build Coastguard Worker 
767*6a54128fSAndroid Build Coastguard Worker 	if (data->undo_file) {
768*6a54128fSAndroid Build Coastguard Worker 		retval = try_reopen_undo_file(undo_fd, data);
769*6a54128fSAndroid Build Coastguard Worker 		if (retval)
770*6a54128fSAndroid Build Coastguard Worker 			goto cleanup;
771*6a54128fSAndroid Build Coastguard Worker 	}
772*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_add_exit_fn(undo_atexit, data);
773*6a54128fSAndroid Build Coastguard Worker 	if (retval)
774*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
775*6a54128fSAndroid Build Coastguard Worker 
776*6a54128fSAndroid Build Coastguard Worker 	*channel = io;
777*6a54128fSAndroid Build Coastguard Worker 	if (undo_fd >= 0)
778*6a54128fSAndroid Build Coastguard Worker 		close(undo_fd);
779*6a54128fSAndroid Build Coastguard Worker 	return retval;
780*6a54128fSAndroid Build Coastguard Worker 
781*6a54128fSAndroid Build Coastguard Worker cleanup:
782*6a54128fSAndroid Build Coastguard Worker 	ext2fs_remove_exit_fn(undo_atexit, data);
783*6a54128fSAndroid Build Coastguard Worker 	if (undo_fd >= 0)
784*6a54128fSAndroid Build Coastguard Worker 		close(undo_fd);
785*6a54128fSAndroid Build Coastguard Worker 	if (data && data->undo_file)
786*6a54128fSAndroid Build Coastguard Worker 		io_channel_close(data->undo_file);
787*6a54128fSAndroid Build Coastguard Worker 	if (data && data->tdb_file)
788*6a54128fSAndroid Build Coastguard Worker 		free(data->tdb_file);
789*6a54128fSAndroid Build Coastguard Worker 	if (data && data->real)
790*6a54128fSAndroid Build Coastguard Worker 		io_channel_close(data->real);
791*6a54128fSAndroid Build Coastguard Worker 	if (data)
792*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&data);
793*6a54128fSAndroid Build Coastguard Worker 	if (io && io->name)
794*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&io->name);
795*6a54128fSAndroid Build Coastguard Worker 	if (io)
796*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&io);
797*6a54128fSAndroid Build Coastguard Worker 	return retval;
798*6a54128fSAndroid Build Coastguard Worker }
799*6a54128fSAndroid Build Coastguard Worker 
undo_close(io_channel channel)800*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_close(io_channel channel)
801*6a54128fSAndroid Build Coastguard Worker {
802*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
803*6a54128fSAndroid Build Coastguard Worker 	errcode_t	err, retval = 0;
804*6a54128fSAndroid Build Coastguard Worker 
805*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
806*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
807*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
808*6a54128fSAndroid Build Coastguard Worker 
809*6a54128fSAndroid Build Coastguard Worker 	if (--channel->refcount > 0)
810*6a54128fSAndroid Build Coastguard Worker 		return 0;
811*6a54128fSAndroid Build Coastguard Worker 	/* Before closing write the file system identity */
812*6a54128fSAndroid Build Coastguard Worker 	if (!getenv("UNDO_IO_SIMULATE_UNFINISHED"))
813*6a54128fSAndroid Build Coastguard Worker 		data->hdr.state = ext2fs_cpu_to_le32(E2UNDO_STATE_FINISHED);
814*6a54128fSAndroid Build Coastguard Worker 	err = write_undo_indexes(data, 1);
815*6a54128fSAndroid Build Coastguard Worker 	ext2fs_remove_exit_fn(undo_atexit, data);
816*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
817*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_close(data->real);
818*6a54128fSAndroid Build Coastguard Worker 	if (data->tdb_file)
819*6a54128fSAndroid Build Coastguard Worker 		free(data->tdb_file);
820*6a54128fSAndroid Build Coastguard Worker 	if (data->undo_file)
821*6a54128fSAndroid Build Coastguard Worker 		io_channel_close(data->undo_file);
822*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&data->keyb);
823*6a54128fSAndroid Build Coastguard Worker 	if (data->written_block_map)
824*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_generic_bitmap(data->written_block_map);
825*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&channel->private_data);
826*6a54128fSAndroid Build Coastguard Worker 	if (channel->name)
827*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&channel->name);
828*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&channel);
829*6a54128fSAndroid Build Coastguard Worker 
830*6a54128fSAndroid Build Coastguard Worker 	if (err)
831*6a54128fSAndroid Build Coastguard Worker 		return err;
832*6a54128fSAndroid Build Coastguard Worker 	return retval;
833*6a54128fSAndroid Build Coastguard Worker }
834*6a54128fSAndroid Build Coastguard Worker 
undo_set_blksize(io_channel channel,int blksize)835*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_set_blksize(io_channel channel, int blksize)
836*6a54128fSAndroid Build Coastguard Worker {
837*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
838*6a54128fSAndroid Build Coastguard Worker 	errcode_t		retval = 0;
839*6a54128fSAndroid Build Coastguard Worker 
840*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
841*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
842*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
843*6a54128fSAndroid Build Coastguard Worker 
844*6a54128fSAndroid Build Coastguard Worker 	if (blksize > E2UNDO_MAX_BLOCK_SIZE || blksize < E2UNDO_MIN_BLOCK_SIZE)
845*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INVALID_ARGUMENT;
846*6a54128fSAndroid Build Coastguard Worker 
847*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
848*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_set_blksize(data->real, blksize);
849*6a54128fSAndroid Build Coastguard Worker 	/*
850*6a54128fSAndroid Build Coastguard Worker 	 * Set the block size used for tdb
851*6a54128fSAndroid Build Coastguard Worker 	 */
852*6a54128fSAndroid Build Coastguard Worker 	if (!data->tdb_data_size || !data->tdb_written)
853*6a54128fSAndroid Build Coastguard Worker 		data->tdb_data_size = blksize;
854*6a54128fSAndroid Build Coastguard Worker 	channel->block_size = blksize;
855*6a54128fSAndroid Build Coastguard Worker 	return retval;
856*6a54128fSAndroid Build Coastguard Worker }
857*6a54128fSAndroid Build Coastguard Worker 
undo_read_blk64(io_channel channel,unsigned long long block,int count,void * buf)858*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
859*6a54128fSAndroid Build Coastguard Worker 			       int count, void *buf)
860*6a54128fSAndroid Build Coastguard Worker {
861*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
862*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
863*6a54128fSAndroid Build Coastguard Worker 
864*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
865*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
866*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
867*6a54128fSAndroid Build Coastguard Worker 
868*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
869*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(data->real, block, count, buf);
870*6a54128fSAndroid Build Coastguard Worker 
871*6a54128fSAndroid Build Coastguard Worker 	return retval;
872*6a54128fSAndroid Build Coastguard Worker }
873*6a54128fSAndroid Build Coastguard Worker 
undo_read_blk(io_channel channel,unsigned long block,int count,void * buf)874*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_read_blk(io_channel channel, unsigned long block,
875*6a54128fSAndroid Build Coastguard Worker 			       int count, void *buf)
876*6a54128fSAndroid Build Coastguard Worker {
877*6a54128fSAndroid Build Coastguard Worker 	return undo_read_blk64(channel, block, count, buf);
878*6a54128fSAndroid Build Coastguard Worker }
879*6a54128fSAndroid Build Coastguard Worker 
undo_write_blk64(io_channel channel,unsigned long long block,int count,const void * buf)880*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
881*6a54128fSAndroid Build Coastguard Worker 				int count, const void *buf)
882*6a54128fSAndroid Build Coastguard Worker {
883*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
884*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
885*6a54128fSAndroid Build Coastguard Worker 
886*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
887*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
888*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
889*6a54128fSAndroid Build Coastguard Worker 	/*
890*6a54128fSAndroid Build Coastguard Worker 	 * First write the existing content into database
891*6a54128fSAndroid Build Coastguard Worker 	 */
892*6a54128fSAndroid Build Coastguard Worker 	retval = undo_write_tdb(channel, block, count);
893*6a54128fSAndroid Build Coastguard Worker 	if (retval)
894*6a54128fSAndroid Build Coastguard Worker 		 return retval;
895*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
896*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(data->real, block, count, buf);
897*6a54128fSAndroid Build Coastguard Worker 
898*6a54128fSAndroid Build Coastguard Worker 	return retval;
899*6a54128fSAndroid Build Coastguard Worker }
900*6a54128fSAndroid Build Coastguard Worker 
undo_write_blk(io_channel channel,unsigned long block,int count,const void * buf)901*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_write_blk(io_channel channel, unsigned long block,
902*6a54128fSAndroid Build Coastguard Worker 				int count, const void *buf)
903*6a54128fSAndroid Build Coastguard Worker {
904*6a54128fSAndroid Build Coastguard Worker 	return undo_write_blk64(channel, block, count, buf);
905*6a54128fSAndroid Build Coastguard Worker }
906*6a54128fSAndroid Build Coastguard Worker 
undo_write_byte(io_channel channel,unsigned long offset,int size,const void * buf)907*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
908*6a54128fSAndroid Build Coastguard Worker 				 int size, const void *buf)
909*6a54128fSAndroid Build Coastguard Worker {
910*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
911*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
912*6a54128fSAndroid Build Coastguard Worker 	ext2_loff_t	location;
913*6a54128fSAndroid Build Coastguard Worker 	unsigned long blk_num, count;;
914*6a54128fSAndroid Build Coastguard Worker 
915*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
916*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
917*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
918*6a54128fSAndroid Build Coastguard Worker 
919*6a54128fSAndroid Build Coastguard Worker 	location = offset + data->offset;
920*6a54128fSAndroid Build Coastguard Worker 	blk_num = location/channel->block_size;
921*6a54128fSAndroid Build Coastguard Worker 	/*
922*6a54128fSAndroid Build Coastguard Worker 	 * the size specified may spread across multiple blocks
923*6a54128fSAndroid Build Coastguard Worker 	 * also make sure we account for the fact that block start
924*6a54128fSAndroid Build Coastguard Worker 	 * offset for tdb is different from the backing I/O manager
925*6a54128fSAndroid Build Coastguard Worker 	 * due to possible different block size
926*6a54128fSAndroid Build Coastguard Worker 	 */
927*6a54128fSAndroid Build Coastguard Worker 	count = (size + (location % channel->block_size) +
928*6a54128fSAndroid Build Coastguard Worker 			channel->block_size  -1)/channel->block_size;
929*6a54128fSAndroid Build Coastguard Worker 	retval = undo_write_tdb(channel, blk_num, count);
930*6a54128fSAndroid Build Coastguard Worker 	if (retval)
931*6a54128fSAndroid Build Coastguard Worker 		return retval;
932*6a54128fSAndroid Build Coastguard Worker 	if (data->real && data->real->manager->write_byte)
933*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_byte(data->real, offset, size, buf);
934*6a54128fSAndroid Build Coastguard Worker 
935*6a54128fSAndroid Build Coastguard Worker 	return retval;
936*6a54128fSAndroid Build Coastguard Worker }
937*6a54128fSAndroid Build Coastguard Worker 
undo_discard(io_channel channel,unsigned long long block,unsigned long long count)938*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_discard(io_channel channel, unsigned long long block,
939*6a54128fSAndroid Build Coastguard Worker 			      unsigned long long count)
940*6a54128fSAndroid Build Coastguard Worker {
941*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
942*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
943*6a54128fSAndroid Build Coastguard Worker 	int icount;
944*6a54128fSAndroid Build Coastguard Worker 
945*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
946*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
947*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
948*6a54128fSAndroid Build Coastguard Worker 
949*6a54128fSAndroid Build Coastguard Worker 	if (count > INT_MAX)
950*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_UNIMPLEMENTED;
951*6a54128fSAndroid Build Coastguard Worker 	icount = count;
952*6a54128fSAndroid Build Coastguard Worker 
953*6a54128fSAndroid Build Coastguard Worker 	/*
954*6a54128fSAndroid Build Coastguard Worker 	 * First write the existing content into database
955*6a54128fSAndroid Build Coastguard Worker 	 */
956*6a54128fSAndroid Build Coastguard Worker 	retval = undo_write_tdb(channel, block, icount);
957*6a54128fSAndroid Build Coastguard Worker 	if (retval)
958*6a54128fSAndroid Build Coastguard Worker 		return retval;
959*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
960*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_discard(data->real, block, count);
961*6a54128fSAndroid Build Coastguard Worker 
962*6a54128fSAndroid Build Coastguard Worker 	return retval;
963*6a54128fSAndroid Build Coastguard Worker }
964*6a54128fSAndroid Build Coastguard Worker 
undo_zeroout(io_channel channel,unsigned long long block,unsigned long long count)965*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_zeroout(io_channel channel, unsigned long long block,
966*6a54128fSAndroid Build Coastguard Worker 			      unsigned long long count)
967*6a54128fSAndroid Build Coastguard Worker {
968*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
969*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
970*6a54128fSAndroid Build Coastguard Worker 	int icount;
971*6a54128fSAndroid Build Coastguard Worker 
972*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
973*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
974*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
975*6a54128fSAndroid Build Coastguard Worker 
976*6a54128fSAndroid Build Coastguard Worker 	if (count > INT_MAX)
977*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_UNIMPLEMENTED;
978*6a54128fSAndroid Build Coastguard Worker 	icount = count;
979*6a54128fSAndroid Build Coastguard Worker 
980*6a54128fSAndroid Build Coastguard Worker 	/*
981*6a54128fSAndroid Build Coastguard Worker 	 * First write the existing content into database
982*6a54128fSAndroid Build Coastguard Worker 	 */
983*6a54128fSAndroid Build Coastguard Worker 	retval = undo_write_tdb(channel, block, icount);
984*6a54128fSAndroid Build Coastguard Worker 	if (retval)
985*6a54128fSAndroid Build Coastguard Worker 		return retval;
986*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
987*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_zeroout(data->real, block, count);
988*6a54128fSAndroid Build Coastguard Worker 
989*6a54128fSAndroid Build Coastguard Worker 	return retval;
990*6a54128fSAndroid Build Coastguard Worker }
991*6a54128fSAndroid Build Coastguard Worker 
undo_cache_readahead(io_channel channel,unsigned long long block,unsigned long long count)992*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_cache_readahead(io_channel channel,
993*6a54128fSAndroid Build Coastguard Worker 				      unsigned long long block,
994*6a54128fSAndroid Build Coastguard Worker 				      unsigned long long count)
995*6a54128fSAndroid Build Coastguard Worker {
996*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
997*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
998*6a54128fSAndroid Build Coastguard Worker 
999*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1000*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
1001*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1002*6a54128fSAndroid Build Coastguard Worker 
1003*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
1004*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_cache_readahead(data->real, block, count);
1005*6a54128fSAndroid Build Coastguard Worker 
1006*6a54128fSAndroid Build Coastguard Worker 	return retval;
1007*6a54128fSAndroid Build Coastguard Worker }
1008*6a54128fSAndroid Build Coastguard Worker 
1009*6a54128fSAndroid Build Coastguard Worker /*
1010*6a54128fSAndroid Build Coastguard Worker  * Flush data buffers to disk.
1011*6a54128fSAndroid Build Coastguard Worker  */
undo_flush(io_channel channel)1012*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_flush(io_channel channel)
1013*6a54128fSAndroid Build Coastguard Worker {
1014*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
1015*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
1016*6a54128fSAndroid Build Coastguard Worker 
1017*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1018*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
1019*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1020*6a54128fSAndroid Build Coastguard Worker 
1021*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
1022*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_flush(data->real);
1023*6a54128fSAndroid Build Coastguard Worker 
1024*6a54128fSAndroid Build Coastguard Worker 	return retval;
1025*6a54128fSAndroid Build Coastguard Worker }
1026*6a54128fSAndroid Build Coastguard Worker 
undo_set_option(io_channel channel,const char * option,const char * arg)1027*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_set_option(io_channel channel, const char *option,
1028*6a54128fSAndroid Build Coastguard Worker 				 const char *arg)
1029*6a54128fSAndroid Build Coastguard Worker {
1030*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
1031*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
1032*6a54128fSAndroid Build Coastguard Worker 	unsigned long tmp;
1033*6a54128fSAndroid Build Coastguard Worker 	char *end;
1034*6a54128fSAndroid Build Coastguard Worker 
1035*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1036*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
1037*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1038*6a54128fSAndroid Build Coastguard Worker 
1039*6a54128fSAndroid Build Coastguard Worker 	if (!strcmp(option, "tdb_data_size")) {
1040*6a54128fSAndroid Build Coastguard Worker 		if (!arg)
1041*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_INVALID_ARGUMENT;
1042*6a54128fSAndroid Build Coastguard Worker 
1043*6a54128fSAndroid Build Coastguard Worker 		tmp = strtoul(arg, &end, 0);
1044*6a54128fSAndroid Build Coastguard Worker 		if (*end)
1045*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_INVALID_ARGUMENT;
1046*6a54128fSAndroid Build Coastguard Worker 		if (tmp > E2UNDO_MAX_BLOCK_SIZE || tmp < E2UNDO_MIN_BLOCK_SIZE)
1047*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_INVALID_ARGUMENT;
1048*6a54128fSAndroid Build Coastguard Worker 		if (!data->tdb_data_size || !data->tdb_written) {
1049*6a54128fSAndroid Build Coastguard Worker 			data->tdb_written = -1;
1050*6a54128fSAndroid Build Coastguard Worker 			data->tdb_data_size = tmp;
1051*6a54128fSAndroid Build Coastguard Worker 		}
1052*6a54128fSAndroid Build Coastguard Worker 		return 0;
1053*6a54128fSAndroid Build Coastguard Worker 	}
1054*6a54128fSAndroid Build Coastguard Worker 	/*
1055*6a54128fSAndroid Build Coastguard Worker 	 * Need to support offset option to work with
1056*6a54128fSAndroid Build Coastguard Worker 	 * Unix I/O manager
1057*6a54128fSAndroid Build Coastguard Worker 	 */
1058*6a54128fSAndroid Build Coastguard Worker 	if (data->real && data->real->manager->set_option) {
1059*6a54128fSAndroid Build Coastguard Worker 		retval = data->real->manager->set_option(data->real,
1060*6a54128fSAndroid Build Coastguard Worker 							option, arg);
1061*6a54128fSAndroid Build Coastguard Worker 	}
1062*6a54128fSAndroid Build Coastguard Worker 	if (!retval && !strcmp(option, "offset")) {
1063*6a54128fSAndroid Build Coastguard Worker 		if (!arg)
1064*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_INVALID_ARGUMENT;
1065*6a54128fSAndroid Build Coastguard Worker 
1066*6a54128fSAndroid Build Coastguard Worker 		tmp = strtoul(arg, &end, 0);
1067*6a54128fSAndroid Build Coastguard Worker 		if (*end)
1068*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_INVALID_ARGUMENT;
1069*6a54128fSAndroid Build Coastguard Worker 		data->offset = tmp;
1070*6a54128fSAndroid Build Coastguard Worker 	}
1071*6a54128fSAndroid Build Coastguard Worker 	return retval;
1072*6a54128fSAndroid Build Coastguard Worker }
1073*6a54128fSAndroid Build Coastguard Worker 
undo_get_stats(io_channel channel,io_stats * stats)1074*6a54128fSAndroid Build Coastguard Worker static errcode_t undo_get_stats(io_channel channel, io_stats *stats)
1075*6a54128fSAndroid Build Coastguard Worker {
1076*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
1077*6a54128fSAndroid Build Coastguard Worker 	struct undo_private_data *data;
1078*6a54128fSAndroid Build Coastguard Worker 
1079*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1080*6a54128fSAndroid Build Coastguard Worker 	data = (struct undo_private_data *) channel->private_data;
1081*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1082*6a54128fSAndroid Build Coastguard Worker 
1083*6a54128fSAndroid Build Coastguard Worker 	if (data->real)
1084*6a54128fSAndroid Build Coastguard Worker 		retval = (data->real->manager->get_stats)(data->real, stats);
1085*6a54128fSAndroid Build Coastguard Worker 
1086*6a54128fSAndroid Build Coastguard Worker 	return retval;
1087*6a54128fSAndroid Build Coastguard Worker }
1088*6a54128fSAndroid Build Coastguard Worker 
1089*6a54128fSAndroid Build Coastguard Worker static struct struct_io_manager struct_undo_manager = {
1090*6a54128fSAndroid Build Coastguard Worker 	.magic		= EXT2_ET_MAGIC_IO_MANAGER,
1091*6a54128fSAndroid Build Coastguard Worker 	.name		= "Undo I/O Manager",
1092*6a54128fSAndroid Build Coastguard Worker 	.open		= undo_open,
1093*6a54128fSAndroid Build Coastguard Worker 	.close		= undo_close,
1094*6a54128fSAndroid Build Coastguard Worker 	.set_blksize	= undo_set_blksize,
1095*6a54128fSAndroid Build Coastguard Worker 	.read_blk	= undo_read_blk,
1096*6a54128fSAndroid Build Coastguard Worker 	.write_blk	= undo_write_blk,
1097*6a54128fSAndroid Build Coastguard Worker 	.flush		= undo_flush,
1098*6a54128fSAndroid Build Coastguard Worker 	.write_byte	= undo_write_byte,
1099*6a54128fSAndroid Build Coastguard Worker 	.set_option	= undo_set_option,
1100*6a54128fSAndroid Build Coastguard Worker 	.get_stats	= undo_get_stats,
1101*6a54128fSAndroid Build Coastguard Worker 	.read_blk64	= undo_read_blk64,
1102*6a54128fSAndroid Build Coastguard Worker 	.write_blk64	= undo_write_blk64,
1103*6a54128fSAndroid Build Coastguard Worker 	.discard	= undo_discard,
1104*6a54128fSAndroid Build Coastguard Worker 	.zeroout	= undo_zeroout,
1105*6a54128fSAndroid Build Coastguard Worker 	.cache_readahead	= undo_cache_readahead,
1106*6a54128fSAndroid Build Coastguard Worker };
1107*6a54128fSAndroid Build Coastguard Worker 
1108*6a54128fSAndroid Build Coastguard Worker io_manager undo_io_manager = &struct_undo_manager;
1109