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> 3246c9a946SArnd Bergmann #include <linux/random.h> 33174cd4b1SIngo Molnar #include <linux/sched/signal.h> 34b95f1b31SPaul Gortmaker #include <linux/export.h> 35853ac43aSMatt Mackall #include <linux/swap.h> 36e2e40f2cSChristoph Hellwig #include <linux/uio.h> 37f3f0e1d2SKirill A. Shutemov #include <linux/khugepaged.h> 38749df87bSMike Kravetz #include <linux/hugetlb.h> 39853ac43aSMatt Mackall 4095cc09d6SAndrea Arcangeli #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ 4195cc09d6SAndrea Arcangeli 42853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 43853ac43aSMatt Mackall 44853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 471da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 481da177e4SLinus Torvalds * which makes it a completely usable filesystem. 491da177e4SLinus Torvalds */ 501da177e4SLinus Torvalds 5139f0247dSAndreas Gruenbacher #include <linux/xattr.h> 52a5694255SChristoph Hellwig #include <linux/exportfs.h> 531c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 54feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 551da177e4SLinus Torvalds #include <linux/mman.h> 561da177e4SLinus Torvalds #include <linux/string.h> 571da177e4SLinus Torvalds #include <linux/slab.h> 581da177e4SLinus Torvalds #include <linux/backing-dev.h> 591da177e4SLinus Torvalds #include <linux/shmem_fs.h> 601da177e4SLinus Torvalds #include <linux/writeback.h> 611da177e4SLinus Torvalds #include <linux/blkdev.h> 62bda97eabSHugh Dickins #include <linux/pagevec.h> 6341ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6483e4fa9cSHugh Dickins #include <linux/falloc.h> 65708e3508SHugh Dickins #include <linux/splice.h> 661da177e4SLinus Torvalds #include <linux/security.h> 671da177e4SLinus Torvalds #include <linux/swapops.h> 681da177e4SLinus Torvalds #include <linux/mempolicy.h> 691da177e4SLinus Torvalds #include <linux/namei.h> 70b00dc3adSHugh Dickins #include <linux/ctype.h> 71304dbdb7SLee Schermerhorn #include <linux/migrate.h> 72c1f60a5aSChristoph Lameter #include <linux/highmem.h> 73680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7492562927SMimi Zohar #include <linux/magic.h> 759183df25SDavid Herrmann #include <linux/syscalls.h> 7640e041a2SDavid Herrmann #include <linux/fcntl.h> 779183df25SDavid Herrmann #include <uapi/linux/memfd.h> 78cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 794c27fe4cSMike Rapoport #include <linux/rmap.h> 802b4db796SAmir Goldstein #include <linux/uuid.h> 81304dbdb7SLee Schermerhorn 827c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 831da177e4SLinus Torvalds #include <asm/pgtable.h> 841da177e4SLinus Torvalds 85dd56b046SMel Gorman #include "internal.h" 86dd56b046SMel Gorman 8709cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 8809cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 911da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 921da177e4SLinus Torvalds 9369f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9469f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9569f07ec9SHugh Dickins 961aac1400SHugh Dickins /* 97f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 98f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 99f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 1001aac1400SHugh Dickins */ 1011aac1400SHugh Dickins struct shmem_falloc { 1028e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 1031aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1041aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1051aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1061aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1071aac1400SHugh Dickins }; 1081aac1400SHugh Dickins 109b76db735SAndrew Morton #ifdef CONFIG_TMPFS 110680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 111680d794bSakpm@linux-foundation.org { 112ca79b0c2SArun KS return totalram_pages() / 2; 113680d794bSakpm@linux-foundation.org } 114680d794bSakpm@linux-foundation.org 115680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 116680d794bSakpm@linux-foundation.org { 117ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 118ca79b0c2SArun KS 119ca79b0c2SArun KS return min(nr_pages - totalhigh_pages(), nr_pages / 2); 120680d794bSakpm@linux-foundation.org } 121b76db735SAndrew Morton #endif 122680d794bSakpm@linux-foundation.org 123bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 124bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 125bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 12668da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1279e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, 128cfda0526SMike Rapoport gfp_t gfp, struct vm_area_struct *vma, 1292b740303SSouptick Joarder struct vm_fault *vmf, vm_fault_t *fault_type); 13068da9f05SHugh Dickins 131f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index, 1329e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp) 13368da9f05SHugh Dickins { 13468da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 135cfda0526SMike Rapoport mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 13668da9f05SHugh Dickins } 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds return sb->s_fs_info; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /* 1441da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1451da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1461da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1471da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1481da177e4SLinus Torvalds */ 1491da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1501da177e4SLinus Torvalds { 1510b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 152191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1561da177e4SLinus Torvalds { 1570b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1581da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 16177142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 16277142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 16377142517SKonstantin Khlebnikov { 16477142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 16577142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 16677142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 16777142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 16877142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 16977142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 17077142517SKonstantin Khlebnikov } 17177142517SKonstantin Khlebnikov return 0; 17277142517SKonstantin Khlebnikov } 17377142517SKonstantin Khlebnikov 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 17675edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 1771da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1781da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1791da177e4SLinus Torvalds */ 180800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1811da177e4SLinus Torvalds { 182800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 183800d8c63SKirill A. Shutemov return 0; 184800d8c63SKirill A. Shutemov 185800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 186800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1901da177e4SLinus Torvalds { 1910b0a0806SHugh Dickins if (flags & VM_NORESERVE) 19209cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1950f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages) 1960f079694SMike Rapoport { 1970f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 1980f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1990f079694SMike Rapoport 2000f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 2010f079694SMike Rapoport return false; 2020f079694SMike Rapoport 2030f079694SMike Rapoport if (sbinfo->max_blocks) { 2040f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2050f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2060f079694SMike Rapoport goto unacct; 2070f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 2080f079694SMike Rapoport } 2090f079694SMike Rapoport 2100f079694SMike Rapoport return true; 2110f079694SMike Rapoport 2120f079694SMike Rapoport unacct: 2130f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2140f079694SMike Rapoport return false; 2150f079694SMike Rapoport } 2160f079694SMike Rapoport 2170f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2180f079694SMike Rapoport { 2190f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2200f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2210f079694SMike Rapoport 2220f079694SMike Rapoport if (sbinfo->max_blocks) 2230f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2240f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2250f079694SMike Rapoport } 2260f079694SMike Rapoport 227759b9775SHugh Dickins static const struct super_operations shmem_ops; 228f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 22915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 23092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 23192e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 23292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 233f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 234779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2351da177e4SLinus Torvalds 236b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 237b0506e48SMike Rapoport { 238b0506e48SMike Rapoport return vma->vm_ops == &shmem_vm_ops; 239b0506e48SMike Rapoport } 240b0506e48SMike Rapoport 2411da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 242cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2431da177e4SLinus Torvalds 2445b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2455b04c689SPavel Emelyanov { 2465b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2475b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2485b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2495b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2505b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2515b04c689SPavel Emelyanov return -ENOSPC; 2525b04c689SPavel Emelyanov } 2535b04c689SPavel Emelyanov sbinfo->free_inodes--; 2545b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2555b04c689SPavel Emelyanov } 2565b04c689SPavel Emelyanov return 0; 2575b04c689SPavel Emelyanov } 2585b04c689SPavel Emelyanov 2595b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2605b04c689SPavel Emelyanov { 2615b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2625b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2635b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2645b04c689SPavel Emelyanov sbinfo->free_inodes++; 2655b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2665b04c689SPavel Emelyanov } 2675b04c689SPavel Emelyanov } 2685b04c689SPavel Emelyanov 26946711810SRandy Dunlap /** 27041ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2711da177e4SLinus Torvalds * @inode: inode to recalc 2721da177e4SLinus Torvalds * 2731da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2741da177e4SLinus Torvalds * undirtied hole pages behind our back. 2751da177e4SLinus Torvalds * 2761da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2771da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2781da177e4SLinus Torvalds * 2791da177e4SLinus Torvalds * It has to be called with the spinlock held. 2801da177e4SLinus Torvalds */ 2811da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2821da177e4SLinus Torvalds { 2831da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2841da177e4SLinus Torvalds long freed; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2871da177e4SLinus Torvalds if (freed > 0) { 2881da177e4SLinus Torvalds info->alloced -= freed; 28954af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2900f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 294800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 295800d8c63SKirill A. Shutemov { 296800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 2974595ef88SKirill A. Shutemov unsigned long flags; 298800d8c63SKirill A. Shutemov 2990f079694SMike Rapoport if (!shmem_inode_acct_block(inode, pages)) 300800d8c63SKirill A. Shutemov return false; 301b1cc94abSMike Rapoport 302aaa52e34SHugh Dickins /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ 303aaa52e34SHugh Dickins inode->i_mapping->nrpages += pages; 304aaa52e34SHugh Dickins 3054595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 306800d8c63SKirill A. Shutemov info->alloced += pages; 307800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 308800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3094595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 310800d8c63SKirill A. Shutemov 311800d8c63SKirill A. Shutemov return true; 312800d8c63SKirill A. Shutemov } 313800d8c63SKirill A. Shutemov 314800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 315800d8c63SKirill A. Shutemov { 316800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3174595ef88SKirill A. Shutemov unsigned long flags; 318800d8c63SKirill A. Shutemov 319aaa52e34SHugh Dickins /* nrpages adjustment done by __delete_from_page_cache() or caller */ 320aaa52e34SHugh Dickins 3214595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 322800d8c63SKirill A. Shutemov info->alloced -= pages; 323800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 324800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3254595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 326800d8c63SKirill A. Shutemov 3270f079694SMike Rapoport shmem_inode_unacct_blocks(inode, pages); 328800d8c63SKirill A. Shutemov } 329800d8c63SKirill A. Shutemov 3307a5d0fbbSHugh Dickins /* 33162f945b6SMatthew Wilcox * Replace item expected in xarray by a new item, while holding xa_lock. 3327a5d0fbbSHugh Dickins */ 33362f945b6SMatthew Wilcox static int shmem_replace_entry(struct address_space *mapping, 3347a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 3357a5d0fbbSHugh Dickins { 33662f945b6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 3376dbaf22cSJohannes Weiner void *item; 3387a5d0fbbSHugh Dickins 3397a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 3406dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 34162f945b6SMatthew Wilcox item = xas_load(&xas); 3427a5d0fbbSHugh Dickins if (item != expected) 3437a5d0fbbSHugh Dickins return -ENOENT; 34462f945b6SMatthew Wilcox xas_store(&xas, replacement); 3457a5d0fbbSHugh Dickins return 0; 3467a5d0fbbSHugh Dickins } 3477a5d0fbbSHugh Dickins 3487a5d0fbbSHugh Dickins /* 349d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 350d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 351d1899228SHugh Dickins * 352d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 353d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 354d1899228SHugh Dickins */ 355d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 356d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 357d1899228SHugh Dickins { 358a12831bfSMatthew Wilcox return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); 359d1899228SHugh Dickins } 360d1899228SHugh Dickins 361d1899228SHugh Dickins /* 3625a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 3635a6e75f8SKirill A. Shutemov * 3645a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 3655a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 3665a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 3675a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 3685a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 3695a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 3705a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 3715a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 3725a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 3735a6e75f8SKirill A. Shutemov */ 3745a6e75f8SKirill A. Shutemov 3755a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 3765a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 3775a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 3785a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 3795a6e75f8SKirill A. Shutemov 3805a6e75f8SKirill A. Shutemov /* 3815a6e75f8SKirill A. Shutemov * Special values. 3825a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 3835a6e75f8SKirill A. Shutemov * 3845a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 3855a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 3865a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 3875a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 3885a6e75f8SKirill A. Shutemov * 3895a6e75f8SKirill A. Shutemov */ 3905a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 3915a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 3925a6e75f8SKirill A. Shutemov 393e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3945a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 3955a6e75f8SKirill A. Shutemov 3965b9c98f3SMike Kravetz static int shmem_huge __read_mostly; 3975a6e75f8SKirill A. Shutemov 398f1f5929cSJérémy Lefaure #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 3995a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 4005a6e75f8SKirill A. Shutemov { 4015a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 4025a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 4035a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 4045a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 4055a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 4065a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 4075a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 4085a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 4095a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 4105a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 4115a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 4125a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 4135a6e75f8SKirill A. Shutemov return -EINVAL; 4145a6e75f8SKirill A. Shutemov } 4155a6e75f8SKirill A. Shutemov 4165a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 4175a6e75f8SKirill A. Shutemov { 4185a6e75f8SKirill A. Shutemov switch (huge) { 4195a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 4205a6e75f8SKirill A. Shutemov return "never"; 4215a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 4225a6e75f8SKirill A. Shutemov return "always"; 4235a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 4245a6e75f8SKirill A. Shutemov return "within_size"; 4255a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4265a6e75f8SKirill A. Shutemov return "advise"; 4275a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 4285a6e75f8SKirill A. Shutemov return "deny"; 4295a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 4305a6e75f8SKirill A. Shutemov return "force"; 4315a6e75f8SKirill A. Shutemov default: 4325a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 4335a6e75f8SKirill A. Shutemov return "bad_val"; 4345a6e75f8SKirill A. Shutemov } 4355a6e75f8SKirill A. Shutemov } 436f1f5929cSJérémy Lefaure #endif 4375a6e75f8SKirill A. Shutemov 438779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 439779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 440779750d2SKirill A. Shutemov { 441779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 442253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 443779750d2SKirill A. Shutemov struct inode *inode; 444779750d2SKirill A. Shutemov struct shmem_inode_info *info; 445779750d2SKirill A. Shutemov struct page *page; 446779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 447779750d2SKirill A. Shutemov int removed = 0, split = 0; 448779750d2SKirill A. Shutemov 449779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 450779750d2SKirill A. Shutemov return SHRINK_STOP; 451779750d2SKirill A. Shutemov 452779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 453779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 454779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 455779750d2SKirill A. Shutemov 456779750d2SKirill A. Shutemov /* pin the inode */ 457779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 458779750d2SKirill A. Shutemov 459779750d2SKirill A. Shutemov /* inode is about to be evicted */ 460779750d2SKirill A. Shutemov if (!inode) { 461779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 462779750d2SKirill A. Shutemov removed++; 463779750d2SKirill A. Shutemov goto next; 464779750d2SKirill A. Shutemov } 465779750d2SKirill A. Shutemov 466779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 467779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 468779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 469253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 470779750d2SKirill A. Shutemov removed++; 471779750d2SKirill A. Shutemov goto next; 472779750d2SKirill A. Shutemov } 473779750d2SKirill A. Shutemov 474779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 475779750d2SKirill A. Shutemov next: 476779750d2SKirill A. Shutemov if (!--batch) 477779750d2SKirill A. Shutemov break; 478779750d2SKirill A. Shutemov } 479779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 480779750d2SKirill A. Shutemov 481253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 482253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 483253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 484253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 485253fd0f0SKirill A. Shutemov iput(inode); 486253fd0f0SKirill A. Shutemov } 487253fd0f0SKirill A. Shutemov 488779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 489779750d2SKirill A. Shutemov int ret; 490779750d2SKirill A. Shutemov 491779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 492779750d2SKirill A. Shutemov inode = &info->vfs_inode; 493779750d2SKirill A. Shutemov 494b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 495b3cd54b2SKirill A. Shutemov goto leave; 496779750d2SKirill A. Shutemov 497b3cd54b2SKirill A. Shutemov page = find_get_page(inode->i_mapping, 498779750d2SKirill A. Shutemov (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); 499779750d2SKirill A. Shutemov if (!page) 500779750d2SKirill A. Shutemov goto drop; 501779750d2SKirill A. Shutemov 502b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 503779750d2SKirill A. Shutemov if (!PageTransHuge(page)) { 504779750d2SKirill A. Shutemov put_page(page); 505779750d2SKirill A. Shutemov goto drop; 506779750d2SKirill A. Shutemov } 507779750d2SKirill A. Shutemov 508b3cd54b2SKirill A. Shutemov /* 509b3cd54b2SKirill A. Shutemov * Leave the inode on the list if we failed to lock 510b3cd54b2SKirill A. Shutemov * the page at this time. 511b3cd54b2SKirill A. Shutemov * 512b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 513b3cd54b2SKirill A. Shutemov * reclaim path. 514b3cd54b2SKirill A. Shutemov */ 515b3cd54b2SKirill A. Shutemov if (!trylock_page(page)) { 516b3cd54b2SKirill A. Shutemov put_page(page); 517b3cd54b2SKirill A. Shutemov goto leave; 518b3cd54b2SKirill A. Shutemov } 519b3cd54b2SKirill A. Shutemov 520779750d2SKirill A. Shutemov ret = split_huge_page(page); 521779750d2SKirill A. Shutemov unlock_page(page); 522779750d2SKirill A. Shutemov put_page(page); 523779750d2SKirill A. Shutemov 524b3cd54b2SKirill A. Shutemov /* If split failed leave the inode on the list */ 525b3cd54b2SKirill A. Shutemov if (ret) 526b3cd54b2SKirill A. Shutemov goto leave; 527779750d2SKirill A. Shutemov 528779750d2SKirill A. Shutemov split++; 529779750d2SKirill A. Shutemov drop: 530779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 531779750d2SKirill A. Shutemov removed++; 532b3cd54b2SKirill A. Shutemov leave: 533779750d2SKirill A. Shutemov iput(inode); 534779750d2SKirill A. Shutemov } 535779750d2SKirill A. Shutemov 536779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 537779750d2SKirill A. Shutemov list_splice_tail(&list, &sbinfo->shrinklist); 538779750d2SKirill A. Shutemov sbinfo->shrinklist_len -= removed; 539779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 540779750d2SKirill A. Shutemov 541779750d2SKirill A. Shutemov return split; 542779750d2SKirill A. Shutemov } 543779750d2SKirill A. Shutemov 544779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 545779750d2SKirill A. Shutemov struct shrink_control *sc) 546779750d2SKirill A. Shutemov { 547779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 548779750d2SKirill A. Shutemov 549779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 550779750d2SKirill A. Shutemov return SHRINK_STOP; 551779750d2SKirill A. Shutemov 552779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 553779750d2SKirill A. Shutemov } 554779750d2SKirill A. Shutemov 555779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 556779750d2SKirill A. Shutemov struct shrink_control *sc) 557779750d2SKirill A. Shutemov { 558779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 559779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 560779750d2SKirill A. Shutemov } 561e496cf3dSKirill A. Shutemov #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5625a6e75f8SKirill A. Shutemov 5635a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 5645a6e75f8SKirill A. Shutemov 565779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 566779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 567779750d2SKirill A. Shutemov { 568779750d2SKirill A. Shutemov return 0; 569779750d2SKirill A. Shutemov } 570e496cf3dSKirill A. Shutemov #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5715a6e75f8SKirill A. Shutemov 57289fdcd26SYang Shi static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) 57389fdcd26SYang Shi { 57489fdcd26SYang Shi if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 57589fdcd26SYang Shi (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) && 57689fdcd26SYang Shi shmem_huge != SHMEM_HUGE_DENY) 57789fdcd26SYang Shi return true; 57889fdcd26SYang Shi return false; 57989fdcd26SYang Shi } 58089fdcd26SYang Shi 5815a6e75f8SKirill A. Shutemov /* 58246f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 58346f65ec1SHugh Dickins */ 58446f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 58546f65ec1SHugh Dickins struct address_space *mapping, 586552446a4SMatthew Wilcox pgoff_t index, void *expected, gfp_t gfp) 58746f65ec1SHugh Dickins { 588552446a4SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page)); 589552446a4SMatthew Wilcox unsigned long i = 0; 590552446a4SMatthew Wilcox unsigned long nr = 1UL << compound_order(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 602552446a4SMatthew Wilcox do { 603552446a4SMatthew Wilcox void *entry; 604552446a4SMatthew Wilcox xas_lock_irq(&xas); 605552446a4SMatthew Wilcox entry = xas_find_conflict(&xas); 606552446a4SMatthew Wilcox if (entry != expected) 607552446a4SMatthew Wilcox xas_set_err(&xas, -EEXIST); 608552446a4SMatthew Wilcox xas_create_range(&xas); 609552446a4SMatthew Wilcox if (xas_error(&xas)) 610552446a4SMatthew Wilcox goto unlock; 611552446a4SMatthew Wilcox next: 612552446a4SMatthew Wilcox xas_store(&xas, page + i); 613552446a4SMatthew Wilcox if (++i < nr) { 614552446a4SMatthew Wilcox xas_next(&xas); 615552446a4SMatthew Wilcox goto next; 616552446a4SMatthew Wilcox } 617800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 618800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 61911fb9989SMel Gorman __inc_node_page_state(page, NR_SHMEM_THPS); 620552446a4SMatthew Wilcox } 621552446a4SMatthew Wilcox mapping->nrpages += nr; 62211fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); 62311fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); 624552446a4SMatthew Wilcox unlock: 625552446a4SMatthew Wilcox xas_unlock_irq(&xas); 626552446a4SMatthew Wilcox } while (xas_nomem(&xas, gfp)); 627552446a4SMatthew Wilcox 628552446a4SMatthew Wilcox if (xas_error(&xas)) { 62946f65ec1SHugh Dickins page->mapping = NULL; 630800d8c63SKirill A. Shutemov page_ref_sub(page, nr); 631552446a4SMatthew Wilcox return xas_error(&xas); 63246f65ec1SHugh Dickins } 633552446a4SMatthew Wilcox 634552446a4SMatthew Wilcox return 0; 63546f65ec1SHugh Dickins } 63646f65ec1SHugh Dickins 63746f65ec1SHugh Dickins /* 6386922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 6396922c0c7SHugh Dickins */ 6406922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 6416922c0c7SHugh Dickins { 6426922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 6436922c0c7SHugh Dickins int error; 6446922c0c7SHugh Dickins 645800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 646800d8c63SKirill A. Shutemov 647b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 64862f945b6SMatthew Wilcox error = shmem_replace_entry(mapping, page->index, page, radswap); 6496922c0c7SHugh Dickins page->mapping = NULL; 6506922c0c7SHugh Dickins mapping->nrpages--; 65111fb9989SMel Gorman __dec_node_page_state(page, NR_FILE_PAGES); 65211fb9989SMel Gorman __dec_node_page_state(page, NR_SHMEM); 653b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 65409cbfeafSKirill A. Shutemov put_page(page); 6556922c0c7SHugh Dickins BUG_ON(error); 6566922c0c7SHugh Dickins } 6576922c0c7SHugh Dickins 6586922c0c7SHugh Dickins /* 659c121d3bbSMatthew Wilcox * Remove swap entry from page cache, free the swap and its page cache. 6607a5d0fbbSHugh Dickins */ 6617a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 6627a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 6637a5d0fbbSHugh Dickins { 6646dbaf22cSJohannes Weiner void *old; 6657a5d0fbbSHugh Dickins 66655f3f7eaSMatthew Wilcox old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); 6676dbaf22cSJohannes Weiner if (old != radswap) 6686dbaf22cSJohannes Weiner return -ENOENT; 6697a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 6706dbaf22cSJohannes Weiner return 0; 6717a5d0fbbSHugh Dickins } 6727a5d0fbbSHugh Dickins 6737a5d0fbbSHugh Dickins /* 6746a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 67548131e03SVlastimil Babka * given offsets are swapped out. 6766a15a370SVlastimil Babka * 677b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 6786a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 6796a15a370SVlastimil Babka */ 68048131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 68148131e03SVlastimil Babka pgoff_t start, pgoff_t end) 6826a15a370SVlastimil Babka { 6837ae3424fSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start); 6846a15a370SVlastimil Babka struct page *page; 68548131e03SVlastimil Babka unsigned long swapped = 0; 6866a15a370SVlastimil Babka 6876a15a370SVlastimil Babka rcu_read_lock(); 6887ae3424fSMatthew Wilcox xas_for_each(&xas, page, end - 1) { 6897ae3424fSMatthew Wilcox if (xas_retry(&xas, page)) 6902cf938aaSMatthew Wilcox continue; 6913159f943SMatthew Wilcox if (xa_is_value(page)) 6926a15a370SVlastimil Babka swapped++; 6936a15a370SVlastimil Babka 6946a15a370SVlastimil Babka if (need_resched()) { 6957ae3424fSMatthew Wilcox xas_pause(&xas); 6966a15a370SVlastimil Babka cond_resched_rcu(); 6976a15a370SVlastimil Babka } 6986a15a370SVlastimil Babka } 6996a15a370SVlastimil Babka 7006a15a370SVlastimil Babka rcu_read_unlock(); 7016a15a370SVlastimil Babka 7026a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 7036a15a370SVlastimil Babka } 7046a15a370SVlastimil Babka 7056a15a370SVlastimil Babka /* 70648131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 70748131e03SVlastimil Babka * given vma is swapped out. 70848131e03SVlastimil Babka * 709b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 71048131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 71148131e03SVlastimil Babka */ 71248131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 71348131e03SVlastimil Babka { 71448131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 71548131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 71648131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 71748131e03SVlastimil Babka unsigned long swapped; 71848131e03SVlastimil Babka 71948131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 72048131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 72148131e03SVlastimil Babka 72248131e03SVlastimil Babka /* 72348131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 72448131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 72548131e03SVlastimil Babka * already track. 72648131e03SVlastimil Babka */ 72748131e03SVlastimil Babka if (!swapped) 72848131e03SVlastimil Babka return 0; 72948131e03SVlastimil Babka 73048131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 73148131e03SVlastimil Babka return swapped << PAGE_SHIFT; 73248131e03SVlastimil Babka 73348131e03SVlastimil Babka /* Here comes the more involved part */ 73448131e03SVlastimil Babka return shmem_partial_swap_usage(mapping, 73548131e03SVlastimil Babka linear_page_index(vma, vma->vm_start), 73648131e03SVlastimil Babka linear_page_index(vma, vma->vm_end)); 73748131e03SVlastimil Babka } 73848131e03SVlastimil Babka 73948131e03SVlastimil Babka /* 74024513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 74124513264SHugh Dickins */ 74224513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 74324513264SHugh Dickins { 74424513264SHugh Dickins struct pagevec pvec; 74524513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 74624513264SHugh Dickins pgoff_t index = 0; 74724513264SHugh Dickins 74886679820SMel Gorman pagevec_init(&pvec); 74924513264SHugh Dickins /* 75024513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 75124513264SHugh Dickins */ 75224513264SHugh Dickins while (!mapping_unevictable(mapping)) { 75324513264SHugh Dickins /* 75424513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 75524513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 75624513264SHugh Dickins */ 7570cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 75824513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 75924513264SHugh Dickins if (!pvec.nr) 76024513264SHugh Dickins break; 76124513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 7620cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 76364e3d12fSKuo-Hsin Yang check_move_unevictable_pages(&pvec); 76424513264SHugh Dickins pagevec_release(&pvec); 76524513264SHugh Dickins cond_resched(); 76624513264SHugh Dickins } 7677a5d0fbbSHugh Dickins } 7687a5d0fbbSHugh Dickins 7697a5d0fbbSHugh Dickins /* 7707f4446eeSMatthew Wilcox * Remove range of pages and swap entries from page cache, and free them. 7711635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 7727a5d0fbbSHugh Dickins */ 7731635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 7741635f6a7SHugh Dickins bool unfalloc) 7751da177e4SLinus Torvalds { 776285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 7771da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 77809cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 77909cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 78009cbfeafSKirill A. Shutemov unsigned int partial_start = lstart & (PAGE_SIZE - 1); 78109cbfeafSKirill A. Shutemov unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); 782bda97eabSHugh Dickins struct pagevec pvec; 7837a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 7847a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 785285b2c4fSHugh Dickins pgoff_t index; 786bda97eabSHugh Dickins int i; 7871da177e4SLinus Torvalds 78883e4fa9cSHugh Dickins if (lend == -1) 78983e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 790bda97eabSHugh Dickins 79186679820SMel Gorman pagevec_init(&pvec); 792bda97eabSHugh Dickins index = start; 79383e4fa9cSHugh Dickins while (index < end) { 7940cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 79583e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 7967a5d0fbbSHugh Dickins pvec.pages, indices); 7977a5d0fbbSHugh Dickins if (!pvec.nr) 7987a5d0fbbSHugh Dickins break; 799bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 800bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 801bda97eabSHugh Dickins 8027a5d0fbbSHugh Dickins index = indices[i]; 80383e4fa9cSHugh Dickins if (index >= end) 804bda97eabSHugh Dickins break; 805bda97eabSHugh Dickins 8063159f943SMatthew Wilcox if (xa_is_value(page)) { 8071635f6a7SHugh Dickins if (unfalloc) 8081635f6a7SHugh Dickins continue; 8097a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 8107a5d0fbbSHugh Dickins index, page); 8117a5d0fbbSHugh Dickins continue; 8127a5d0fbbSHugh Dickins } 8137a5d0fbbSHugh Dickins 814800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); 815800d8c63SKirill A. Shutemov 816bda97eabSHugh Dickins if (!trylock_page(page)) 817bda97eabSHugh Dickins continue; 818800d8c63SKirill A. Shutemov 819800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 820800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 821800d8c63SKirill A. Shutemov clear_highpage(page); 822800d8c63SKirill A. Shutemov unlock_page(page); 823800d8c63SKirill A. Shutemov continue; 824800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 825800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 826800d8c63SKirill A. Shutemov /* 827800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 828800d8c63SKirill A. Shutemov * zero out the page 829800d8c63SKirill A. Shutemov */ 830800d8c63SKirill A. Shutemov clear_highpage(page); 831800d8c63SKirill A. Shutemov unlock_page(page); 832800d8c63SKirill A. Shutemov continue; 833800d8c63SKirill A. Shutemov } 834800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 835800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 836800d8c63SKirill A. Shutemov } 837800d8c63SKirill A. Shutemov 8381635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 839800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 840800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 841309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 842bda97eabSHugh Dickins truncate_inode_page(mapping, page); 8437a5d0fbbSHugh Dickins } 8441635f6a7SHugh Dickins } 845bda97eabSHugh Dickins unlock_page(page); 846bda97eabSHugh Dickins } 8470cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 84824513264SHugh Dickins pagevec_release(&pvec); 849bda97eabSHugh Dickins cond_resched(); 850bda97eabSHugh Dickins index++; 851bda97eabSHugh Dickins } 852bda97eabSHugh Dickins 85383e4fa9cSHugh Dickins if (partial_start) { 854bda97eabSHugh Dickins struct page *page = NULL; 8559e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, start - 1, &page, SGP_READ); 856bda97eabSHugh Dickins if (page) { 85709cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 85883e4fa9cSHugh Dickins if (start > end) { 85983e4fa9cSHugh Dickins top = partial_end; 86083e4fa9cSHugh Dickins partial_end = 0; 86183e4fa9cSHugh Dickins } 86283e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 863bda97eabSHugh Dickins set_page_dirty(page); 864bda97eabSHugh Dickins unlock_page(page); 86509cbfeafSKirill A. Shutemov put_page(page); 866bda97eabSHugh Dickins } 867bda97eabSHugh Dickins } 86883e4fa9cSHugh Dickins if (partial_end) { 86983e4fa9cSHugh Dickins struct page *page = NULL; 8709e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, end, &page, SGP_READ); 87183e4fa9cSHugh Dickins if (page) { 87283e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 87383e4fa9cSHugh Dickins set_page_dirty(page); 87483e4fa9cSHugh Dickins unlock_page(page); 87509cbfeafSKirill A. Shutemov put_page(page); 87683e4fa9cSHugh Dickins } 87783e4fa9cSHugh Dickins } 87883e4fa9cSHugh Dickins if (start >= end) 87983e4fa9cSHugh Dickins return; 880bda97eabSHugh Dickins 881bda97eabSHugh Dickins index = start; 882b1a36650SHugh Dickins while (index < end) { 883bda97eabSHugh Dickins cond_resched(); 8840cd6144aSJohannes Weiner 8850cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 88683e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 8877a5d0fbbSHugh Dickins pvec.pages, indices); 8887a5d0fbbSHugh Dickins if (!pvec.nr) { 889b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 890b1a36650SHugh Dickins if (index == start || end != -1) 891bda97eabSHugh Dickins break; 892b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 893bda97eabSHugh Dickins index = start; 894bda97eabSHugh Dickins continue; 895bda97eabSHugh Dickins } 896bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 897bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 898bda97eabSHugh Dickins 8997a5d0fbbSHugh Dickins index = indices[i]; 90083e4fa9cSHugh Dickins if (index >= end) 901bda97eabSHugh Dickins break; 902bda97eabSHugh Dickins 9033159f943SMatthew Wilcox if (xa_is_value(page)) { 9041635f6a7SHugh Dickins if (unfalloc) 9051635f6a7SHugh Dickins continue; 906b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 907b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 908b1a36650SHugh Dickins index--; 909b1a36650SHugh Dickins break; 910b1a36650SHugh Dickins } 911b1a36650SHugh Dickins nr_swaps_freed++; 9127a5d0fbbSHugh Dickins continue; 9137a5d0fbbSHugh Dickins } 9147a5d0fbbSHugh Dickins 915bda97eabSHugh Dickins lock_page(page); 916800d8c63SKirill A. Shutemov 917800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 918800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 919800d8c63SKirill A. Shutemov clear_highpage(page); 920800d8c63SKirill A. Shutemov unlock_page(page); 921800d8c63SKirill A. Shutemov /* 922800d8c63SKirill A. Shutemov * Partial thp truncate due 'start' in middle 923800d8c63SKirill A. Shutemov * of THP: don't need to look on these pages 924800d8c63SKirill A. Shutemov * again on !pvec.nr restart. 925800d8c63SKirill A. Shutemov */ 926800d8c63SKirill A. Shutemov if (index != round_down(end, HPAGE_PMD_NR)) 927800d8c63SKirill A. Shutemov start++; 928800d8c63SKirill A. Shutemov continue; 929800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 930800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 931800d8c63SKirill A. Shutemov /* 932800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 933800d8c63SKirill A. Shutemov * zero out the page 934800d8c63SKirill A. Shutemov */ 935800d8c63SKirill A. Shutemov clear_highpage(page); 936800d8c63SKirill A. Shutemov unlock_page(page); 937800d8c63SKirill A. Shutemov continue; 938800d8c63SKirill A. Shutemov } 939800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 940800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 941800d8c63SKirill A. Shutemov } 942800d8c63SKirill A. Shutemov 9431635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 944800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 945800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 946309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 947bda97eabSHugh Dickins truncate_inode_page(mapping, page); 948b1a36650SHugh Dickins } else { 949b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 950b1a36650SHugh Dickins unlock_page(page); 951b1a36650SHugh Dickins index--; 952b1a36650SHugh Dickins break; 9537a5d0fbbSHugh Dickins } 9541635f6a7SHugh Dickins } 955bda97eabSHugh Dickins unlock_page(page); 956bda97eabSHugh Dickins } 9570cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 95824513264SHugh Dickins pagevec_release(&pvec); 959bda97eabSHugh Dickins index++; 960bda97eabSHugh Dickins } 96194c1e62dSHugh Dickins 9624595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 9637a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 9641da177e4SLinus Torvalds shmem_recalc_inode(inode); 9654595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 9661635f6a7SHugh Dickins } 9671da177e4SLinus Torvalds 9681635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 9691635f6a7SHugh Dickins { 9701635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 971078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 9721da177e4SLinus Torvalds } 97394c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 9741da177e4SLinus Torvalds 975a528d35eSDavid Howells static int shmem_getattr(const struct path *path, struct kstat *stat, 976a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 97744a30220SYu Zhao { 978a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 97944a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 98089fdcd26SYang Shi struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb); 98144a30220SYu Zhao 982d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 9834595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 98444a30220SYu Zhao shmem_recalc_inode(inode); 9854595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 986d0424c42SHugh Dickins } 98744a30220SYu Zhao generic_fillattr(inode, stat); 98889fdcd26SYang Shi 98989fdcd26SYang Shi if (is_huge_enabled(sb_info)) 99089fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 99189fdcd26SYang Shi 99244a30220SYu Zhao return 0; 99344a30220SYu Zhao } 99444a30220SYu Zhao 99594c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 9961da177e4SLinus Torvalds { 99775c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 99840e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 999779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10001da177e4SLinus Torvalds int error; 10011da177e4SLinus Torvalds 100231051c85SJan Kara error = setattr_prepare(dentry, attr); 1003db78b877SChristoph Hellwig if (error) 1004db78b877SChristoph Hellwig return error; 1005db78b877SChristoph Hellwig 100694c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 100794c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 100894c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 10093889e6e7Snpiggin@suse.de 101040e041a2SDavid Herrmann /* protected by i_mutex */ 101140e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 101240e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 101340e041a2SDavid Herrmann return -EPERM; 101440e041a2SDavid Herrmann 101594c1e62dSHugh Dickins if (newsize != oldsize) { 101677142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 101777142517SKonstantin Khlebnikov oldsize, newsize); 101877142517SKonstantin Khlebnikov if (error) 101977142517SKonstantin Khlebnikov return error; 102094c1e62dSHugh Dickins i_size_write(inode, newsize); 1021078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 102294c1e62dSHugh Dickins } 1023afa2db2fSJosef Bacik if (newsize <= oldsize) { 102494c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1025d0424c42SHugh Dickins if (oldsize > holebegin) 1026d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1027d0424c42SHugh Dickins holebegin, 0, 1); 1028d0424c42SHugh Dickins if (info->alloced) 1029d0424c42SHugh Dickins shmem_truncate_range(inode, 1030d0424c42SHugh Dickins newsize, (loff_t)-1); 103194c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1032d0424c42SHugh Dickins if (oldsize > holebegin) 1033d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1034d0424c42SHugh Dickins holebegin, 0, 1); 1035779750d2SKirill A. Shutemov 1036779750d2SKirill A. Shutemov /* 1037779750d2SKirill A. Shutemov * Part of the huge page can be beyond i_size: subject 1038779750d2SKirill A. Shutemov * to shrink under memory pressure. 1039779750d2SKirill A. Shutemov */ 1040779750d2SKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 1041779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1042d041353dSCong Wang /* 1043d041353dSCong Wang * _careful to defend against unlocked access to 1044d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1045d041353dSCong Wang */ 1046d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1047779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1048779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1049779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1050779750d2SKirill A. Shutemov } 1051779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1052779750d2SKirill A. Shutemov } 105394c1e62dSHugh Dickins } 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10566a1a90adSChristoph Hellwig setattr_copy(inode, attr); 1057db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1058feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 10591da177e4SLinus Torvalds return error; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10621f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 10631da177e4SLinus Torvalds { 10641da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1065779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10661da177e4SLinus Torvalds 10673889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 10681da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 10691da177e4SLinus Torvalds inode->i_size = 0; 10703889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1071779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1072779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1073779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1074779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1075779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1076779750d2SKirill A. Shutemov } 1077779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1078779750d2SKirill A. Shutemov } 10791da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 1080cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 10811da177e4SLinus Torvalds list_del_init(&info->swaplist); 1082cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 10831da177e4SLinus Torvalds } 10843ed47db3SAl Viro } 1085b09e0fa4SEric Paris 108638f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 10870f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 10885b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1089dbd5768fSJan Kara clear_inode(inode); 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 1092e21a2955SMatthew Wilcox static unsigned long find_swap_entry(struct xarray *xa, void *item) 1093478922e2SMatthew Wilcox { 1094e21a2955SMatthew Wilcox XA_STATE(xas, xa, 0); 1095478922e2SMatthew Wilcox unsigned int checked = 0; 1096e21a2955SMatthew Wilcox void *entry; 1097478922e2SMatthew Wilcox 1098478922e2SMatthew Wilcox rcu_read_lock(); 1099e21a2955SMatthew Wilcox xas_for_each(&xas, entry, ULONG_MAX) { 1100e21a2955SMatthew Wilcox if (xas_retry(&xas, entry)) 11015b9c98f3SMike Kravetz continue; 1102e21a2955SMatthew Wilcox if (entry == item) 1103478922e2SMatthew Wilcox break; 1104478922e2SMatthew Wilcox checked++; 1105e21a2955SMatthew Wilcox if ((checked % XA_CHECK_SCHED) != 0) 1106478922e2SMatthew Wilcox continue; 1107e21a2955SMatthew Wilcox xas_pause(&xas); 1108478922e2SMatthew Wilcox cond_resched_rcu(); 1109478922e2SMatthew Wilcox } 1110478922e2SMatthew Wilcox rcu_read_unlock(); 1111e21a2955SMatthew Wilcox 1112e21a2955SMatthew Wilcox return entry ? xas.xa_index : -1; 1113478922e2SMatthew Wilcox } 1114478922e2SMatthew Wilcox 111546f65ec1SHugh Dickins /* 111646f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 111746f65ec1SHugh Dickins */ 111841ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 1119bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 11201da177e4SLinus Torvalds { 1121285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 112246f65ec1SHugh Dickins void *radswap; 112341ffe5d5SHugh Dickins pgoff_t index; 1124bde05d1cSHugh Dickins gfp_t gfp; 1125bde05d1cSHugh Dickins int error = 0; 11261da177e4SLinus Torvalds 112746f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 1128b93b0163SMatthew Wilcox index = find_swap_entry(&mapping->i_pages, radswap); 112946f65ec1SHugh Dickins if (index == -1) 113000501b53SJohannes Weiner return -EAGAIN; /* tell shmem_unuse we found nothing */ 11312e0e26c7SHugh Dickins 11321b1b32f2SHugh Dickins /* 11331b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 11341f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 11351b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 1136285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 11371b1b32f2SHugh Dickins */ 11381b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 11392e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 11402e0e26c7SHugh Dickins 1141bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 1142bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 1143bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1144bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 1145bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1146bde05d1cSHugh Dickins /* 1147bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 11480142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 11490142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 11507f4446eeSMatthew Wilcox * complete without emptying the page cache, our page lock 11510142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 11520142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 11537f4446eeSMatthew Wilcox * trylock_page(), removing swap from page cache whatever. 11540142ef6cSHugh Dickins * 11550142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 11560142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 11570142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 11580142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 11590142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 11600142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 11610142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 11620142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 1163bde05d1cSHugh Dickins */ 1164bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 1165bde05d1cSHugh Dickins error = -ENOENT; 1166bde05d1cSHugh Dickins } 1167bde05d1cSHugh Dickins 1168d13d1443SKAMEZAWA Hiroyuki /* 1169778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 1170778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 1171778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 1172d13d1443SKAMEZAWA Hiroyuki */ 1173bde05d1cSHugh Dickins if (!error) 1174bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 1175552446a4SMatthew Wilcox radswap, gfp); 117648f170fbSHugh Dickins if (error != -ENOMEM) { 117746f65ec1SHugh Dickins /* 117846f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 117946f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 118046f65ec1SHugh Dickins */ 1181bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 1182bde05d1cSHugh Dickins set_page_dirty(*pagep); 118346f65ec1SHugh Dickins if (!error) { 11844595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1185285b2c4fSHugh Dickins info->swapped--; 11864595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 118741ffe5d5SHugh Dickins swap_free(swap); 118846f65ec1SHugh Dickins } 11891da177e4SLinus Torvalds } 11902e0e26c7SHugh Dickins return error; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds /* 119446f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 11951da177e4SLinus Torvalds */ 119641ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 11971da177e4SLinus Torvalds { 119841ffe5d5SHugh Dickins struct list_head *this, *next; 11991da177e4SLinus Torvalds struct shmem_inode_info *info; 120000501b53SJohannes Weiner struct mem_cgroup *memcg; 1201bde05d1cSHugh Dickins int error = 0; 1202bde05d1cSHugh Dickins 1203bde05d1cSHugh Dickins /* 1204bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 12050142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 1206bde05d1cSHugh Dickins */ 12070142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 1208bde05d1cSHugh Dickins goto out; 1209778dd893SHugh Dickins 1210778dd893SHugh Dickins /* 1211778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 1212778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 1213778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 1214778dd893SHugh Dickins */ 12152cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL, 12162cf85583STejun Heo &memcg, false); 1217778dd893SHugh Dickins if (error) 1218778dd893SHugh Dickins goto out; 12197f4446eeSMatthew Wilcox /* No memory allocation: swap entry occupies the slot for the page */ 122000501b53SJohannes Weiner error = -EAGAIN; 12211da177e4SLinus Torvalds 1222cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 122341ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 122441ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 1225285b2c4fSHugh Dickins if (info->swapped) 122600501b53SJohannes Weiner error = shmem_unuse_inode(info, swap, &page); 12276922c0c7SHugh Dickins else 12286922c0c7SHugh Dickins list_del_init(&info->swaplist); 1229cb5f7b9aSHugh Dickins cond_resched(); 123000501b53SJohannes Weiner if (error != -EAGAIN) 1231778dd893SHugh Dickins break; 123200501b53SJohannes Weiner /* found nothing in this: move on to search the next */ 12331da177e4SLinus Torvalds } 1234cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1235778dd893SHugh Dickins 123600501b53SJohannes Weiner if (error) { 123700501b53SJohannes Weiner if (error != -ENOMEM) 123800501b53SJohannes Weiner error = 0; 1239f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 124000501b53SJohannes Weiner } else 1241f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 1242778dd893SHugh Dickins out: 1243aaa46865SHugh Dickins unlock_page(page); 124409cbfeafSKirill A. Shutemov put_page(page); 1245778dd893SHugh Dickins return error; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds /* 12491da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 12501da177e4SLinus Torvalds */ 12511da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 12521da177e4SLinus Torvalds { 12531da177e4SLinus Torvalds struct shmem_inode_info *info; 12541da177e4SLinus Torvalds struct address_space *mapping; 12551da177e4SLinus Torvalds struct inode *inode; 12566922c0c7SHugh Dickins swp_entry_t swap; 12576922c0c7SHugh Dickins pgoff_t index; 12581da177e4SLinus Torvalds 1259800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 12601da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 12611da177e4SLinus Torvalds mapping = page->mapping; 12621da177e4SLinus Torvalds index = page->index; 12631da177e4SLinus Torvalds inode = mapping->host; 12641da177e4SLinus Torvalds info = SHMEM_I(inode); 12651da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 12661da177e4SLinus Torvalds goto redirty; 1267d9fe526aSHugh Dickins if (!total_swap_pages) 12681da177e4SLinus Torvalds goto redirty; 12691da177e4SLinus Torvalds 1270d9fe526aSHugh Dickins /* 127197b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 127297b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 127397b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 127497b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 127597b713baSChristoph Hellwig * threads or sync. 1276d9fe526aSHugh Dickins */ 127748f170fbSHugh Dickins if (!wbc->for_reclaim) { 127848f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 127948f170fbSHugh Dickins goto redirty; 128048f170fbSHugh Dickins } 12811635f6a7SHugh Dickins 12821635f6a7SHugh Dickins /* 12831635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 12841635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 12851635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 12861aac1400SHugh Dickins * 12871aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 12881aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 12891aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 12901aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 12911aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 12921635f6a7SHugh Dickins */ 12931635f6a7SHugh Dickins if (!PageUptodate(page)) { 12941aac1400SHugh Dickins if (inode->i_private) { 12951aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 12961aac1400SHugh Dickins spin_lock(&inode->i_lock); 12971aac1400SHugh Dickins shmem_falloc = inode->i_private; 12981aac1400SHugh Dickins if (shmem_falloc && 12998e205f77SHugh Dickins !shmem_falloc->waitq && 13001aac1400SHugh Dickins index >= shmem_falloc->start && 13011aac1400SHugh Dickins index < shmem_falloc->next) 13021aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 13031aac1400SHugh Dickins else 13041aac1400SHugh Dickins shmem_falloc = NULL; 13051aac1400SHugh Dickins spin_unlock(&inode->i_lock); 13061aac1400SHugh Dickins if (shmem_falloc) 13071aac1400SHugh Dickins goto redirty; 13081aac1400SHugh Dickins } 13091635f6a7SHugh Dickins clear_highpage(page); 13101635f6a7SHugh Dickins flush_dcache_page(page); 13111635f6a7SHugh Dickins SetPageUptodate(page); 13121635f6a7SHugh Dickins } 13131635f6a7SHugh Dickins 131438d8b4e6SHuang Ying swap = get_swap_page(page); 131548f170fbSHugh Dickins if (!swap.val) 131648f170fbSHugh Dickins goto redirty; 1317d9fe526aSHugh Dickins 1318b1dea800SHugh Dickins /* 1319b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 13206922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 13216922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1322b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 13236922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 13246922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1325b1dea800SHugh Dickins */ 1326b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 132705bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 132805bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 1329b1dea800SHugh Dickins 133048f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 13314595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1332267a4c76SHugh Dickins shmem_recalc_inode(inode); 1333267a4c76SHugh Dickins info->swapped++; 13344595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1335267a4c76SHugh Dickins 1336aaa46865SHugh Dickins swap_shmem_alloc(swap); 13376922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 13386922c0c7SHugh Dickins 13396922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1340d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 13419fab5619SHugh Dickins swap_writepage(page, wbc); 13421da177e4SLinus Torvalds return 0; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13456922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 134675f6d6d2SMinchan Kim put_swap_page(page, swap); 13471da177e4SLinus Torvalds redirty: 13481da177e4SLinus Torvalds set_page_dirty(page); 1349d9fe526aSHugh Dickins if (wbc->for_reclaim) 1350d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1351d9fe526aSHugh Dickins unlock_page(page); 1352d9fe526aSHugh Dickins return 0; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 135575edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 135671fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1357680d794bSakpm@linux-foundation.org { 1358680d794bSakpm@linux-foundation.org char buffer[64]; 1359680d794bSakpm@linux-foundation.org 136071fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1361095f1fc4SLee Schermerhorn return; /* show nothing */ 1362095f1fc4SLee Schermerhorn 1363a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1364095f1fc4SLee Schermerhorn 1365095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1366680d794bSakpm@linux-foundation.org } 136771fe804bSLee Schermerhorn 136871fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 136971fe804bSLee Schermerhorn { 137071fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 137171fe804bSLee Schermerhorn if (sbinfo->mpol) { 137271fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 137371fe804bSLee Schermerhorn mpol = sbinfo->mpol; 137471fe804bSLee Schermerhorn mpol_get(mpol); 137571fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 137671fe804bSLee Schermerhorn } 137771fe804bSLee Schermerhorn return mpol; 137871fe804bSLee Schermerhorn } 137975edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 138075edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 138175edd345SHugh Dickins { 138275edd345SHugh Dickins } 138375edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 138475edd345SHugh Dickins { 138575edd345SHugh Dickins return NULL; 138675edd345SHugh Dickins } 138775edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 138875edd345SHugh Dickins #ifndef CONFIG_NUMA 138975edd345SHugh Dickins #define vm_policy vm_private_data 139075edd345SHugh Dickins #endif 1391680d794bSakpm@linux-foundation.org 1392800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1393800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1394800d8c63SKirill A. Shutemov { 1395800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 13962c4541e2SKirill A. Shutemov vma_init(vma, NULL); 1397800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1398800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1399800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1400800d8c63SKirill A. Shutemov } 1401800d8c63SKirill A. Shutemov 1402800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1403800d8c63SKirill A. Shutemov { 1404800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1405800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1406800d8c63SKirill A. Shutemov } 1407800d8c63SKirill A. Shutemov 140841ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 140941ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 14101da177e4SLinus Torvalds { 14111da177e4SLinus Torvalds struct vm_area_struct pvma; 141218a2f371SMel Gorman struct page *page; 1413e9e9b7ecSMinchan Kim struct vm_fault vmf; 14141da177e4SLinus Torvalds 1415800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1416e9e9b7ecSMinchan Kim vmf.vma = &pvma; 1417e9e9b7ecSMinchan Kim vmf.address = 0; 1418e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1419800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 142018a2f371SMel Gorman 1421800d8c63SKirill A. Shutemov return page; 1422800d8c63SKirill A. Shutemov } 142318a2f371SMel Gorman 1424800d8c63SKirill A. Shutemov static struct page *shmem_alloc_hugepage(gfp_t gfp, 1425800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1426800d8c63SKirill A. Shutemov { 1427800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 14287b8d046fSMatthew Wilcox struct address_space *mapping = info->vfs_inode.i_mapping; 14297b8d046fSMatthew Wilcox pgoff_t hindex; 1430800d8c63SKirill A. Shutemov struct page *page; 1431800d8c63SKirill A. Shutemov 1432e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1433800d8c63SKirill A. Shutemov return NULL; 1434800d8c63SKirill A. Shutemov 14354620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 14367b8d046fSMatthew Wilcox if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, 14377b8d046fSMatthew Wilcox XA_PRESENT)) 1438800d8c63SKirill A. Shutemov return NULL; 1439800d8c63SKirill A. Shutemov 1440800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1441800d8c63SKirill A. Shutemov page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, 1442356ff8a9SDavid Rientjes HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); 1443800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1444800d8c63SKirill A. Shutemov if (page) 1445800d8c63SKirill A. Shutemov prep_transhuge_page(page); 144618a2f371SMel Gorman return page; 144718a2f371SMel Gorman } 144818a2f371SMel Gorman 144918a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 145018a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 145118a2f371SMel Gorman { 145218a2f371SMel Gorman struct vm_area_struct pvma; 145318a2f371SMel Gorman struct page *page; 145418a2f371SMel Gorman 1455800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1456800d8c63SKirill A. Shutemov page = alloc_page_vma(gfp, &pvma, 0); 1457800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 145818a2f371SMel Gorman 1459800d8c63SKirill A. Shutemov return page; 1460800d8c63SKirill A. Shutemov } 1461800d8c63SKirill A. Shutemov 1462800d8c63SKirill A. Shutemov static struct page *shmem_alloc_and_acct_page(gfp_t gfp, 14630f079694SMike Rapoport struct inode *inode, 1464800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1465800d8c63SKirill A. Shutemov { 14660f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 1467800d8c63SKirill A. Shutemov struct page *page; 1468800d8c63SKirill A. Shutemov int nr; 1469800d8c63SKirill A. Shutemov int err = -ENOSPC; 1470800d8c63SKirill A. Shutemov 1471e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1472800d8c63SKirill A. Shutemov huge = false; 1473800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1474800d8c63SKirill A. Shutemov 14750f079694SMike Rapoport if (!shmem_inode_acct_block(inode, nr)) 1476800d8c63SKirill A. Shutemov goto failed; 1477800d8c63SKirill A. Shutemov 1478800d8c63SKirill A. Shutemov if (huge) 1479800d8c63SKirill A. Shutemov page = shmem_alloc_hugepage(gfp, info, index); 1480800d8c63SKirill A. Shutemov else 1481800d8c63SKirill A. Shutemov page = shmem_alloc_page(gfp, info, index); 148275edd345SHugh Dickins if (page) { 148375edd345SHugh Dickins __SetPageLocked(page); 148475edd345SHugh Dickins __SetPageSwapBacked(page); 1485800d8c63SKirill A. Shutemov return page; 148675edd345SHugh Dickins } 148718a2f371SMel Gorman 1488800d8c63SKirill A. Shutemov err = -ENOMEM; 14890f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1490800d8c63SKirill A. Shutemov failed: 1491800d8c63SKirill A. Shutemov return ERR_PTR(err); 14921da177e4SLinus Torvalds } 149371fe804bSLee Schermerhorn 14941da177e4SLinus Torvalds /* 1495bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1496bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 1497bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1498bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1499bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1500bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1501bde05d1cSHugh Dickins * 1502bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1503bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1504bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1505bde05d1cSHugh Dickins */ 1506bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 1507bde05d1cSHugh Dickins { 1508bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 1509bde05d1cSHugh Dickins } 1510bde05d1cSHugh Dickins 1511bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 1512bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1513bde05d1cSHugh Dickins { 1514bde05d1cSHugh Dickins struct page *oldpage, *newpage; 1515bde05d1cSHugh Dickins struct address_space *swap_mapping; 1516c1cb20d4SYu Zhao swp_entry_t entry; 1517bde05d1cSHugh Dickins pgoff_t swap_index; 1518bde05d1cSHugh Dickins int error; 1519bde05d1cSHugh Dickins 1520bde05d1cSHugh Dickins oldpage = *pagep; 1521c1cb20d4SYu Zhao entry.val = page_private(oldpage); 1522c1cb20d4SYu Zhao swap_index = swp_offset(entry); 1523bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1524bde05d1cSHugh Dickins 1525bde05d1cSHugh Dickins /* 1526bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1527bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1528bde05d1cSHugh Dickins */ 1529bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1530bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1531bde05d1cSHugh Dickins if (!newpage) 1532bde05d1cSHugh Dickins return -ENOMEM; 1533bde05d1cSHugh Dickins 153409cbfeafSKirill A. Shutemov get_page(newpage); 1535bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 15360142ef6cSHugh Dickins flush_dcache_page(newpage); 1537bde05d1cSHugh Dickins 15389956edf3SHugh Dickins __SetPageLocked(newpage); 15399956edf3SHugh Dickins __SetPageSwapBacked(newpage); 1540bde05d1cSHugh Dickins SetPageUptodate(newpage); 1541c1cb20d4SYu Zhao set_page_private(newpage, entry.val); 1542bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1543bde05d1cSHugh Dickins 1544bde05d1cSHugh Dickins /* 1545bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1546bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1547bde05d1cSHugh Dickins */ 1548b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 154962f945b6SMatthew Wilcox error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage); 15500142ef6cSHugh Dickins if (!error) { 155111fb9989SMel Gorman __inc_node_page_state(newpage, NR_FILE_PAGES); 155211fb9989SMel Gorman __dec_node_page_state(oldpage, NR_FILE_PAGES); 15530142ef6cSHugh Dickins } 1554b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1555bde05d1cSHugh Dickins 15560142ef6cSHugh Dickins if (unlikely(error)) { 15570142ef6cSHugh Dickins /* 15580142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 15590142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 15600142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 15610142ef6cSHugh Dickins */ 15620142ef6cSHugh Dickins oldpage = newpage; 15630142ef6cSHugh Dickins } else { 15646a93ca8fSJohannes Weiner mem_cgroup_migrate(oldpage, newpage); 1565bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 15660142ef6cSHugh Dickins *pagep = newpage; 15670142ef6cSHugh Dickins } 1568bde05d1cSHugh Dickins 1569bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1570bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1571bde05d1cSHugh Dickins 1572bde05d1cSHugh Dickins unlock_page(oldpage); 157309cbfeafSKirill A. Shutemov put_page(oldpage); 157409cbfeafSKirill A. Shutemov put_page(oldpage); 15750142ef6cSHugh Dickins return error; 1576bde05d1cSHugh Dickins } 1577bde05d1cSHugh Dickins 1578bde05d1cSHugh Dickins /* 157968da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 15801da177e4SLinus Torvalds * 15811da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 15821da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 15839e18eb29SAndres Lagar-Cavilla * entry since a page cannot live in both the swap and page cache. 15849e18eb29SAndres Lagar-Cavilla * 15859e18eb29SAndres Lagar-Cavilla * fault_mm and fault_type are only supplied by shmem_fault: 15869e18eb29SAndres Lagar-Cavilla * otherwise they are NULL. 15871da177e4SLinus Torvalds */ 158841ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 15899e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, gfp_t gfp, 15902b740303SSouptick Joarder struct vm_area_struct *vma, struct vm_fault *vmf, 15912b740303SSouptick Joarder vm_fault_t *fault_type) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 159423f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 15951da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 15969e18eb29SAndres Lagar-Cavilla struct mm_struct *charge_mm; 159700501b53SJohannes Weiner struct mem_cgroup *memcg; 159827ab7006SHugh Dickins struct page *page; 15991da177e4SLinus Torvalds swp_entry_t swap; 1600657e3038SKirill A. Shutemov enum sgp_type sgp_huge = sgp; 1601800d8c63SKirill A. Shutemov pgoff_t hindex = index; 16021da177e4SLinus Torvalds int error; 160354af6042SHugh Dickins int once = 0; 16041635f6a7SHugh Dickins int alloced = 0; 16051da177e4SLinus Torvalds 160609cbfeafSKirill A. Shutemov if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 16071da177e4SLinus Torvalds return -EFBIG; 1608657e3038SKirill A. Shutemov if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) 1609657e3038SKirill A. Shutemov sgp = SGP_CACHE; 16101da177e4SLinus Torvalds repeat: 161154af6042SHugh Dickins swap.val = 0; 16120cd6144aSJohannes Weiner page = find_lock_entry(mapping, index); 16133159f943SMatthew Wilcox if (xa_is_value(page)) { 161454af6042SHugh Dickins swap = radix_to_swp_entry(page); 161554af6042SHugh Dickins page = NULL; 161654af6042SHugh Dickins } 161754af6042SHugh Dickins 161875edd345SHugh Dickins if (sgp <= SGP_CACHE && 161909cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 162054af6042SHugh Dickins error = -EINVAL; 1621267a4c76SHugh Dickins goto unlock; 162254af6042SHugh Dickins } 162354af6042SHugh Dickins 162466d2f4d2SHugh Dickins if (page && sgp == SGP_WRITE) 162566d2f4d2SHugh Dickins mark_page_accessed(page); 162666d2f4d2SHugh Dickins 16271635f6a7SHugh Dickins /* fallocated page? */ 16281635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 16291635f6a7SHugh Dickins if (sgp != SGP_READ) 16301635f6a7SHugh Dickins goto clear; 16311635f6a7SHugh Dickins unlock_page(page); 163209cbfeafSKirill A. Shutemov put_page(page); 16331635f6a7SHugh Dickins page = NULL; 16341635f6a7SHugh Dickins } 163554af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 163654af6042SHugh Dickins *pagep = page; 163754af6042SHugh Dickins return 0; 163827ab7006SHugh Dickins } 163927ab7006SHugh Dickins 1640b409f9fcSHugh Dickins /* 164154af6042SHugh Dickins * Fast cache lookup did not find it: 164254af6042SHugh Dickins * bring it back from swap or allocate. 1643b409f9fcSHugh Dickins */ 164454af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 1645cfda0526SMike Rapoport charge_mm = vma ? vma->vm_mm : current->mm; 164627ab7006SHugh Dickins 16471da177e4SLinus Torvalds if (swap.val) { 16481da177e4SLinus Torvalds /* Look it up and read it in.. */ 1649ec560175SHuang Ying page = lookup_swap_cache(swap, NULL, 0); 165027ab7006SHugh Dickins if (!page) { 16519e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 16529e18eb29SAndres Lagar-Cavilla if (fault_type) { 165368da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 16549e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 16552262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 16569e18eb29SAndres Lagar-Cavilla } 16579e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 165841ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 165927ab7006SHugh Dickins if (!page) { 16601da177e4SLinus Torvalds error = -ENOMEM; 166154af6042SHugh Dickins goto failed; 1662285b2c4fSHugh Dickins } 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 166654af6042SHugh Dickins lock_page(page); 16670142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1668d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1669bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1670d1899228SHugh Dickins goto unlock; 1671bde05d1cSHugh Dickins } 167227ab7006SHugh Dickins if (!PageUptodate(page)) { 16731da177e4SLinus Torvalds error = -EIO; 167454af6042SHugh Dickins goto failed; 167554af6042SHugh Dickins } 167654af6042SHugh Dickins wait_on_page_writeback(page); 167754af6042SHugh Dickins 1678bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1679bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1680bde05d1cSHugh Dickins if (error) 168154af6042SHugh Dickins goto failed; 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 16842cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, 1685f627c2f5SKirill A. Shutemov false); 1686d1899228SHugh Dickins if (!error) { 168754af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1688552446a4SMatthew Wilcox swp_to_radix_entry(swap), gfp); 1689215c02bcSHugh Dickins /* 1690215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1691215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1692215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1693215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1694215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1695215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1696215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 169793aa7d95SVladimir Davydov * the rest. 1698215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1699215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1700215c02bcSHugh Dickins */ 170100501b53SJohannes Weiner if (error) { 1702f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1703215c02bcSHugh Dickins delete_from_swap_cache(page); 1704d1899228SHugh Dickins } 170500501b53SJohannes Weiner } 170654af6042SHugh Dickins if (error) 170754af6042SHugh Dickins goto failed; 170854af6042SHugh Dickins 1709f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 171000501b53SJohannes Weiner 17114595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 171254af6042SHugh Dickins info->swapped--; 171354af6042SHugh Dickins shmem_recalc_inode(inode); 17144595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 171527ab7006SHugh Dickins 171666d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 171766d2f4d2SHugh Dickins mark_page_accessed(page); 171866d2f4d2SHugh Dickins 171927ab7006SHugh Dickins delete_from_swap_cache(page); 172027ab7006SHugh Dickins set_page_dirty(page); 172127ab7006SHugh Dickins swap_free(swap); 172227ab7006SHugh Dickins 172354af6042SHugh Dickins } else { 1724cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1725cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1726cfda0526SMike Rapoport return 0; 1727cfda0526SMike Rapoport } 1728cfda0526SMike Rapoport 1729800d8c63SKirill A. Shutemov /* shmem_symlink() */ 1730800d8c63SKirill A. Shutemov if (mapping->a_ops != &shmem_aops) 1731800d8c63SKirill A. Shutemov goto alloc_nohuge; 1732657e3038SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) 1733800d8c63SKirill A. Shutemov goto alloc_nohuge; 1734800d8c63SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 1735800d8c63SKirill A. Shutemov goto alloc_huge; 1736800d8c63SKirill A. Shutemov switch (sbinfo->huge) { 1737800d8c63SKirill A. Shutemov loff_t i_size; 1738800d8c63SKirill A. Shutemov pgoff_t off; 1739800d8c63SKirill A. Shutemov case SHMEM_HUGE_NEVER: 1740800d8c63SKirill A. Shutemov goto alloc_nohuge; 1741800d8c63SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 1742800d8c63SKirill A. Shutemov off = round_up(index, HPAGE_PMD_NR); 1743800d8c63SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 1744800d8c63SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 1745800d8c63SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 1746800d8c63SKirill A. Shutemov goto alloc_huge; 1747800d8c63SKirill A. Shutemov /* fallthrough */ 1748800d8c63SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 1749657e3038SKirill A. Shutemov if (sgp_huge == SGP_HUGE) 1750657e3038SKirill A. Shutemov goto alloc_huge; 1751657e3038SKirill A. Shutemov /* TODO: implement fadvise() hints */ 1752800d8c63SKirill A. Shutemov goto alloc_nohuge; 175359a16eadSHugh Dickins } 17541da177e4SLinus Torvalds 1755800d8c63SKirill A. Shutemov alloc_huge: 17560f079694SMike Rapoport page = shmem_alloc_and_acct_page(gfp, inode, index, true); 1757800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 17580f079694SMike Rapoport alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, 1759800d8c63SKirill A. Shutemov index, false); 176054af6042SHugh Dickins } 1761800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1762779750d2SKirill A. Shutemov int retry = 5; 1763800d8c63SKirill A. Shutemov error = PTR_ERR(page); 1764800d8c63SKirill A. Shutemov page = NULL; 1765779750d2SKirill A. Shutemov if (error != -ENOSPC) 1766779750d2SKirill A. Shutemov goto failed; 1767779750d2SKirill A. Shutemov /* 1768779750d2SKirill A. Shutemov * Try to reclaim some spece by splitting a huge page 1769779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1770779750d2SKirill A. Shutemov */ 1771779750d2SKirill A. Shutemov while (retry--) { 1772779750d2SKirill A. Shutemov int ret; 1773779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1774779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1775779750d2SKirill A. Shutemov break; 1776779750d2SKirill A. Shutemov if (ret) 1777779750d2SKirill A. Shutemov goto alloc_nohuge; 1778779750d2SKirill A. Shutemov } 1779800d8c63SKirill A. Shutemov goto failed; 1780800d8c63SKirill A. Shutemov } 1781800d8c63SKirill A. Shutemov 1782800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 1783800d8c63SKirill A. Shutemov hindex = round_down(index, HPAGE_PMD_NR); 1784800d8c63SKirill A. Shutemov else 1785800d8c63SKirill A. Shutemov hindex = index; 1786800d8c63SKirill A. Shutemov 178766d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1788eb39d618SHugh Dickins __SetPageReferenced(page); 178966d2f4d2SHugh Dickins 17902cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, 1791800d8c63SKirill A. Shutemov PageTransHuge(page)); 179254af6042SHugh Dickins if (error) 1793800d8c63SKirill A. Shutemov goto unacct; 1794800d8c63SKirill A. Shutemov error = shmem_add_to_page_cache(page, mapping, hindex, 1795552446a4SMatthew Wilcox NULL, gfp & GFP_RECLAIM_MASK); 1796b065b432SHugh Dickins if (error) { 1797800d8c63SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, 1798800d8c63SKirill A. Shutemov PageTransHuge(page)); 1799800d8c63SKirill A. Shutemov goto unacct; 1800b065b432SHugh Dickins } 1801800d8c63SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, 1802800d8c63SKirill A. Shutemov PageTransHuge(page)); 180354af6042SHugh Dickins lru_cache_add_anon(page); 180454af6042SHugh Dickins 18054595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1806800d8c63SKirill A. Shutemov info->alloced += 1 << compound_order(page); 1807800d8c63SKirill A. Shutemov inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); 180854af6042SHugh Dickins shmem_recalc_inode(inode); 18094595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18101635f6a7SHugh Dickins alloced = true; 181154af6042SHugh Dickins 1812779750d2SKirill A. Shutemov if (PageTransHuge(page) && 1813779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1814779750d2SKirill A. Shutemov hindex + HPAGE_PMD_NR - 1) { 1815779750d2SKirill A. Shutemov /* 1816779750d2SKirill A. Shutemov * Part of the huge page is beyond i_size: subject 1817779750d2SKirill A. Shutemov * to shrink under memory pressure. 1818779750d2SKirill A. Shutemov */ 1819779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1820d041353dSCong Wang /* 1821d041353dSCong Wang * _careful to defend against unlocked access to 1822d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1823d041353dSCong Wang */ 1824d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1825779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1826779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1827779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1828779750d2SKirill A. Shutemov } 1829779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1830779750d2SKirill A. Shutemov } 1831779750d2SKirill A. Shutemov 1832ec9516fbSHugh Dickins /* 18331635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 18341635f6a7SHugh Dickins */ 18351635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 18361635f6a7SHugh Dickins sgp = SGP_WRITE; 18371635f6a7SHugh Dickins clear: 18381635f6a7SHugh Dickins /* 18391635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 18401635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 18411635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1842ec9516fbSHugh Dickins */ 1843800d8c63SKirill A. Shutemov if (sgp != SGP_WRITE && !PageUptodate(page)) { 1844800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 1845800d8c63SKirill A. Shutemov int i; 1846800d8c63SKirill A. Shutemov 1847800d8c63SKirill A. Shutemov for (i = 0; i < (1 << compound_order(head)); i++) { 1848800d8c63SKirill A. Shutemov clear_highpage(head + i); 1849800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 1850800d8c63SKirill A. Shutemov } 1851800d8c63SKirill A. Shutemov SetPageUptodate(head); 1852ec9516fbSHugh Dickins } 18531da177e4SLinus Torvalds } 1854bde05d1cSHugh Dickins 185554af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 185675edd345SHugh Dickins if (sgp <= SGP_CACHE && 185709cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1858267a4c76SHugh Dickins if (alloced) { 1859267a4c76SHugh Dickins ClearPageDirty(page); 1860267a4c76SHugh Dickins delete_from_page_cache(page); 18614595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1862267a4c76SHugh Dickins shmem_recalc_inode(inode); 18634595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1864267a4c76SHugh Dickins } 186554af6042SHugh Dickins error = -EINVAL; 1866267a4c76SHugh Dickins goto unlock; 1867ff36b801SShaohua Li } 1868800d8c63SKirill A. Shutemov *pagep = page + index - hindex; 186954af6042SHugh Dickins return 0; 1870d00806b1SNick Piggin 1871d0217ac0SNick Piggin /* 187254af6042SHugh Dickins * Error recovery. 18731da177e4SLinus Torvalds */ 187454af6042SHugh Dickins unacct: 18750f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1 << compound_order(page)); 1876800d8c63SKirill A. Shutemov 1877800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 1878800d8c63SKirill A. Shutemov unlock_page(page); 1879800d8c63SKirill A. Shutemov put_page(page); 1880800d8c63SKirill A. Shutemov goto alloc_nohuge; 1881800d8c63SKirill A. Shutemov } 188254af6042SHugh Dickins failed: 1883267a4c76SHugh Dickins if (swap.val && !shmem_confirm_swap(mapping, index, swap)) 188454af6042SHugh Dickins error = -EEXIST; 1885d1899228SHugh Dickins unlock: 188627ab7006SHugh Dickins if (page) { 188754af6042SHugh Dickins unlock_page(page); 188809cbfeafSKirill A. Shutemov put_page(page); 188954af6042SHugh Dickins } 189054af6042SHugh Dickins if (error == -ENOSPC && !once++) { 18914595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 189254af6042SHugh Dickins shmem_recalc_inode(inode); 18934595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18941da177e4SLinus Torvalds goto repeat; 1895d8dc74f2SAdrian Bunk } 18967f4446eeSMatthew Wilcox if (error == -EEXIST) 189754af6042SHugh Dickins goto repeat; 189854af6042SHugh Dickins return error; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 190110d20bd2SLinus Torvalds /* 190210d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 190310d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 190410d20bd2SLinus Torvalds * target. 190510d20bd2SLinus Torvalds */ 1906ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 190710d20bd2SLinus Torvalds { 190810d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 19092055da97SIngo Molnar list_del_init(&wait->entry); 191010d20bd2SLinus Torvalds return ret; 191110d20bd2SLinus Torvalds } 191210d20bd2SLinus Torvalds 191320acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 19141da177e4SLinus Torvalds { 191511bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1916496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 19179e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 1918657e3038SKirill A. Shutemov enum sgp_type sgp; 191920acce67SSouptick Joarder int err; 192020acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 19211da177e4SLinus Torvalds 1922f00cdc6dSHugh Dickins /* 1923f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1924f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1925f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 19268e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 19278e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 19288e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 19298e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 19308e205f77SHugh Dickins * 19318e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 19328e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 19338e205f77SHugh Dickins * we just need to make racing faults a rare case. 19348e205f77SHugh Dickins * 19358e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 19368e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 19378e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 1938f00cdc6dSHugh Dickins */ 1939f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 1940f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 1941f00cdc6dSHugh Dickins 1942f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1943f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 19448e205f77SHugh Dickins if (shmem_falloc && 19458e205f77SHugh Dickins shmem_falloc->waitq && 19468e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 19478e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 19488e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 194910d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 19508e205f77SHugh Dickins 19518e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 1952f00cdc6dSHugh Dickins if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && 1953f00cdc6dSHugh Dickins !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 19548e205f77SHugh Dickins /* It's polite to up mmap_sem if we can */ 1955f00cdc6dSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 19568e205f77SHugh Dickins ret = VM_FAULT_RETRY; 1957f00cdc6dSHugh Dickins } 19588e205f77SHugh Dickins 19598e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 19608e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 19618e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 19628e205f77SHugh Dickins spin_unlock(&inode->i_lock); 19638e205f77SHugh Dickins schedule(); 19648e205f77SHugh Dickins 19658e205f77SHugh Dickins /* 19668e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 19678e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 19688e205f77SHugh Dickins * is usually invalid by the time we reach here, but 19698e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 19708e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 19718e205f77SHugh Dickins */ 19728e205f77SHugh Dickins spin_lock(&inode->i_lock); 19738e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 19748e205f77SHugh Dickins spin_unlock(&inode->i_lock); 19758e205f77SHugh Dickins return ret; 1976f00cdc6dSHugh Dickins } 19778e205f77SHugh Dickins spin_unlock(&inode->i_lock); 1978f00cdc6dSHugh Dickins } 1979f00cdc6dSHugh Dickins 1980657e3038SKirill A. Shutemov sgp = SGP_CACHE; 198118600332SMichal Hocko 198218600332SMichal Hocko if ((vma->vm_flags & VM_NOHUGEPAGE) || 198318600332SMichal Hocko test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) 1984657e3038SKirill A. Shutemov sgp = SGP_NOHUGE; 198518600332SMichal Hocko else if (vma->vm_flags & VM_HUGEPAGE) 198618600332SMichal Hocko sgp = SGP_HUGE; 1987657e3038SKirill A. Shutemov 198820acce67SSouptick Joarder err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, 1989cfda0526SMike Rapoport gfp, vma, vmf, &ret); 199020acce67SSouptick Joarder if (err) 199120acce67SSouptick Joarder return vmf_error(err); 199268da9f05SHugh Dickins return ret; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 1995c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 1996c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 1997c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 1998c01d5b30SHugh Dickins { 1999c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2000c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2001c01d5b30SHugh Dickins unsigned long addr; 2002c01d5b30SHugh Dickins unsigned long offset; 2003c01d5b30SHugh Dickins unsigned long inflated_len; 2004c01d5b30SHugh Dickins unsigned long inflated_addr; 2005c01d5b30SHugh Dickins unsigned long inflated_offset; 2006c01d5b30SHugh Dickins 2007c01d5b30SHugh Dickins if (len > TASK_SIZE) 2008c01d5b30SHugh Dickins return -ENOMEM; 2009c01d5b30SHugh Dickins 2010c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2011c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2012c01d5b30SHugh Dickins 2013e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 2014c01d5b30SHugh Dickins return addr; 2015c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2016c01d5b30SHugh Dickins return addr; 2017c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2018c01d5b30SHugh Dickins return addr; 2019c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2020c01d5b30SHugh Dickins return addr; 2021c01d5b30SHugh Dickins 2022c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2023c01d5b30SHugh Dickins return addr; 2024c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2025c01d5b30SHugh Dickins return addr; 2026c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2027c01d5b30SHugh Dickins return addr; 2028c01d5b30SHugh Dickins /* 2029c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2030c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 2031c01d5b30SHugh Dickins * But if caller specified an address hint, respect that as before. 2032c01d5b30SHugh Dickins */ 2033c01d5b30SHugh Dickins if (uaddr) 2034c01d5b30SHugh Dickins return addr; 2035c01d5b30SHugh Dickins 2036c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2037c01d5b30SHugh Dickins struct super_block *sb; 2038c01d5b30SHugh Dickins 2039c01d5b30SHugh Dickins if (file) { 2040c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2041c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2042c01d5b30SHugh Dickins } else { 2043c01d5b30SHugh Dickins /* 2044c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2045c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2046c01d5b30SHugh Dickins */ 2047c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2048c01d5b30SHugh Dickins return addr; 2049c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2050c01d5b30SHugh Dickins } 20513089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2052c01d5b30SHugh Dickins return addr; 2053c01d5b30SHugh Dickins } 2054c01d5b30SHugh Dickins 2055c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2056c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2057c01d5b30SHugh Dickins return addr; 2058c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2059c01d5b30SHugh Dickins return addr; 2060c01d5b30SHugh Dickins 2061c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2062c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2063c01d5b30SHugh Dickins return addr; 2064c01d5b30SHugh Dickins if (inflated_len < len) 2065c01d5b30SHugh Dickins return addr; 2066c01d5b30SHugh Dickins 2067c01d5b30SHugh Dickins inflated_addr = get_area(NULL, 0, inflated_len, 0, flags); 2068c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2069c01d5b30SHugh Dickins return addr; 2070c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2071c01d5b30SHugh Dickins return addr; 2072c01d5b30SHugh Dickins 2073c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2074c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2075c01d5b30SHugh Dickins if (inflated_offset > offset) 2076c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2077c01d5b30SHugh Dickins 2078c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2079c01d5b30SHugh Dickins return addr; 2080c01d5b30SHugh Dickins return inflated_addr; 2081c01d5b30SHugh Dickins } 2082c01d5b30SHugh Dickins 20831da177e4SLinus Torvalds #ifdef CONFIG_NUMA 208441ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 20851da177e4SLinus Torvalds { 2086496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 208741ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds 2090d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2091d8dc74f2SAdrian Bunk unsigned long addr) 20921da177e4SLinus Torvalds { 2093496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 209441ffe5d5SHugh Dickins pgoff_t index; 20951da177e4SLinus Torvalds 209641ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 209741ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds #endif 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 21021da177e4SLinus Torvalds { 2103496ad9aaSAl Viro struct inode *inode = file_inode(file); 21041da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 21051da177e4SLinus Torvalds int retval = -ENOMEM; 21061da177e4SLinus Torvalds 21074595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 21081da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 21091da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 21101da177e4SLinus Torvalds goto out_nomem; 21111da177e4SLinus Torvalds info->flags |= VM_LOCKED; 211289e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 21151da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 21161da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 211789e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds retval = 0; 212089e004eaSLee Schermerhorn 21211da177e4SLinus Torvalds out_nomem: 21224595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 21231da177e4SLinus Torvalds return retval; 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 21269b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 21271da177e4SLinus Torvalds { 21281da177e4SLinus Torvalds file_accessed(file); 21291da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2130e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 2131f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 2132f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 2133f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 2134f3f0e1d2SKirill A. Shutemov } 21351da177e4SLinus Torvalds return 0; 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds 2138454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 213909208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 21401da177e4SLinus Torvalds { 21411da177e4SLinus Torvalds struct inode *inode; 21421da177e4SLinus Torvalds struct shmem_inode_info *info; 21431da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 21441da177e4SLinus Torvalds 21455b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 21461da177e4SLinus Torvalds return NULL; 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds inode = new_inode(sb); 21491da177e4SLinus Torvalds if (inode) { 215085fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2151454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 21521da177e4SLinus Torvalds inode->i_blocks = 0; 2153078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 215446c9a946SArnd Bergmann inode->i_generation = prandom_u32(); 21551da177e4SLinus Torvalds info = SHMEM_I(inode); 21561da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 21571da177e4SLinus Torvalds spin_lock_init(&info->lock); 215840e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 21590b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2160779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 21611da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 216238f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 216372c04902SAl Viro cache_no_acl(inode); 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds switch (mode & S_IFMT) { 21661da177e4SLinus Torvalds default: 216739f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 21681da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 21691da177e4SLinus Torvalds break; 21701da177e4SLinus Torvalds case S_IFREG: 217114fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 21721da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 21731da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 217471fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 217571fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 21761da177e4SLinus Torvalds break; 21771da177e4SLinus Torvalds case S_IFDIR: 2178d8c76e6fSDave Hansen inc_nlink(inode); 21791da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 21801da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 21811da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 21821da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 21831da177e4SLinus Torvalds break; 21841da177e4SLinus Torvalds case S_IFLNK: 21851da177e4SLinus Torvalds /* 21861da177e4SLinus Torvalds * Must not load anything in the rbtree, 21871da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 21881da177e4SLinus Torvalds */ 218971fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 21901da177e4SLinus Torvalds break; 21911da177e4SLinus Torvalds } 2192b45d71fbSJoel Fernandes (Google) 2193b45d71fbSJoel Fernandes (Google) lockdep_annotate_inode_mutex_key(inode); 21945b04c689SPavel Emelyanov } else 21955b04c689SPavel Emelyanov shmem_free_inode(sb); 21961da177e4SLinus Torvalds return inode; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21990cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 22000cd6144aSJohannes Weiner { 2201f8005451SHugh Dickins return mapping->a_ops == &shmem_aops; 22020cd6144aSJohannes Weiner } 22030cd6144aSJohannes Weiner 22048d103963SMike Rapoport static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, 22054c27fe4cSMike Rapoport pmd_t *dst_pmd, 22064c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 22074c27fe4cSMike Rapoport unsigned long dst_addr, 22084c27fe4cSMike Rapoport unsigned long src_addr, 22098d103963SMike Rapoport bool zeropage, 22104c27fe4cSMike Rapoport struct page **pagep) 22114c27fe4cSMike Rapoport { 22124c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 22134c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 22144c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 22154c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 22164c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 22174c27fe4cSMike Rapoport struct mem_cgroup *memcg; 22184c27fe4cSMike Rapoport spinlock_t *ptl; 22194c27fe4cSMike Rapoport void *page_kaddr; 22204c27fe4cSMike Rapoport struct page *page; 22214c27fe4cSMike Rapoport pte_t _dst_pte, *dst_pte; 22224c27fe4cSMike Rapoport int ret; 2223e2a50c1fSAndrea Arcangeli pgoff_t offset, max_off; 22244c27fe4cSMike Rapoport 22254c27fe4cSMike Rapoport ret = -ENOMEM; 22260f079694SMike Rapoport if (!shmem_inode_acct_block(inode, 1)) 22274c27fe4cSMike Rapoport goto out; 22284c27fe4cSMike Rapoport 2229cb658a45SAndrea Arcangeli if (!*pagep) { 22304c27fe4cSMike Rapoport page = shmem_alloc_page(gfp, info, pgoff); 22314c27fe4cSMike Rapoport if (!page) 22320f079694SMike Rapoport goto out_unacct_blocks; 22334c27fe4cSMike Rapoport 22348d103963SMike Rapoport if (!zeropage) { /* mcopy_atomic */ 22354c27fe4cSMike Rapoport page_kaddr = kmap_atomic(page); 22368d103963SMike Rapoport ret = copy_from_user(page_kaddr, 22378d103963SMike Rapoport (const void __user *)src_addr, 22384c27fe4cSMike Rapoport PAGE_SIZE); 22394c27fe4cSMike Rapoport kunmap_atomic(page_kaddr); 22404c27fe4cSMike Rapoport 22414c27fe4cSMike Rapoport /* fallback to copy_from_user outside mmap_sem */ 22424c27fe4cSMike Rapoport if (unlikely(ret)) { 22434c27fe4cSMike Rapoport *pagep = page; 22440f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 22454c27fe4cSMike Rapoport /* don't free the page */ 22469e368259SAndrea Arcangeli return -ENOENT; 22474c27fe4cSMike Rapoport } 22488d103963SMike Rapoport } else { /* mfill_zeropage_atomic */ 22498d103963SMike Rapoport clear_highpage(page); 22508d103963SMike Rapoport } 22514c27fe4cSMike Rapoport } else { 22524c27fe4cSMike Rapoport page = *pagep; 22534c27fe4cSMike Rapoport *pagep = NULL; 22544c27fe4cSMike Rapoport } 22554c27fe4cSMike Rapoport 22569cc90c66SAndrea Arcangeli VM_BUG_ON(PageLocked(page) || PageSwapBacked(page)); 22579cc90c66SAndrea Arcangeli __SetPageLocked(page); 22589cc90c66SAndrea Arcangeli __SetPageSwapBacked(page); 2259a425d358SAndrea Arcangeli __SetPageUptodate(page); 22609cc90c66SAndrea Arcangeli 2261e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2262e2a50c1fSAndrea Arcangeli offset = linear_page_index(dst_vma, dst_addr); 2263e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 2264e2a50c1fSAndrea Arcangeli if (unlikely(offset >= max_off)) 2265e2a50c1fSAndrea Arcangeli goto out_release; 2266e2a50c1fSAndrea Arcangeli 22672cf85583STejun Heo ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false); 22684c27fe4cSMike Rapoport if (ret) 22694c27fe4cSMike Rapoport goto out_release; 22704c27fe4cSMike Rapoport 2271552446a4SMatthew Wilcox ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL, 2272552446a4SMatthew Wilcox gfp & GFP_RECLAIM_MASK); 22734c27fe4cSMike Rapoport if (ret) 22744c27fe4cSMike Rapoport goto out_release_uncharge; 22754c27fe4cSMike Rapoport 22764c27fe4cSMike Rapoport mem_cgroup_commit_charge(page, memcg, false, false); 22774c27fe4cSMike Rapoport 22784c27fe4cSMike Rapoport _dst_pte = mk_pte(page, dst_vma->vm_page_prot); 22794c27fe4cSMike Rapoport if (dst_vma->vm_flags & VM_WRITE) 22804c27fe4cSMike Rapoport _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); 2281dcf7fe9dSAndrea Arcangeli else { 2282dcf7fe9dSAndrea Arcangeli /* 2283dcf7fe9dSAndrea Arcangeli * We don't set the pte dirty if the vma has no 2284dcf7fe9dSAndrea Arcangeli * VM_WRITE permission, so mark the page dirty or it 2285dcf7fe9dSAndrea Arcangeli * could be freed from under us. We could do it 2286dcf7fe9dSAndrea Arcangeli * unconditionally before unlock_page(), but doing it 2287dcf7fe9dSAndrea Arcangeli * only if VM_WRITE is not set is faster. 2288dcf7fe9dSAndrea Arcangeli */ 2289dcf7fe9dSAndrea Arcangeli set_page_dirty(page); 2290dcf7fe9dSAndrea Arcangeli } 22914c27fe4cSMike Rapoport 22924c27fe4cSMike Rapoport dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 2293e2a50c1fSAndrea Arcangeli 2294e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2295e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 2296e2a50c1fSAndrea Arcangeli if (unlikely(offset >= max_off)) 2297e2a50c1fSAndrea Arcangeli goto out_release_uncharge_unlock; 2298e2a50c1fSAndrea Arcangeli 2299e2a50c1fSAndrea Arcangeli ret = -EEXIST; 23004c27fe4cSMike Rapoport if (!pte_none(*dst_pte)) 23014c27fe4cSMike Rapoport goto out_release_uncharge_unlock; 23024c27fe4cSMike Rapoport 23034c27fe4cSMike Rapoport lru_cache_add_anon(page); 23044c27fe4cSMike Rapoport 23054c27fe4cSMike Rapoport spin_lock(&info->lock); 23064c27fe4cSMike Rapoport info->alloced++; 23074c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 23084c27fe4cSMike Rapoport shmem_recalc_inode(inode); 23094c27fe4cSMike Rapoport spin_unlock(&info->lock); 23104c27fe4cSMike Rapoport 23114c27fe4cSMike Rapoport inc_mm_counter(dst_mm, mm_counter_file(page)); 23124c27fe4cSMike Rapoport page_add_file_rmap(page, false); 23134c27fe4cSMike Rapoport set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 23144c27fe4cSMike Rapoport 23154c27fe4cSMike Rapoport /* No need to invalidate - it was non-present before */ 23164c27fe4cSMike Rapoport update_mmu_cache(dst_vma, dst_addr, dst_pte); 23174c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 2318e2a50c1fSAndrea Arcangeli unlock_page(page); 23194c27fe4cSMike Rapoport ret = 0; 23204c27fe4cSMike Rapoport out: 23214c27fe4cSMike Rapoport return ret; 23224c27fe4cSMike Rapoport out_release_uncharge_unlock: 23234c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 2324dcf7fe9dSAndrea Arcangeli ClearPageDirty(page); 2325e2a50c1fSAndrea Arcangeli delete_from_page_cache(page); 23264c27fe4cSMike Rapoport out_release_uncharge: 23274c27fe4cSMike Rapoport mem_cgroup_cancel_charge(page, memcg, false); 23284c27fe4cSMike Rapoport out_release: 23299cc90c66SAndrea Arcangeli unlock_page(page); 23304c27fe4cSMike Rapoport put_page(page); 23314c27fe4cSMike Rapoport out_unacct_blocks: 23320f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 23334c27fe4cSMike Rapoport goto out; 23344c27fe4cSMike Rapoport } 23354c27fe4cSMike Rapoport 23368d103963SMike Rapoport int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, 23378d103963SMike Rapoport pmd_t *dst_pmd, 23388d103963SMike Rapoport struct vm_area_struct *dst_vma, 23398d103963SMike Rapoport unsigned long dst_addr, 23408d103963SMike Rapoport unsigned long src_addr, 23418d103963SMike Rapoport struct page **pagep) 23428d103963SMike Rapoport { 23438d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 23448d103963SMike Rapoport dst_addr, src_addr, false, pagep); 23458d103963SMike Rapoport } 23468d103963SMike Rapoport 23478d103963SMike Rapoport int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, 23488d103963SMike Rapoport pmd_t *dst_pmd, 23498d103963SMike Rapoport struct vm_area_struct *dst_vma, 23508d103963SMike Rapoport unsigned long dst_addr) 23518d103963SMike Rapoport { 23528d103963SMike Rapoport struct page *page = NULL; 23538d103963SMike Rapoport 23548d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 23558d103963SMike Rapoport dst_addr, 0, true, &page); 23568d103963SMike Rapoport } 23578d103963SMike Rapoport 23581da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 235992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 236069f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 23611da177e4SLinus Torvalds 23626d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 23636d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 23646d9d88d0SJarkko Sakkinen #else 23656d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 23666d9d88d0SJarkko Sakkinen #endif 23676d9d88d0SJarkko Sakkinen 23681da177e4SLinus Torvalds static int 2369800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 2370800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 2371800d15a5SNick Piggin struct page **pagep, void **fsdata) 23721da177e4SLinus Torvalds { 2373800d15a5SNick Piggin struct inode *inode = mapping->host; 237440e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 237509cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 237640e041a2SDavid Herrmann 237740e041a2SDavid Herrmann /* i_mutex is held by caller */ 23783f472cc9SSteven Rostedt (VMware) if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) { 237940e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) 238040e041a2SDavid Herrmann return -EPERM; 238140e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 238240e041a2SDavid Herrmann return -EPERM; 238340e041a2SDavid Herrmann } 238440e041a2SDavid Herrmann 23859e18eb29SAndres Lagar-Cavilla return shmem_getpage(inode, index, pagep, SGP_WRITE); 2386800d15a5SNick Piggin } 2387800d15a5SNick Piggin 2388800d15a5SNick Piggin static int 2389800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2390800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2391800d15a5SNick Piggin struct page *page, void *fsdata) 2392800d15a5SNick Piggin { 2393800d15a5SNick Piggin struct inode *inode = mapping->host; 2394800d15a5SNick Piggin 2395800d15a5SNick Piggin if (pos + copied > inode->i_size) 2396800d15a5SNick Piggin i_size_write(inode, pos + copied); 2397800d15a5SNick Piggin 2398ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2399800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2400800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2401800d8c63SKirill A. Shutemov int i; 2402800d8c63SKirill A. Shutemov 2403800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2404800d8c63SKirill A. Shutemov if (head + i == page) 2405800d8c63SKirill A. Shutemov continue; 2406800d8c63SKirill A. Shutemov clear_highpage(head + i); 2407800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2408800d8c63SKirill A. Shutemov } 2409800d8c63SKirill A. Shutemov } 241009cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 241109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2412ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 241309cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2414ec9516fbSHugh Dickins } 2415800d8c63SKirill A. Shutemov SetPageUptodate(head); 2416ec9516fbSHugh Dickins } 2417d3602444SHugh Dickins set_page_dirty(page); 24186746aff7SWu Fengguang unlock_page(page); 241909cbfeafSKirill A. Shutemov put_page(page); 2420d3602444SHugh Dickins 2421800d15a5SNick Piggin return copied; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 24242ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 24251da177e4SLinus Torvalds { 24266e58e79dSAl Viro struct file *file = iocb->ki_filp; 24276e58e79dSAl Viro struct inode *inode = file_inode(file); 24281da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 242941ffe5d5SHugh Dickins pgoff_t index; 243041ffe5d5SHugh Dickins unsigned long offset; 2431a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 2432f7c1d074SGeert Uytterhoeven int error = 0; 2433cb66a7a1SAl Viro ssize_t retval = 0; 24346e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2435a0ee5ec5SHugh Dickins 2436a0ee5ec5SHugh Dickins /* 2437a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 2438a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 2439a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 2440a0ee5ec5SHugh Dickins */ 2441777eda2cSAl Viro if (!iter_is_iovec(to)) 244275edd345SHugh Dickins sgp = SGP_CACHE; 24431da177e4SLinus Torvalds 244409cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 244509cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds for (;;) { 24481da177e4SLinus Torvalds struct page *page = NULL; 244941ffe5d5SHugh Dickins pgoff_t end_index; 245041ffe5d5SHugh Dickins unsigned long nr, ret; 24511da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 24521da177e4SLinus Torvalds 245309cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24541da177e4SLinus Torvalds if (index > end_index) 24551da177e4SLinus Torvalds break; 24561da177e4SLinus Torvalds if (index == end_index) { 245709cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24581da177e4SLinus Torvalds if (nr <= offset) 24591da177e4SLinus Torvalds break; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 24629e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, sgp); 24636e58e79dSAl Viro if (error) { 24646e58e79dSAl Viro if (error == -EINVAL) 24656e58e79dSAl Viro error = 0; 24661da177e4SLinus Torvalds break; 24671da177e4SLinus Torvalds } 246875edd345SHugh Dickins if (page) { 246975edd345SHugh Dickins if (sgp == SGP_CACHE) 247075edd345SHugh Dickins set_page_dirty(page); 2471d3602444SHugh Dickins unlock_page(page); 247275edd345SHugh Dickins } 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds /* 24751da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 24761b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 24771da177e4SLinus Torvalds */ 247809cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 24791da177e4SLinus Torvalds i_size = i_size_read(inode); 248009cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24811da177e4SLinus Torvalds if (index == end_index) { 248209cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24831da177e4SLinus Torvalds if (nr <= offset) { 24841da177e4SLinus Torvalds if (page) 248509cbfeafSKirill A. Shutemov put_page(page); 24861da177e4SLinus Torvalds break; 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds nr -= offset; 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds if (page) { 24921da177e4SLinus Torvalds /* 24931da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 24941da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 24951da177e4SLinus Torvalds * before reading the page on the kernel side. 24961da177e4SLinus Torvalds */ 24971da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 24981da177e4SLinus Torvalds flush_dcache_page(page); 24991da177e4SLinus Torvalds /* 25001da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 25011da177e4SLinus Torvalds */ 25021da177e4SLinus Torvalds if (!offset) 25031da177e4SLinus Torvalds mark_page_accessed(page); 2504b5810039SNick Piggin } else { 25051da177e4SLinus Torvalds page = ZERO_PAGE(0); 250609cbfeafSKirill A. Shutemov get_page(page); 2507b5810039SNick Piggin } 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds /* 25101da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 25111da177e4SLinus Torvalds * now we can copy it to user space... 25121da177e4SLinus Torvalds */ 25132ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 25146e58e79dSAl Viro retval += ret; 25151da177e4SLinus Torvalds offset += ret; 251609cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 251709cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 25181da177e4SLinus Torvalds 251909cbfeafSKirill A. Shutemov put_page(page); 25202ba5bbedSAl Viro if (!iov_iter_count(to)) 25211da177e4SLinus Torvalds break; 25226e58e79dSAl Viro if (ret < nr) { 25236e58e79dSAl Viro error = -EFAULT; 25246e58e79dSAl Viro break; 25256e58e79dSAl Viro } 25261da177e4SLinus Torvalds cond_resched(); 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 252909cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 25306e58e79dSAl Viro file_accessed(file); 25316e58e79dSAl Viro return retval ? retval : error; 25321da177e4SLinus Torvalds } 25331da177e4SLinus Torvalds 2534220f2ac9SHugh Dickins /* 25357f4446eeSMatthew Wilcox * llseek SEEK_DATA or SEEK_HOLE through the page cache. 2536220f2ac9SHugh Dickins */ 2537220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 2538965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 2539220f2ac9SHugh Dickins { 2540220f2ac9SHugh Dickins struct page *page; 2541220f2ac9SHugh Dickins struct pagevec pvec; 2542220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 2543220f2ac9SHugh Dickins bool done = false; 2544220f2ac9SHugh Dickins int i; 2545220f2ac9SHugh Dickins 254686679820SMel Gorman pagevec_init(&pvec); 2547220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 2548220f2ac9SHugh Dickins while (!done) { 25490cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 2550220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 2551220f2ac9SHugh Dickins if (!pvec.nr) { 2552965c8e59SAndrew Morton if (whence == SEEK_DATA) 2553220f2ac9SHugh Dickins index = end; 2554220f2ac9SHugh Dickins break; 2555220f2ac9SHugh Dickins } 2556220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 2557220f2ac9SHugh Dickins if (index < indices[i]) { 2558965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 2559220f2ac9SHugh Dickins done = true; 2560220f2ac9SHugh Dickins break; 2561220f2ac9SHugh Dickins } 2562220f2ac9SHugh Dickins index = indices[i]; 2563220f2ac9SHugh Dickins } 2564220f2ac9SHugh Dickins page = pvec.pages[i]; 25653159f943SMatthew Wilcox if (page && !xa_is_value(page)) { 2566220f2ac9SHugh Dickins if (!PageUptodate(page)) 2567220f2ac9SHugh Dickins page = NULL; 2568220f2ac9SHugh Dickins } 2569220f2ac9SHugh Dickins if (index >= end || 2570965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 2571965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 2572220f2ac9SHugh Dickins done = true; 2573220f2ac9SHugh Dickins break; 2574220f2ac9SHugh Dickins } 2575220f2ac9SHugh Dickins } 25760cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 2577220f2ac9SHugh Dickins pagevec_release(&pvec); 2578220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 2579220f2ac9SHugh Dickins cond_resched(); 2580220f2ac9SHugh Dickins } 2581220f2ac9SHugh Dickins return index; 2582220f2ac9SHugh Dickins } 2583220f2ac9SHugh Dickins 2584965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2585220f2ac9SHugh Dickins { 2586220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2587220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2588220f2ac9SHugh Dickins pgoff_t start, end; 2589220f2ac9SHugh Dickins loff_t new_offset; 2590220f2ac9SHugh Dickins 2591965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2592965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2593220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 25945955102cSAl Viro inode_lock(inode); 2595220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 2596220f2ac9SHugh Dickins 25971a413646SYufen Yu if (offset < 0 || offset >= inode->i_size) 2598220f2ac9SHugh Dickins offset = -ENXIO; 2599220f2ac9SHugh Dickins else { 260009cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 260109cbfeafSKirill A. Shutemov end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 2602965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 260309cbfeafSKirill A. Shutemov new_offset <<= PAGE_SHIFT; 2604220f2ac9SHugh Dickins if (new_offset > offset) { 2605220f2ac9SHugh Dickins if (new_offset < inode->i_size) 2606220f2ac9SHugh Dickins offset = new_offset; 2607965c8e59SAndrew Morton else if (whence == SEEK_DATA) 2608220f2ac9SHugh Dickins offset = -ENXIO; 2609220f2ac9SHugh Dickins else 2610220f2ac9SHugh Dickins offset = inode->i_size; 2611220f2ac9SHugh Dickins } 2612220f2ac9SHugh Dickins } 2613220f2ac9SHugh Dickins 2614387aae6fSHugh Dickins if (offset >= 0) 261546a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 26165955102cSAl Viro inode_unlock(inode); 2617220f2ac9SHugh Dickins return offset; 2618220f2ac9SHugh Dickins } 2619220f2ac9SHugh Dickins 262083e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 262183e4fa9cSHugh Dickins loff_t len) 262283e4fa9cSHugh Dickins { 2623496ad9aaSAl Viro struct inode *inode = file_inode(file); 2624e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 262540e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 26261aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2627e2d12e22SHugh Dickins pgoff_t start, index, end; 2628e2d12e22SHugh Dickins int error; 262983e4fa9cSHugh Dickins 263013ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 263113ace4d0SHugh Dickins return -EOPNOTSUPP; 263213ace4d0SHugh Dickins 26335955102cSAl Viro inode_lock(inode); 263483e4fa9cSHugh Dickins 263583e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 263683e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 263783e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 263883e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 26398e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 264083e4fa9cSHugh Dickins 264140e041a2SDavid Herrmann /* protected by i_mutex */ 264240e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) { 264340e041a2SDavid Herrmann error = -EPERM; 264440e041a2SDavid Herrmann goto out; 264540e041a2SDavid Herrmann } 264640e041a2SDavid Herrmann 26478e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2648f00cdc6dSHugh Dickins shmem_falloc.start = unmap_start >> PAGE_SHIFT; 2649f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2650f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2651f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2652f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2653f00cdc6dSHugh Dickins 265483e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 265583e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 265683e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 265783e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 265883e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 26598e205f77SHugh Dickins 26608e205f77SHugh Dickins spin_lock(&inode->i_lock); 26618e205f77SHugh Dickins inode->i_private = NULL; 26628e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 26632055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 26648e205f77SHugh Dickins spin_unlock(&inode->i_lock); 266583e4fa9cSHugh Dickins error = 0; 26668e205f77SHugh Dickins goto out; 266783e4fa9cSHugh Dickins } 266883e4fa9cSHugh Dickins 2669e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2670e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2671e2d12e22SHugh Dickins if (error) 2672e2d12e22SHugh Dickins goto out; 2673e2d12e22SHugh Dickins 267440e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 267540e041a2SDavid Herrmann error = -EPERM; 267640e041a2SDavid Herrmann goto out; 267740e041a2SDavid Herrmann } 267840e041a2SDavid Herrmann 267909cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 268009cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2681e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2682e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2683e2d12e22SHugh Dickins error = -ENOSPC; 2684e2d12e22SHugh Dickins goto out; 2685e2d12e22SHugh Dickins } 2686e2d12e22SHugh Dickins 26878e205f77SHugh Dickins shmem_falloc.waitq = NULL; 26881aac1400SHugh Dickins shmem_falloc.start = start; 26891aac1400SHugh Dickins shmem_falloc.next = start; 26901aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 26911aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 26921aac1400SHugh Dickins spin_lock(&inode->i_lock); 26931aac1400SHugh Dickins inode->i_private = &shmem_falloc; 26941aac1400SHugh Dickins spin_unlock(&inode->i_lock); 26951aac1400SHugh Dickins 2696e2d12e22SHugh Dickins for (index = start; index < end; index++) { 2697e2d12e22SHugh Dickins struct page *page; 2698e2d12e22SHugh Dickins 2699e2d12e22SHugh Dickins /* 2700e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2701e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2702e2d12e22SHugh Dickins */ 2703e2d12e22SHugh Dickins if (signal_pending(current)) 2704e2d12e22SHugh Dickins error = -EINTR; 27051aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 27061aac1400SHugh Dickins error = -ENOMEM; 2707e2d12e22SHugh Dickins else 27089e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_FALLOC); 2709e2d12e22SHugh Dickins if (error) { 27101635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 27117f556567SHugh Dickins if (index > start) { 27121635f6a7SHugh Dickins shmem_undo_range(inode, 271309cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2714b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 27157f556567SHugh Dickins } 27161aac1400SHugh Dickins goto undone; 2717e2d12e22SHugh Dickins } 2718e2d12e22SHugh Dickins 2719e2d12e22SHugh Dickins /* 27201aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 27211aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 27221aac1400SHugh Dickins */ 27231aac1400SHugh Dickins shmem_falloc.next++; 27241aac1400SHugh Dickins if (!PageUptodate(page)) 27251aac1400SHugh Dickins shmem_falloc.nr_falloced++; 27261aac1400SHugh Dickins 27271aac1400SHugh Dickins /* 27281635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 27291635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 27301635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2731e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2732e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2733e2d12e22SHugh Dickins */ 2734e2d12e22SHugh Dickins set_page_dirty(page); 2735e2d12e22SHugh Dickins unlock_page(page); 273609cbfeafSKirill A. Shutemov put_page(page); 2737e2d12e22SHugh Dickins cond_resched(); 2738e2d12e22SHugh Dickins } 2739e2d12e22SHugh Dickins 2740e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2741e2d12e22SHugh Dickins i_size_write(inode, offset + len); 2742078cd827SDeepa Dinamani inode->i_ctime = current_time(inode); 27431aac1400SHugh Dickins undone: 27441aac1400SHugh Dickins spin_lock(&inode->i_lock); 27451aac1400SHugh Dickins inode->i_private = NULL; 27461aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2747e2d12e22SHugh Dickins out: 27485955102cSAl Viro inode_unlock(inode); 274983e4fa9cSHugh Dickins return error; 275083e4fa9cSHugh Dickins } 275183e4fa9cSHugh Dickins 2752726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 27531da177e4SLinus Torvalds { 2754726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 27551da177e4SLinus Torvalds 27561da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 275709cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 27581da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 27590edd73b3SHugh Dickins if (sbinfo->max_blocks) { 27601da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 276141ffe5d5SHugh Dickins buf->f_bavail = 276241ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 276341ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 27640edd73b3SHugh Dickins } 27650edd73b3SHugh Dickins if (sbinfo->max_inodes) { 27661da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 27671da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 27701da177e4SLinus Torvalds return 0; 27711da177e4SLinus Torvalds } 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds /* 27741da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 27751da177e4SLinus Torvalds */ 27761da177e4SLinus Torvalds static int 27771a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 27781da177e4SLinus Torvalds { 27790b0a0806SHugh Dickins struct inode *inode; 27801da177e4SLinus Torvalds int error = -ENOSPC; 27811da177e4SLinus Torvalds 2782454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 27831da177e4SLinus Torvalds if (inode) { 2784feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2785feda821eSChristoph Hellwig if (error) 2786feda821eSChristoph Hellwig goto out_iput; 27872a7dba39SEric Paris error = security_inode_init_security(inode, dir, 27889d8f13baSMimi Zohar &dentry->d_name, 27896d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2790feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2791feda821eSChristoph Hellwig goto out_iput; 279237ec43cdSMimi Zohar 2793718deb6bSAl Viro error = 0; 27941da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2795078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 27961da177e4SLinus Torvalds d_instantiate(dentry, inode); 27971da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds return error; 2800feda821eSChristoph Hellwig out_iput: 2801feda821eSChristoph Hellwig iput(inode); 2802feda821eSChristoph Hellwig return error; 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds 280560545d0dSAl Viro static int 280660545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 280760545d0dSAl Viro { 280860545d0dSAl Viro struct inode *inode; 280960545d0dSAl Viro int error = -ENOSPC; 281060545d0dSAl Viro 281160545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 281260545d0dSAl Viro if (inode) { 281360545d0dSAl Viro error = security_inode_init_security(inode, dir, 281460545d0dSAl Viro NULL, 281560545d0dSAl Viro shmem_initxattrs, NULL); 2816feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2817feda821eSChristoph Hellwig goto out_iput; 2818feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2819feda821eSChristoph Hellwig if (error) 2820feda821eSChristoph Hellwig goto out_iput; 282160545d0dSAl Viro d_tmpfile(dentry, inode); 282260545d0dSAl Viro } 282360545d0dSAl Viro return error; 2824feda821eSChristoph Hellwig out_iput: 2825feda821eSChristoph Hellwig iput(inode); 2826feda821eSChristoph Hellwig return error; 282760545d0dSAl Viro } 282860545d0dSAl Viro 282918bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 28301da177e4SLinus Torvalds { 28311da177e4SLinus Torvalds int error; 28321da177e4SLinus Torvalds 28331da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 28341da177e4SLinus Torvalds return error; 2835d8c76e6fSDave Hansen inc_nlink(dir); 28361da177e4SLinus Torvalds return 0; 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 28394acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2840ebfc3b49SAl Viro bool excl) 28411da177e4SLinus Torvalds { 28421da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 28431da177e4SLinus Torvalds } 28441da177e4SLinus Torvalds 28451da177e4SLinus Torvalds /* 28461da177e4SLinus Torvalds * Link a file.. 28471da177e4SLinus Torvalds */ 28481da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 28491da177e4SLinus Torvalds { 285075c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 28515b04c689SPavel Emelyanov int ret; 28521da177e4SLinus Torvalds 28531da177e4SLinus Torvalds /* 28541da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 28551da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 28561da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 2857*1062af92SDarrick J. Wong * But if an O_TMPFILE file is linked into the tmpfs, the 2858*1062af92SDarrick J. Wong * first link must skip that, to get the accounting right. 28591da177e4SLinus Torvalds */ 2860*1062af92SDarrick J. Wong if (inode->i_nlink) { 28615b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 28625b04c689SPavel Emelyanov if (ret) 28635b04c689SPavel Emelyanov goto out; 2864*1062af92SDarrick J. Wong } 28651da177e4SLinus Torvalds 28661da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2867078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 2868d8c76e6fSDave Hansen inc_nlink(inode); 28697de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 28701da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 28711da177e4SLinus Torvalds d_instantiate(dentry, inode); 28725b04c689SPavel Emelyanov out: 28735b04c689SPavel Emelyanov return ret; 28741da177e4SLinus Torvalds } 28751da177e4SLinus Torvalds 28761da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 28771da177e4SLinus Torvalds { 287875c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 28791da177e4SLinus Torvalds 28805b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 28815b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 28821da177e4SLinus Torvalds 28831da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 2884078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 28859a53c3a7SDave Hansen drop_nlink(inode); 28861da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 28871da177e4SLinus Torvalds return 0; 28881da177e4SLinus Torvalds } 28891da177e4SLinus Torvalds 28901da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 28911da177e4SLinus Torvalds { 28921da177e4SLinus Torvalds if (!simple_empty(dentry)) 28931da177e4SLinus Torvalds return -ENOTEMPTY; 28941da177e4SLinus Torvalds 289575c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 28969a53c3a7SDave Hansen drop_nlink(dir); 28971da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds 290037456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 290137456771SMiklos Szeredi { 2902e36cb0b8SDavid Howells bool old_is_dir = d_is_dir(old_dentry); 2903e36cb0b8SDavid Howells bool new_is_dir = d_is_dir(new_dentry); 290437456771SMiklos Szeredi 290537456771SMiklos Szeredi if (old_dir != new_dir && old_is_dir != new_is_dir) { 290637456771SMiklos Szeredi if (old_is_dir) { 290737456771SMiklos Szeredi drop_nlink(old_dir); 290837456771SMiklos Szeredi inc_nlink(new_dir); 290937456771SMiklos Szeredi } else { 291037456771SMiklos Szeredi drop_nlink(new_dir); 291137456771SMiklos Szeredi inc_nlink(old_dir); 291237456771SMiklos Szeredi } 291337456771SMiklos Szeredi } 291437456771SMiklos Szeredi old_dir->i_ctime = old_dir->i_mtime = 291537456771SMiklos Szeredi new_dir->i_ctime = new_dir->i_mtime = 291675c3cfa8SDavid Howells d_inode(old_dentry)->i_ctime = 2917078cd827SDeepa Dinamani d_inode(new_dentry)->i_ctime = current_time(old_dir); 291837456771SMiklos Szeredi 291937456771SMiklos Szeredi return 0; 292037456771SMiklos Szeredi } 292137456771SMiklos Szeredi 292246fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) 292346fdb794SMiklos Szeredi { 292446fdb794SMiklos Szeredi struct dentry *whiteout; 292546fdb794SMiklos Szeredi int error; 292646fdb794SMiklos Szeredi 292746fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 292846fdb794SMiklos Szeredi if (!whiteout) 292946fdb794SMiklos Szeredi return -ENOMEM; 293046fdb794SMiklos Szeredi 293146fdb794SMiklos Szeredi error = shmem_mknod(old_dir, whiteout, 293246fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 293346fdb794SMiklos Szeredi dput(whiteout); 293446fdb794SMiklos Szeredi if (error) 293546fdb794SMiklos Szeredi return error; 293646fdb794SMiklos Szeredi 293746fdb794SMiklos Szeredi /* 293846fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 293946fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 294046fdb794SMiklos Szeredi * 294146fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 294246fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 294346fdb794SMiklos Szeredi */ 294446fdb794SMiklos Szeredi d_rehash(whiteout); 294546fdb794SMiklos Szeredi return 0; 294646fdb794SMiklos Szeredi } 294746fdb794SMiklos Szeredi 29481da177e4SLinus Torvalds /* 29491da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 29501da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 29511da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 29521da177e4SLinus Torvalds * gets overwritten. 29531da177e4SLinus Torvalds */ 29543b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 29551da177e4SLinus Torvalds { 295675c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 29571da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 29581da177e4SLinus Torvalds 295946fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 29603b69ff51SMiklos Szeredi return -EINVAL; 29613b69ff51SMiklos Szeredi 296237456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 296337456771SMiklos Szeredi return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); 296437456771SMiklos Szeredi 29651da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 29661da177e4SLinus Torvalds return -ENOTEMPTY; 29671da177e4SLinus Torvalds 296846fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 296946fdb794SMiklos Szeredi int error; 297046fdb794SMiklos Szeredi 297146fdb794SMiklos Szeredi error = shmem_whiteout(old_dir, old_dentry); 297246fdb794SMiklos Szeredi if (error) 297346fdb794SMiklos Szeredi return error; 297446fdb794SMiklos Szeredi } 297546fdb794SMiklos Szeredi 297675c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 29771da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 2978b928095bSMiklos Szeredi if (they_are_dirs) { 297975c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 29809a53c3a7SDave Hansen drop_nlink(old_dir); 2981b928095bSMiklos Szeredi } 29821da177e4SLinus Torvalds } else if (they_are_dirs) { 29839a53c3a7SDave Hansen drop_nlink(old_dir); 2984d8c76e6fSDave Hansen inc_nlink(new_dir); 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 29881da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 29891da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 29901da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 2991078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 29921da177e4SLinus Torvalds return 0; 29931da177e4SLinus Torvalds } 29941da177e4SLinus Torvalds 29951da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 29961da177e4SLinus Torvalds { 29971da177e4SLinus Torvalds int error; 29981da177e4SLinus Torvalds int len; 29991da177e4SLinus Torvalds struct inode *inode; 30009276aad6SHugh Dickins struct page *page; 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds len = strlen(symname) + 1; 300309cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 30041da177e4SLinus Torvalds return -ENAMETOOLONG; 30051da177e4SLinus Torvalds 30060825a6f9SJoe Perches inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, 30070825a6f9SJoe Perches VM_NORESERVE); 30081da177e4SLinus Torvalds if (!inode) 30091da177e4SLinus Torvalds return -ENOSPC; 30101da177e4SLinus Torvalds 30119d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 30126d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3013570bc1c2SStephen Smalley if (error) { 3014570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 3015570bc1c2SStephen Smalley iput(inode); 3016570bc1c2SStephen Smalley return error; 3017570bc1c2SStephen Smalley } 3018570bc1c2SStephen Smalley error = 0; 3019570bc1c2SStephen Smalley } 3020570bc1c2SStephen Smalley 30211da177e4SLinus Torvalds inode->i_size = len-1; 302269f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 30233ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 30243ed47db3SAl Viro if (!inode->i_link) { 302569f07ec9SHugh Dickins iput(inode); 302669f07ec9SHugh Dickins return -ENOMEM; 302769f07ec9SHugh Dickins } 302869f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 30291da177e4SLinus Torvalds } else { 3030e8ecde25SAl Viro inode_nohighmem(inode); 30319e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_WRITE); 30321da177e4SLinus Torvalds if (error) { 30331da177e4SLinus Torvalds iput(inode); 30341da177e4SLinus Torvalds return error; 30351da177e4SLinus Torvalds } 303614fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 30371da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 303821fc61c7SAl Viro memcpy(page_address(page), symname, len); 3039ec9516fbSHugh Dickins SetPageUptodate(page); 30401da177e4SLinus Torvalds set_page_dirty(page); 30416746aff7SWu Fengguang unlock_page(page); 304209cbfeafSKirill A. Shutemov put_page(page); 30431da177e4SLinus Torvalds } 30441da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3045078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 30461da177e4SLinus Torvalds d_instantiate(dentry, inode); 30471da177e4SLinus Torvalds dget(dentry); 30481da177e4SLinus Torvalds return 0; 30491da177e4SLinus Torvalds } 30501da177e4SLinus Torvalds 3051fceef393SAl Viro static void shmem_put_link(void *arg) 3052fceef393SAl Viro { 3053fceef393SAl Viro mark_page_accessed(arg); 3054fceef393SAl Viro put_page(arg); 3055fceef393SAl Viro } 3056fceef393SAl Viro 30576b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3058fceef393SAl Viro struct inode *inode, 3059fceef393SAl Viro struct delayed_call *done) 30601da177e4SLinus Torvalds { 30611da177e4SLinus Torvalds struct page *page = NULL; 30626b255391SAl Viro int error; 30636a6c9904SAl Viro if (!dentry) { 30646a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 30656a6c9904SAl Viro if (!page) 30666b255391SAl Viro return ERR_PTR(-ECHILD); 30676a6c9904SAl Viro if (!PageUptodate(page)) { 30686a6c9904SAl Viro put_page(page); 30696a6c9904SAl Viro return ERR_PTR(-ECHILD); 30706a6c9904SAl Viro } 30716a6c9904SAl Viro } else { 30729e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_READ); 3073680baacbSAl Viro if (error) 3074680baacbSAl Viro return ERR_PTR(error); 3075d3602444SHugh Dickins unlock_page(page); 30761da177e4SLinus Torvalds } 3077fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 307821fc61c7SAl Viro return page_address(page); 30791da177e4SLinus Torvalds } 30801da177e4SLinus Torvalds 3081b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3082b09e0fa4SEric Paris /* 3083b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3084b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3085b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3086b09e0fa4SEric Paris * filesystem level, though. 3087b09e0fa4SEric Paris */ 3088b09e0fa4SEric Paris 30896d9d88d0SJarkko Sakkinen /* 30906d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 30916d9d88d0SJarkko Sakkinen */ 30926d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 30936d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 30946d9d88d0SJarkko Sakkinen void *fs_info) 30956d9d88d0SJarkko Sakkinen { 30966d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 30976d9d88d0SJarkko Sakkinen const struct xattr *xattr; 309838f38657SAristeu Rozanski struct simple_xattr *new_xattr; 30996d9d88d0SJarkko Sakkinen size_t len; 31006d9d88d0SJarkko Sakkinen 31016d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 310238f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 31036d9d88d0SJarkko Sakkinen if (!new_xattr) 31046d9d88d0SJarkko Sakkinen return -ENOMEM; 31056d9d88d0SJarkko Sakkinen 31066d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 31076d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 31086d9d88d0SJarkko Sakkinen GFP_KERNEL); 31096d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 31106d9d88d0SJarkko Sakkinen kfree(new_xattr); 31116d9d88d0SJarkko Sakkinen return -ENOMEM; 31126d9d88d0SJarkko Sakkinen } 31136d9d88d0SJarkko Sakkinen 31146d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 31156d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 31166d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 31176d9d88d0SJarkko Sakkinen xattr->name, len); 31186d9d88d0SJarkko Sakkinen 311938f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 31206d9d88d0SJarkko Sakkinen } 31216d9d88d0SJarkko Sakkinen 31226d9d88d0SJarkko Sakkinen return 0; 31236d9d88d0SJarkko Sakkinen } 31246d9d88d0SJarkko Sakkinen 3125aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3126b296821aSAl Viro struct dentry *unused, struct inode *inode, 3127b296821aSAl Viro const char *name, void *buffer, size_t size) 3128aa7c5241SAndreas Gruenbacher { 3129b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3130aa7c5241SAndreas Gruenbacher 3131aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3132aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3133aa7c5241SAndreas Gruenbacher } 3134aa7c5241SAndreas Gruenbacher 3135aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 313659301226SAl Viro struct dentry *unused, struct inode *inode, 313759301226SAl Viro const char *name, const void *value, 313859301226SAl Viro size_t size, int flags) 3139aa7c5241SAndreas Gruenbacher { 314059301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3141aa7c5241SAndreas Gruenbacher 3142aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3143aa7c5241SAndreas Gruenbacher return simple_xattr_set(&info->xattrs, name, value, size, flags); 3144aa7c5241SAndreas Gruenbacher } 3145aa7c5241SAndreas Gruenbacher 3146aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3147aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3148aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3149aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3150aa7c5241SAndreas Gruenbacher }; 3151aa7c5241SAndreas Gruenbacher 3152aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3153aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3154aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3155aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3156aa7c5241SAndreas Gruenbacher }; 3157aa7c5241SAndreas Gruenbacher 3158b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3159b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3160feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3161feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3162b09e0fa4SEric Paris #endif 3163aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3164aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3165b09e0fa4SEric Paris NULL 3166b09e0fa4SEric Paris }; 3167b09e0fa4SEric Paris 3168b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3169b09e0fa4SEric Paris { 317075c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3171786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3172b09e0fa4SEric Paris } 3173b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3174b09e0fa4SEric Paris 317569f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 31766b255391SAl Viro .get_link = simple_get_link, 3177b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3178b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3179b09e0fa4SEric Paris #endif 31801da177e4SLinus Torvalds }; 31811da177e4SLinus Torvalds 318292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 31836b255391SAl Viro .get_link = shmem_get_link, 3184b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3185b09e0fa4SEric Paris .listxattr = shmem_listxattr, 318639f0247dSAndreas Gruenbacher #endif 3187b09e0fa4SEric Paris }; 318839f0247dSAndreas Gruenbacher 318991828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 319091828a40SDavid M. Grimes { 319191828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 319291828a40SDavid M. Grimes } 319391828a40SDavid M. Grimes 319491828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 319591828a40SDavid M. Grimes { 319691828a40SDavid M. Grimes __u32 *fh = vfh; 319791828a40SDavid M. Grimes __u64 inum = fh[2]; 319891828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 319991828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 320091828a40SDavid M. Grimes } 320191828a40SDavid M. Grimes 320212ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 320312ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 320412ba780dSAmir Goldstein { 320512ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 320612ba780dSAmir Goldstein 320712ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 320812ba780dSAmir Goldstein } 320912ba780dSAmir Goldstein 321012ba780dSAmir Goldstein 3211480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3212480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 321391828a40SDavid M. Grimes { 321491828a40SDavid M. Grimes struct inode *inode; 3215480b116cSChristoph Hellwig struct dentry *dentry = NULL; 321635c2a7f4SHugh Dickins u64 inum; 321791828a40SDavid M. Grimes 3218480b116cSChristoph Hellwig if (fh_len < 3) 3219480b116cSChristoph Hellwig return NULL; 3220480b116cSChristoph Hellwig 322135c2a7f4SHugh Dickins inum = fid->raw[2]; 322235c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 322335c2a7f4SHugh Dickins 3224480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3225480b116cSChristoph Hellwig shmem_match, fid->raw); 322691828a40SDavid M. Grimes if (inode) { 322712ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 322891828a40SDavid M. Grimes iput(inode); 322991828a40SDavid M. Grimes } 323091828a40SDavid M. Grimes 3231480b116cSChristoph Hellwig return dentry; 323291828a40SDavid M. Grimes } 323391828a40SDavid M. Grimes 3234b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3235b0b0382bSAl Viro struct inode *parent) 323691828a40SDavid M. Grimes { 32375fe0c237SAneesh Kumar K.V if (*len < 3) { 32385fe0c237SAneesh Kumar K.V *len = 3; 323994e07a75SNamjae Jeon return FILEID_INVALID; 32405fe0c237SAneesh Kumar K.V } 324191828a40SDavid M. Grimes 32421d3382cbSAl Viro if (inode_unhashed(inode)) { 324391828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 324491828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 324591828a40SDavid M. Grimes * time, we need a lock to ensure we only try 324691828a40SDavid M. Grimes * to do it once 324791828a40SDavid M. Grimes */ 324891828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 324991828a40SDavid M. Grimes spin_lock(&lock); 32501d3382cbSAl Viro if (inode_unhashed(inode)) 325191828a40SDavid M. Grimes __insert_inode_hash(inode, 325291828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 325391828a40SDavid M. Grimes spin_unlock(&lock); 325491828a40SDavid M. Grimes } 325591828a40SDavid M. Grimes 325691828a40SDavid M. Grimes fh[0] = inode->i_generation; 325791828a40SDavid M. Grimes fh[1] = inode->i_ino; 325891828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 325991828a40SDavid M. Grimes 326091828a40SDavid M. Grimes *len = 3; 326191828a40SDavid M. Grimes return 1; 326291828a40SDavid M. Grimes } 326391828a40SDavid M. Grimes 326439655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 326591828a40SDavid M. Grimes .get_parent = shmem_get_parent, 326691828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3267480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 326891828a40SDavid M. Grimes }; 326991828a40SDavid M. Grimes 3270680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 3271680d794bSakpm@linux-foundation.org bool remount) 32721da177e4SLinus Torvalds { 32731da177e4SLinus Torvalds char *this_char, *value, *rest; 327449cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 32758751e039SEric W. Biederman uid_t uid; 32768751e039SEric W. Biederman gid_t gid; 32771da177e4SLinus Torvalds 3278b00dc3adSHugh Dickins while (options != NULL) { 3279b00dc3adSHugh Dickins this_char = options; 3280b00dc3adSHugh Dickins for (;;) { 3281b00dc3adSHugh Dickins /* 3282b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3283b00dc3adSHugh Dickins * mount options form a comma-separated list, 3284b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3285b00dc3adSHugh Dickins */ 3286b00dc3adSHugh Dickins options = strchr(options, ','); 3287b00dc3adSHugh Dickins if (options == NULL) 3288b00dc3adSHugh Dickins break; 3289b00dc3adSHugh Dickins options++; 3290b00dc3adSHugh Dickins if (!isdigit(*options)) { 3291b00dc3adSHugh Dickins options[-1] = '\0'; 3292b00dc3adSHugh Dickins break; 3293b00dc3adSHugh Dickins } 3294b00dc3adSHugh Dickins } 32951da177e4SLinus Torvalds if (!*this_char) 32961da177e4SLinus Torvalds continue; 32971da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 32981da177e4SLinus Torvalds *value++ = 0; 32991da177e4SLinus Torvalds } else { 33001170532bSJoe Perches pr_err("tmpfs: No value for mount option '%s'\n", 33011da177e4SLinus Torvalds this_char); 330249cd0a5cSGreg Thelen goto error; 33031da177e4SLinus Torvalds } 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 33061da177e4SLinus Torvalds unsigned long long size; 33071da177e4SLinus Torvalds size = memparse(value,&rest); 33081da177e4SLinus Torvalds if (*rest == '%') { 33091da177e4SLinus Torvalds size <<= PAGE_SHIFT; 3310ca79b0c2SArun KS size *= totalram_pages(); 33111da177e4SLinus Torvalds do_div(size, 100); 33121da177e4SLinus Torvalds rest++; 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds if (*rest) 33151da177e4SLinus Torvalds goto bad_val; 3316680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 331709cbfeafSKirill A. Shutemov DIV_ROUND_UP(size, PAGE_SIZE); 33181da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 3319680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 33201da177e4SLinus Torvalds if (*rest) 33211da177e4SLinus Torvalds goto bad_val; 33221da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 3323680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 33241da177e4SLinus Torvalds if (*rest) 33251da177e4SLinus Torvalds goto bad_val; 33261da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 3327680d794bSakpm@linux-foundation.org if (remount) 33281da177e4SLinus Torvalds continue; 3329680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 33301da177e4SLinus Torvalds if (*rest) 33311da177e4SLinus Torvalds goto bad_val; 33321da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 3333680d794bSakpm@linux-foundation.org if (remount) 33341da177e4SLinus Torvalds continue; 33358751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 33361da177e4SLinus Torvalds if (*rest) 33371da177e4SLinus Torvalds goto bad_val; 33388751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 33398751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 33408751e039SEric W. Biederman goto bad_val; 33411da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 3342680d794bSakpm@linux-foundation.org if (remount) 33431da177e4SLinus Torvalds continue; 33448751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 33451da177e4SLinus Torvalds if (*rest) 33461da177e4SLinus Torvalds goto bad_val; 33478751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 33488751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 33498751e039SEric W. Biederman goto bad_val; 3350e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 33515a6e75f8SKirill A. Shutemov } else if (!strcmp(this_char, "huge")) { 33525a6e75f8SKirill A. Shutemov int huge; 33535a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(value); 33545a6e75f8SKirill A. Shutemov if (huge < 0) 33555a6e75f8SKirill A. Shutemov goto bad_val; 33565a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 33575a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER) 33585a6e75f8SKirill A. Shutemov goto bad_val; 33595a6e75f8SKirill A. Shutemov sbinfo->huge = huge; 33605a6e75f8SKirill A. Shutemov #endif 33615a6e75f8SKirill A. Shutemov #ifdef CONFIG_NUMA 33627339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 336349cd0a5cSGreg Thelen mpol_put(mpol); 336449cd0a5cSGreg Thelen mpol = NULL; 336549cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 33667339ff83SRobin Holt goto bad_val; 33675a6e75f8SKirill A. Shutemov #endif 33681da177e4SLinus Torvalds } else { 33691170532bSJoe Perches pr_err("tmpfs: Bad mount option %s\n", this_char); 337049cd0a5cSGreg Thelen goto error; 33711da177e4SLinus Torvalds } 33721da177e4SLinus Torvalds } 337349cd0a5cSGreg Thelen sbinfo->mpol = mpol; 33741da177e4SLinus Torvalds return 0; 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds bad_val: 33771170532bSJoe Perches pr_err("tmpfs: Bad value '%s' for mount option '%s'\n", 33781da177e4SLinus Torvalds value, this_char); 337949cd0a5cSGreg Thelen error: 338049cd0a5cSGreg Thelen mpol_put(mpol); 33811da177e4SLinus Torvalds return 1; 33821da177e4SLinus Torvalds 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds 33851da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 33861da177e4SLinus Torvalds { 33871da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3388680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 33890edd73b3SHugh Dickins unsigned long inodes; 33900edd73b3SHugh Dickins int error = -EINVAL; 33911da177e4SLinus Torvalds 33925f00110fSGreg Thelen config.mpol = NULL; 3393680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 33940edd73b3SHugh Dickins return error; 33950edd73b3SHugh Dickins 33960edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 33970edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 33987e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 33990edd73b3SHugh Dickins goto out; 3400680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 34010edd73b3SHugh Dickins goto out; 34020edd73b3SHugh Dickins /* 340354af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 34040edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 34050edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 34060edd73b3SHugh Dickins */ 3407680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 34080edd73b3SHugh Dickins goto out; 3409680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 34100edd73b3SHugh Dickins goto out; 34110edd73b3SHugh Dickins 34120edd73b3SHugh Dickins error = 0; 34135a6e75f8SKirill A. Shutemov sbinfo->huge = config.huge; 3414680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 3415680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 3416680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 341771fe804bSLee Schermerhorn 34185f00110fSGreg Thelen /* 34195f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 34205f00110fSGreg Thelen */ 34215f00110fSGreg Thelen if (config.mpol) { 342271fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 342371fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 34245f00110fSGreg Thelen } 34250edd73b3SHugh Dickins out: 34260edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 34270edd73b3SHugh Dickins return error; 34281da177e4SLinus Torvalds } 3429680d794bSakpm@linux-foundation.org 343034c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3431680d794bSakpm@linux-foundation.org { 343234c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3433680d794bSakpm@linux-foundation.org 3434680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3435680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 343609cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3437680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3438680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 34390825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 344009208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 34418751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 34428751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 34438751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 34448751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 34458751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 34468751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3447e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 34485a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 34495a6e75f8SKirill A. Shutemov if (sbinfo->huge) 34505a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 34515a6e75f8SKirill A. Shutemov #endif 345271fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3453680d794bSakpm@linux-foundation.org return 0; 3454680d794bSakpm@linux-foundation.org } 34559183df25SDavid Herrmann 3456680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 34571da177e4SLinus Torvalds 34581da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 34591da177e4SLinus Torvalds { 3460602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3461602586a8SHugh Dickins 3462602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 346349cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3464602586a8SHugh Dickins kfree(sbinfo); 34651da177e4SLinus Torvalds sb->s_fs_info = NULL; 34661da177e4SLinus Torvalds } 34671da177e4SLinus Torvalds 34682b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 34691da177e4SLinus Torvalds { 34701da177e4SLinus Torvalds struct inode *inode; 34710edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3472680d794bSakpm@linux-foundation.org int err = -ENOMEM; 3473680d794bSakpm@linux-foundation.org 3474680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3475425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3476680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3477680d794bSakpm@linux-foundation.org if (!sbinfo) 3478680d794bSakpm@linux-foundation.org return -ENOMEM; 3479680d794bSakpm@linux-foundation.org 34800825a6f9SJoe Perches sbinfo->mode = 0777 | S_ISVTX; 348176aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 348276aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 3483680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 34841da177e4SLinus Torvalds 34850edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 34861da177e4SLinus Torvalds /* 34871da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 34881da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 34891da177e4SLinus Torvalds * but the internal instance is left unlimited. 34901da177e4SLinus Torvalds */ 34911751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 3492680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 3493680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 3494680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 3495680d794bSakpm@linux-foundation.org err = -EINVAL; 3496680d794bSakpm@linux-foundation.org goto failed; 3497680d794bSakpm@linux-foundation.org } 3498ca4e0519SAl Viro } else { 34991751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 35001da177e4SLinus Torvalds } 350191828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 35021751e8a6SLinus Torvalds sb->s_flags |= SB_NOSEC; 35030edd73b3SHugh Dickins #else 35041751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 35050edd73b3SHugh Dickins #endif 35061da177e4SLinus Torvalds 35071da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 3508908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3509602586a8SHugh Dickins goto failed; 3510680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 3511779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3512779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 35131da177e4SLinus Torvalds 3514285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 351509cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 351609cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 35171da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 35181da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3519cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3520b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 352139f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3522b09e0fa4SEric Paris #endif 3523b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 35241751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 352539f0247dSAndreas Gruenbacher #endif 35262b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 35270edd73b3SHugh Dickins 3528454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 35291da177e4SLinus Torvalds if (!inode) 35301da177e4SLinus Torvalds goto failed; 3531680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3532680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3533318ceed0SAl Viro sb->s_root = d_make_root(inode); 3534318ceed0SAl Viro if (!sb->s_root) 353548fde701SAl Viro goto failed; 35361da177e4SLinus Torvalds return 0; 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds failed: 35391da177e4SLinus Torvalds shmem_put_super(sb); 35401da177e4SLinus Torvalds return err; 35411da177e4SLinus Torvalds } 35421da177e4SLinus Torvalds 3543fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 35441da177e4SLinus Torvalds 35451da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 35461da177e4SLinus Torvalds { 354741ffe5d5SHugh Dickins struct shmem_inode_info *info; 354841ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 354941ffe5d5SHugh Dickins if (!info) 35501da177e4SLinus Torvalds return NULL; 355141ffe5d5SHugh Dickins return &info->vfs_inode; 35521da177e4SLinus Torvalds } 35531da177e4SLinus Torvalds 355441ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 3555fa0d7e3dSNick Piggin { 3556fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 355784e710daSAl Viro if (S_ISLNK(inode->i_mode)) 35583ed47db3SAl Viro kfree(inode->i_link); 3559fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3560fa0d7e3dSNick Piggin } 3561fa0d7e3dSNick Piggin 35621da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 35631da177e4SLinus Torvalds { 356409208d15SAl Viro if (S_ISREG(inode->i_mode)) 35651da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 356641ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 35671da177e4SLinus Torvalds } 35681da177e4SLinus Torvalds 356941ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 35701da177e4SLinus Torvalds { 357141ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 357241ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 35731da177e4SLinus Torvalds } 35741da177e4SLinus Torvalds 35759a8ec03eSweiping zhang static void shmem_init_inodecache(void) 35761da177e4SLinus Torvalds { 35771da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 35781da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 35795d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 35801da177e4SLinus Torvalds } 35811da177e4SLinus Torvalds 358241ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 35831da177e4SLinus Torvalds { 35841a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 35851da177e4SLinus Torvalds } 35861da177e4SLinus Torvalds 3587f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 35881da177e4SLinus Torvalds .writepage = shmem_writepage, 358976719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 35901da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3591800d15a5SNick Piggin .write_begin = shmem_write_begin, 3592800d15a5SNick Piggin .write_end = shmem_write_end, 35931da177e4SLinus Torvalds #endif 35941c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 3595304dbdb7SLee Schermerhorn .migratepage = migrate_page, 35961c93923cSAndrew Morton #endif 3597aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 35981da177e4SLinus Torvalds }; 35991da177e4SLinus Torvalds 360015ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 36011da177e4SLinus Torvalds .mmap = shmem_mmap, 3602c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 36031da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3604220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 36052ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 36068174202bSAl Viro .write_iter = generic_file_write_iter, 36071b061d92SChristoph Hellwig .fsync = noop_fsync, 360882c156f8SAl Viro .splice_read = generic_file_splice_read, 3609f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 361083e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 36111da177e4SLinus Torvalds #endif 36121da177e4SLinus Torvalds }; 36131da177e4SLinus Torvalds 361492e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 361544a30220SYu Zhao .getattr = shmem_getattr, 361694c1e62dSHugh Dickins .setattr = shmem_setattr, 3617b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3618b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3619feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3620b09e0fa4SEric Paris #endif 36211da177e4SLinus Torvalds }; 36221da177e4SLinus Torvalds 362392e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 36241da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 36251da177e4SLinus Torvalds .create = shmem_create, 36261da177e4SLinus Torvalds .lookup = simple_lookup, 36271da177e4SLinus Torvalds .link = shmem_link, 36281da177e4SLinus Torvalds .unlink = shmem_unlink, 36291da177e4SLinus Torvalds .symlink = shmem_symlink, 36301da177e4SLinus Torvalds .mkdir = shmem_mkdir, 36311da177e4SLinus Torvalds .rmdir = shmem_rmdir, 36321da177e4SLinus Torvalds .mknod = shmem_mknod, 36332773bf00SMiklos Szeredi .rename = shmem_rename2, 363460545d0dSAl Viro .tmpfile = shmem_tmpfile, 36351da177e4SLinus Torvalds #endif 3636b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3637b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3638b09e0fa4SEric Paris #endif 363939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 364094c1e62dSHugh Dickins .setattr = shmem_setattr, 3641feda821eSChristoph Hellwig .set_acl = simple_set_acl, 364239f0247dSAndreas Gruenbacher #endif 364339f0247dSAndreas Gruenbacher }; 364439f0247dSAndreas Gruenbacher 364592e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 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 36531da177e4SLinus Torvalds }; 36541da177e4SLinus Torvalds 3655759b9775SHugh Dickins static const struct super_operations shmem_ops = { 36561da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 36571da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 36581da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 36591da177e4SLinus Torvalds .statfs = shmem_statfs, 36601da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 3661680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 36621da177e4SLinus Torvalds #endif 36631f895f75SAl Viro .evict_inode = shmem_evict_inode, 36641da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 36651da177e4SLinus Torvalds .put_super = shmem_put_super, 3666779750d2SKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3667779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3668779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3669779750d2SKirill A. Shutemov #endif 36701da177e4SLinus Torvalds }; 36711da177e4SLinus Torvalds 3672f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 367354cb8821SNick Piggin .fault = shmem_fault, 3674d7c17551SNing Qu .map_pages = filemap_map_pages, 36751da177e4SLinus Torvalds #ifdef CONFIG_NUMA 36761da177e4SLinus Torvalds .set_policy = shmem_set_policy, 36771da177e4SLinus Torvalds .get_policy = shmem_get_policy, 36781da177e4SLinus Torvalds #endif 36791da177e4SLinus Torvalds }; 36801da177e4SLinus Torvalds 36813c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 36823c26ff6eSAl Viro int flags, const char *dev_name, void *data) 36831da177e4SLinus Torvalds { 36843c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 36851da177e4SLinus Torvalds } 36861da177e4SLinus Torvalds 368741ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 36881da177e4SLinus Torvalds .owner = THIS_MODULE, 36891da177e4SLinus Torvalds .name = "tmpfs", 36903c26ff6eSAl Viro .mount = shmem_mount, 36911da177e4SLinus Torvalds .kill_sb = kill_litter_super, 36922b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 36931da177e4SLinus Torvalds }; 36941da177e4SLinus Torvalds 369541ffe5d5SHugh Dickins int __init shmem_init(void) 36961da177e4SLinus Torvalds { 36971da177e4SLinus Torvalds int error; 36981da177e4SLinus Torvalds 369916203a7aSRob Landley /* If rootfs called this, don't re-init */ 370016203a7aSRob Landley if (shmem_inode_cachep) 370116203a7aSRob Landley return 0; 370216203a7aSRob Landley 37039a8ec03eSweiping zhang shmem_init_inodecache(); 37041da177e4SLinus Torvalds 370541ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 37061da177e4SLinus Torvalds if (error) { 37071170532bSJoe Perches pr_err("Could not register tmpfs\n"); 37081da177e4SLinus Torvalds goto out2; 37091da177e4SLinus Torvalds } 371095dc112aSGreg Kroah-Hartman 3711ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 37121da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 37131da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 37141170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 37151da177e4SLinus Torvalds goto out1; 37161da177e4SLinus Torvalds } 37175a6e75f8SKirill A. Shutemov 3718e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3719435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 37205a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 37215a6e75f8SKirill A. Shutemov else 37225a6e75f8SKirill A. Shutemov shmem_huge = 0; /* just in case it was patched */ 37235a6e75f8SKirill A. Shutemov #endif 37241da177e4SLinus Torvalds return 0; 37251da177e4SLinus Torvalds 37261da177e4SLinus Torvalds out1: 372741ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 37281da177e4SLinus Torvalds out2: 372941ffe5d5SHugh Dickins shmem_destroy_inodecache(); 37301da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 37311da177e4SLinus Torvalds return error; 37321da177e4SLinus Torvalds } 3733853ac43aSMatt Mackall 3734e496cf3dSKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS) 37355a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 37365a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 37375a6e75f8SKirill A. Shutemov { 37385a6e75f8SKirill A. Shutemov int values[] = { 37395a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 37405a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 37415a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 37425a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 37435a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 37445a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 37455a6e75f8SKirill A. Shutemov }; 37465a6e75f8SKirill A. Shutemov int i, count; 37475a6e75f8SKirill A. Shutemov 37485a6e75f8SKirill A. Shutemov for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { 37495a6e75f8SKirill A. Shutemov const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; 37505a6e75f8SKirill A. Shutemov 37515a6e75f8SKirill A. Shutemov count += sprintf(buf + count, fmt, 37525a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 37535a6e75f8SKirill A. Shutemov } 37545a6e75f8SKirill A. Shutemov buf[count - 1] = '\n'; 37555a6e75f8SKirill A. Shutemov return count; 37565a6e75f8SKirill A. Shutemov } 37575a6e75f8SKirill A. Shutemov 37585a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 37595a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 37605a6e75f8SKirill A. Shutemov { 37615a6e75f8SKirill A. Shutemov char tmp[16]; 37625a6e75f8SKirill A. Shutemov int huge; 37635a6e75f8SKirill A. Shutemov 37645a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 37655a6e75f8SKirill A. Shutemov return -EINVAL; 37665a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 37675a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 37685a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 37695a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 37705a6e75f8SKirill A. Shutemov 37715a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 37725a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 37735a6e75f8SKirill A. Shutemov return -EINVAL; 37745a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 37755a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 37765a6e75f8SKirill A. Shutemov return -EINVAL; 37775a6e75f8SKirill A. Shutemov 37785a6e75f8SKirill A. Shutemov shmem_huge = huge; 3779435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 37805a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 37815a6e75f8SKirill A. Shutemov return count; 37825a6e75f8SKirill A. Shutemov } 37835a6e75f8SKirill A. Shutemov 37845a6e75f8SKirill A. Shutemov struct kobj_attribute shmem_enabled_attr = 37855a6e75f8SKirill A. Shutemov __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); 37863b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */ 3787f3f0e1d2SKirill A. Shutemov 37883b33719cSArnd Bergmann #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3789f3f0e1d2SKirill A. Shutemov bool shmem_huge_enabled(struct vm_area_struct *vma) 3790f3f0e1d2SKirill A. Shutemov { 3791f3f0e1d2SKirill A. Shutemov struct inode *inode = file_inode(vma->vm_file); 3792f3f0e1d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 3793f3f0e1d2SKirill A. Shutemov loff_t i_size; 3794f3f0e1d2SKirill A. Shutemov pgoff_t off; 3795f3f0e1d2SKirill A. Shutemov 3796f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 3797f3f0e1d2SKirill A. Shutemov return true; 3798f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY) 3799f3f0e1d2SKirill A. Shutemov return false; 3800f3f0e1d2SKirill A. Shutemov switch (sbinfo->huge) { 3801f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_NEVER: 3802f3f0e1d2SKirill A. Shutemov return false; 3803f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 3804f3f0e1d2SKirill A. Shutemov return true; 3805f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 3806f3f0e1d2SKirill A. Shutemov off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); 3807f3f0e1d2SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 3808f3f0e1d2SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 3809f3f0e1d2SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 3810f3f0e1d2SKirill A. Shutemov return true; 3811c8402871SGustavo A. R. Silva /* fall through */ 3812f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 3813f3f0e1d2SKirill A. Shutemov /* TODO: implement fadvise() hints */ 3814f3f0e1d2SKirill A. Shutemov return (vma->vm_flags & VM_HUGEPAGE); 3815f3f0e1d2SKirill A. Shutemov default: 3816f3f0e1d2SKirill A. Shutemov VM_BUG_ON(1); 3817f3f0e1d2SKirill A. Shutemov return false; 3818f3f0e1d2SKirill A. Shutemov } 3819f3f0e1d2SKirill A. Shutemov } 38203b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 38215a6e75f8SKirill A. Shutemov 3822853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 3823853ac43aSMatt Mackall 3824853ac43aSMatt Mackall /* 3825853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 3826853ac43aSMatt Mackall * 3827853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 3828853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 3829853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 3830853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 3831853ac43aSMatt Mackall */ 3832853ac43aSMatt Mackall 383341ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 3834853ac43aSMatt Mackall .name = "tmpfs", 38353c26ff6eSAl Viro .mount = ramfs_mount, 3836853ac43aSMatt Mackall .kill_sb = kill_litter_super, 38372b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 3838853ac43aSMatt Mackall }; 3839853ac43aSMatt Mackall 384041ffe5d5SHugh Dickins int __init shmem_init(void) 3841853ac43aSMatt Mackall { 384241ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 3843853ac43aSMatt Mackall 384441ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 3845853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 3846853ac43aSMatt Mackall 3847853ac43aSMatt Mackall return 0; 3848853ac43aSMatt Mackall } 3849853ac43aSMatt Mackall 385041ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 3851853ac43aSMatt Mackall { 3852853ac43aSMatt Mackall return 0; 3853853ac43aSMatt Mackall } 3854853ac43aSMatt Mackall 38553f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 38563f96b79aSHugh Dickins { 38573f96b79aSHugh Dickins return 0; 38583f96b79aSHugh Dickins } 38593f96b79aSHugh Dickins 386024513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 386124513264SHugh Dickins { 386224513264SHugh Dickins } 386324513264SHugh Dickins 3864c01d5b30SHugh Dickins #ifdef CONFIG_MMU 3865c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 3866c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 3867c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 3868c01d5b30SHugh Dickins { 3869c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 3870c01d5b30SHugh Dickins } 3871c01d5b30SHugh Dickins #endif 3872c01d5b30SHugh Dickins 387341ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 387494c1e62dSHugh Dickins { 387541ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 387694c1e62dSHugh Dickins } 387794c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 387894c1e62dSHugh Dickins 3879853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 38800b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 3881454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 38820b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 38830b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 3884853ac43aSMatt Mackall 3885853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 3886853ac43aSMatt Mackall 3887853ac43aSMatt Mackall /* common code */ 38881da177e4SLinus Torvalds 3889703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 3890c7277090SEric Paris unsigned long flags, unsigned int i_flags) 38911da177e4SLinus Torvalds { 38921da177e4SLinus Torvalds struct inode *inode; 389393dec2daSAl Viro struct file *res; 38941da177e4SLinus Torvalds 3895703321b6SMatthew Auld if (IS_ERR(mnt)) 3896703321b6SMatthew Auld return ERR_CAST(mnt); 38971da177e4SLinus Torvalds 3898285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 38991da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 39001da177e4SLinus Torvalds 39011da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 39021da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 39031da177e4SLinus Torvalds 390493dec2daSAl Viro inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, 390593dec2daSAl Viro flags); 3906dac2d1f6SAl Viro if (unlikely(!inode)) { 3907dac2d1f6SAl Viro shmem_unacct_size(flags, size); 3908dac2d1f6SAl Viro return ERR_PTR(-ENOSPC); 3909dac2d1f6SAl Viro } 3910c7277090SEric Paris inode->i_flags |= i_flags; 39111da177e4SLinus Torvalds inode->i_size = size; 39126d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 391326567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 391493dec2daSAl Viro if (!IS_ERR(res)) 391593dec2daSAl Viro res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 39164b42af81SAl Viro &shmem_file_operations); 39176b4d0b27SAl Viro if (IS_ERR(res)) 391893dec2daSAl Viro iput(inode); 39196b4d0b27SAl Viro return res; 39201da177e4SLinus Torvalds } 3921c7277090SEric Paris 3922c7277090SEric Paris /** 3923c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 3924c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 3925c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 3926e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 3927e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 3928c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3929c7277090SEric Paris * @size: size to be set for the file 3930c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3931c7277090SEric Paris */ 3932c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 3933c7277090SEric Paris { 3934703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 3935c7277090SEric Paris } 3936c7277090SEric Paris 3937c7277090SEric Paris /** 3938c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 3939c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3940c7277090SEric Paris * @size: size to be set for the file 3941c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3942c7277090SEric Paris */ 3943c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 3944c7277090SEric Paris { 3945703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 3946c7277090SEric Paris } 3947395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 39481da177e4SLinus Torvalds 394946711810SRandy Dunlap /** 3950703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 3951703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 3952703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 3953703321b6SMatthew Auld * @size: size to be set for the file 3954703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3955703321b6SMatthew Auld */ 3956703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 3957703321b6SMatthew Auld loff_t size, unsigned long flags) 3958703321b6SMatthew Auld { 3959703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 3960703321b6SMatthew Auld } 3961703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 3962703321b6SMatthew Auld 3963703321b6SMatthew Auld /** 39641da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 39651da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 39661da177e4SLinus Torvalds */ 39671da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 39681da177e4SLinus Torvalds { 39691da177e4SLinus Torvalds struct file *file; 39701da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 39711da177e4SLinus Torvalds 397266fc1303SHugh Dickins /* 397366fc1303SHugh Dickins * Cloning a new file under mmap_sem leads to a lock ordering conflict 397466fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 397566fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 397666fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 397766fc1303SHugh Dickins */ 3978703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 39791da177e4SLinus Torvalds if (IS_ERR(file)) 39801da177e4SLinus Torvalds return PTR_ERR(file); 39811da177e4SLinus Torvalds 39821da177e4SLinus Torvalds if (vma->vm_file) 39831da177e4SLinus Torvalds fput(vma->vm_file); 39841da177e4SLinus Torvalds vma->vm_file = file; 39851da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 3986f3f0e1d2SKirill A. Shutemov 3987e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 3988f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 3989f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 3990f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 3991f3f0e1d2SKirill A. Shutemov } 3992f3f0e1d2SKirill A. Shutemov 39931da177e4SLinus Torvalds return 0; 39941da177e4SLinus Torvalds } 3995d9d90e5eSHugh Dickins 3996d9d90e5eSHugh Dickins /** 3997d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 3998d9d90e5eSHugh Dickins * @mapping: the page's address_space 3999d9d90e5eSHugh Dickins * @index: the page index 4000d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4001d9d90e5eSHugh Dickins * 4002d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4003d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 4004d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 4005d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4006d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4007d9d90e5eSHugh Dickins * 400868da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 400968da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4010d9d90e5eSHugh Dickins */ 4011d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4012d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4013d9d90e5eSHugh Dickins { 401468da9f05SHugh Dickins #ifdef CONFIG_SHMEM 401568da9f05SHugh Dickins struct inode *inode = mapping->host; 40169276aad6SHugh Dickins struct page *page; 401768da9f05SHugh Dickins int error; 401868da9f05SHugh Dickins 401968da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 40209e18eb29SAndres Lagar-Cavilla error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, 4021cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 402268da9f05SHugh Dickins if (error) 402368da9f05SHugh Dickins page = ERR_PTR(error); 402468da9f05SHugh Dickins else 402568da9f05SHugh Dickins unlock_page(page); 402668da9f05SHugh Dickins return page; 402768da9f05SHugh Dickins #else 402868da9f05SHugh Dickins /* 402968da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 403068da9f05SHugh Dickins */ 4031d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 403268da9f05SHugh Dickins #endif 4033d9d90e5eSHugh Dickins } 4034d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4035