Whamcloud - gitweb
b9294c360c46d20969881278ef56eb72fbf13a3a
[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, int flush)
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                 /* Move on to the next key block if it's full. */
191                 if (data->keys_in_block == KEYS_PER_BLOCK(data)) {
192                         memset(data->keyb, 0, data->tdb_data_size);
193                         data->keys_in_block = 0;
194                         data->key_blk_num = data->undo_blk_num;
195                         data->undo_blk_num++;
196                         flush = 1;
197                 }
198         }
199
200         /* Prepare superblock for write */
201         channel = data->real;
202         block_size = channel->block_size;
203
204         io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
205         retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
206         if (retval)
207                 goto err_out;
208         sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)&super, SUPERBLOCK_SIZE);
209         super.s_magic = ~super.s_magic;
210
211         /* Write the undo header to disk. */
212         memcpy(data->hdr.magic, E2UNDO_MAGIC, sizeof(data->hdr.magic));
213         data->hdr.num_keys = ext2fs_cpu_to_le64(data->num_keys);
214         data->hdr.super_offset = ext2fs_cpu_to_le64(data->super_blk_num);
215         data->hdr.key_offset = ext2fs_cpu_to_le64(data->first_key_blk);
216         data->hdr.fs_block_size = ext2fs_cpu_to_le32(block_size);
217         data->hdr.sb_crc = ext2fs_cpu_to_le32(sb_crc);
218         hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&data->hdr,
219                                    sizeof(data->hdr) -
220                                    sizeof(data->hdr.header_crc));
221         data->hdr.header_crc = ext2fs_cpu_to_le32(hdr_crc);
222         retval = io_channel_write_blk64(data->undo_file, 0,
223                                         -(int)sizeof(data->hdr),
224                                         &data->hdr);
225         if (retval)
226                 goto err_out;
227
228         /*
229          * Record the entire superblock (in FS byte order) so that we can't
230          * apply e2undo files to the wrong FS or out of order.
231          */
232         dbg_printf("Writing superblock to block %llu\n", data->super_blk_num);
233         retval = io_channel_write_blk64(data->undo_file, data->super_blk_num,
234                                         -SUPERBLOCK_SIZE, &super);
235         if (retval)
236                 goto err_out;
237
238         if (flush)
239                 retval = io_channel_flush(data->undo_file);
240 err_out:
241         io_channel_set_blksize(channel, block_size);
242         return retval;
243 }
244
245 static errcode_t undo_setup_tdb(struct undo_private_data *data)
246 {
247         int i;
248         errcode_t retval;
249
250         if (data->tdb_written == 1)
251                 return 0;
252
253         data->tdb_written = 1;
254
255         /* Make a bitmap to track what we've written */
256         memset(&data->fake_fs, 0, sizeof(data->fake_fs));
257         data->fake_fs.blocksize = data->tdb_data_size;
258         retval = ext2fs_alloc_generic_bmap(&data->fake_fs,
259                                 EXT2_ET_MAGIC_BLOCK_BITMAP64,
260                                 EXT2FS_BMAP64_RBTREE,
261                                 0, ~1ULL, ~1ULL,
262                                 "undo block map", &data->written_block_map);
263         if (retval)
264                 return retval;
265
266         /* Allocate key block */
267         retval = ext2fs_get_mem(data->tdb_data_size, &data->keyb);
268         if (retval)
269                 return retval;
270         data->key_blk_num = data->first_key_blk;
271
272         /* Record block size */
273         dbg_printf("Undo block size %llu\n", data->tdb_data_size);
274         dbg_printf("Keys per block %llu\n", KEYS_PER_BLOCK(data));
275         data->hdr.block_size = ext2fs_cpu_to_le32(data->tdb_data_size);
276         io_channel_set_blksize(data->undo_file, data->tdb_data_size);
277
278         /* Ensure that we have space for header blocks */
279         for (i = 0; i <= 2; i++) {
280                 retval = io_channel_read_blk64(data->undo_file, i, 1,
281                                                data->keyb);
282                 if (retval)
283                         memset(data->keyb, 0, data->tdb_data_size);
284                 retval = io_channel_write_blk64(data->undo_file, i, 1,
285                                                 data->keyb);
286                 if (retval)
287                         return retval;
288                 retval = io_channel_flush(data->undo_file);
289                 if (retval)
290                         return retval;
291         }
292         memset(data->keyb, 0, data->tdb_data_size);
293         return 0;
294 }
295
296 static errcode_t undo_write_tdb(io_channel channel,
297                                 unsigned long long block, int count)
298
299 {
300         int size, sz;
301         unsigned long long block_num, backing_blk_num;
302         errcode_t retval = 0;
303         ext2_loff_t offset;
304         struct undo_private_data *data;
305         unsigned char *read_ptr;
306         unsigned long long end_block;
307         unsigned long long data_size;
308         void *data_ptr;
309         struct undo_key *key;
310         __u32 blk_crc;
311
312         data = (struct undo_private_data *) channel->private_data;
313
314         if (data->undo_file == NULL) {
315                 /*
316                  * Transaction database not initialized
317                  */
318                 return 0;
319         }
320
321         if (count == 1)
322                 size = channel->block_size;
323         else {
324                 if (count < 0)
325                         size = -count;
326                 else
327                         size = count * channel->block_size;
328         }
329
330         retval = undo_setup_tdb(data);
331         if (retval)
332                 return retval;
333         /*
334          * Data is stored in tdb database as blocks of tdb_data_size size
335          * This helps in efficient lookup further.
336          *
337          * We divide the disk to blocks of tdb_data_size.
338          */
339         offset = (block * channel->block_size) + data->offset ;
340         block_num = offset / data->tdb_data_size;
341         end_block = (offset + size - 1) / data->tdb_data_size;
342
343         while (block_num <= end_block) {
344                 __u32 keysz;
345
346                 /*
347                  * Check if we have the record already
348                  */
349                 if (ext2fs_test_block_bitmap2(data->written_block_map,
350                                                    block_num)) {
351                         /* Try the next block */
352                         block_num++;
353                         continue;
354                 }
355                 ext2fs_mark_block_bitmap2(data->written_block_map, block_num);
356
357                 /*
358                  * Read one block using the backing I/O manager
359                  * The backing I/O manager block size may be
360                  * different from the tdb_data_size.
361                  * Also we need to recalcuate the block number with respect
362                  * to the backing I/O manager.
363                  */
364                 offset = block_num * data->tdb_data_size;
365                 backing_blk_num = (offset - data->offset) / channel->block_size;
366
367                 count = data->tdb_data_size +
368                                 ((offset - data->offset) % channel->block_size);
369                 retval = ext2fs_get_mem(count, &read_ptr);
370                 if (retval) {
371                         return retval;
372                 }
373
374                 memset(read_ptr, 0, count);
375                 actual_size = 0;
376                 if ((count % channel->block_size) == 0)
377                         sz = count / channel->block_size;
378                 else
379                         sz = -count;
380                 retval = io_channel_read_blk64(data->real, backing_blk_num,
381                                              sz, read_ptr);
382                 if (retval) {
383                         if (retval != EXT2_ET_SHORT_READ) {
384                                 free(read_ptr);
385                                 return retval;
386                         }
387                         /*
388                          * short read so update the record size
389                          * accordingly
390                          */
391                         data_size = actual_size;
392                 } else {
393                         data_size = data->tdb_data_size;
394                 }
395                 if (data_size == 0) {
396                         free(read_ptr);
397                         block_num++;
398                         continue;
399                 }
400                 dbg_printf("Read %llu bytes from FS block %llu (blk=%llu cnt=%u)\n",
401                        data_size, backing_blk_num, block, count);
402                 if ((data_size % data->undo_file->block_size) == 0)
403                         sz = data_size / data->undo_file->block_size;
404                 else
405                         sz = -actual_size;
406                 data_ptr = read_ptr + ((offset - data->offset) %
407                                        data->undo_file->block_size);
408                 /* extend this key? */
409                 if (data->keys_in_block) {
410                         key = data->keyb->keys + data->keys_in_block - 1;
411                         keysz = ext2fs_le32_to_cpu(key->size);
412                 } else {
413                         key = NULL;
414                         keysz = 0;
415                 }
416                 if (key != NULL &&
417                     ext2fs_le64_to_cpu(key->fsblk) +
418                     ((keysz + data->tdb_data_size - 1) /
419                      data->tdb_data_size) == backing_blk_num &&
420                     E2UNDO_MAX_EXTENT_BLOCKS * data->tdb_data_size >
421                     keysz + sz) {
422                         blk_crc = ext2fs_le32_to_cpu(key->blk_crc);
423                         blk_crc = ext2fs_crc32c_le(blk_crc,
424                                                    (unsigned char *)data_ptr,
425                                                    data_size);
426                         key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
427                         key->size = ext2fs_cpu_to_le32(keysz + data_size);
428                 } else {
429                         data->num_keys++;
430                         key = data->keyb->keys + data->keys_in_block;
431                         data->keys_in_block++;
432                         key->fsblk = ext2fs_cpu_to_le64(backing_blk_num);
433                         blk_crc = ext2fs_crc32c_le(~0,
434                                                    (unsigned char *)data_ptr,
435                                                    data_size);
436                         key->blk_crc = ext2fs_cpu_to_le32(blk_crc);
437                         key->size = ext2fs_cpu_to_le32(data_size);
438                 }
439                 dbg_printf("Writing block %llu to offset %llu size %d key %zu\n",
440                        block_num,
441                        data->undo_blk_num,
442                        sz, data->num_keys - 1);
443                 retval = io_channel_write_blk64(data->undo_file,
444                                         data->undo_blk_num, sz, data_ptr);
445                 if (retval) {
446                         free(read_ptr);
447                         return retval;
448                 }
449                 data->undo_blk_num++;
450                 free(read_ptr);
451
452                 /* Write out the key block */
453                 retval = write_undo_indexes(data, 0);
454                 if (retval)
455                         return retval;
456
457                 /* Next block */
458                 block_num++;
459         }
460
461         return retval;
462 }
463
464 static errcode_t undo_io_read_error(io_channel channel ATTR((unused)),
465                                     unsigned long block ATTR((unused)),
466                                     int count ATTR((unused)),
467                                     void *data ATTR((unused)),
468                                     size_t size ATTR((unused)),
469                                     int actual,
470                                     errcode_t error ATTR((unused)))
471 {
472         actual_size = actual;
473         return error;
474 }
475
476 static void undo_err_handler_init(io_channel channel)
477 {
478         channel->read_error = undo_io_read_error;
479 }
480
481 static int check_filesystem(struct undo_header *hdr, io_channel undo_file,
482                             unsigned int blocksize, blk64_t super_block,
483                             io_channel channel)
484 {
485         struct ext2_super_block super, *sb;
486         char *buf;
487         __u32 sb_crc;
488         errcode_t retval;
489
490         io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
491         retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
492         if (retval)
493                 return retval;
494
495         /*
496          * Compare the FS and the undo file superblock so that we don't
497          * append to something that doesn't match this FS.
498          */
499         retval = ext2fs_get_mem(blocksize, &buf);
500         if (retval)
501                 return retval;
502         retval = io_channel_read_blk64(undo_file, super_block,
503                                        -SUPERBLOCK_SIZE, buf);
504         if (retval)
505                 goto out;
506         sb = (struct ext2_super_block *)buf;
507         sb->s_magic = ~sb->s_magic;
508         if (memcmp(&super, buf, sizeof(super))) {
509                 retval = -1;
510                 goto out;
511         }
512         sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, SUPERBLOCK_SIZE);
513         if (ext2fs_le32_to_cpu(hdr->sb_crc) != sb_crc) {
514                 retval = -1;
515                 goto out;
516         }
517
518 out:
519         ext2fs_free_mem(&buf);
520         return retval;
521 }
522
523 /*
524  * Try to re-open the undo file, so that we can resume where we left off.
525  * That way, the user can pass the same undo file to various programs as
526  * part of an FS upgrade instead of having to create multiple files and
527  * then apply them in correct order.
528  */
529 static errcode_t try_reopen_undo_file(int undo_fd,
530                                       struct undo_private_data *data)
531 {
532         struct undo_header hdr;
533         struct undo_key *dkey;
534         ext2fs_struct_stat statbuf;
535         unsigned int blocksize, fs_blocksize;
536         blk64_t super_block, lblk;
537         size_t num_keys, keys_per_block, i;
538         __u32 hdr_crc, key_crc;
539         errcode_t retval;
540
541         /* Zero size already? */
542         retval = ext2fs_fstat(undo_fd, &statbuf);
543         if (retval)
544                 goto bad_file;
545         if (statbuf.st_size == 0)
546                 goto out;
547
548         /* check the file header */
549         retval = io_channel_read_blk64(data->undo_file, 0, -(int)sizeof(hdr),
550                                        &hdr);
551         if (retval)
552                 goto bad_file;
553
554         if (memcmp(hdr.magic, E2UNDO_MAGIC,
555                     sizeof(hdr.magic)))
556                 goto bad_file;
557         hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&hdr,
558                                    sizeof(struct undo_header) -
559                                    sizeof(__u32));
560         if (ext2fs_le32_to_cpu(hdr.header_crc) != hdr_crc)
561                 goto bad_file;
562         blocksize = ext2fs_le32_to_cpu(hdr.block_size);
563         fs_blocksize = ext2fs_le32_to_cpu(hdr.fs_block_size);
564         if (blocksize > E2UNDO_MAX_BLOCK_SIZE ||
565             blocksize < E2UNDO_MIN_BLOCK_SIZE ||
566             !blocksize || !fs_blocksize)
567                 goto bad_file;
568         super_block = ext2fs_le64_to_cpu(hdr.super_offset);
569         num_keys = ext2fs_le64_to_cpu(hdr.num_keys);
570         io_channel_set_blksize(data->undo_file, blocksize);
571         if (hdr.f_compat || hdr.f_incompat || hdr.f_rocompat)
572                 goto bad_file;
573
574         /* Superblock matches this FS? */
575         if (check_filesystem(&hdr, data->undo_file, blocksize, super_block,
576                              data->real) != 0) {
577                 retval = EXT2_ET_UNDO_FILE_WRONG;
578                 goto out;
579         }
580
581         /* Try to set ourselves up */
582         data->tdb_data_size = blocksize;
583         retval = undo_setup_tdb(data);
584         if (retval)
585                 goto bad_file;
586         data->num_keys = num_keys;
587         data->super_blk_num = super_block;
588         data->first_key_blk = ext2fs_le64_to_cpu(hdr.key_offset);
589
590         /* load the written block map */
591         keys_per_block = KEYS_PER_BLOCK(data);
592         lblk = data->first_key_blk;
593         dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n",
594                    num_keys, keys_per_block, blocksize);
595         for (i = 0; i < num_keys; i += keys_per_block) {
596                 size_t j, max_j;
597                 __le32 crc;
598
599                 data->key_blk_num = lblk;
600                 retval = io_channel_read_blk64(data->undo_file,
601                                                lblk, 1, data->keyb);
602                 if (retval)
603                         goto bad_key_replay;
604
605                 /* check keys */
606                 if (ext2fs_le32_to_cpu(data->keyb->magic) != KEYBLOCK_MAGIC) {
607                         retval = EXT2_ET_UNDO_FILE_CORRUPT;
608                         goto bad_key_replay;
609                 }
610                 crc = data->keyb->crc;
611                 data->keyb->crc = 0;
612                 key_crc = ext2fs_crc32c_le(~0, (unsigned char *)data->keyb,
613                                            blocksize);
614                 if (ext2fs_le32_to_cpu(crc) != key_crc) {
615                         retval = EXT2_ET_UNDO_FILE_CORRUPT;
616                         goto bad_key_replay;
617                 }
618
619                 /* load keys from key block */
620                 lblk++;
621                 max_j = data->num_keys - i;
622                 if (max_j > keys_per_block)
623                         max_j = keys_per_block;
624                 for (j = 0, dkey = data->keyb->keys;
625                      j < max_j;
626                      j++, dkey++) {
627                         blk64_t fsblk = ext2fs_le64_to_cpu(dkey->fsblk);
628                         blk64_t undo_blk = fsblk * fs_blocksize / blocksize;
629                         size_t size = ext2fs_le32_to_cpu(dkey->size);
630
631                         ext2fs_mark_block_bitmap_range2(data->written_block_map,
632                                          undo_blk,
633                                         (size + blocksize - 1) / blocksize);
634                         lblk += (size + blocksize - 1) / blocksize;
635                         data->undo_blk_num = lblk;
636                         data->keys_in_block = j + 1;
637                 }
638         }
639         dbg_printf("Reopen undo, keyblk=%llu undoblk=%llu nrkeys=%zu kib=%zu\n",
640                    data->key_blk_num, data->undo_blk_num, data->num_keys,
641                    data->keys_in_block);
642
643         data->hdr.state = hdr.state & ~E2UNDO_STATE_FINISHED;
644         data->hdr.f_compat = hdr.f_compat;
645         data->hdr.f_incompat = hdr.f_incompat;
646         data->hdr.f_rocompat = hdr.f_rocompat;
647         return retval;
648
649 bad_key_replay:
650         data->key_blk_num = data->undo_blk_num = 0;
651         data->keys_in_block = 0;
652         ext2fs_free_mem(&data->keyb);
653         ext2fs_free_generic_bitmap(data->written_block_map);
654         data->tdb_written = 0;
655         goto out;
656 bad_file:
657         retval = EXT2_ET_UNDO_FILE_CORRUPT;
658 out:
659         return retval;
660 }
661
662 static void undo_atexit(void *p)
663 {
664         struct undo_private_data *data = p;
665         errcode_t err;
666
667         err = write_undo_indexes(data, 1);
668         io_channel_close(data->undo_file);
669
670         com_err(data->tdb_file, err, "while force-closing undo file");
671 }
672
673 static errcode_t undo_open(const char *name, int flags, io_channel *channel)
674 {
675         io_channel      io = NULL;
676         struct undo_private_data *data = NULL;
677         int             undo_fd = -1;
678         errcode_t       retval;
679
680         if (name == 0)
681                 return EXT2_ET_BAD_DEVICE_NAME;
682         retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
683         if (retval)
684                 goto cleanup;
685         memset(io, 0, sizeof(struct struct_io_channel));
686         io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
687         retval = ext2fs_get_mem(sizeof(struct undo_private_data), &data);
688         if (retval)
689                 goto cleanup;
690
691         io->manager = undo_io_manager;
692         retval = ext2fs_get_mem(strlen(name)+1, &io->name);
693         if (retval)
694                 goto cleanup;
695
696         strcpy(io->name, name);
697         io->private_data = data;
698         io->block_size = 1024;
699         io->read_error = 0;
700         io->write_error = 0;
701         io->refcount = 1;
702
703         memset(data, 0, sizeof(struct undo_private_data));
704         data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
705         data->super_blk_num = 1;
706         data->first_key_blk = 2;
707         data->undo_blk_num = 3;
708
709         if (undo_io_backing_manager) {
710                 retval = undo_io_backing_manager->open(name, flags,
711                                                        &data->real);
712                 if (retval)
713                         goto cleanup;
714
715                 data->tdb_file = strdup(tdb_file);
716                 if (data->tdb_file == NULL)
717                         goto cleanup;
718                 undo_fd = ext2fs_open_file(data->tdb_file, O_RDWR | O_CREAT,
719                                            0600);
720                 if (undo_fd < 0)
721                         goto cleanup;
722
723                 retval = undo_io_backing_manager->open(data->tdb_file,
724                                                        IO_FLAG_RW,
725                                                        &data->undo_file);
726                 if (retval)
727                         goto cleanup;
728         } else {
729                 data->real = NULL;
730                 data->undo_file = NULL;
731         }
732
733         if (data->real)
734                 io->flags = (io->flags & ~CHANNEL_FLAGS_DISCARD_ZEROES) |
735                             (data->real->flags & CHANNEL_FLAGS_DISCARD_ZEROES);
736
737         /*
738          * setup err handler for read so that we know
739          * when the backing manager fails do short read
740          */
741         if (data->real)
742                 undo_err_handler_init(data->real);
743
744         if (data->undo_file) {
745                 retval = try_reopen_undo_file(undo_fd, data);
746                 if (retval)
747                         goto cleanup;
748         }
749         retval = ext2fs_add_exit_fn(undo_atexit, data);
750         if (retval)
751                 goto cleanup;
752
753         *channel = io;
754         if (undo_fd >= 0)
755                 close(undo_fd);
756         return retval;
757
758 cleanup:
759         ext2fs_remove_exit_fn(undo_atexit, data);
760         if (undo_fd >= 0)
761                 close(undo_fd);
762         if (data && data->undo_file)
763                 io_channel_close(data->undo_file);
764         if (data && data->tdb_file)
765                 free(data->tdb_file);
766         if (data && data->real)
767                 io_channel_close(data->real);
768         if (data)
769                 ext2fs_free_mem(&data);
770         if (io)
771                 ext2fs_free_mem(&io);
772         return retval;
773 }
774
775 static errcode_t undo_close(io_channel channel)
776 {
777         struct undo_private_data *data;
778         errcode_t       err, retval = 0;
779
780         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
781         data = (struct undo_private_data *) channel->private_data;
782         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
783
784         if (--channel->refcount > 0)
785                 return 0;
786         /* Before closing write the file system identity */
787         if (!getenv("UNDO_IO_SIMULATE_UNFINISHED"))
788                 data->hdr.state = ext2fs_cpu_to_le32(E2UNDO_STATE_FINISHED);
789         err = write_undo_indexes(data, 1);
790         ext2fs_remove_exit_fn(undo_atexit, data);
791         if (data->real)
792                 retval = io_channel_close(data->real);
793         if (data->tdb_file)
794                 free(data->tdb_file);
795         if (data->undo_file)
796                 io_channel_close(data->undo_file);
797         ext2fs_free_mem(&data->keyb);
798         if (data->written_block_map)
799                 ext2fs_free_generic_bitmap(data->written_block_map);
800         ext2fs_free_mem(&channel->private_data);
801         if (channel->name)
802                 ext2fs_free_mem(&channel->name);
803         ext2fs_free_mem(&channel);
804
805         if (err)
806                 return err;
807         return retval;
808 }
809
810 static errcode_t undo_set_blksize(io_channel channel, int blksize)
811 {
812         struct undo_private_data *data;
813         errcode_t               retval = 0;
814
815         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
816         data = (struct undo_private_data *) channel->private_data;
817         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
818
819         if (blksize > E2UNDO_MAX_BLOCK_SIZE || blksize < E2UNDO_MIN_BLOCK_SIZE)
820                 return EXT2_ET_INVALID_ARGUMENT;
821
822         if (data->real)
823                 retval = io_channel_set_blksize(data->real, blksize);
824         /*
825          * Set the block size used for tdb
826          */
827         if (!data->tdb_data_size || !data->tdb_written)
828                 data->tdb_data_size = blksize;
829         channel->block_size = blksize;
830         return retval;
831 }
832
833 static errcode_t undo_read_blk64(io_channel channel, unsigned long long block,
834                                int count, void *buf)
835 {
836         errcode_t       retval = 0;
837         struct undo_private_data *data;
838
839         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
840         data = (struct undo_private_data *) channel->private_data;
841         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
842
843         if (data->real)
844                 retval = io_channel_read_blk64(data->real, block, count, buf);
845
846         return retval;
847 }
848
849 static errcode_t undo_read_blk(io_channel channel, unsigned long block,
850                                int count, void *buf)
851 {
852         return undo_read_blk64(channel, block, count, buf);
853 }
854
855 static errcode_t undo_write_blk64(io_channel channel, unsigned long long block,
856                                 int count, const void *buf)
857 {
858         struct undo_private_data *data;
859         errcode_t       retval = 0;
860
861         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
862         data = (struct undo_private_data *) channel->private_data;
863         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
864         /*
865          * First write the existing content into database
866          */
867         retval = undo_write_tdb(channel, block, count);
868         if (retval)
869                  return retval;
870         if (data->real)
871                 retval = io_channel_write_blk64(data->real, block, count, buf);
872
873         return retval;
874 }
875
876 static errcode_t undo_write_blk(io_channel channel, unsigned long block,
877                                 int count, const void *buf)
878 {
879         return undo_write_blk64(channel, block, count, buf);
880 }
881
882 static errcode_t undo_write_byte(io_channel channel, unsigned long offset,
883                                  int size, const void *buf)
884 {
885         struct undo_private_data *data;
886         errcode_t       retval = 0;
887         ext2_loff_t     location;
888         unsigned long blk_num, count;;
889
890         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
891         data = (struct undo_private_data *) channel->private_data;
892         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
893
894         location = offset + data->offset;
895         blk_num = location/channel->block_size;
896         /*
897          * the size specified may spread across multiple blocks
898          * also make sure we account for the fact that block start
899          * offset for tdb is different from the backing I/O manager
900          * due to possible different block size
901          */
902         count = (size + (location % channel->block_size) +
903                         channel->block_size  -1)/channel->block_size;
904         retval = undo_write_tdb(channel, blk_num, count);
905         if (retval)
906                 return retval;
907         if (data->real && data->real->manager->write_byte)
908                 retval = io_channel_write_byte(data->real, offset, size, buf);
909
910         return retval;
911 }
912
913 static errcode_t undo_discard(io_channel channel, unsigned long long block,
914                               unsigned long long count)
915 {
916         struct undo_private_data *data;
917         errcode_t       retval = 0;
918         int icount;
919
920         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
921         data = (struct undo_private_data *) channel->private_data;
922         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
923
924         if (count > INT_MAX)
925                 return EXT2_ET_UNIMPLEMENTED;
926         icount = count;
927
928         /*
929          * First write the existing content into database
930          */
931         retval = undo_write_tdb(channel, block, icount);
932         if (retval)
933                 return retval;
934         if (data->real)
935                 retval = io_channel_discard(data->real, block, count);
936
937         return retval;
938 }
939
940 static errcode_t undo_zeroout(io_channel channel, unsigned long long block,
941                               unsigned long long count)
942 {
943         struct undo_private_data *data;
944         errcode_t       retval = 0;
945         int icount;
946
947         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
948         data = (struct undo_private_data *) channel->private_data;
949         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
950
951         if (count > INT_MAX)
952                 return EXT2_ET_UNIMPLEMENTED;
953         icount = count;
954
955         /*
956          * First write the existing content into database
957          */
958         retval = undo_write_tdb(channel, block, icount);
959         if (retval)
960                 return retval;
961         if (data->real)
962                 retval = io_channel_zeroout(data->real, block, count);
963
964         return retval;
965 }
966
967 static errcode_t undo_cache_readahead(io_channel channel,
968                                       unsigned long long block,
969                                       unsigned long long count)
970 {
971         struct undo_private_data *data;
972         errcode_t       retval = 0;
973
974         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
975         data = (struct undo_private_data *) channel->private_data;
976         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
977
978         if (data->real)
979                 retval = io_channel_cache_readahead(data->real, block, count);
980
981         return retval;
982 }
983
984 /*
985  * Flush data buffers to disk.
986  */
987 static errcode_t undo_flush(io_channel channel)
988 {
989         errcode_t       retval = 0;
990         struct undo_private_data *data;
991
992         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
993         data = (struct undo_private_data *) channel->private_data;
994         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
995
996         if (data->real)
997                 retval = io_channel_flush(data->real);
998
999         return retval;
1000 }
1001
1002 static errcode_t undo_set_option(io_channel channel, const char *option,
1003                                  const char *arg)
1004 {
1005         errcode_t       retval = 0;
1006         struct undo_private_data *data;
1007         unsigned long tmp;
1008         char *end;
1009
1010         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1011         data = (struct undo_private_data *) channel->private_data;
1012         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1013
1014         if (!strcmp(option, "tdb_data_size")) {
1015                 if (!arg)
1016                         return EXT2_ET_INVALID_ARGUMENT;
1017
1018                 tmp = strtoul(arg, &end, 0);
1019                 if (*end)
1020                         return EXT2_ET_INVALID_ARGUMENT;
1021                 if (tmp > E2UNDO_MAX_BLOCK_SIZE || tmp < E2UNDO_MIN_BLOCK_SIZE)
1022                         return EXT2_ET_INVALID_ARGUMENT;
1023                 if (!data->tdb_data_size || !data->tdb_written) {
1024                         data->tdb_written = -1;
1025                         data->tdb_data_size = tmp;
1026                 }
1027                 return 0;
1028         }
1029         /*
1030          * Need to support offset option to work with
1031          * Unix I/O manager
1032          */
1033         if (data->real && data->real->manager->set_option) {
1034                 retval = data->real->manager->set_option(data->real,
1035                                                         option, arg);
1036         }
1037         if (!retval && !strcmp(option, "offset")) {
1038                 if (!arg)
1039                         return EXT2_ET_INVALID_ARGUMENT;
1040
1041                 tmp = strtoul(arg, &end, 0);
1042                 if (*end)
1043                         return EXT2_ET_INVALID_ARGUMENT;
1044                 data->offset = tmp;
1045         }
1046         return retval;
1047 }
1048
1049 static errcode_t undo_get_stats(io_channel channel, io_stats *stats)
1050 {
1051         errcode_t       retval = 0;
1052         struct undo_private_data *data;
1053
1054         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1055         data = (struct undo_private_data *) channel->private_data;
1056         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1057
1058         if (data->real)
1059                 retval = (data->real->manager->get_stats)(data->real, stats);
1060
1061         return retval;
1062 }
1063
1064 static struct struct_io_manager struct_undo_manager = {
1065         .magic          = EXT2_ET_MAGIC_IO_MANAGER,
1066         .name           = "Undo I/O Manager",
1067         .open           = undo_open,
1068         .close          = undo_close,
1069         .set_blksize    = undo_set_blksize,
1070         .read_blk       = undo_read_blk,
1071         .write_blk      = undo_write_blk,
1072         .flush          = undo_flush,
1073         .write_byte     = undo_write_byte,
1074         .set_option     = undo_set_option,
1075         .get_stats      = undo_get_stats,
1076         .read_blk64     = undo_read_blk64,
1077         .write_blk64    = undo_write_blk64,
1078         .discard        = undo_discard,
1079         .zeroout        = undo_zeroout,
1080         .cache_readahead        = undo_cache_readahead,
1081 };
1082
1083 io_manager undo_io_manager = &struct_undo_manager;