xref: /linux/fs/erofs/data.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2021, Alibaba Cloud
6  */
7 #include "internal.h"
8 #include <linux/filelock.h>
9 #include <linux/sched/mm.h>
10 #include <trace/events/erofs.h>
11 
12 void erofs_unmap_metabuf(struct erofs_buf *buf)
13 {
14 	if (!buf->base)
15 		return;
16 	kunmap_local(buf->base);
17 	buf->base = NULL;
18 }
19 
20 void erofs_put_metabuf(struct erofs_buf *buf)
21 {
22 	if (!buf->page)
23 		return;
24 	erofs_unmap_metabuf(buf);
25 	folio_put(page_folio(buf->page));
26 	buf->page = NULL;
27 }
28 
29 void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
30 {
31 	pgoff_t index = (buf->off + offset) >> PAGE_SHIFT;
32 	struct folio *folio = NULL;
33 
34 	if (buf->page) {
35 		folio = page_folio(buf->page);
36 		if (folio->mapping != buf->mapping)
37 			/* the cached folio belongs to another address_space */
38 			folio = NULL;
39 		else if (folio_file_page(folio, index) != buf->page)
40 			erofs_unmap_metabuf(buf);
41 	}
42 	if (!folio || !folio_contains(folio, index)) {
43 		erofs_put_metabuf(buf);
44 		folio = read_cache_folio(buf->mapping, index,
45 				buf->mc ? erofs_read_meta_folio : NULL, NULL);
46 		if (IS_ERR(folio))
47 			return folio;
48 	}
49 	buf->page = folio_file_page(folio, index);
50 	if (!need_kmap)
51 		return NULL;
52 	if (!buf->base)
53 		buf->base = kmap_local_page(buf->page);
54 	return buf->base + ((buf->off + offset) & ~PAGE_MASK);
55 }
56 
57 int erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb,
58 		       bool in_metabox)
59 {
60 	struct erofs_sb_info *sbi = EROFS_SB(sb);
61 
62 	buf->mc = false;
63 	buf->off = 0;
64 	if (in_metabox) {
65 		if (unlikely(!sbi->metabox_inode))
66 			return -EFSCORRUPTED;
67 		buf->mapping = sbi->metabox_inode->i_mapping;
68 		return 0;
69 	}
70 	if (erofs_is_fileio_mode(sbi)) {
71 		buf->mapping = sbi->managed_cache->i_mapping;
72 		buf->mc = true;
73 	} else {
74 		buf->off = sbi->dif0.fsoff;
75 		buf->mapping = sb->s_bdev->bd_mapping;
76 	}
77 	return 0;
78 }
79 
80 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
81 			 erofs_off_t offset, bool in_metabox)
82 {
83 	int err;
84 
85 	err = erofs_init_metabuf(buf, sb, in_metabox);
86 	if (err)
87 		return ERR_PTR(err);
88 	return erofs_bread(buf, offset, true);
89 }
90 
91 static int erofs_map_chunks(struct inode *inode, struct erofs_map_blocks *map)
92 {
93 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
94 	struct super_block *sb = inode->i_sb;
95 	struct erofs_inode *vi = EROFS_I(inode);
96 	struct erofs_inode_chunk_index *idx;
97 	unsigned int unit = vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES ?
98 		sizeof(*idx) : EROFS_BLOCK_MAP_ENTRY_SIZE;
99 	erofs_blk_t addrmask = (vi->chunkformat & EROFS_CHUNK_FORMAT_48BIT) ?
100 		BIT_ULL(48) - 1 : BIT_ULL(32) - 1;
101 	u64 nr = map->m_la >> vi->chunkbits, chunksize = 1ULL << vi->chunkbits;
102 	erofs_off_t pos = ALIGN(erofs_iloc(inode) + vi->inode_isize +
103 				vi->xattr_isize, unit) + unit * nr;
104 	/* m_llen will be clamped to EOF in the end */
105 	erofs_off_t endpos = round_up(pos + 1, sb->s_blocksize);
106 	u64 last, addr;
107 
108 	idx = erofs_read_metabuf(&buf, sb, pos, erofs_inode_in_metabox(inode));
109 	if (IS_ERR(idx))
110 		return PTR_ERR(idx);
111 
112 	map->m_la = nr << vi->chunkbits;
113 	map->m_llen = 0;
114 	nr = 0;
115 	do {
116 		if (unit == EROFS_BLOCK_MAP_ENTRY_SIZE) {
117 			addr = le32_to_cpu(((__le32 *)idx)[nr]);
118 			if (addr == (u32)EROFS_NULL_ADDR)
119 				addr = EROFS_NULL_ADDR;
120 		} else {
121 			addr = (((u64)le16_to_cpu(idx[nr].startblk_hi) << 32) |
122 				le32_to_cpu(idx[nr].startblk_lo)) & addrmask;
123 			if (addr ^ (EROFS_NULL_ADDR & addrmask))
124 				addr |= (u64)(le16_to_cpu(idx[nr].device_id) &
125 					EROFS_SB(sb)->device_id_mask) << 48;
126 			else
127 				addr = EROFS_NULL_ADDR;
128 		}
129 		if (!nr) {
130 			last = addr;
131 			continue;
132 		}
133 		/* expand and account the prior chunk here */
134 		map->m_llen += chunksize;
135 		if (last != EROFS_NULL_ADDR)
136 			last += erofs_blknr(sb, chunksize);
137 	} while (addr == last && pos + (++nr) * unit < endpos);
138 
139 	if (last != EROFS_NULL_ADDR) {
140 		map->m_pa = erofs_pos(sb, last & addrmask) - map->m_llen;
141 		map->m_deviceid = last >> 48;
142 		map->m_flags = EROFS_MAP_MAPPED;
143 	}
144 	if (addr == last)
145 		map->m_llen += chunksize;
146 	map->m_llen = min_t(erofs_off_t, map->m_llen,
147 			round_up(inode->i_size - map->m_la, sb->s_blocksize));
148 	erofs_put_metabuf(&buf);
149 	return 0;
150 }
151 
152 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map)
153 {
154 	struct super_block *sb = inode->i_sb;
155 	struct erofs_inode *vi = EROFS_I(inode);
156 	bool tailinline = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
157 	erofs_off_t pos;
158 	int err = 0;
159 
160 	trace_erofs_map_blocks_enter(inode, map, 0);
161 	map->m_deviceid = 0;
162 	map->m_flags = 0;
163 	if (map->m_la >= inode->i_size)
164 		goto out;
165 	if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
166 		err = erofs_map_chunks(inode, map);
167 	} else if (tailinline || vi->startblk != EROFS_NULL_ADDR) {
168 		pos = erofs_pos(sb, erofs_iblks(inode) - tailinline);
169 		map->m_flags = EROFS_MAP_MAPPED;
170 		if (map->m_la < pos) {
171 			map->m_pa = erofs_pos(sb, vi->startblk) + map->m_la;
172 			map->m_llen = pos - map->m_la;
173 		} else {
174 			map->m_pa = erofs_iloc(inode) + vi->inode_isize +
175 				vi->xattr_isize + erofs_blkoff(sb, map->m_la);
176 			map->m_llen = inode->i_size - map->m_la;
177 			map->m_flags |= EROFS_MAP_META;
178 			if (erofs_blkoff(sb, map->m_pa) + map->m_llen >
179 			    sb->s_blocksize) {
180 				erofs_err(sb, "inline data across blocks @ nid %llu", vi->nid);
181 				return -EFSCORRUPTED;
182 			}
183 		}
184 	}
185 out:
186 	map->m_plen = err ? 0 : map->m_llen;
187 	trace_erofs_map_blocks_exit(inode, map, 0, err);
188 	return err;
189 }
190 
191 static void erofs_fill_from_devinfo(struct erofs_map_dev *map,
192 		struct super_block *sb, struct erofs_device_info *dif)
193 {
194 	map->m_sb = sb;
195 	map->m_dif = dif;
196 	map->m_bdev = NULL;
197 	if (dif->file && S_ISBLK(file_inode(dif->file)->i_mode))
198 		map->m_bdev = file_bdev(dif->file);
199 }
200 
201 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
202 {
203 	struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
204 	struct erofs_device_info *dif;
205 	erofs_off_t startoff;
206 	int id;
207 
208 	erofs_fill_from_devinfo(map, sb, &EROFS_SB(sb)->dif0);
209 	map->m_bdev = sb->s_bdev;	/* use s_bdev for the primary device */
210 	if (map->m_deviceid) {
211 		down_read(&devs->rwsem);
212 		dif = idr_find(&devs->tree, map->m_deviceid - 1);
213 		if (!dif) {
214 			up_read(&devs->rwsem);
215 			return -ENODEV;
216 		}
217 		if (devs->flatdev) {
218 			map->m_pa += erofs_pos(sb, dif->uniaddr);
219 			up_read(&devs->rwsem);
220 			return 0;
221 		}
222 		erofs_fill_from_devinfo(map, sb, dif);
223 		up_read(&devs->rwsem);
224 	} else if (devs->extra_devices && !devs->flatdev) {
225 		down_read(&devs->rwsem);
226 		idr_for_each_entry(&devs->tree, dif, id) {
227 			if (!dif->uniaddr)
228 				continue;
229 
230 			startoff = erofs_pos(sb, dif->uniaddr);
231 			if (map->m_pa >= startoff &&
232 			    map->m_pa < startoff + erofs_pos(sb, dif->blocks)) {
233 				map->m_pa -= startoff;
234 				erofs_fill_from_devinfo(map, sb, dif);
235 				break;
236 			}
237 		}
238 		up_read(&devs->rwsem);
239 	}
240 	return 0;
241 }
242 
243 /*
244  * bit 30: I/O error occurred on this folio
245  * bit 29: CPU has dirty data in D-cache (needs aliasing handling);
246  * bit 0 - 28: remaining parts to complete this folio, biased by 1 so that
247  *	       ->private stays non-NULL while the folio is attached
248  */
249 #define EROFS_ONLINEFOLIO_EIO		30
250 #define EROFS_ONLINEFOLIO_DIRTY		29
251 #define EROFS_ONLINEFOLIO_COUNT_MASK	(BIT(EROFS_ONLINEFOLIO_DIRTY) - 1)
252 #define EROFS_ONLINEFOLIO_BIAS		1
253 
254 void erofs_onlinefolio_init(struct folio *folio)
255 {
256 	union {
257 		atomic_t o;
258 		void *v;
259 	} u = { .o = ATOMIC_INIT(1 + EROFS_ONLINEFOLIO_BIAS) };
260 
261 	/* valid only if file-backed folio is locked */
262 	folio_attach_private(folio, u.v);
263 }
264 
265 void erofs_onlinefolio_split(struct folio *folio)
266 {
267 	atomic_inc((atomic_t *)&folio->private);
268 }
269 
270 void erofs_onlinefolio_end(struct folio *folio, int err, bool dirty)
271 {
272 	int orig, v;
273 
274 	do {
275 		orig = atomic_read((atomic_t *)&folio->private);
276 		DBG_BUGON((orig & EROFS_ONLINEFOLIO_COUNT_MASK) <= EROFS_ONLINEFOLIO_BIAS);
277 		v = dirty << EROFS_ONLINEFOLIO_DIRTY;
278 		v |= (orig - 1) | (!!err << EROFS_ONLINEFOLIO_EIO);
279 	} while (atomic_cmpxchg((atomic_t *)&folio->private, orig, v) != orig);
280 
281 	if ((v & EROFS_ONLINEFOLIO_COUNT_MASK) != EROFS_ONLINEFOLIO_BIAS)
282 		return;
283 	folio_detach_private(folio);
284 	if (v & BIT(EROFS_ONLINEFOLIO_DIRTY))
285 		flush_dcache_folio(folio);
286 	folio_end_read(folio, !(v & BIT(EROFS_ONLINEFOLIO_EIO)));
287 }
288 
289 static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
290 		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
291 {
292 	struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
293 	struct erofs_iomap_iter_ctx *ctx = iter->private;
294 	struct inode *realinode = ctx ? ctx->realinode : inode;
295 	struct super_block *sb = realinode->i_sb;
296 	struct erofs_map_blocks map;
297 	struct erofs_map_dev mdev;
298 	int ret;
299 
300 	map.m_la = offset;
301 	map.m_llen = length;
302 	ret = erofs_map_blocks(realinode, &map);
303 	if (ret < 0)
304 		return ret;
305 
306 	iomap->offset = map.m_la;
307 	iomap->length = map.m_llen;
308 	iomap->flags = 0;
309 	iomap->addr = IOMAP_NULL_ADDR;
310 	iomap->private = NULL;
311 	if (!(map.m_flags & EROFS_MAP_MAPPED)) {
312 		iomap->type = IOMAP_HOLE;
313 		return 0;
314 	}
315 
316 	if (!(map.m_flags & EROFS_MAP_META) || !erofs_inode_in_metabox(realinode)) {
317 		mdev = (struct erofs_map_dev) {
318 			.m_deviceid = map.m_deviceid,
319 			.m_pa = map.m_pa,
320 		};
321 		ret = erofs_map_dev(sb, &mdev);
322 		if (ret)
323 			return ret;
324 
325 		iomap->private = mdev.m_dif;
326 		if (flags & IOMAP_DAX)
327 			iomap->dax_dev = mdev.m_dif->dax_dev;
328 		else
329 			iomap->bdev = mdev.m_bdev;
330 		iomap->addr = mdev.m_dif->fsoff + mdev.m_pa;
331 		if (flags & IOMAP_DAX)
332 			iomap->addr += mdev.m_dif->dax_part_off;
333 	}
334 
335 	if (map.m_flags & EROFS_MAP_META) {
336 		iomap->type = IOMAP_INLINE;
337 		/* read context should read the inlined data */
338 		if (ctx) {
339 			struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
340 			void *ptr;
341 
342 			ptr = erofs_read_metabuf(&buf, sb, map.m_pa,
343 						 erofs_inode_in_metabox(realinode));
344 			if (IS_ERR(ptr))
345 				return PTR_ERR(ptr);
346 			iomap->inline_data = ptr;
347 			ctx->page = buf.page;
348 			ctx->base = buf.base;
349 		}
350 	} else {
351 		iomap->type = IOMAP_MAPPED;
352 	}
353 	return 0;
354 }
355 
356 static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
357 		ssize_t written, unsigned int flags, struct iomap *iomap)
358 {
359 	struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
360 	struct erofs_iomap_iter_ctx *ctx = iter->private;
361 
362 	if (ctx && ctx->base) {
363 		struct erofs_buf buf = {
364 			.page = ctx->page,
365 			.base = ctx->base,
366 		};
367 
368 		DBG_BUGON(iomap->type != IOMAP_INLINE);
369 		erofs_put_metabuf(&buf);
370 		ctx->base = NULL;
371 	}
372 	return written;
373 }
374 
375 static DEFINE_IOMAP_ITER_NEXT_END(erofs_iomap_next, erofs_iomap_begin,
376 				  erofs_iomap_end);
377 
378 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
379 		 u64 start, u64 len)
380 {
381 	if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
382 		if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP))
383 			return -EOPNOTSUPP;
384 		return iomap_fiemap(inode, fieinfo, start, len,
385 				    z_erofs_iomap_next_report);
386 	}
387 	return iomap_fiemap(inode, fieinfo, start, len, erofs_iomap_next);
388 }
389 
390 /*
391  * since we dont have write or truncate flows, so no inode
392  * locking needs to be held at the moment.
393  */
394 static int erofs_read_folio(struct file *file, struct folio *folio)
395 {
396 	bool need_iput;
397 	struct erofs_iomap_iter_ctx iter_ctx = {
398 		.realinode = erofs_real_inode(folio_inode(folio), &need_iput),
399 	};
400 	struct erofs_sb_info *sbi = EROFS_I_SB(iter_ctx.realinode);
401 	struct iomap_read_folio_ctx read_ctx = {
402 		.ops = erofs_is_fileio_mode(sbi) ?
403 			&erofs_fileio_iomap_read_ops : &iomap_bio_read_ops,
404 		.cur_folio = folio,
405 	};
406 
407 	trace_erofs_read_folio(iter_ctx.realinode, folio, true);
408 	iomap_read_folio(erofs_iomap_next, &read_ctx, &iter_ctx);
409 	if (need_iput)
410 		iput(iter_ctx.realinode);
411 	return 0;
412 }
413 
414 static void erofs_readahead(struct readahead_control *rac)
415 {
416 	bool need_iput;
417 	struct erofs_iomap_iter_ctx iter_ctx = {
418 		.realinode = erofs_real_inode(rac->mapping->host, &need_iput),
419 	};
420 	struct erofs_sb_info *sbi = EROFS_I_SB(iter_ctx.realinode);
421 	struct iomap_read_folio_ctx read_ctx = {
422 		.ops = erofs_is_fileio_mode(sbi) ?
423 			&erofs_fileio_iomap_read_ops : &iomap_bio_read_ops,
424 		.rac = rac,
425 	};
426 
427 	trace_erofs_readahead(iter_ctx.realinode, readahead_index(rac),
428 			      readahead_count(rac), true);
429 	iomap_readahead(erofs_iomap_next, &read_ctx, &iter_ctx);
430 	if (need_iput)
431 		iput(iter_ctx.realinode);
432 }
433 
434 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
435 {
436 	return iomap_bmap(mapping, block, erofs_iomap_next);
437 }
438 
439 static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
440 {
441 	struct inode *inode = file_inode(iocb->ki_filp);
442 
443 	/* no need taking (shared) inode lock since it's a ro filesystem */
444 	if (!iov_iter_count(to))
445 		return 0;
446 
447 	if (IS_ENABLED(CONFIG_FS_DAX) && IS_DAX(inode))
448 		return dax_iomap_rw(iocb, to, erofs_iomap_next);
449 
450 	if ((iocb->ki_flags & IOCB_DIRECT) && inode->i_sb->s_bdev) {
451 		struct erofs_iomap_iter_ctx iter_ctx = {
452 			.realinode = inode,
453 		};
454 
455 		return iomap_dio_rw(iocb, to, erofs_iomap_next,
456 				    NULL, 0, &iter_ctx, 0);
457 	}
458 	return filemap_read(iocb, to, 0);
459 }
460 
461 const struct address_space_operations erofs_aops = {
462 	.read_folio = erofs_read_folio,
463 	.readahead = erofs_readahead,
464 	.bmap = erofs_bmap,
465 	.direct_IO = noop_direct_IO,
466 	.release_folio = iomap_release_folio,
467 	.invalidate_folio = iomap_invalidate_folio,
468 };
469 
470 #ifdef CONFIG_FS_DAX
471 static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
472 		unsigned int order)
473 {
474 	return dax_iomap_fault(vmf, order, NULL, NULL, erofs_iomap_next);
475 }
476 
477 static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
478 {
479 	return erofs_dax_huge_fault(vmf, 0);
480 }
481 
482 static const struct vm_operations_struct erofs_dax_vm_ops = {
483 	.fault		= erofs_dax_fault,
484 	.huge_fault	= erofs_dax_huge_fault,
485 };
486 
487 static int erofs_file_mmap_prepare(struct vm_area_desc *desc)
488 {
489 	if (!IS_DAX(file_inode(desc->file)))
490 		return generic_file_readonly_mmap_prepare(desc);
491 
492 	if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT))
493 		return -EINVAL;
494 
495 	desc->vm_ops = &erofs_dax_vm_ops;
496 	vma_desc_set_flags(desc, VMA_HUGEPAGE_BIT);
497 	return 0;
498 }
499 #else
500 #define erofs_file_mmap_prepare	generic_file_readonly_mmap_prepare
501 #endif
502 
503 loff_t erofs_file_llseek(struct file *file, loff_t offset, int whence)
504 {
505 	struct inode *inode = file->f_mapping->host;
506 	iomap_iter_next_fn next = erofs_iomap_next;
507 
508 	if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
509 		if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP))
510 			return generic_file_llseek(file, offset, whence);
511 		next = z_erofs_iomap_next_report;
512 	}
513 
514 	if (whence == SEEK_HOLE)
515 		offset = iomap_seek_hole(inode, offset, next);
516 	else if (whence == SEEK_DATA)
517 		offset = iomap_seek_data(inode, offset, next);
518 	else
519 		return generic_file_llseek(file, offset, whence);
520 
521 	if (offset < 0)
522 		return offset;
523 	return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
524 }
525 
526 const struct file_operations erofs_file_fops = {
527 	.llseek		= erofs_file_llseek,
528 	.read_iter	= erofs_file_read_iter,
529 	.unlocked_ioctl = erofs_ioctl,
530 #ifdef CONFIG_COMPAT
531 	.compat_ioctl   = erofs_compat_ioctl,
532 #endif
533 	.mmap_prepare	= erofs_file_mmap_prepare,
534 	.get_unmapped_area = thp_get_unmapped_area,
535 	.splice_read	= filemap_splice_read,
536 	.setlease	= generic_setlease,
537 };
538