/src/barebox/fs/ext4/ext4_common.c
Line | Count | Source |
1 | | /* |
2 | | * (C) Copyright 2011 - 2012 Samsung Electronics |
3 | | * EXT4 filesystem implementation in Uboot by |
4 | | * Uma Shankar <uma.shankar@samsung.com> |
5 | | * Manjunatha C Achar <a.manjunatha@samsung.com> |
6 | | * |
7 | | * ext4ls and ext4load : Based on ext2 ls load support in Uboot. |
8 | | * |
9 | | * (C) Copyright 2004 |
10 | | * esd gmbh <www.esd-electronics.com> |
11 | | * Reinhard Arlt <reinhard.arlt@esd-electronics.com> |
12 | | * |
13 | | * based on code from grub2 fs/ext2.c and fs/fshelp.c by |
14 | | * GRUB -- GRand Unified Bootloader |
15 | | * Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
16 | | * |
17 | | * ext4write : Based on generic ext4 protocol. |
18 | | * |
19 | | * This program is free software; you can redistribute it and/or modify |
20 | | * it under the terms of the GNU General Public License as published by |
21 | | * the Free Software Foundation; either version 2 of the License, or |
22 | | * (at your option) any later version. |
23 | | * |
24 | | * This program is distributed in the hope that it will be useful, |
25 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
27 | | * GNU General Public License for more details. |
28 | | * |
29 | | */ |
30 | | |
31 | | #include <common.h> |
32 | | #include <malloc.h> |
33 | | #include <linux/stat.h> |
34 | | #include <linux/time.h> |
35 | | #include <linux/log2.h> |
36 | | #include <linux/magic.h> |
37 | | #include <asm/byteorder.h> |
38 | | #include <dma.h> |
39 | | |
40 | | #include "ext4_common.h" |
41 | | |
42 | | /* |
43 | | * Validate that eh_entries does not exceed the capacity of the buffer |
44 | | * holding the extent block. Returns 0 if valid, -EINVAL otherwise. |
45 | | */ |
46 | | static int ext4_check_eh_entries(struct ext4_extent_header *ext_block, |
47 | | char *buf, int blksz) |
48 | 115k | { |
49 | 115k | int max_entries; |
50 | | /* ext4_extent and ext4_extent_idx are both 12 bytes */ |
51 | 115k | const int entry_size = sizeof(struct ext4_extent); |
52 | | |
53 | 115k | if ((char *)ext_block == buf) |
54 | 0 | max_entries = (blksz - sizeof(*ext_block)) / entry_size; |
55 | 115k | else |
56 | 115k | max_entries = (sizeof(((struct ext2_inode *)0)->b) - |
57 | 115k | sizeof(*ext_block)) / entry_size; |
58 | | |
59 | 115k | if (le16_to_cpu(ext_block->eh_entries) > max_entries) |
60 | 556 | return -EINVAL; |
61 | | |
62 | 114k | return 0; |
63 | 115k | } |
64 | | |
65 | | static struct ext4_extent_header *ext4fs_get_extent_block(struct ext2_data *data, |
66 | | char *buf, struct ext4_extent_header *ext_block, |
67 | | uint32_t fileblock, int log2_blksz) |
68 | 118k | { |
69 | 118k | struct ext4_extent_idx *index; |
70 | 118k | sector_t block; |
71 | 118k | struct ext_filesystem *fs = data->fs; |
72 | 118k | int blksz = EXT2_BLOCK_SIZE(data); |
73 | 118k | int depth = le16_to_cpu(ext_block->eh_depth); |
74 | 118k | ssize_t ret; |
75 | 118k | int i; |
76 | | |
77 | 118k | if (depth > EXT4_MAX_EXTENT_DEPTH) |
78 | 1.96k | return NULL; |
79 | | |
80 | 116k | while (1) { |
81 | 116k | index = (struct ext4_extent_idx *)(ext_block + 1); |
82 | | |
83 | 116k | if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC) |
84 | 1.71k | return NULL; |
85 | | |
86 | | /* Each level is one closer to the leaves than its parent */ |
87 | 115k | if (le16_to_cpu(ext_block->eh_depth) != depth) |
88 | 0 | return NULL; |
89 | | |
90 | 115k | if (depth == 0) |
91 | 113k | return ext_block; |
92 | | |
93 | 1.46k | if (ext4_check_eh_entries(ext_block, buf, blksz)) |
94 | 289 | return NULL; |
95 | | |
96 | | /* An index node without entries leads nowhere */ |
97 | 1.17k | if (!ext_block->eh_entries) |
98 | 1 | return NULL; |
99 | | |
100 | 1.17k | i = -1; |
101 | 2.73k | do { |
102 | 2.73k | i++; |
103 | 2.73k | if (i >= le16_to_cpu(ext_block->eh_entries)) |
104 | 252 | break; |
105 | 2.73k | } while (fileblock >= le32_to_cpu(index[i].ei_block)); |
106 | | |
107 | | /* |
108 | | * A block before the first index is in a hole, which the |
109 | | * leaf the first index leads to maps as such. |
110 | | */ |
111 | 1.17k | if (i > 0) |
112 | 721 | i--; |
113 | | |
114 | 1.17k | block = le16_to_cpu(index[i].ei_leaf_hi); |
115 | 1.17k | block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo); |
116 | | |
117 | 1.17k | ret = ext4fs_devread(fs, block << log2_blksz, 0, blksz, buf); |
118 | 1.17k | if (ret) |
119 | 828 | return NULL; |
120 | 348 | else |
121 | 348 | ext_block = (struct ext4_extent_header *)buf; |
122 | | |
123 | 348 | depth--; |
124 | 348 | } |
125 | 116k | } |
126 | | |
127 | | static ssize_t ext4fs_blockgroup(struct ext2_data *data, int group, |
128 | | struct ext2_block_group *blkgrp) |
129 | 14.9k | { |
130 | 14.9k | sector_t blkno; |
131 | 14.9k | unsigned int blkoff, desc_per_blk; |
132 | 14.9k | struct ext_filesystem *fs = data->fs; |
133 | 14.9k | int desc_size = fs->gdsize; |
134 | | |
135 | 14.9k | desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size; |
136 | | |
137 | 14.9k | blkno = le32_to_cpu(data->sblock.first_data_block) + 1 + |
138 | 14.9k | group / desc_per_blk; |
139 | 14.9k | blkoff = (group % desc_per_blk) * desc_size; |
140 | | |
141 | 14.9k | dev_dbg(fs->dev, "read %d group descriptor (blkno %llu blkoff %u)\n", |
142 | 14.9k | group, blkno, blkoff); |
143 | | |
144 | | /* Descriptors can be larger than the fields we know about */ |
145 | 14.9k | return ext4fs_devread(fs, blkno << LOG2_EXT2_BLOCK_SIZE(data), blkoff, |
146 | 14.9k | min_t(int, desc_size, sizeof(*blkgrp)), |
147 | 14.9k | (char *)blkgrp); |
148 | 14.9k | } |
149 | | |
150 | | int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode) |
151 | 16.0k | { |
152 | 16.0k | struct ext2_block_group blkgrp; |
153 | 16.0k | struct ext2_sblock *sblock = &data->sblock; |
154 | 16.0k | struct ext_filesystem *fs = data->fs; |
155 | 16.0k | int inodes_per_block; |
156 | 16.0k | ssize_t ret; |
157 | 16.0k | sector_t blkno; |
158 | 16.0k | unsigned int blkoff; |
159 | | |
160 | 16.0k | if (ino < EXT2_ROOT_INO || |
161 | 15.4k | (uint32_t)ino > le32_to_cpu(sblock->total_inodes)) { |
162 | 1.03k | dev_err(fs->dev, "invalid inode number %u\n", (uint32_t)ino); |
163 | 1.03k | return -EINVAL; |
164 | 1.03k | } |
165 | | |
166 | | /* It is easier to calculate if the first inode is 0. */ |
167 | 14.9k | ino--; |
168 | 14.9k | ret = ext4fs_blockgroup(data, ino / le32_to_cpu |
169 | 14.9k | (sblock->inodes_per_group), &blkgrp); |
170 | 14.9k | if (ret) |
171 | 457 | return ret; |
172 | | |
173 | 14.5k | inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz; |
174 | 14.5k | blkno = le32_to_cpu(blkgrp.inode_table_id) + |
175 | 14.5k | (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block; |
176 | 14.5k | blkoff = (ino % inodes_per_block) * fs->inodesz; |
177 | | /* Read the inode. */ |
178 | 14.5k | ret = ext4fs_devread(fs, blkno << LOG2_EXT2_BLOCK_SIZE(data), blkoff, |
179 | 14.5k | sizeof(struct ext2_inode), (char *)inode); |
180 | 14.5k | if (ret) |
181 | 1.07k | return ret; |
182 | | |
183 | 13.4k | return 0; |
184 | 14.5k | } |
185 | | |
186 | | static int ext4fs_get_indir_block(struct ext2fs_node *node, |
187 | | struct ext4fs_indir_block *indir, uint32_t blkno) |
188 | 657k | { |
189 | 657k | struct ext_filesystem *fs = node->data->fs; |
190 | 657k | int blksz; |
191 | 657k | ssize_t ret; |
192 | | |
193 | 657k | blksz = EXT2_BLOCK_SIZE(node->data); |
194 | | |
195 | 657k | if (indir->blkno == blkno) |
196 | 641k | return 0; |
197 | | |
198 | 15.9k | if (!blkno) { |
199 | | /* A hole, so are all blocks the indirect block would map */ |
200 | 4.53k | memset(indir->data, 0, blksz); |
201 | 11.4k | } else { |
202 | 11.4k | ret = ext4fs_devread(fs, (sector_t)blkno << LOG2_EXT2_BLOCK_SIZE(node->data), |
203 | 11.4k | 0, blksz, (void *)indir->data); |
204 | 11.4k | if (ret) { |
205 | 10.5k | indir->blkno = EXT4FS_INDIR_NONE; |
206 | 10.5k | dev_err(fs->dev, "** SI ext2fs read block (indir 1)" |
207 | 10.5k | "failed. **\n"); |
208 | 10.5k | return ret; |
209 | 10.5k | } |
210 | 11.4k | } |
211 | | |
212 | 5.46k | indir->blkno = blkno; |
213 | | |
214 | 5.46k | return 0; |
215 | 15.9k | } |
216 | | |
217 | | int64_t read_allocated_block(struct ext2fs_node *node, uint64_t fileblock) |
218 | 994k | { |
219 | 994k | int64_t blknr; |
220 | 994k | int blksz; |
221 | 994k | int log2_blksz; |
222 | 994k | uint32_t rblock; |
223 | 994k | uint32_t perblock_parent; |
224 | 994k | uint32_t perblock_child; |
225 | 994k | unsigned long long start; |
226 | 994k | struct ext2_inode *inode = &node->inode; |
227 | 994k | struct ext2_data *data = node->data; |
228 | 994k | int ret; |
229 | | |
230 | | /* get the blocksize of the filesystem */ |
231 | 994k | blksz = EXT2_BLOCK_SIZE(node->data); |
232 | 994k | log2_blksz = LOG2_EXT2_BLOCK_SIZE(node->data); |
233 | | |
234 | | /* Block numbers within a file are 32 bit wide, as in Linux */ |
235 | 994k | if (fileblock >= EXT_MAX_BLOCKS) |
236 | 2.65k | return -EINVAL; |
237 | | |
238 | 992k | if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) { |
239 | 118k | uint64_t startblock, endblock; |
240 | 118k | char *buf = zalloc(blksz); |
241 | 118k | struct ext4_extent_header *ext_block; |
242 | 118k | struct ext4_extent *extent; |
243 | 118k | int i; |
244 | | |
245 | 118k | if (!buf) |
246 | 0 | return -ENOMEM; |
247 | | |
248 | 118k | ext_block = ext4fs_get_extent_block(node->data, buf, |
249 | 118k | (struct ext4_extent_header *)inode->b.blocks.dir_blocks, |
250 | 118k | fileblock, log2_blksz); |
251 | 118k | if (!ext_block) { |
252 | 4.79k | pr_err("invalid extent block\n"); |
253 | 4.79k | free(buf); |
254 | 4.79k | return -EINVAL; |
255 | 4.79k | } |
256 | | |
257 | 113k | extent = (struct ext4_extent *)(ext_block + 1); |
258 | | |
259 | 113k | if (ext4_check_eh_entries(ext_block, buf, blksz)) { |
260 | 267 | free(buf); |
261 | 267 | return -EINVAL; |
262 | 267 | } |
263 | | |
264 | 151k | for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) { |
265 | 138k | unsigned int len = le16_to_cpu(extent[i].ee_len); |
266 | 138k | bool unwritten = len > EXT_INIT_MAX_LEN; |
267 | | |
268 | 138k | if (unwritten) |
269 | 24.6k | len -= EXT_INIT_MAX_LEN; |
270 | | |
271 | 138k | startblock = le32_to_cpu(extent[i].ee_block); |
272 | 138k | endblock = startblock + len; |
273 | | |
274 | 138k | if (startblock > fileblock) { |
275 | | /* Sparse file */ |
276 | 56.8k | free(buf); |
277 | 56.8k | return 0; |
278 | 56.8k | } |
279 | | |
280 | 81.7k | if (fileblock < endblock && unwritten) { |
281 | | /* Allocated, but never written: reads as zeroes */ |
282 | 17.8k | free(buf); |
283 | 17.8k | return 0; |
284 | 17.8k | } |
285 | | |
286 | 63.8k | if (fileblock < endblock) { |
287 | 25.6k | start = le16_to_cpu(extent[i].ee_start_hi); |
288 | 25.6k | start = (start << 32) + |
289 | 25.6k | le32_to_cpu(extent[i].ee_start_lo); |
290 | 25.6k | free(buf); |
291 | 25.6k | return (fileblock - startblock) + start; |
292 | 25.6k | } |
293 | 63.8k | } |
294 | | |
295 | 13.1k | free(buf); |
296 | 13.1k | return 0; |
297 | 113k | } |
298 | | |
299 | 873k | if (fileblock < INDIRECT_BLOCKS) { |
300 | | /* Direct blocks. */ |
301 | 238k | blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]); |
302 | 635k | } else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) { |
303 | | /* Indirect. */ |
304 | 612k | ret = ext4fs_get_indir_block(node, &data->indir1, |
305 | 612k | le32_to_cpu(inode->b.blocks.indir_block)); |
306 | 612k | if (ret) |
307 | 3.48k | return ret; |
308 | 609k | blknr = le32_to_cpu(data->indir1.data[fileblock - INDIRECT_BLOCKS]); |
309 | 609k | } else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 * |
310 | 22.1k | (blksz / 4 + 1)))) { |
311 | | /* Double indirect. */ |
312 | 10.4k | uint32_t perblock = blksz / 4; |
313 | | |
314 | 10.4k | rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4); |
315 | | |
316 | 10.4k | ret = ext4fs_get_indir_block(node, &data->indir1, |
317 | 10.4k | le32_to_cpu(inode->b.blocks.double_indir_block)); |
318 | 10.4k | if (ret) |
319 | 3.53k | return ret; |
320 | | |
321 | 6.95k | ret = ext4fs_get_indir_block(node, &data->indir2, |
322 | 6.95k | le32_to_cpu(data->indir1.data[rblock / perblock])); |
323 | 6.95k | if (ret) |
324 | 470 | return ret; |
325 | | |
326 | 6.48k | blknr = le32_to_cpu(data->indir2.data[rblock % perblock]); |
327 | 11.6k | } else { |
328 | | /* Triple indirect. */ |
329 | 11.6k | rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 + |
330 | 11.6k | (blksz / 4 * blksz / 4)); |
331 | 11.6k | perblock_child = blksz / 4; |
332 | 11.6k | perblock_parent = ((blksz / 4) * (blksz / 4)); |
333 | | |
334 | | /* Beyond what the triple indirect block can map */ |
335 | 11.6k | if (rblock / perblock_parent >= perblock_child) |
336 | 684 | return -EINVAL; |
337 | | |
338 | 10.9k | ret = ext4fs_get_indir_block(node, &data->indir1, |
339 | 10.9k | le32_to_cpu(inode->b.blocks.triple_indir_block)); |
340 | 10.9k | if (ret) |
341 | 2.40k | return ret; |
342 | | |
343 | 8.53k | ret = ext4fs_get_indir_block(node, &data->indir2, |
344 | 8.53k | le32_to_cpu(data->indir1.data[rblock / perblock_parent])); |
345 | 8.53k | if (ret) |
346 | 386 | return ret; |
347 | | |
348 | 8.15k | ret = ext4fs_get_indir_block(node, &data->indir3, |
349 | 8.15k | le32_to_cpu(data->indir2.data[(rblock / perblock_child) % |
350 | 8.15k | perblock_child])); |
351 | 8.15k | if (ret) |
352 | 257 | return ret; |
353 | | |
354 | 7.89k | blknr = le32_to_cpu(data->indir3.data[rblock % perblock_child]); |
355 | 7.89k | } |
356 | | |
357 | 862k | return blknr; |
358 | 873k | } |
359 | | |
360 | | int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, |
361 | | struct ext2fs_node **fnode, int *ftype) |
362 | 0 | { |
363 | 0 | unsigned int fpos = 0; |
364 | 0 | int status, ret; |
365 | 0 | struct ext2fs_node *diro = (struct ext2fs_node *) dir; |
366 | 0 | struct ext_filesystem *fs = dir->data->fs; |
367 | |
|
368 | 0 | if (name != NULL) |
369 | 0 | dev_dbg(fs->dev, "Iterate dir %s\n", name); |
370 | |
|
371 | 0 | if (!diro->inode_read) { |
372 | 0 | ret = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); |
373 | 0 | if (ret) |
374 | 0 | return ret; |
375 | 0 | } |
376 | | /* Search the file. */ |
377 | 0 | while (fpos < le32_to_cpu(diro->inode.size)) { |
378 | 0 | struct ext2_dirent dirent; |
379 | |
|
380 | 0 | status = ext4fs_read_file(diro, fpos, |
381 | 0 | sizeof(struct ext2_dirent), |
382 | 0 | (char *) &dirent); |
383 | 0 | if (status < 1) |
384 | 0 | return -EINVAL; |
385 | | |
386 | 0 | if (dirent.namelen != 0) { |
387 | 0 | char filename[dirent.namelen + 1]; |
388 | 0 | struct ext2fs_node *fdiro; |
389 | 0 | int type = FILETYPE_UNKNOWN; |
390 | |
|
391 | 0 | status = ext4fs_read_file(diro, |
392 | 0 | fpos + |
393 | 0 | sizeof(struct ext2_dirent), |
394 | 0 | dirent.namelen, filename); |
395 | 0 | if (status < 1) |
396 | 0 | return -EINVAL; |
397 | | |
398 | 0 | fdiro = zalloc(sizeof(struct ext2fs_node)); |
399 | 0 | if (!fdiro) |
400 | 0 | return -ENOMEM; |
401 | | |
402 | 0 | fdiro->data = diro->data; |
403 | 0 | fdiro->ino = le32_to_cpu(dirent.inode); |
404 | |
|
405 | 0 | filename[dirent.namelen] = '\0'; |
406 | |
|
407 | 0 | if (dirent.filetype != FILETYPE_UNKNOWN) { |
408 | 0 | fdiro->inode_read = 0; |
409 | |
|
410 | 0 | if (dirent.filetype == FILETYPE_DIRECTORY) |
411 | 0 | type = FILETYPE_DIRECTORY; |
412 | 0 | else if (dirent.filetype == FILETYPE_SYMLINK) |
413 | 0 | type = FILETYPE_SYMLINK; |
414 | 0 | else if (dirent.filetype == FILETYPE_REG) |
415 | 0 | type = FILETYPE_REG; |
416 | 0 | } else { |
417 | 0 | ret = ext4fs_read_inode(diro->data, |
418 | 0 | le32_to_cpu |
419 | 0 | (dirent.inode), |
420 | 0 | &fdiro->inode); |
421 | 0 | if (ret) { |
422 | 0 | free(fdiro); |
423 | 0 | return ret; |
424 | 0 | } |
425 | 0 | fdiro->inode_read = 1; |
426 | |
|
427 | 0 | if ((le16_to_cpu(fdiro->inode.mode) & |
428 | 0 | FILETYPE_INO_MASK) == |
429 | 0 | FILETYPE_INO_DIRECTORY) { |
430 | 0 | type = FILETYPE_DIRECTORY; |
431 | 0 | } else if ((le16_to_cpu(fdiro->inode.mode) |
432 | 0 | & FILETYPE_INO_MASK) == |
433 | 0 | FILETYPE_INO_SYMLINK) { |
434 | 0 | type = FILETYPE_SYMLINK; |
435 | 0 | } else if ((le16_to_cpu(fdiro->inode.mode) |
436 | 0 | & FILETYPE_INO_MASK) == |
437 | 0 | FILETYPE_INO_REG) { |
438 | 0 | type = FILETYPE_REG; |
439 | 0 | } |
440 | 0 | } |
441 | | |
442 | 0 | dev_dbg(fs->dev, "iterate >%s<\n", filename); |
443 | |
|
444 | 0 | if (strcmp(filename, name) == 0) { |
445 | 0 | *ftype = type; |
446 | 0 | *fnode = fdiro; |
447 | 0 | return 0; |
448 | 0 | } |
449 | | |
450 | 0 | free(fdiro); |
451 | 0 | } |
452 | 0 | if (le16_to_cpu(dirent.direntlen) < sizeof(struct ext2_dirent)) |
453 | 0 | return -EINVAL; |
454 | 0 | fpos += le16_to_cpu(dirent.direntlen); |
455 | 0 | } |
456 | 0 | return -ENOENT; |
457 | 0 | } |
458 | | |
459 | | char *ext4fs_read_symlink(struct ext2fs_node *node) |
460 | 0 | { |
461 | 0 | char *symlink; |
462 | 0 | struct ext2fs_node *diro = node; |
463 | 0 | int status, ret; |
464 | 0 | size_t alloc_size; |
465 | |
|
466 | 0 | if (!diro->inode_read) { |
467 | 0 | ret = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); |
468 | 0 | if (ret) |
469 | 0 | return NULL; |
470 | 0 | } |
471 | | |
472 | 0 | if (__builtin_add_overflow(le32_to_cpu(diro->inode.size), 1, &alloc_size)) |
473 | 0 | return NULL; |
474 | | |
475 | 0 | symlink = zalloc(alloc_size); |
476 | 0 | if (!symlink) |
477 | 0 | return 0; |
478 | | |
479 | 0 | if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) { |
480 | 0 | strncpy(symlink, diro->inode.b.symlink, |
481 | 0 | le32_to_cpu(diro->inode.size)); |
482 | 0 | } else { |
483 | 0 | status = ext4fs_read_file(diro, 0, |
484 | 0 | le32_to_cpu(diro->inode.size), |
485 | 0 | symlink); |
486 | 0 | if (status == 0) { |
487 | 0 | free(symlink); |
488 | 0 | return NULL; |
489 | 0 | } |
490 | 0 | } |
491 | | |
492 | 0 | symlink[le32_to_cpu(diro->inode.size)] = '\0'; |
493 | |
|
494 | 0 | return symlink; |
495 | 0 | } |
496 | | |
497 | | int ext4fs_find_file(const char *currpath, |
498 | | struct ext2fs_node *currroot, |
499 | | struct ext2fs_node **currfound, int *foundtype) |
500 | 0 | { |
501 | 0 | char fpath[strlen(currpath) + 1]; |
502 | 0 | char *name = fpath; |
503 | 0 | char *next; |
504 | 0 | int type = FILETYPE_DIRECTORY; |
505 | 0 | struct ext2fs_node *currnode = currroot; |
506 | 0 | struct ext2fs_node *oldnode = currroot; |
507 | 0 | int ret = 0; |
508 | |
|
509 | 0 | strncpy(fpath, currpath, strlen(currpath) + 1); |
510 | | |
511 | | /* Remove all leading slashes. */ |
512 | 0 | while (*name == '/') |
513 | 0 | name++; |
514 | |
|
515 | 0 | if (!*name) { |
516 | 0 | *currfound = currnode; |
517 | 0 | goto out; |
518 | 0 | } |
519 | | |
520 | 0 | for (;;) { |
521 | | /* Extract the actual part from the pathname. */ |
522 | 0 | next = strchr(name, '/'); |
523 | 0 | if (next) { |
524 | | /* Remove all leading slashes. */ |
525 | 0 | while (*next == '/') |
526 | 0 | *(next++) = '\0'; |
527 | 0 | } |
528 | |
|
529 | 0 | if (type != FILETYPE_DIRECTORY) { |
530 | 0 | ext4fs_free_node(currnode, currroot); |
531 | 0 | return -ENOENT; |
532 | 0 | } |
533 | | |
534 | 0 | oldnode = currnode; |
535 | | |
536 | | /* Iterate over the directory. */ |
537 | 0 | ret = ext4fs_iterate_dir(currnode, name, &currnode, &type); |
538 | 0 | if (ret) |
539 | 0 | return ret; |
540 | | |
541 | 0 | ext4fs_free_node(oldnode, currroot); |
542 | | |
543 | | /* Found the node! */ |
544 | 0 | if (!next || *next == '\0') { |
545 | 0 | *currfound = currnode; |
546 | 0 | goto out; |
547 | 0 | } |
548 | 0 | name = next; |
549 | 0 | } |
550 | | |
551 | 0 | out: |
552 | 0 | if (foundtype) |
553 | 0 | *foundtype = type; |
554 | |
|
555 | 0 | return ret; |
556 | 0 | } |
557 | | |
558 | | int ext4fs_open(struct ext2_data *data, const char *filename, struct ext2fs_node **inode) |
559 | 0 | { |
560 | 0 | struct ext2fs_node *fdiro = NULL; |
561 | 0 | int status, ret; |
562 | 0 | int type; |
563 | |
|
564 | 0 | status = ext4fs_find_file(filename, &data->diropen, &fdiro, &type); |
565 | 0 | if (status) |
566 | 0 | goto fail; |
567 | | |
568 | 0 | if (type != FILETYPE_REG) |
569 | 0 | return -EINVAL; |
570 | | |
571 | 0 | if (!fdiro->inode_read) { |
572 | 0 | ret = ext4fs_read_inode(fdiro->data, fdiro->ino, |
573 | 0 | &fdiro->inode); |
574 | 0 | if (ret) |
575 | 0 | goto fail; |
576 | 0 | } |
577 | | |
578 | 0 | *inode = fdiro; |
579 | |
|
580 | 0 | return 0; |
581 | 0 | fail: |
582 | 0 | ext4fs_free_node(fdiro, &data->diropen); |
583 | |
|
584 | 0 | return -ENOENT; |
585 | 0 | } |
586 | | |
587 | | int ext4fs_mount(struct ext_filesystem *fs) |
588 | 1.78k | { |
589 | 1.78k | struct ext2_data *data; |
590 | 1.78k | ssize_t ret; |
591 | 1.78k | int blksz; |
592 | | |
593 | 1.78k | data = zalloc(sizeof(struct ext2_data)); |
594 | 1.78k | if (!data) |
595 | 0 | return -ENOMEM; |
596 | | |
597 | | /* Read the superblock. */ |
598 | 1.78k | ret = ext4fs_devread(fs, 1 * 2, 0, sizeof(struct ext2_sblock), |
599 | 1.78k | (char *)&data->sblock); |
600 | 1.78k | if (ret) |
601 | 26 | goto fail; |
602 | | |
603 | | /* Make sure this is an ext2 filesystem. */ |
604 | 1.75k | if (le16_to_cpu(data->sblock.magic) != EXT2_SUPER_MAGIC) { |
605 | 24 | ret = -EINVAL; |
606 | 24 | goto fail; |
607 | 24 | } |
608 | | |
609 | 1.73k | if (le32_to_cpu(data->sblock.log2_block_size) > |
610 | 1.73k | EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE) { |
611 | 47 | dev_err(fs->dev, "invalid block size %u\n", |
612 | 47 | le32_to_cpu(data->sblock.log2_block_size)); |
613 | 47 | ret = -EINVAL; |
614 | 47 | goto fail; |
615 | 47 | } |
616 | | |
617 | 1.68k | if (le32_to_cpu(data->sblock.revision_level) == 0) { |
618 | 1.26k | fs->inodesz = EXT2_GOOD_OLD_INODE_SIZE; |
619 | 1.26k | fs->gdsize = EXT4_MIN_DESC_SIZE; |
620 | 1.26k | } else { |
621 | 421 | debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n", |
622 | 421 | le32_to_cpu(data->sblock.feature_compatibility), |
623 | 0 | le32_to_cpu(data->sblock.feature_incompat), |
624 | 0 | le32_to_cpu(data->sblock.feature_ro_compat)); |
625 | | |
626 | 421 | fs->inodesz = le16_to_cpu(data->sblock.inode_size); |
627 | 421 | if (fs->inodesz < EXT2_GOOD_OLD_INODE_SIZE || |
628 | 412 | fs->inodesz > EXT2_BLOCK_SIZE(data) || |
629 | 393 | !is_power_of_2(fs->inodesz)) { |
630 | 42 | dev_err(fs->dev, "unsupported inode size %u\n", |
631 | 42 | fs->inodesz); |
632 | 42 | ret = -EINVAL; |
633 | 42 | goto fail; |
634 | 42 | } |
635 | | |
636 | 379 | fs->gdsize = EXT4_MIN_DESC_SIZE; |
637 | | |
638 | 379 | if (le32_to_cpu(data->sblock.feature_incompat) & |
639 | 379 | EXT4_FEATURE_INCOMPAT_64BIT) { |
640 | 48 | fs->gdsize = le16_to_cpu(data->sblock.descriptor_size); |
641 | | |
642 | 48 | if (fs->gdsize < EXT4_MIN_DESC_SIZE_64BIT || |
643 | 40 | fs->gdsize > EXT4_MAX_DESC_SIZE || |
644 | 33 | !is_power_of_2(fs->gdsize)) { |
645 | 33 | dev_err(fs->dev, "unsupported descriptor size %u\n", |
646 | 33 | fs->gdsize); |
647 | 33 | ret = -EINVAL; |
648 | 33 | goto fail; |
649 | 33 | } |
650 | 48 | } |
651 | 379 | } |
652 | | |
653 | 1.61k | dev_info(fs->dev, "EXT2 rev %d, inode_size %d, descriptor size %d\n", |
654 | 1.61k | le32_to_cpu(data->sblock.revision_level), |
655 | 1.61k | fs->inodesz, fs->gdsize); |
656 | | |
657 | 1.61k | if (!le32_to_cpu(data->sblock.inodes_per_group)) { |
658 | 2 | dev_err(fs->dev, "invalid superblock: inodes_per_group=0\n"); |
659 | 2 | ret = -EINVAL; |
660 | 2 | goto fail; |
661 | 2 | } |
662 | | |
663 | 1.60k | data->diropen.data = data; |
664 | 1.60k | data->diropen.ino = 2; |
665 | 1.60k | data->diropen.inode_read = 1; |
666 | 1.60k | data->inode = &data->diropen.inode; |
667 | 1.60k | data->fs = fs; |
668 | 1.60k | fs->data = data; |
669 | | |
670 | 1.60k | blksz = EXT2_BLOCK_SIZE(data); |
671 | | |
672 | 1.60k | fs->data->indir1.data = malloc(blksz); |
673 | 1.60k | fs->data->indir2.data = malloc(blksz); |
674 | 1.60k | fs->data->indir3.data = malloc(blksz); |
675 | | |
676 | 1.60k | if (!fs->data->indir1.data || !fs->data->indir2.data || |
677 | 1.60k | !fs->data->indir3.data) { |
678 | 0 | ret = -ENOMEM; |
679 | 0 | goto fail; |
680 | 0 | } |
681 | | |
682 | 1.60k | fs->data->indir1.blkno = EXT4FS_INDIR_NONE; |
683 | 1.60k | fs->data->indir2.blkno = EXT4FS_INDIR_NONE; |
684 | 1.60k | fs->data->indir3.blkno = EXT4FS_INDIR_NONE; |
685 | | |
686 | 1.60k | ret = ext4fs_read_inode(data, 2, data->inode); |
687 | 1.60k | if (ret) |
688 | 48 | goto fail; |
689 | | |
690 | 1.56k | return 0; |
691 | 222 | fail: |
692 | 222 | free(data->indir1.data); |
693 | 222 | free(data->indir2.data); |
694 | 222 | free(data->indir3.data); |
695 | 222 | free(data); |
696 | 222 | fs->data = NULL; |
697 | | |
698 | 222 | return ret; |
699 | 1.60k | } |
700 | | |
701 | | void ext4fs_umount(struct ext_filesystem *fs) |
702 | 1.56k | { |
703 | 1.56k | free(fs->data->indir1.data); |
704 | 1.56k | free(fs->data->indir2.data); |
705 | 1.56k | free(fs->data->indir3.data); |
706 | 1.56k | free(fs->data); |
707 | 1.56k | } |