Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
146k
{
49
146k
  int max_entries;
50
  /* ext4_extent and ext4_extent_idx are both 12 bytes */
51
146k
  const int entry_size = sizeof(struct ext4_extent);
52
53
146k
  if ((char *)ext_block == buf)
54
5.89k
    max_entries = (blksz - sizeof(*ext_block)) / entry_size;
55
140k
  else
56
140k
    max_entries = (sizeof(((struct ext2_inode *)0)->b) -
57
140k
             sizeof(*ext_block)) / entry_size;
58
59
146k
  if (le16_to_cpu(ext_block->eh_entries) > max_entries)
60
1.15k
    return -EINVAL;
61
62
145k
  return 0;
63
146k
}
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
143k
{
69
143k
  struct ext4_extent_idx *index;
70
143k
  sector_t block;
71
143k
  struct ext_filesystem *fs = data->fs;
72
143k
  int blksz = EXT2_BLOCK_SIZE(data);
73
143k
  int depth = le16_to_cpu(ext_block->eh_depth);
74
143k
  ssize_t ret;
75
143k
  int i;
76
77
143k
  if (depth > EXT4_MAX_EXTENT_DEPTH)
78
1.89k
    return NULL;
79
80
148k
  while (1) {
81
148k
    index = (struct ext4_extent_idx *)(ext_block + 1);
82
83
148k
    if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
84
1.19k
      return NULL;
85
86
    /* Each level is one closer to the leaves than its parent */
87
146k
    if (le16_to_cpu(ext_block->eh_depth) != depth)
88
454
      return NULL;
89
90
146k
    if (depth == 0)
91
137k
      return ext_block;
92
93
8.53k
    if (ext4_check_eh_entries(ext_block, buf, blksz))
94
518
      return NULL;
95
96
    /* An index node without entries leads nowhere */
97
8.01k
    if (!ext_block->eh_entries)
98
156
      return NULL;
99
100
7.85k
    i = -1;
101
15.7k
    do {
102
15.7k
      i++;
103
15.7k
      if (i >= le16_to_cpu(ext_block->eh_entries))
104
427
        break;
105
15.7k
    } 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
7.85k
    if (i > 0)
112
948
      i--;
113
114
7.85k
    block = le16_to_cpu(index[i].ei_leaf_hi);
115
7.85k
    block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
116
117
7.85k
    ret = ext4fs_devread(fs, block << log2_blksz, 0, blksz, buf);
118
7.85k
    if (ret)
119
932
      return NULL;
120
6.92k
    else
121
6.92k
      ext_block = (struct ext4_extent_header *)buf;
122
123
6.92k
    depth--;
124
6.92k
  }
125
141k
}
126
127
static ssize_t ext4fs_blockgroup(struct ext2_data *data, int group,
128
    struct ext2_block_group *blkgrp)
