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