^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * JFFS2 -- Journalling Flash File System, Version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright © 2001-2007 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * University of Szeged, Hungary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Created by Arjan van de Ven <arjan@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * For licensing information, see the file 'LICENCE' in this directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "compr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Available compressors are on this list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static LIST_HEAD(jffs2_compressor_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* Actual compression mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* Statistics for blocks stored without compression */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Return 1 to use this compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int jffs2_is_best_compression(struct jffs2_compressor *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) switch (jffs2_compression_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) case JFFS2_COMPR_MODE_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (bestsize > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) case JFFS2_COMPR_MODE_FAVOURLZO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Shouldn't happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * jffs2_selected_compress:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * @compr: Explicit compression type to use (ie, JFFS2_COMPR_ZLIB).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * If 0, just take the first available compression mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @data_in: Pointer to uncompressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @cpage_out: Pointer to returned pointer to buffer for compressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @datalen: On entry, holds the amount of data available for compression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * On exit, expected to hold the amount of data actually compressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @cdatalen: On entry, holds the amount of space available for compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * data. On exit, expected to hold the actual size of the compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Returns: the compression type used. Zero is used to show that the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * could not be compressed; probably because we couldn't find the requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * compression mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int jffs2_selected_compress(u8 compr, unsigned char *data_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned char **cpage_out, u32 *datalen, u32 *cdatalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct jffs2_compressor *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int err, ret = JFFS2_COMPR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) uint32_t orig_slen, orig_dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) char *output_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) output_buf = kmalloc(*cdatalen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!output_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) pr_warn("No memory for compressor allocation. Compression failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) orig_slen = *datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) orig_dlen = *cdatalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) list_for_each_entry(this, &jffs2_compressor_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* Skip decompress-only and disabled modules */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!this->compress || this->disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Skip if not the desired compression type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (compr && (compr != this->compr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Either compression type was unspecified, or we found our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * compressor; either way, we're good to go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) this->usecount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *datalen = orig_slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *cdatalen = orig_dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) err = this->compress(data_in, output_buf, datalen, cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) this->usecount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = this->compr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) this->stat_compr_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) this->stat_compr_orig_size += *datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) this->stat_compr_new_size += *cdatalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (ret == JFFS2_COMPR_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) kfree(output_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *cpage_out = output_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* jffs2_compress:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * @data_in: Pointer to uncompressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @cpage_out: Pointer to returned pointer to buffer for compressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @datalen: On entry, holds the amount of data available for compression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * On exit, expected to hold the amount of data actually compressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @cdatalen: On entry, holds the amount of space available for compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * data. On exit, expected to hold the actual size of the compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Returns: Lower byte to be stored with data indicating compression type used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Zero is used to show that the data could not be compressed - the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * compressed version was actually larger than the original.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Upper byte will be used later. (soon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * If the cdata buffer isn't large enough to hold all the uncompressed data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * jffs2_compress should compress as much as will fit, and should set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * *datalen accordingly to show the amount of data which were compressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) unsigned char *data_in, unsigned char **cpage_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) uint32_t *datalen, uint32_t *cdatalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int ret = JFFS2_COMPR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int mode, compr_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct jffs2_compressor *this, *best=NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) unsigned char *output_buf = NULL, *tmp_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) uint32_t orig_slen, orig_dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) uint32_t best_slen=0, best_dlen=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (c->mount_opts.override_compr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) mode = c->mount_opts.compr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mode = jffs2_compression_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) case JFFS2_COMPR_MODE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) case JFFS2_COMPR_MODE_PRIORITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret = jffs2_selected_compress(0, data_in, cpage_out, datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) case JFFS2_COMPR_MODE_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) case JFFS2_COMPR_MODE_FAVOURLZO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) orig_slen = *datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) orig_dlen = *cdatalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) list_for_each_entry(this, &jffs2_compressor_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Skip decompress-only backwards-compatibility and disabled modules */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if ((!this->compress)||(this->disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Allocating memory for output buffer if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if ((this->compr_buf_size < orig_slen) && (this->compr_buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) kfree(this->compr_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) this->compr_buf_size=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) this->compr_buf=NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (!this->compr_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) tmp_buf = kmalloc(orig_slen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (!tmp_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) pr_warn("No memory for compressor allocation. (%d bytes)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) orig_slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) this->compr_buf = tmp_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) this->compr_buf_size = orig_slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) this->usecount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) *datalen = orig_slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) *cdatalen = orig_dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) this->usecount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!compr_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) && (*cdatalen < *datalen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) best_dlen = *cdatalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) best_slen = *datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) best = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (best_dlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) *cdatalen = best_dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *datalen = best_slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) output_buf = best->compr_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) best->compr_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) best->compr_buf_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) best->stat_compr_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) best->stat_compr_orig_size += best_slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) best->stat_compr_new_size += best_dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) ret = best->compr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) *cpage_out = output_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) case JFFS2_COMPR_MODE_FORCELZO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ret = jffs2_selected_compress(JFFS2_COMPR_LZO, data_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) cpage_out, datalen, cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) case JFFS2_COMPR_MODE_FORCEZLIB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ret = jffs2_selected_compress(JFFS2_COMPR_ZLIB, data_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) cpage_out, datalen, cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) pr_err("unknown compression mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (ret == JFFS2_COMPR_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *cpage_out = data_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) *datalen = *cdatalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) none_stat_compr_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) none_stat_compr_size += *datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) uint16_t comprtype, unsigned char *cdata_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct jffs2_compressor *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* Older code had a bug where it would write non-zero 'usercompr'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) fields. Deal with it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) comprtype &= 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) switch (comprtype & 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) case JFFS2_COMPR_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* This should be special-cased elsewhere, but we might as well deal with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) memcpy(data_out, cdata_in, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) none_stat_decompr_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) case JFFS2_COMPR_ZERO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) memset(data_out, 0, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) list_for_each_entry(this, &jffs2_compressor_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (comprtype == this->compr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) this->usecount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) ret = this->decompress(cdata_in, data_out, cdatalen, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) pr_warn("Decompressor \"%s\" returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) this->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) this->stat_decompr_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) this->usecount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) pr_warn("compression type 0x%02x not available\n", comprtype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int jffs2_register_compressor(struct jffs2_compressor *comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct jffs2_compressor *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!comp->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) pr_warn("NULL compressor name at registering JFFS2 compressor. Failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) comp->compr_buf_size=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) comp->compr_buf=NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) comp->usecount=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) comp->stat_compr_orig_size=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) comp->stat_compr_new_size=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) comp->stat_compr_blocks=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) comp->stat_decompr_blocks=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) jffs2_dbg(1, "Registering JFFS2 compressor \"%s\"\n", comp->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) list_for_each_entry(this, &jffs2_compressor_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (this->priority < comp->priority) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) list_add(&comp->list, this->list.prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) list_add_tail(&comp->list, &jffs2_compressor_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int jffs2_unregister_compressor(struct jffs2_compressor *comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) D2(struct jffs2_compressor *this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) jffs2_dbg(1, "Unregistering JFFS2 compressor \"%s\"\n", comp->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) spin_lock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (comp->usecount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) pr_warn("Compressor module is in use. Unregister failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) list_del(&comp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) spin_unlock(&jffs2_compressor_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (orig != comprbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) kfree(comprbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int __init jffs2_compressors_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* Registering compressors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) #ifdef CONFIG_JFFS2_ZLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) jffs2_zlib_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) #ifdef CONFIG_JFFS2_RTIME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) jffs2_rtime_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) #ifdef CONFIG_JFFS2_RUBIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) jffs2_rubinmips_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) jffs2_dynrubin_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) #ifdef CONFIG_JFFS2_LZO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) jffs2_lzo_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /* Setting default compression mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) #ifdef CONFIG_JFFS2_CMODE_NONE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) jffs2_dbg(1, "default compression mode: none\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #ifdef CONFIG_JFFS2_CMODE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) jffs2_dbg(1, "default compression mode: size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) jffs2_dbg(1, "default compression mode: favourlzo\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) jffs2_dbg(1, "default compression mode: priority\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) int jffs2_compressors_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* Unregistering compressors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) #ifdef CONFIG_JFFS2_LZO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) jffs2_lzo_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) #ifdef CONFIG_JFFS2_RUBIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) jffs2_dynrubin_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) jffs2_rubinmips_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) #ifdef CONFIG_JFFS2_RTIME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) jffs2_rtime_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) #ifdef CONFIG_JFFS2_ZLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) jffs2_zlib_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }