11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28250297edSAndrew Morton #include <linux/ramfs.h> 29caefba17SHugh Dickins #include <linux/pagemap.h> 30853ac43aSMatt Mackall #include <linux/file.h> 31853ac43aSMatt Mackall #include <linux/mm.h> 32174cd4b1SIngo Molnar #include <linux/sched/signal.h> 33b95f1b31SPaul Gortmaker #include <linux/export.h> 34853ac43aSMatt Mackall #include <linux/swap.h> 35e2e40f2cSChristoph Hellwig #include <linux/uio.h> 36f3f0e1d2SKirill A. Shutemov #include <linux/khugepaged.h> 37749df87bSMike Kravetz #include <linux/hugetlb.h> 38853ac43aSMatt Mackall 3995cc09d6SAndrea Arcangeli #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ 4095cc09d6SAndrea Arcangeli 41853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 42853ac43aSMatt Mackall 43853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 441da177e4SLinus Torvalds /* 451da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 461da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 471da177e4SLinus Torvalds * which makes it a completely usable filesystem. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 5039f0247dSAndreas Gruenbacher #include <linux/xattr.h> 51a5694255SChristoph Hellwig #include <linux/exportfs.h> 521c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 53feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 541da177e4SLinus Torvalds #include <linux/mman.h> 551da177e4SLinus Torvalds #include <linux/string.h> 561da177e4SLinus Torvalds #include <linux/slab.h> 571da177e4SLinus Torvalds #include <linux/backing-dev.h> 581da177e4SLinus Torvalds #include <linux/shmem_fs.h> 591da177e4SLinus Torvalds #include <linux/writeback.h> 601da177e4SLinus Torvalds #include <linux/blkdev.h> 61bda97eabSHugh Dickins #include <linux/pagevec.h> 6241ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6383e4fa9cSHugh Dickins #include <linux/falloc.h> 64708e3508SHugh Dickins #include <linux/splice.h> 651da177e4SLinus Torvalds #include <linux/security.h> 661da177e4SLinus Torvalds #include <linux/swapops.h> 671da177e4SLinus Torvalds #include <linux/mempolicy.h> 681da177e4SLinus Torvalds #include <linux/namei.h> 69b00dc3adSHugh Dickins #include <linux/ctype.h> 70304dbdb7SLee Schermerhorn #include <linux/migrate.h> 71c1f60a5aSChristoph Lameter #include <linux/highmem.h> 72680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7392562927SMimi Zohar #include <linux/magic.h> 749183df25SDavid Herrmann #include <linux/syscalls.h> 7540e041a2SDavid Herrmann #include <linux/fcntl.h> 769183df25SDavid Herrmann #include <uapi/linux/memfd.h> 77cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 784c27fe4cSMike Rapoport #include <linux/rmap.h> 792b4db796SAmir Goldstein #include <linux/uuid.h> 80304dbdb7SLee Schermerhorn 817c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 821da177e4SLinus Torvalds #include <asm/pgtable.h> 831da177e4SLinus Torvalds 84dd56b046SMel Gorman #include "internal.h" 85dd56b046SMel Gorman 8609cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 8709cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 901da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 911da177e4SLinus Torvalds 9269f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9369f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9469f07ec9SHugh Dickins 951aac1400SHugh Dickins /* 96f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 97f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 98f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 991aac1400SHugh Dickins */ 1001aac1400SHugh Dickins struct shmem_falloc { 1018e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 1021aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1031aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1041aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1051aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1061aac1400SHugh Dickins }; 1071aac1400SHugh Dickins 108b76db735SAndrew Morton #ifdef CONFIG_TMPFS 109680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 110680d794bSakpm@linux-foundation.org { 111680d794bSakpm@linux-foundation.org return totalram_pages / 2; 112680d794bSakpm@linux-foundation.org } 113680d794bSakpm@linux-foundation.org 114680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 115680d794bSakpm@linux-foundation.org { 116680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 117680d794bSakpm@linux-foundation.org } 118b76db735SAndrew Morton #endif 119680d794bSakpm@linux-foundation.org 120bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 121bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 122bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 12368da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1249e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, 125cfda0526SMike Rapoport gfp_t gfp, struct vm_area_struct *vma, 126cfda0526SMike Rapoport struct vm_fault *vmf, int *fault_type); 12768da9f05SHugh Dickins 128f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index, 1299e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp) 13068da9f05SHugh Dickins { 13168da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 132cfda0526SMike Rapoport mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 13368da9f05SHugh Dickins } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds return sb->s_fs_info; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* 1411da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1421da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1431da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1441da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1471da177e4SLinus Torvalds { 1480b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 149191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1531da177e4SLinus Torvalds { 1540b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1551da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 15877142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 15977142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 16077142517SKonstantin Khlebnikov { 16177142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 16277142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 16377142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 16477142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 16577142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 16677142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 16777142517SKonstantin Khlebnikov } 16877142517SKonstantin Khlebnikov return 0; 16977142517SKonstantin Khlebnikov } 17077142517SKonstantin Khlebnikov 1711da177e4SLinus Torvalds /* 1721da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 17375edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 1741da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1751da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1761da177e4SLinus Torvalds */ 177800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1781da177e4SLinus Torvalds { 179800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 180800d8c63SKirill A. Shutemov return 0; 181800d8c63SKirill A. Shutemov 182800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 183800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1871da177e4SLinus Torvalds { 1880b0a0806SHugh Dickins if (flags & VM_NORESERVE) 18909cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1920f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages) 1930f079694SMike Rapoport { 1940f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 1950f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1960f079694SMike Rapoport 1970f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 1980f079694SMike Rapoport return false; 1990f079694SMike Rapoport 2000f079694SMike Rapoport if (sbinfo->max_blocks) { 2010f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2020f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2030f079694SMike Rapoport goto unacct; 2040f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 2050f079694SMike Rapoport } 2060f079694SMike Rapoport 2070f079694SMike Rapoport return true; 2080f079694SMike Rapoport 2090f079694SMike Rapoport unacct: 2100f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2110f079694SMike Rapoport return false; 2120f079694SMike Rapoport } 2130f079694SMike Rapoport 2140f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2150f079694SMike Rapoport { 2160f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2170f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2180f079694SMike Rapoport 2190f079694SMike Rapoport if (sbinfo->max_blocks) 2200f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2210f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2220f079694SMike Rapoport } 2230f079694SMike Rapoport 224759b9775SHugh Dickins static const struct super_operations shmem_ops; 225f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 22615ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 22792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 22892e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 22992e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 230f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 231779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2321da177e4SLinus Torvalds 233b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 234b0506e48SMike Rapoport { 235b0506e48SMike Rapoport return vma->vm_ops == &shmem_vm_ops; 236b0506e48SMike Rapoport } 237b0506e48SMike Rapoport 2381da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 239cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2401da177e4SLinus Torvalds 2415b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2425b04c689SPavel Emelyanov { 2435b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2445b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2455b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2465b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2475b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2485b04c689SPavel Emelyanov return -ENOSPC; 2495b04c689SPavel Emelyanov } 2505b04c689SPavel Emelyanov sbinfo->free_inodes--; 2515b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2525b04c689SPavel Emelyanov } 2535b04c689SPavel Emelyanov return 0; 2545b04c689SPavel Emelyanov } 2555b04c689SPavel Emelyanov 2565b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2575b04c689SPavel Emelyanov { 2585b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2595b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2605b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2615b04c689SPavel Emelyanov sbinfo->free_inodes++; 2625b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2635b04c689SPavel Emelyanov } 2645b04c689SPavel Emelyanov } 2655b04c689SPavel Emelyanov 26646711810SRandy Dunlap /** 26741ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2681da177e4SLinus Torvalds * @inode: inode to recalc 2691da177e4SLinus Torvalds * 2701da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2711da177e4SLinus Torvalds * undirtied hole pages behind our back. 2721da177e4SLinus Torvalds * 2731da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2741da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2751da177e4SLinus Torvalds * 2761da177e4SLinus Torvalds * It has to be called with the spinlock held. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2811da177e4SLinus Torvalds long freed; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2841da177e4SLinus Torvalds if (freed > 0) { 2851da177e4SLinus Torvalds info->alloced -= freed; 28654af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2870f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 291800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 292800d8c63SKirill A. Shutemov { 293800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 2944595ef88SKirill A. Shutemov unsigned long flags; 295800d8c63SKirill A. Shutemov 2960f079694SMike Rapoport if (!shmem_inode_acct_block(inode, pages)) 297800d8c63SKirill A. Shutemov return false; 298b1cc94abSMike Rapoport 2994595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 300800d8c63SKirill A. Shutemov info->alloced += pages; 301800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 302800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3034595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 304800d8c63SKirill A. Shutemov inode->i_mapping->nrpages += pages; 305800d8c63SKirill A. Shutemov 306800d8c63SKirill A. Shutemov return true; 307800d8c63SKirill A. Shutemov } 308800d8c63SKirill A. Shutemov 309800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 310800d8c63SKirill A. Shutemov { 311800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3124595ef88SKirill A. Shutemov unsigned long flags; 313800d8c63SKirill A. Shutemov 3144595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 315800d8c63SKirill A. Shutemov info->alloced -= pages; 316800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 317800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3184595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 319800d8c63SKirill A. Shutemov 3200f079694SMike Rapoport shmem_inode_unacct_blocks(inode, pages); 321800d8c63SKirill A. Shutemov } 322800d8c63SKirill A. Shutemov 3237a5d0fbbSHugh Dickins /* 3247a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 3257a5d0fbbSHugh Dickins */ 3267a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 3277a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 3287a5d0fbbSHugh Dickins { 329f7942430SJohannes Weiner struct radix_tree_node *node; 3305b9c98f3SMike Kravetz void __rcu **pslot; 3316dbaf22cSJohannes Weiner void *item; 3327a5d0fbbSHugh Dickins 3337a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 3346dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 335b93b0163SMatthew Wilcox item = __radix_tree_lookup(&mapping->i_pages, index, &node, &pslot); 336f7942430SJohannes Weiner if (!item) 3376dbaf22cSJohannes Weiner return -ENOENT; 3387a5d0fbbSHugh Dickins if (item != expected) 3397a5d0fbbSHugh Dickins return -ENOENT; 340b93b0163SMatthew Wilcox __radix_tree_replace(&mapping->i_pages, node, pslot, 341c7df8ad2SMel Gorman replacement, NULL); 3427a5d0fbbSHugh Dickins return 0; 3437a5d0fbbSHugh Dickins } 3447a5d0fbbSHugh Dickins 3457a5d0fbbSHugh Dickins /* 346d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 347d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 348d1899228SHugh Dickins * 349d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 350d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 351d1899228SHugh Dickins */ 352d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 353d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 354d1899228SHugh Dickins { 355d1899228SHugh Dickins void *item; 356d1899228SHugh Dickins 357d1899228SHugh Dickins rcu_read_lock(); 358b93b0163SMatthew Wilcox item = radix_tree_lookup(&mapping->i_pages, index); 359d1899228SHugh Dickins rcu_read_unlock(); 360d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 361d1899228SHugh Dickins } 362d1899228SHugh Dickins 363d1899228SHugh Dickins /* 3645a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 3655a6e75f8SKirill A. Shutemov * 3665a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 3675a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 3685a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 3695a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 3705a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 3715a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 3725a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 3735a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 3745a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 3755a6e75f8SKirill A. Shutemov */ 3765a6e75f8SKirill A. Shutemov 3775a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 3785a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 3795a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 3805a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 3815a6e75f8SKirill A. Shutemov 3825a6e75f8SKirill A. Shutemov /* 3835a6e75f8SKirill A. Shutemov * Special values. 3845a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 3855a6e75f8SKirill A. Shutemov * 3865a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 3875a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 3885a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 3895a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 3905a6e75f8SKirill A. Shutemov * 3915a6e75f8SKirill A. Shutemov */ 3925a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 3935a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 3945a6e75f8SKirill A. Shutemov 395e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3965a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 3975a6e75f8SKirill A. Shutemov 3985b9c98f3SMike Kravetz static int shmem_huge __read_mostly; 3995a6e75f8SKirill A. Shutemov 400f1f5929cSJérémy Lefaure #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 4015a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 4025a6e75f8SKirill A. Shutemov { 4035a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 4045a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 4055a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 4065a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 4075a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 4085a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 4095a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 4105a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 4115a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 4125a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 4135a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 4145a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 4155a6e75f8SKirill A. Shutemov return -EINVAL; 4165a6e75f8SKirill A. Shutemov } 4175a6e75f8SKirill A. Shutemov 4185a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 4195a6e75f8SKirill A. Shutemov { 4205a6e75f8SKirill A. Shutemov switch (huge) { 4215a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 4225a6e75f8SKirill A. Shutemov return "never"; 4235a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 4245a6e75f8SKirill A. Shutemov return "always"; 4255a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 4265a6e75f8SKirill A. Shutemov return "within_size"; 4275a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4285a6e75f8SKirill A. Shutemov return "advise"; 4295a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 4305a6e75f8SKirill A. Shutemov return "deny"; 4315a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 4325a6e75f8SKirill A. Shutemov return "force"; 4335a6e75f8SKirill A. Shutemov default: 4345a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 4355a6e75f8SKirill A. Shutemov return "bad_val"; 4365a6e75f8SKirill A. Shutemov } 4375a6e75f8SKirill A. Shutemov } 438f1f5929cSJérémy Lefaure #endif 4395a6e75f8SKirill A. Shutemov 440779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 441779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 442779750d2SKirill A. Shutemov { 443779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 444253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 445779750d2SKirill A. Shutemov struct inode *inode; 446779750d2SKirill A. Shutemov struct shmem_inode_info *info; 447779750d2SKirill A. Shutemov struct page *page; 448779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 449779750d2SKirill A. Shutemov int removed = 0, split = 0; 450779750d2SKirill A. Shutemov 451779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 452779750d2SKirill A. Shutemov return SHRINK_STOP; 453779750d2SKirill A. Shutemov 454779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 455779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 456779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 457779750d2SKirill A. Shutemov 458779750d2SKirill A. Shutemov /* pin the inode */ 459779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 460779750d2SKirill A. Shutemov 461779750d2SKirill A. Shutemov /* inode is about to be evicted */ 462779750d2SKirill A. Shutemov if (!inode) { 463779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 464779750d2SKirill A. Shutemov removed++; 465779750d2SKirill A. Shutemov goto next; 466779750d2SKirill A. Shutemov } 467779750d2SKirill A. Shutemov 468779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 469779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 470779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 471253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 472779750d2SKirill A. Shutemov removed++; 473779750d2SKirill A. Shutemov goto next; 474779750d2SKirill A. Shutemov } 475779750d2SKirill A. Shutemov 476779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 477779750d2SKirill A. Shutemov next: 478779750d2SKirill A. Shutemov if (!--batch) 479779750d2SKirill A. Shutemov break; 480779750d2SKirill A. Shutemov } 481779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 482779750d2SKirill A. Shutemov 483253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 484253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 485253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 486253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 487253fd0f0SKirill A. Shutemov iput(inode); 488253fd0f0SKirill A. Shutemov } 489253fd0f0SKirill A. Shutemov 490779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 491779750d2SKirill A. Shutemov int ret; 492779750d2SKirill A. Shutemov 493779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 494779750d2SKirill A. Shutemov inode = &info->vfs_inode; 495779750d2SKirill A. Shutemov 496b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 497b3cd54b2SKirill A. Shutemov goto leave; 498779750d2SKirill A. Shutemov 499b3cd54b2SKirill A. Shutemov page = find_get_page(inode->i_mapping, 500779750d2SKirill A. Shutemov (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); 501779750d2SKirill A. Shutemov if (!page) 502779750d2SKirill A. Shutemov goto drop; 503779750d2SKirill A. Shutemov 504b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 505779750d2SKirill A. Shutemov if (!PageTransHuge(page)) { 506779750d2SKirill A. Shutemov put_page(page); 507779750d2SKirill A. Shutemov goto drop; 508779750d2SKirill A. Shutemov } 509779750d2SKirill A. Shutemov 510b3cd54b2SKirill A. Shutemov /* 511b3cd54b2SKirill A. Shutemov * Leave the inode on the list if we failed to lock 512b3cd54b2SKirill A. Shutemov * the page at this time. 513b3cd54b2SKirill A. Shutemov * 514b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 515b3cd54b2SKirill A. Shutemov * reclaim path. 516b3cd54b2SKirill A. Shutemov */ 517b3cd54b2SKirill A. Shutemov if (!trylock_page(page)) { 518b3cd54b2SKirill A. Shutemov put_page(page); 519b3cd54b2SKirill A. Shutemov goto leave; 520b3cd54b2SKirill A. Shutemov } 521b3cd54b2SKirill A. Shutemov 522779750d2SKirill A. Shutemov ret = split_huge_page(page); 523779750d2SKirill A. Shutemov unlock_page(page); 524779750d2SKirill A. Shutemov put_page(page); 525779750d2SKirill A. Shutemov 526b3cd54b2SKirill A. Shutemov /* If split failed leave the inode on the list */ 527b3cd54b2SKirill A. Shutemov if (ret) 528b3cd54b2SKirill A. Shutemov goto leave; 529779750d2SKirill A. Shutemov 530779750d2SKirill A. Shutemov split++; 531779750d2SKirill A. Shutemov drop: 532779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 533779750d2SKirill A. Shutemov removed++; 534b3cd54b2SKirill A. Shutemov leave: 535779750d2SKirill A. Shutemov iput(inode); 536779750d2SKirill A. Shutemov } 537779750d2SKirill A. Shutemov 538779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 539779750d2SKirill A. Shutemov list_splice_tail(&list, &sbinfo->shrinklist); 540779750d2SKirill A. Shutemov sbinfo->shrinklist_len -= removed; 541779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 542779750d2SKirill A. Shutemov 543779750d2SKirill A. Shutemov return split; 544779750d2SKirill A. Shutemov } 545779750d2SKirill A. Shutemov 546779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 547779750d2SKirill A. Shutemov struct shrink_control *sc) 548779750d2SKirill A. Shutemov { 549779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 550779750d2SKirill A. Shutemov 551779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 552779750d2SKirill A. Shutemov return SHRINK_STOP; 553779750d2SKirill A. Shutemov 554779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 555779750d2SKirill A. Shutemov } 556779750d2SKirill A. Shutemov 557779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 558779750d2SKirill A. Shutemov struct shrink_control *sc) 559779750d2SKirill A. Shutemov { 560779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 561779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 562779750d2SKirill A. Shutemov } 563e496cf3dSKirill A. Shutemov #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5645a6e75f8SKirill A. Shutemov 5655a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 5665a6e75f8SKirill A. Shutemov 567779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 568779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 569779750d2SKirill A. Shutemov { 570779750d2SKirill A. Shutemov return 0; 571779750d2SKirill A. Shutemov } 572e496cf3dSKirill A. Shutemov #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5735a6e75f8SKirill A. Shutemov 57489fdcd26SYang Shi static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) 57589fdcd26SYang Shi { 57689fdcd26SYang Shi if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 57789fdcd26SYang Shi (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) && 57889fdcd26SYang Shi shmem_huge != SHMEM_HUGE_DENY) 57989fdcd26SYang Shi return true; 58089fdcd26SYang Shi return false; 58189fdcd26SYang Shi } 58289fdcd26SYang Shi 5835a6e75f8SKirill A. Shutemov /* 58446f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 58546f65ec1SHugh Dickins */ 58646f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 58746f65ec1SHugh Dickins struct address_space *mapping, 588fed400a1SWang Sheng-Hui pgoff_t index, void *expected) 58946f65ec1SHugh Dickins { 590800d8c63SKirill A. Shutemov int error, nr = hpage_nr_pages(page); 59146f65ec1SHugh Dickins 592800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 593800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(index != round_down(index, nr), page); 594309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 595309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapBacked(page), page); 596800d8c63SKirill A. Shutemov VM_BUG_ON(expected && PageTransHuge(page)); 59746f65ec1SHugh Dickins 598800d8c63SKirill A. Shutemov page_ref_add(page, nr); 59946f65ec1SHugh Dickins page->mapping = mapping; 60046f65ec1SHugh Dickins page->index = index; 60146f65ec1SHugh Dickins 602b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 603800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 604800d8c63SKirill A. Shutemov void __rcu **results; 605800d8c63SKirill A. Shutemov pgoff_t idx; 606800d8c63SKirill A. Shutemov int i; 607800d8c63SKirill A. Shutemov 608800d8c63SKirill A. Shutemov error = 0; 609b93b0163SMatthew Wilcox if (radix_tree_gang_lookup_slot(&mapping->i_pages, 610800d8c63SKirill A. Shutemov &results, &idx, index, 1) && 611800d8c63SKirill A. Shutemov idx < index + HPAGE_PMD_NR) { 612800d8c63SKirill A. Shutemov error = -EEXIST; 613800d8c63SKirill A. Shutemov } 614800d8c63SKirill A. Shutemov 615800d8c63SKirill A. Shutemov if (!error) { 616800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 617b93b0163SMatthew Wilcox error = radix_tree_insert(&mapping->i_pages, 618800d8c63SKirill A. Shutemov index + i, page + i); 619800d8c63SKirill A. Shutemov VM_BUG_ON(error); 620800d8c63SKirill A. Shutemov } 621800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 622800d8c63SKirill A. Shutemov } 623800d8c63SKirill A. Shutemov } else if (!expected) { 624b93b0163SMatthew Wilcox error = radix_tree_insert(&mapping->i_pages, index, page); 625800d8c63SKirill A. Shutemov } else { 626b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 627b065b432SHugh Dickins page); 628800d8c63SKirill A. Shutemov } 629800d8c63SKirill A. Shutemov 63046f65ec1SHugh Dickins if (!error) { 631800d8c63SKirill A. Shutemov mapping->nrpages += nr; 632800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 63311fb9989SMel Gorman __inc_node_page_state(page, NR_SHMEM_THPS); 63411fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); 63511fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); 636b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 63746f65ec1SHugh Dickins } else { 63846f65ec1SHugh Dickins page->mapping = NULL; 639b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 640800d8c63SKirill A. Shutemov page_ref_sub(page, nr); 64146f65ec1SHugh Dickins } 64246f65ec1SHugh Dickins return error; 64346f65ec1SHugh Dickins } 64446f65ec1SHugh Dickins 64546f65ec1SHugh Dickins /* 6466922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 6476922c0c7SHugh Dickins */ 6486922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 6496922c0c7SHugh Dickins { 6506922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 6516922c0c7SHugh Dickins int error; 6526922c0c7SHugh Dickins 653800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 654800d8c63SKirill A. Shutemov 655b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 6566922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 6576922c0c7SHugh Dickins page->mapping = NULL; 6586922c0c7SHugh Dickins mapping->nrpages--; 65911fb9989SMel Gorman __dec_node_page_state(page, NR_FILE_PAGES); 66011fb9989SMel Gorman __dec_node_page_state(page, NR_SHMEM); 661b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 66209cbfeafSKirill A. Shutemov put_page(page); 6636922c0c7SHugh Dickins BUG_ON(error); 6646922c0c7SHugh Dickins } 6656922c0c7SHugh Dickins 6666922c0c7SHugh Dickins /* 6677a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 6687a5d0fbbSHugh Dickins */ 6697a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 6707a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 6717a5d0fbbSHugh Dickins { 6726dbaf22cSJohannes Weiner void *old; 6737a5d0fbbSHugh Dickins 674b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 675b93b0163SMatthew Wilcox old = radix_tree_delete_item(&mapping->i_pages, index, radswap); 676b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 6776dbaf22cSJohannes Weiner if (old != radswap) 6786dbaf22cSJohannes Weiner return -ENOENT; 6797a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 6806dbaf22cSJohannes Weiner return 0; 6817a5d0fbbSHugh Dickins } 6827a5d0fbbSHugh Dickins 6837a5d0fbbSHugh Dickins /* 6846a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 68548131e03SVlastimil Babka * given offsets are swapped out. 6866a15a370SVlastimil Babka * 687b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 6886a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 6896a15a370SVlastimil Babka */ 69048131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 69148131e03SVlastimil Babka pgoff_t start, pgoff_t end) 6926a15a370SVlastimil Babka { 6936a15a370SVlastimil Babka struct radix_tree_iter iter; 6945b9c98f3SMike Kravetz void __rcu **slot; 6956a15a370SVlastimil Babka struct page *page; 69648131e03SVlastimil Babka unsigned long swapped = 0; 6976a15a370SVlastimil Babka 6986a15a370SVlastimil Babka rcu_read_lock(); 6996a15a370SVlastimil Babka 700b93b0163SMatthew Wilcox radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) { 7016a15a370SVlastimil Babka if (iter.index >= end) 7026a15a370SVlastimil Babka break; 7036a15a370SVlastimil Babka 7046a15a370SVlastimil Babka page = radix_tree_deref_slot(slot); 7056a15a370SVlastimil Babka 7062cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 7072cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 7082cf938aaSMatthew Wilcox continue; 7092cf938aaSMatthew Wilcox } 7106a15a370SVlastimil Babka 7116a15a370SVlastimil Babka if (radix_tree_exceptional_entry(page)) 7126a15a370SVlastimil Babka swapped++; 7136a15a370SVlastimil Babka 7146a15a370SVlastimil Babka if (need_resched()) { 715148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 7166a15a370SVlastimil Babka cond_resched_rcu(); 7176a15a370SVlastimil Babka } 7186a15a370SVlastimil Babka } 7196a15a370SVlastimil Babka 7206a15a370SVlastimil Babka rcu_read_unlock(); 7216a15a370SVlastimil Babka 7226a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 7236a15a370SVlastimil Babka } 7246a15a370SVlastimil Babka 7256a15a370SVlastimil Babka /* 72648131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 72748131e03SVlastimil Babka * given vma is swapped out. 72848131e03SVlastimil Babka * 729b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 73048131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 73148131e03SVlastimil Babka */ 73248131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 73348131e03SVlastimil Babka { 73448131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 73548131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 73648131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 73748131e03SVlastimil Babka unsigned long swapped; 73848131e03SVlastimil Babka 73948131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 74048131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 74148131e03SVlastimil Babka 74248131e03SVlastimil Babka /* 74348131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 74448131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 74548131e03SVlastimil Babka * already track. 74648131e03SVlastimil Babka */ 74748131e03SVlastimil Babka if (!swapped) 74848131e03SVlastimil Babka return 0; 74948131e03SVlastimil Babka 75048131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 75148131e03SVlastimil Babka return swapped << PAGE_SHIFT; 75248131e03SVlastimil Babka 75348131e03SVlastimil Babka /* Here comes the more involved part */ 75448131e03SVlastimil Babka return shmem_partial_swap_usage(mapping, 75548131e03SVlastimil Babka linear_page_index(vma, vma->vm_start), 75648131e03SVlastimil Babka linear_page_index(vma, vma->vm_end)); 75748131e03SVlastimil Babka } 75848131e03SVlastimil Babka 75948131e03SVlastimil Babka /* 76024513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 76124513264SHugh Dickins */ 76224513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 76324513264SHugh Dickins { 76424513264SHugh Dickins struct pagevec pvec; 76524513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 76624513264SHugh Dickins pgoff_t index = 0; 76724513264SHugh Dickins 76886679820SMel Gorman pagevec_init(&pvec); 76924513264SHugh Dickins /* 77024513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 77124513264SHugh Dickins */ 77224513264SHugh Dickins while (!mapping_unevictable(mapping)) { 77324513264SHugh Dickins /* 77424513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 77524513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 77624513264SHugh Dickins */ 7770cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 77824513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 77924513264SHugh Dickins if (!pvec.nr) 78024513264SHugh Dickins break; 78124513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 7820cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 78324513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 78424513264SHugh Dickins pagevec_release(&pvec); 78524513264SHugh Dickins cond_resched(); 78624513264SHugh Dickins } 7877a5d0fbbSHugh Dickins } 7887a5d0fbbSHugh Dickins 7897a5d0fbbSHugh Dickins /* 7907a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 7911635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 7927a5d0fbbSHugh Dickins */ 7931635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 7941635f6a7SHugh Dickins bool unfalloc) 7951da177e4SLinus Torvalds { 796285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 7971da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 79809cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 79909cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 80009cbfeafSKirill A. Shutemov unsigned int partial_start = lstart & (PAGE_SIZE - 1); 80109cbfeafSKirill A. Shutemov unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); 802bda97eabSHugh Dickins struct pagevec pvec; 8037a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 8047a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 805285b2c4fSHugh Dickins pgoff_t index; 806bda97eabSHugh Dickins int i; 8071da177e4SLinus Torvalds 80883e4fa9cSHugh Dickins if (lend == -1) 80983e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 810bda97eabSHugh Dickins 81186679820SMel Gorman pagevec_init(&pvec); 812bda97eabSHugh Dickins index = start; 81383e4fa9cSHugh Dickins while (index < end) { 8140cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 81583e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 8167a5d0fbbSHugh Dickins pvec.pages, indices); 8177a5d0fbbSHugh Dickins if (!pvec.nr) 8187a5d0fbbSHugh Dickins break; 819bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 820bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 821bda97eabSHugh Dickins 8227a5d0fbbSHugh Dickins index = indices[i]; 82383e4fa9cSHugh Dickins if (index >= end) 824bda97eabSHugh Dickins break; 825bda97eabSHugh Dickins 8267a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 8271635f6a7SHugh Dickins if (unfalloc) 8281635f6a7SHugh Dickins continue; 8297a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 8307a5d0fbbSHugh Dickins index, page); 8317a5d0fbbSHugh Dickins continue; 8327a5d0fbbSHugh Dickins } 8337a5d0fbbSHugh Dickins 834800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); 835800d8c63SKirill A. Shutemov 836bda97eabSHugh Dickins if (!trylock_page(page)) 837bda97eabSHugh Dickins continue; 838800d8c63SKirill A. Shutemov 839800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 840800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 841800d8c63SKirill A. Shutemov clear_highpage(page); 842800d8c63SKirill A. Shutemov unlock_page(page); 843800d8c63SKirill A. Shutemov continue; 844800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 845800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 846800d8c63SKirill A. Shutemov /* 847800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 848800d8c63SKirill A. Shutemov * zero out the page 849800d8c63SKirill A. Shutemov */ 850800d8c63SKirill A. Shutemov clear_highpage(page); 851800d8c63SKirill A. Shutemov unlock_page(page); 852800d8c63SKirill A. Shutemov continue; 853800d8c63SKirill A. Shutemov } 854800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 855800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 856800d8c63SKirill A. Shutemov } 857800d8c63SKirill A. Shutemov 8581635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 859800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 860800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 861309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 862bda97eabSHugh Dickins truncate_inode_page(mapping, page); 8637a5d0fbbSHugh Dickins } 8641635f6a7SHugh Dickins } 865bda97eabSHugh Dickins unlock_page(page); 866bda97eabSHugh Dickins } 8670cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 86824513264SHugh Dickins pagevec_release(&pvec); 869bda97eabSHugh Dickins cond_resched(); 870bda97eabSHugh Dickins index++; 871bda97eabSHugh Dickins } 872bda97eabSHugh Dickins 87383e4fa9cSHugh Dickins if (partial_start) { 874bda97eabSHugh Dickins struct page *page = NULL; 8759e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, start - 1, &page, SGP_READ); 876bda97eabSHugh Dickins if (page) { 87709cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 87883e4fa9cSHugh Dickins if (start > end) { 87983e4fa9cSHugh Dickins top = partial_end; 88083e4fa9cSHugh Dickins partial_end = 0; 88183e4fa9cSHugh Dickins } 88283e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 883bda97eabSHugh Dickins set_page_dirty(page); 884bda97eabSHugh Dickins unlock_page(page); 88509cbfeafSKirill A. Shutemov put_page(page); 886bda97eabSHugh Dickins } 887bda97eabSHugh Dickins } 88883e4fa9cSHugh Dickins if (partial_end) { 88983e4fa9cSHugh Dickins struct page *page = NULL; 8909e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, end, &page, SGP_READ); 89183e4fa9cSHugh Dickins if (page) { 89283e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 89383e4fa9cSHugh Dickins set_page_dirty(page); 89483e4fa9cSHugh Dickins unlock_page(page); 89509cbfeafSKirill A. Shutemov put_page(page); 89683e4fa9cSHugh Dickins } 89783e4fa9cSHugh Dickins } 89883e4fa9cSHugh Dickins if (start >= end) 89983e4fa9cSHugh Dickins return; 900bda97eabSHugh Dickins 901bda97eabSHugh Dickins index = start; 902b1a36650SHugh Dickins while (index < end) { 903bda97eabSHugh Dickins cond_resched(); 9040cd6144aSJohannes Weiner 9050cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 90683e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 9077a5d0fbbSHugh Dickins pvec.pages, indices); 9087a5d0fbbSHugh Dickins if (!pvec.nr) { 909b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 910b1a36650SHugh Dickins if (index == start || end != -1) 911bda97eabSHugh Dickins break; 912b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 913bda97eabSHugh Dickins index = start; 914bda97eabSHugh Dickins continue; 915bda97eabSHugh Dickins } 916bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 917bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 918bda97eabSHugh Dickins 9197a5d0fbbSHugh Dickins index = indices[i]; 92083e4fa9cSHugh Dickins if (index >= end) 921bda97eabSHugh Dickins break; 922bda97eabSHugh Dickins 9237a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 9241635f6a7SHugh Dickins if (unfalloc) 9251635f6a7SHugh Dickins continue; 926b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 927b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 928b1a36650SHugh Dickins index--; 929b1a36650SHugh Dickins break; 930b1a36650SHugh Dickins } 931b1a36650SHugh Dickins nr_swaps_freed++; 9327a5d0fbbSHugh Dickins continue; 9337a5d0fbbSHugh Dickins } 9347a5d0fbbSHugh Dickins 935bda97eabSHugh Dickins lock_page(page); 936800d8c63SKirill A. Shutemov 937800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 938800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 939800d8c63SKirill A. Shutemov clear_highpage(page); 940800d8c63SKirill A. Shutemov unlock_page(page); 941800d8c63SKirill A. Shutemov /* 942800d8c63SKirill A. Shutemov * Partial thp truncate due 'start' in middle 943800d8c63SKirill A. Shutemov * of THP: don't need to look on these pages 944800d8c63SKirill A. Shutemov * again on !pvec.nr restart. 945800d8c63SKirill A. Shutemov */ 946800d8c63SKirill A. Shutemov if (index != round_down(end, HPAGE_PMD_NR)) 947800d8c63SKirill A. Shutemov start++; 948800d8c63SKirill A. Shutemov continue; 949800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 950800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 951800d8c63SKirill A. Shutemov /* 952800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 953800d8c63SKirill A. Shutemov * zero out the page 954800d8c63SKirill A. Shutemov */ 955800d8c63SKirill A. Shutemov clear_highpage(page); 956800d8c63SKirill A. Shutemov unlock_page(page); 957800d8c63SKirill A. Shutemov continue; 958800d8c63SKirill A. Shutemov } 959800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 960800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 961800d8c63SKirill A. Shutemov } 962800d8c63SKirill A. Shutemov 9631635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 964800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 965800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 966309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 967bda97eabSHugh Dickins truncate_inode_page(mapping, page); 968b1a36650SHugh Dickins } else { 969b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 970b1a36650SHugh Dickins unlock_page(page); 971b1a36650SHugh Dickins index--; 972b1a36650SHugh Dickins break; 9737a5d0fbbSHugh Dickins } 9741635f6a7SHugh Dickins } 975bda97eabSHugh Dickins unlock_page(page); 976bda97eabSHugh Dickins } 9770cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 97824513264SHugh Dickins pagevec_release(&pvec); 979bda97eabSHugh Dickins index++; 980bda97eabSHugh Dickins } 98194c1e62dSHugh Dickins 9824595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 9837a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 9841da177e4SLinus Torvalds shmem_recalc_inode(inode); 9854595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 9861635f6a7SHugh Dickins } 9871da177e4SLinus Torvalds 9881635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 9891635f6a7SHugh Dickins { 9901635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 991078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 9921da177e4SLinus Torvalds } 99394c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 9941da177e4SLinus Torvalds 995a528d35eSDavid Howells static int shmem_getattr(const struct path *path, struct kstat *stat, 996a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 99744a30220SYu Zhao { 998a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 99944a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 100089fdcd26SYang Shi struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb); 100144a30220SYu Zhao 1002d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 10034595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 100444a30220SYu Zhao shmem_recalc_inode(inode); 10054595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1006d0424c42SHugh Dickins } 100744a30220SYu Zhao generic_fillattr(inode, stat); 100889fdcd26SYang Shi 100989fdcd26SYang Shi if (is_huge_enabled(sb_info)) 101089fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 101189fdcd26SYang Shi 101244a30220SYu Zhao return 0; 101344a30220SYu Zhao } 101444a30220SYu Zhao 101594c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 10161da177e4SLinus Torvalds { 101775c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 101840e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 1019779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10201da177e4SLinus Torvalds int error; 10211da177e4SLinus Torvalds 102231051c85SJan Kara error = setattr_prepare(dentry, attr); 1023db78b877SChristoph Hellwig if (error) 1024db78b877SChristoph Hellwig return error; 1025db78b877SChristoph Hellwig 102694c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 102794c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 102894c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 10293889e6e7Snpiggin@suse.de 103040e041a2SDavid Herrmann /* protected by i_mutex */ 103140e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 103240e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 103340e041a2SDavid Herrmann return -EPERM; 103440e041a2SDavid Herrmann 103594c1e62dSHugh Dickins if (newsize != oldsize) { 103677142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 103777142517SKonstantin Khlebnikov oldsize, newsize); 103877142517SKonstantin Khlebnikov if (error) 103977142517SKonstantin Khlebnikov return error; 104094c1e62dSHugh Dickins i_size_write(inode, newsize); 1041078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 104294c1e62dSHugh Dickins } 1043afa2db2fSJosef Bacik if (newsize <= oldsize) { 104494c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1045d0424c42SHugh Dickins if (oldsize > holebegin) 1046d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1047d0424c42SHugh Dickins holebegin, 0, 1); 1048d0424c42SHugh Dickins if (info->alloced) 1049d0424c42SHugh Dickins shmem_truncate_range(inode, 1050d0424c42SHugh Dickins newsize, (loff_t)-1); 105194c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1052d0424c42SHugh Dickins if (oldsize > holebegin) 1053d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1054d0424c42SHugh Dickins holebegin, 0, 1); 1055779750d2SKirill A. Shutemov 1056779750d2SKirill A. Shutemov /* 1057779750d2SKirill A. Shutemov * Part of the huge page can be beyond i_size: subject 1058779750d2SKirill A. Shutemov * to shrink under memory pressure. 1059779750d2SKirill A. Shutemov */ 1060779750d2SKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 1061779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1062d041353dSCong Wang /* 1063d041353dSCong Wang * _careful to defend against unlocked access to 1064d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1065d041353dSCong Wang */ 1066d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1067779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1068779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1069779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1070779750d2SKirill A. Shutemov } 1071779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1072779750d2SKirill A. Shutemov } 107394c1e62dSHugh Dickins } 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10766a1a90adSChristoph Hellwig setattr_copy(inode, attr); 1077db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1078feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 10791da177e4SLinus Torvalds return error; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds 10821f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 10831da177e4SLinus Torvalds { 10841da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1085779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10861da177e4SLinus Torvalds 10873889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 10881da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 10891da177e4SLinus Torvalds inode->i_size = 0; 10903889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1091779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1092779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1093779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1094779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1095779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1096779750d2SKirill A. Shutemov } 1097779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1098779750d2SKirill A. Shutemov } 10991da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 1100cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 11011da177e4SLinus Torvalds list_del_init(&info->swaplist); 1102cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 11031da177e4SLinus Torvalds } 11043ed47db3SAl Viro } 1105b09e0fa4SEric Paris 110638f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 11070f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 11085b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1109dbd5768fSJan Kara clear_inode(inode); 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 1112478922e2SMatthew Wilcox static unsigned long find_swap_entry(struct radix_tree_root *root, void *item) 1113478922e2SMatthew Wilcox { 1114478922e2SMatthew Wilcox struct radix_tree_iter iter; 11155b9c98f3SMike Kravetz void __rcu **slot; 1116478922e2SMatthew Wilcox unsigned long found = -1; 1117478922e2SMatthew Wilcox unsigned int checked = 0; 1118478922e2SMatthew Wilcox 1119478922e2SMatthew Wilcox rcu_read_lock(); 1120478922e2SMatthew Wilcox radix_tree_for_each_slot(slot, root, &iter, 0) { 11215b9c98f3SMike Kravetz void *entry = radix_tree_deref_slot(slot); 11225b9c98f3SMike Kravetz 11235b9c98f3SMike Kravetz if (radix_tree_deref_retry(entry)) { 11245b9c98f3SMike Kravetz slot = radix_tree_iter_retry(&iter); 11255b9c98f3SMike Kravetz continue; 11265b9c98f3SMike Kravetz } 11275b9c98f3SMike Kravetz if (entry == item) { 1128478922e2SMatthew Wilcox found = iter.index; 1129478922e2SMatthew Wilcox break; 1130478922e2SMatthew Wilcox } 1131478922e2SMatthew Wilcox checked++; 1132478922e2SMatthew Wilcox if ((checked % 4096) != 0) 1133478922e2SMatthew Wilcox continue; 1134478922e2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 1135478922e2SMatthew Wilcox cond_resched_rcu(); 1136478922e2SMatthew Wilcox } 1137478922e2SMatthew Wilcox 1138478922e2SMatthew Wilcox rcu_read_unlock(); 1139478922e2SMatthew Wilcox return found; 1140478922e2SMatthew Wilcox } 1141478922e2SMatthew Wilcox 114246f65ec1SHugh Dickins /* 114346f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 114446f65ec1SHugh Dickins */ 114541ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 1146bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 11471da177e4SLinus Torvalds { 1148285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 114946f65ec1SHugh Dickins void *radswap; 115041ffe5d5SHugh Dickins pgoff_t index; 1151bde05d1cSHugh Dickins gfp_t gfp; 1152bde05d1cSHugh Dickins int error = 0; 11531da177e4SLinus Torvalds 115446f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 1155b93b0163SMatthew Wilcox index = find_swap_entry(&mapping->i_pages, radswap); 115646f65ec1SHugh Dickins if (index == -1) 115700501b53SJohannes Weiner return -EAGAIN; /* tell shmem_unuse we found nothing */ 11582e0e26c7SHugh Dickins 11591b1b32f2SHugh Dickins /* 11601b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 11611f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 11621b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 1163285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 11641b1b32f2SHugh Dickins */ 11651b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 11662e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 11672e0e26c7SHugh Dickins 1168bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 1169bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 1170bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1171bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 1172bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1173bde05d1cSHugh Dickins /* 1174bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 11750142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 11760142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 11770142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 11780142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 11790142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 11800142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 11810142ef6cSHugh Dickins * 11820142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 11830142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 11840142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 11850142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 11860142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 11870142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 11880142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 11890142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 1190bde05d1cSHugh Dickins */ 1191bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 1192bde05d1cSHugh Dickins error = -ENOENT; 1193bde05d1cSHugh Dickins } 1194bde05d1cSHugh Dickins 1195d13d1443SKAMEZAWA Hiroyuki /* 1196778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 1197778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 1198778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 1199d13d1443SKAMEZAWA Hiroyuki */ 1200bde05d1cSHugh Dickins if (!error) 1201bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 1202fed400a1SWang Sheng-Hui radswap); 120348f170fbSHugh Dickins if (error != -ENOMEM) { 120446f65ec1SHugh Dickins /* 120546f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 120646f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 120746f65ec1SHugh Dickins */ 1208bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 1209bde05d1cSHugh Dickins set_page_dirty(*pagep); 121046f65ec1SHugh Dickins if (!error) { 12114595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1212285b2c4fSHugh Dickins info->swapped--; 12134595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 121441ffe5d5SHugh Dickins swap_free(swap); 121546f65ec1SHugh Dickins } 12161da177e4SLinus Torvalds } 12172e0e26c7SHugh Dickins return error; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /* 122146f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 12221da177e4SLinus Torvalds */ 122341ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 12241da177e4SLinus Torvalds { 122541ffe5d5SHugh Dickins struct list_head *this, *next; 12261da177e4SLinus Torvalds struct shmem_inode_info *info; 122700501b53SJohannes Weiner struct mem_cgroup *memcg; 1228bde05d1cSHugh Dickins int error = 0; 1229bde05d1cSHugh Dickins 1230bde05d1cSHugh Dickins /* 1231bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 12320142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 1233bde05d1cSHugh Dickins */ 12340142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 1235bde05d1cSHugh Dickins goto out; 1236778dd893SHugh Dickins 1237778dd893SHugh Dickins /* 1238778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 1239778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 1240778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 1241778dd893SHugh Dickins */ 1242f627c2f5SKirill A. Shutemov error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg, 1243f627c2f5SKirill A. Shutemov false); 1244778dd893SHugh Dickins if (error) 1245778dd893SHugh Dickins goto out; 124646f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 124700501b53SJohannes Weiner error = -EAGAIN; 12481da177e4SLinus Torvalds 1249cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 125041ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 125141ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 1252285b2c4fSHugh Dickins if (info->swapped) 125300501b53SJohannes Weiner error = shmem_unuse_inode(info, swap, &page); 12546922c0c7SHugh Dickins else 12556922c0c7SHugh Dickins list_del_init(&info->swaplist); 1256cb5f7b9aSHugh Dickins cond_resched(); 125700501b53SJohannes Weiner if (error != -EAGAIN) 1258778dd893SHugh Dickins break; 125900501b53SJohannes Weiner /* found nothing in this: move on to search the next */ 12601da177e4SLinus Torvalds } 1261cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1262778dd893SHugh Dickins 126300501b53SJohannes Weiner if (error) { 126400501b53SJohannes Weiner if (error != -ENOMEM) 126500501b53SJohannes Weiner error = 0; 1266f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 126700501b53SJohannes Weiner } else 1268f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 1269778dd893SHugh Dickins out: 1270aaa46865SHugh Dickins unlock_page(page); 127109cbfeafSKirill A. Shutemov put_page(page); 1272778dd893SHugh Dickins return error; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds /* 12761da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 12771da177e4SLinus Torvalds */ 12781da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 12791da177e4SLinus Torvalds { 12801da177e4SLinus Torvalds struct shmem_inode_info *info; 12811da177e4SLinus Torvalds struct address_space *mapping; 12821da177e4SLinus Torvalds struct inode *inode; 12836922c0c7SHugh Dickins swp_entry_t swap; 12846922c0c7SHugh Dickins pgoff_t index; 12851da177e4SLinus Torvalds 1286800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 12871da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 12881da177e4SLinus Torvalds mapping = page->mapping; 12891da177e4SLinus Torvalds index = page->index; 12901da177e4SLinus Torvalds inode = mapping->host; 12911da177e4SLinus Torvalds info = SHMEM_I(inode); 12921da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 12931da177e4SLinus Torvalds goto redirty; 1294d9fe526aSHugh Dickins if (!total_swap_pages) 12951da177e4SLinus Torvalds goto redirty; 12961da177e4SLinus Torvalds 1297d9fe526aSHugh Dickins /* 129897b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 129997b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 130097b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 130197b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 130297b713baSChristoph Hellwig * threads or sync. 1303d9fe526aSHugh Dickins */ 130448f170fbSHugh Dickins if (!wbc->for_reclaim) { 130548f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 130648f170fbSHugh Dickins goto redirty; 130748f170fbSHugh Dickins } 13081635f6a7SHugh Dickins 13091635f6a7SHugh Dickins /* 13101635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 13111635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 13121635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 13131aac1400SHugh Dickins * 13141aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 13151aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 13161aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 13171aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 13181aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 13191635f6a7SHugh Dickins */ 13201635f6a7SHugh Dickins if (!PageUptodate(page)) { 13211aac1400SHugh Dickins if (inode->i_private) { 13221aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 13231aac1400SHugh Dickins spin_lock(&inode->i_lock); 13241aac1400SHugh Dickins shmem_falloc = inode->i_private; 13251aac1400SHugh Dickins if (shmem_falloc && 13268e205f77SHugh Dickins !shmem_falloc->waitq && 13271aac1400SHugh Dickins index >= shmem_falloc->start && 13281aac1400SHugh Dickins index < shmem_falloc->next) 13291aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 13301aac1400SHugh Dickins else 13311aac1400SHugh Dickins shmem_falloc = NULL; 13321aac1400SHugh Dickins spin_unlock(&inode->i_lock); 13331aac1400SHugh Dickins if (shmem_falloc) 13341aac1400SHugh Dickins goto redirty; 13351aac1400SHugh Dickins } 13361635f6a7SHugh Dickins clear_highpage(page); 13371635f6a7SHugh Dickins flush_dcache_page(page); 13381635f6a7SHugh Dickins SetPageUptodate(page); 13391635f6a7SHugh Dickins } 13401635f6a7SHugh Dickins 134138d8b4e6SHuang Ying swap = get_swap_page(page); 134248f170fbSHugh Dickins if (!swap.val) 134348f170fbSHugh Dickins goto redirty; 1344d9fe526aSHugh Dickins 1345b1dea800SHugh Dickins /* 1346b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 13476922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 13486922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1349b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 13506922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 13516922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1352b1dea800SHugh Dickins */ 1353b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 135405bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 135505bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 1356b1dea800SHugh Dickins 135748f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 13584595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1359267a4c76SHugh Dickins shmem_recalc_inode(inode); 1360267a4c76SHugh Dickins info->swapped++; 13614595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1362267a4c76SHugh Dickins 1363aaa46865SHugh Dickins swap_shmem_alloc(swap); 13646922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 13656922c0c7SHugh Dickins 13666922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1367d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 13689fab5619SHugh Dickins swap_writepage(page, wbc); 13691da177e4SLinus Torvalds return 0; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13726922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 137375f6d6d2SMinchan Kim put_swap_page(page, swap); 13741da177e4SLinus Torvalds redirty: 13751da177e4SLinus Torvalds set_page_dirty(page); 1376d9fe526aSHugh Dickins if (wbc->for_reclaim) 1377d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1378d9fe526aSHugh Dickins unlock_page(page); 1379d9fe526aSHugh Dickins return 0; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 138275edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 138371fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1384680d794bSakpm@linux-foundation.org { 1385680d794bSakpm@linux-foundation.org char buffer[64]; 1386680d794bSakpm@linux-foundation.org 138771fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1388095f1fc4SLee Schermerhorn return; /* show nothing */ 1389095f1fc4SLee Schermerhorn 1390a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1391095f1fc4SLee Schermerhorn 1392095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1393680d794bSakpm@linux-foundation.org } 139471fe804bSLee Schermerhorn 139571fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 139671fe804bSLee Schermerhorn { 139771fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 139871fe804bSLee Schermerhorn if (sbinfo->mpol) { 139971fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 140071fe804bSLee Schermerhorn mpol = sbinfo->mpol; 140171fe804bSLee Schermerhorn mpol_get(mpol); 140271fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 140371fe804bSLee Schermerhorn } 140471fe804bSLee Schermerhorn return mpol; 140571fe804bSLee Schermerhorn } 140675edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 140775edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 140875edd345SHugh Dickins { 140975edd345SHugh Dickins } 141075edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 141175edd345SHugh Dickins { 141275edd345SHugh Dickins return NULL; 141375edd345SHugh Dickins } 141475edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 141575edd345SHugh Dickins #ifndef CONFIG_NUMA 141675edd345SHugh Dickins #define vm_policy vm_private_data 141775edd345SHugh Dickins #endif 1418680d794bSakpm@linux-foundation.org 1419800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1420800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1421800d8c63SKirill A. Shutemov { 1422800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 1423daa28075SKirill A. Shutemov memset(vma, 0, sizeof(*vma)); 1424800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1425800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1426800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1427800d8c63SKirill A. Shutemov } 1428800d8c63SKirill A. Shutemov 1429800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1430800d8c63SKirill A. Shutemov { 1431800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1432800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1433800d8c63SKirill A. Shutemov } 1434800d8c63SKirill A. Shutemov 143541ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 143641ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 14371da177e4SLinus Torvalds { 14381da177e4SLinus Torvalds struct vm_area_struct pvma; 143918a2f371SMel Gorman struct page *page; 1440e9e9b7ecSMinchan Kim struct vm_fault vmf; 14411da177e4SLinus Torvalds 1442800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1443e9e9b7ecSMinchan Kim vmf.vma = &pvma; 1444e9e9b7ecSMinchan Kim vmf.address = 0; 1445e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1446800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 144718a2f371SMel Gorman 1448800d8c63SKirill A. Shutemov return page; 1449800d8c63SKirill A. Shutemov } 145018a2f371SMel Gorman 1451800d8c63SKirill A. Shutemov static struct page *shmem_alloc_hugepage(gfp_t gfp, 1452800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1453800d8c63SKirill A. Shutemov { 1454800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 1455800d8c63SKirill A. Shutemov struct inode *inode = &info->vfs_inode; 1456800d8c63SKirill A. Shutemov struct address_space *mapping = inode->i_mapping; 14574620a06eSGeert Uytterhoeven pgoff_t idx, hindex; 1458800d8c63SKirill A. Shutemov void __rcu **results; 1459800d8c63SKirill A. Shutemov struct page *page; 1460800d8c63SKirill A. Shutemov 1461e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1462800d8c63SKirill A. Shutemov return NULL; 1463800d8c63SKirill A. Shutemov 14644620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 1465800d8c63SKirill A. Shutemov rcu_read_lock(); 1466b93b0163SMatthew Wilcox if (radix_tree_gang_lookup_slot(&mapping->i_pages, &results, &idx, 1467800d8c63SKirill A. Shutemov hindex, 1) && idx < hindex + HPAGE_PMD_NR) { 1468800d8c63SKirill A. Shutemov rcu_read_unlock(); 1469800d8c63SKirill A. Shutemov return NULL; 1470800d8c63SKirill A. Shutemov } 1471800d8c63SKirill A. Shutemov rcu_read_unlock(); 1472800d8c63SKirill A. Shutemov 1473800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1474800d8c63SKirill A. Shutemov page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, 1475800d8c63SKirill A. Shutemov HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); 1476800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1477800d8c63SKirill A. Shutemov if (page) 1478800d8c63SKirill A. Shutemov prep_transhuge_page(page); 147918a2f371SMel Gorman return page; 148018a2f371SMel Gorman } 148118a2f371SMel Gorman 148218a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 148318a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 148418a2f371SMel Gorman { 148518a2f371SMel Gorman struct vm_area_struct pvma; 148618a2f371SMel Gorman struct page *page; 148718a2f371SMel Gorman 1488800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1489800d8c63SKirill A. Shutemov page = alloc_page_vma(gfp, &pvma, 0); 1490800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 149118a2f371SMel Gorman 1492800d8c63SKirill A. Shutemov return page; 1493800d8c63SKirill A. Shutemov } 1494800d8c63SKirill A. Shutemov 1495800d8c63SKirill A. Shutemov static struct page *shmem_alloc_and_acct_page(gfp_t gfp, 14960f079694SMike Rapoport struct inode *inode, 1497800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1498800d8c63SKirill A. Shutemov { 14990f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 1500800d8c63SKirill A. Shutemov struct page *page; 1501800d8c63SKirill A. Shutemov int nr; 1502800d8c63SKirill A. Shutemov int err = -ENOSPC; 1503800d8c63SKirill A. Shutemov 1504e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1505800d8c63SKirill A. Shutemov huge = false; 1506800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1507800d8c63SKirill A. Shutemov 15080f079694SMike Rapoport if (!shmem_inode_acct_block(inode, nr)) 1509800d8c63SKirill A. Shutemov goto failed; 1510800d8c63SKirill A. Shutemov 1511800d8c63SKirill A. Shutemov if (huge) 1512800d8c63SKirill A. Shutemov page = shmem_alloc_hugepage(gfp, info, index); 1513800d8c63SKirill A. Shutemov else 1514800d8c63SKirill A. Shutemov page = shmem_alloc_page(gfp, info, index); 151575edd345SHugh Dickins if (page) { 151675edd345SHugh Dickins __SetPageLocked(page); 151775edd345SHugh Dickins __SetPageSwapBacked(page); 1518800d8c63SKirill A. Shutemov return page; 151975edd345SHugh Dickins } 152018a2f371SMel Gorman 1521800d8c63SKirill A. Shutemov err = -ENOMEM; 15220f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1523800d8c63SKirill A. Shutemov failed: 1524800d8c63SKirill A. Shutemov return ERR_PTR(err); 15251da177e4SLinus Torvalds } 152671fe804bSLee Schermerhorn 15271da177e4SLinus Torvalds /* 1528bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1529bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 1530bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1531bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1532bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1533bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1534bde05d1cSHugh Dickins * 1535bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1536bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1537bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1538bde05d1cSHugh Dickins */ 1539bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 1540bde05d1cSHugh Dickins { 1541bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 1542bde05d1cSHugh Dickins } 1543bde05d1cSHugh Dickins 1544bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 1545bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1546bde05d1cSHugh Dickins { 1547bde05d1cSHugh Dickins struct page *oldpage, *newpage; 1548bde05d1cSHugh Dickins struct address_space *swap_mapping; 1549bde05d1cSHugh Dickins pgoff_t swap_index; 1550bde05d1cSHugh Dickins int error; 1551bde05d1cSHugh Dickins 1552bde05d1cSHugh Dickins oldpage = *pagep; 1553bde05d1cSHugh Dickins swap_index = page_private(oldpage); 1554bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1555bde05d1cSHugh Dickins 1556bde05d1cSHugh Dickins /* 1557bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1558bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1559bde05d1cSHugh Dickins */ 1560bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1561bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1562bde05d1cSHugh Dickins if (!newpage) 1563bde05d1cSHugh Dickins return -ENOMEM; 1564bde05d1cSHugh Dickins 156509cbfeafSKirill A. Shutemov get_page(newpage); 1566bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 15670142ef6cSHugh Dickins flush_dcache_page(newpage); 1568bde05d1cSHugh Dickins 15699956edf3SHugh Dickins __SetPageLocked(newpage); 15709956edf3SHugh Dickins __SetPageSwapBacked(newpage); 1571bde05d1cSHugh Dickins SetPageUptodate(newpage); 1572bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 1573bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1574bde05d1cSHugh Dickins 1575bde05d1cSHugh Dickins /* 1576bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1577bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1578bde05d1cSHugh Dickins */ 1579b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 1580bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 1581bde05d1cSHugh Dickins newpage); 15820142ef6cSHugh Dickins if (!error) { 158311fb9989SMel Gorman __inc_node_page_state(newpage, NR_FILE_PAGES); 158411fb9989SMel Gorman __dec_node_page_state(oldpage, NR_FILE_PAGES); 15850142ef6cSHugh Dickins } 1586b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1587bde05d1cSHugh Dickins 15880142ef6cSHugh Dickins if (unlikely(error)) { 15890142ef6cSHugh Dickins /* 15900142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 15910142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 15920142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 15930142ef6cSHugh Dickins */ 15940142ef6cSHugh Dickins oldpage = newpage; 15950142ef6cSHugh Dickins } else { 15966a93ca8fSJohannes Weiner mem_cgroup_migrate(oldpage, newpage); 1597bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 15980142ef6cSHugh Dickins *pagep = newpage; 15990142ef6cSHugh Dickins } 1600bde05d1cSHugh Dickins 1601bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1602bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1603bde05d1cSHugh Dickins 1604bde05d1cSHugh Dickins unlock_page(oldpage); 160509cbfeafSKirill A. Shutemov put_page(oldpage); 160609cbfeafSKirill A. Shutemov put_page(oldpage); 16070142ef6cSHugh Dickins return error; 1608bde05d1cSHugh Dickins } 1609bde05d1cSHugh Dickins 1610bde05d1cSHugh Dickins /* 161168da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 16121da177e4SLinus Torvalds * 16131da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 16141da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 16159e18eb29SAndres Lagar-Cavilla * entry since a page cannot live in both the swap and page cache. 16169e18eb29SAndres Lagar-Cavilla * 16179e18eb29SAndres Lagar-Cavilla * fault_mm and fault_type are only supplied by shmem_fault: 16189e18eb29SAndres Lagar-Cavilla * otherwise they are NULL. 16191da177e4SLinus Torvalds */ 162041ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 16219e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, gfp_t gfp, 1622cfda0526SMike Rapoport struct vm_area_struct *vma, struct vm_fault *vmf, int *fault_type) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 162523f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 16261da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 16279e18eb29SAndres Lagar-Cavilla struct mm_struct *charge_mm; 162800501b53SJohannes Weiner struct mem_cgroup *memcg; 162927ab7006SHugh Dickins struct page *page; 16301da177e4SLinus Torvalds swp_entry_t swap; 1631657e3038SKirill A. Shutemov enum sgp_type sgp_huge = sgp; 1632800d8c63SKirill A. Shutemov pgoff_t hindex = index; 16331da177e4SLinus Torvalds int error; 163454af6042SHugh Dickins int once = 0; 16351635f6a7SHugh Dickins int alloced = 0; 16361da177e4SLinus Torvalds 163709cbfeafSKirill A. Shutemov if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 16381da177e4SLinus Torvalds return -EFBIG; 1639657e3038SKirill A. Shutemov if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) 1640657e3038SKirill A. Shutemov sgp = SGP_CACHE; 16411da177e4SLinus Torvalds repeat: 164254af6042SHugh Dickins swap.val = 0; 16430cd6144aSJohannes Weiner page = find_lock_entry(mapping, index); 164454af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 164554af6042SHugh Dickins swap = radix_to_swp_entry(page); 164654af6042SHugh Dickins page = NULL; 164754af6042SHugh Dickins } 164854af6042SHugh Dickins 164975edd345SHugh Dickins if (sgp <= SGP_CACHE && 165009cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 165154af6042SHugh Dickins error = -EINVAL; 1652267a4c76SHugh Dickins goto unlock; 165354af6042SHugh Dickins } 165454af6042SHugh Dickins 165566d2f4d2SHugh Dickins if (page && sgp == SGP_WRITE) 165666d2f4d2SHugh Dickins mark_page_accessed(page); 165766d2f4d2SHugh Dickins 16581635f6a7SHugh Dickins /* fallocated page? */ 16591635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 16601635f6a7SHugh Dickins if (sgp != SGP_READ) 16611635f6a7SHugh Dickins goto clear; 16621635f6a7SHugh Dickins unlock_page(page); 166309cbfeafSKirill A. Shutemov put_page(page); 16641635f6a7SHugh Dickins page = NULL; 16651635f6a7SHugh Dickins } 166654af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 166754af6042SHugh Dickins *pagep = page; 166854af6042SHugh Dickins return 0; 166927ab7006SHugh Dickins } 167027ab7006SHugh Dickins 1671b409f9fcSHugh Dickins /* 167254af6042SHugh Dickins * Fast cache lookup did not find it: 167354af6042SHugh Dickins * bring it back from swap or allocate. 1674b409f9fcSHugh Dickins */ 167554af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 1676cfda0526SMike Rapoport charge_mm = vma ? vma->vm_mm : current->mm; 167727ab7006SHugh Dickins 16781da177e4SLinus Torvalds if (swap.val) { 16791da177e4SLinus Torvalds /* Look it up and read it in.. */ 1680ec560175SHuang Ying page = lookup_swap_cache(swap, NULL, 0); 168127ab7006SHugh Dickins if (!page) { 16829e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 16839e18eb29SAndres Lagar-Cavilla if (fault_type) { 168468da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 16859e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 16862262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 16879e18eb29SAndres Lagar-Cavilla } 16889e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 168941ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 169027ab7006SHugh Dickins if (!page) { 16911da177e4SLinus Torvalds error = -ENOMEM; 169254af6042SHugh Dickins goto failed; 1693285b2c4fSHugh Dickins } 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 169754af6042SHugh Dickins lock_page(page); 16980142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1699d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1700bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1701d1899228SHugh Dickins goto unlock; 1702bde05d1cSHugh Dickins } 170327ab7006SHugh Dickins if (!PageUptodate(page)) { 17041da177e4SLinus Torvalds error = -EIO; 170554af6042SHugh Dickins goto failed; 170654af6042SHugh Dickins } 170754af6042SHugh Dickins wait_on_page_writeback(page); 170854af6042SHugh Dickins 1709bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1710bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1711bde05d1cSHugh Dickins if (error) 171254af6042SHugh Dickins goto failed; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17159e18eb29SAndres Lagar-Cavilla error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg, 1716f627c2f5SKirill A. Shutemov false); 1717d1899228SHugh Dickins if (!error) { 171854af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1719fed400a1SWang Sheng-Hui swp_to_radix_entry(swap)); 1720215c02bcSHugh Dickins /* 1721215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1722215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1723215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1724215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1725215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1726215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1727215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 172893aa7d95SVladimir Davydov * the rest. 1729215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1730215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1731215c02bcSHugh Dickins */ 173200501b53SJohannes Weiner if (error) { 1733f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1734215c02bcSHugh Dickins delete_from_swap_cache(page); 1735d1899228SHugh Dickins } 173600501b53SJohannes Weiner } 173754af6042SHugh Dickins if (error) 173854af6042SHugh Dickins goto failed; 173954af6042SHugh Dickins 1740f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 174100501b53SJohannes Weiner 17424595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 174354af6042SHugh Dickins info->swapped--; 174454af6042SHugh Dickins shmem_recalc_inode(inode); 17454595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 174627ab7006SHugh Dickins 174766d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 174866d2f4d2SHugh Dickins mark_page_accessed(page); 174966d2f4d2SHugh Dickins 175027ab7006SHugh Dickins delete_from_swap_cache(page); 175127ab7006SHugh Dickins set_page_dirty(page); 175227ab7006SHugh Dickins swap_free(swap); 175327ab7006SHugh Dickins 175454af6042SHugh Dickins } else { 1755cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1756cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1757cfda0526SMike Rapoport return 0; 1758cfda0526SMike Rapoport } 1759cfda0526SMike Rapoport 1760800d8c63SKirill A. Shutemov /* shmem_symlink() */ 1761800d8c63SKirill A. Shutemov if (mapping->a_ops != &shmem_aops) 1762800d8c63SKirill A. Shutemov goto alloc_nohuge; 1763657e3038SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) 1764800d8c63SKirill A. Shutemov goto alloc_nohuge; 1765800d8c63SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 1766800d8c63SKirill A. Shutemov goto alloc_huge; 1767800d8c63SKirill A. Shutemov switch (sbinfo->huge) { 1768800d8c63SKirill A. Shutemov loff_t i_size; 1769800d8c63SKirill A. Shutemov pgoff_t off; 1770800d8c63SKirill A. Shutemov case SHMEM_HUGE_NEVER: 1771800d8c63SKirill A. Shutemov goto alloc_nohuge; 1772800d8c63SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 1773800d8c63SKirill A. Shutemov off = round_up(index, HPAGE_PMD_NR); 1774800d8c63SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 1775800d8c63SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 1776800d8c63SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 1777800d8c63SKirill A. Shutemov goto alloc_huge; 1778800d8c63SKirill A. Shutemov /* fallthrough */ 1779800d8c63SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 1780657e3038SKirill A. Shutemov if (sgp_huge == SGP_HUGE) 1781657e3038SKirill A. Shutemov goto alloc_huge; 1782657e3038SKirill A. Shutemov /* TODO: implement fadvise() hints */ 1783800d8c63SKirill A. Shutemov goto alloc_nohuge; 178459a16eadSHugh Dickins } 17851da177e4SLinus Torvalds 1786800d8c63SKirill A. Shutemov alloc_huge: 17870f079694SMike Rapoport page = shmem_alloc_and_acct_page(gfp, inode, index, true); 1788800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 17890f079694SMike Rapoport alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, 1790800d8c63SKirill A. Shutemov index, false); 179154af6042SHugh Dickins } 1792800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1793779750d2SKirill A. Shutemov int retry = 5; 1794800d8c63SKirill A. Shutemov error = PTR_ERR(page); 1795800d8c63SKirill A. Shutemov page = NULL; 1796779750d2SKirill A. Shutemov if (error != -ENOSPC) 1797779750d2SKirill A. Shutemov goto failed; 1798779750d2SKirill A. Shutemov /* 1799779750d2SKirill A. Shutemov * Try to reclaim some spece by splitting a huge page 1800779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1801779750d2SKirill A. Shutemov */ 1802779750d2SKirill A. Shutemov while (retry--) { 1803779750d2SKirill A. Shutemov int ret; 1804779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1805779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1806779750d2SKirill A. Shutemov break; 1807779750d2SKirill A. Shutemov if (ret) 1808779750d2SKirill A. Shutemov goto alloc_nohuge; 1809779750d2SKirill A. Shutemov } 1810800d8c63SKirill A. Shutemov goto failed; 1811800d8c63SKirill A. Shutemov } 1812800d8c63SKirill A. Shutemov 1813800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 1814800d8c63SKirill A. Shutemov hindex = round_down(index, HPAGE_PMD_NR); 1815800d8c63SKirill A. Shutemov else 1816800d8c63SKirill A. Shutemov hindex = index; 1817800d8c63SKirill A. Shutemov 181866d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1819eb39d618SHugh Dickins __SetPageReferenced(page); 182066d2f4d2SHugh Dickins 18219e18eb29SAndres Lagar-Cavilla error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg, 1822800d8c63SKirill A. Shutemov PageTransHuge(page)); 182354af6042SHugh Dickins if (error) 1824800d8c63SKirill A. Shutemov goto unacct; 1825800d8c63SKirill A. Shutemov error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK, 1826800d8c63SKirill A. Shutemov compound_order(page)); 1827b065b432SHugh Dickins if (!error) { 1828800d8c63SKirill A. Shutemov error = shmem_add_to_page_cache(page, mapping, hindex, 1829fed400a1SWang Sheng-Hui NULL); 1830b065b432SHugh Dickins radix_tree_preload_end(); 1831b065b432SHugh Dickins } 1832b065b432SHugh Dickins if (error) { 1833800d8c63SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, 1834800d8c63SKirill A. Shutemov PageTransHuge(page)); 1835800d8c63SKirill A. Shutemov goto unacct; 1836b065b432SHugh Dickins } 1837800d8c63SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, 1838800d8c63SKirill A. Shutemov PageTransHuge(page)); 183954af6042SHugh Dickins lru_cache_add_anon(page); 184054af6042SHugh Dickins 18414595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1842800d8c63SKirill A. Shutemov info->alloced += 1 << compound_order(page); 1843800d8c63SKirill A. Shutemov inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); 184454af6042SHugh Dickins shmem_recalc_inode(inode); 18454595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18461635f6a7SHugh Dickins alloced = true; 184754af6042SHugh Dickins 1848779750d2SKirill A. Shutemov if (PageTransHuge(page) && 1849779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1850779750d2SKirill A. Shutemov hindex + HPAGE_PMD_NR - 1) { 1851779750d2SKirill A. Shutemov /* 1852779750d2SKirill A. Shutemov * Part of the huge page is beyond i_size: subject 1853779750d2SKirill A. Shutemov * to shrink under memory pressure. 1854779750d2SKirill A. Shutemov */ 1855779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1856d041353dSCong Wang /* 1857d041353dSCong Wang * _careful to defend against unlocked access to 1858d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1859d041353dSCong Wang */ 1860d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1861779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1862779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1863779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1864779750d2SKirill A. Shutemov } 1865779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1866779750d2SKirill A. Shutemov } 1867779750d2SKirill A. Shutemov 1868ec9516fbSHugh Dickins /* 18691635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 18701635f6a7SHugh Dickins */ 18711635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 18721635f6a7SHugh Dickins sgp = SGP_WRITE; 18731635f6a7SHugh Dickins clear: 18741635f6a7SHugh Dickins /* 18751635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 18761635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 18771635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1878ec9516fbSHugh Dickins */ 1879800d8c63SKirill A. Shutemov if (sgp != SGP_WRITE && !PageUptodate(page)) { 1880800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 1881800d8c63SKirill A. Shutemov int i; 1882800d8c63SKirill A. Shutemov 1883800d8c63SKirill A. Shutemov for (i = 0; i < (1 << compound_order(head)); i++) { 1884800d8c63SKirill A. Shutemov clear_highpage(head + i); 1885800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 1886800d8c63SKirill A. Shutemov } 1887800d8c63SKirill A. Shutemov SetPageUptodate(head); 1888ec9516fbSHugh Dickins } 18891da177e4SLinus Torvalds } 1890bde05d1cSHugh Dickins 189154af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 189275edd345SHugh Dickins if (sgp <= SGP_CACHE && 189309cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1894267a4c76SHugh Dickins if (alloced) { 1895267a4c76SHugh Dickins ClearPageDirty(page); 1896267a4c76SHugh Dickins delete_from_page_cache(page); 18974595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1898267a4c76SHugh Dickins shmem_recalc_inode(inode); 18994595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1900267a4c76SHugh Dickins } 190154af6042SHugh Dickins error = -EINVAL; 1902267a4c76SHugh Dickins goto unlock; 1903ff36b801SShaohua Li } 1904800d8c63SKirill A. Shutemov *pagep = page + index - hindex; 190554af6042SHugh Dickins return 0; 1906d00806b1SNick Piggin 1907d0217ac0SNick Piggin /* 190854af6042SHugh Dickins * Error recovery. 19091da177e4SLinus Torvalds */ 191054af6042SHugh Dickins unacct: 19110f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1 << compound_order(page)); 1912800d8c63SKirill A. Shutemov 1913800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 1914800d8c63SKirill A. Shutemov unlock_page(page); 1915800d8c63SKirill A. Shutemov put_page(page); 1916800d8c63SKirill A. Shutemov goto alloc_nohuge; 1917800d8c63SKirill A. Shutemov } 191854af6042SHugh Dickins failed: 1919267a4c76SHugh Dickins if (swap.val && !shmem_confirm_swap(mapping, index, swap)) 192054af6042SHugh Dickins error = -EEXIST; 1921d1899228SHugh Dickins unlock: 192227ab7006SHugh Dickins if (page) { 192354af6042SHugh Dickins unlock_page(page); 192409cbfeafSKirill A. Shutemov put_page(page); 192554af6042SHugh Dickins } 192654af6042SHugh Dickins if (error == -ENOSPC && !once++) { 19274595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 192854af6042SHugh Dickins shmem_recalc_inode(inode); 19294595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 19301da177e4SLinus Torvalds goto repeat; 1931d8dc74f2SAdrian Bunk } 1932d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 193354af6042SHugh Dickins goto repeat; 193454af6042SHugh Dickins return error; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 193710d20bd2SLinus Torvalds /* 193810d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 193910d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 194010d20bd2SLinus Torvalds * target. 194110d20bd2SLinus Torvalds */ 1942ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 194310d20bd2SLinus Torvalds { 194410d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 19452055da97SIngo Molnar list_del_init(&wait->entry); 194610d20bd2SLinus Torvalds return ret; 194710d20bd2SLinus Torvalds } 194810d20bd2SLinus Torvalds 194920acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 19501da177e4SLinus Torvalds { 195111bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1952496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 19539e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 1954657e3038SKirill A. Shutemov enum sgp_type sgp; 195520acce67SSouptick Joarder int err; 195620acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 19571da177e4SLinus Torvalds 1958f00cdc6dSHugh Dickins /* 1959f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1960f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1961f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 19628e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 19638e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 19648e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 19658e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 19668e205f77SHugh Dickins * 19678e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 19688e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 19698e205f77SHugh Dickins * we just need to make racing faults a rare case. 19708e205f77SHugh Dickins * 19718e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 19728e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 19738e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 1974f00cdc6dSHugh Dickins */ 1975f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 1976f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 1977f00cdc6dSHugh Dickins 1978f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1979f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 19808e205f77SHugh Dickins if (shmem_falloc && 19818e205f77SHugh Dickins shmem_falloc->waitq && 19828e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 19838e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 19848e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 198510d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 19868e205f77SHugh Dickins 19878e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 1988f00cdc6dSHugh Dickins if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && 1989f00cdc6dSHugh Dickins !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 19908e205f77SHugh Dickins /* It's polite to up mmap_sem if we can */ 1991f00cdc6dSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 19928e205f77SHugh Dickins ret = VM_FAULT_RETRY; 1993f00cdc6dSHugh Dickins } 19948e205f77SHugh Dickins 19958e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 19968e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 19978e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 19988e205f77SHugh Dickins spin_unlock(&inode->i_lock); 19998e205f77SHugh Dickins schedule(); 20008e205f77SHugh Dickins 20018e205f77SHugh Dickins /* 20028e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 20038e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 20048e205f77SHugh Dickins * is usually invalid by the time we reach here, but 20058e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 20068e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 20078e205f77SHugh Dickins */ 20088e205f77SHugh Dickins spin_lock(&inode->i_lock); 20098e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 20108e205f77SHugh Dickins spin_unlock(&inode->i_lock); 20118e205f77SHugh Dickins return ret; 2012f00cdc6dSHugh Dickins } 20138e205f77SHugh Dickins spin_unlock(&inode->i_lock); 2014f00cdc6dSHugh Dickins } 2015f00cdc6dSHugh Dickins 2016657e3038SKirill A. Shutemov sgp = SGP_CACHE; 201718600332SMichal Hocko 201818600332SMichal Hocko if ((vma->vm_flags & VM_NOHUGEPAGE) || 201918600332SMichal Hocko test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) 2020657e3038SKirill A. Shutemov sgp = SGP_NOHUGE; 202118600332SMichal Hocko else if (vma->vm_flags & VM_HUGEPAGE) 202218600332SMichal Hocko sgp = SGP_HUGE; 2023657e3038SKirill A. Shutemov 202420acce67SSouptick Joarder err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, 2025cfda0526SMike Rapoport gfp, vma, vmf, &ret); 202620acce67SSouptick Joarder if (err) 202720acce67SSouptick Joarder return vmf_error(err); 202868da9f05SHugh Dickins return ret; 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds 2031c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 2032c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 2033c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 2034c01d5b30SHugh Dickins { 2035c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2036c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2037c01d5b30SHugh Dickins unsigned long addr; 2038c01d5b30SHugh Dickins unsigned long offset; 2039c01d5b30SHugh Dickins unsigned long inflated_len; 2040c01d5b30SHugh Dickins unsigned long inflated_addr; 2041c01d5b30SHugh Dickins unsigned long inflated_offset; 2042c01d5b30SHugh Dickins 2043c01d5b30SHugh Dickins if (len > TASK_SIZE) 2044c01d5b30SHugh Dickins return -ENOMEM; 2045c01d5b30SHugh Dickins 2046c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2047c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2048c01d5b30SHugh Dickins 2049e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 2050c01d5b30SHugh Dickins return addr; 2051c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2052c01d5b30SHugh Dickins return addr; 2053c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2054c01d5b30SHugh Dickins return addr; 2055c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2056c01d5b30SHugh Dickins return addr; 2057c01d5b30SHugh Dickins 2058c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2059c01d5b30SHugh Dickins return addr; 2060c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2061c01d5b30SHugh Dickins return addr; 2062c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2063c01d5b30SHugh Dickins return addr; 2064c01d5b30SHugh Dickins /* 2065c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2066c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 2067c01d5b30SHugh Dickins * But if caller specified an address hint, respect that as before. 2068c01d5b30SHugh Dickins */ 2069c01d5b30SHugh Dickins if (uaddr) 2070c01d5b30SHugh Dickins return addr; 2071c01d5b30SHugh Dickins 2072c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2073c01d5b30SHugh Dickins struct super_block *sb; 2074c01d5b30SHugh Dickins 2075c01d5b30SHugh Dickins if (file) { 2076c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2077c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2078c01d5b30SHugh Dickins } else { 2079c01d5b30SHugh Dickins /* 2080c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2081c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2082c01d5b30SHugh Dickins */ 2083c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2084c01d5b30SHugh Dickins return addr; 2085c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2086c01d5b30SHugh Dickins } 20873089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2088c01d5b30SHugh Dickins return addr; 2089c01d5b30SHugh Dickins } 2090c01d5b30SHugh Dickins 2091c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2092c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2093c01d5b30SHugh Dickins return addr; 2094c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2095c01d5b30SHugh Dickins return addr; 2096c01d5b30SHugh Dickins 2097c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2098c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2099c01d5b30SHugh Dickins return addr; 2100c01d5b30SHugh Dickins if (inflated_len < len) 2101c01d5b30SHugh Dickins return addr; 2102c01d5b30SHugh Dickins 2103c01d5b30SHugh Dickins inflated_addr = get_area(NULL, 0, inflated_len, 0, flags); 2104c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2105c01d5b30SHugh Dickins return addr; 2106c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2107c01d5b30SHugh Dickins return addr; 2108c01d5b30SHugh Dickins 2109c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2110c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2111c01d5b30SHugh Dickins if (inflated_offset > offset) 2112c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2113c01d5b30SHugh Dickins 2114c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2115c01d5b30SHugh Dickins return addr; 2116c01d5b30SHugh Dickins return inflated_addr; 2117c01d5b30SHugh Dickins } 2118c01d5b30SHugh Dickins 21191da177e4SLinus Torvalds #ifdef CONFIG_NUMA 212041ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 21211da177e4SLinus Torvalds { 2122496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 212341ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 2126d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2127d8dc74f2SAdrian Bunk unsigned long addr) 21281da177e4SLinus Torvalds { 2129496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 213041ffe5d5SHugh Dickins pgoff_t index; 21311da177e4SLinus Torvalds 213241ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 213341ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 21341da177e4SLinus Torvalds } 21351da177e4SLinus Torvalds #endif 21361da177e4SLinus Torvalds 21371da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 21381da177e4SLinus Torvalds { 2139496ad9aaSAl Viro struct inode *inode = file_inode(file); 21401da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 21411da177e4SLinus Torvalds int retval = -ENOMEM; 21421da177e4SLinus Torvalds 21434595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 21441da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 21451da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 21461da177e4SLinus Torvalds goto out_nomem; 21471da177e4SLinus Torvalds info->flags |= VM_LOCKED; 214889e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 21511da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 21521da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 215389e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds retval = 0; 215689e004eaSLee Schermerhorn 21571da177e4SLinus Torvalds out_nomem: 21584595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 21591da177e4SLinus Torvalds return retval; 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds 21629b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 21631da177e4SLinus Torvalds { 21641da177e4SLinus Torvalds file_accessed(file); 21651da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2166e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 2167f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 2168f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 2169f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 2170f3f0e1d2SKirill A. Shutemov } 21711da177e4SLinus Torvalds return 0; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 2174454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 217509208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 21761da177e4SLinus Torvalds { 21771da177e4SLinus Torvalds struct inode *inode; 21781da177e4SLinus Torvalds struct shmem_inode_info *info; 21791da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 21801da177e4SLinus Torvalds 21815b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 21821da177e4SLinus Torvalds return NULL; 21831da177e4SLinus Torvalds 21841da177e4SLinus Torvalds inode = new_inode(sb); 21851da177e4SLinus Torvalds if (inode) { 218685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2187454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 21881da177e4SLinus Torvalds inode->i_blocks = 0; 2189078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 219091828a40SDavid M. Grimes inode->i_generation = get_seconds(); 21911da177e4SLinus Torvalds info = SHMEM_I(inode); 21921da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 21931da177e4SLinus Torvalds spin_lock_init(&info->lock); 219440e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 21950b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2196779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 21971da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 219838f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 219972c04902SAl Viro cache_no_acl(inode); 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds switch (mode & S_IFMT) { 22021da177e4SLinus Torvalds default: 220339f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 22041da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 22051da177e4SLinus Torvalds break; 22061da177e4SLinus Torvalds case S_IFREG: 220714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 22081da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 22091da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 221071fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 221171fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 22121da177e4SLinus Torvalds break; 22131da177e4SLinus Torvalds case S_IFDIR: 2214d8c76e6fSDave Hansen inc_nlink(inode); 22151da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 22161da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 22171da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 22181da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 22191da177e4SLinus Torvalds break; 22201da177e4SLinus Torvalds case S_IFLNK: 22211da177e4SLinus Torvalds /* 22221da177e4SLinus Torvalds * Must not load anything in the rbtree, 22231da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 22241da177e4SLinus Torvalds */ 222571fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 22261da177e4SLinus Torvalds break; 22271da177e4SLinus Torvalds } 22285b04c689SPavel Emelyanov } else 22295b04c689SPavel Emelyanov shmem_free_inode(sb); 22301da177e4SLinus Torvalds return inode; 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds 22330cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 22340cd6144aSJohannes Weiner { 2235f8005451SHugh Dickins return mapping->a_ops == &shmem_aops; 22360cd6144aSJohannes Weiner } 22370cd6144aSJohannes Weiner 22388d103963SMike Rapoport static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, 22394c27fe4cSMike Rapoport pmd_t *dst_pmd, 22404c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 22414c27fe4cSMike Rapoport unsigned long dst_addr, 22424c27fe4cSMike Rapoport unsigned long src_addr, 22438d103963SMike Rapoport bool zeropage, 22444c27fe4cSMike Rapoport struct page **pagep) 22454c27fe4cSMike Rapoport { 22464c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 22474c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 22484c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 22494c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 22504c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 22514c27fe4cSMike Rapoport struct mem_cgroup *memcg; 22524c27fe4cSMike Rapoport spinlock_t *ptl; 22534c27fe4cSMike Rapoport void *page_kaddr; 22544c27fe4cSMike Rapoport struct page *page; 22554c27fe4cSMike Rapoport pte_t _dst_pte, *dst_pte; 22564c27fe4cSMike Rapoport int ret; 22574c27fe4cSMike Rapoport 22584c27fe4cSMike Rapoport ret = -ENOMEM; 22590f079694SMike Rapoport if (!shmem_inode_acct_block(inode, 1)) 22604c27fe4cSMike Rapoport goto out; 22614c27fe4cSMike Rapoport 2262cb658a45SAndrea Arcangeli if (!*pagep) { 22634c27fe4cSMike Rapoport page = shmem_alloc_page(gfp, info, pgoff); 22644c27fe4cSMike Rapoport if (!page) 22650f079694SMike Rapoport goto out_unacct_blocks; 22664c27fe4cSMike Rapoport 22678d103963SMike Rapoport if (!zeropage) { /* mcopy_atomic */ 22684c27fe4cSMike Rapoport page_kaddr = kmap_atomic(page); 22698d103963SMike Rapoport ret = copy_from_user(page_kaddr, 22708d103963SMike Rapoport (const void __user *)src_addr, 22714c27fe4cSMike Rapoport PAGE_SIZE); 22724c27fe4cSMike Rapoport kunmap_atomic(page_kaddr); 22734c27fe4cSMike Rapoport 22744c27fe4cSMike Rapoport /* fallback to copy_from_user outside mmap_sem */ 22754c27fe4cSMike Rapoport if (unlikely(ret)) { 22764c27fe4cSMike Rapoport *pagep = page; 22770f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 22784c27fe4cSMike Rapoport /* don't free the page */ 22794c27fe4cSMike Rapoport return -EFAULT; 22804c27fe4cSMike Rapoport } 22818d103963SMike Rapoport } else { /* mfill_zeropage_atomic */ 22828d103963SMike Rapoport clear_highpage(page); 22838d103963SMike Rapoport } 22844c27fe4cSMike Rapoport } else { 22854c27fe4cSMike Rapoport page = *pagep; 22864c27fe4cSMike Rapoport *pagep = NULL; 22874c27fe4cSMike Rapoport } 22884c27fe4cSMike Rapoport 22899cc90c66SAndrea Arcangeli VM_BUG_ON(PageLocked(page) || PageSwapBacked(page)); 22909cc90c66SAndrea Arcangeli __SetPageLocked(page); 22919cc90c66SAndrea Arcangeli __SetPageSwapBacked(page); 2292a425d358SAndrea Arcangeli __SetPageUptodate(page); 22939cc90c66SAndrea Arcangeli 22944c27fe4cSMike Rapoport ret = mem_cgroup_try_charge(page, dst_mm, gfp, &memcg, false); 22954c27fe4cSMike Rapoport if (ret) 22964c27fe4cSMike Rapoport goto out_release; 22974c27fe4cSMike Rapoport 22984c27fe4cSMike Rapoport ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK); 22994c27fe4cSMike Rapoport if (!ret) { 23004c27fe4cSMike Rapoport ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL); 23014c27fe4cSMike Rapoport radix_tree_preload_end(); 23024c27fe4cSMike Rapoport } 23034c27fe4cSMike Rapoport if (ret) 23044c27fe4cSMike Rapoport goto out_release_uncharge; 23054c27fe4cSMike Rapoport 23064c27fe4cSMike Rapoport mem_cgroup_commit_charge(page, memcg, false, false); 23074c27fe4cSMike Rapoport 23084c27fe4cSMike Rapoport _dst_pte = mk_pte(page, dst_vma->vm_page_prot); 23094c27fe4cSMike Rapoport if (dst_vma->vm_flags & VM_WRITE) 23104c27fe4cSMike Rapoport _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); 23114c27fe4cSMike Rapoport 23124c27fe4cSMike Rapoport ret = -EEXIST; 23134c27fe4cSMike Rapoport dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 23144c27fe4cSMike Rapoport if (!pte_none(*dst_pte)) 23154c27fe4cSMike Rapoport goto out_release_uncharge_unlock; 23164c27fe4cSMike Rapoport 23174c27fe4cSMike Rapoport lru_cache_add_anon(page); 23184c27fe4cSMike Rapoport 23194c27fe4cSMike Rapoport spin_lock(&info->lock); 23204c27fe4cSMike Rapoport info->alloced++; 23214c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 23224c27fe4cSMike Rapoport shmem_recalc_inode(inode); 23234c27fe4cSMike Rapoport spin_unlock(&info->lock); 23244c27fe4cSMike Rapoport 23254c27fe4cSMike Rapoport inc_mm_counter(dst_mm, mm_counter_file(page)); 23264c27fe4cSMike Rapoport page_add_file_rmap(page, false); 23274c27fe4cSMike Rapoport set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 23284c27fe4cSMike Rapoport 23294c27fe4cSMike Rapoport /* No need to invalidate - it was non-present before */ 23304c27fe4cSMike Rapoport update_mmu_cache(dst_vma, dst_addr, dst_pte); 23314c27fe4cSMike Rapoport unlock_page(page); 23324c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 23334c27fe4cSMike Rapoport ret = 0; 23344c27fe4cSMike Rapoport out: 23354c27fe4cSMike Rapoport return ret; 23364c27fe4cSMike Rapoport out_release_uncharge_unlock: 23374c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 23384c27fe4cSMike Rapoport out_release_uncharge: 23394c27fe4cSMike Rapoport mem_cgroup_cancel_charge(page, memcg, false); 23404c27fe4cSMike Rapoport out_release: 23419cc90c66SAndrea Arcangeli unlock_page(page); 23424c27fe4cSMike Rapoport put_page(page); 23434c27fe4cSMike Rapoport out_unacct_blocks: 23440f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 23454c27fe4cSMike Rapoport goto out; 23464c27fe4cSMike Rapoport } 23474c27fe4cSMike Rapoport 23488d103963SMike Rapoport int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, 23498d103963SMike Rapoport pmd_t *dst_pmd, 23508d103963SMike Rapoport struct vm_area_struct *dst_vma, 23518d103963SMike Rapoport unsigned long dst_addr, 23528d103963SMike Rapoport unsigned long src_addr, 23538d103963SMike Rapoport struct page **pagep) 23548d103963SMike Rapoport { 23558d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 23568d103963SMike Rapoport dst_addr, src_addr, false, pagep); 23578d103963SMike Rapoport } 23588d103963SMike Rapoport 23598d103963SMike Rapoport int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, 23608d103963SMike Rapoport pmd_t *dst_pmd, 23618d103963SMike Rapoport struct vm_area_struct *dst_vma, 23628d103963SMike Rapoport unsigned long dst_addr) 23638d103963SMike Rapoport { 23648d103963SMike Rapoport struct page *page = NULL; 23658d103963SMike Rapoport 23668d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 23678d103963SMike Rapoport dst_addr, 0, true, &page); 23688d103963SMike Rapoport } 23698d103963SMike Rapoport 23701da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 237192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 237269f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 23731da177e4SLinus Torvalds 23746d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 23756d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 23766d9d88d0SJarkko Sakkinen #else 23776d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 23786d9d88d0SJarkko Sakkinen #endif 23796d9d88d0SJarkko Sakkinen 23801da177e4SLinus Torvalds static int 2381800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 2382800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 2383800d15a5SNick Piggin struct page **pagep, void **fsdata) 23841da177e4SLinus Torvalds { 2385800d15a5SNick Piggin struct inode *inode = mapping->host; 238640e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 238709cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 238840e041a2SDavid Herrmann 238940e041a2SDavid Herrmann /* i_mutex is held by caller */ 23903f472cc9SSteven Rostedt (VMware) if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) { 239140e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) 239240e041a2SDavid Herrmann return -EPERM; 239340e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 239440e041a2SDavid Herrmann return -EPERM; 239540e041a2SDavid Herrmann } 239640e041a2SDavid Herrmann 23979e18eb29SAndres Lagar-Cavilla return shmem_getpage(inode, index, pagep, SGP_WRITE); 2398800d15a5SNick Piggin } 2399800d15a5SNick Piggin 2400800d15a5SNick Piggin static int 2401800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2402800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2403800d15a5SNick Piggin struct page *page, void *fsdata) 2404800d15a5SNick Piggin { 2405800d15a5SNick Piggin struct inode *inode = mapping->host; 2406800d15a5SNick Piggin 2407800d15a5SNick Piggin if (pos + copied > inode->i_size) 2408800d15a5SNick Piggin i_size_write(inode, pos + copied); 2409800d15a5SNick Piggin 2410ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2411800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2412800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2413800d8c63SKirill A. Shutemov int i; 2414800d8c63SKirill A. Shutemov 2415800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2416800d8c63SKirill A. Shutemov if (head + i == page) 2417800d8c63SKirill A. Shutemov continue; 2418800d8c63SKirill A. Shutemov clear_highpage(head + i); 2419800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2420800d8c63SKirill A. Shutemov } 2421800d8c63SKirill A. Shutemov } 242209cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 242309cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2424ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 242509cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2426ec9516fbSHugh Dickins } 2427800d8c63SKirill A. Shutemov SetPageUptodate(head); 2428ec9516fbSHugh Dickins } 2429d3602444SHugh Dickins set_page_dirty(page); 24306746aff7SWu Fengguang unlock_page(page); 243109cbfeafSKirill A. Shutemov put_page(page); 2432d3602444SHugh Dickins 2433800d15a5SNick Piggin return copied; 24341da177e4SLinus Torvalds } 24351da177e4SLinus Torvalds 24362ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 24371da177e4SLinus Torvalds { 24386e58e79dSAl Viro struct file *file = iocb->ki_filp; 24396e58e79dSAl Viro struct inode *inode = file_inode(file); 24401da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 244141ffe5d5SHugh Dickins pgoff_t index; 244241ffe5d5SHugh Dickins unsigned long offset; 2443a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 2444f7c1d074SGeert Uytterhoeven int error = 0; 2445cb66a7a1SAl Viro ssize_t retval = 0; 24466e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2447a0ee5ec5SHugh Dickins 2448a0ee5ec5SHugh Dickins /* 2449a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 2450a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 2451a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 2452a0ee5ec5SHugh Dickins */ 2453777eda2cSAl Viro if (!iter_is_iovec(to)) 245475edd345SHugh Dickins sgp = SGP_CACHE; 24551da177e4SLinus Torvalds 245609cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 245709cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds for (;;) { 24601da177e4SLinus Torvalds struct page *page = NULL; 246141ffe5d5SHugh Dickins pgoff_t end_index; 246241ffe5d5SHugh Dickins unsigned long nr, ret; 24631da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 24641da177e4SLinus Torvalds 246509cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24661da177e4SLinus Torvalds if (index > end_index) 24671da177e4SLinus Torvalds break; 24681da177e4SLinus Torvalds if (index == end_index) { 246909cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24701da177e4SLinus Torvalds if (nr <= offset) 24711da177e4SLinus Torvalds break; 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds 24749e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, sgp); 24756e58e79dSAl Viro if (error) { 24766e58e79dSAl Viro if (error == -EINVAL) 24776e58e79dSAl Viro error = 0; 24781da177e4SLinus Torvalds break; 24791da177e4SLinus Torvalds } 248075edd345SHugh Dickins if (page) { 248175edd345SHugh Dickins if (sgp == SGP_CACHE) 248275edd345SHugh Dickins set_page_dirty(page); 2483d3602444SHugh Dickins unlock_page(page); 248475edd345SHugh Dickins } 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds /* 24871da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 24881b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 24891da177e4SLinus Torvalds */ 249009cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 24911da177e4SLinus Torvalds i_size = i_size_read(inode); 249209cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24931da177e4SLinus Torvalds if (index == end_index) { 249409cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24951da177e4SLinus Torvalds if (nr <= offset) { 24961da177e4SLinus Torvalds if (page) 249709cbfeafSKirill A. Shutemov put_page(page); 24981da177e4SLinus Torvalds break; 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds nr -= offset; 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds if (page) { 25041da177e4SLinus Torvalds /* 25051da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 25061da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 25071da177e4SLinus Torvalds * before reading the page on the kernel side. 25081da177e4SLinus Torvalds */ 25091da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 25101da177e4SLinus Torvalds flush_dcache_page(page); 25111da177e4SLinus Torvalds /* 25121da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 25131da177e4SLinus Torvalds */ 25141da177e4SLinus Torvalds if (!offset) 25151da177e4SLinus Torvalds mark_page_accessed(page); 2516b5810039SNick Piggin } else { 25171da177e4SLinus Torvalds page = ZERO_PAGE(0); 251809cbfeafSKirill A. Shutemov get_page(page); 2519b5810039SNick Piggin } 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds /* 25221da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 25231da177e4SLinus Torvalds * now we can copy it to user space... 25241da177e4SLinus Torvalds */ 25252ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 25266e58e79dSAl Viro retval += ret; 25271da177e4SLinus Torvalds offset += ret; 252809cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 252909cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 25301da177e4SLinus Torvalds 253109cbfeafSKirill A. Shutemov put_page(page); 25322ba5bbedSAl Viro if (!iov_iter_count(to)) 25331da177e4SLinus Torvalds break; 25346e58e79dSAl Viro if (ret < nr) { 25356e58e79dSAl Viro error = -EFAULT; 25366e58e79dSAl Viro break; 25376e58e79dSAl Viro } 25381da177e4SLinus Torvalds cond_resched(); 25391da177e4SLinus Torvalds } 25401da177e4SLinus Torvalds 254109cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 25426e58e79dSAl Viro file_accessed(file); 25436e58e79dSAl Viro return retval ? retval : error; 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds 2546220f2ac9SHugh Dickins /* 2547220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 2548220f2ac9SHugh Dickins */ 2549220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 2550965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 2551220f2ac9SHugh Dickins { 2552220f2ac9SHugh Dickins struct page *page; 2553220f2ac9SHugh Dickins struct pagevec pvec; 2554220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 2555220f2ac9SHugh Dickins bool done = false; 2556220f2ac9SHugh Dickins int i; 2557220f2ac9SHugh Dickins 255886679820SMel Gorman pagevec_init(&pvec); 2559220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 2560220f2ac9SHugh Dickins while (!done) { 25610cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 2562220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 2563220f2ac9SHugh Dickins if (!pvec.nr) { 2564965c8e59SAndrew Morton if (whence == SEEK_DATA) 2565220f2ac9SHugh Dickins index = end; 2566220f2ac9SHugh Dickins break; 2567220f2ac9SHugh Dickins } 2568220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 2569220f2ac9SHugh Dickins if (index < indices[i]) { 2570965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 2571220f2ac9SHugh Dickins done = true; 2572220f2ac9SHugh Dickins break; 2573220f2ac9SHugh Dickins } 2574220f2ac9SHugh Dickins index = indices[i]; 2575220f2ac9SHugh Dickins } 2576220f2ac9SHugh Dickins page = pvec.pages[i]; 2577220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 2578220f2ac9SHugh Dickins if (!PageUptodate(page)) 2579220f2ac9SHugh Dickins page = NULL; 2580220f2ac9SHugh Dickins } 2581220f2ac9SHugh Dickins if (index >= end || 2582965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 2583965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 2584220f2ac9SHugh Dickins done = true; 2585220f2ac9SHugh Dickins break; 2586220f2ac9SHugh Dickins } 2587220f2ac9SHugh Dickins } 25880cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 2589220f2ac9SHugh Dickins pagevec_release(&pvec); 2590220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 2591220f2ac9SHugh Dickins cond_resched(); 2592220f2ac9SHugh Dickins } 2593220f2ac9SHugh Dickins return index; 2594220f2ac9SHugh Dickins } 2595220f2ac9SHugh Dickins 2596965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2597220f2ac9SHugh Dickins { 2598220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2599220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2600220f2ac9SHugh Dickins pgoff_t start, end; 2601220f2ac9SHugh Dickins loff_t new_offset; 2602220f2ac9SHugh Dickins 2603965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2604965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2605220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 26065955102cSAl Viro inode_lock(inode); 2607220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 2608220f2ac9SHugh Dickins 2609220f2ac9SHugh Dickins if (offset < 0) 2610220f2ac9SHugh Dickins offset = -EINVAL; 2611220f2ac9SHugh Dickins else if (offset >= inode->i_size) 2612220f2ac9SHugh Dickins offset = -ENXIO; 2613220f2ac9SHugh Dickins else { 261409cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 261509cbfeafSKirill A. Shutemov end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 2616965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 261709cbfeafSKirill A. Shutemov new_offset <<= PAGE_SHIFT; 2618220f2ac9SHugh Dickins if (new_offset > offset) { 2619220f2ac9SHugh Dickins if (new_offset < inode->i_size) 2620220f2ac9SHugh Dickins offset = new_offset; 2621965c8e59SAndrew Morton else if (whence == SEEK_DATA) 2622220f2ac9SHugh Dickins offset = -ENXIO; 2623220f2ac9SHugh Dickins else 2624220f2ac9SHugh Dickins offset = inode->i_size; 2625220f2ac9SHugh Dickins } 2626220f2ac9SHugh Dickins } 2627220f2ac9SHugh Dickins 2628387aae6fSHugh Dickins if (offset >= 0) 262946a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 26305955102cSAl Viro inode_unlock(inode); 2631220f2ac9SHugh Dickins return offset; 2632220f2ac9SHugh Dickins } 2633220f2ac9SHugh Dickins 263483e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 263583e4fa9cSHugh Dickins loff_t len) 263683e4fa9cSHugh Dickins { 2637496ad9aaSAl Viro struct inode *inode = file_inode(file); 2638e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 263940e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 26401aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2641e2d12e22SHugh Dickins pgoff_t start, index, end; 2642e2d12e22SHugh Dickins int error; 264383e4fa9cSHugh Dickins 264413ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 264513ace4d0SHugh Dickins return -EOPNOTSUPP; 264613ace4d0SHugh Dickins 26475955102cSAl Viro inode_lock(inode); 264883e4fa9cSHugh Dickins 264983e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 265083e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 265183e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 265283e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 26538e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 265483e4fa9cSHugh Dickins 265540e041a2SDavid Herrmann /* protected by i_mutex */ 265640e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) { 265740e041a2SDavid Herrmann error = -EPERM; 265840e041a2SDavid Herrmann goto out; 265940e041a2SDavid Herrmann } 266040e041a2SDavid Herrmann 26618e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2662f00cdc6dSHugh Dickins shmem_falloc.start = unmap_start >> PAGE_SHIFT; 2663f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2664f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2665f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2666f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2667f00cdc6dSHugh Dickins 266883e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 266983e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 267083e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 267183e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 267283e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 26738e205f77SHugh Dickins 26748e205f77SHugh Dickins spin_lock(&inode->i_lock); 26758e205f77SHugh Dickins inode->i_private = NULL; 26768e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 26772055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 26788e205f77SHugh Dickins spin_unlock(&inode->i_lock); 267983e4fa9cSHugh Dickins error = 0; 26808e205f77SHugh Dickins goto out; 268183e4fa9cSHugh Dickins } 268283e4fa9cSHugh Dickins 2683e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2684e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2685e2d12e22SHugh Dickins if (error) 2686e2d12e22SHugh Dickins goto out; 2687e2d12e22SHugh Dickins 268840e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 268940e041a2SDavid Herrmann error = -EPERM; 269040e041a2SDavid Herrmann goto out; 269140e041a2SDavid Herrmann } 269240e041a2SDavid Herrmann 269309cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 269409cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2695e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2696e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2697e2d12e22SHugh Dickins error = -ENOSPC; 2698e2d12e22SHugh Dickins goto out; 2699e2d12e22SHugh Dickins } 2700e2d12e22SHugh Dickins 27018e205f77SHugh Dickins shmem_falloc.waitq = NULL; 27021aac1400SHugh Dickins shmem_falloc.start = start; 27031aac1400SHugh Dickins shmem_falloc.next = start; 27041aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 27051aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 27061aac1400SHugh Dickins spin_lock(&inode->i_lock); 27071aac1400SHugh Dickins inode->i_private = &shmem_falloc; 27081aac1400SHugh Dickins spin_unlock(&inode->i_lock); 27091aac1400SHugh Dickins 2710e2d12e22SHugh Dickins for (index = start; index < end; index++) { 2711e2d12e22SHugh Dickins struct page *page; 2712e2d12e22SHugh Dickins 2713e2d12e22SHugh Dickins /* 2714e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2715e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2716e2d12e22SHugh Dickins */ 2717e2d12e22SHugh Dickins if (signal_pending(current)) 2718e2d12e22SHugh Dickins error = -EINTR; 27191aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 27201aac1400SHugh Dickins error = -ENOMEM; 2721e2d12e22SHugh Dickins else 27229e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_FALLOC); 2723e2d12e22SHugh Dickins if (error) { 27241635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 27257f556567SHugh Dickins if (index > start) { 27261635f6a7SHugh Dickins shmem_undo_range(inode, 272709cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2728b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 27297f556567SHugh Dickins } 27301aac1400SHugh Dickins goto undone; 2731e2d12e22SHugh Dickins } 2732e2d12e22SHugh Dickins 2733e2d12e22SHugh Dickins /* 27341aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 27351aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 27361aac1400SHugh Dickins */ 27371aac1400SHugh Dickins shmem_falloc.next++; 27381aac1400SHugh Dickins if (!PageUptodate(page)) 27391aac1400SHugh Dickins shmem_falloc.nr_falloced++; 27401aac1400SHugh Dickins 27411aac1400SHugh Dickins /* 27421635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 27431635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 27441635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2745e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2746e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2747e2d12e22SHugh Dickins */ 2748e2d12e22SHugh Dickins set_page_dirty(page); 2749e2d12e22SHugh Dickins unlock_page(page); 275009cbfeafSKirill A. Shutemov put_page(page); 2751e2d12e22SHugh Dickins cond_resched(); 2752e2d12e22SHugh Dickins } 2753e2d12e22SHugh Dickins 2754e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2755e2d12e22SHugh Dickins i_size_write(inode, offset + len); 2756078cd827SDeepa Dinamani inode->i_ctime = current_time(inode); 27571aac1400SHugh Dickins undone: 27581aac1400SHugh Dickins spin_lock(&inode->i_lock); 27591aac1400SHugh Dickins inode->i_private = NULL; 27601aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2761e2d12e22SHugh Dickins out: 27625955102cSAl Viro inode_unlock(inode); 276383e4fa9cSHugh Dickins return error; 276483e4fa9cSHugh Dickins } 276583e4fa9cSHugh Dickins 2766726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 27671da177e4SLinus Torvalds { 2768726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 27691da177e4SLinus Torvalds 27701da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 277109cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 27721da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 27730edd73b3SHugh Dickins if (sbinfo->max_blocks) { 27741da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 277541ffe5d5SHugh Dickins buf->f_bavail = 277641ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 277741ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 27780edd73b3SHugh Dickins } 27790edd73b3SHugh Dickins if (sbinfo->max_inodes) { 27801da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 27811da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 27841da177e4SLinus Torvalds return 0; 27851da177e4SLinus Torvalds } 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds /* 27881da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 27891da177e4SLinus Torvalds */ 27901da177e4SLinus Torvalds static int 27911a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 27921da177e4SLinus Torvalds { 27930b0a0806SHugh Dickins struct inode *inode; 27941da177e4SLinus Torvalds int error = -ENOSPC; 27951da177e4SLinus Torvalds 2796454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 27971da177e4SLinus Torvalds if (inode) { 2798feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2799feda821eSChristoph Hellwig if (error) 2800feda821eSChristoph Hellwig goto out_iput; 28012a7dba39SEric Paris error = security_inode_init_security(inode, dir, 28029d8f13baSMimi Zohar &dentry->d_name, 28036d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2804feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2805feda821eSChristoph Hellwig goto out_iput; 280637ec43cdSMimi Zohar 2807718deb6bSAl Viro error = 0; 28081da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2809078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 28101da177e4SLinus Torvalds d_instantiate(dentry, inode); 28111da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds return error; 2814feda821eSChristoph Hellwig out_iput: 2815feda821eSChristoph Hellwig iput(inode); 2816feda821eSChristoph Hellwig return error; 28171da177e4SLinus Torvalds } 28181da177e4SLinus Torvalds 281960545d0dSAl Viro static int 282060545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 282160545d0dSAl Viro { 282260545d0dSAl Viro struct inode *inode; 282360545d0dSAl Viro int error = -ENOSPC; 282460545d0dSAl Viro 282560545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 282660545d0dSAl Viro if (inode) { 282760545d0dSAl Viro error = security_inode_init_security(inode, dir, 282860545d0dSAl Viro NULL, 282960545d0dSAl Viro shmem_initxattrs, NULL); 2830feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2831feda821eSChristoph Hellwig goto out_iput; 2832feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2833feda821eSChristoph Hellwig if (error) 2834feda821eSChristoph Hellwig goto out_iput; 283560545d0dSAl Viro d_tmpfile(dentry, inode); 283660545d0dSAl Viro } 283760545d0dSAl Viro return error; 2838feda821eSChristoph Hellwig out_iput: 2839feda821eSChristoph Hellwig iput(inode); 2840feda821eSChristoph Hellwig return error; 284160545d0dSAl Viro } 284260545d0dSAl Viro 284318bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 28441da177e4SLinus Torvalds { 28451da177e4SLinus Torvalds int error; 28461da177e4SLinus Torvalds 28471da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 28481da177e4SLinus Torvalds return error; 2849d8c76e6fSDave Hansen inc_nlink(dir); 28501da177e4SLinus Torvalds return 0; 28511da177e4SLinus Torvalds } 28521da177e4SLinus Torvalds 28534acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2854ebfc3b49SAl Viro bool excl) 28551da177e4SLinus Torvalds { 28561da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds /* 28601da177e4SLinus Torvalds * Link a file.. 28611da177e4SLinus Torvalds */ 28621da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 28631da177e4SLinus Torvalds { 286475c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 28655b04c689SPavel Emelyanov int ret; 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds /* 28681da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 28691da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 28701da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 28711da177e4SLinus Torvalds */ 28725b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 28735b04c689SPavel Emelyanov if (ret) 28745b04c689SPavel Emelyanov goto out; 28751da177e4SLinus Torvalds 28761da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2877078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 2878d8c76e6fSDave Hansen inc_nlink(inode); 28797de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 28801da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 28811da177e4SLinus Torvalds d_instantiate(dentry, inode); 28825b04c689SPavel Emelyanov out: 28835b04c689SPavel Emelyanov return ret; 28841da177e4SLinus Torvalds } 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 28871da177e4SLinus Torvalds { 288875c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 28891da177e4SLinus Torvalds 28905b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 28915b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 2894078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 28959a53c3a7SDave Hansen drop_nlink(inode); 28961da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 28971da177e4SLinus Torvalds return 0; 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds 29001da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 29011da177e4SLinus Torvalds { 29021da177e4SLinus Torvalds if (!simple_empty(dentry)) 29031da177e4SLinus Torvalds return -ENOTEMPTY; 29041da177e4SLinus Torvalds 290575c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 29069a53c3a7SDave Hansen drop_nlink(dir); 29071da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 29081da177e4SLinus Torvalds } 29091da177e4SLinus Torvalds 291037456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 291137456771SMiklos Szeredi { 2912e36cb0b8SDavid Howells bool old_is_dir = d_is_dir(old_dentry); 2913e36cb0b8SDavid Howells bool new_is_dir = d_is_dir(new_dentry); 291437456771SMiklos Szeredi 291537456771SMiklos Szeredi if (old_dir != new_dir && old_is_dir != new_is_dir) { 291637456771SMiklos Szeredi if (old_is_dir) { 291737456771SMiklos Szeredi drop_nlink(old_dir); 291837456771SMiklos Szeredi inc_nlink(new_dir); 291937456771SMiklos Szeredi } else { 292037456771SMiklos Szeredi drop_nlink(new_dir); 292137456771SMiklos Szeredi inc_nlink(old_dir); 292237456771SMiklos Szeredi } 292337456771SMiklos Szeredi } 292437456771SMiklos Szeredi old_dir->i_ctime = old_dir->i_mtime = 292537456771SMiklos Szeredi new_dir->i_ctime = new_dir->i_mtime = 292675c3cfa8SDavid Howells d_inode(old_dentry)->i_ctime = 2927078cd827SDeepa Dinamani d_inode(new_dentry)->i_ctime = current_time(old_dir); 292837456771SMiklos Szeredi 292937456771SMiklos Szeredi return 0; 293037456771SMiklos Szeredi } 293137456771SMiklos Szeredi 293246fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) 293346fdb794SMiklos Szeredi { 293446fdb794SMiklos Szeredi struct dentry *whiteout; 293546fdb794SMiklos Szeredi int error; 293646fdb794SMiklos Szeredi 293746fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 293846fdb794SMiklos Szeredi if (!whiteout) 293946fdb794SMiklos Szeredi return -ENOMEM; 294046fdb794SMiklos Szeredi 294146fdb794SMiklos Szeredi error = shmem_mknod(old_dir, whiteout, 294246fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 294346fdb794SMiklos Szeredi dput(whiteout); 294446fdb794SMiklos Szeredi if (error) 294546fdb794SMiklos Szeredi return error; 294646fdb794SMiklos Szeredi 294746fdb794SMiklos Szeredi /* 294846fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 294946fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 295046fdb794SMiklos Szeredi * 295146fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 295246fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 295346fdb794SMiklos Szeredi */ 295446fdb794SMiklos Szeredi d_rehash(whiteout); 295546fdb794SMiklos Szeredi return 0; 295646fdb794SMiklos Szeredi } 295746fdb794SMiklos Szeredi 29581da177e4SLinus Torvalds /* 29591da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 29601da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 29611da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 29621da177e4SLinus Torvalds * gets overwritten. 29631da177e4SLinus Torvalds */ 29643b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 29651da177e4SLinus Torvalds { 296675c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 29671da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 29681da177e4SLinus Torvalds 296946fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 29703b69ff51SMiklos Szeredi return -EINVAL; 29713b69ff51SMiklos Szeredi 297237456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 297337456771SMiklos Szeredi return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); 297437456771SMiklos Szeredi 29751da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 29761da177e4SLinus Torvalds return -ENOTEMPTY; 29771da177e4SLinus Torvalds 297846fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 297946fdb794SMiklos Szeredi int error; 298046fdb794SMiklos Szeredi 298146fdb794SMiklos Szeredi error = shmem_whiteout(old_dir, old_dentry); 298246fdb794SMiklos Szeredi if (error) 298346fdb794SMiklos Szeredi return error; 298446fdb794SMiklos Szeredi } 298546fdb794SMiklos Szeredi 298675c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 29871da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 2988b928095bSMiklos Szeredi if (they_are_dirs) { 298975c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 29909a53c3a7SDave Hansen drop_nlink(old_dir); 2991b928095bSMiklos Szeredi } 29921da177e4SLinus Torvalds } else if (they_are_dirs) { 29939a53c3a7SDave Hansen drop_nlink(old_dir); 2994d8c76e6fSDave Hansen inc_nlink(new_dir); 29951da177e4SLinus Torvalds } 29961da177e4SLinus Torvalds 29971da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 29981da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 29991da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 30001da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3001078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 30021da177e4SLinus Torvalds return 0; 30031da177e4SLinus Torvalds } 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 30061da177e4SLinus Torvalds { 30071da177e4SLinus Torvalds int error; 30081da177e4SLinus Torvalds int len; 30091da177e4SLinus Torvalds struct inode *inode; 30109276aad6SHugh Dickins struct page *page; 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds len = strlen(symname) + 1; 301309cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 30141da177e4SLinus Torvalds return -ENAMETOOLONG; 30151da177e4SLinus Torvalds 30160825a6f9SJoe Perches inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, 30170825a6f9SJoe Perches VM_NORESERVE); 30181da177e4SLinus Torvalds if (!inode) 30191da177e4SLinus Torvalds return -ENOSPC; 30201da177e4SLinus Torvalds 30219d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 30226d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3023570bc1c2SStephen Smalley if (error) { 3024570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 3025570bc1c2SStephen Smalley iput(inode); 3026570bc1c2SStephen Smalley return error; 3027570bc1c2SStephen Smalley } 3028570bc1c2SStephen Smalley error = 0; 3029570bc1c2SStephen Smalley } 3030570bc1c2SStephen Smalley 30311da177e4SLinus Torvalds inode->i_size = len-1; 303269f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 30333ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 30343ed47db3SAl Viro if (!inode->i_link) { 303569f07ec9SHugh Dickins iput(inode); 303669f07ec9SHugh Dickins return -ENOMEM; 303769f07ec9SHugh Dickins } 303869f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 30391da177e4SLinus Torvalds } else { 3040e8ecde25SAl Viro inode_nohighmem(inode); 30419e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_WRITE); 30421da177e4SLinus Torvalds if (error) { 30431da177e4SLinus Torvalds iput(inode); 30441da177e4SLinus Torvalds return error; 30451da177e4SLinus Torvalds } 304614fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 30471da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 304821fc61c7SAl Viro memcpy(page_address(page), symname, len); 3049ec9516fbSHugh Dickins SetPageUptodate(page); 30501da177e4SLinus Torvalds set_page_dirty(page); 30516746aff7SWu Fengguang unlock_page(page); 305209cbfeafSKirill A. Shutemov put_page(page); 30531da177e4SLinus Torvalds } 30541da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3055078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 30561da177e4SLinus Torvalds d_instantiate(dentry, inode); 30571da177e4SLinus Torvalds dget(dentry); 30581da177e4SLinus Torvalds return 0; 30591da177e4SLinus Torvalds } 30601da177e4SLinus Torvalds 3061fceef393SAl Viro static void shmem_put_link(void *arg) 3062fceef393SAl Viro { 3063fceef393SAl Viro mark_page_accessed(arg); 3064fceef393SAl Viro put_page(arg); 3065fceef393SAl Viro } 3066fceef393SAl Viro 30676b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3068fceef393SAl Viro struct inode *inode, 3069fceef393SAl Viro struct delayed_call *done) 30701da177e4SLinus Torvalds { 30711da177e4SLinus Torvalds struct page *page = NULL; 30726b255391SAl Viro int error; 30736a6c9904SAl Viro if (!dentry) { 30746a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 30756a6c9904SAl Viro if (!page) 30766b255391SAl Viro return ERR_PTR(-ECHILD); 30776a6c9904SAl Viro if (!PageUptodate(page)) { 30786a6c9904SAl Viro put_page(page); 30796a6c9904SAl Viro return ERR_PTR(-ECHILD); 30806a6c9904SAl Viro } 30816a6c9904SAl Viro } else { 30829e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_READ); 3083680baacbSAl Viro if (error) 3084680baacbSAl Viro return ERR_PTR(error); 3085d3602444SHugh Dickins unlock_page(page); 30861da177e4SLinus Torvalds } 3087fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 308821fc61c7SAl Viro return page_address(page); 30891da177e4SLinus Torvalds } 30901da177e4SLinus Torvalds 3091b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3092b09e0fa4SEric Paris /* 3093b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3094b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3095b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3096b09e0fa4SEric Paris * filesystem level, though. 3097b09e0fa4SEric Paris */ 3098b09e0fa4SEric Paris 30996d9d88d0SJarkko Sakkinen /* 31006d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 31016d9d88d0SJarkko Sakkinen */ 31026d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 31036d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 31046d9d88d0SJarkko Sakkinen void *fs_info) 31056d9d88d0SJarkko Sakkinen { 31066d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 31076d9d88d0SJarkko Sakkinen const struct xattr *xattr; 310838f38657SAristeu Rozanski struct simple_xattr *new_xattr; 31096d9d88d0SJarkko Sakkinen size_t len; 31106d9d88d0SJarkko Sakkinen 31116d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 311238f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 31136d9d88d0SJarkko Sakkinen if (!new_xattr) 31146d9d88d0SJarkko Sakkinen return -ENOMEM; 31156d9d88d0SJarkko Sakkinen 31166d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 31176d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 31186d9d88d0SJarkko Sakkinen GFP_KERNEL); 31196d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 31206d9d88d0SJarkko Sakkinen kfree(new_xattr); 31216d9d88d0SJarkko Sakkinen return -ENOMEM; 31226d9d88d0SJarkko Sakkinen } 31236d9d88d0SJarkko Sakkinen 31246d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 31256d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 31266d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 31276d9d88d0SJarkko Sakkinen xattr->name, len); 31286d9d88d0SJarkko Sakkinen 312938f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 31306d9d88d0SJarkko Sakkinen } 31316d9d88d0SJarkko Sakkinen 31326d9d88d0SJarkko Sakkinen return 0; 31336d9d88d0SJarkko Sakkinen } 31346d9d88d0SJarkko Sakkinen 3135aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3136b296821aSAl Viro struct dentry *unused, struct inode *inode, 3137b296821aSAl Viro const char *name, void *buffer, size_t size) 3138aa7c5241SAndreas Gruenbacher { 3139b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3140aa7c5241SAndreas Gruenbacher 3141aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3142aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3143aa7c5241SAndreas Gruenbacher } 3144aa7c5241SAndreas Gruenbacher 3145aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 314659301226SAl Viro struct dentry *unused, struct inode *inode, 314759301226SAl Viro const char *name, const void *value, 314859301226SAl Viro size_t size, int flags) 3149aa7c5241SAndreas Gruenbacher { 315059301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3151aa7c5241SAndreas Gruenbacher 3152aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3153aa7c5241SAndreas Gruenbacher return simple_xattr_set(&info->xattrs, name, value, size, flags); 3154aa7c5241SAndreas Gruenbacher } 3155aa7c5241SAndreas Gruenbacher 3156aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3157aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3158aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3159aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3160aa7c5241SAndreas Gruenbacher }; 3161aa7c5241SAndreas Gruenbacher 3162aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3163aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3164aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3165aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3166aa7c5241SAndreas Gruenbacher }; 3167aa7c5241SAndreas Gruenbacher 3168b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3169b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3170feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3171feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3172b09e0fa4SEric Paris #endif 3173aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3174aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3175b09e0fa4SEric Paris NULL 3176b09e0fa4SEric Paris }; 3177b09e0fa4SEric Paris 3178b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3179b09e0fa4SEric Paris { 318075c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3181786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3182b09e0fa4SEric Paris } 3183b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3184b09e0fa4SEric Paris 318569f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 31866b255391SAl Viro .get_link = simple_get_link, 3187b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3188b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3189b09e0fa4SEric Paris #endif 31901da177e4SLinus Torvalds }; 31911da177e4SLinus Torvalds 319292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 31936b255391SAl Viro .get_link = shmem_get_link, 3194b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3195b09e0fa4SEric Paris .listxattr = shmem_listxattr, 319639f0247dSAndreas Gruenbacher #endif 3197b09e0fa4SEric Paris }; 319839f0247dSAndreas Gruenbacher 319991828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 320091828a40SDavid M. Grimes { 320191828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 320291828a40SDavid M. Grimes } 320391828a40SDavid M. Grimes 320491828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 320591828a40SDavid M. Grimes { 320691828a40SDavid M. Grimes __u32 *fh = vfh; 320791828a40SDavid M. Grimes __u64 inum = fh[2]; 320891828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 320991828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 321091828a40SDavid M. Grimes } 321191828a40SDavid M. Grimes 321212ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 321312ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 321412ba780dSAmir Goldstein { 321512ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 321612ba780dSAmir Goldstein 321712ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 321812ba780dSAmir Goldstein } 321912ba780dSAmir Goldstein 322012ba780dSAmir Goldstein 3221480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3222480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 322391828a40SDavid M. Grimes { 322491828a40SDavid M. Grimes struct inode *inode; 3225480b116cSChristoph Hellwig struct dentry *dentry = NULL; 322635c2a7f4SHugh Dickins u64 inum; 322791828a40SDavid M. Grimes 3228480b116cSChristoph Hellwig if (fh_len < 3) 3229480b116cSChristoph Hellwig return NULL; 3230480b116cSChristoph Hellwig 323135c2a7f4SHugh Dickins inum = fid->raw[2]; 323235c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 323335c2a7f4SHugh Dickins 3234480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3235480b116cSChristoph Hellwig shmem_match, fid->raw); 323691828a40SDavid M. Grimes if (inode) { 323712ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 323891828a40SDavid M. Grimes iput(inode); 323991828a40SDavid M. Grimes } 324091828a40SDavid M. Grimes 3241480b116cSChristoph Hellwig return dentry; 324291828a40SDavid M. Grimes } 324391828a40SDavid M. Grimes 3244b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3245b0b0382bSAl Viro struct inode *parent) 324691828a40SDavid M. Grimes { 32475fe0c237SAneesh Kumar K.V if (*len < 3) { 32485fe0c237SAneesh Kumar K.V *len = 3; 324994e07a75SNamjae Jeon return FILEID_INVALID; 32505fe0c237SAneesh Kumar K.V } 325191828a40SDavid M. Grimes 32521d3382cbSAl Viro if (inode_unhashed(inode)) { 325391828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 325491828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 325591828a40SDavid M. Grimes * time, we need a lock to ensure we only try 325691828a40SDavid M. Grimes * to do it once 325791828a40SDavid M. Grimes */ 325891828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 325991828a40SDavid M. Grimes spin_lock(&lock); 32601d3382cbSAl Viro if (inode_unhashed(inode)) 326191828a40SDavid M. Grimes __insert_inode_hash(inode, 326291828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 326391828a40SDavid M. Grimes spin_unlock(&lock); 326491828a40SDavid M. Grimes } 326591828a40SDavid M. Grimes 326691828a40SDavid M. Grimes fh[0] = inode->i_generation; 326791828a40SDavid M. Grimes fh[1] = inode->i_ino; 326891828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 326991828a40SDavid M. Grimes 327091828a40SDavid M. Grimes *len = 3; 327191828a40SDavid M. Grimes return 1; 327291828a40SDavid M. Grimes } 327391828a40SDavid M. Grimes 327439655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 327591828a40SDavid M. Grimes .get_parent = shmem_get_parent, 327691828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3277480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 327891828a40SDavid M. Grimes }; 327991828a40SDavid M. Grimes 3280680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 3281680d794bSakpm@linux-foundation.org bool remount) 32821da177e4SLinus Torvalds { 32831da177e4SLinus Torvalds char *this_char, *value, *rest; 328449cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 32858751e039SEric W. Biederman uid_t uid; 32868751e039SEric W. Biederman gid_t gid; 32871da177e4SLinus Torvalds 3288b00dc3adSHugh Dickins while (options != NULL) { 3289b00dc3adSHugh Dickins this_char = options; 3290b00dc3adSHugh Dickins for (;;) { 3291b00dc3adSHugh Dickins /* 3292b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3293b00dc3adSHugh Dickins * mount options form a comma-separated list, 3294b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3295b00dc3adSHugh Dickins */ 3296b00dc3adSHugh Dickins options = strchr(options, ','); 3297b00dc3adSHugh Dickins if (options == NULL) 3298b00dc3adSHugh Dickins break; 3299b00dc3adSHugh Dickins options++; 3300b00dc3adSHugh Dickins if (!isdigit(*options)) { 3301b00dc3adSHugh Dickins options[-1] = '\0'; 3302b00dc3adSHugh Dickins break; 3303b00dc3adSHugh Dickins } 3304b00dc3adSHugh Dickins } 33051da177e4SLinus Torvalds if (!*this_char) 33061da177e4SLinus Torvalds continue; 33071da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 33081da177e4SLinus Torvalds *value++ = 0; 33091da177e4SLinus Torvalds } else { 33101170532bSJoe Perches pr_err("tmpfs: No value for mount option '%s'\n", 33111da177e4SLinus Torvalds this_char); 331249cd0a5cSGreg Thelen goto error; 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 33161da177e4SLinus Torvalds unsigned long long size; 33171da177e4SLinus Torvalds size = memparse(value,&rest); 33181da177e4SLinus Torvalds if (*rest == '%') { 33191da177e4SLinus Torvalds size <<= PAGE_SHIFT; 33201da177e4SLinus Torvalds size *= totalram_pages; 33211da177e4SLinus Torvalds do_div(size, 100); 33221da177e4SLinus Torvalds rest++; 33231da177e4SLinus Torvalds } 33241da177e4SLinus Torvalds if (*rest) 33251da177e4SLinus Torvalds goto bad_val; 3326680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 332709cbfeafSKirill A. Shutemov DIV_ROUND_UP(size, PAGE_SIZE); 33281da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 3329680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 33301da177e4SLinus Torvalds if (*rest) 33311da177e4SLinus Torvalds goto bad_val; 33321da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 3333680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 33341da177e4SLinus Torvalds if (*rest) 33351da177e4SLinus Torvalds goto bad_val; 33361da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 3337680d794bSakpm@linux-foundation.org if (remount) 33381da177e4SLinus Torvalds continue; 3339680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 33401da177e4SLinus Torvalds if (*rest) 33411da177e4SLinus Torvalds goto bad_val; 33421da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 3343680d794bSakpm@linux-foundation.org if (remount) 33441da177e4SLinus Torvalds continue; 33458751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 33461da177e4SLinus Torvalds if (*rest) 33471da177e4SLinus Torvalds goto bad_val; 33488751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 33498751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 33508751e039SEric W. Biederman goto bad_val; 33511da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 3352680d794bSakpm@linux-foundation.org if (remount) 33531da177e4SLinus Torvalds continue; 33548751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 33551da177e4SLinus Torvalds if (*rest) 33561da177e4SLinus Torvalds goto bad_val; 33578751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 33588751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 33598751e039SEric W. Biederman goto bad_val; 3360e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 33615a6e75f8SKirill A. Shutemov } else if (!strcmp(this_char, "huge")) { 33625a6e75f8SKirill A. Shutemov int huge; 33635a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(value); 33645a6e75f8SKirill A. Shutemov if (huge < 0) 33655a6e75f8SKirill A. Shutemov goto bad_val; 33665a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 33675a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER) 33685a6e75f8SKirill A. Shutemov goto bad_val; 33695a6e75f8SKirill A. Shutemov sbinfo->huge = huge; 33705a6e75f8SKirill A. Shutemov #endif 33715a6e75f8SKirill A. Shutemov #ifdef CONFIG_NUMA 33727339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 337349cd0a5cSGreg Thelen mpol_put(mpol); 337449cd0a5cSGreg Thelen mpol = NULL; 337549cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 33767339ff83SRobin Holt goto bad_val; 33775a6e75f8SKirill A. Shutemov #endif 33781da177e4SLinus Torvalds } else { 33791170532bSJoe Perches pr_err("tmpfs: Bad mount option %s\n", this_char); 338049cd0a5cSGreg Thelen goto error; 33811da177e4SLinus Torvalds } 33821da177e4SLinus Torvalds } 338349cd0a5cSGreg Thelen sbinfo->mpol = mpol; 33841da177e4SLinus Torvalds return 0; 33851da177e4SLinus Torvalds 33861da177e4SLinus Torvalds bad_val: 33871170532bSJoe Perches pr_err("tmpfs: Bad value '%s' for mount option '%s'\n", 33881da177e4SLinus Torvalds value, this_char); 338949cd0a5cSGreg Thelen error: 339049cd0a5cSGreg Thelen mpol_put(mpol); 33911da177e4SLinus Torvalds return 1; 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds } 33941da177e4SLinus Torvalds 33951da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 33961da177e4SLinus Torvalds { 33971da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3398680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 33990edd73b3SHugh Dickins unsigned long inodes; 34000edd73b3SHugh Dickins int error = -EINVAL; 34011da177e4SLinus Torvalds 34025f00110fSGreg Thelen config.mpol = NULL; 3403680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 34040edd73b3SHugh Dickins return error; 34050edd73b3SHugh Dickins 34060edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 34070edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 34087e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 34090edd73b3SHugh Dickins goto out; 3410680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 34110edd73b3SHugh Dickins goto out; 34120edd73b3SHugh Dickins /* 341354af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 34140edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 34150edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 34160edd73b3SHugh Dickins */ 3417680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 34180edd73b3SHugh Dickins goto out; 3419680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 34200edd73b3SHugh Dickins goto out; 34210edd73b3SHugh Dickins 34220edd73b3SHugh Dickins error = 0; 34235a6e75f8SKirill A. Shutemov sbinfo->huge = config.huge; 3424680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 3425680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 3426680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 342771fe804bSLee Schermerhorn 34285f00110fSGreg Thelen /* 34295f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 34305f00110fSGreg Thelen */ 34315f00110fSGreg Thelen if (config.mpol) { 343271fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 343371fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 34345f00110fSGreg Thelen } 34350edd73b3SHugh Dickins out: 34360edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 34370edd73b3SHugh Dickins return error; 34381da177e4SLinus Torvalds } 3439680d794bSakpm@linux-foundation.org 344034c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3441680d794bSakpm@linux-foundation.org { 344234c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3443680d794bSakpm@linux-foundation.org 3444680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3445680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 344609cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3447680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3448680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 34490825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 345009208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 34518751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 34528751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 34538751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 34548751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 34558751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 34568751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3457e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 34585a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 34595a6e75f8SKirill A. Shutemov if (sbinfo->huge) 34605a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 34615a6e75f8SKirill A. Shutemov #endif 346271fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3463680d794bSakpm@linux-foundation.org return 0; 3464680d794bSakpm@linux-foundation.org } 34659183df25SDavid Herrmann 3466680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 34671da177e4SLinus Torvalds 34681da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 34691da177e4SLinus Torvalds { 3470602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3471602586a8SHugh Dickins 3472602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 347349cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3474602586a8SHugh Dickins kfree(sbinfo); 34751da177e4SLinus Torvalds sb->s_fs_info = NULL; 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds 34782b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 34791da177e4SLinus Torvalds { 34801da177e4SLinus Torvalds struct inode *inode; 34810edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3482680d794bSakpm@linux-foundation.org int err = -ENOMEM; 3483680d794bSakpm@linux-foundation.org 3484680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3485425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3486680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3487680d794bSakpm@linux-foundation.org if (!sbinfo) 3488680d794bSakpm@linux-foundation.org return -ENOMEM; 3489680d794bSakpm@linux-foundation.org 34900825a6f9SJoe Perches sbinfo->mode = 0777 | S_ISVTX; 349176aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 349276aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 3493680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 34941da177e4SLinus Torvalds 34950edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 34961da177e4SLinus Torvalds /* 34971da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 34981da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 34991da177e4SLinus Torvalds * but the internal instance is left unlimited. 35001da177e4SLinus Torvalds */ 35011751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 3502680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 3503680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 3504680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 3505680d794bSakpm@linux-foundation.org err = -EINVAL; 3506680d794bSakpm@linux-foundation.org goto failed; 3507680d794bSakpm@linux-foundation.org } 3508ca4e0519SAl Viro } else { 35091751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 35101da177e4SLinus Torvalds } 351191828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 35121751e8a6SLinus Torvalds sb->s_flags |= SB_NOSEC; 35130edd73b3SHugh Dickins #else 35141751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 35150edd73b3SHugh Dickins #endif 35161da177e4SLinus Torvalds 35171da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 3518908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3519602586a8SHugh Dickins goto failed; 3520680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 3521779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3522779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 35231da177e4SLinus Torvalds 3524285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 352509cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 352609cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 35271da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 35281da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3529cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3530b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 353139f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3532b09e0fa4SEric Paris #endif 3533b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 35341751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 353539f0247dSAndreas Gruenbacher #endif 35362b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 35370edd73b3SHugh Dickins 3538454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 35391da177e4SLinus Torvalds if (!inode) 35401da177e4SLinus Torvalds goto failed; 3541680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3542680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3543318ceed0SAl Viro sb->s_root = d_make_root(inode); 3544318ceed0SAl Viro if (!sb->s_root) 354548fde701SAl Viro goto failed; 35461da177e4SLinus Torvalds return 0; 35471da177e4SLinus Torvalds 35481da177e4SLinus Torvalds failed: 35491da177e4SLinus Torvalds shmem_put_super(sb); 35501da177e4SLinus Torvalds return err; 35511da177e4SLinus Torvalds } 35521da177e4SLinus Torvalds 3553fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 35541da177e4SLinus Torvalds 35551da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 35561da177e4SLinus Torvalds { 355741ffe5d5SHugh Dickins struct shmem_inode_info *info; 355841ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 355941ffe5d5SHugh Dickins if (!info) 35601da177e4SLinus Torvalds return NULL; 356141ffe5d5SHugh Dickins return &info->vfs_inode; 35621da177e4SLinus Torvalds } 35631da177e4SLinus Torvalds 356441ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 3565fa0d7e3dSNick Piggin { 3566fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 356784e710daSAl Viro if (S_ISLNK(inode->i_mode)) 35683ed47db3SAl Viro kfree(inode->i_link); 3569fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3570fa0d7e3dSNick Piggin } 3571fa0d7e3dSNick Piggin 35721da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 35731da177e4SLinus Torvalds { 357409208d15SAl Viro if (S_ISREG(inode->i_mode)) 35751da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 357641ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 35771da177e4SLinus Torvalds } 35781da177e4SLinus Torvalds 357941ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 35801da177e4SLinus Torvalds { 358141ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 358241ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 35831da177e4SLinus Torvalds } 35841da177e4SLinus Torvalds 35859a8ec03eSweiping zhang static void shmem_init_inodecache(void) 35861da177e4SLinus Torvalds { 35871da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 35881da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 35895d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 35901da177e4SLinus Torvalds } 35911da177e4SLinus Torvalds 359241ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 35931da177e4SLinus Torvalds { 35941a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 35951da177e4SLinus Torvalds } 35961da177e4SLinus Torvalds 3597f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 35981da177e4SLinus Torvalds .writepage = shmem_writepage, 359976719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 36001da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3601800d15a5SNick Piggin .write_begin = shmem_write_begin, 3602800d15a5SNick Piggin .write_end = shmem_write_end, 36031da177e4SLinus Torvalds #endif 36041c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 3605304dbdb7SLee Schermerhorn .migratepage = migrate_page, 36061c93923cSAndrew Morton #endif 3607aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36081da177e4SLinus Torvalds }; 36091da177e4SLinus Torvalds 361015ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 36111da177e4SLinus Torvalds .mmap = shmem_mmap, 3612c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 36131da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3614220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 36152ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 36168174202bSAl Viro .write_iter = generic_file_write_iter, 36171b061d92SChristoph Hellwig .fsync = noop_fsync, 361882c156f8SAl Viro .splice_read = generic_file_splice_read, 3619f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 362083e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 36211da177e4SLinus Torvalds #endif 36221da177e4SLinus Torvalds }; 36231da177e4SLinus Torvalds 362492e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 362544a30220SYu Zhao .getattr = shmem_getattr, 362694c1e62dSHugh Dickins .setattr = shmem_setattr, 3627b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3628b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3629feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3630b09e0fa4SEric Paris #endif 36311da177e4SLinus Torvalds }; 36321da177e4SLinus Torvalds 363392e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 36341da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 36351da177e4SLinus Torvalds .create = shmem_create, 36361da177e4SLinus Torvalds .lookup = simple_lookup, 36371da177e4SLinus Torvalds .link = shmem_link, 36381da177e4SLinus Torvalds .unlink = shmem_unlink, 36391da177e4SLinus Torvalds .symlink = shmem_symlink, 36401da177e4SLinus Torvalds .mkdir = shmem_mkdir, 36411da177e4SLinus Torvalds .rmdir = shmem_rmdir, 36421da177e4SLinus Torvalds .mknod = shmem_mknod, 36432773bf00SMiklos Szeredi .rename = shmem_rename2, 364460545d0dSAl Viro .tmpfile = shmem_tmpfile, 36451da177e4SLinus Torvalds #endif 3646b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3647b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3648b09e0fa4SEric Paris #endif 364939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 365094c1e62dSHugh Dickins .setattr = shmem_setattr, 3651feda821eSChristoph Hellwig .set_acl = simple_set_acl, 365239f0247dSAndreas Gruenbacher #endif 365339f0247dSAndreas Gruenbacher }; 365439f0247dSAndreas Gruenbacher 365592e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3656b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3657b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3658b09e0fa4SEric Paris #endif 365939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 366094c1e62dSHugh Dickins .setattr = shmem_setattr, 3661feda821eSChristoph Hellwig .set_acl = simple_set_acl, 366239f0247dSAndreas Gruenbacher #endif 36631da177e4SLinus Torvalds }; 36641da177e4SLinus Torvalds 3665759b9775SHugh Dickins static const struct super_operations shmem_ops = { 36661da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 36671da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 36681da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 36691da177e4SLinus Torvalds .statfs = shmem_statfs, 36701da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 3671680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 36721da177e4SLinus Torvalds #endif 36731f895f75SAl Viro .evict_inode = shmem_evict_inode, 36741da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 36751da177e4SLinus Torvalds .put_super = shmem_put_super, 3676779750d2SKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3677779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3678779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3679779750d2SKirill A. Shutemov #endif 36801da177e4SLinus Torvalds }; 36811da177e4SLinus Torvalds 3682f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 368354cb8821SNick Piggin .fault = shmem_fault, 3684d7c17551SNing Qu .map_pages = filemap_map_pages, 36851da177e4SLinus Torvalds #ifdef CONFIG_NUMA 36861da177e4SLinus Torvalds .set_policy = shmem_set_policy, 36871da177e4SLinus Torvalds .get_policy = shmem_get_policy, 36881da177e4SLinus Torvalds #endif 36891da177e4SLinus Torvalds }; 36901da177e4SLinus Torvalds 36913c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 36923c26ff6eSAl Viro int flags, const char *dev_name, void *data) 36931da177e4SLinus Torvalds { 36943c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 36951da177e4SLinus Torvalds } 36961da177e4SLinus Torvalds 369741ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 36981da177e4SLinus Torvalds .owner = THIS_MODULE, 36991da177e4SLinus Torvalds .name = "tmpfs", 37003c26ff6eSAl Viro .mount = shmem_mount, 37011da177e4SLinus Torvalds .kill_sb = kill_litter_super, 37022b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 37031da177e4SLinus Torvalds }; 37041da177e4SLinus Torvalds 370541ffe5d5SHugh Dickins int __init shmem_init(void) 37061da177e4SLinus Torvalds { 37071da177e4SLinus Torvalds int error; 37081da177e4SLinus Torvalds 370916203a7aSRob Landley /* If rootfs called this, don't re-init */ 371016203a7aSRob Landley if (shmem_inode_cachep) 371116203a7aSRob Landley return 0; 371216203a7aSRob Landley 37139a8ec03eSweiping zhang shmem_init_inodecache(); 37141da177e4SLinus Torvalds 371541ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 37161da177e4SLinus Torvalds if (error) { 37171170532bSJoe Perches pr_err("Could not register tmpfs\n"); 37181da177e4SLinus Torvalds goto out2; 37191da177e4SLinus Torvalds } 372095dc112aSGreg Kroah-Hartman 3721ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 37221da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 37231da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 37241170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 37251da177e4SLinus Torvalds goto out1; 37261da177e4SLinus Torvalds } 37275a6e75f8SKirill A. Shutemov 3728e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3729435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 37305a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 37315a6e75f8SKirill A. Shutemov else 37325a6e75f8SKirill A. Shutemov shmem_huge = 0; /* just in case it was patched */ 37335a6e75f8SKirill A. Shutemov #endif 37341da177e4SLinus Torvalds return 0; 37351da177e4SLinus Torvalds 37361da177e4SLinus Torvalds out1: 373741ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 37381da177e4SLinus Torvalds out2: 373941ffe5d5SHugh Dickins shmem_destroy_inodecache(); 37401da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 37411da177e4SLinus Torvalds return error; 37421da177e4SLinus Torvalds } 3743853ac43aSMatt Mackall 3744e496cf3dSKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS) 37455a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 37465a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 37475a6e75f8SKirill A. Shutemov { 37485a6e75f8SKirill A. Shutemov int values[] = { 37495a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 37505a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 37515a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 37525a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 37535a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 37545a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 37555a6e75f8SKirill A. Shutemov }; 37565a6e75f8SKirill A. Shutemov int i, count; 37575a6e75f8SKirill A. Shutemov 37585a6e75f8SKirill A. Shutemov for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { 37595a6e75f8SKirill A. Shutemov const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; 37605a6e75f8SKirill A. Shutemov 37615a6e75f8SKirill A. Shutemov count += sprintf(buf + count, fmt, 37625a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 37635a6e75f8SKirill A. Shutemov } 37645a6e75f8SKirill A. Shutemov buf[count - 1] = '\n'; 37655a6e75f8SKirill A. Shutemov return count; 37665a6e75f8SKirill A. Shutemov } 37675a6e75f8SKirill A. Shutemov 37685a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 37695a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 37705a6e75f8SKirill A. Shutemov { 37715a6e75f8SKirill A. Shutemov char tmp[16]; 37725a6e75f8SKirill A. Shutemov int huge; 37735a6e75f8SKirill A. Shutemov 37745a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 37755a6e75f8SKirill A. Shutemov return -EINVAL; 37765a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 37775a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 37785a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 37795a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 37805a6e75f8SKirill A. Shutemov 37815a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 37825a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 37835a6e75f8SKirill A. Shutemov return -EINVAL; 37845a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 37855a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 37865a6e75f8SKirill A. Shutemov return -EINVAL; 37875a6e75f8SKirill A. Shutemov 37885a6e75f8SKirill A. Shutemov shmem_huge = huge; 3789435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 37905a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 37915a6e75f8SKirill A. Shutemov return count; 37925a6e75f8SKirill A. Shutemov } 37935a6e75f8SKirill A. Shutemov 37945a6e75f8SKirill A. Shutemov struct kobj_attribute shmem_enabled_attr = 37955a6e75f8SKirill A. Shutemov __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); 37963b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */ 3797f3f0e1d2SKirill A. Shutemov 37983b33719cSArnd Bergmann #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3799f3f0e1d2SKirill A. Shutemov bool shmem_huge_enabled(struct vm_area_struct *vma) 3800f3f0e1d2SKirill A. Shutemov { 3801f3f0e1d2SKirill A. Shutemov struct inode *inode = file_inode(vma->vm_file); 3802f3f0e1d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 3803f3f0e1d2SKirill A. Shutemov loff_t i_size; 3804f3f0e1d2SKirill A. Shutemov pgoff_t off; 3805f3f0e1d2SKirill A. Shutemov 3806f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 3807f3f0e1d2SKirill A. Shutemov return true; 3808f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY) 3809f3f0e1d2SKirill A. Shutemov return false; 3810f3f0e1d2SKirill A. Shutemov switch (sbinfo->huge) { 3811f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_NEVER: 3812f3f0e1d2SKirill A. Shutemov return false; 3813f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 3814f3f0e1d2SKirill A. Shutemov return true; 3815f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 3816f3f0e1d2SKirill A. Shutemov off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); 3817f3f0e1d2SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 3818f3f0e1d2SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 3819f3f0e1d2SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 3820f3f0e1d2SKirill A. Shutemov return true; 3821c8402871SGustavo A. R. Silva /* fall through */ 3822f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 3823f3f0e1d2SKirill A. Shutemov /* TODO: implement fadvise() hints */ 3824f3f0e1d2SKirill A. Shutemov return (vma->vm_flags & VM_HUGEPAGE); 3825f3f0e1d2SKirill A. Shutemov default: 3826f3f0e1d2SKirill A. Shutemov VM_BUG_ON(1); 3827f3f0e1d2SKirill A. Shutemov return false; 3828f3f0e1d2SKirill A. Shutemov } 3829f3f0e1d2SKirill A. Shutemov } 38303b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 38315a6e75f8SKirill A. Shutemov 3832853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 3833853ac43aSMatt Mackall 3834853ac43aSMatt Mackall /* 3835853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 3836853ac43aSMatt Mackall * 3837853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 3838853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 3839853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 3840853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 3841853ac43aSMatt Mackall */ 3842853ac43aSMatt Mackall 384341ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 3844853ac43aSMatt Mackall .name = "tmpfs", 38453c26ff6eSAl Viro .mount = ramfs_mount, 3846853ac43aSMatt Mackall .kill_sb = kill_litter_super, 38472b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 3848853ac43aSMatt Mackall }; 3849853ac43aSMatt Mackall 385041ffe5d5SHugh Dickins int __init shmem_init(void) 3851853ac43aSMatt Mackall { 385241ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 3853853ac43aSMatt Mackall 385441ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 3855853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 3856853ac43aSMatt Mackall 3857853ac43aSMatt Mackall return 0; 3858853ac43aSMatt Mackall } 3859853ac43aSMatt Mackall 386041ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 3861853ac43aSMatt Mackall { 3862853ac43aSMatt Mackall return 0; 3863853ac43aSMatt Mackall } 3864853ac43aSMatt Mackall 38653f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 38663f96b79aSHugh Dickins { 38673f96b79aSHugh Dickins return 0; 38683f96b79aSHugh Dickins } 38693f96b79aSHugh Dickins 387024513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 387124513264SHugh Dickins { 387224513264SHugh Dickins } 387324513264SHugh Dickins 3874c01d5b30SHugh Dickins #ifdef CONFIG_MMU 3875c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 3876c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 3877c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 3878c01d5b30SHugh Dickins { 3879c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 3880c01d5b30SHugh Dickins } 3881c01d5b30SHugh Dickins #endif 3882c01d5b30SHugh Dickins 388341ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 388494c1e62dSHugh Dickins { 388541ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 388694c1e62dSHugh Dickins } 388794c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 388894c1e62dSHugh Dickins 3889853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 38900b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 3891454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 38920b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 38930b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 3894853ac43aSMatt Mackall 3895853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 3896853ac43aSMatt Mackall 3897853ac43aSMatt Mackall /* common code */ 38981da177e4SLinus Torvalds 389919938e35SRasmus Villemoes static const struct dentry_operations anon_ops = { 3900118b2302SAl Viro .d_dname = simple_dname 39013451538aSAl Viro }; 39023451538aSAl Viro 3903703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 3904c7277090SEric Paris unsigned long flags, unsigned int i_flags) 39051da177e4SLinus Torvalds { 39066b4d0b27SAl Viro struct file *res; 39071da177e4SLinus Torvalds struct inode *inode; 39082c48b9c4SAl Viro struct path path; 39093451538aSAl Viro struct super_block *sb; 39101da177e4SLinus Torvalds struct qstr this; 39111da177e4SLinus Torvalds 3912703321b6SMatthew Auld if (IS_ERR(mnt)) 3913703321b6SMatthew Auld return ERR_CAST(mnt); 39141da177e4SLinus Torvalds 3915285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 39161da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 39171da177e4SLinus Torvalds 39181da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 39191da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 39201da177e4SLinus Torvalds 3921703321b6SMatthew Auld sb = mnt->mnt_sb; 3922*dac2d1f6SAl Viro inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 3923*dac2d1f6SAl Viro if (unlikely(!inode)) { 3924*dac2d1f6SAl Viro shmem_unacct_size(flags, size); 3925*dac2d1f6SAl Viro return ERR_PTR(-ENOSPC); 3926*dac2d1f6SAl Viro } 3927c7277090SEric Paris inode->i_flags |= i_flags; 39281da177e4SLinus Torvalds inode->i_size = size; 39296d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 393026567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 3931*dac2d1f6SAl Viro if (IS_ERR(res)) { 3932*dac2d1f6SAl Viro iput(inode); 3933*dac2d1f6SAl Viro return res; 3934*dac2d1f6SAl Viro } 3935*dac2d1f6SAl Viro 3936*dac2d1f6SAl Viro this.name = name; 3937*dac2d1f6SAl Viro this.len = strlen(name); 3938*dac2d1f6SAl Viro this.hash = 0; /* will go */ 3939*dac2d1f6SAl Viro path.mnt = mntget(mnt); 3940*dac2d1f6SAl Viro path.dentry = d_alloc_pseudo(sb, &this); 3941*dac2d1f6SAl Viro if (!path.dentry) { 3942*dac2d1f6SAl Viro iput(inode); 3943*dac2d1f6SAl Viro return ERR_PTR(-ENOMEM); 3944*dac2d1f6SAl Viro } 3945*dac2d1f6SAl Viro d_set_d_op(path.dentry, &anon_ops); 3946*dac2d1f6SAl Viro 3947*dac2d1f6SAl Viro d_instantiate(path.dentry, inode); 39484b42af81SAl Viro 3949c9c554f2SAl Viro res = alloc_file(&path, O_RDWR, &shmem_file_operations); 39506b4d0b27SAl Viro if (IS_ERR(res)) 395166ee4b88SKonstantin Khlebnikov path_put(&path); 39526b4d0b27SAl Viro return res; 39531da177e4SLinus Torvalds } 3954c7277090SEric Paris 3955c7277090SEric Paris /** 3956c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 3957c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 3958c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 3959e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 3960e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 3961c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3962c7277090SEric Paris * @size: size to be set for the file 3963c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3964c7277090SEric Paris */ 3965c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 3966c7277090SEric Paris { 3967703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 3968c7277090SEric Paris } 3969c7277090SEric Paris 3970c7277090SEric Paris /** 3971c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 3972c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3973c7277090SEric Paris * @size: size to be set for the file 3974c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3975c7277090SEric Paris */ 3976c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 3977c7277090SEric Paris { 3978703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 3979c7277090SEric Paris } 3980395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 39811da177e4SLinus Torvalds 398246711810SRandy Dunlap /** 3983703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 3984703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 3985703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 3986703321b6SMatthew Auld * @size: size to be set for the file 3987703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3988703321b6SMatthew Auld */ 3989703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 3990703321b6SMatthew Auld loff_t size, unsigned long flags) 3991703321b6SMatthew Auld { 3992703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 3993703321b6SMatthew Auld } 3994703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 3995703321b6SMatthew Auld 3996703321b6SMatthew Auld /** 39971da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 39981da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 39991da177e4SLinus Torvalds */ 40001da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 40011da177e4SLinus Torvalds { 40021da177e4SLinus Torvalds struct file *file; 40031da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 40041da177e4SLinus Torvalds 400566fc1303SHugh Dickins /* 400666fc1303SHugh Dickins * Cloning a new file under mmap_sem leads to a lock ordering conflict 400766fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 400866fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 400966fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 401066fc1303SHugh Dickins */ 4011703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 40121da177e4SLinus Torvalds if (IS_ERR(file)) 40131da177e4SLinus Torvalds return PTR_ERR(file); 40141da177e4SLinus Torvalds 40151da177e4SLinus Torvalds if (vma->vm_file) 40161da177e4SLinus Torvalds fput(vma->vm_file); 40171da177e4SLinus Torvalds vma->vm_file = file; 40181da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 4019f3f0e1d2SKirill A. Shutemov 4020e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 4021f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 4022f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 4023f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 4024f3f0e1d2SKirill A. Shutemov } 4025f3f0e1d2SKirill A. Shutemov 40261da177e4SLinus Torvalds return 0; 40271da177e4SLinus Torvalds } 4028d9d90e5eSHugh Dickins 4029d9d90e5eSHugh Dickins /** 4030d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 4031d9d90e5eSHugh Dickins * @mapping: the page's address_space 4032d9d90e5eSHugh Dickins * @index: the page index 4033d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4034d9d90e5eSHugh Dickins * 4035d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4036d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 4037d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 4038d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4039d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4040d9d90e5eSHugh Dickins * 404168da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 404268da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4043d9d90e5eSHugh Dickins */ 4044d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4045d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4046d9d90e5eSHugh Dickins { 404768da9f05SHugh Dickins #ifdef CONFIG_SHMEM 404868da9f05SHugh Dickins struct inode *inode = mapping->host; 40499276aad6SHugh Dickins struct page *page; 405068da9f05SHugh Dickins int error; 405168da9f05SHugh Dickins 405268da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 40539e18eb29SAndres Lagar-Cavilla error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, 4054cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 405568da9f05SHugh Dickins if (error) 405668da9f05SHugh Dickins page = ERR_PTR(error); 405768da9f05SHugh Dickins else 405868da9f05SHugh Dickins unlock_page(page); 405968da9f05SHugh Dickins return page; 406068da9f05SHugh Dickins #else 406168da9f05SHugh Dickins /* 406268da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 406368da9f05SHugh Dickins */ 4064d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 406568da9f05SHugh Dickins #endif 4066d9d90e5eSHugh Dickins } 4067d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4068