|
15 | 15 |
|
16 | 16 | #include <linux/module.h> |
17 | 17 | #include <linux/fs.h> |
| 18 | +#include <linux/file.h> |
18 | 19 | #include <linux/pagemap.h> |
| 20 | +#include <linux/pfn_t.h> |
| 21 | +#include <linux/ramfs.h> |
19 | 22 | #include <linux/init.h> |
20 | 23 | #include <linux/string.h> |
21 | 24 | #include <linux/blkdev.h> |
@@ -51,6 +54,7 @@ static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb) |
51 | 54 | static const struct super_operations cramfs_ops; |
52 | 55 | static const struct inode_operations cramfs_dir_inode_operations; |
53 | 56 | static const struct file_operations cramfs_directory_operations; |
| 57 | +static const struct file_operations cramfs_physmem_fops; |
54 | 58 | static const struct address_space_operations cramfs_aops; |
55 | 59 |
|
56 | 60 | static DEFINE_MUTEX(read_mutex); |
@@ -98,6 +102,10 @@ static struct inode *get_cramfs_inode(struct super_block *sb, |
98 | 102 | case S_IFREG: |
99 | 103 | inode->i_fop = &generic_ro_fops; |
100 | 104 | inode->i_data.a_ops = &cramfs_aops; |
| 105 | + if (IS_ENABLED(CONFIG_CRAMFS_MTD) && |
| 106 | + CRAMFS_SB(sb)->flags & CRAMFS_FLAG_EXT_BLOCK_POINTERS && |
| 107 | + CRAMFS_SB(sb)->linear_phys_addr) |
| 108 | + inode->i_fop = &cramfs_physmem_fops; |
101 | 109 | break; |
102 | 110 | case S_IFDIR: |
103 | 111 | inode->i_op = &cramfs_dir_inode_operations; |
@@ -279,6 +287,207 @@ static void *cramfs_read(struct super_block *sb, unsigned int offset, |
279 | 287 | return NULL; |
280 | 288 | } |
281 | 289 |
|
| 290 | +/* |
| 291 | + * For a mapping to be possible, we need a range of uncompressed and |
| 292 | + * contiguous blocks. Return the offset for the first block and number of |
| 293 | + * valid blocks for which that is true, or zero otherwise. |
| 294 | + */ |
| 295 | +static u32 cramfs_get_block_range(struct inode *inode, u32 pgoff, u32 *pages) |
| 296 | +{ |
| 297 | + struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb); |
| 298 | + int i; |
| 299 | + u32 *blockptrs, first_block_addr; |
| 300 | + |
| 301 | + /* |
| 302 | + * We can dereference memory directly here as this code may be |
| 303 | + * reached only when there is a direct filesystem image mapping |
| 304 | + * available in memory. |
| 305 | + */ |
| 306 | + blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode) + pgoff * 4); |
| 307 | + first_block_addr = blockptrs[0] & ~CRAMFS_BLK_FLAGS; |
| 308 | + i = 0; |
| 309 | + do { |
| 310 | + u32 block_off = i * (PAGE_SIZE >> CRAMFS_BLK_DIRECT_PTR_SHIFT); |
| 311 | + u32 expect = (first_block_addr + block_off) | |
| 312 | + CRAMFS_BLK_FLAG_DIRECT_PTR | |
| 313 | + CRAMFS_BLK_FLAG_UNCOMPRESSED; |
| 314 | + if (blockptrs[i] != expect) { |
| 315 | + pr_debug("range: block %d/%d got %#x expects %#x\n", |
| 316 | + pgoff+i, pgoff + *pages - 1, |
| 317 | + blockptrs[i], expect); |
| 318 | + if (i == 0) |
| 319 | + return 0; |
| 320 | + break; |
| 321 | + } |
| 322 | + } while (++i < *pages); |
| 323 | + |
| 324 | + *pages = i; |
| 325 | + return first_block_addr << CRAMFS_BLK_DIRECT_PTR_SHIFT; |
| 326 | +} |
| 327 | + |
| 328 | +#ifdef CONFIG_MMU |
| 329 | + |
| 330 | +/* |
| 331 | + * Return true if the last page of a file in the filesystem image contains |
| 332 | + * some other data that doesn't belong to that file. It is assumed that the |
| 333 | + * last block is CRAMFS_BLK_FLAG_DIRECT_PTR | CRAMFS_BLK_FLAG_UNCOMPRESSED |
| 334 | + * (verified by cramfs_get_block_range() and directly accessible in memory. |
| 335 | + */ |
| 336 | +static bool cramfs_last_page_is_shared(struct inode *inode) |
| 337 | +{ |
| 338 | + struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb); |
| 339 | + u32 partial, last_page, blockaddr, *blockptrs; |
| 340 | + char *tail_data; |
| 341 | + |
| 342 | + partial = offset_in_page(inode->i_size); |
| 343 | + if (!partial) |
| 344 | + return false; |
| 345 | + last_page = inode->i_size >> PAGE_SHIFT; |
| 346 | + blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode)); |
| 347 | + blockaddr = blockptrs[last_page] & ~CRAMFS_BLK_FLAGS; |
| 348 | + blockaddr <<= CRAMFS_BLK_DIRECT_PTR_SHIFT; |
| 349 | + tail_data = sbi->linear_virt_addr + blockaddr + partial; |
| 350 | + return memchr_inv(tail_data, 0, PAGE_SIZE - partial) ? true : false; |
| 351 | +} |
| 352 | + |
| 353 | +static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma) |
| 354 | +{ |
| 355 | + struct inode *inode = file_inode(file); |
| 356 | + struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb); |
| 357 | + unsigned int pages, max_pages, offset; |
| 358 | + unsigned long address, pgoff = vma->vm_pgoff; |
| 359 | + char *bailout_reason; |
| 360 | + int ret; |
| 361 | + |
| 362 | + ret = generic_file_readonly_mmap(file, vma); |
| 363 | + if (ret) |
| 364 | + return ret; |
| 365 | + |
| 366 | + /* |
| 367 | + * Now try to pre-populate ptes for this vma with a direct |
| 368 | + * mapping avoiding memory allocation when possible. |
| 369 | + */ |
| 370 | + |
| 371 | + /* Could COW work here? */ |
| 372 | + bailout_reason = "vma is writable"; |
| 373 | + if (vma->vm_flags & VM_WRITE) |
| 374 | + goto bailout; |
| 375 | + |
| 376 | + max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 377 | + bailout_reason = "beyond file limit"; |
| 378 | + if (pgoff >= max_pages) |
| 379 | + goto bailout; |
| 380 | + pages = min(vma_pages(vma), max_pages - pgoff); |
| 381 | + |
| 382 | + offset = cramfs_get_block_range(inode, pgoff, &pages); |
| 383 | + bailout_reason = "unsuitable block layout"; |
| 384 | + if (!offset) |
| 385 | + goto bailout; |
| 386 | + address = sbi->linear_phys_addr + offset; |
| 387 | + bailout_reason = "data is not page aligned"; |
| 388 | + if (!PAGE_ALIGNED(address)) |
| 389 | + goto bailout; |
| 390 | + |
| 391 | + /* Don't map the last page if it contains some other data */ |
| 392 | + if (pgoff + pages == max_pages && cramfs_last_page_is_shared(inode)) { |
| 393 | + pr_debug("mmap: %s: last page is shared\n", |
| 394 | + file_dentry(file)->d_name.name); |
| 395 | + pages--; |
| 396 | + } |
| 397 | + |
| 398 | + if (!pages) { |
| 399 | + bailout_reason = "no suitable block remaining"; |
| 400 | + goto bailout; |
| 401 | + } |
| 402 | + |
| 403 | + if (pages == vma_pages(vma)) { |
| 404 | + /* |
| 405 | + * The entire vma is mappable. remap_pfn_range() will |
| 406 | + * make it distinguishable from a non-direct mapping |
| 407 | + * in /proc/<pid>/maps by substituting the file offset |
| 408 | + * with the actual physical address. |
| 409 | + */ |
| 410 | + ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT, |
| 411 | + pages * PAGE_SIZE, vma->vm_page_prot); |
| 412 | + } else { |
| 413 | + /* |
| 414 | + * Let's create a mixed map if we can't map it all. |
| 415 | + * The normal paging machinery will take care of the |
| 416 | + * unpopulated ptes via cramfs_readpage(). |
| 417 | + */ |
| 418 | + int i; |
| 419 | + vma->vm_flags |= VM_MIXEDMAP; |
| 420 | + for (i = 0; i < pages && !ret; i++) { |
| 421 | + unsigned long off = i * PAGE_SIZE; |
| 422 | + pfn_t pfn = phys_to_pfn_t(address + off, PFN_DEV); |
| 423 | + ret = vm_insert_mixed(vma, vma->vm_start + off, pfn); |
| 424 | + } |
| 425 | + } |
| 426 | + |
| 427 | + if (!ret) |
| 428 | + pr_debug("mapped %s[%lu] at 0x%08lx (%u/%lu pages) " |
| 429 | + "to vma 0x%08lx, page_prot 0x%llx\n", |
| 430 | + file_dentry(file)->d_name.name, pgoff, |
| 431 | + address, pages, vma_pages(vma), vma->vm_start, |
| 432 | + (unsigned long long)pgprot_val(vma->vm_page_prot)); |
| 433 | + return ret; |
| 434 | + |
| 435 | +bailout: |
| 436 | + pr_debug("%s[%lu]: direct mmap impossible: %s\n", |
| 437 | + file_dentry(file)->d_name.name, pgoff, bailout_reason); |
| 438 | + /* Didn't manage any direct map, but normal paging is still possible */ |
| 439 | + return 0; |
| 440 | +} |
| 441 | + |
| 442 | +#else /* CONFIG_MMU */ |
| 443 | + |
| 444 | +static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma) |
| 445 | +{ |
| 446 | + return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS; |
| 447 | +} |
| 448 | + |
| 449 | +static unsigned long cramfs_physmem_get_unmapped_area(struct file *file, |
| 450 | + unsigned long addr, unsigned long len, |
| 451 | + unsigned long pgoff, unsigned long flags) |
| 452 | +{ |
| 453 | + struct inode *inode = file_inode(file); |
| 454 | + struct super_block *sb = inode->i_sb; |
| 455 | + struct cramfs_sb_info *sbi = CRAMFS_SB(sb); |
| 456 | + unsigned int pages, block_pages, max_pages, offset; |
| 457 | + |
| 458 | + pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 459 | + max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 460 | + if (pgoff >= max_pages || pages > max_pages - pgoff) |
| 461 | + return -EINVAL; |
| 462 | + block_pages = pages; |
| 463 | + offset = cramfs_get_block_range(inode, pgoff, &block_pages); |
| 464 | + if (!offset || block_pages != pages) |
| 465 | + return -ENOSYS; |
| 466 | + addr = sbi->linear_phys_addr + offset; |
| 467 | + pr_debug("get_unmapped for %s ofs %#lx siz %lu at 0x%08lx\n", |
| 468 | + file_dentry(file)->d_name.name, pgoff*PAGE_SIZE, len, addr); |
| 469 | + return addr; |
| 470 | +} |
| 471 | + |
| 472 | +static unsigned int cramfs_physmem_mmap_capabilities(struct file *file) |
| 473 | +{ |
| 474 | + return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | |
| 475 | + NOMMU_MAP_READ | NOMMU_MAP_EXEC; |
| 476 | +} |
| 477 | + |
| 478 | +#endif /* CONFIG_MMU */ |
| 479 | + |
| 480 | +static const struct file_operations cramfs_physmem_fops = { |
| 481 | + .llseek = generic_file_llseek, |
| 482 | + .read_iter = generic_file_read_iter, |
| 483 | + .splice_read = generic_file_splice_read, |
| 484 | + .mmap = cramfs_physmem_mmap, |
| 485 | +#ifndef CONFIG_MMU |
| 486 | + .get_unmapped_area = cramfs_physmem_get_unmapped_area, |
| 487 | + .mmap_capabilities = cramfs_physmem_mmap_capabilities, |
| 488 | +#endif |
| 489 | +}; |
| 490 | + |
282 | 491 | static void cramfs_kill_sb(struct super_block *sb) |
283 | 492 | { |
284 | 493 | struct cramfs_sb_info *sbi = CRAMFS_SB(sb); |
|
0 commit comments