1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/fs.h>
34#include <linux/buffer_head.h>
35#include <linux/kernel.h>
36
37#include "vxfs.h"
38#include "vxfs_inode.h"
39#include "vxfs_extern.h"
40
41
42#ifdef DIAGNOSTIC
43static void
44vxfs_typdump(struct vxfs_typed *typ)
45{
46 printk(KERN_DEBUG "type=%Lu ", typ->vt_hdr >> VXFS_TYPED_TYPESHIFT);
47 printk("offset=%Lx ", typ->vt_hdr & VXFS_TYPED_OFFSETMASK);
48 printk("block=%x ", typ->vt_block);
49 printk("size=%x\n", typ->vt_size);
50}
51#endif
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66static daddr_t
67vxfs_bmap_ext4(struct inode *ip, long bn)
68{
69 struct super_block *sb = ip->i_sb;
70 struct vxfs_inode_info *vip = VXFS_INO(ip);
71 unsigned long bsize = sb->s_blocksize;
72 u32 indsize = vip->vii_ext4.ve4_indsize;
73 int i;
74
75 if (indsize > sb->s_blocksize)
76 goto fail_size;
77
78 for (i = 0; i < VXFS_NDADDR; i++) {
79 struct direct *d = vip->vii_ext4.ve4_direct + i;
80 if (bn >= 0 && bn < d->size)
81 return (bn + d->extent);
82 bn -= d->size;
83 }
84
85 if ((bn / (indsize * indsize * bsize / 4)) == 0) {
86 struct buffer_head *buf;
87 daddr_t bno;
88 u32 *indir;
89
90 buf = sb_bread(sb, vip->vii_ext4.ve4_indir[0]);
91 if (!buf || !buffer_mapped(buf))
92 goto fail_buf;
93
94 indir = (u32 *)buf->b_data;
95 bno = indir[(bn/indsize) % (indsize*bn)] + (bn%indsize);
96
97 brelse(buf);
98 return bno;
99 } else
100 printk(KERN_WARNING "no matching indir?");
101
102 return 0;
103
104fail_size:
105 printk("vxfs: indirect extent too big!\n");
106fail_buf:
107 return 0;
108}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127static daddr_t
128vxfs_bmap_indir(struct inode *ip, long indir, int size, long block)
129{
130 struct buffer_head *bp = NULL;
131 daddr_t pblock = 0;
132 int i;
133
134 for (i = 0; i < size * VXFS_TYPED_PER_BLOCK(ip->i_sb); i++) {
135 struct vxfs_typed *typ;
136 int64_t off;
137
138 bp = sb_bread(ip->i_sb,
139 indir + (i / VXFS_TYPED_PER_BLOCK(ip->i_sb)));
140 if (!bp || !buffer_mapped(bp))
141 return 0;
142
143 typ = ((struct vxfs_typed *)bp->b_data) +
144 (i % VXFS_TYPED_PER_BLOCK(ip->i_sb));
145 off = (typ->vt_hdr & VXFS_TYPED_OFFSETMASK);
146
147 if (block < off) {
148 brelse(bp);
149 continue;
150 }
151
152 switch ((u_int32_t)(typ->vt_hdr >> VXFS_TYPED_TYPESHIFT)) {
153 case VXFS_TYPED_INDIRECT:
154 pblock = vxfs_bmap_indir(ip, typ->vt_block,
155 typ->vt_size, block - off);
156 if (pblock == -2)
157 break;
158 goto out;
159 case VXFS_TYPED_DATA:
160 if ((block - off) >= typ->vt_size)
161 break;
162 pblock = (typ->vt_block + block - off);
163 goto out;
164 case VXFS_TYPED_INDIRECT_DEV4:
165 case VXFS_TYPED_DATA_DEV4: {
166 struct vxfs_typed_dev4 *typ4 =
167 (struct vxfs_typed_dev4 *)typ;
168
169 printk(KERN_INFO "\n\nTYPED_DEV4 detected!\n");
170 printk(KERN_INFO "block: %Lu\tsize: %Ld\tdev: %d\n",
171 (unsigned long long) typ4->vd4_block,
172 (unsigned long long) typ4->vd4_size,
173 typ4->vd4_dev);
174 goto fail;
175 }
176 default:
177 BUG();
178 }
179 brelse(bp);
180 }
181
182fail:
183 pblock = 0;
184out:
185 brelse(bp);
186 return (pblock);
187}
188
189
190
191
192
193
194
195
196
197
198
199
200static daddr_t
201vxfs_bmap_typed(struct inode *ip, long iblock)
202{
203 struct vxfs_inode_info *vip = VXFS_INO(ip);
204 daddr_t pblock = 0;
205 int i;
206
207 for (i = 0; i < VXFS_NTYPED; i++) {
208 struct vxfs_typed *typ = vip->vii_org.typed + i;
209 int64_t off = (typ->vt_hdr & VXFS_TYPED_OFFSETMASK);
210
211#ifdef DIAGNOSTIC
212 vxfs_typdump(typ);
213#endif
214 if (iblock < off)
215 continue;
216 switch ((u_int32_t)(typ->vt_hdr >> VXFS_TYPED_TYPESHIFT)) {
217 case VXFS_TYPED_INDIRECT:
218 pblock = vxfs_bmap_indir(ip, typ->vt_block,
219 typ->vt_size, iblock - off);
220 if (pblock == -2)
221 break;
222 return (pblock);
223 case VXFS_TYPED_DATA:
224 if ((iblock - off) < typ->vt_size)
225 return (typ->vt_block + iblock - off);
226 break;
227 case VXFS_TYPED_INDIRECT_DEV4:
228 case VXFS_TYPED_DATA_DEV4: {
229 struct vxfs_typed_dev4 *typ4 =
230 (struct vxfs_typed_dev4 *)typ;
231
232 printk(KERN_INFO "\n\nTYPED_DEV4 detected!\n");
233 printk(KERN_INFO "block: %Lu\tsize: %Ld\tdev: %d\n",
234 (unsigned long long) typ4->vd4_block,
235 (unsigned long long) typ4->vd4_size,
236 typ4->vd4_dev);
237 return 0;
238 }
239 default:
240 BUG();
241 }
242 }
243
244 return 0;
245}
246
247
248
249
250
251
252
253
254
255
256
257
258
259daddr_t
260vxfs_bmap1(struct inode *ip, long iblock)
261{
262 struct vxfs_inode_info *vip = VXFS_INO(ip);
263
264 if (VXFS_ISEXT4(vip))
265 return vxfs_bmap_ext4(ip, iblock);
266 if (VXFS_ISTYPED(vip))
267 return vxfs_bmap_typed(ip, iblock);
268 if (VXFS_ISNONE(vip))
269 goto unsupp;
270 if (VXFS_ISIMMED(vip))
271 goto unsupp;
272
273 printk(KERN_WARNING "vxfs: inode %ld has no valid orgtype (%x)\n",
274 ip->i_ino, vip->vii_orgtype);
275 BUG();
276
277unsupp:
278 printk(KERN_WARNING "vxfs: inode %ld has an unsupported orgtype (%x)\n",
279 ip->i_ino, vip->vii_orgtype);
280 return 0;
281}
282