chiark / gitweb /
a7bd091c5e61f5f0ef67f3056965f37c581d1da5
[elogind.git] / extras / volume_id / volume_id / ufs.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "logging.h"
28 #include "util.h"
29 #include "ufs.h"
30
31 struct ufs_super_block {
32         uint32_t        fs_link;
33         uint32_t        fs_rlink;
34         uint32_t        fs_sblkno;
35         uint32_t        fs_cblkno;
36         uint32_t        fs_iblkno;
37         uint32_t        fs_dblkno;
38         uint32_t        fs_cgoffset;
39         uint32_t        fs_cgmask;
40         uint32_t        fs_time;
41         uint32_t        fs_size;
42         uint32_t        fs_dsize;
43         uint32_t        fs_ncg; 
44         uint32_t        fs_bsize;
45         uint32_t        fs_fsize;
46         uint32_t        fs_frag;
47         uint32_t        fs_minfree;
48         uint32_t        fs_rotdelay;
49         uint32_t        fs_rps; 
50         uint32_t        fs_bmask;
51         uint32_t        fs_fmask;
52         uint32_t        fs_bshift;
53         uint32_t        fs_fshift;
54         uint32_t        fs_maxcontig;
55         uint32_t        fs_maxbpg;
56         uint32_t        fs_fragshift;
57         uint32_t        fs_fsbtodb;
58         uint32_t        fs_sbsize;
59         uint32_t        fs_csmask;
60         uint32_t        fs_csshift;
61         uint32_t        fs_nindir;
62         uint32_t        fs_inopb;
63         uint32_t        fs_nspf;
64         uint32_t        fs_optim;
65         uint32_t        fs_npsect_state;
66         uint32_t        fs_interleave;
67         uint32_t        fs_trackskew;
68         uint32_t        fs_id[2];
69         uint32_t        fs_csaddr;
70         uint32_t        fs_cssize;
71         uint32_t        fs_cgsize;
72         uint32_t        fs_ntrak;
73         uint32_t        fs_nsect;
74         uint32_t        fs_spc; 
75         uint32_t        fs_ncyl;
76         uint32_t        fs_cpg;
77         uint32_t        fs_ipg;
78         uint32_t        fs_fpg;
79         struct ufs_csum {
80                 uint32_t        cs_ndir;
81                 uint32_t        cs_nbfree;
82                 uint32_t        cs_nifree;
83                 uint32_t        cs_nffree;
84         } __attribute__((__packed__)) fs_cstotal;
85         int8_t          fs_fmod;
86         int8_t          fs_clean;
87         int8_t          fs_ronly;
88         int8_t          fs_flags;
89         union {
90                 struct {
91                         int8_t  fs_fsmnt[512];
92                         uint32_t        fs_cgrotor;
93                         uint32_t        fs_csp[31];
94                         uint32_t        fs_maxcluster;
95                         uint32_t        fs_cpc;
96                         uint16_t        fs_opostbl[16][8];
97                 } __attribute__((__packed__)) fs_u1;
98                 struct {
99                         int8_t          fs_fsmnt[468];
100                         uint8_t         fs_volname[32];
101                         uint64_t        fs_swuid;
102                         int32_t         fs_pad;
103                         uint32_t        fs_cgrotor;
104                         uint32_t        fs_ocsp[28];
105                         uint32_t        fs_contigdirs;
106                         uint32_t        fs_csp; 
107                         uint32_t        fs_maxcluster;
108                         uint32_t        fs_active;
109                         int32_t         fs_old_cpc;
110                         int32_t         fs_maxbsize;
111                         int64_t         fs_sparecon64[17];
112                         int64_t         fs_sblockloc;
113                         struct ufs2_csum_total {
114                                 uint64_t        cs_ndir;
115                                 uint64_t        cs_nbfree;
116                                 uint64_t        cs_nifree;
117                                 uint64_t        cs_nffree;
118                                 uint64_t        cs_numclusters;
119                                 uint64_t        cs_spare[3];
120                         } __attribute__((__packed__)) fs_cstotal;
121                         struct ufs_timeval {
122                                 int32_t         tv_sec;
123                                 int32_t         tv_usec;
124                         } __attribute__((__packed__)) fs_time;
125                         int64_t         fs_size;
126                         int64_t         fs_dsize;
127                         uint64_t        fs_csaddr;
128                         int64_t         fs_pendingblocks;
129                         int32_t         fs_pendinginodes;
130                 } __attribute__((__packed__)) fs_u2;
131         }  fs_u11;
132         union {
133                 struct {
134                         int32_t         fs_sparecon[53];
135                         int32_t         fs_reclaim;
136                         int32_t         fs_sparecon2[1];
137                         int32_t         fs_state;
138                         uint32_t        fs_qbmask[2];
139                         uint32_t        fs_qfmask[2];
140                 } __attribute__((__packed__)) fs_sun;
141                 struct {
142                         int32_t         fs_sparecon[53];
143                         int32_t         fs_reclaim;
144                         int32_t         fs_sparecon2[1];
145                         uint32_t        fs_npsect;
146                         uint32_t        fs_qbmask[2];
147                         uint32_t        fs_qfmask[2];
148                 } __attribute__((__packed__)) fs_sunx86;
149                 struct {
150                         int32_t         fs_sparecon[50];
151                         int32_t         fs_contigsumsize;
152                         int32_t         fs_maxsymlinklen;
153                         int32_t         fs_inodefmt;
154                         uint32_t        fs_maxfilesize[2];
155                         uint32_t        fs_qbmask[2];
156                         uint32_t        fs_qfmask[2];
157                         int32_t         fs_state;
158                 } __attribute__((__packed__)) fs_44;
159         } fs_u2;
160         int32_t         fs_postblformat;
161         int32_t         fs_nrpos;
162         int32_t         fs_postbloff;
163         int32_t         fs_rotbloff;
164         uint32_t        fs_magic;
165         uint8_t         fs_space[1];
166 } __attribute__((__packed__));
167
168 #define UFS_MAGIC                       0x00011954
169 #define UFS2_MAGIC                      0x19540119
170 #define UFS_MAGIC_FEA                   0x00195612
171 #define UFS_MAGIC_LFN                   0x00095014
172
173 int volume_id_probe_ufs(struct volume_id *id, uint64_t off)
174 {
175         uint32_t magic;
176         int i;
177         struct ufs_super_block *ufs;
178         int offsets[] = {0, 8, 64, 256, -1};
179
180         dbg("probing at offset 0x%llx", (unsigned long long) off);
181
182         for (i = 0; offsets[i] >= 0; i++) {     
183                 ufs = (struct ufs_super_block *) volume_id_get_buffer(id, off + (offsets[i] * 0x400), 0x800);
184                 if (ufs == NULL)
185                         return -1;
186
187                 dbg("offset 0x%x", offsets[i] * 0x400);
188                 magic = be32_to_cpu(ufs->fs_magic);
189                 if ((magic == UFS_MAGIC) ||
190                     (magic == UFS2_MAGIC) ||
191                     (magic == UFS_MAGIC_FEA) ||
192                     (magic == UFS_MAGIC_LFN)) {
193                         dbg("magic 0x%08x(be)", magic);
194                         goto found;
195                 }
196                 magic = le32_to_cpu(ufs->fs_magic);
197                 if ((magic == UFS_MAGIC) ||
198                     (magic == UFS2_MAGIC) ||
199                     (magic == UFS_MAGIC_FEA) ||
200                     (magic == UFS_MAGIC_LFN)) {
201                         dbg("magic 0x%08x(le)", magic);
202                         goto found;
203                 }
204         }
205         return -1;
206
207 found:
208         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
209         id->type = "ufs";
210
211         return 0;
212 }