blob: bd37f53f60cb652a52e1caaa4c64f48b2c6f3a68 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang81781b02018-07-26 20:21:47 +08002/*
Gao Xiang81781b02018-07-26 20:21:47 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiangc5aa9032021-08-20 18:00:19 +08005 * Copyright (C) 2021, Alibaba Cloud
Gao Xiang81781b02018-07-26 20:21:47 +08006 */
7#include "internal.h"
8#include <linux/prefetch.h>
Gao Xiang06252e92021-08-05 08:36:00 +08009#include <linux/dax.h>
Chao Yu13f06f42018-07-26 20:21:55 +080010#include <trace/events/erofs.h>
11
Gao Xiange655b5b2019-09-04 10:09:03 +080012struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr)
Gao Xiang81781b02018-07-26 20:21:47 +080013{
Gao Xiang55252ab2019-09-22 02:43:55 +080014 struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping;
15 struct page *page;
Gao Xiang81781b02018-07-26 20:21:47 +080016
Gao Xiang55252ab2019-09-22 02:43:55 +080017 page = read_cache_page_gfp(mapping, blkaddr,
Gao Xiang618f40e2019-09-04 10:09:12 +080018 mapping_gfp_constraint(mapping, ~__GFP_FS));
Gao Xiang55252ab2019-09-22 02:43:55 +080019 /* should already be PageUptodate */
20 if (!IS_ERR(page))
21 lock_page(page);
22 return page;
Gao Xiang81781b02018-07-26 20:21:47 +080023}
24
Gao Xiangfdf80a42022-01-02 12:00:13 +080025void erofs_unmap_metabuf(struct erofs_buf *buf)
26{
27 if (buf->kmap_type == EROFS_KMAP)
28 kunmap(buf->page);
29 else if (buf->kmap_type == EROFS_KMAP_ATOMIC)
30 kunmap_atomic(buf->base);
31 buf->base = NULL;
32 buf->kmap_type = EROFS_NO_KMAP;
33}
34
35void erofs_put_metabuf(struct erofs_buf *buf)
36{
37 if (!buf->page)
38 return;
39 erofs_unmap_metabuf(buf);
40 put_page(buf->page);
41 buf->page = NULL;
42}
43
44void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
45 erofs_blk_t blkaddr, enum erofs_kmap_type type)
46{
47 struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping;
48 erofs_off_t offset = blknr_to_addr(blkaddr);
49 pgoff_t index = offset >> PAGE_SHIFT;
50 struct page *page = buf->page;
51
52 if (!page || page->index != index) {
53 erofs_put_metabuf(buf);
54 page = read_cache_page_gfp(mapping, index,
55 mapping_gfp_constraint(mapping, ~__GFP_FS));
56 if (IS_ERR(page))
57 return page;
58 /* should already be PageUptodate, no need to lock page */
59 buf->page = page;
60 }
61 if (buf->kmap_type == EROFS_NO_KMAP) {
62 if (type == EROFS_KMAP)
63 buf->base = kmap(page);
64 else if (type == EROFS_KMAP_ATOMIC)
65 buf->base = kmap_atomic(page);
66 buf->kmap_type = type;
67 } else if (buf->kmap_type != type) {
68 DBG_BUGON(1);
69 return ERR_PTR(-EFAULT);
70 }
71 if (type == EROFS_NO_KMAP)
72 return NULL;
73 return buf->base + (offset & ~PAGE_MASK);
74}
75
Gao Xiang81781b02018-07-26 20:21:47 +080076static int erofs_map_blocks_flatmode(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +000077 struct erofs_map_blocks *map,
78 int flags)
Gao Xiang81781b02018-07-26 20:21:47 +080079{
80 erofs_blk_t nblocks, lastblk;
81 u64 offset = map->m_la;
Gao Xianga5876e22019-09-04 10:08:56 +080082 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang8a765682019-09-04 10:08:54 +080083 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
Gao Xiang81781b02018-07-26 20:21:47 +080084
Gao Xiangfdf80a42022-01-02 12:00:13 +080085 nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
Gao Xiang8a765682019-09-04 10:08:54 +080086 lastblk = nblocks - tailendpacking;
Gao Xiang81781b02018-07-26 20:21:47 +080087
Gao Xiang81781b02018-07-26 20:21:47 +080088 /* there is no hole in flatmode */
89 map->m_flags = EROFS_MAP_MAPPED;
Gao Xiang81781b02018-07-26 20:21:47 +080090 if (offset < blknr_to_addr(lastblk)) {
91 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
92 map->m_plen = blknr_to_addr(lastblk) - offset;
Gao Xiang8a765682019-09-04 10:08:54 +080093 } else if (tailendpacking) {
Gao Xiang81781b02018-07-26 20:21:47 +080094 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
95 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
96
97 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
98 vi->xattr_isize + erofs_blkoff(map->m_la);
99 map->m_plen = inode->i_size - offset;
100
Gao Xiang469407a2021-12-09 09:29:18 +0800101 /* inline data should be located in the same meta block */
102 if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800103 erofs_err(inode->i_sb,
104 "inline data cross block boundary @ nid %llu",
105 vi->nid);
Chen Gong9141b602018-09-18 22:27:28 +0800106 DBG_BUGON(1);
Gao Xiang469407a2021-12-09 09:29:18 +0800107 return -EFSCORRUPTED;
Chen Gong9141b602018-09-18 22:27:28 +0800108 }
Gao Xiang81781b02018-07-26 20:21:47 +0800109 map->m_flags |= EROFS_MAP_META;
110 } else {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800111 erofs_err(inode->i_sb,
112 "internal error @ nid: %llu (size %llu), m_la 0x%llx",
113 vi->nid, inode->i_size, map->m_la);
Chen Gong9141b602018-09-18 22:27:28 +0800114 DBG_BUGON(1);
Gao Xiang469407a2021-12-09 09:29:18 +0800115 return -EIO;
Gao Xiang81781b02018-07-26 20:21:47 +0800116 }
Gao Xiang469407a2021-12-09 09:29:18 +0800117 return 0;
Gao Xiang81781b02018-07-26 20:21:47 +0800118}
119
Gao Xiangc5aa9032021-08-20 18:00:19 +0800120static int erofs_map_blocks(struct inode *inode,
121 struct erofs_map_blocks *map, int flags)
122{
123 struct super_block *sb = inode->i_sb;
124 struct erofs_inode *vi = EROFS_I(inode);
125 struct erofs_inode_chunk_index *idx;
Gao Xiangfdf80a42022-01-02 12:00:13 +0800126 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800127 u64 chunknr;
128 unsigned int unit;
129 erofs_off_t pos;
Gao Xiangfdf80a42022-01-02 12:00:13 +0800130 void *kaddr;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800131 int err = 0;
132
Gao Xiang469407a2021-12-09 09:29:18 +0800133 trace_erofs_map_blocks_enter(inode, map, flags);
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800134 map->m_deviceid = 0;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800135 if (map->m_la >= inode->i_size) {
136 /* leave out-of-bound access unmapped */
137 map->m_flags = 0;
138 map->m_plen = 0;
139 goto out;
140 }
141
Gao Xiang469407a2021-12-09 09:29:18 +0800142 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
143 err = erofs_map_blocks_flatmode(inode, map, flags);
144 goto out;
145 }
Gao Xiangc5aa9032021-08-20 18:00:19 +0800146
147 if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
148 unit = sizeof(*idx); /* chunk index */
149 else
150 unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */
151
152 chunknr = map->m_la >> vi->chunkbits;
153 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
154 vi->xattr_isize, unit) + unit * chunknr;
155
Gao Xiangfdf80a42022-01-02 12:00:13 +0800156 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP);
157 if (IS_ERR(kaddr)) {
158 err = PTR_ERR(kaddr);
Gao Xiang469407a2021-12-09 09:29:18 +0800159 goto out;
160 }
Gao Xiangc5aa9032021-08-20 18:00:19 +0800161 map->m_la = chunknr << vi->chunkbits;
162 map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
163 roundup(inode->i_size - map->m_la, EROFS_BLKSIZ));
164
165 /* handle block map */
166 if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
Gao Xiangfdf80a42022-01-02 12:00:13 +0800167 __le32 *blkaddr = kaddr + erofs_blkoff(pos);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800168
169 if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
170 map->m_flags = 0;
171 } else {
172 map->m_pa = blknr_to_addr(le32_to_cpu(*blkaddr));
173 map->m_flags = EROFS_MAP_MAPPED;
174 }
175 goto out_unlock;
176 }
177 /* parse chunk indexes */
Gao Xiangfdf80a42022-01-02 12:00:13 +0800178 idx = kaddr + erofs_blkoff(pos);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800179 switch (le32_to_cpu(idx->blkaddr)) {
180 case EROFS_NULL_ADDR:
181 map->m_flags = 0;
182 break;
183 default:
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800184 map->m_deviceid = le16_to_cpu(idx->device_id) &
185 EROFS_SB(sb)->device_id_mask;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800186 map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr));
187 map->m_flags = EROFS_MAP_MAPPED;
188 break;
189 }
190out_unlock:
Gao Xiangfdf80a42022-01-02 12:00:13 +0800191 erofs_put_metabuf(&buf);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800192out:
Gao Xiang469407a2021-12-09 09:29:18 +0800193 if (!err)
194 map->m_llen = map->m_plen;
195 trace_erofs_map_blocks_exit(inode, map, flags, 0);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800196 return err;
197}
198
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800199int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
200{
201 struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
202 struct erofs_device_info *dif;
203 int id;
204
205 /* primary device by default */
206 map->m_bdev = sb->s_bdev;
207 map->m_daxdev = EROFS_SB(sb)->dax_dev;
208
209 if (map->m_deviceid) {
210 down_read(&devs->rwsem);
211 dif = idr_find(&devs->tree, map->m_deviceid - 1);
212 if (!dif) {
213 up_read(&devs->rwsem);
214 return -ENODEV;
215 }
216 map->m_bdev = dif->bdev;
217 map->m_daxdev = dif->dax_dev;
218 up_read(&devs->rwsem);
219 } else if (devs->extra_devices) {
220 down_read(&devs->rwsem);
221 idr_for_each_entry(&devs->tree, dif, id) {
222 erofs_off_t startoff, length;
223
224 if (!dif->mapped_blkaddr)
225 continue;
226 startoff = blknr_to_addr(dif->mapped_blkaddr);
227 length = blknr_to_addr(dif->blocks);
228
229 if (map->m_pa >= startoff &&
230 map->m_pa < startoff + length) {
231 map->m_pa -= startoff;
232 map->m_bdev = dif->bdev;
233 map->m_daxdev = dif->dax_dev;
234 break;
235 }
236 }
237 up_read(&devs->rwsem);
238 }
239 return 0;
240}
241
Huang Jianana08e67a2021-08-05 08:35:59 +0800242static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
243 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
244{
245 int ret;
246 struct erofs_map_blocks map;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800247 struct erofs_map_dev mdev;
Huang Jianana08e67a2021-08-05 08:35:59 +0800248
249 map.m_la = offset;
250 map.m_llen = length;
251
Gao Xiangc5aa9032021-08-20 18:00:19 +0800252 ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
Huang Jianana08e67a2021-08-05 08:35:59 +0800253 if (ret < 0)
254 return ret;
255
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800256 mdev = (struct erofs_map_dev) {
257 .m_deviceid = map.m_deviceid,
258 .m_pa = map.m_pa,
259 };
260 ret = erofs_map_dev(inode->i_sb, &mdev);
261 if (ret)
262 return ret;
263
264 iomap->bdev = mdev.m_bdev;
265 iomap->dax_dev = mdev.m_daxdev;
Huang Jianana08e67a2021-08-05 08:35:59 +0800266 iomap->offset = map.m_la;
267 iomap->length = map.m_llen;
268 iomap->flags = 0;
Gao Xiang771c9942021-08-05 08:36:01 +0800269 iomap->private = NULL;
Huang Jianana08e67a2021-08-05 08:35:59 +0800270
271 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
272 iomap->type = IOMAP_HOLE;
273 iomap->addr = IOMAP_NULL_ADDR;
274 if (!iomap->length)
275 iomap->length = length;
276 return 0;
277 }
278
Huang Jianana08e67a2021-08-05 08:35:59 +0800279 if (map.m_flags & EROFS_MAP_META) {
Gao Xiangfdf80a42022-01-02 12:00:13 +0800280 void *ptr;
281 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
Gao Xiang771c9942021-08-05 08:36:01 +0800282
283 iomap->type = IOMAP_INLINE;
Gao Xiangfdf80a42022-01-02 12:00:13 +0800284 ptr = erofs_read_metabuf(&buf, inode->i_sb,
285 erofs_blknr(mdev.m_pa), EROFS_KMAP);
286 if (IS_ERR(ptr))
287 return PTR_ERR(ptr);
288 iomap->inline_data = ptr + erofs_blkoff(mdev.m_pa);
289 iomap->private = buf.base;
Gao Xiang771c9942021-08-05 08:36:01 +0800290 } else {
291 iomap->type = IOMAP_MAPPED;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800292 iomap->addr = mdev.m_pa;
Huang Jianana08e67a2021-08-05 08:35:59 +0800293 }
Huang Jianana08e67a2021-08-05 08:35:59 +0800294 return 0;
295}
296
Gao Xiang771c9942021-08-05 08:36:01 +0800297static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
298 ssize_t written, unsigned int flags, struct iomap *iomap)
299{
Gao Xiangfdf80a42022-01-02 12:00:13 +0800300 void *ptr = iomap->private;
Gao Xiang771c9942021-08-05 08:36:01 +0800301
Gao Xiangfdf80a42022-01-02 12:00:13 +0800302 if (ptr) {
303 struct erofs_buf buf = {
304 .page = kmap_to_page(ptr),
305 .base = ptr,
306 .kmap_type = EROFS_KMAP,
307 };
308
Gao Xiang771c9942021-08-05 08:36:01 +0800309 DBG_BUGON(iomap->type != IOMAP_INLINE);
Gao Xiangfdf80a42022-01-02 12:00:13 +0800310 erofs_put_metabuf(&buf);
Gao Xiang771c9942021-08-05 08:36:01 +0800311 } else {
312 DBG_BUGON(iomap->type == IOMAP_INLINE);
313 }
314 return written;
315}
316
Huang Jianana08e67a2021-08-05 08:35:59 +0800317static const struct iomap_ops erofs_iomap_ops = {
318 .iomap_begin = erofs_iomap_begin,
Gao Xiang771c9942021-08-05 08:36:01 +0800319 .iomap_end = erofs_iomap_end,
Huang Jianana08e67a2021-08-05 08:35:59 +0800320};
321
Gao Xiangeadcd6b2021-08-13 13:29:31 +0800322int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
323 u64 start, u64 len)
324{
325 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
326#ifdef CONFIG_EROFS_FS_ZIP
327 return iomap_fiemap(inode, fieinfo, start, len,
328 &z_erofs_iomap_report_ops);
329#else
330 return -EOPNOTSUPP;
331#endif
332 }
333 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
334}
335
Gao Xiang771c9942021-08-05 08:36:01 +0800336/*
337 * since we dont have write or truncate flows, so no inode
338 * locking needs to be held at the moment.
339 */
340static int erofs_readpage(struct file *file, struct page *page)
341{
342 return iomap_readpage(page, &erofs_iomap_ops);
343}
344
345static void erofs_readahead(struct readahead_control *rac)
346{
347 return iomap_readahead(rac, &erofs_iomap_ops);
348}
349
350static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
351{
352 return iomap_bmap(mapping, block, &erofs_iomap_ops);
353}
354
Huang Jianana08e67a2021-08-05 08:35:59 +0800355static int erofs_prepare_dio(struct kiocb *iocb, struct iov_iter *to)
356{
357 struct inode *inode = file_inode(iocb->ki_filp);
358 loff_t align = iocb->ki_pos | iov_iter_count(to) |
359 iov_iter_alignment(to);
360 struct block_device *bdev = inode->i_sb->s_bdev;
361 unsigned int blksize_mask;
362
363 if (bdev)
364 blksize_mask = (1 << ilog2(bdev_logical_block_size(bdev))) - 1;
365 else
366 blksize_mask = (1 << inode->i_blkbits) - 1;
367
368 if (align & blksize_mask)
369 return -EINVAL;
Huang Jianana08e67a2021-08-05 08:35:59 +0800370 return 0;
371}
372
373static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
374{
375 /* no need taking (shared) inode lock since it's a ro filesystem */
376 if (!iov_iter_count(to))
377 return 0;
378
Gao Xiang06252e92021-08-05 08:36:00 +0800379#ifdef CONFIG_FS_DAX
380 if (IS_DAX(iocb->ki_filp->f_mapping->host))
381 return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
382#endif
Huang Jianana08e67a2021-08-05 08:35:59 +0800383 if (iocb->ki_flags & IOCB_DIRECT) {
384 int err = erofs_prepare_dio(iocb, to);
385
386 if (!err)
387 return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
Andreas Gruenbacher4fdccaa2021-07-24 12:26:41 +0200388 NULL, 0, 0);
Huang Jianana08e67a2021-08-05 08:35:59 +0800389 if (err < 0)
390 return err;
391 }
392 return filemap_read(iocb, to, 0);
393}
394
Gao Xiang81781b02018-07-26 20:21:47 +0800395/* for uncompressed (aligned) files and raw access for other files */
396const struct address_space_operations erofs_raw_access_aops = {
Gao Xiang771c9942021-08-05 08:36:01 +0800397 .readpage = erofs_readpage,
398 .readahead = erofs_readahead,
Chao Yu9da681e2019-07-16 17:32:56 +0800399 .bmap = erofs_bmap,
Huang Jianana08e67a2021-08-05 08:35:59 +0800400 .direct_IO = noop_direct_IO,
401};
402
Gao Xiang06252e92021-08-05 08:36:00 +0800403#ifdef CONFIG_FS_DAX
404static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
405 enum page_entry_size pe_size)
406{
407 return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops);
408}
409
410static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
411{
412 return erofs_dax_huge_fault(vmf, PE_SIZE_PTE);
413}
414
415static const struct vm_operations_struct erofs_dax_vm_ops = {
416 .fault = erofs_dax_fault,
417 .huge_fault = erofs_dax_huge_fault,
418};
419
420static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
421{
422 if (!IS_DAX(file_inode(file)))
423 return generic_file_readonly_mmap(file, vma);
424
425 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
426 return -EINVAL;
427
428 vma->vm_ops = &erofs_dax_vm_ops;
429 vma->vm_flags |= VM_HUGEPAGE;
430 return 0;
431}
432#else
433#define erofs_file_mmap generic_file_readonly_mmap
434#endif
435
Huang Jianana08e67a2021-08-05 08:35:59 +0800436const struct file_operations erofs_file_fops = {
437 .llseek = generic_file_llseek,
438 .read_iter = erofs_file_read_iter,
Gao Xiang06252e92021-08-05 08:36:00 +0800439 .mmap = erofs_file_mmap,
Huang Jianana08e67a2021-08-05 08:35:59 +0800440 .splice_read = generic_file_splice_read,
Gao Xiang81781b02018-07-26 20:21:47 +0800441};