OSDN Git Service

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