blob: 6a26c293ae2d928dd0fa3c5b6929acc0190cc23d [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang152a3332019-06-24 15:22:52 +08002/*
Gao Xiang152a3332019-06-24 15:22:52 +08003 * Copyright (C) 2018-2019 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
6 */
7#include "internal.h"
8#include <asm/unaligned.h>
9#include <trace/events/erofs.h>
10
11int z_erofs_fill_inode(struct inode *inode)
12{
Gao Xianga5876e22019-09-04 10:08:56 +080013 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +080014
Gao Xiang8a765682019-09-04 10:08:54 +080015 if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
Gao Xiang152a3332019-06-24 15:22:52 +080016 vi->z_advise = 0;
17 vi->z_algorithmtype[0] = 0;
18 vi->z_algorithmtype[1] = 0;
Gao Xiangeace9942019-07-31 23:57:48 +080019 vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
Gao Xiang152a3332019-06-24 15:22:52 +080020 vi->z_physical_clusterbits[0] = vi->z_logical_clusterbits;
21 vi->z_physical_clusterbits[1] = vi->z_logical_clusterbits;
Gao Xianga5876e22019-09-04 10:08:56 +080022 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +080023 }
24
25 inode->i_mapping->a_ops = &z_erofs_vle_normalaccess_aops;
26 return 0;
27}
28
29static int fill_inode_lazy(struct inode *inode)
30{
Gao Xianga5876e22019-09-04 10:08:56 +080031 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +080032 struct super_block *const sb = inode->i_sb;
33 int err;
34 erofs_off_t pos;
35 struct page *page;
36 void *kaddr;
37 struct z_erofs_map_header *h;
38
Gao Xianga5876e22019-09-04 10:08:56 +080039 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
Gao Xiang152a3332019-06-24 15:22:52 +080040 return 0;
41
Gao Xianga5876e22019-09-04 10:08:56 +080042 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
Gao Xiang152a3332019-06-24 15:22:52 +080043 return -ERESTARTSYS;
44
45 err = 0;
Gao Xianga5876e22019-09-04 10:08:56 +080046 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
Gao Xiang152a3332019-06-24 15:22:52 +080047 goto out_unlock;
48
Gao Xiang8a765682019-09-04 10:08:54 +080049 DBG_BUGON(vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
Gao Xiang152a3332019-06-24 15:22:52 +080050
51 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
52 vi->xattr_isize, 8);
Gao Xiange655b5b2019-09-04 10:09:03 +080053 page = erofs_get_meta_page(sb, erofs_blknr(pos));
Gao Xiang152a3332019-06-24 15:22:52 +080054 if (IS_ERR(page)) {
55 err = PTR_ERR(page);
56 goto out_unlock;
57 }
58
59 kaddr = kmap_atomic(page);
60
61 h = kaddr + erofs_blkoff(pos);
62 vi->z_advise = le16_to_cpu(h->h_advise);
63 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
64 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
65
66 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080067 erofs_err(sb, "unknown compression format %u for nid %llu, please upgrade kernel",
68 vi->z_algorithmtype[0], vi->nid);
Gao Xiangff784a72019-08-14 18:37:05 +080069 err = -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +080070 goto unmap_done;
71 }
72
73 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
74 vi->z_physical_clusterbits[0] = vi->z_logical_clusterbits +
75 ((h->h_clusterbits >> 3) & 3);
76
77 if (vi->z_physical_clusterbits[0] != LOG_BLOCK_SIZE) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080078 erofs_err(sb, "unsupported physical clusterbits %u for nid %llu, please upgrade kernel",
79 vi->z_physical_clusterbits[0], vi->nid);
Gao Xiangff784a72019-08-14 18:37:05 +080080 err = -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +080081 goto unmap_done;
82 }
83
84 vi->z_physical_clusterbits[1] = vi->z_logical_clusterbits +
85 ((h->h_clusterbits >> 5) & 7);
Gao Xianga5876e22019-09-04 10:08:56 +080086 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +080087unmap_done:
88 kunmap_atomic(kaddr);
89 unlock_page(page);
90 put_page(page);
Gao Xiang152a3332019-06-24 15:22:52 +080091out_unlock:
Gao Xianga5876e22019-09-04 10:08:56 +080092 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +080093 return err;
94}
95
96struct z_erofs_maprecorder {
97 struct inode *inode;
98 struct erofs_map_blocks *map;
99 void *kaddr;
100
101 unsigned long lcn;
102 /* compression extent information gathered */
103 u8 type;
104 u16 clusterofs;
105 u16 delta[2];
106 erofs_blk_t pblk;
107};
108
109static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
110 erofs_blk_t eblk)
111{
112 struct super_block *const sb = m->inode->i_sb;
113 struct erofs_map_blocks *const map = m->map;
114 struct page *mpage = map->mpage;
115
116 if (mpage) {
117 if (mpage->index == eblk) {
118 if (!m->kaddr)
119 m->kaddr = kmap_atomic(mpage);
120 return 0;
121 }
122
123 if (m->kaddr) {
124 kunmap_atomic(m->kaddr);
125 m->kaddr = NULL;
126 }
127 put_page(mpage);
128 }
129
Gao Xiange655b5b2019-09-04 10:09:03 +0800130 mpage = erofs_get_meta_page(sb, eblk);
Gao Xiang152a3332019-06-24 15:22:52 +0800131 if (IS_ERR(mpage)) {
132 map->mpage = NULL;
133 return PTR_ERR(mpage);
134 }
135 m->kaddr = kmap_atomic(mpage);
136 unlock_page(mpage);
137 map->mpage = mpage;
138 return 0;
139}
140
141static int vle_legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
142 unsigned long lcn)
143{
144 struct inode *const inode = m->inode;
Gao Xianga5876e22019-09-04 10:08:56 +0800145 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800146 const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
147 const erofs_off_t pos =
148 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
149 vi->xattr_isize) +
150 lcn * sizeof(struct z_erofs_vle_decompressed_index);
151 struct z_erofs_vle_decompressed_index *di;
152 unsigned int advise, type;
153 int err;
154
155 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
156 if (err)
157 return err;
158
159 m->lcn = lcn;
160 di = m->kaddr + erofs_blkoff(pos);
161
162 advise = le16_to_cpu(di->di_advise);
163 type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
164 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
165 switch (type) {
166 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
167 m->clusterofs = 1 << vi->z_logical_clusterbits;
168 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
169 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
170 break;
171 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
172 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
173 m->clusterofs = le16_to_cpu(di->di_clusterofs);
174 m->pblk = le32_to_cpu(di->di_u.blkaddr);
175 break;
176 default:
177 DBG_BUGON(1);
Gao Xiang382329a2019-08-14 18:37:04 +0800178 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800179 }
180 m->type = type;
181 return 0;
182}
183
184static unsigned int decode_compactedbits(unsigned int lobits,
185 unsigned int lomask,
186 u8 *in, unsigned int pos, u8 *type)
187{
188 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
189 const unsigned int lo = v & lomask;
190
191 *type = (v >> lobits) & 3;
192 return lo;
193}
194
195static int unpack_compacted_index(struct z_erofs_maprecorder *m,
196 unsigned int amortizedshift,
197 unsigned int eofs)
198{
Gao Xianga5876e22019-09-04 10:08:56 +0800199 struct erofs_inode *const vi = EROFS_I(m->inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800200 const unsigned int lclusterbits = vi->z_logical_clusterbits;
201 const unsigned int lomask = (1 << lclusterbits) - 1;
202 unsigned int vcnt, base, lo, encodebits, nblk;
203 int i;
204 u8 *in, type;
205
206 if (1 << amortizedshift == 4)
207 vcnt = 2;
208 else if (1 << amortizedshift == 2 && lclusterbits == 12)
209 vcnt = 16;
210 else
Gao Xiangff784a72019-08-14 18:37:05 +0800211 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800212
213 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
214 base = round_down(eofs, vcnt << amortizedshift);
215 in = m->kaddr + base;
216
217 i = (eofs - base) >> amortizedshift;
218
219 lo = decode_compactedbits(lclusterbits, lomask,
220 in, encodebits * i, &type);
221 m->type = type;
222 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
223 m->clusterofs = 1 << lclusterbits;
224 if (i + 1 != vcnt) {
225 m->delta[0] = lo;
226 return 0;
227 }
228 /*
229 * since the last lcluster in the pack is special,
230 * of which lo saves delta[1] rather than delta[0].
231 * Hence, get delta[0] by the previous lcluster indirectly.
232 */
233 lo = decode_compactedbits(lclusterbits, lomask,
234 in, encodebits * (i - 1), &type);
235 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
236 lo = 0;
237 m->delta[0] = lo + 1;
238 return 0;
239 }
240 m->clusterofs = lo;
241 m->delta[0] = 0;
242 /* figout out blkaddr (pblk) for HEAD lclusters */
243 nblk = 1;
244 while (i > 0) {
245 --i;
246 lo = decode_compactedbits(lclusterbits, lomask,
247 in, encodebits * i, &type);
248 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
249 i -= lo;
250
251 if (i >= 0)
252 ++nblk;
253 }
254 in += (vcnt << amortizedshift) - sizeof(__le32);
255 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
256 return 0;
257}
258
259static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
260 unsigned long lcn)
261{
262 struct inode *const inode = m->inode;
Gao Xianga5876e22019-09-04 10:08:56 +0800263 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800264 const unsigned int lclusterbits = vi->z_logical_clusterbits;
265 const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
266 vi->inode_isize + vi->xattr_isize, 8) +
267 sizeof(struct z_erofs_map_header);
268 const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
269 unsigned int compacted_4b_initial, compacted_2b;
270 unsigned int amortizedshift;
271 erofs_off_t pos;
272 int err;
273
274 if (lclusterbits != 12)
Gao Xiangff784a72019-08-14 18:37:05 +0800275 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800276
277 if (lcn >= totalidx)
278 return -EINVAL;
279
280 m->lcn = lcn;
281 /* used to align to 32-byte (compacted_2b) alignment */
282 compacted_4b_initial = (32 - ebase % 32) / 4;
283 if (compacted_4b_initial == 32 / 4)
284 compacted_4b_initial = 0;
285
286 if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B)
287 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
288 else
289 compacted_2b = 0;
290
291 pos = ebase;
292 if (lcn < compacted_4b_initial) {
293 amortizedshift = 2;
294 goto out;
295 }
296 pos += compacted_4b_initial * 4;
297 lcn -= compacted_4b_initial;
298
299 if (lcn < compacted_2b) {
300 amortizedshift = 1;
301 goto out;
302 }
303 pos += compacted_2b * 2;
304 lcn -= compacted_2b;
305 amortizedshift = 2;
306out:
307 pos += lcn * (1 << amortizedshift);
308 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
309 if (err)
310 return err;
311 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos));
312}
313
314static int vle_load_cluster_from_disk(struct z_erofs_maprecorder *m,
315 unsigned int lcn)
316{
Gao Xianga5876e22019-09-04 10:08:56 +0800317 const unsigned int datamode = EROFS_I(m->inode)->datalayout;
Gao Xiang152a3332019-06-24 15:22:52 +0800318
319 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
320 return vle_legacy_load_cluster_from_disk(m, lcn);
321
322 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
323 return compacted_load_cluster_from_disk(m, lcn);
324
325 return -EINVAL;
326}
327
328static int vle_extent_lookback(struct z_erofs_maprecorder *m,
329 unsigned int lookback_distance)
330{
Gao Xianga5876e22019-09-04 10:08:56 +0800331 struct erofs_inode *const vi = EROFS_I(m->inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800332 struct erofs_map_blocks *const map = m->map;
333 const unsigned int lclusterbits = vi->z_logical_clusterbits;
334 unsigned long lcn = m->lcn;
335 int err;
336
337 if (lcn < lookback_distance) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800338 erofs_err(m->inode->i_sb,
339 "bogus lookback distance @ nid %llu", vi->nid);
Gao Xiang152a3332019-06-24 15:22:52 +0800340 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800341 return -EFSCORRUPTED;
Gao Xiang152a3332019-06-24 15:22:52 +0800342 }
343
344 /* load extent head logical cluster if needed */
345 lcn -= lookback_distance;
346 err = vle_load_cluster_from_disk(m, lcn);
347 if (err)
348 return err;
349
350 switch (m->type) {
351 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800352 if (!m->delta[0]) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800353 erofs_err(m->inode->i_sb,
354 "invalid lookback distance 0 @ nid %llu",
355 vi->nid);
Gao Xiang598bb892019-08-19 18:34:26 +0800356 DBG_BUGON(1);
357 return -EFSCORRUPTED;
358 }
Gao Xiang152a3332019-06-24 15:22:52 +0800359 return vle_extent_lookback(m, m->delta[0]);
360 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
361 map->m_flags &= ~EROFS_MAP_ZIPPED;
362 /* fallthrough */
363 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
364 map->m_la = (lcn << lclusterbits) | m->clusterofs;
365 break;
366 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800367 erofs_err(m->inode->i_sb,
368 "unknown type %u @ lcn %lu of nid %llu",
369 m->type, lcn, vi->nid);
Gao Xiang152a3332019-06-24 15:22:52 +0800370 DBG_BUGON(1);
Gao Xiang382329a2019-08-14 18:37:04 +0800371 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800372 }
373 return 0;
374}
375
376int z_erofs_map_blocks_iter(struct inode *inode,
377 struct erofs_map_blocks *map,
378 int flags)
379{
Gao Xianga5876e22019-09-04 10:08:56 +0800380 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800381 struct z_erofs_maprecorder m = {
382 .inode = inode,
383 .map = map,
384 };
385 int err = 0;
386 unsigned int lclusterbits, endoff;
387 unsigned long long ofs, end;
388
389 trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
390
391 /* when trying to read beyond EOF, leave it unmapped */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800392 if (map->m_la >= inode->i_size) {
Gao Xiang152a3332019-06-24 15:22:52 +0800393 map->m_llen = map->m_la + 1 - inode->i_size;
394 map->m_la = inode->i_size;
395 map->m_flags = 0;
396 goto out;
397 }
398
399 err = fill_inode_lazy(inode);
400 if (err)
401 goto out;
402
403 lclusterbits = vi->z_logical_clusterbits;
404 ofs = map->m_la;
405 m.lcn = ofs >> lclusterbits;
406 endoff = ofs & ((1 << lclusterbits) - 1);
407
408 err = vle_load_cluster_from_disk(&m, m.lcn);
409 if (err)
410 goto unmap_out;
411
412 map->m_flags = EROFS_MAP_ZIPPED; /* by default, compressed */
413 end = (m.lcn + 1ULL) << lclusterbits;
414
415 switch (m.type) {
416 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
417 if (endoff >= m.clusterofs)
418 map->m_flags &= ~EROFS_MAP_ZIPPED;
419 /* fallthrough */
420 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
421 if (endoff >= m.clusterofs) {
422 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
423 break;
424 }
425 /* m.lcn should be >= 1 if endoff < m.clusterofs */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800426 if (!m.lcn) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800427 erofs_err(inode->i_sb,
428 "invalid logical cluster 0 at nid %llu",
429 vi->nid);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800430 err = -EFSCORRUPTED;
Gao Xiang152a3332019-06-24 15:22:52 +0800431 goto unmap_out;
432 }
433 end = (m.lcn << lclusterbits) | m.clusterofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800434 map->m_flags |= EROFS_MAP_FULL_MAPPED;
Gao Xiang152a3332019-06-24 15:22:52 +0800435 m.delta[0] = 1;
436 /* fallthrough */
437 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
438 /* get the correspoinding first chunk */
439 err = vle_extent_lookback(&m, m.delta[0]);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800440 if (err)
Gao Xiang152a3332019-06-24 15:22:52 +0800441 goto unmap_out;
442 break;
443 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800444 erofs_err(inode->i_sb,
445 "unknown type %u @ offset %llu of nid %llu",
446 m.type, ofs, vi->nid);
Gao Xiang382329a2019-08-14 18:37:04 +0800447 err = -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800448 goto unmap_out;
449 }
450
451 map->m_llen = end - map->m_la;
452 map->m_plen = 1 << lclusterbits;
453 map->m_pa = blknr_to_addr(m.pblk);
454 map->m_flags |= EROFS_MAP_MAPPED;
455
456unmap_out:
457 if (m.kaddr)
458 kunmap_atomic(m.kaddr);
459
460out:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800461 erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
462 __func__, map->m_la, map->m_pa,
463 map->m_llen, map->m_plen, map->m_flags);
Gao Xiang152a3332019-06-24 15:22:52 +0800464
465 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
466
467 /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
468 DBG_BUGON(err < 0 && err != -ENOMEM);
469 return err;
470}
471