blob: fa7ddb7ad98008722b45da5b9160638bb0cd03be [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 Xiangfdf80a42022-01-02 12:00:13 +080012void erofs_unmap_metabuf(struct erofs_buf *buf)
13{
14 if (buf->kmap_type == EROFS_KMAP)
15 kunmap(buf->page);
16 else if (buf->kmap_type == EROFS_KMAP_ATOMIC)
17 kunmap_atomic(buf->base);
18 buf->base = NULL;
19 buf->kmap_type = EROFS_NO_KMAP;
20}
21
22void erofs_put_metabuf(struct erofs_buf *buf)
23{
24 if (!buf->page)
25 return;
26 erofs_unmap_metabuf(buf);
27 put_page(buf->page);
28 buf->page = NULL;
29}
30
31void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
32 erofs_blk_t blkaddr, enum erofs_kmap_type type)
33{
34 struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping;
35 erofs_off_t offset = blknr_to_addr(blkaddr);
36 pgoff_t index = offset >> PAGE_SHIFT;
37 struct page *page = buf->page;
38
39 if (!page || page->index != index) {
40 erofs_put_metabuf(buf);
41 page = read_cache_page_gfp(mapping, index,
42 mapping_gfp_constraint(mapping, ~__GFP_FS));
43 if (IS_ERR(page))
44 return page;
45 /* should already be PageUptodate, no need to lock page */
46 buf->page = page;
47 }
48 if (buf->kmap_type == EROFS_NO_KMAP) {
49 if (type == EROFS_KMAP)
50 buf->base = kmap(page);
51 else if (type == EROFS_KMAP_ATOMIC)
52 buf->base = kmap_atomic(page);
53 buf->kmap_type = type;
54 } else if (buf->kmap_type != type) {
55 DBG_BUGON(1);
56 return ERR_PTR(-EFAULT);
57 }
58 if (type == EROFS_NO_KMAP)
59 return NULL;
60 return buf->base + (offset & ~PAGE_MASK);
61}
62
Gao Xiang81781b02018-07-26 20:21:47 +080063static int erofs_map_blocks_flatmode(struct inode *inode,
Bhagyashri P. Digholef0950b02018-11-05 19:48:38 +000064 struct erofs_map_blocks *map,
65 int flags)
Gao Xiang81781b02018-07-26 20:21:47 +080066{
67 erofs_blk_t nblocks, lastblk;
68 u64 offset = map->m_la;
Gao Xianga5876e22019-09-04 10:08:56 +080069 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang8a765682019-09-04 10:08:54 +080070 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
Gao Xiang81781b02018-07-26 20:21:47 +080071
Gao Xiangfdf80a42022-01-02 12:00:13 +080072 nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
Gao Xiang8a765682019-09-04 10:08:54 +080073 lastblk = nblocks - tailendpacking;
Gao Xiang81781b02018-07-26 20:21:47 +080074
Gao Xiang81781b02018-07-26 20:21:47 +080075 /* there is no hole in flatmode */
76 map->m_flags = EROFS_MAP_MAPPED;
Gao Xiang81781b02018-07-26 20:21:47 +080077 if (offset < blknr_to_addr(lastblk)) {
78 map->m_pa = blknr_to_addr(vi->raw_blkaddr) + map->m_la;
79 map->m_plen = blknr_to_addr(lastblk) - offset;
Gao Xiang8a765682019-09-04 10:08:54 +080080 } else if (tailendpacking) {
Gao Xiang81781b02018-07-26 20:21:47 +080081 /* 2 - inode inline B: inode, [xattrs], inline last blk... */
82 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
83
84 map->m_pa = iloc(sbi, vi->nid) + vi->inode_isize +
85 vi->xattr_isize + erofs_blkoff(map->m_la);
86 map->m_plen = inode->i_size - offset;
87
Gao Xiang469407a2021-12-09 09:29:18 +080088 /* inline data should be located in the same meta block */
89 if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080090 erofs_err(inode->i_sb,
91 "inline data cross block boundary @ nid %llu",
92 vi->nid);
Chen Gong9141b602018-09-18 22:27:28 +080093 DBG_BUGON(1);
Gao Xiang469407a2021-12-09 09:29:18 +080094 return -EFSCORRUPTED;
Chen Gong9141b602018-09-18 22:27:28 +080095 }
Gao Xiang81781b02018-07-26 20:21:47 +080096 map->m_flags |= EROFS_MAP_META;
97 } else {
Gao Xiang4f761fa2019-09-04 10:09:09 +080098 erofs_err(inode->i_sb,
99 "internal error @ nid: %llu (size %llu), m_la 0x%llx",
100 vi->nid, inode->i_size, map->m_la);
Chen Gong9141b602018-09-18 22:27:28 +0800101 DBG_BUGON(1);
Gao Xiang469407a2021-12-09 09:29:18 +0800102 return -EIO;
Gao Xiang81781b02018-07-26 20:21:47 +0800103 }
Gao Xiang469407a2021-12-09 09:29:18 +0800104 return 0;
Gao Xiang81781b02018-07-26 20:21:47 +0800105}
106
Gao Xiangc5aa9032021-08-20 18:00:19 +0800107static int erofs_map_blocks(struct inode *inode,
108 struct erofs_map_blocks *map, int flags)
109{
110 struct super_block *sb = inode->i_sb;
111 struct erofs_inode *vi = EROFS_I(inode);
112 struct erofs_inode_chunk_index *idx;
Gao Xiangfdf80a42022-01-02 12:00:13 +0800113 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800114 u64 chunknr;
115 unsigned int unit;
116 erofs_off_t pos;
Gao Xiangfdf80a42022-01-02 12:00:13 +0800117 void *kaddr;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800118 int err = 0;
119
Gao Xiang469407a2021-12-09 09:29:18 +0800120 trace_erofs_map_blocks_enter(inode, map, flags);
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800121 map->m_deviceid = 0;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800122 if (map->m_la >= inode->i_size) {
123 /* leave out-of-bound access unmapped */
124 map->m_flags = 0;
125 map->m_plen = 0;
126 goto out;
127 }
128
Gao Xiang469407a2021-12-09 09:29:18 +0800129 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) {
130 err = erofs_map_blocks_flatmode(inode, map, flags);
131 goto out;
132 }
Gao Xiangc5aa9032021-08-20 18:00:19 +0800133
134 if (vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
135 unit = sizeof(*idx); /* chunk index */
136 else
137 unit = EROFS_BLOCK_MAP_ENTRY_SIZE; /* block map */
138
139 chunknr = map->m_la >> vi->chunkbits;
140 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
141 vi->xattr_isize, unit) + unit * chunknr;
142
Gao Xiangfdf80a42022-01-02 12:00:13 +0800143 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP);
144 if (IS_ERR(kaddr)) {
145 err = PTR_ERR(kaddr);
Gao Xiang469407a2021-12-09 09:29:18 +0800146 goto out;
147 }
Gao Xiangc5aa9032021-08-20 18:00:19 +0800148 map->m_la = chunknr << vi->chunkbits;
149 map->m_plen = min_t(erofs_off_t, 1UL << vi->chunkbits,
150 roundup(inode->i_size - map->m_la, EROFS_BLKSIZ));
151
152 /* handle block map */
153 if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
Gao Xiangfdf80a42022-01-02 12:00:13 +0800154 __le32 *blkaddr = kaddr + erofs_blkoff(pos);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800155
156 if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
157 map->m_flags = 0;
158 } else {
159 map->m_pa = blknr_to_addr(le32_to_cpu(*blkaddr));
160 map->m_flags = EROFS_MAP_MAPPED;
161 }
162 goto out_unlock;
163 }
164 /* parse chunk indexes */
Gao Xiangfdf80a42022-01-02 12:00:13 +0800165 idx = kaddr + erofs_blkoff(pos);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800166 switch (le32_to_cpu(idx->blkaddr)) {
167 case EROFS_NULL_ADDR:
168 map->m_flags = 0;
169 break;
170 default:
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800171 map->m_deviceid = le16_to_cpu(idx->device_id) &
172 EROFS_SB(sb)->device_id_mask;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800173 map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr));
174 map->m_flags = EROFS_MAP_MAPPED;
175 break;
176 }
177out_unlock:
Gao Xiangfdf80a42022-01-02 12:00:13 +0800178 erofs_put_metabuf(&buf);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800179out:
Gao Xiang469407a2021-12-09 09:29:18 +0800180 if (!err)
181 map->m_llen = map->m_plen;
182 trace_erofs_map_blocks_exit(inode, map, flags, 0);
Gao Xiangc5aa9032021-08-20 18:00:19 +0800183 return err;
184}
185
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800186int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
187{
188 struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
189 struct erofs_device_info *dif;
190 int id;
191
192 /* primary device by default */
193 map->m_bdev = sb->s_bdev;
194 map->m_daxdev = EROFS_SB(sb)->dax_dev;
Christoph Hellwigde205112021-11-29 11:22:00 +0100195 map->m_dax_part_off = EROFS_SB(sb)->dax_part_off;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800196
197 if (map->m_deviceid) {
198 down_read(&devs->rwsem);
199 dif = idr_find(&devs->tree, map->m_deviceid - 1);
200 if (!dif) {
201 up_read(&devs->rwsem);
202 return -ENODEV;
203 }
204 map->m_bdev = dif->bdev;
205 map->m_daxdev = dif->dax_dev;
Christoph Hellwigde205112021-11-29 11:22:00 +0100206 map->m_dax_part_off = dif->dax_part_off;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800207 up_read(&devs->rwsem);
208 } else if (devs->extra_devices) {
209 down_read(&devs->rwsem);
210 idr_for_each_entry(&devs->tree, dif, id) {
211 erofs_off_t startoff, length;
212
213 if (!dif->mapped_blkaddr)
214 continue;
215 startoff = blknr_to_addr(dif->mapped_blkaddr);
216 length = blknr_to_addr(dif->blocks);
217
218 if (map->m_pa >= startoff &&
219 map->m_pa < startoff + length) {
220 map->m_pa -= startoff;
221 map->m_bdev = dif->bdev;
222 map->m_daxdev = dif->dax_dev;
Christoph Hellwigde205112021-11-29 11:22:00 +0100223 map->m_dax_part_off = dif->dax_part_off;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800224 break;
225 }
226 }
227 up_read(&devs->rwsem);
228 }
229 return 0;
230}
231
Huang Jianana08e67a2021-08-05 08:35:59 +0800232static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
233 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
234{
235 int ret;
236 struct erofs_map_blocks map;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800237 struct erofs_map_dev mdev;
Huang Jianana08e67a2021-08-05 08:35:59 +0800238
239 map.m_la = offset;
240 map.m_llen = length;
241
Gao Xiangc5aa9032021-08-20 18:00:19 +0800242 ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
Huang Jianana08e67a2021-08-05 08:35:59 +0800243 if (ret < 0)
244 return ret;
245
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800246 mdev = (struct erofs_map_dev) {
247 .m_deviceid = map.m_deviceid,
248 .m_pa = map.m_pa,
249 };
250 ret = erofs_map_dev(inode->i_sb, &mdev);
251 if (ret)
252 return ret;
253
Huang Jianana08e67a2021-08-05 08:35:59 +0800254 iomap->offset = map.m_la;
Christoph Hellwigde205112021-11-29 11:22:00 +0100255 if (flags & IOMAP_DAX) {
256 iomap->dax_dev = mdev.m_daxdev;
257 iomap->offset += mdev.m_dax_part_off;
258 } else {
259 iomap->bdev = mdev.m_bdev;
260 }
Huang Jianana08e67a2021-08-05 08:35:59 +0800261 iomap->length = map.m_llen;
262 iomap->flags = 0;
Gao Xiang771c9942021-08-05 08:36:01 +0800263 iomap->private = NULL;
Huang Jianana08e67a2021-08-05 08:35:59 +0800264
265 if (!(map.m_flags & EROFS_MAP_MAPPED)) {
266 iomap->type = IOMAP_HOLE;
267 iomap->addr = IOMAP_NULL_ADDR;
268 if (!iomap->length)
269 iomap->length = length;
270 return 0;
271 }
272
Huang Jianana08e67a2021-08-05 08:35:59 +0800273 if (map.m_flags & EROFS_MAP_META) {
Gao Xiangfdf80a42022-01-02 12:00:13 +0800274 void *ptr;
275 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
Gao Xiang771c9942021-08-05 08:36:01 +0800276
277 iomap->type = IOMAP_INLINE;
Gao Xiangfdf80a42022-01-02 12:00:13 +0800278 ptr = erofs_read_metabuf(&buf, inode->i_sb,
279 erofs_blknr(mdev.m_pa), EROFS_KMAP);
280 if (IS_ERR(ptr))
281 return PTR_ERR(ptr);
282 iomap->inline_data = ptr + erofs_blkoff(mdev.m_pa);
283 iomap->private = buf.base;
Gao Xiang771c9942021-08-05 08:36:01 +0800284 } else {
285 iomap->type = IOMAP_MAPPED;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800286 iomap->addr = mdev.m_pa;
Huang Jianana08e67a2021-08-05 08:35:59 +0800287 }
Huang Jianana08e67a2021-08-05 08:35:59 +0800288 return 0;
289}
290
Gao Xiang771c9942021-08-05 08:36:01 +0800291static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
292 ssize_t written, unsigned int flags, struct iomap *iomap)
293{
Gao Xiangfdf80a42022-01-02 12:00:13 +0800294 void *ptr = iomap->private;
Gao Xiang771c9942021-08-05 08:36:01 +0800295
Gao Xiangfdf80a42022-01-02 12:00:13 +0800296 if (ptr) {
297 struct erofs_buf buf = {
298 .page = kmap_to_page(ptr),
299 .base = ptr,
300 .kmap_type = EROFS_KMAP,
301 };
302
Gao Xiang771c9942021-08-05 08:36:01 +0800303 DBG_BUGON(iomap->type != IOMAP_INLINE);
Gao Xiangfdf80a42022-01-02 12:00:13 +0800304 erofs_put_metabuf(&buf);
Gao Xiang771c9942021-08-05 08:36:01 +0800305 } else {
306 DBG_BUGON(iomap->type == IOMAP_INLINE);
307 }
308 return written;
309}
310
Huang Jianana08e67a2021-08-05 08:35:59 +0800311static const struct iomap_ops erofs_iomap_ops = {
312 .iomap_begin = erofs_iomap_begin,
Gao Xiang771c9942021-08-05 08:36:01 +0800313 .iomap_end = erofs_iomap_end,
Huang Jianana08e67a2021-08-05 08:35:59 +0800314};
315
Gao Xiangeadcd6b2021-08-13 13:29:31 +0800316int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
317 u64 start, u64 len)
318{
319 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
320#ifdef CONFIG_EROFS_FS_ZIP
321 return iomap_fiemap(inode, fieinfo, start, len,
322 &z_erofs_iomap_report_ops);
323#else
324 return -EOPNOTSUPP;
325#endif
326 }
327 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
328}
329
Gao Xiang771c9942021-08-05 08:36:01 +0800330/*
331 * since we dont have write or truncate flows, so no inode
332 * locking needs to be held at the moment.
333 */
334static int erofs_readpage(struct file *file, struct page *page)
335{
336 return iomap_readpage(page, &erofs_iomap_ops);
337}
338
339static void erofs_readahead(struct readahead_control *rac)
340{
341 return iomap_readahead(rac, &erofs_iomap_ops);
342}
343
344static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
345{
346 return iomap_bmap(mapping, block, &erofs_iomap_ops);
347}
348
Huang Jianana08e67a2021-08-05 08:35:59 +0800349static int erofs_prepare_dio(struct kiocb *iocb, struct iov_iter *to)
350{
351 struct inode *inode = file_inode(iocb->ki_filp);
352 loff_t align = iocb->ki_pos | iov_iter_count(to) |
353 iov_iter_alignment(to);
354 struct block_device *bdev = inode->i_sb->s_bdev;
355 unsigned int blksize_mask;
356
357 if (bdev)
358 blksize_mask = (1 << ilog2(bdev_logical_block_size(bdev))) - 1;
359 else
360 blksize_mask = (1 << inode->i_blkbits) - 1;
361
362 if (align & blksize_mask)
363 return -EINVAL;
Huang Jianana08e67a2021-08-05 08:35:59 +0800364 return 0;
365}
366
367static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
368{
369 /* no need taking (shared) inode lock since it's a ro filesystem */
370 if (!iov_iter_count(to))
371 return 0;
372
Gao Xiang06252e92021-08-05 08:36:00 +0800373#ifdef CONFIG_FS_DAX
374 if (IS_DAX(iocb->ki_filp->f_mapping->host))
375 return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
376#endif
Huang Jianana08e67a2021-08-05 08:35:59 +0800377 if (iocb->ki_flags & IOCB_DIRECT) {
378 int err = erofs_prepare_dio(iocb, to);
379
380 if (!err)
381 return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
Andreas Gruenbacher4fdccaa2021-07-24 12:26:41 +0200382 NULL, 0, 0);
Huang Jianana08e67a2021-08-05 08:35:59 +0800383 if (err < 0)
384 return err;
385 }
386 return filemap_read(iocb, to, 0);
387}
388
Gao Xiang81781b02018-07-26 20:21:47 +0800389/* for uncompressed (aligned) files and raw access for other files */
390const struct address_space_operations erofs_raw_access_aops = {
Gao Xiang771c9942021-08-05 08:36:01 +0800391 .readpage = erofs_readpage,
392 .readahead = erofs_readahead,
Chao Yu9da681e2019-07-16 17:32:56 +0800393 .bmap = erofs_bmap,
Huang Jianana08e67a2021-08-05 08:35:59 +0800394 .direct_IO = noop_direct_IO,
395};
396
Gao Xiang06252e92021-08-05 08:36:00 +0800397#ifdef CONFIG_FS_DAX
398static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
399 enum page_entry_size pe_size)
400{
401 return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops);
402}
403
404static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
405{
406 return erofs_dax_huge_fault(vmf, PE_SIZE_PTE);
407}
408
409static const struct vm_operations_struct erofs_dax_vm_ops = {
410 .fault = erofs_dax_fault,
411 .huge_fault = erofs_dax_huge_fault,
412};
413
414static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma)
415{
416 if (!IS_DAX(file_inode(file)))
417 return generic_file_readonly_mmap(file, vma);
418
419 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
420 return -EINVAL;
421
422 vma->vm_ops = &erofs_dax_vm_ops;
423 vma->vm_flags |= VM_HUGEPAGE;
424 return 0;
425}
426#else
427#define erofs_file_mmap generic_file_readonly_mmap
428#endif
429
Huang Jianana08e67a2021-08-05 08:35:59 +0800430const struct file_operations erofs_file_fops = {
431 .llseek = generic_file_llseek,
432 .read_iter = erofs_file_read_iter,
Gao Xiang06252e92021-08-05 08:36:00 +0800433 .mmap = erofs_file_mmap,
Huang Jianana08e67a2021-08-05 08:35:59 +0800434 .splice_read = generic_file_splice_read,
Gao Xiang81781b02018-07-26 20:21:47 +0800435};