129
15.7k
{
130
15.7k
  sector_t blkno;
131
15.7k
  unsigned int blkoff, desc_per_blk;
132
15.7k
  struct ext_filesystem *fs = data->fs;
133
15.7k
  int desc_size = fs->gdsize;
134
135
15.7k
  desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
136
137
15.7k
  blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
138
15.7k
      group / desc_per_blk;
139
15.7k
  blkoff = (group % desc_per_blk) * desc_size;
140
141
15.7k
  dev_dbg(fs->dev, "read %d group descriptor (blkno %llu blkoff %u)\n",
142
15.7k
        group, blkno, blkoff);
143
144
  /* Descriptors can be larger than the fields we know about */
145
15.7k
  return ext4fs_devread(fs, blkno << LOG2_EXT2_BLOCK_SIZE(data), blkoff,
146
15.7k
            min_t(int, desc_size, sizeof(*blkgrp)),
147
15.7k
            (char *)blkgrp);
148
15.7k
}
149
150
int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
151
18.5k
{
152
18.5k
  struct ext2_block_group blkgrp;
153
18.5k
  struct ext2_sblock *sblock = &data->sblock;
154
18.5k
  struct ext_filesystem *fs = data->fs;
155
18.5k
  int inodes_per_block;
156
18.5k
  ssize_t ret;
157
18.5k
  sector_t blkno;
158
18.5k
  unsigned int blkoff;
159
160
18.5k
  if (ino < EXT2_ROOT_INO ||
161
17.0k
      (uint32_t)ino > le32_to_cpu(sblock->total_inodes)) {
162
2.80k
    dev_err(fs->dev, "invalid inode number %u\n", (uint32_t)ino);
163
2.80k
    return -EINVAL;
164
2.80k
  }
165
166
  /* It is easier to calculate if the first inode is 0. */
167
15.7k
  ino--;
168
15.7k
  ret = ext4fs_blockgroup(data, ino / le32_to_cpu
169
15.7k
           (sblock->inodes_per_group), &blkgrp);
170
15.7k
  if (ret)
171
580
    return ret;
172
173
15.1k
  inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
174
15.1k
  blkno = le32_to_cpu(blkgrp.inode_table_id) +
175
15.1k
      (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
176
15.1k
  blkoff = (ino % inodes_per_block) * fs->inodesz;
177
  /* Read the inode. */
178
15.1k
  ret = ext4fs_devread(fs, blkno << LOG2_EXT2_BLOCK_SIZE(data), blkoff,
179
15.1k
        sizeof(struct ext2_inode), (char *)inode);
180
15.1k
  if (ret)
181
3.93k
    return ret;
182
183
11.2k
  return 0;
184
15.1k
}
185
186
static int ext4fs_get_indir_block(struct ext2fs_node *node,
187
        struct ext4fs_indir_block *indir, uint32_t blkno)
188
612k
{
189
612k
  struct ext_filesystem *fs = node->data->fs;
190
612k
  int blksz;
191
612k
  ssize_t ret;
192
193
612k
  blksz = EXT2_BLOCK_SIZE(node->data);
194
195
612k
  if (indir->blkno == blkno)
196
598k
    return 0;
197
198
14.1k
  if (!blkno) {
199
    /* A hole, so are all blocks the indirect block would map */
200
4.31k
    memset(indir->data, 0, blksz);
201
9.82k
  } else {
202
9.82k
    ret = ext4fs_devread(fs, (sector_t)blkno << LOG2_EXT2_BLOCK_SIZE(node->data),
203
9.82k
             0, blksz, (void *)indir->data);
204
9.82k
    if (ret) {
205
8.72k
      indir->blkno = EXT4FS_INDIR_NONE;
206
8.72k
      dev_err(fs->dev, "** SI ext2fs read block (indir 1)"
207
8.72k
        "failed. **\n");
208
8.72k
      return ret;
209
8.72k
    }
210
9.82k
  }
211
212
5.40k
  indir->blkno = blkno;
213
214
5.40k
  return 0;
215
14.1k
}
216
217
int64_t read_allocated_block(struct ext2fs_node *node, uint64_t fileblock)
218
984k
{
219
984k
  int64_t blknr;
220
984k
  int blksz;
221
984k
  int log2_blksz;
222
984k
  uint32_t rblock;
223
984k
  uint32_t perblock_parent;
224
984k
  uint32_t perblock_child;
225
984k
  unsigned long long start;
226
984k
  struct ext2_inode *inode = &node->inode;
227
984k
  struct ext2_data *data = node->data;
228
984k
  int ret;
229
230
  /* get the blocksize of the filesystem */
231
984k
  blksz = EXT2_BLOCK_SIZE(node->data);
232
984k
  log2_blksz = LOG2_EXT2_BLOCK_SIZE(node->data);
233
234
  /* Block numbers within a file are 32 bit wide, as in Linux */
235
984k
  if (fileblock >= EXT_MAX_BLOCKS)
236
1.90k
    return -EINVAL;
237
238
982k
  if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
239
143k
    uint64_t startblock, endblock;
240
143k
    char *buf = zalloc(blksz);
241
143k
    struct ext4_extent_header *ext_block;
242
143k
    struct ext4_extent *extent;
243
143k
    int i;
244
245
143k
    if (!buf)
246
0
      return -ENOMEM;
247
248
143k
    ext_block = ext4fs_get_extent_block(node->data, buf,
249
143k
        (struct ext4_extent_header *)inode->b.blocks.dir_blocks,
250
143k
        fileblock, log2_blksz);
251
143k
    if (!ext_block) {
252
5.14k
      pr_err("invalid extent block\n");
253
5.14k
      free(buf);
254
5.14k
      return -EINVAL;
255
5.14k
    }
256
257
137k
    extent = (struct ext4_extent *)(ext_block + 1);
258
259
137k
    if (ext4_check_eh_entries(ext_block, buf, blksz)) {
260
636
      free(buf);
261
636
      return -EINVAL;
262
636
    }
263
264
233k
    for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
265
182k
      unsigned int len = le16_to_cpu(extent[i].ee_len);
266
182k
      bool unwritten = len > EXT_INIT_MAX_LEN;
267
268
182k
      if (unwritten)
269
25.6k
        len -= EXT_INIT_MAX_LEN;
270
271
182k
      startblock = le32_to_cpu(extent[i].ee_block);
272
182k
      endblock = startblock + len;
273
274
182k
      if (startblock > fileblock) {
275
        /* Sparse file */
276
76.6k
        free(buf);
277
76.6k
        return 0;
278
76.6k
      }
279
280
105k
      if (fileblock < endblock && unwritten) {
281
        /* Allocated, but never written: reads as zeroes */
282
864
        free(buf);
283
864
        return 0;
284
864
      }
285
286
104k
      if (fileblock < endblock) {
287
8.25k
        start = le16_to_cpu(extent[i].ee_start_hi);
288
8.25k
        start = (start << 32) +
289
8.25k
          le32_to_cpu(extent[i].ee_start_lo);
290
8.25k
        free(buf);
291
8.25k
        return (fileblock - startblock) + start;
292
8.25k
      }
293
104k
    }
294
295
51.5k
    free(buf);
296
51.5k
    return 0;
297
137k
  }
