source: mainline/uspace/lib/ext2/libext2_superblock.c@ 8bd5dad

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

Initialize libext2 in ext2info & fixed some bugs in libext2

  • Property mode set to 100644
File size: 4.3 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/** Read a superblock directly from device (i.e. no libblock cache)
134 *
135 * @param devmap_handle Device handle of the block device.
136 * @param superblock Pointer where to store pointer to new superblock
137 *
138 * @return EOK on success or negative error code on failure.
139 */
140int ext2_superblock_read_direct(devmap_handle_t devmap_handle,
141 ext2_superblock_t **superblock)
142{
143 void *data;
144 int rc;
145
146 data = malloc(EXT2_SUPERBLOCK_SIZE);
147 if (data == NULL) {
148 return ENOMEM;
149 }
150
151 rc = block_read_bytes_direct(devmap_handle, EXT2_SUPERBLOCK_OFFSET,
152 EXT2_SUPERBLOCK_SIZE, data);
153 if (rc != EOK) {
154 free(data);
155 return rc;
156 }
157
158 (*superblock) = data;
159 return EOK;
160}
161
162
163/** @}
164 */
Note: See TracBrowser for help on using the repository browser.