Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2018-2019 HUAWEI, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *             https://www.huawei.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Created by Gao Xiang <gaoxiang25@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <trace/events/erofs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) int z_erofs_fill_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	struct erofs_inode *const vi = EROFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	if (!erofs_sb_has_big_pcluster(sbi) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	    vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 		vi->z_advise = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		vi->z_algorithmtype[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		vi->z_algorithmtype[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	inode->i_mapping->a_ops = &z_erofs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int z_erofs_fill_inode_lazy(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct erofs_inode *const vi = EROFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct super_block *const sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	erofs_off_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	void *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct z_erofs_map_header *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		 * paired with smp_mb() at the end of the function to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		 * fields will only be observed after the bit is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	DBG_BUGON(!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		  vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		    vi->xattr_isize, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	page = erofs_get_meta_page(sb, erofs_blknr(pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	kaddr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	h = kaddr + erofs_blkoff(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	vi->z_advise = le16_to_cpu(h->h_advise);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		erofs_err(sb, "unknown compression format %u for nid %llu, please upgrade kernel",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			  vi->z_algorithmtype[0], vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		goto unmap_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	    vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			    Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			  vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		goto unmap_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			  vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		goto unmap_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* paired with smp_mb() at the beginning of the function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) unmap_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct z_erofs_maprecorder {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct erofs_map_blocks *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	void *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	unsigned long lcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* compression extent information gathered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	u8  type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	u16 clusterofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	u16 delta[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	erofs_blk_t pblk, compressedlcs;
^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) static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				  erofs_blk_t eblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct super_block *const sb = m->inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct erofs_map_blocks *const map = m->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct page *mpage = map->mpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (mpage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (mpage->index == eblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			if (!m->kaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				m->kaddr = kmap_atomic(mpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (m->kaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			kunmap_atomic(m->kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			m->kaddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		put_page(mpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	mpage = erofs_get_meta_page(sb, eblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (IS_ERR(mpage)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		map->mpage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return PTR_ERR(mpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	m->kaddr = kmap_atomic(mpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	unlock_page(mpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	map->mpage = mpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 					 unsigned long lcn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct inode *const inode = m->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct erofs_inode *const vi = EROFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	const erofs_off_t pos =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 					       vi->xattr_isize) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		lcn * sizeof(struct z_erofs_vle_decompressed_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct z_erofs_vle_decompressed_index *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	unsigned int advise, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	err = z_erofs_reload_indexes(m, erofs_blknr(pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	m->lcn = lcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	di = m->kaddr + erofs_blkoff(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	advise = le16_to_cpu(di->di_advise);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		m->clusterofs = 1 << vi->z_logical_clusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			if (!(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			m->compressedlcs = m->delta[0] &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				~Z_EROFS_VLE_DI_D0_CBLKCNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			m->delta[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		m->clusterofs = le16_to_cpu(di->di_clusterofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		m->pblk = le32_to_cpu(di->di_u.blkaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	m->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static unsigned int decode_compactedbits(unsigned int lobits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 					 unsigned int lomask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 					 u8 *in, unsigned int pos, u8 *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	const unsigned int lo = v & lomask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	*type = (v >> lobits) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int unpack_compacted_index(struct z_erofs_maprecorder *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				  unsigned int amortizedshift,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				  unsigned int eofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct erofs_inode *const vi = EROFS_I(m->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	const unsigned int lomask = (1 << lclusterbits) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	unsigned int vcnt, base, lo, encodebits, nblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	u8 *in, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	bool big_pcluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (1 << amortizedshift == 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		vcnt = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	else if (1 << amortizedshift == 2 && lclusterbits == 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		vcnt = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	base = round_down(eofs, vcnt << amortizedshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	in = m->kaddr + base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	i = (eofs - base) >> amortizedshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	lo = decode_compactedbits(lclusterbits, lomask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				  in, encodebits * i, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	m->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		m->clusterofs = 1 << lclusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			if (!big_pcluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			m->delta[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		} else if (i + 1 != (int)vcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			m->delta[0] = lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		 * since the last lcluster in the pack is special,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 * of which lo saves delta[1] rather than delta[0].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 * Hence, get delta[0] by the previous lcluster indirectly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		lo = decode_compactedbits(lclusterbits, lomask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 					  in, encodebits * (i - 1), &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			lo = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			lo = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		m->delta[0] = lo + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	m->clusterofs = lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	m->delta[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	/* figout out blkaddr (pblk) for HEAD lclusters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (!big_pcluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		nblk = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		while (i > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			--i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			lo = decode_compactedbits(lclusterbits, lomask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 						  in, encodebits * i, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				i -= lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			if (i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				++nblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		nblk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		while (i > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			--i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			lo = decode_compactedbits(lclusterbits, lomask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 						  in, encodebits * i, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 				if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 					--i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 					nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				/* bigpcluster shouldn't have plain d0 == 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				if (lo <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 					DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 					return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 				i -= lo - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			++nblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	in += (vcnt << amortizedshift) - sizeof(__le32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 					    unsigned long lcn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct inode *const inode = m->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	struct erofs_inode *const vi = EROFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 					vi->inode_isize + vi->xattr_isize, 8) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		sizeof(struct z_erofs_map_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	unsigned int compacted_4b_initial, compacted_2b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	unsigned int amortizedshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	erofs_off_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (lclusterbits != 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (lcn >= totalidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	m->lcn = lcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	/* used to align to 32-byte (compacted_2b) alignment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	compacted_4b_initial = (32 - ebase % 32) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (compacted_4b_initial == 32 / 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		compacted_4b_initial = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		compacted_2b = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	pos = ebase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (lcn < compacted_4b_initial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		amortizedshift = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	pos += compacted_4b_initial * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	lcn -= compacted_4b_initial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (lcn < compacted_2b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		amortizedshift = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	pos += compacted_2b * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	lcn -= compacted_2b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	amortizedshift = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	pos += lcn * (1 << amortizedshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	err = z_erofs_reload_indexes(m, erofs_blknr(pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 					  unsigned int lcn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	const unsigned int datamode = EROFS_I(m->inode)->datalayout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		return legacy_load_cluster_from_disk(m, lcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (datamode == EROFS_INODE_FLAT_COMPRESSION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return compacted_load_cluster_from_disk(m, lcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				   unsigned int lookback_distance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct erofs_inode *const vi = EROFS_I(m->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	struct erofs_map_blocks *const map = m->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	unsigned long lcn = m->lcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (lcn < lookback_distance) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		erofs_err(m->inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			  "bogus lookback distance @ nid %llu", vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		return -EFSCORRUPTED;
^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) 	/* load extent head logical cluster if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	lcn -= lookback_distance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	err = z_erofs_load_cluster_from_disk(m, lcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	switch (m->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		if (!m->delta[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			erofs_err(m->inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 				  "invalid lookback distance 0 @ nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 				  vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		return z_erofs_extent_lookback(m, m->delta[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		map->m_flags &= ~EROFS_MAP_ZIPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		map->m_la = (lcn << lclusterbits) | m->clusterofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		erofs_err(m->inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			  "unknown type %u @ lcn %lu of nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			  m->type, lcn, vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 					    unsigned int initial_lcn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	struct erofs_inode *const vi = EROFS_I(m->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct erofs_map_blocks *const map = m->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	unsigned long lcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		  m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	if (!(map->m_flags & EROFS_MAP_ZIPPED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		map->m_plen = 1 << lclusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	lcn = m->lcn + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (m->compressedlcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	err = z_erofs_load_cluster_from_disk(m, lcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	 * If the 1st NONHEAD lcluster has already been handled initially w/o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	 * valid compressedlcs, which means at least it mustn't be CBLKCNT, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	 * an internal implemenatation error is detected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	 * The following code can also handle it properly anyway, but let's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	 * BUG_ON in the debugging mode only for developers to notice that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	DBG_BUGON(lcn == initial_lcn &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		  m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	switch (m->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		m->compressedlcs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		if (m->delta[0] != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			goto err_bonus_cblkcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		if (m->compressedlcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		erofs_err(m->inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			  "cannot found CBLKCNT @ lcn %lu of nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			  lcn, vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	map->m_plen = m->compressedlcs << lclusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) err_bonus_cblkcnt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	erofs_err(m->inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		  "bogus CBLKCNT @ lcn %lu of nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		  lcn, vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) int z_erofs_map_blocks_iter(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			    struct erofs_map_blocks *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			    int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	struct erofs_inode *const vi = EROFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	struct z_erofs_maprecorder m = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		.inode = inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		.map = map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	unsigned int lclusterbits, endoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	unsigned long initial_lcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	unsigned long long ofs, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	/* when trying to read beyond EOF, leave it unmapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (map->m_la >= inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		map->m_llen = map->m_la + 1 - inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		map->m_la = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		map->m_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	err = z_erofs_fill_inode_lazy(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	lclusterbits = vi->z_logical_clusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	ofs = map->m_la;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	initial_lcn = ofs >> lclusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	endoff = ofs & ((1 << lclusterbits) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	err = z_erofs_load_cluster_from_disk(&m, initial_lcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		goto unmap_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	map->m_flags = EROFS_MAP_ZIPPED;	/* by default, compressed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	end = (m.lcn + 1ULL) << lclusterbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	switch (m.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		if (endoff >= m.clusterofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			map->m_flags &= ~EROFS_MAP_ZIPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		if (endoff >= m.clusterofs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 			map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		/* m.lcn should be >= 1 if endoff < m.clusterofs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		if (!m.lcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			erofs_err(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 				  "invalid logical cluster 0 at nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 				  vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			goto unmap_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		end = (m.lcn << lclusterbits) | m.clusterofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		map->m_flags |= EROFS_MAP_FULL_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		m.delta[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		/* get the corresponding first chunk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		err = z_erofs_extent_lookback(&m, m.delta[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			goto unmap_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		erofs_err(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			  "unknown type %u @ offset %llu of nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			  m.type, ofs, vi->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		goto unmap_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	map->m_llen = end - map->m_la;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	map->m_pa = blknr_to_addr(m.pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	map->m_flags |= EROFS_MAP_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) unmap_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	if (m.kaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		kunmap_atomic(m.kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		  __func__, map->m_la, map->m_pa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		  map->m_llen, map->m_plen, map->m_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	/* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	DBG_BUGON(err < 0 && err != -ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)