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/prefetch.h> 9 #include <linux/sched/mm.h> 10 #include <linux/dax.h> 11 #include <trace/events/erofs.h> 12 13 void erofs_unmap_metabuf(struct erofs_buf *buf) 14 { 15 if (buf->kmap_type == EROFS_KMAP) 16 kunmap_local(buf->base); 17 buf->base = NULL; 18 buf->kmap_type = EROFS_NO_KMAP; 19 } 20 21 void erofs_put_metabuf(struct erofs_buf *buf) 22 { 23 if (!buf->page) 24 return; 25 erofs_unmap_metabuf(buf); 26 put_page(buf->page); 27 buf->page = NULL; 28 } 29 30 void *erofs_bread(struct erofs_buf *buf, struct inode *inode, 31 erofs_blk_t blkaddr, enum erofs_kmap_type type) 32 { 33 struct address_space *const mapping = inode->i_mapping; 34 erofs_off_t offset = blknr_to_addr(blkaddr); 35 pgoff_t index = offset >> PAGE_SHIFT; 36 struct page *page = buf->page; 37 struct folio *folio; 38 unsigned int nofs_flag; 39 40 if (!page || page->index != index) { 41 erofs_put_metabuf(buf); 42 43 nofs_flag = memalloc_nofs_save(); 44 folio = read_cache_folio(mapping, index, NULL, NULL); 45 memalloc_nofs_restore(nofs_flag); 46 if (IS_ERR(folio)) 47 return folio; 48 49 /* should already be PageUptodate, no need to lock page */ 50 page = folio_file_page(folio, index); 51 buf->page = page; 52 } 53 if (buf->kmap_type == EROFS_NO_KMAP) { 54 if (type == EROFS_KMAP) 55 buf->base = kmap_local_page(page); 56 buf->kmap_type = type; 57 } else if (buf->kmap_type != type) { 58 DBG_BUGON(1); 59 return ERR_PTR(-EFAULT); 60 } 61 if (type == EROFS_NO_KMAP) 62 return NULL; 63 return buf->base + (offset & ~PAGE_MASK); 64 } 65 66 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb, 67 erofs_blk_t blkaddr, enum erofs_kmap_type type) 68 { 69 if (erofs_is_fscache_mode(sb)) 70 return erofs_bread(buf, EROFS_SB(sb)->s_fscache->inode, 71 blkaddr, type); 72 73 return erofs_bread(buf, sb->s_bdev->bd_inode, blkaddr, type); 74 } 75 76 static int erofs_map_blocks_flatmode(struct inode *inode, 77 struct erofs_map_blocks *map, 78 int flags) 79 { 80 erofs_blk_t nblocks, lastblk; 81 u64 offset = map->m_la; 82 struct erofs_inode *vi = EROFS_I(inode); 83 bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE); 84 85 nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ); 86 lastblk = nblocks - tailendpacking; 87 88 /* there is no hole in flatmode */ 89 map->m_flags = EROFS_MAP_MAPPED; 90 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; 93 } else if (tailendpacking) { 94 /* 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 101 /* inline data should be located in the same meta block */ 102 if (erofs_blkoff(map->m_pa) + map->m_plen > EROFS_BLKSIZ) { 103 erofs_err(inode->i_sb, 104 "inline data cross block boundary @ nid %llu", 105 vi->nid); 106 DBG_BUGON(1); 107 return -EFSCORRUPTED; 108 } 109 map->m_flags |= EROFS_MAP_META; 110 } else { 111 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); 114 DBG_BUGON(1); 115 return -EIO; 116 } 117 return 0; 118 } 119 120 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; 126 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 127 u64 chunknr; 128 unsigned int unit; 129 erofs_off_t pos; 130 void *kaddr; 131 int err = 0; 132 133 trace_erofs_map_blocks_enter(inode, map, flags); 134 map->m_deviceid = 0; 135 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 142 if (vi->datalayout != EROFS_INODE_CHUNK_BASED) { 143 err = erofs_map_blocks_flatmode(inode, map, flags); 144 goto out; 145 } 146 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 156 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP); 157 if (IS_ERR(kaddr)) { 158 err = PTR_ERR(kaddr); 159 goto out; 160 } 161 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)) { 167 __le32 *blkaddr = kaddr + erofs_blkoff(pos); 168 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 */ 178 idx = kaddr + erofs_blkoff(pos); 179 switch (le32_to_cpu(idx->blkaddr)) { 180 case EROFS_NULL_ADDR: 181 map->m_flags = 0; 182 break; 183 default: 184 map->m_deviceid = le16_to_cpu(idx->device_id) & 185 EROFS_SB(sb)->device_id_mask; 186 map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr)); 187 map->m_flags = EROFS_MAP_MAPPED; 188 break; 189 } 190 out_unlock: 191 erofs_put_metabuf(&buf); 192 out: 193 if (!err) 194 map->m_llen = map->m_plen; 195 trace_erofs_map_blocks_exit(inode, map, flags, 0); 196 return err; 197 } 198 199 int 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 map->m_dax_part_off = EROFS_SB(sb)->dax_part_off; 209 map->m_fscache = EROFS_SB(sb)->s_fscache; 210 211 if (map->m_deviceid) { 212 down_read(&devs->rwsem); 213 dif = idr_find(&devs->tree, map->m_deviceid - 1); 214 if (!dif) { 215 up_read(&devs->rwsem); 216 return -ENODEV; 217 } 218 map->m_bdev = dif->bdev; 219 map->m_daxdev = dif->dax_dev; 220 map->m_dax_part_off = dif->dax_part_off; 221 map->m_fscache = dif->fscache; 222 up_read(&devs->rwsem); 223 } else if (devs->extra_devices) { 224 down_read(&devs->rwsem); 225 idr_for_each_entry(&devs->tree, dif, id) { 226 erofs_off_t startoff, length; 227 228 if (!dif->mapped_blkaddr) 229 continue; 230 startoff = blknr_to_addr(dif->mapped_blkaddr); 231 length = blknr_to_addr(dif->blocks); 232 233 if (map->m_pa >= startoff && 234 map->m_pa < startoff + length) { 235 map->m_pa -= startoff; 236 map->m_bdev = dif->bdev; 237 map->m_daxdev = dif->dax_dev; 238 map->m_dax_part_off = dif->dax_part_off; 239 map->m_fscache = dif->fscache; 240 break; 241 } 242 } 243 up_read(&devs->rwsem); 244 } 245 return 0; 246 } 247 248 static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 249 unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 250 { 251 int ret; 252 struct erofs_map_blocks map; 253 struct erofs_map_dev mdev; 254 255 map.m_la = offset; 256 map.m_llen = length; 257 258 ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW); 259 if (ret < 0) 260 return ret; 261 262 mdev = (struct erofs_map_dev) { 263 .m_deviceid = map.m_deviceid, 264 .m_pa = map.m_pa, 265 }; 266 ret = erofs_map_dev(inode->i_sb, &mdev); 267 if (ret) 268 return ret; 269 270 iomap->offset = map.m_la; 271 if (flags & IOMAP_DAX) 272 iomap->dax_dev = mdev.m_daxdev; 273 else 274 iomap->bdev = mdev.m_bdev; 275 iomap->length = map.m_llen; 276 iomap->flags = 0; 277 iomap->private = NULL; 278 279 if (!(map.m_flags & EROFS_MAP_MAPPED)) { 280 iomap->type = IOMAP_HOLE; 281 iomap->addr = IOMAP_NULL_ADDR; 282 if (!iomap->length) 283 iomap->length = length; 284 return 0; 285 } 286 287 if (map.m_flags & EROFS_MAP_META) { 288 void *ptr; 289 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 290 291 iomap->type = IOMAP_INLINE; 292 ptr = erofs_read_metabuf(&buf, inode->i_sb, 293 erofs_blknr(mdev.m_pa), EROFS_KMAP); 294 if (IS_ERR(ptr)) 295 return PTR_ERR(ptr); 296 iomap->inline_data = ptr + erofs_blkoff(mdev.m_pa); 297 iomap->private = buf.base; 298 } else { 299 iomap->type = IOMAP_MAPPED; 300 iomap->addr = mdev.m_pa; 301 if (flags & IOMAP_DAX) 302 iomap->addr += mdev.m_dax_part_off; 303 } 304 return 0; 305 } 306 307 static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length, 308 ssize_t written, unsigned int flags, struct iomap *iomap) 309 { 310 void *ptr = iomap->private; 311 312 if (ptr) { 313 struct erofs_buf buf = { 314 .page = kmap_to_page(ptr), 315 .base = ptr, 316 .kmap_type = EROFS_KMAP, 317 }; 318 319 DBG_BUGON(iomap->type != IOMAP_INLINE); 320 erofs_put_metabuf(&buf); 321 } else { 322 DBG_BUGON(iomap->type == IOMAP_INLINE); 323 } 324 return written; 325 } 326 327 static const struct iomap_ops erofs_iomap_ops = { 328 .iomap_begin = erofs_iomap_begin, 329 .iomap_end = erofs_iomap_end, 330 }; 331 332 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 333 u64 start, u64 len) 334 { 335 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) { 336 #ifdef CONFIG_EROFS_FS_ZIP 337 return iomap_fiemap(inode, fieinfo, start, len, 338 &z_erofs_iomap_report_ops); 339 #else 340 return -EOPNOTSUPP; 341 #endif 342 } 343 return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops); 344 } 345 346 /* 347 * since we dont have write or truncate flows, so no inode 348 * locking needs to be held at the moment. 349 */ 350 static int erofs_read_folio(struct file *file, struct folio *folio) 351 { 352 return iomap_read_folio(folio, &erofs_iomap_ops); 353 } 354 355 static void erofs_readahead(struct readahead_control *rac) 356 { 357 return iomap_readahead(rac, &erofs_iomap_ops); 358 } 359 360 static sector_t erofs_bmap(struct address_space *mapping, sector_t block) 361 { 362 return iomap_bmap(mapping, block, &erofs_iomap_ops); 363 } 364 365 static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 366 { 367 struct inode *inode = file_inode(iocb->ki_filp); 368 369 /* no need taking (shared) inode lock since it's a ro filesystem */ 370 if (!iov_iter_count(to)) 371 return 0; 372 373 #ifdef CONFIG_FS_DAX 374 if (IS_DAX(inode)) 375 return dax_iomap_rw(iocb, to, &erofs_iomap_ops); 376 #endif 377 if (iocb->ki_flags & IOCB_DIRECT) { 378 struct block_device *bdev = inode->i_sb->s_bdev; 379 unsigned int blksize_mask; 380 381 if (bdev) 382 blksize_mask = bdev_logical_block_size(bdev) - 1; 383 else 384 blksize_mask = (1 << inode->i_blkbits) - 1; 385 386 if ((iocb->ki_pos | iov_iter_count(to) | 387 iov_iter_alignment(to)) & blksize_mask) 388 return -EINVAL; 389 390 return iomap_dio_rw(iocb, to, &erofs_iomap_ops, 391 NULL, 0, NULL, 0); 392 } 393 return filemap_read(iocb, to, 0); 394 } 395 396 /* for uncompressed (aligned) files and raw access for other files */ 397 const struct address_space_operations erofs_raw_access_aops = { 398 .read_folio = erofs_read_folio, 399 .readahead = erofs_readahead, 400 .bmap = erofs_bmap, 401 .direct_IO = noop_direct_IO, 402 .release_folio = iomap_release_folio, 403 .invalidate_folio = iomap_invalidate_folio, 404 }; 405 406 #ifdef CONFIG_FS_DAX 407 static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf, 408 enum page_entry_size pe_size) 409 { 410 return dax_iomap_fault(vmf, pe_size, NULL, NULL, &erofs_iomap_ops); 411 } 412 413 static vm_fault_t erofs_dax_fault(struct vm_fault *vmf) 414 { 415 return erofs_dax_huge_fault(vmf, PE_SIZE_PTE); 416 } 417 418 static const struct vm_operations_struct erofs_dax_vm_ops = { 419 .fault = erofs_dax_fault, 420 .huge_fault = erofs_dax_huge_fault, 421 }; 422 423 static int erofs_file_mmap(struct file *file, struct vm_area_struct *vma) 424 { 425 if (!IS_DAX(file_inode(file))) 426 return generic_file_readonly_mmap(file, vma); 427 428 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 429 return -EINVAL; 430 431 vma->vm_ops = &erofs_dax_vm_ops; 432 vma->vm_flags |= VM_HUGEPAGE; 433 return 0; 434 } 435 #else 436 #define erofs_file_mmap generic_file_readonly_mmap 437 #endif 438 439 const struct file_operations erofs_file_fops = { 440 .llseek = generic_file_llseek, 441 .read_iter = erofs_file_read_iter, 442 .mmap = erofs_file_mmap, 443 .splice_read = generic_file_splice_read, 444 }; 445