1 /*
2     geom_dal.c -- parted device abstraction layer
3     Copyright (C) 2001, 2002, 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 #if (DYNAMIC_LOADING || HAVE_LIBREISERFS) && !DISCOVER_ONLY
22 
23 #include "geom_dal.h"
24 
25 #include <parted/parted.h>
26 #include <parted/debug.h>
27 
__len(dal_t * dal)28 static blk_t __len(dal_t *dal) {
29     PED_ASSERT(dal != NULL, return 0);
30 
31     return ((PedGeometry *)dal->dev)->length /
32 	(dal->block_size / PED_SECTOR_SIZE_DEFAULT);
33 }
34 
__read(dal_t * dal,void * buff,blk_t block,blk_t count)35 static int __read(dal_t *dal, void *buff, blk_t block, blk_t count) {
36     blk_t k;
37     PedSector block_pos;
38     PedSector block_count;
39 
40     PED_ASSERT(dal != NULL, return 0);
41 
42     k = dal->block_size / PED_SECTOR_SIZE_DEFAULT;
43     block_pos = (PedSector)(block * k);
44     block_count = (PedSector)(count * k);
45 
46     return ped_geometry_read((PedGeometry *)dal->dev, buff, block_pos, block_count);
47 }
48 
__write(dal_t * dal,void * buff,blk_t block,blk_t count)49 static int __write(dal_t *dal, void *buff, blk_t block, blk_t count) {
50     blk_t k;
51     PedSector block_pos;
52     PedSector block_count;
53 
54     PED_ASSERT(dal != NULL, return 0);
55 
56     k = dal->block_size / PED_SECTOR_SIZE_DEFAULT;
57     block_pos = (PedSector)(block * k);
58     block_count = (PedSector)(count * k);
59 
60     return ped_geometry_write((PedGeometry *)dal->dev, buff, block_pos,
61 	block_count);
62 }
63 
__sync(dal_t * dal)64 static int __sync(dal_t *dal) {
65     PED_ASSERT(dal != NULL, return 0);
66     return ped_geometry_sync((PedGeometry *)dal->dev);
67 }
68 
__flags(dal_t * dal)69 static int __flags(dal_t *dal) {
70     PED_ASSERT(dal != NULL, return 0);
71     return dal->flags;
72 }
73 
__equals(dal_t * dal1,dal_t * dal2)74 static int __equals(dal_t *dal1, dal_t *dal2) {
75     PED_ASSERT(dal1 != NULL, return 0);
76     PED_ASSERT(dal2 != NULL, return 0);
77 
78     return ped_geometry_test_equal((PedGeometry *)dal1->dev,
79 	(PedGeometry *)dal2->dev);
80 }
81 
__stat(dal_t * dal,struct stat * st)82 static int __stat(dal_t *dal, struct stat *st) {
83 
84     PED_ASSERT(dal != NULL, return 0);
85     PED_ASSERT(st != NULL, return 0);
86 
87     if (stat(((PedGeometry *)dal->dev)->dev->path, st))
88 	return 0;
89 
90     return 1;
91 }
92 
__dev(dal_t * dal)93 static dev_t __dev(dal_t *dal) {
94     struct stat st;
95 
96     if (!__stat(dal, &st))
97 	return (dev_t)0;
98 
99     return st.st_dev;
100 }
101 
102 static struct dal_ops ops = {
103     __len, __read, __write, __sync,
104     __flags, __equals, __stat, __dev
105 };
106 
geom_dal_create(PedGeometry * geom,size_t block_size,int flags)107 dal_t *geom_dal_create(PedGeometry *geom, size_t block_size, int flags) {
108     dal_t *dal;
109 
110     if (!geom)
111 	return NULL;
112 
113     if (!(dal = ped_malloc(sizeof(dal_t))))
114 	return NULL;
115 
116     dal->ops = &ops;
117     dal->dev = geom;
118     dal->block_size = block_size;
119     dal->flags = flags;
120     dal->len = 0;
121 
122     return dal;
123 }
124 
geom_dal_reopen(dal_t * dal,int flags)125 int geom_dal_reopen(dal_t *dal, int flags) {
126 
127     if (!dal) return 0;
128     dal->flags = flags;
129 
130     return 1;
131 }
132 
geom_dal_free(dal_t * dal)133 void geom_dal_free(dal_t *dal) {
134     PED_ASSERT(dal != NULL, return);
135     ped_free(dal);
136 }
137 
138 #endif
139