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