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