Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/fs/jffs2/compr.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * JFFS2 -- Journalling Flash File System, Version 2.
4
 *
5
 * Copyright © 2001-2007 Red Hat, Inc.
6
 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
7
 * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
8
 *        University of Szeged, Hungary
9
 *
10
 * Created by Arjan van de Ven <arjan@infradead.org>
11
 */
12
#define pr_fmt(fmt) "jffs2: " fmt
13
#include <common.h>
14
#include "compr.h"
15
16
/* Available compressors are on this list */
17
static LIST_HEAD(jffs2_compressor_list);
18
19
/* Statistics for blocks stored without compression */
20
static uint32_t none_stat_decompr_blocks=0;
21
22
/*
23
 * Return 1 to use this compression
24
 */
25
int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
26
         uint16_t comprtype, unsigned char *cdata_in,
27
         unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
28
0
{
29
0
  struct jffs2_compressor *this;
30
0
  int ret;
31
32
  /* Older code had a bug where it would write non-zero 'usercompr'
33
     fields. Deal with it. */
34
0
  if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
35
0
    comprtype &= 0xff;
36
37
0
  switch (comprtype & 0xff) {
38
0
  case JFFS2_COMPR_NONE:
39
    /* This should be special-cased elsewhere, but we might as well deal with it */
40
0
    memcpy(data_out, cdata_in, datalen);
41
0
    none_stat_decompr_blocks++;
42
0
    break;
43
0
  case JFFS2_COMPR_ZERO:
44
0
    memset(data_out, 0, datalen);
45
0
    break;
46
0
  default:
47
0
    list_for_each_entry(this, &jffs2_compressor_list, list) {
48
0
      if (comprtype == this->compr) {
49
0
        this->usecount++;
50
0
        ret = this->decompress(cdata_in, data_out, cdatalen, datalen);
51
0
        if (ret) {
52
0
          pr_warn("Decompressor \"%s\" returned %d\n",
53
0
            this->name, ret);
54
0
        }
55
0
        else {
56
0
          this->stat_decompr_blocks++;
57
0
        }
58
0
        this->usecount--;
59
0
        return ret;
60
0
      }
61
0
    }
62
0
    pr_warn("compression type 0x%02x not available\n", comprtype);
63
0
    return -EIO;
64
0
  }
65
0
  return 0;
66
0
}
67
68
int jffs2_register_compressor(struct jffs2_compressor *comp)
69
0
{
70
0
  struct jffs2_compressor *this;
71
72
0
  if (!comp->name) {
73
0
    pr_warn("NULL compressor name at registering JFFS2 compressor. Failed.\n");
74
0
    return -1;
75
0
  }
76
0
  comp->compr_buf_size=0;
77
0
  comp->compr_buf=NULL;
78
0
  comp->usecount=0;
79
0
  comp->stat_compr_orig_size=0;
80
0
  comp->stat_compr_new_size=0;
81
0
  comp->stat_compr_blocks=0;
82
0
  comp->stat_decompr_blocks=0;
83
0
  jffs2_dbg(1, "Registering JFFS2 compressor \"%s\"\n", comp->name);
84
85
0
  list_for_each_entry(this, &jffs2_compressor_list, list) {
86
0
    if (this->priority < comp->priority) {
87
0
      list_add(&comp->list, this->list.prev);
88
0
      goto out;
89
0
    }
90
0
  }
91
0
  list_add_tail(&comp->list, &jffs2_compressor_list);
92
0
out:
93
0
  D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
94
0
    printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
95
0
  })
96
97
98
0
  return 0;
99
0
}
100
101
int jffs2_unregister_compressor(struct jffs2_compressor *comp)
102
0
{
103
0
  D2(struct jffs2_compressor *this);
104
105
0
  jffs2_dbg(1, "Unregistering JFFS2 compressor \"%s\"\n", comp->name);
106
107
0
  if (comp->usecount) {
108
0
    pr_warn("Compressor module is in use. Unregister failed.\n");
109
0
    return -1;
110
0
  }
111
0
  list_del(&comp->list);
112
113
0
  D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
114
0
    printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
115
0
  })
116
0
  return 0;
117
0
}
118
119
int __init jffs2_compressors_init(void)
120
0
{
121
/* Registering compressors */
122
#ifdef CONFIG_FS_JFFS2_COMPRESSION_ZLIB
123
  jffs2_zlib_init();
124
#endif
125
#ifdef CONFIG_FS_JFFS2_COMPRESSION_LZO
126
  jffs2_lzo_init();
127
#endif
128
#ifdef CONFIG_FS_JFFS2_COMPRESSION_RTIME
129
  jffs2_rtime_init();
130
#endif
131
#ifdef CONFIG_FS_JFFS2_COMPRESSION_RUBIN
132
  jffs2_dynrubin_init();
133
#endif
134
/* Setting default compression mode */
135
0
  return 0;
136
0
}
137
138
int jffs2_compressors_exit(void)
139
0
{
140
#ifdef CONFIG_FS_JFFS2_COMPRESSION_ZLIB
141
  jffs2_zlib_exit();
142
#endif
143
#ifdef CONFIG_FS_JFFS2_COMPRESSION_LZO
144
  jffs2_lzo_exit();
145
#endif
146
#ifdef CONFIG_FS_JFFS2_COMPRESSION_RTIME
147
  jffs2_rtime_exit();
148
#endif
149
#ifdef CONFIG_FS_JFFS2_COMPRESSION_RUBIN
150
  jffs2_dynrubin_exit();
151
#endif
152
0
  return 0;
153
0
}