298
299
839k
  if (fileblock < INDIRECT_BLOCKS) {
300
    /* Direct blocks. */
301
240k
    blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
302
599k
  } else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
303
    /* Indirect. */
304
584k
    ret = ext4fs_get_indir_block(node, &data->indir1,
305
584k
        le32_to_cpu(inode->b.blocks.indir_block));
306
584k
    if (ret)
307
2.90k
      return ret;
308
581k
    blknr = le32_to_cpu(data->indir1.data[fileblock - INDIRECT_BLOCKS]);
309
581k
  } else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
310
15.0k
          (blksz / 4 + 1)))) {
311
    /* Double indirect. */
312
6.34k
    uint32_t perblock = blksz / 4;
313
314
6.34k
    rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
315
316
6.34k
    ret = ext4fs_get_indir_block(node, &data->indir1,
317
6.34k
        le32_to_cpu(inode->b.blocks.double_indir_block));
318
6.34k
    if (ret)
319
2.21k
      return ret;
320
321
4.13k
    ret = ext4fs_get_indir_block(node, &data->indir2,
322
4.13k
        le32_to_cpu(data->indir1.data[rblock / perblock]));
323
4.13k
    if (ret)
324
271
      return ret;
325
326
3.86k
    blknr = le32_to_cpu(data->indir2.data[rblock % perblock]);
327
8.68k
  } else {
328
    /* Triple indirect. */
329
8.68k
    rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
330
8.68k
              (blksz / 4 * blksz / 4));
331
8.68k
    perblock_child = blksz / 4;
332
8.68k
    perblock_parent = ((blksz / 4) * (blksz / 4));
333
334
    /* Beyond what the triple indirect block can map */
335
8.68k
    if (rblock / perblock_parent >= perblock_child)
336
648
      return -EINVAL;
337
338
8.03k
    ret = ext4fs_get_indir_block(node, &data->indir1,
339
8.03k
        le32_to_cpu(inode->b.blocks.triple_indir_block));
340
8.03k
    if (ret)
341
2.78k
      return ret;
342
343
5.25k
    ret = ext4fs_get_indir_block(node, &data->indir2,
344
5.25k
        le32_to_cpu(data->indir1.data[rblock / perblock_parent]));
345
5.25k
    if (ret)
346
262
      return ret;
347
348
4.98k
    ret = ext4fs_get_indir_block(node, &data->indir3,
349
4.98k
        le32_to_cpu(data->indir2.data[(rblock / perblock_child) %
350
4.98k
                    perblock_child]));
351
4.98k
    if (ret)
352
292
      return ret;
353
354
4.69k
    blknr = le32_to_cpu(data->indir3.data[rblock % perblock_child]);
355
4.69k
  }
356
357
830k
  return blknr;
