1 /* 2 parted_io.c -- parted I/O code interface for libext2resize 3 Copyright (C) 1998-2000, 2007 Free Software Foundation, Inc. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include <config.h> 20 21 #ifndef DISCOVER_ONLY 22 23 #include <parted/parted.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include "ext2.h" 27 28 /* pseudo-header.... */ 29 30 loff_t llseek(unsigned int fd, loff_t offset, unsigned int whence); 31 32 struct my_cookie 33 { 34 int logsize; 35 PedGeometry* geom; 36 }; 37 38 /* ...then this must be pseudo-code :-) */ 39 40 static int do_close (void *cookie); 41 static int do_sync (void *cookie); 42 static blk_t do_get_size (void *cookie); 43 static int do_read (void *cookie, void *ptr, blk_t block, blk_t numblocks); 44 static int do_set_blocksize(void *cookie, int logsize); 45 static int do_write (void *cookie, void *ptr, blk_t block, blk_t numblocks); 46 47 struct ext2_dev_ops ops = 48 { 49 .close = do_close, 50 .get_size = do_get_size, 51 .read = do_read, 52 .set_blocksize = do_set_blocksize, 53 .sync = do_sync, 54 .write = do_write 55 }; 56 57 58 59 static int do_close(void *cookie) 60 { 61 struct my_cookie *monster = cookie; 62 63 return ped_geometry_sync(monster->geom); 64 } 65 66 static int do_sync(void *cookie) 67 { 68 struct my_cookie *monster = cookie; 69 70 return ped_geometry_sync(monster->geom); 71 } 72 73 static blk_t do_get_size(void *cookie) 74 { 75 struct my_cookie *monster = cookie; 76 77 return monster->geom->length >> (monster->logsize - 9); 78 } 79 80 static int do_read(void *cookie, void *ptr, blk_t block, blk_t num) 81 { 82 struct my_cookie *monster = cookie; 83 84 return ped_geometry_read(monster->geom, ptr, block << (monster->logsize - 9), num << (monster->logsize - 9)); 85 } 86 87 static int do_set_blocksize(void *cookie, int logsize) 88 { 89 struct my_cookie *monster = cookie; 90 91 monster->logsize = logsize; 92 return 1; 93 } 94 95 static int do_write(void *cookie, void *ptr, blk_t block, blk_t num) 96 { 97 struct my_cookie *monster = cookie; 98 99 return ped_geometry_write(monster->geom, ptr, 100 block << (monster->logsize - 9), 101 num << (monster->logsize - 9)); 102 } 103 104 105 struct ext2_dev_handle *ext2_make_dev_handle_from_parted_geometry(PedGeometry* geom) 106 { 107 struct ext2_dev_handle *dh; 108 struct my_cookie *monster; 109 110 if ((dh = ped_malloc(sizeof(struct ext2_dev_handle))) == NULL) 111 goto error; 112 113 if ((monster = ped_malloc(sizeof(struct my_cookie))) == NULL) 114 goto error_free_dh; 115 116 dh->ops = &ops; 117 dh->cookie = monster; 118 monster->logsize = 9; 119 monster->geom = geom; 120 121 return dh; 122 123 error_free_dh: 124 ped_free(dh); 125 error: 126 return NULL; 127 } 128 129 void ext2_destroy_dev_handle(struct ext2_dev_handle *handle) 130 { 131 ped_geometry_destroy(((struct my_cookie *)handle->cookie)->geom); 132 ped_free(handle->cookie); 133 ped_free(handle); 134 } 135 #endif /* !DISCOVER_ONLY */