source: mainline/uspace/lib/ext2/libext2_superblock.c@ f6fa2c2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f6fa2c2 was f6fa2c2, checked in by Martin Sucha <sucha14@…>, 14 years ago

Added read support and code in ext2info for more superblock entries

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Sucha
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libext2
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include "libext2.h"
37#include <errno.h>
38#include <malloc.h>
39#include <libblock.h>
40
41/**
42 * Return a magic number from ext2 superblock, this should be equal to
43 * EXT_SUPERBLOCK_MAGIC for valid ext2 superblock
44 *
45 * @param sb pointer to superblock
46 */
47inline uint16_t ext2_superblock_get_magic(ext2_superblock_t *sb)
48{
49 return uint16_t_le2host(sb->magic);
50}
51
52/**
53 * Get the position of first ext2 data block (i.e. the block number
54 * containing main superblock)
55 *
56 * @param sb pointer to superblock
57 */
58inline uint32_t ext2_superblock_get_first_block(ext2_superblock_t *sb)
59{
60 return uint32_t_le2host(sb->first_block);
61}
62
63/**
64 * Get the number of bits to shift a value of 1024 to the left necessary
65 * to get the size of a block
66 *
67 * @param sb pointer to superblock
68 */
69inline uint32_t ext2_superblock_get_block_size_log2(ext2_superblock_t *sb)
70{
71 return uint32_t_le2host(sb->block_size_log2);
72}
73
74/**
75 * Get the size of a block, in bytes
76 *
77 * @param sb pointer to superblock
78 */
79inline uint32_t ext2_superblock_get_block_size(ext2_superblock_t *sb)
80{
81 return 1024 << ext2_superblock_get_block_size_log2(sb);
82}
83
84/**
85 * Get the number of bits to shift a value of 1024 to the left necessary
86 * to get the size of a fragment (note that this is a signed integer and
87 * if negative, the value should be shifted to the right instead)
88 *
89 * @param sb pointer to superblock
90 */
91inline int32_t ext2_superblock_get_fragment_size_log2(ext2_superblock_t *sb)
92{
93 return uint32_t_le2host(sb->fragment_size_log2);
94}
95
96/**
97 * Get the size of a fragment, in bytes
98 *
99 * @param sb pointer to superblock
100 */
101inline uint32_t ext2_superblock_get_fragment_size(ext2_superblock_t *sb)
102{
103 int32_t log = ext2_superblock_get_fragment_size_log2(sb);
104 if (log >= 0) {
105 return 1024 << log;
106 }
107 else {
108 return 1024 >> -log;
109 }
110}
111
112/**
113 * Get number of blocks per block group
114 *
115 * @param sb pointer to superblock
116 */
117inline uint32_t ext2_superblock_get_blocks_per_group(ext2_superblock_t *sb)
118{
119 return uint32_t_le2host(sb->blocks_per_group);
120}
121
122/**
123 * Get number of fragments per block group
124 *
125 * @param sb pointer to superblock
126 */
127inline uint32_t ext2_superblock_get_fragments_per_group(ext2_superblock_t *sb)
128{
129 return uint32_t_le2host(sb->fragments_per_group);
130}
131
132/**
133 * Get filesystem state
134 *
135 * @param sb pointer to superblock
136 */
137inline uint16_t ext2_superblock_get_state(ext2_superblock_t *sb)
138{
139 return uint16_t_le2host(sb->state);
140}
141
142/**
143 * Get minor revision number
144 *
145 * @param sb pointer to superblock
146 */
147inline uint16_t ext2_superblock_get_rev_minor(ext2_superblock_t *sb)
148{
149 return uint16_t_le2host(sb->rev_minor);
150}
151
152/**
153 * Get major revision number
154 *
155 * @param sb pointer to superblock
156 */
157inline uint32_t ext2_superblock_get_rev_major(ext2_superblock_t *sb)
158{
159 return uint32_t_le2host(sb->rev_major);
160}
161
162/**
163 * Get index of first regular inode
164 *
165 * @param sb pointer to superblock
166 */
167inline uint32_t ext2_superblock_get_first_inode(ext2_superblock_t *sb)
168{
169 if (ext2_superblock_get_rev_major(sb) == 0) {
170 return EXT2_REV0_FIRST_INODE;
171 }
172 return uint32_t_le2host(sb->first_inode);
173}
174
175/**
176 * Get size of inode
177 *
178 * @param sb pointer to superblock
179 */
180inline uint16_t ext2_superblock_get_inode_size(ext2_superblock_t *sb)
181{
182 if (ext2_superblock_get_rev_major(sb) == 0) {
183 return EXT2_REV0_INODE_SIZE;
184 }
185 return uint32_t_le2host(sb->inode_size);
186}
187
188/**
189 * Get total inode count
190 *
191 * @param sb pointer to superblock
192 */
193inline uint32_t ext2_superblock_get_total_inode_count(ext2_superblock_t *sb)
194{
195 return uint32_t_le2host(sb->total_inode_count);
196}
197
198/**
199 * Get total block count
200 *
201 * @param sb pointer to superblock
202 */
203inline uint32_t ext2_superblock_get_total_block_count(ext2_superblock_t *sb)
204{
205 return uint32_t_le2host(sb->total_block_count);
206}
207
208/**
209 * Get amount of blocks reserved for the superuser
210 *
211 * @param sb pointer to superblock
212 */
213inline uint32_t ext2_superblock_get_reserved_block_count(ext2_superblock_t *sb)
214{
215 return uint32_t_le2host(sb->reserved_block_count);
216}
217
218/**
219 * Get amount of free blocks
220 *
221 * @param sb pointer to superblock
222 */
223inline uint32_t ext2_superblock_get_free_block_count(ext2_superblock_t *sb)
224{
225 return uint32_t_le2host(sb->free_block_count);
226}
227
228/**
229 * Get amount of free inodes
230 *
231 * @param sb pointer to superblock
232 */
233inline uint32_t ext2_superblock_get_free_inode_count(ext2_superblock_t *sb)
234{
235 return uint32_t_le2host(sb->free_inode_count);
236}
237
238/**
239 * Get id of operating system that created the filesystem
240 *
241 * @param sb pointer to superblock
242 */
243inline uint32_t ext2_superblock_get_os(ext2_superblock_t *sb)
244{
245 return uint32_t_le2host(sb->os);
246}
247
248/** Read a superblock directly from device (i.e. no libblock cache)
249 *
250 * @param devmap_handle Device handle of the block device.
251 * @param superblock Pointer where to store pointer to new superblock
252 *
253 * @return EOK on success or negative error code on failure.
254 */
255int ext2_superblock_read_direct(devmap_handle_t devmap_handle,
256 ext2_superblock_t **superblock)
257{
258 void *data;
259 int rc;
260
261 data = malloc(EXT2_SUPERBLOCK_SIZE);
262 if (data == NULL) {
263 return ENOMEM;
264 }
265
266 rc = block_read_bytes_direct(devmap_handle, EXT2_SUPERBLOCK_OFFSET,
267 EXT2_SUPERBLOCK_SIZE, data);
268 if (rc != EOK) {
269 free(data);
270 return rc;
271 }
272
273 (*superblock) = data;
274 return EOK;
275}
276
277
278/** @}
279 */
Note: See TracBrowser for help on using the repository browser.