358
839k
}
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.32k
{
589
1.32k
  struct ext2_data *data;
590
1.32k
  ssize_t ret;
591
1.32k
  int blksz;
592
593
1.32k
  data = zalloc(sizeof(struct ext2_data));
594
1.32k
  if (!data)
595
0
    return -ENOMEM;
596
597
  /* Read the superblock. */
598
1.32k
  ret = ext4fs_devread(fs, 1 * 2, 0, sizeof(struct ext2_sblock),
599
1.32k
        (char *)&data->sblock);
600
1.32k
  if (ret)
601
30
    goto fail;
602
603
  /* Make sure this is an ext2 filesystem. */
604
1.29k
  if (le16_to_cpu(data->sblock.magic) != EXT2_SUPER_MAGIC) {
605
21
    ret = -EINVAL;
606
21
    goto fail;
607
21
  }
608
609
1.27k
  if (le32_to_cpu(data->sblock.log2_block_size) >
610
1.27k
      EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE) {
611
34
    dev_err(fs->dev, "invalid block size %u\n",
612
34
      le32_to_cpu(data->sblock.log2_block_size));
613
34
    ret = -EINVAL;
614
34
    goto fail;
615
34
  }
616
617
1.23k
  if (le32_to_cpu(data->sblock.revision_level) == 0) {
618
1.00k
    fs->inodesz = EXT2_GOOD_OLD_INODE_SIZE;
619
1.00k
    fs->gdsize = EXT4_MIN_DESC_SIZE;
620
1.00k
  } else {
621
230
    debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
622
230
          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
230
    fs->inodesz = le16_to_cpu(data->sblock.inode_size);
627
230
    if (fs->inodesz < EXT2_GOOD_OLD_INODE_SIZE ||
628
222
        fs->inodesz > EXT2_BLOCK_SIZE(data) ||
629
208
        !is_power_of_2(fs->inodesz)) {
630
33
      dev_err(fs->dev, "unsupported inode size %u\n",
631
33
        fs->inodesz);
632
33
      ret = -EINVAL;
633
33
      goto fail;
634
33
    }
635
636
197
    fs->gdsize = EXT4_MIN_DESC_SIZE;
637
638
197
    if (le32_to_cpu(data->sblock.feature_incompat) &
639
197
        EXT4_FEATURE_INCOMPAT_64BIT) {
640
41
      fs->gdsize = le16_to_cpu(data->sblock.descriptor_size);
641
642
41
      if (fs->gdsize < EXT4_MIN_DESC_SIZE_64BIT ||
643
35
          fs->gdsize > EXT4_MAX_DESC_SIZE ||
644
26
          !is_power_of_2(fs->gdsize)) {
645
26
        dev_err(fs->dev, "unsupported descriptor size %u\n",
646
26
          fs->gdsize);
647
26
        ret = -EINVAL;
648
26
        goto fail;
649
26
      }
650
41
    }
651
197
  }
652
653
1.17k
  dev_info(fs->dev, "EXT2 rev %d, inode_size %d, descriptor size %d\n",
654
1.17k
        le32_to_cpu(data->sblock.revision_level),
655
1.17k
        fs->inodesz, fs->gdsize);
656
657
1.17k
  if (!le32_to_cpu(data->sblock.inodes_per_group)) {
658
1
    dev_err(fs->dev, "invalid superblock: inodes_per_group=0\n");
659
1
    ret = -EINVAL;
660
1
    goto fail;
661
1
  }
662
663
1.17k
  data->diropen.data = data;
664
1.17k
  data->diropen.ino = 2;
665
1.17k
  data->diropen.inode_read = 1;
666
1.17k
  data->inode = &data->diropen.inode;
667
1.17k
  data->fs = fs;
668
1.17k
  fs->data = data;
669
670
1.17k
  blksz = EXT2_BLOCK_SIZE(data);
671
672
1.17k
  fs->data->indir1.data = malloc(blksz);
673
1.17k
  fs->data->indir2.data = malloc(blksz);
674
1.17k
  fs->data->indir3.data = malloc(blksz);
675
676
1.17k
  if (!fs->data->indir1.data || !fs->data->indir2.data ||
677
1.17k
      !fs->data->indir3.data) {
678
0
    ret = -ENOMEM;
679
0
    goto fail;
680
0
  }
681
682
1.17k
  fs->data->indir1.blkno = EXT4FS_INDIR_NONE;
683
1.17k
  fs->data->indir2.blkno = EXT4FS_INDIR_NONE;
684
1.17k
  fs->data->indir3.blkno = EXT4FS_INDIR_NONE;
685
686
1.17k
  ret = ext4fs_read_inode(data, 2, data->inode);
687
1.17k
  if (ret)
688
22
    goto fail;
689
690
1.15k
  return 0;
691
167
fail:
692
167
  free(data->indir1.data);
693
167
  free(data->indir2.data);
694
167
  free(data->indir3.data);
695
167
  free(data);
696
167
  fs->data = NULL;
697
698
167
  return ret;
699
1.17k
}
700
701
void ext4fs_umount(struct ext_filesystem *fs)
702
1.15k
{
703
1.15k
  free(fs->data->indir1.data);
704
1.15k
  free(fs->data->indir2.data);
705
1.15k
  free(fs->data->indir3.data);
706
1.15k
  free(fs->data);
707
1.15k
}