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> 31e408e695STheodore Ts'o #include <linux/fileattr.h> 32853ac43aSMatt Mackall #include <linux/mm.h> 3346c9a946SArnd Bergmann #include <linux/random.h> 34174cd4b1SIngo Molnar #include <linux/sched/signal.h> 35b95f1b31SPaul Gortmaker #include <linux/export.h> 365ff2121aSMatthew Wilcox (Oracle) #include <linux/shmem_fs.h> 37853ac43aSMatt Mackall #include <linux/swap.h> 38e2e40f2cSChristoph Hellwig #include <linux/uio.h> 39749df87bSMike Kravetz #include <linux/hugetlb.h> 40626c3920SAl Viro #include <linux/fs_parser.h> 4186a2f3f2SMiaohe Lin #include <linux/swapfile.h> 4236f05cabSJeff Layton #include <linux/iversion.h> 43014bb1deSNeilBrown #include "swap.h" 4495cc09d6SAndrea Arcangeli 45853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 46853ac43aSMatt Mackall 47853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 481da177e4SLinus Torvalds /* 491da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 501da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 511da177e4SLinus Torvalds * which makes it a completely usable filesystem. 521da177e4SLinus Torvalds */ 531da177e4SLinus Torvalds 5439f0247dSAndreas Gruenbacher #include <linux/xattr.h> 55a5694255SChristoph Hellwig #include <linux/exportfs.h> 561c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 57feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 581da177e4SLinus Torvalds #include <linux/mman.h> 591da177e4SLinus Torvalds #include <linux/string.h> 601da177e4SLinus Torvalds #include <linux/slab.h> 611da177e4SLinus Torvalds #include <linux/backing-dev.h> 621da177e4SLinus Torvalds #include <linux/writeback.h> 63bda97eabSHugh Dickins #include <linux/pagevec.h> 6441ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6583e4fa9cSHugh Dickins #include <linux/falloc.h> 66708e3508SHugh Dickins #include <linux/splice.h> 671da177e4SLinus Torvalds #include <linux/security.h> 681da177e4SLinus Torvalds #include <linux/swapops.h> 691da177e4SLinus Torvalds #include <linux/mempolicy.h> 701da177e4SLinus Torvalds #include <linux/namei.h> 71b00dc3adSHugh Dickins #include <linux/ctype.h> 72304dbdb7SLee Schermerhorn #include <linux/migrate.h> 73c1f60a5aSChristoph Lameter #include <linux/highmem.h> 74680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7592562927SMimi Zohar #include <linux/magic.h> 769183df25SDavid Herrmann #include <linux/syscalls.h> 7740e041a2SDavid Herrmann #include <linux/fcntl.h> 789183df25SDavid Herrmann #include <uapi/linux/memfd.h> 79cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 804c27fe4cSMike Rapoport #include <linux/rmap.h> 812b4db796SAmir Goldstein #include <linux/uuid.h> 82304dbdb7SLee Schermerhorn 837c0f6ba6SLinus Torvalds #include <linux/uaccess.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 989608703eSJan Kara * inode->i_private (with i_rwsem 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 1090b5071ddSAl Viro struct shmem_options { 1100b5071ddSAl Viro unsigned long long blocks; 1110b5071ddSAl Viro unsigned long long inodes; 1120b5071ddSAl Viro struct mempolicy *mpol; 1130b5071ddSAl Viro kuid_t uid; 1140b5071ddSAl Viro kgid_t gid; 1150b5071ddSAl Viro umode_t mode; 116ea3271f7SChris Down bool full_inums; 1170b5071ddSAl Viro int huge; 1180b5071ddSAl Viro int seen; 1190b5071ddSAl Viro #define SHMEM_SEEN_BLOCKS 1 1200b5071ddSAl Viro #define SHMEM_SEEN_INODES 2 1210b5071ddSAl Viro #define SHMEM_SEEN_HUGE 4 122ea3271f7SChris Down #define SHMEM_SEEN_INUMS 8 1230b5071ddSAl Viro }; 1240b5071ddSAl Viro 125b76db735SAndrew Morton #ifdef CONFIG_TMPFS 126680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 127680d794bSakpm@linux-foundation.org { 128ca79b0c2SArun KS return totalram_pages() / 2; 129680d794bSakpm@linux-foundation.org } 130680d794bSakpm@linux-foundation.org 131680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 132680d794bSakpm@linux-foundation.org { 133ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 134ca79b0c2SArun KS 135ca79b0c2SArun KS return min(nr_pages - totalhigh_pages(), nr_pages / 2); 136680d794bSakpm@linux-foundation.org } 137b76db735SAndrew Morton #endif 138680d794bSakpm@linux-foundation.org 139da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 140da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 141c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 142c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1451da177e4SLinus Torvalds { 1461da177e4SLinus Torvalds return sb->s_fs_info; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* 1501da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1511da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1521da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1531da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1561da177e4SLinus Torvalds { 1570b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 158191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1621da177e4SLinus Torvalds { 1630b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1641da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 16777142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 16877142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 16977142517SKonstantin Khlebnikov { 17077142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 17177142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 17277142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 17377142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 17477142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 17577142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 17677142517SKonstantin Khlebnikov } 17777142517SKonstantin Khlebnikov return 0; 17877142517SKonstantin Khlebnikov } 17977142517SKonstantin Khlebnikov 1801da177e4SLinus Torvalds /* 1811da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 18275edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 183923e2f0eSMatthew Wilcox (Oracle) * shmem_get_folio reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1841da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1851da177e4SLinus Torvalds */ 186800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1871da177e4SLinus Torvalds { 188800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 189800d8c63SKirill A. Shutemov return 0; 190800d8c63SKirill A. Shutemov 191800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 192800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1961da177e4SLinus Torvalds { 1970b0a0806SHugh Dickins if (flags & VM_NORESERVE) 19809cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2010f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages) 2020f079694SMike Rapoport { 2030f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2040f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2050f079694SMike Rapoport 2060f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 2070f079694SMike Rapoport return false; 2080f079694SMike Rapoport 2090f079694SMike Rapoport if (sbinfo->max_blocks) { 2100f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2110f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2120f079694SMike Rapoport goto unacct; 2130f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 2140f079694SMike Rapoport } 2150f079694SMike Rapoport 2160f079694SMike Rapoport return true; 2170f079694SMike Rapoport 2180f079694SMike Rapoport unacct: 2190f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2200f079694SMike Rapoport return false; 2210f079694SMike Rapoport } 2220f079694SMike Rapoport 2230f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2240f079694SMike Rapoport { 2250f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2260f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2270f079694SMike Rapoport 2280f079694SMike Rapoport if (sbinfo->max_blocks) 2290f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2300f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2310f079694SMike Rapoport } 2320f079694SMike Rapoport 233759b9775SHugh Dickins static const struct super_operations shmem_ops; 23430e6a51dSHui Su const struct address_space_operations shmem_aops; 23515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 23692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 23792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 23892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 239f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 240d09e8ca6SPasha Tatashin static const struct vm_operations_struct shmem_anon_vm_ops; 241779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2421da177e4SLinus Torvalds 243d09e8ca6SPasha Tatashin bool vma_is_anon_shmem(struct vm_area_struct *vma) 244d09e8ca6SPasha Tatashin { 245d09e8ca6SPasha Tatashin return vma->vm_ops == &shmem_anon_vm_ops; 246d09e8ca6SPasha Tatashin } 247d09e8ca6SPasha Tatashin 248b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 249b0506e48SMike Rapoport { 250d09e8ca6SPasha Tatashin return vma_is_anon_shmem(vma) || vma->vm_ops == &shmem_vm_ops; 251b0506e48SMike Rapoport } 252b0506e48SMike Rapoport 2531da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 254cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2551da177e4SLinus Torvalds 256e809d5f0SChris Down /* 257e809d5f0SChris Down * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and 258e809d5f0SChris Down * produces a novel ino for the newly allocated inode. 259e809d5f0SChris Down * 260e809d5f0SChris Down * It may also be called when making a hard link to permit the space needed by 261e809d5f0SChris Down * each dentry. However, in that case, no new inode number is needed since that 262e809d5f0SChris Down * internally draws from another pool of inode numbers (currently global 263e809d5f0SChris Down * get_next_ino()). This case is indicated by passing NULL as inop. 264e809d5f0SChris Down */ 265e809d5f0SChris Down #define SHMEM_INO_BATCH 1024 266e809d5f0SChris Down static int shmem_reserve_inode(struct super_block *sb, ino_t *inop) 2675b04c689SPavel Emelyanov { 2685b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 269e809d5f0SChris Down ino_t ino; 270e809d5f0SChris Down 271e809d5f0SChris Down if (!(sb->s_flags & SB_KERNMOUNT)) { 272bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 273bb3e96d6SByron Stanoszek if (sbinfo->max_inodes) { 2745b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 275bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 2765b04c689SPavel Emelyanov return -ENOSPC; 2775b04c689SPavel Emelyanov } 2785b04c689SPavel Emelyanov sbinfo->free_inodes--; 279bb3e96d6SByron Stanoszek } 280e809d5f0SChris Down if (inop) { 281e809d5f0SChris Down ino = sbinfo->next_ino++; 282e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 283e809d5f0SChris Down ino = sbinfo->next_ino++; 284ea3271f7SChris Down if (unlikely(!sbinfo->full_inums && 285ea3271f7SChris Down ino > UINT_MAX)) { 286e809d5f0SChris Down /* 287e809d5f0SChris Down * Emulate get_next_ino uint wraparound for 288e809d5f0SChris Down * compatibility 289e809d5f0SChris Down */ 290ea3271f7SChris Down if (IS_ENABLED(CONFIG_64BIT)) 291ea3271f7SChris Down pr_warn("%s: inode number overflow on device %d, consider using inode64 mount option\n", 292ea3271f7SChris Down __func__, MINOR(sb->s_dev)); 293ea3271f7SChris Down sbinfo->next_ino = 1; 294ea3271f7SChris Down ino = sbinfo->next_ino++; 2955b04c689SPavel Emelyanov } 296e809d5f0SChris Down *inop = ino; 297e809d5f0SChris Down } 298bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 299e809d5f0SChris Down } else if (inop) { 300e809d5f0SChris Down /* 301e809d5f0SChris Down * __shmem_file_setup, one of our callers, is lock-free: it 302e809d5f0SChris Down * doesn't hold stat_lock in shmem_reserve_inode since 303e809d5f0SChris Down * max_inodes is always 0, and is called from potentially 304e809d5f0SChris Down * unknown contexts. As such, use a per-cpu batched allocator 305e809d5f0SChris Down * which doesn't require the per-sb stat_lock unless we are at 306e809d5f0SChris Down * the batch boundary. 307ea3271f7SChris Down * 308ea3271f7SChris Down * We don't need to worry about inode{32,64} since SB_KERNMOUNT 309ea3271f7SChris Down * shmem mounts are not exposed to userspace, so we don't need 310ea3271f7SChris Down * to worry about things like glibc compatibility. 311e809d5f0SChris Down */ 312e809d5f0SChris Down ino_t *next_ino; 313bf11b9a8SSebastian Andrzej Siewior 314e809d5f0SChris Down next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu()); 315e809d5f0SChris Down ino = *next_ino; 316e809d5f0SChris Down if (unlikely(ino % SHMEM_INO_BATCH == 0)) { 317bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 318e809d5f0SChris Down ino = sbinfo->next_ino; 319e809d5f0SChris Down sbinfo->next_ino += SHMEM_INO_BATCH; 320bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 321e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 322e809d5f0SChris Down ino++; 323e809d5f0SChris Down } 324e809d5f0SChris Down *inop = ino; 325e809d5f0SChris Down *next_ino = ++ino; 326e809d5f0SChris Down put_cpu(); 327e809d5f0SChris Down } 328e809d5f0SChris Down 3295b04c689SPavel Emelyanov return 0; 3305b04c689SPavel Emelyanov } 3315b04c689SPavel Emelyanov 3325b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 3335b04c689SPavel Emelyanov { 3345b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3355b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 336bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 3375b04c689SPavel Emelyanov sbinfo->free_inodes++; 338bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3395b04c689SPavel Emelyanov } 3405b04c689SPavel Emelyanov } 3415b04c689SPavel Emelyanov 34246711810SRandy Dunlap /** 34341ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 3441da177e4SLinus Torvalds * @inode: inode to recalc 3451da177e4SLinus Torvalds * 3461da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 3471da177e4SLinus Torvalds * undirtied hole pages behind our back. 3481da177e4SLinus Torvalds * 3491da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 3501da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 3511da177e4SLinus Torvalds * 3521da177e4SLinus Torvalds * It has to be called with the spinlock held. 3531da177e4SLinus Torvalds */ 3541da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 3551da177e4SLinus Torvalds { 3561da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 3571da177e4SLinus Torvalds long freed; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 3601da177e4SLinus Torvalds if (freed > 0) { 3611da177e4SLinus Torvalds info->alloced -= freed; 36254af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 3630f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 367800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 368800d8c63SKirill A. Shutemov { 369800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3704595ef88SKirill A. Shutemov unsigned long flags; 371800d8c63SKirill A. Shutemov 3720f079694SMike Rapoport if (!shmem_inode_acct_block(inode, pages)) 373800d8c63SKirill A. Shutemov return false; 374b1cc94abSMike Rapoport 375aaa52e34SHugh Dickins /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ 376aaa52e34SHugh Dickins inode->i_mapping->nrpages += pages; 377aaa52e34SHugh Dickins 3784595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 379800d8c63SKirill A. Shutemov info->alloced += pages; 380800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 381800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3824595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 383800d8c63SKirill A. Shutemov 384800d8c63SKirill A. Shutemov return true; 385800d8c63SKirill A. Shutemov } 386800d8c63SKirill A. Shutemov 387800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 388800d8c63SKirill A. Shutemov { 389800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3904595ef88SKirill A. Shutemov unsigned long flags; 391800d8c63SKirill A. Shutemov 3926ffcd825SMatthew Wilcox (Oracle) /* nrpages adjustment done by __filemap_remove_folio() or caller */ 393aaa52e34SHugh Dickins 3944595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 395800d8c63SKirill A. Shutemov info->alloced -= pages; 396800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 397800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3984595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 399800d8c63SKirill A. Shutemov 4000f079694SMike Rapoport shmem_inode_unacct_blocks(inode, pages); 401800d8c63SKirill A. Shutemov } 402800d8c63SKirill A. Shutemov 4037a5d0fbbSHugh Dickins /* 40462f945b6SMatthew Wilcox * Replace item expected in xarray by a new item, while holding xa_lock. 4057a5d0fbbSHugh Dickins */ 40662f945b6SMatthew Wilcox static int shmem_replace_entry(struct address_space *mapping, 4077a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 4087a5d0fbbSHugh Dickins { 40962f945b6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 4106dbaf22cSJohannes Weiner void *item; 4117a5d0fbbSHugh Dickins 4127a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 4136dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 41462f945b6SMatthew Wilcox item = xas_load(&xas); 4157a5d0fbbSHugh Dickins if (item != expected) 4167a5d0fbbSHugh Dickins return -ENOENT; 41762f945b6SMatthew Wilcox xas_store(&xas, replacement); 4187a5d0fbbSHugh Dickins return 0; 4197a5d0fbbSHugh Dickins } 4207a5d0fbbSHugh Dickins 4217a5d0fbbSHugh Dickins /* 422d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 423d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 424d1899228SHugh Dickins * 425d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 426d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 427d1899228SHugh Dickins */ 428d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 429d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 430d1899228SHugh Dickins { 431a12831bfSMatthew Wilcox return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); 432d1899228SHugh Dickins } 433d1899228SHugh Dickins 434d1899228SHugh Dickins /* 4355a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 4365a6e75f8SKirill A. Shutemov * 4375a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 4385a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 4395a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 4405a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 4415a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 4425a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 4435a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 4445a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 4455a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 4465a6e75f8SKirill A. Shutemov */ 4475a6e75f8SKirill A. Shutemov 4485a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 4495a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 4505a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 4515a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 4525a6e75f8SKirill A. Shutemov 4535a6e75f8SKirill A. Shutemov /* 4545a6e75f8SKirill A. Shutemov * Special values. 4555a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 4565a6e75f8SKirill A. Shutemov * 4575a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 4585a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 4595a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 4605a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 4615a6e75f8SKirill A. Shutemov * 4625a6e75f8SKirill A. Shutemov */ 4635a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 4645a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 4655a6e75f8SKirill A. Shutemov 466396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4675a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 4685a6e75f8SKirill A. Shutemov 4695e6e5a12SHugh Dickins static int shmem_huge __read_mostly = SHMEM_HUGE_NEVER; 4705a6e75f8SKirill A. Shutemov 4712cf13384SDavid Stevens bool shmem_is_huge(struct inode *inode, pgoff_t index, bool shmem_huge_force, 4722cf13384SDavid Stevens struct mm_struct *mm, unsigned long vm_flags) 473c852023eSHugh Dickins { 474c852023eSHugh Dickins loff_t i_size; 475c852023eSHugh Dickins 476f7cd16a5SXavier Roche if (!S_ISREG(inode->i_mode)) 477f7cd16a5SXavier Roche return false; 4782cf13384SDavid Stevens if (mm && ((vm_flags & VM_NOHUGEPAGE) || test_bit(MMF_DISABLE_THP, &mm->flags))) 479c852023eSHugh Dickins return false; 4807c6c6cc4SZach O'Keefe if (shmem_huge == SHMEM_HUGE_DENY) 4817c6c6cc4SZach O'Keefe return false; 4823de0c269SZach O'Keefe if (shmem_huge_force || shmem_huge == SHMEM_HUGE_FORCE) 4833de0c269SZach O'Keefe return true; 4845e6e5a12SHugh Dickins 4855e6e5a12SHugh Dickins switch (SHMEM_SB(inode->i_sb)->huge) { 486c852023eSHugh Dickins case SHMEM_HUGE_ALWAYS: 487c852023eSHugh Dickins return true; 488c852023eSHugh Dickins case SHMEM_HUGE_WITHIN_SIZE: 489de6ee659SLiu Yuntao index = round_up(index + 1, HPAGE_PMD_NR); 490c852023eSHugh Dickins i_size = round_up(i_size_read(inode), PAGE_SIZE); 491de6ee659SLiu Yuntao if (i_size >> PAGE_SHIFT >= index) 492c852023eSHugh Dickins return true; 493c852023eSHugh Dickins fallthrough; 494c852023eSHugh Dickins case SHMEM_HUGE_ADVISE: 4952cf13384SDavid Stevens if (mm && (vm_flags & VM_HUGEPAGE)) 4965e6e5a12SHugh Dickins return true; 4975e6e5a12SHugh Dickins fallthrough; 498c852023eSHugh Dickins default: 499c852023eSHugh Dickins return false; 500c852023eSHugh Dickins } 501c852023eSHugh Dickins } 5025a6e75f8SKirill A. Shutemov 503e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) 5045a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 5055a6e75f8SKirill A. Shutemov { 5065a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 5075a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 5085a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 5095a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 5105a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 5115a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 5125a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 5135a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 5145a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 5155a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 5165a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 5175a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 5185a6e75f8SKirill A. Shutemov return -EINVAL; 5195a6e75f8SKirill A. Shutemov } 520e5f2249aSArnd Bergmann #endif 5215a6e75f8SKirill A. Shutemov 522e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 5235a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 5245a6e75f8SKirill A. Shutemov { 5255a6e75f8SKirill A. Shutemov switch (huge) { 5265a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 5275a6e75f8SKirill A. Shutemov return "never"; 5285a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 5295a6e75f8SKirill A. Shutemov return "always"; 5305a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 5315a6e75f8SKirill A. Shutemov return "within_size"; 5325a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 5335a6e75f8SKirill A. Shutemov return "advise"; 5345a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 5355a6e75f8SKirill A. Shutemov return "deny"; 5365a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 5375a6e75f8SKirill A. Shutemov return "force"; 5385a6e75f8SKirill A. Shutemov default: 5395a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 5405a6e75f8SKirill A. Shutemov return "bad_val"; 5415a6e75f8SKirill A. Shutemov } 5425a6e75f8SKirill A. Shutemov } 543f1f5929cSJérémy Lefaure #endif 5445a6e75f8SKirill A. Shutemov 545779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 546779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 547779750d2SKirill A. Shutemov { 548779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 549253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 550779750d2SKirill A. Shutemov struct inode *inode; 551779750d2SKirill A. Shutemov struct shmem_inode_info *info; 55205624571SMatthew Wilcox (Oracle) struct folio *folio; 553779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 55462c9827cSGang Li int split = 0; 555779750d2SKirill A. Shutemov 556779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 557779750d2SKirill A. Shutemov return SHRINK_STOP; 558779750d2SKirill A. Shutemov 559779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 560779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 561779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 562779750d2SKirill A. Shutemov 563779750d2SKirill A. Shutemov /* pin the inode */ 564779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 565779750d2SKirill A. Shutemov 566779750d2SKirill A. Shutemov /* inode is about to be evicted */ 567779750d2SKirill A. Shutemov if (!inode) { 568779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 569779750d2SKirill A. Shutemov goto next; 570779750d2SKirill A. Shutemov } 571779750d2SKirill A. Shutemov 572779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 573779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 574779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 575253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 576779750d2SKirill A. Shutemov goto next; 577779750d2SKirill A. Shutemov } 578779750d2SKirill A. Shutemov 579779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 580779750d2SKirill A. Shutemov next: 58162c9827cSGang Li sbinfo->shrinklist_len--; 582779750d2SKirill A. Shutemov if (!--batch) 583779750d2SKirill A. Shutemov break; 584779750d2SKirill A. Shutemov } 585779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 586779750d2SKirill A. Shutemov 587253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 588253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 589253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 590253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 591253fd0f0SKirill A. Shutemov iput(inode); 592253fd0f0SKirill A. Shutemov } 593253fd0f0SKirill A. Shutemov 594779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 595779750d2SKirill A. Shutemov int ret; 59605624571SMatthew Wilcox (Oracle) pgoff_t index; 597779750d2SKirill A. Shutemov 598779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 599779750d2SKirill A. Shutemov inode = &info->vfs_inode; 600779750d2SKirill A. Shutemov 601b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 60262c9827cSGang Li goto move_back; 603779750d2SKirill A. Shutemov 60405624571SMatthew Wilcox (Oracle) index = (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT; 60505624571SMatthew Wilcox (Oracle) folio = filemap_get_folio(inode->i_mapping, index); 60605624571SMatthew Wilcox (Oracle) if (!folio) 607779750d2SKirill A. Shutemov goto drop; 608779750d2SKirill A. Shutemov 609b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 61005624571SMatthew Wilcox (Oracle) if (!folio_test_large(folio)) { 61105624571SMatthew Wilcox (Oracle) folio_put(folio); 612779750d2SKirill A. Shutemov goto drop; 613779750d2SKirill A. Shutemov } 614779750d2SKirill A. Shutemov 615b3cd54b2SKirill A. Shutemov /* 61662c9827cSGang Li * Move the inode on the list back to shrinklist if we failed 61762c9827cSGang Li * to lock the page at this time. 618b3cd54b2SKirill A. Shutemov * 619b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 620b3cd54b2SKirill A. Shutemov * reclaim path. 621b3cd54b2SKirill A. Shutemov */ 62205624571SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) { 62305624571SMatthew Wilcox (Oracle) folio_put(folio); 62462c9827cSGang Li goto move_back; 625b3cd54b2SKirill A. Shutemov } 626b3cd54b2SKirill A. Shutemov 627d788f5b3SMatthew Wilcox (Oracle) ret = split_folio(folio); 62805624571SMatthew Wilcox (Oracle) folio_unlock(folio); 62905624571SMatthew Wilcox (Oracle) folio_put(folio); 630779750d2SKirill A. Shutemov 63162c9827cSGang Li /* If split failed move the inode on the list back to shrinklist */ 632b3cd54b2SKirill A. Shutemov if (ret) 63362c9827cSGang Li goto move_back; 634779750d2SKirill A. Shutemov 635779750d2SKirill A. Shutemov split++; 636779750d2SKirill A. Shutemov drop: 637779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 63862c9827cSGang Li goto put; 63962c9827cSGang Li move_back: 64062c9827cSGang Li /* 64162c9827cSGang Li * Make sure the inode is either on the global list or deleted 64262c9827cSGang Li * from any local list before iput() since it could be deleted 64362c9827cSGang Li * in another thread once we put the inode (then the local list 64462c9827cSGang Li * is corrupted). 64562c9827cSGang Li */ 64662c9827cSGang Li spin_lock(&sbinfo->shrinklist_lock); 64762c9827cSGang Li list_move(&info->shrinklist, &sbinfo->shrinklist); 64862c9827cSGang Li sbinfo->shrinklist_len++; 64962c9827cSGang Li spin_unlock(&sbinfo->shrinklist_lock); 65062c9827cSGang Li put: 651779750d2SKirill A. Shutemov iput(inode); 652779750d2SKirill A. Shutemov } 653779750d2SKirill A. Shutemov 654779750d2SKirill A. Shutemov return split; 655779750d2SKirill A. Shutemov } 656779750d2SKirill A. Shutemov 657779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 658779750d2SKirill A. Shutemov struct shrink_control *sc) 659779750d2SKirill A. Shutemov { 660779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 661779750d2SKirill A. Shutemov 662779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 663779750d2SKirill A. Shutemov return SHRINK_STOP; 664779750d2SKirill A. Shutemov 665779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 666779750d2SKirill A. Shutemov } 667779750d2SKirill A. Shutemov 668779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 669779750d2SKirill A. Shutemov struct shrink_control *sc) 670779750d2SKirill A. Shutemov { 671779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 672779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 673779750d2SKirill A. Shutemov } 674396bcc52SMatthew Wilcox (Oracle) #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 6755a6e75f8SKirill A. Shutemov 6765a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 6775a6e75f8SKirill A. Shutemov 6782cf13384SDavid Stevens bool shmem_is_huge(struct inode *inode, pgoff_t index, bool shmem_huge_force, 6792cf13384SDavid Stevens struct mm_struct *mm, unsigned long vm_flags) 6805e6e5a12SHugh Dickins { 6815e6e5a12SHugh Dickins return false; 6825e6e5a12SHugh Dickins } 6835e6e5a12SHugh Dickins 684779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 685779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 686779750d2SKirill A. Shutemov { 687779750d2SKirill A. Shutemov return 0; 688779750d2SKirill A. Shutemov } 689396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 6905a6e75f8SKirill A. Shutemov 6915a6e75f8SKirill A. Shutemov /* 6922bb876b5SMatthew Wilcox (Oracle) * Like filemap_add_folio, but error if expected item has gone. 69346f65ec1SHugh Dickins */ 694b7dd44a1SMatthew Wilcox (Oracle) static int shmem_add_to_page_cache(struct folio *folio, 69546f65ec1SHugh Dickins struct address_space *mapping, 6963fea5a49SJohannes Weiner pgoff_t index, void *expected, gfp_t gfp, 6973fea5a49SJohannes Weiner struct mm_struct *charge_mm) 69846f65ec1SHugh Dickins { 699b7dd44a1SMatthew Wilcox (Oracle) XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio)); 700b7dd44a1SMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 7013fea5a49SJohannes Weiner int error; 70246f65ec1SHugh Dickins 703b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(index != round_down(index, nr), folio); 704b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 705b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio); 706b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON(expected && folio_test_large(folio)); 70746f65ec1SHugh Dickins 708b7dd44a1SMatthew Wilcox (Oracle) folio_ref_add(folio, nr); 709b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = mapping; 710b7dd44a1SMatthew Wilcox (Oracle) folio->index = index; 71146f65ec1SHugh Dickins 712b7dd44a1SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) { 713b7dd44a1SMatthew Wilcox (Oracle) error = mem_cgroup_charge(folio, charge_mm, gfp); 7143fea5a49SJohannes Weiner if (error) { 715b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 7163fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK); 7173fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK_CHARGE); 7183fea5a49SJohannes Weiner } 7193fea5a49SJohannes Weiner goto error; 7203fea5a49SJohannes Weiner } 7214c6355b2SJohannes Weiner } 722b7dd44a1SMatthew Wilcox (Oracle) folio_throttle_swaprate(folio, gfp); 7233fea5a49SJohannes Weiner 724552446a4SMatthew Wilcox do { 725552446a4SMatthew Wilcox xas_lock_irq(&xas); 7266b24ca4aSMatthew Wilcox (Oracle) if (expected != xas_find_conflict(&xas)) { 727552446a4SMatthew Wilcox xas_set_err(&xas, -EEXIST); 7286b24ca4aSMatthew Wilcox (Oracle) goto unlock; 7296b24ca4aSMatthew Wilcox (Oracle) } 7306b24ca4aSMatthew Wilcox (Oracle) if (expected && xas_find_conflict(&xas)) { 7316b24ca4aSMatthew Wilcox (Oracle) xas_set_err(&xas, -EEXIST); 7326b24ca4aSMatthew Wilcox (Oracle) goto unlock; 7336b24ca4aSMatthew Wilcox (Oracle) } 734b7dd44a1SMatthew Wilcox (Oracle) xas_store(&xas, folio); 735552446a4SMatthew Wilcox if (xas_error(&xas)) 736552446a4SMatthew Wilcox goto unlock; 737b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 738800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 739b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr); 740552446a4SMatthew Wilcox } 741552446a4SMatthew Wilcox mapping->nrpages += nr; 742b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr); 743b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, nr); 744552446a4SMatthew Wilcox unlock: 745552446a4SMatthew Wilcox xas_unlock_irq(&xas); 746552446a4SMatthew Wilcox } while (xas_nomem(&xas, gfp)); 747552446a4SMatthew Wilcox 748552446a4SMatthew Wilcox if (xas_error(&xas)) { 7493fea5a49SJohannes Weiner error = xas_error(&xas); 7503fea5a49SJohannes Weiner goto error; 75146f65ec1SHugh Dickins } 752552446a4SMatthew Wilcox 753552446a4SMatthew Wilcox return 0; 7543fea5a49SJohannes Weiner error: 755b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = NULL; 756b7dd44a1SMatthew Wilcox (Oracle) folio_ref_sub(folio, nr); 7573fea5a49SJohannes Weiner return error; 75846f65ec1SHugh Dickins } 75946f65ec1SHugh Dickins 76046f65ec1SHugh Dickins /* 7614cd400fdSMatthew Wilcox (Oracle) * Like delete_from_page_cache, but substitutes swap for @folio. 7626922c0c7SHugh Dickins */ 7634cd400fdSMatthew Wilcox (Oracle) static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) 7646922c0c7SHugh Dickins { 7654cd400fdSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 7664cd400fdSMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 7676922c0c7SHugh Dickins int error; 7686922c0c7SHugh Dickins 769b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 7704cd400fdSMatthew Wilcox (Oracle) error = shmem_replace_entry(mapping, folio->index, folio, radswap); 7714cd400fdSMatthew Wilcox (Oracle) folio->mapping = NULL; 7724cd400fdSMatthew Wilcox (Oracle) mapping->nrpages -= nr; 7734cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr); 7744cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr); 775b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 7764cd400fdSMatthew Wilcox (Oracle) folio_put(folio); 7776922c0c7SHugh Dickins BUG_ON(error); 7786922c0c7SHugh Dickins } 7796922c0c7SHugh Dickins 7806922c0c7SHugh Dickins /* 781c121d3bbSMatthew Wilcox * Remove swap entry from page cache, free the swap and its page cache. 7827a5d0fbbSHugh Dickins */ 7837a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 7847a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 7857a5d0fbbSHugh Dickins { 7866dbaf22cSJohannes Weiner void *old; 7877a5d0fbbSHugh Dickins 78855f3f7eaSMatthew Wilcox old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); 7896dbaf22cSJohannes Weiner if (old != radswap) 7906dbaf22cSJohannes Weiner return -ENOENT; 7917a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 7926dbaf22cSJohannes Weiner return 0; 7937a5d0fbbSHugh Dickins } 7947a5d0fbbSHugh Dickins 7957a5d0fbbSHugh Dickins /* 7966a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 79748131e03SVlastimil Babka * given offsets are swapped out. 7986a15a370SVlastimil Babka * 7999608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 8006a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 8016a15a370SVlastimil Babka */ 80248131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 80348131e03SVlastimil Babka pgoff_t start, pgoff_t end) 8046a15a370SVlastimil Babka { 8057ae3424fSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start); 8066a15a370SVlastimil Babka struct page *page; 80748131e03SVlastimil Babka unsigned long swapped = 0; 8086a15a370SVlastimil Babka 8096a15a370SVlastimil Babka rcu_read_lock(); 8107ae3424fSMatthew Wilcox xas_for_each(&xas, page, end - 1) { 8117ae3424fSMatthew Wilcox if (xas_retry(&xas, page)) 8122cf938aaSMatthew Wilcox continue; 8133159f943SMatthew Wilcox if (xa_is_value(page)) 8146a15a370SVlastimil Babka swapped++; 8156a15a370SVlastimil Babka 8166a15a370SVlastimil Babka if (need_resched()) { 8177ae3424fSMatthew Wilcox xas_pause(&xas); 8186a15a370SVlastimil Babka cond_resched_rcu(); 8196a15a370SVlastimil Babka } 8206a15a370SVlastimil Babka } 8216a15a370SVlastimil Babka 8226a15a370SVlastimil Babka rcu_read_unlock(); 8236a15a370SVlastimil Babka 8246a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 8256a15a370SVlastimil Babka } 8266a15a370SVlastimil Babka 8276a15a370SVlastimil Babka /* 82848131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 82948131e03SVlastimil Babka * given vma is swapped out. 83048131e03SVlastimil Babka * 8319608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 83248131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 83348131e03SVlastimil Babka */ 83448131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 83548131e03SVlastimil Babka { 83648131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 83748131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 83848131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 83948131e03SVlastimil Babka unsigned long swapped; 84048131e03SVlastimil Babka 84148131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 84248131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 84348131e03SVlastimil Babka 84448131e03SVlastimil Babka /* 84548131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 84648131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 84748131e03SVlastimil Babka * already track. 84848131e03SVlastimil Babka */ 84948131e03SVlastimil Babka if (!swapped) 85048131e03SVlastimil Babka return 0; 85148131e03SVlastimil Babka 85248131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 85348131e03SVlastimil Babka return swapped << PAGE_SHIFT; 85448131e03SVlastimil Babka 85548131e03SVlastimil Babka /* Here comes the more involved part */ 85602399c88SPeter Xu return shmem_partial_swap_usage(mapping, vma->vm_pgoff, 85702399c88SPeter Xu vma->vm_pgoff + vma_pages(vma)); 85848131e03SVlastimil Babka } 85948131e03SVlastimil Babka 86048131e03SVlastimil Babka /* 86124513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 86224513264SHugh Dickins */ 86324513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 86424513264SHugh Dickins { 865105c988fSMatthew Wilcox (Oracle) struct folio_batch fbatch; 86624513264SHugh Dickins pgoff_t index = 0; 86724513264SHugh Dickins 868105c988fSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 86924513264SHugh Dickins /* 87024513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 87124513264SHugh Dickins */ 872105c988fSMatthew Wilcox (Oracle) while (!mapping_unevictable(mapping) && 873105c988fSMatthew Wilcox (Oracle) filemap_get_folios(mapping, &index, ~0UL, &fbatch)) { 874105c988fSMatthew Wilcox (Oracle) check_move_unevictable_folios(&fbatch); 875105c988fSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 87624513264SHugh Dickins cond_resched(); 87724513264SHugh Dickins } 8787a5d0fbbSHugh Dickins } 8797a5d0fbbSHugh Dickins 880b9a8a419SMatthew Wilcox (Oracle) static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) 88171725ed1SHugh Dickins { 882b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 88371725ed1SHugh Dickins 884b9a8a419SMatthew Wilcox (Oracle) /* 885a7f5862cSMatthew Wilcox (Oracle) * At first avoid shmem_get_folio(,,,SGP_READ): that fails 886b9a8a419SMatthew Wilcox (Oracle) * beyond i_size, and reports fallocated pages as holes. 887b9a8a419SMatthew Wilcox (Oracle) */ 888b9a8a419SMatthew Wilcox (Oracle) folio = __filemap_get_folio(inode->i_mapping, index, 889b9a8a419SMatthew Wilcox (Oracle) FGP_ENTRY | FGP_LOCK, 0); 890b9a8a419SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 891b9a8a419SMatthew Wilcox (Oracle) return folio; 892b9a8a419SMatthew Wilcox (Oracle) /* 893b9a8a419SMatthew Wilcox (Oracle) * But read a page back from swap if any of it is within i_size 894b9a8a419SMatthew Wilcox (Oracle) * (although in some cases this is just a waste of time). 895b9a8a419SMatthew Wilcox (Oracle) */ 896a7f5862cSMatthew Wilcox (Oracle) folio = NULL; 897a7f5862cSMatthew Wilcox (Oracle) shmem_get_folio(inode, index, &folio, SGP_READ); 898a7f5862cSMatthew Wilcox (Oracle) return folio; 89971725ed1SHugh Dickins } 90071725ed1SHugh Dickins 90171725ed1SHugh Dickins /* 9027f4446eeSMatthew Wilcox * Remove range of pages and swap entries from page cache, and free them. 9031635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 9047a5d0fbbSHugh Dickins */ 9051635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 9061635f6a7SHugh Dickins bool unfalloc) 9071da177e4SLinus Torvalds { 908285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 9091da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 91009cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 91109cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 9120e499ed3SMatthew Wilcox (Oracle) struct folio_batch fbatch; 9137a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 914b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 915b9a8a419SMatthew Wilcox (Oracle) bool same_folio; 9167a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 917285b2c4fSHugh Dickins pgoff_t index; 918bda97eabSHugh Dickins int i; 9191da177e4SLinus Torvalds 92083e4fa9cSHugh Dickins if (lend == -1) 92183e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 922bda97eabSHugh Dickins 923d144bf62SHugh Dickins if (info->fallocend > start && info->fallocend <= end && !unfalloc) 924d144bf62SHugh Dickins info->fallocend = start; 925d144bf62SHugh Dickins 92651dcbdacSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 927bda97eabSHugh Dickins index = start; 9283392ca12SVishal Moola (Oracle) while (index < end && find_lock_entries(mapping, &index, end - 1, 92951dcbdacSMatthew Wilcox (Oracle) &fbatch, indices)) { 93051dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 931b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 932bda97eabSHugh Dickins 9337b774aabSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 9341635f6a7SHugh Dickins if (unfalloc) 9351635f6a7SHugh Dickins continue; 9367a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 9373392ca12SVishal Moola (Oracle) indices[i], folio); 9387a5d0fbbSHugh Dickins continue; 9397a5d0fbbSHugh Dickins } 9407a5d0fbbSHugh Dickins 9417b774aabSMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) 9421e84a3d9SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 9437b774aabSMatthew Wilcox (Oracle) folio_unlock(folio); 944bda97eabSHugh Dickins } 94551dcbdacSMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 94651dcbdacSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 947bda97eabSHugh Dickins cond_resched(); 948bda97eabSHugh Dickins } 949bda97eabSHugh Dickins 95044bcabd7SHugh Dickins /* 95144bcabd7SHugh Dickins * When undoing a failed fallocate, we want none of the partial folio 95244bcabd7SHugh Dickins * zeroing and splitting below, but shall want to truncate the whole 95344bcabd7SHugh Dickins * folio when !uptodate indicates that it was added by this fallocate, 95444bcabd7SHugh Dickins * even when [lstart, lend] covers only a part of the folio. 95544bcabd7SHugh Dickins */ 95644bcabd7SHugh Dickins if (unfalloc) 95744bcabd7SHugh Dickins goto whole_folios; 95844bcabd7SHugh Dickins 959b9a8a419SMatthew Wilcox (Oracle) same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT); 960b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lstart >> PAGE_SHIFT); 961b9a8a419SMatthew Wilcox (Oracle) if (folio) { 962b9a8a419SMatthew Wilcox (Oracle) same_folio = lend < folio_pos(folio) + folio_size(folio); 963b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 964b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) { 965b9a8a419SMatthew Wilcox (Oracle) start = folio->index + folio_nr_pages(folio); 966b9a8a419SMatthew Wilcox (Oracle) if (same_folio) 967b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 96883e4fa9cSHugh Dickins } 969b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 970b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 971b9a8a419SMatthew Wilcox (Oracle) folio = NULL; 972bda97eabSHugh Dickins } 973b9a8a419SMatthew Wilcox (Oracle) 974b9a8a419SMatthew Wilcox (Oracle) if (!same_folio) 975b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT); 976b9a8a419SMatthew Wilcox (Oracle) if (folio) { 977b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 978b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) 979b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 980b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 981b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 982bda97eabSHugh Dickins } 983bda97eabSHugh Dickins 98444bcabd7SHugh Dickins whole_folios: 98544bcabd7SHugh Dickins 986bda97eabSHugh Dickins index = start; 987b1a36650SHugh Dickins while (index < end) { 988bda97eabSHugh Dickins cond_resched(); 9890cd6144aSJohannes Weiner 9909fb6beeaSVishal Moola (Oracle) if (!find_get_entries(mapping, &index, end - 1, &fbatch, 991cf2039afSMatthew Wilcox (Oracle) indices)) { 992b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 993b1a36650SHugh Dickins if (index == start || end != -1) 994bda97eabSHugh Dickins break; 995b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 996bda97eabSHugh Dickins index = start; 997bda97eabSHugh Dickins continue; 998bda97eabSHugh Dickins } 9990e499ed3SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 1000b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 1001bda97eabSHugh Dickins 10020e499ed3SMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 10031635f6a7SHugh Dickins if (unfalloc) 10041635f6a7SHugh Dickins continue; 10059fb6beeaSVishal Moola (Oracle) if (shmem_free_swap(mapping, indices[i], folio)) { 1006b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 10079fb6beeaSVishal Moola (Oracle) index = indices[i]; 1008b1a36650SHugh Dickins break; 1009b1a36650SHugh Dickins } 1010b1a36650SHugh Dickins nr_swaps_freed++; 10117a5d0fbbSHugh Dickins continue; 10127a5d0fbbSHugh Dickins } 10137a5d0fbbSHugh Dickins 10140e499ed3SMatthew Wilcox (Oracle) folio_lock(folio); 1015800d8c63SKirill A. Shutemov 10160e499ed3SMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) { 10170e499ed3SMatthew Wilcox (Oracle) if (folio_mapping(folio) != mapping) { 1018b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 10190e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 10209fb6beeaSVishal Moola (Oracle) index = indices[i]; 1021b1a36650SHugh Dickins break; 10227a5d0fbbSHugh Dickins } 10230e499ed3SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_writeback(folio), 10240e499ed3SMatthew Wilcox (Oracle) folio); 10250e499ed3SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 102671725ed1SHugh Dickins } 10270e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 1028bda97eabSHugh Dickins } 10290e499ed3SMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 10300e499ed3SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1031bda97eabSHugh Dickins } 103294c1e62dSHugh Dickins 10334595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 10347a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 10351da177e4SLinus Torvalds shmem_recalc_inode(inode); 10364595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 10371635f6a7SHugh Dickins } 10381da177e4SLinus Torvalds 10391635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 10401635f6a7SHugh Dickins { 10411635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 1042078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 104336f05cabSJeff Layton inode_inc_iversion(inode); 10441da177e4SLinus Torvalds } 104594c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 10461da177e4SLinus Torvalds 1047b74d24f7SChristian Brauner static int shmem_getattr(struct mnt_idmap *idmap, 1048549c7297SChristian Brauner const struct path *path, struct kstat *stat, 1049a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 105044a30220SYu Zhao { 1051a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 105244a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 105344a30220SYu Zhao 1054d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 10554595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 105644a30220SYu Zhao shmem_recalc_inode(inode); 10574595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1058d0424c42SHugh Dickins } 1059e408e695STheodore Ts'o if (info->fsflags & FS_APPEND_FL) 1060e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_APPEND; 1061e408e695STheodore Ts'o if (info->fsflags & FS_IMMUTABLE_FL) 1062e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_IMMUTABLE; 1063e408e695STheodore Ts'o if (info->fsflags & FS_NODUMP_FL) 1064e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_NODUMP; 1065e408e695STheodore Ts'o stat->attributes_mask |= (STATX_ATTR_APPEND | 1066e408e695STheodore Ts'o STATX_ATTR_IMMUTABLE | 1067e408e695STheodore Ts'o STATX_ATTR_NODUMP); 10687a80e5b8SGiuseppe Scrivano generic_fillattr(idmap, inode, stat); 106989fdcd26SYang Shi 10702cf13384SDavid Stevens if (shmem_is_huge(inode, 0, false, NULL, 0)) 107189fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 107289fdcd26SYang Shi 1073f7cd16a5SXavier Roche if (request_mask & STATX_BTIME) { 1074f7cd16a5SXavier Roche stat->result_mask |= STATX_BTIME; 1075f7cd16a5SXavier Roche stat->btime.tv_sec = info->i_crtime.tv_sec; 1076f7cd16a5SXavier Roche stat->btime.tv_nsec = info->i_crtime.tv_nsec; 1077f7cd16a5SXavier Roche } 1078f7cd16a5SXavier Roche 107944a30220SYu Zhao return 0; 108044a30220SYu Zhao } 108144a30220SYu Zhao 1082c1632a0fSChristian Brauner static int shmem_setattr(struct mnt_idmap *idmap, 1083549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 10841da177e4SLinus Torvalds { 108575c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 108640e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 10871da177e4SLinus Torvalds int error; 108836f05cabSJeff Layton bool update_mtime = false; 108936f05cabSJeff Layton bool update_ctime = true; 10901da177e4SLinus Torvalds 10917a80e5b8SGiuseppe Scrivano error = setattr_prepare(idmap, dentry, attr); 1092db78b877SChristoph Hellwig if (error) 1093db78b877SChristoph Hellwig return error; 1094db78b877SChristoph Hellwig 10956fd73538SDaniel Verkamp if ((info->seals & F_SEAL_EXEC) && (attr->ia_valid & ATTR_MODE)) { 10966fd73538SDaniel Verkamp if ((inode->i_mode ^ attr->ia_mode) & 0111) { 10976fd73538SDaniel Verkamp return -EPERM; 10986fd73538SDaniel Verkamp } 10996fd73538SDaniel Verkamp } 11006fd73538SDaniel Verkamp 110194c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 110294c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 110394c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 11043889e6e7Snpiggin@suse.de 11059608703eSJan Kara /* protected by i_rwsem */ 110640e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 110740e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 110840e041a2SDavid Herrmann return -EPERM; 110940e041a2SDavid Herrmann 111094c1e62dSHugh Dickins if (newsize != oldsize) { 111177142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 111277142517SKonstantin Khlebnikov oldsize, newsize); 111377142517SKonstantin Khlebnikov if (error) 111477142517SKonstantin Khlebnikov return error; 111594c1e62dSHugh Dickins i_size_write(inode, newsize); 111636f05cabSJeff Layton update_mtime = true; 111736f05cabSJeff Layton } else { 111836f05cabSJeff Layton update_ctime = false; 111994c1e62dSHugh Dickins } 1120afa2db2fSJosef Bacik if (newsize <= oldsize) { 112194c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1122d0424c42SHugh Dickins if (oldsize > holebegin) 1123d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1124d0424c42SHugh Dickins holebegin, 0, 1); 1125d0424c42SHugh Dickins if (info->alloced) 1126d0424c42SHugh Dickins shmem_truncate_range(inode, 1127d0424c42SHugh Dickins newsize, (loff_t)-1); 112894c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1129d0424c42SHugh Dickins if (oldsize > holebegin) 1130d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1131d0424c42SHugh Dickins holebegin, 0, 1); 113294c1e62dSHugh Dickins } 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 11357a80e5b8SGiuseppe Scrivano setattr_copy(idmap, inode, attr); 1136db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 11377a80e5b8SGiuseppe Scrivano error = posix_acl_chmod(idmap, dentry, inode->i_mode); 113836f05cabSJeff Layton if (!error && update_ctime) { 113936f05cabSJeff Layton inode->i_ctime = current_time(inode); 114036f05cabSJeff Layton if (update_mtime) 114136f05cabSJeff Layton inode->i_mtime = inode->i_ctime; 114236f05cabSJeff Layton inode_inc_iversion(inode); 114336f05cabSJeff Layton } 11441da177e4SLinus Torvalds return error; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 11481da177e4SLinus Torvalds { 11491da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1150779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 11511da177e4SLinus Torvalds 115230e6a51dSHui Su if (shmem_mapping(inode->i_mapping)) { 11531da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 11541da177e4SLinus Torvalds inode->i_size = 0; 1155bc786390SHugh Dickins mapping_set_exiting(inode->i_mapping); 11563889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1157779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1158779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1159779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1160779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1161779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1162779750d2SKirill A. Shutemov } 1163779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1164779750d2SKirill A. Shutemov } 1165af53d3e9SHugh Dickins while (!list_empty(&info->swaplist)) { 1166af53d3e9SHugh Dickins /* Wait while shmem_unuse() is scanning this inode... */ 1167af53d3e9SHugh Dickins wait_var_event(&info->stop_eviction, 1168af53d3e9SHugh Dickins !atomic_read(&info->stop_eviction)); 1169cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1170af53d3e9SHugh Dickins /* ...but beware of the race if we peeked too early */ 1171af53d3e9SHugh Dickins if (!atomic_read(&info->stop_eviction)) 11721da177e4SLinus Torvalds list_del_init(&info->swaplist); 1173cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 11741da177e4SLinus Torvalds } 11753ed47db3SAl Viro } 1176b09e0fa4SEric Paris 117738f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 11780f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 11795b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1180dbd5768fSJan Kara clear_inode(inode); 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 1183b56a2d8aSVineeth Remanan Pillai static int shmem_find_swap_entries(struct address_space *mapping, 1184da08e9b7SMatthew Wilcox (Oracle) pgoff_t start, struct folio_batch *fbatch, 1185da08e9b7SMatthew Wilcox (Oracle) pgoff_t *indices, unsigned int type) 1186478922e2SMatthew Wilcox { 1187b56a2d8aSVineeth Remanan Pillai XA_STATE(xas, &mapping->i_pages, start); 1188da08e9b7SMatthew Wilcox (Oracle) struct folio *folio; 118987039546SHugh Dickins swp_entry_t entry; 1190478922e2SMatthew Wilcox 1191478922e2SMatthew Wilcox rcu_read_lock(); 1192da08e9b7SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { 1193da08e9b7SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) 11945b9c98f3SMike Kravetz continue; 1195b56a2d8aSVineeth Remanan Pillai 1196da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1197478922e2SMatthew Wilcox continue; 1198b56a2d8aSVineeth Remanan Pillai 1199da08e9b7SMatthew Wilcox (Oracle) entry = radix_to_swp_entry(folio); 12006cec2b95SMiaohe Lin /* 12016cec2b95SMiaohe Lin * swapin error entries can be found in the mapping. But they're 12026cec2b95SMiaohe Lin * deliberately ignored here as we've done everything we can do. 12036cec2b95SMiaohe Lin */ 120487039546SHugh Dickins if (swp_type(entry) != type) 1205b56a2d8aSVineeth Remanan Pillai continue; 1206b56a2d8aSVineeth Remanan Pillai 1207e384200eSHugh Dickins indices[folio_batch_count(fbatch)] = xas.xa_index; 1208da08e9b7SMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio)) 1209da08e9b7SMatthew Wilcox (Oracle) break; 1210b56a2d8aSVineeth Remanan Pillai 1211b56a2d8aSVineeth Remanan Pillai if (need_resched()) { 1212e21a2955SMatthew Wilcox xas_pause(&xas); 1213478922e2SMatthew Wilcox cond_resched_rcu(); 1214478922e2SMatthew Wilcox } 1215b56a2d8aSVineeth Remanan Pillai } 1216478922e2SMatthew Wilcox rcu_read_unlock(); 1217e21a2955SMatthew Wilcox 1218da08e9b7SMatthew Wilcox (Oracle) return xas.xa_index; 1219b56a2d8aSVineeth Remanan Pillai } 1220b56a2d8aSVineeth Remanan Pillai 1221b56a2d8aSVineeth Remanan Pillai /* 1222b56a2d8aSVineeth Remanan Pillai * Move the swapped pages for an inode to page cache. Returns the count 1223b56a2d8aSVineeth Remanan Pillai * of pages swapped in, or the error in case of failure. 1224b56a2d8aSVineeth Remanan Pillai */ 1225da08e9b7SMatthew Wilcox (Oracle) static int shmem_unuse_swap_entries(struct inode *inode, 1226da08e9b7SMatthew Wilcox (Oracle) struct folio_batch *fbatch, pgoff_t *indices) 1227b56a2d8aSVineeth Remanan Pillai { 1228b56a2d8aSVineeth Remanan Pillai int i = 0; 1229b56a2d8aSVineeth Remanan Pillai int ret = 0; 1230b56a2d8aSVineeth Remanan Pillai int error = 0; 1231b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1232b56a2d8aSVineeth Remanan Pillai 1233da08e9b7SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(fbatch); i++) { 1234da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = fbatch->folios[i]; 1235b56a2d8aSVineeth Remanan Pillai 1236da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1237b56a2d8aSVineeth Remanan Pillai continue; 1238da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, indices[i], 1239da08e9b7SMatthew Wilcox (Oracle) &folio, SGP_CACHE, 1240b56a2d8aSVineeth Remanan Pillai mapping_gfp_mask(mapping), 1241b56a2d8aSVineeth Remanan Pillai NULL, NULL); 1242b56a2d8aSVineeth Remanan Pillai if (error == 0) { 1243da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1244da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1245b56a2d8aSVineeth Remanan Pillai ret++; 1246b56a2d8aSVineeth Remanan Pillai } 1247b56a2d8aSVineeth Remanan Pillai if (error == -ENOMEM) 1248b56a2d8aSVineeth Remanan Pillai break; 1249b56a2d8aSVineeth Remanan Pillai error = 0; 1250b56a2d8aSVineeth Remanan Pillai } 1251b56a2d8aSVineeth Remanan Pillai return error ? error : ret; 1252478922e2SMatthew Wilcox } 1253478922e2SMatthew Wilcox 125446f65ec1SHugh Dickins /* 125546f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 125646f65ec1SHugh Dickins */ 125710a9c496SChristoph Hellwig static int shmem_unuse_inode(struct inode *inode, unsigned int type) 12581da177e4SLinus Torvalds { 1259b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1260b56a2d8aSVineeth Remanan Pillai pgoff_t start = 0; 1261da08e9b7SMatthew Wilcox (Oracle) struct folio_batch fbatch; 1262b56a2d8aSVineeth Remanan Pillai pgoff_t indices[PAGEVEC_SIZE]; 1263b56a2d8aSVineeth Remanan Pillai int ret = 0; 12641da177e4SLinus Torvalds 1265b56a2d8aSVineeth Remanan Pillai do { 1266da08e9b7SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1267da08e9b7SMatthew Wilcox (Oracle) shmem_find_swap_entries(mapping, start, &fbatch, indices, type); 1268da08e9b7SMatthew Wilcox (Oracle) if (folio_batch_count(&fbatch) == 0) { 1269b56a2d8aSVineeth Remanan Pillai ret = 0; 1270778dd893SHugh Dickins break; 1271b56a2d8aSVineeth Remanan Pillai } 1272b56a2d8aSVineeth Remanan Pillai 1273da08e9b7SMatthew Wilcox (Oracle) ret = shmem_unuse_swap_entries(inode, &fbatch, indices); 1274b56a2d8aSVineeth Remanan Pillai if (ret < 0) 1275b56a2d8aSVineeth Remanan Pillai break; 1276b56a2d8aSVineeth Remanan Pillai 1277da08e9b7SMatthew Wilcox (Oracle) start = indices[folio_batch_count(&fbatch) - 1]; 1278b56a2d8aSVineeth Remanan Pillai } while (true); 1279b56a2d8aSVineeth Remanan Pillai 1280b56a2d8aSVineeth Remanan Pillai return ret; 1281b56a2d8aSVineeth Remanan Pillai } 1282b56a2d8aSVineeth Remanan Pillai 1283b56a2d8aSVineeth Remanan Pillai /* 1284b56a2d8aSVineeth Remanan Pillai * Read all the shared memory data that resides in the swap 1285b56a2d8aSVineeth Remanan Pillai * device 'type' back into memory, so the swap device can be 1286b56a2d8aSVineeth Remanan Pillai * unused. 1287b56a2d8aSVineeth Remanan Pillai */ 128810a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 1289b56a2d8aSVineeth Remanan Pillai { 1290b56a2d8aSVineeth Remanan Pillai struct shmem_inode_info *info, *next; 1291b56a2d8aSVineeth Remanan Pillai int error = 0; 1292b56a2d8aSVineeth Remanan Pillai 1293b56a2d8aSVineeth Remanan Pillai if (list_empty(&shmem_swaplist)) 1294b56a2d8aSVineeth Remanan Pillai return 0; 1295b56a2d8aSVineeth Remanan Pillai 1296b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1297b56a2d8aSVineeth Remanan Pillai list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { 1298b56a2d8aSVineeth Remanan Pillai if (!info->swapped) { 1299b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1300b56a2d8aSVineeth Remanan Pillai continue; 1301b56a2d8aSVineeth Remanan Pillai } 1302af53d3e9SHugh Dickins /* 1303af53d3e9SHugh Dickins * Drop the swaplist mutex while searching the inode for swap; 1304af53d3e9SHugh Dickins * but before doing so, make sure shmem_evict_inode() will not 1305af53d3e9SHugh Dickins * remove placeholder inode from swaplist, nor let it be freed 1306af53d3e9SHugh Dickins * (igrab() would protect from unlink, but not from unmount). 1307af53d3e9SHugh Dickins */ 1308af53d3e9SHugh Dickins atomic_inc(&info->stop_eviction); 1309b56a2d8aSVineeth Remanan Pillai mutex_unlock(&shmem_swaplist_mutex); 1310b56a2d8aSVineeth Remanan Pillai 131110a9c496SChristoph Hellwig error = shmem_unuse_inode(&info->vfs_inode, type); 1312b56a2d8aSVineeth Remanan Pillai cond_resched(); 1313b56a2d8aSVineeth Remanan Pillai 1314b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1315b56a2d8aSVineeth Remanan Pillai next = list_next_entry(info, swaplist); 1316b56a2d8aSVineeth Remanan Pillai if (!info->swapped) 1317b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1318af53d3e9SHugh Dickins if (atomic_dec_and_test(&info->stop_eviction)) 1319af53d3e9SHugh Dickins wake_up_var(&info->stop_eviction); 1320b56a2d8aSVineeth Remanan Pillai if (error) 1321b56a2d8aSVineeth Remanan Pillai break; 13221da177e4SLinus Torvalds } 1323cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1324778dd893SHugh Dickins 1325778dd893SHugh Dickins return error; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* 13291da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 13301da177e4SLinus Torvalds */ 13311da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 13321da177e4SLinus Torvalds { 1333e2e3fdc7SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 1334*8ccee8c1SLuis Chamberlain struct address_space *mapping = folio->mapping; 1335*8ccee8c1SLuis Chamberlain struct inode *inode = mapping->host; 1336*8ccee8c1SLuis Chamberlain struct shmem_inode_info *info = SHMEM_I(inode); 13376922c0c7SHugh Dickins swp_entry_t swap; 13386922c0c7SHugh Dickins pgoff_t index; 13391da177e4SLinus Torvalds 13401e6decf3SHugh Dickins /* 13411e6decf3SHugh Dickins * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or 13421e6decf3SHugh Dickins * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages, 13431e6decf3SHugh Dickins * and its shmem_writeback() needs them to be split when swapping. 13441e6decf3SHugh Dickins */ 1345f530ed0eSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 13461e6decf3SHugh Dickins /* Ensure the subpages are still dirty */ 1347f530ed0eSMatthew Wilcox (Oracle) folio_test_set_dirty(folio); 13481e6decf3SHugh Dickins if (split_huge_page(page) < 0) 13491e6decf3SHugh Dickins goto redirty; 1350f530ed0eSMatthew Wilcox (Oracle) folio = page_folio(page); 1351f530ed0eSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 13521e6decf3SHugh Dickins } 13531e6decf3SHugh Dickins 1354f530ed0eSMatthew Wilcox (Oracle) index = folio->index; 13551da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 13561da177e4SLinus Torvalds goto redirty; 1357d9fe526aSHugh Dickins if (!total_swap_pages) 13581da177e4SLinus Torvalds goto redirty; 13591da177e4SLinus Torvalds 1360d9fe526aSHugh Dickins /* 136197b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 136297b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 136397b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 136497b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 136597b713baSChristoph Hellwig * threads or sync. 1366d9fe526aSHugh Dickins */ 136748f170fbSHugh Dickins if (!wbc->for_reclaim) { 136848f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 136948f170fbSHugh Dickins goto redirty; 137048f170fbSHugh Dickins } 13711635f6a7SHugh Dickins 13721635f6a7SHugh Dickins /* 13731635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 13741635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 1375f530ed0eSMatthew Wilcox (Oracle) * fallocated folio arriving here is now to initialize it and write it. 13761aac1400SHugh Dickins * 1377f530ed0eSMatthew Wilcox (Oracle) * That's okay for a folio already fallocated earlier, but if we have 13781aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 1379f530ed0eSMatthew Wilcox (Oracle) * of this folio in case we have to undo it, and (b) it may not be a 13801aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 1381f530ed0eSMatthew Wilcox (Oracle) * reactivate the folio, and let shmem_fallocate() quit when too many. 13821635f6a7SHugh Dickins */ 1383f530ed0eSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 13841aac1400SHugh Dickins if (inode->i_private) { 13851aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 13861aac1400SHugh Dickins spin_lock(&inode->i_lock); 13871aac1400SHugh Dickins shmem_falloc = inode->i_private; 13881aac1400SHugh Dickins if (shmem_falloc && 13898e205f77SHugh Dickins !shmem_falloc->waitq && 13901aac1400SHugh Dickins index >= shmem_falloc->start && 13911aac1400SHugh Dickins index < shmem_falloc->next) 13921aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 13931aac1400SHugh Dickins else 13941aac1400SHugh Dickins shmem_falloc = NULL; 13951aac1400SHugh Dickins spin_unlock(&inode->i_lock); 13961aac1400SHugh Dickins if (shmem_falloc) 13971aac1400SHugh Dickins goto redirty; 13981aac1400SHugh Dickins } 1399f530ed0eSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio)); 1400f530ed0eSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 1401f530ed0eSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 14021635f6a7SHugh Dickins } 14031635f6a7SHugh Dickins 1404e2e3fdc7SMatthew Wilcox (Oracle) swap = folio_alloc_swap(folio); 140548f170fbSHugh Dickins if (!swap.val) 140648f170fbSHugh Dickins goto redirty; 1407d9fe526aSHugh Dickins 1408b1dea800SHugh Dickins /* 1409b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 1410f530ed0eSMatthew Wilcox (Oracle) * if it's not already there. Do it now before the folio is 14116922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1412b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 14136922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 14146922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1415b1dea800SHugh Dickins */ 1416b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 141705bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 1418b56a2d8aSVineeth Remanan Pillai list_add(&info->swaplist, &shmem_swaplist); 1419b1dea800SHugh Dickins 1420a4c366f0SMatthew Wilcox (Oracle) if (add_to_swap_cache(folio, swap, 14213852f676SJoonsoo Kim __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, 14223852f676SJoonsoo Kim NULL) == 0) { 14234595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1424267a4c76SHugh Dickins shmem_recalc_inode(inode); 1425267a4c76SHugh Dickins info->swapped++; 14264595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1427267a4c76SHugh Dickins 1428aaa46865SHugh Dickins swap_shmem_alloc(swap); 14294cd400fdSMatthew Wilcox (Oracle) shmem_delete_from_page_cache(folio, swp_to_radix_entry(swap)); 14306922c0c7SHugh Dickins 14316922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1432f530ed0eSMatthew Wilcox (Oracle) BUG_ON(folio_mapped(folio)); 1433f530ed0eSMatthew Wilcox (Oracle) swap_writepage(&folio->page, wbc); 14341da177e4SLinus Torvalds return 0; 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 14376922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 14384081f744SMatthew Wilcox (Oracle) put_swap_folio(folio, swap); 14391da177e4SLinus Torvalds redirty: 1440f530ed0eSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1441d9fe526aSHugh Dickins if (wbc->for_reclaim) 1442f530ed0eSMatthew Wilcox (Oracle) return AOP_WRITEPAGE_ACTIVATE; /* Return with folio locked */ 1443f530ed0eSMatthew Wilcox (Oracle) folio_unlock(folio); 1444d9fe526aSHugh Dickins return 0; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 144775edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 144871fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1449680d794bSakpm@linux-foundation.org { 1450680d794bSakpm@linux-foundation.org char buffer[64]; 1451680d794bSakpm@linux-foundation.org 145271fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1453095f1fc4SLee Schermerhorn return; /* show nothing */ 1454095f1fc4SLee Schermerhorn 1455a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1456095f1fc4SLee Schermerhorn 1457095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1458680d794bSakpm@linux-foundation.org } 145971fe804bSLee Schermerhorn 146071fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 146171fe804bSLee Schermerhorn { 146271fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 146371fe804bSLee Schermerhorn if (sbinfo->mpol) { 1464bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 146571fe804bSLee Schermerhorn mpol = sbinfo->mpol; 146671fe804bSLee Schermerhorn mpol_get(mpol); 1467bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 146871fe804bSLee Schermerhorn } 146971fe804bSLee Schermerhorn return mpol; 147071fe804bSLee Schermerhorn } 147175edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 147275edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 147375edd345SHugh Dickins { 147475edd345SHugh Dickins } 147575edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 147675edd345SHugh Dickins { 147775edd345SHugh Dickins return NULL; 147875edd345SHugh Dickins } 147975edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 148075edd345SHugh Dickins #ifndef CONFIG_NUMA 148175edd345SHugh Dickins #define vm_policy vm_private_data 148275edd345SHugh Dickins #endif 1483680d794bSakpm@linux-foundation.org 1484800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1485800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1486800d8c63SKirill A. Shutemov { 1487800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 14882c4541e2SKirill A. Shutemov vma_init(vma, NULL); 1489800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1490800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1491800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1492800d8c63SKirill A. Shutemov } 1493800d8c63SKirill A. Shutemov 1494800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1495800d8c63SKirill A. Shutemov { 1496800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1497800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1498800d8c63SKirill A. Shutemov } 1499800d8c63SKirill A. Shutemov 15005739a81cSMatthew Wilcox (Oracle) static struct folio *shmem_swapin(swp_entry_t swap, gfp_t gfp, 150141ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 15021da177e4SLinus Torvalds { 15031da177e4SLinus Torvalds struct vm_area_struct pvma; 150418a2f371SMel Gorman struct page *page; 15058c63ca5bSWill Deacon struct vm_fault vmf = { 15068c63ca5bSWill Deacon .vma = &pvma, 15078c63ca5bSWill Deacon }; 15081da177e4SLinus Torvalds 1509800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1510e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1511800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 151218a2f371SMel Gorman 15135739a81cSMatthew Wilcox (Oracle) if (!page) 15145739a81cSMatthew Wilcox (Oracle) return NULL; 15155739a81cSMatthew Wilcox (Oracle) return page_folio(page); 1516800d8c63SKirill A. Shutemov } 151718a2f371SMel Gorman 151878cc8cdcSRik van Riel /* 151978cc8cdcSRik van Riel * Make sure huge_gfp is always more limited than limit_gfp. 152078cc8cdcSRik van Riel * Some of the flags set permissions, while others set limitations. 152178cc8cdcSRik van Riel */ 152278cc8cdcSRik van Riel static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp) 152378cc8cdcSRik van Riel { 152478cc8cdcSRik van Riel gfp_t allowflags = __GFP_IO | __GFP_FS | __GFP_RECLAIM; 152578cc8cdcSRik van Riel gfp_t denyflags = __GFP_NOWARN | __GFP_NORETRY; 1526187df5ddSRik van Riel gfp_t zoneflags = limit_gfp & GFP_ZONEMASK; 1527187df5ddSRik van Riel gfp_t result = huge_gfp & ~(allowflags | GFP_ZONEMASK); 1528187df5ddSRik van Riel 1529187df5ddSRik van Riel /* Allow allocations only from the originally specified zones. */ 1530187df5ddSRik van Riel result |= zoneflags; 153178cc8cdcSRik van Riel 153278cc8cdcSRik van Riel /* 153378cc8cdcSRik van Riel * Minimize the result gfp by taking the union with the deny flags, 153478cc8cdcSRik van Riel * and the intersection of the allow flags. 153578cc8cdcSRik van Riel */ 153678cc8cdcSRik van Riel result |= (limit_gfp & denyflags); 153778cc8cdcSRik van Riel result |= (huge_gfp & limit_gfp) & allowflags; 153878cc8cdcSRik van Riel 153978cc8cdcSRik van Riel return result; 154078cc8cdcSRik van Riel } 154178cc8cdcSRik van Riel 154272827e5cSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_hugefolio(gfp_t gfp, 1543800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1544800d8c63SKirill A. Shutemov { 1545800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 15467b8d046fSMatthew Wilcox struct address_space *mapping = info->vfs_inode.i_mapping; 15477b8d046fSMatthew Wilcox pgoff_t hindex; 1548dfe98499SMatthew Wilcox (Oracle) struct folio *folio; 1549800d8c63SKirill A. Shutemov 15504620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 15517b8d046fSMatthew Wilcox if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, 15527b8d046fSMatthew Wilcox XA_PRESENT)) 1553800d8c63SKirill A. Shutemov return NULL; 1554800d8c63SKirill A. Shutemov 1555800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1556dfe98499SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, &pvma, 0, true); 1557800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1558dfe98499SMatthew Wilcox (Oracle) if (!folio) 1559dcdf11eeSDavid Rientjes count_vm_event(THP_FILE_FALLBACK); 156072827e5cSMatthew Wilcox (Oracle) return folio; 156118a2f371SMel Gorman } 156218a2f371SMel Gorman 15630c023ef5SMatthew Wilcox (Oracle) static struct folio *shmem_alloc_folio(gfp_t gfp, 156418a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 156518a2f371SMel Gorman { 156618a2f371SMel Gorman struct vm_area_struct pvma; 15670c023ef5SMatthew Wilcox (Oracle) struct folio *folio; 156818a2f371SMel Gorman 1569800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 15700c023ef5SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, 0, &pvma, 0, false); 1571800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 157218a2f371SMel Gorman 15730c023ef5SMatthew Wilcox (Oracle) return folio; 157418a2f371SMel Gorman } 157518a2f371SMel Gorman 1576b1d0ec3aSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode, 1577800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1578800d8c63SKirill A. Shutemov { 15790f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 158072827e5cSMatthew Wilcox (Oracle) struct folio *folio; 1581800d8c63SKirill A. Shutemov int nr; 1582800d8c63SKirill A. Shutemov int err = -ENOSPC; 1583800d8c63SKirill A. Shutemov 1584396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 1585800d8c63SKirill A. Shutemov huge = false; 1586800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1587800d8c63SKirill A. Shutemov 15880f079694SMike Rapoport if (!shmem_inode_acct_block(inode, nr)) 1589800d8c63SKirill A. Shutemov goto failed; 1590800d8c63SKirill A. Shutemov 1591800d8c63SKirill A. Shutemov if (huge) 159272827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_hugefolio(gfp, info, index); 1593800d8c63SKirill A. Shutemov else 159472827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, index); 159572827e5cSMatthew Wilcox (Oracle) if (folio) { 159672827e5cSMatthew Wilcox (Oracle) __folio_set_locked(folio); 159772827e5cSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 1598b1d0ec3aSMatthew Wilcox (Oracle) return folio; 159975edd345SHugh Dickins } 160018a2f371SMel Gorman 1601800d8c63SKirill A. Shutemov err = -ENOMEM; 16020f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1603800d8c63SKirill A. Shutemov failed: 1604800d8c63SKirill A. Shutemov return ERR_PTR(err); 16051da177e4SLinus Torvalds } 160671fe804bSLee Schermerhorn 16071da177e4SLinus Torvalds /* 1608bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1609fc26babbSMatthew Wilcox (Oracle) * usual swapin of shmem_get_folio_gfp(), or by the less common swapoff of 1610bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1611bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1612bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1613bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1614bde05d1cSHugh Dickins * 1615bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1616bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1617bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1618bde05d1cSHugh Dickins */ 1619069d849cSMatthew Wilcox (Oracle) static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp) 1620bde05d1cSHugh Dickins { 1621069d849cSMatthew Wilcox (Oracle) return folio_zonenum(folio) > gfp_zone(gfp); 1622bde05d1cSHugh Dickins } 1623bde05d1cSHugh Dickins 16240d698e25SMatthew Wilcox (Oracle) static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, 1625bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1626bde05d1cSHugh Dickins { 1627d21bba2bSMatthew Wilcox (Oracle) struct folio *old, *new; 1628bde05d1cSHugh Dickins struct address_space *swap_mapping; 1629c1cb20d4SYu Zhao swp_entry_t entry; 1630bde05d1cSHugh Dickins pgoff_t swap_index; 1631bde05d1cSHugh Dickins int error; 1632bde05d1cSHugh Dickins 16330d698e25SMatthew Wilcox (Oracle) old = *foliop; 1634907ea17eSMatthew Wilcox (Oracle) entry = folio_swap_entry(old); 1635c1cb20d4SYu Zhao swap_index = swp_offset(entry); 1636907ea17eSMatthew Wilcox (Oracle) swap_mapping = swap_address_space(entry); 1637bde05d1cSHugh Dickins 1638bde05d1cSHugh Dickins /* 1639bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1640bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1641bde05d1cSHugh Dickins */ 1642bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1643907ea17eSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(old), old); 1644907ea17eSMatthew Wilcox (Oracle) new = shmem_alloc_folio(gfp, info, index); 1645907ea17eSMatthew Wilcox (Oracle) if (!new) 1646bde05d1cSHugh Dickins return -ENOMEM; 1647bde05d1cSHugh Dickins 1648907ea17eSMatthew Wilcox (Oracle) folio_get(new); 1649907ea17eSMatthew Wilcox (Oracle) folio_copy(new, old); 1650907ea17eSMatthew Wilcox (Oracle) flush_dcache_folio(new); 1651bde05d1cSHugh Dickins 1652907ea17eSMatthew Wilcox (Oracle) __folio_set_locked(new); 1653907ea17eSMatthew Wilcox (Oracle) __folio_set_swapbacked(new); 1654907ea17eSMatthew Wilcox (Oracle) folio_mark_uptodate(new); 1655907ea17eSMatthew Wilcox (Oracle) folio_set_swap_entry(new, entry); 1656907ea17eSMatthew Wilcox (Oracle) folio_set_swapcache(new); 1657bde05d1cSHugh Dickins 1658bde05d1cSHugh Dickins /* 1659bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1660bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1661bde05d1cSHugh Dickins */ 1662b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 1663907ea17eSMatthew Wilcox (Oracle) error = shmem_replace_entry(swap_mapping, swap_index, old, new); 16640142ef6cSHugh Dickins if (!error) { 1665d21bba2bSMatthew Wilcox (Oracle) mem_cgroup_migrate(old, new); 1666907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_FILE_PAGES, 1); 1667907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_SHMEM, 1); 1668907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_FILE_PAGES, -1); 1669907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_SHMEM, -1); 16700142ef6cSHugh Dickins } 1671b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1672bde05d1cSHugh Dickins 16730142ef6cSHugh Dickins if (unlikely(error)) { 16740142ef6cSHugh Dickins /* 16750142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 16760142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 16770142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 16780142ef6cSHugh Dickins */ 1679907ea17eSMatthew Wilcox (Oracle) old = new; 16800142ef6cSHugh Dickins } else { 1681907ea17eSMatthew Wilcox (Oracle) folio_add_lru(new); 16820d698e25SMatthew Wilcox (Oracle) *foliop = new; 16830142ef6cSHugh Dickins } 1684bde05d1cSHugh Dickins 1685907ea17eSMatthew Wilcox (Oracle) folio_clear_swapcache(old); 1686907ea17eSMatthew Wilcox (Oracle) old->private = NULL; 1687bde05d1cSHugh Dickins 1688907ea17eSMatthew Wilcox (Oracle) folio_unlock(old); 1689907ea17eSMatthew Wilcox (Oracle) folio_put_refs(old, 2); 16900142ef6cSHugh Dickins return error; 1691bde05d1cSHugh Dickins } 1692bde05d1cSHugh Dickins 16936cec2b95SMiaohe Lin static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, 16946cec2b95SMiaohe Lin struct folio *folio, swp_entry_t swap) 16956cec2b95SMiaohe Lin { 16966cec2b95SMiaohe Lin struct address_space *mapping = inode->i_mapping; 16976cec2b95SMiaohe Lin struct shmem_inode_info *info = SHMEM_I(inode); 16986cec2b95SMiaohe Lin swp_entry_t swapin_error; 16996cec2b95SMiaohe Lin void *old; 17006cec2b95SMiaohe Lin 170115520a3fSPeter Xu swapin_error = make_swapin_error_entry(); 17026cec2b95SMiaohe Lin old = xa_cmpxchg_irq(&mapping->i_pages, index, 17036cec2b95SMiaohe Lin swp_to_radix_entry(swap), 17046cec2b95SMiaohe Lin swp_to_radix_entry(swapin_error), 0); 17056cec2b95SMiaohe Lin if (old != swp_to_radix_entry(swap)) 17066cec2b95SMiaohe Lin return; 17076cec2b95SMiaohe Lin 17086cec2b95SMiaohe Lin folio_wait_writeback(folio); 170975fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 17106cec2b95SMiaohe Lin spin_lock_irq(&info->lock); 17116cec2b95SMiaohe Lin /* 17126cec2b95SMiaohe Lin * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks won't 17136cec2b95SMiaohe Lin * be 0 when inode is released and thus trigger WARN_ON(inode->i_blocks) in 17146cec2b95SMiaohe Lin * shmem_evict_inode. 17156cec2b95SMiaohe Lin */ 17166cec2b95SMiaohe Lin info->alloced--; 17176cec2b95SMiaohe Lin info->swapped--; 17186cec2b95SMiaohe Lin shmem_recalc_inode(inode); 17196cec2b95SMiaohe Lin spin_unlock_irq(&info->lock); 17206cec2b95SMiaohe Lin swap_free(swap); 17216cec2b95SMiaohe Lin } 17226cec2b95SMiaohe Lin 1723bde05d1cSHugh Dickins /* 1724833de10fSMiaohe Lin * Swap in the folio pointed to by *foliop. 1725833de10fSMiaohe Lin * Caller has to make sure that *foliop contains a valid swapped folio. 1726833de10fSMiaohe Lin * Returns 0 and the folio in foliop if success. On failure, returns the 1727833de10fSMiaohe Lin * error code and NULL in *foliop. 17281da177e4SLinus Torvalds */ 1729da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 1730da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 1731c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 17322b740303SSouptick Joarder vm_fault_t *fault_type) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 173523f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 173604f94e3fSDan Schatzberg struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; 1737cbc2bd98SKairui Song struct swap_info_struct *si; 1738da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = NULL; 17391da177e4SLinus Torvalds swp_entry_t swap; 17401da177e4SLinus Torvalds int error; 17411da177e4SLinus Torvalds 1742da08e9b7SMatthew Wilcox (Oracle) VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); 1743da08e9b7SMatthew Wilcox (Oracle) swap = radix_to_swp_entry(*foliop); 1744da08e9b7SMatthew Wilcox (Oracle) *foliop = NULL; 174554af6042SHugh Dickins 17466cec2b95SMiaohe Lin if (is_swapin_error_entry(swap)) 17476cec2b95SMiaohe Lin return -EIO; 17486cec2b95SMiaohe Lin 1749cbc2bd98SKairui Song si = get_swap_device(swap); 1750cbc2bd98SKairui Song if (!si) { 1751cbc2bd98SKairui Song if (!shmem_confirm_swap(mapping, index, swap)) 1752cbc2bd98SKairui Song return -EEXIST; 1753cbc2bd98SKairui Song else 1754cbc2bd98SKairui Song return -EINVAL; 1755cbc2bd98SKairui Song } 1756cbc2bd98SKairui Song 17571da177e4SLinus Torvalds /* Look it up and read it in.. */ 17585739a81cSMatthew Wilcox (Oracle) folio = swap_cache_get_folio(swap, NULL, 0); 17595739a81cSMatthew Wilcox (Oracle) if (!folio) { 17609e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 17619e18eb29SAndres Lagar-Cavilla if (fault_type) { 176268da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 17639e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 17642262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 17659e18eb29SAndres Lagar-Cavilla } 17669e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 17675739a81cSMatthew Wilcox (Oracle) folio = shmem_swapin(swap, gfp, info, index); 17685739a81cSMatthew Wilcox (Oracle) if (!folio) { 17691da177e4SLinus Torvalds error = -ENOMEM; 177054af6042SHugh Dickins goto failed; 1771285b2c4fSHugh Dickins } 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 1774833de10fSMiaohe Lin /* We have to do this with folio locked to prevent races */ 1775da08e9b7SMatthew Wilcox (Oracle) folio_lock(folio); 1776da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio) || 1777da08e9b7SMatthew Wilcox (Oracle) folio_swap_entry(folio).val != swap.val || 1778d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1779c5bf121eSVineeth Remanan Pillai error = -EEXIST; 1780d1899228SHugh Dickins goto unlock; 1781bde05d1cSHugh Dickins } 1782da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 17831da177e4SLinus Torvalds error = -EIO; 178454af6042SHugh Dickins goto failed; 178554af6042SHugh Dickins } 1786da08e9b7SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 178754af6042SHugh Dickins 17888a84802eSSteven Price /* 17898a84802eSSteven Price * Some architectures may have to restore extra metadata to the 1790da08e9b7SMatthew Wilcox (Oracle) * folio after reading from swap. 17918a84802eSSteven Price */ 1792da08e9b7SMatthew Wilcox (Oracle) arch_swap_restore(swap, folio); 17938a84802eSSteven Price 1794069d849cSMatthew Wilcox (Oracle) if (shmem_should_replace_folio(folio, gfp)) { 17950d698e25SMatthew Wilcox (Oracle) error = shmem_replace_folio(&folio, gfp, info, index); 1796bde05d1cSHugh Dickins if (error) 179754af6042SHugh Dickins goto failed; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 1800b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, index, 18013fea5a49SJohannes Weiner swp_to_radix_entry(swap), gfp, 18023fea5a49SJohannes Weiner charge_mm); 180354af6042SHugh Dickins if (error) 180454af6042SHugh Dickins goto failed; 180554af6042SHugh Dickins 18064595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 180754af6042SHugh Dickins info->swapped--; 180854af6042SHugh Dickins shmem_recalc_inode(inode); 18094595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 181027ab7006SHugh Dickins 181166d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1812da08e9b7SMatthew Wilcox (Oracle) folio_mark_accessed(folio); 181366d2f4d2SHugh Dickins 181475fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 1815da08e9b7SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 181627ab7006SHugh Dickins swap_free(swap); 1817cbc2bd98SKairui Song put_swap_device(si); 181827ab7006SHugh Dickins 1819da08e9b7SMatthew Wilcox (Oracle) *foliop = folio; 1820c5bf121eSVineeth Remanan Pillai return 0; 1821c5bf121eSVineeth Remanan Pillai failed: 1822c5bf121eSVineeth Remanan Pillai if (!shmem_confirm_swap(mapping, index, swap)) 1823c5bf121eSVineeth Remanan Pillai error = -EEXIST; 18246cec2b95SMiaohe Lin if (error == -EIO) 18256cec2b95SMiaohe Lin shmem_set_folio_swapin_error(inode, index, folio, swap); 1826c5bf121eSVineeth Remanan Pillai unlock: 1827da08e9b7SMatthew Wilcox (Oracle) if (folio) { 1828da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1829da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1830c5bf121eSVineeth Remanan Pillai } 1831cbc2bd98SKairui Song put_swap_device(si); 1832c5bf121eSVineeth Remanan Pillai 1833c5bf121eSVineeth Remanan Pillai return error; 1834c5bf121eSVineeth Remanan Pillai } 1835c5bf121eSVineeth Remanan Pillai 1836c5bf121eSVineeth Remanan Pillai /* 1837fc26babbSMatthew Wilcox (Oracle) * shmem_get_folio_gfp - find page in cache, or get from swap, or allocate 1838c5bf121eSVineeth Remanan Pillai * 1839c5bf121eSVineeth Remanan Pillai * If we allocate a new one we do not mark it dirty. That's up to the 1840c5bf121eSVineeth Remanan Pillai * vm. If we swap it in we mark it dirty since we also free the swap 1841c5bf121eSVineeth Remanan Pillai * entry since a page cannot live in both the swap and page cache. 1842c5bf121eSVineeth Remanan Pillai * 1843c949b097SAxel Rasmussen * vma, vmf, and fault_type are only supplied by shmem_fault: 1844c5bf121eSVineeth Remanan Pillai * otherwise they are NULL. 1845c5bf121eSVineeth Remanan Pillai */ 1846fc26babbSMatthew Wilcox (Oracle) static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, 1847fc26babbSMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, gfp_t gfp, 1848c5bf121eSVineeth Remanan Pillai struct vm_area_struct *vma, struct vm_fault *vmf, 1849c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type) 1850c5bf121eSVineeth Remanan Pillai { 1851c5bf121eSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1852c5bf121eSVineeth Remanan Pillai struct shmem_inode_info *info = SHMEM_I(inode); 1853c5bf121eSVineeth Remanan Pillai struct shmem_sb_info *sbinfo; 1854c5bf121eSVineeth Remanan Pillai struct mm_struct *charge_mm; 1855b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 18566fe7d712SLukas Bulwahn pgoff_t hindex; 1857164cc4feSRik van Riel gfp_t huge_gfp; 1858c5bf121eSVineeth Remanan Pillai int error; 1859c5bf121eSVineeth Remanan Pillai int once = 0; 1860c5bf121eSVineeth Remanan Pillai int alloced = 0; 1861c5bf121eSVineeth Remanan Pillai 1862c5bf121eSVineeth Remanan Pillai if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 1863c5bf121eSVineeth Remanan Pillai return -EFBIG; 1864c5bf121eSVineeth Remanan Pillai repeat: 1865c5bf121eSVineeth Remanan Pillai if (sgp <= SGP_CACHE && 1866c5bf121eSVineeth Remanan Pillai ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1867c5bf121eSVineeth Remanan Pillai return -EINVAL; 1868c5bf121eSVineeth Remanan Pillai } 1869c5bf121eSVineeth Remanan Pillai 1870c5bf121eSVineeth Remanan Pillai sbinfo = SHMEM_SB(inode->i_sb); 187104f94e3fSDan Schatzberg charge_mm = vma ? vma->vm_mm : NULL; 1872c5bf121eSVineeth Remanan Pillai 1873b1d0ec3aSMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, index, FGP_ENTRY | FGP_LOCK, 0); 1874b1d0ec3aSMatthew Wilcox (Oracle) if (folio && vma && userfaultfd_minor(vma)) { 1875b1d0ec3aSMatthew Wilcox (Oracle) if (!xa_is_value(folio)) { 1876b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 1877b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1878c949b097SAxel Rasmussen } 1879c949b097SAxel Rasmussen *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); 1880c949b097SAxel Rasmussen return 0; 1881c949b097SAxel Rasmussen } 1882c949b097SAxel Rasmussen 1883b1d0ec3aSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 1884da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, index, &folio, 1885c5bf121eSVineeth Remanan Pillai sgp, gfp, vma, fault_type); 1886c5bf121eSVineeth Remanan Pillai if (error == -EEXIST) 1887c5bf121eSVineeth Remanan Pillai goto repeat; 1888c5bf121eSVineeth Remanan Pillai 1889fc26babbSMatthew Wilcox (Oracle) *foliop = folio; 1890c5bf121eSVineeth Remanan Pillai return error; 1891c5bf121eSVineeth Remanan Pillai } 1892c5bf121eSVineeth Remanan Pillai 1893b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 1894acdd9f8eSHugh Dickins if (sgp == SGP_WRITE) 1895b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 1896b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) 1897acdd9f8eSHugh Dickins goto out; 1898fc26babbSMatthew Wilcox (Oracle) /* fallocated folio */ 1899c5bf121eSVineeth Remanan Pillai if (sgp != SGP_READ) 1900c5bf121eSVineeth Remanan Pillai goto clear; 1901b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 1902b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1903c5bf121eSVineeth Remanan Pillai } 1904c5bf121eSVineeth Remanan Pillai 1905c5bf121eSVineeth Remanan Pillai /* 1906fc26babbSMatthew Wilcox (Oracle) * SGP_READ: succeed on hole, with NULL folio, letting caller zero. 1907fc26babbSMatthew Wilcox (Oracle) * SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail. 1908acdd9f8eSHugh Dickins */ 1909fc26babbSMatthew Wilcox (Oracle) *foliop = NULL; 1910acdd9f8eSHugh Dickins if (sgp == SGP_READ) 1911acdd9f8eSHugh Dickins return 0; 1912acdd9f8eSHugh Dickins if (sgp == SGP_NOALLOC) 1913acdd9f8eSHugh Dickins return -ENOENT; 1914acdd9f8eSHugh Dickins 1915acdd9f8eSHugh Dickins /* 1916acdd9f8eSHugh Dickins * Fast cache lookup and swap lookup did not find it: allocate. 1917c5bf121eSVineeth Remanan Pillai */ 1918c5bf121eSVineeth Remanan Pillai 1919cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1920cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1921cfda0526SMike Rapoport return 0; 1922cfda0526SMike Rapoport } 1923cfda0526SMike Rapoport 19242cf13384SDavid Stevens if (!shmem_is_huge(inode, index, false, 19252cf13384SDavid Stevens vma ? vma->vm_mm : NULL, vma ? vma->vm_flags : 0)) 1926800d8c63SKirill A. Shutemov goto alloc_nohuge; 192727d80fa2SKees Cook 1928164cc4feSRik van Riel huge_gfp = vma_thp_gfp_mask(vma); 192978cc8cdcSRik van Riel huge_gfp = limit_gfp_mask(huge_gfp, gfp); 1930b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(huge_gfp, inode, index, true); 1931b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 1932c5bf121eSVineeth Remanan Pillai alloc_nohuge: 1933b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(gfp, inode, index, false); 193454af6042SHugh Dickins } 1935b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 1936779750d2SKirill A. Shutemov int retry = 5; 1937c5bf121eSVineeth Remanan Pillai 1938b1d0ec3aSMatthew Wilcox (Oracle) error = PTR_ERR(folio); 1939b1d0ec3aSMatthew Wilcox (Oracle) folio = NULL; 1940779750d2SKirill A. Shutemov if (error != -ENOSPC) 1941c5bf121eSVineeth Remanan Pillai goto unlock; 1942779750d2SKirill A. Shutemov /* 1943fc26babbSMatthew Wilcox (Oracle) * Try to reclaim some space by splitting a large folio 1944779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1945779750d2SKirill A. Shutemov */ 1946779750d2SKirill A. Shutemov while (retry--) { 1947779750d2SKirill A. Shutemov int ret; 1948c5bf121eSVineeth Remanan Pillai 1949779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1950779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1951779750d2SKirill A. Shutemov break; 1952779750d2SKirill A. Shutemov if (ret) 1953779750d2SKirill A. Shutemov goto alloc_nohuge; 1954779750d2SKirill A. Shutemov } 1955c5bf121eSVineeth Remanan Pillai goto unlock; 1956800d8c63SKirill A. Shutemov } 1957800d8c63SKirill A. Shutemov 1958b1d0ec3aSMatthew Wilcox (Oracle) hindex = round_down(index, folio_nr_pages(folio)); 1959800d8c63SKirill A. Shutemov 196066d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1961b1d0ec3aSMatthew Wilcox (Oracle) __folio_set_referenced(folio); 196266d2f4d2SHugh Dickins 1963b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, hindex, 19643fea5a49SJohannes Weiner NULL, gfp & GFP_RECLAIM_MASK, 19653fea5a49SJohannes Weiner charge_mm); 19663fea5a49SJohannes Weiner if (error) 1967800d8c63SKirill A. Shutemov goto unacct; 1968b1d0ec3aSMatthew Wilcox (Oracle) folio_add_lru(folio); 196954af6042SHugh Dickins 19704595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1971b1d0ec3aSMatthew Wilcox (Oracle) info->alloced += folio_nr_pages(folio); 1972fa020a2bSAndrew Morton inode->i_blocks += (blkcnt_t)BLOCKS_PER_PAGE << folio_order(folio); 197354af6042SHugh Dickins shmem_recalc_inode(inode); 19744595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 19751635f6a7SHugh Dickins alloced = true; 197654af6042SHugh Dickins 1977b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio) && 1978779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1979fc26babbSMatthew Wilcox (Oracle) folio_next_index(folio) - 1) { 1980779750d2SKirill A. Shutemov /* 1981fc26babbSMatthew Wilcox (Oracle) * Part of the large folio is beyond i_size: subject 1982779750d2SKirill A. Shutemov * to shrink under memory pressure. 1983779750d2SKirill A. Shutemov */ 1984779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1985d041353dSCong Wang /* 1986d041353dSCong Wang * _careful to defend against unlocked access to 1987d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1988d041353dSCong Wang */ 1989d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1990779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1991779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1992779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1993779750d2SKirill A. Shutemov } 1994779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1995779750d2SKirill A. Shutemov } 1996779750d2SKirill A. Shutemov 1997ec9516fbSHugh Dickins /* 1998fc26babbSMatthew Wilcox (Oracle) * Let SGP_FALLOC use the SGP_WRITE optimization on a new folio. 19991635f6a7SHugh Dickins */ 20001635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 20011635f6a7SHugh Dickins sgp = SGP_WRITE; 20021635f6a7SHugh Dickins clear: 20031635f6a7SHugh Dickins /* 2004fc26babbSMatthew Wilcox (Oracle) * Let SGP_WRITE caller clear ends if write does not fill folio; 2005fc26babbSMatthew Wilcox (Oracle) * but SGP_FALLOC on a folio fallocated earlier must initialize 20061635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 2007ec9516fbSHugh Dickins */ 2008b1d0ec3aSMatthew Wilcox (Oracle) if (sgp != SGP_WRITE && !folio_test_uptodate(folio)) { 2009b1d0ec3aSMatthew Wilcox (Oracle) long i, n = folio_nr_pages(folio); 2010800d8c63SKirill A. Shutemov 2011b1d0ec3aSMatthew Wilcox (Oracle) for (i = 0; i < n; i++) 2012b1d0ec3aSMatthew Wilcox (Oracle) clear_highpage(folio_page(folio, i)); 2013b1d0ec3aSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 2014b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 2015ec9516fbSHugh Dickins } 2016bde05d1cSHugh Dickins 201754af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 201875edd345SHugh Dickins if (sgp <= SGP_CACHE && 201909cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 2020267a4c76SHugh Dickins if (alloced) { 2021b1d0ec3aSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 2022b1d0ec3aSMatthew Wilcox (Oracle) filemap_remove_folio(folio); 20234595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 2024267a4c76SHugh Dickins shmem_recalc_inode(inode); 20254595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 2026267a4c76SHugh Dickins } 202754af6042SHugh Dickins error = -EINVAL; 2028267a4c76SHugh Dickins goto unlock; 2029ff36b801SShaohua Li } 203063ec1973SMatthew Wilcox (Oracle) out: 2031fc26babbSMatthew Wilcox (Oracle) *foliop = folio; 203254af6042SHugh Dickins return 0; 2033d00806b1SNick Piggin 2034d0217ac0SNick Piggin /* 203554af6042SHugh Dickins * Error recovery. 20361da177e4SLinus Torvalds */ 203754af6042SHugh Dickins unacct: 2038b1d0ec3aSMatthew Wilcox (Oracle) shmem_inode_unacct_blocks(inode, folio_nr_pages(folio)); 2039800d8c63SKirill A. Shutemov 2040b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 2041b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2042b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 2043800d8c63SKirill A. Shutemov goto alloc_nohuge; 2044800d8c63SKirill A. Shutemov } 2045d1899228SHugh Dickins unlock: 2046b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 2047b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2048b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 204954af6042SHugh Dickins } 205054af6042SHugh Dickins if (error == -ENOSPC && !once++) { 20514595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 205254af6042SHugh Dickins shmem_recalc_inode(inode); 20534595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 20541da177e4SLinus Torvalds goto repeat; 2055d8dc74f2SAdrian Bunk } 20567f4446eeSMatthew Wilcox if (error == -EEXIST) 205754af6042SHugh Dickins goto repeat; 205854af6042SHugh Dickins return error; 20591da177e4SLinus Torvalds } 20601da177e4SLinus Torvalds 20614e1fc793SMatthew Wilcox (Oracle) int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **foliop, 20624e1fc793SMatthew Wilcox (Oracle) enum sgp_type sgp) 20634e1fc793SMatthew Wilcox (Oracle) { 20644e1fc793SMatthew Wilcox (Oracle) return shmem_get_folio_gfp(inode, index, foliop, sgp, 20654e1fc793SMatthew Wilcox (Oracle) mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 20664e1fc793SMatthew Wilcox (Oracle) } 20674e1fc793SMatthew Wilcox (Oracle) 206810d20bd2SLinus Torvalds /* 206910d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 207010d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 207110d20bd2SLinus Torvalds * target. 207210d20bd2SLinus Torvalds */ 2073ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 207410d20bd2SLinus Torvalds { 207510d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 20762055da97SIngo Molnar list_del_init(&wait->entry); 207710d20bd2SLinus Torvalds return ret; 207810d20bd2SLinus Torvalds } 207910d20bd2SLinus Torvalds 208020acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 20811da177e4SLinus Torvalds { 208211bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 2083496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 20849e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 208568a54100SMatthew Wilcox (Oracle) struct folio *folio = NULL; 208620acce67SSouptick Joarder int err; 208720acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 20881da177e4SLinus Torvalds 2089f00cdc6dSHugh Dickins /* 2090f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 2091f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 20929608703eSJan Kara * locks writers out with its hold on i_rwsem. So refrain from 20938e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 20948e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 20958e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 20968e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 20978e205f77SHugh Dickins * 20988e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 20998e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 21008e205f77SHugh Dickins * we just need to make racing faults a rare case. 21018e205f77SHugh Dickins * 21028e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 21039608703eSJan Kara * standard mutex or completion: but we cannot take i_rwsem in fault, 21048e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 2105f00cdc6dSHugh Dickins */ 2106f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 2107f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 2108f00cdc6dSHugh Dickins 2109f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2110f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 21118e205f77SHugh Dickins if (shmem_falloc && 21128e205f77SHugh Dickins shmem_falloc->waitq && 21138e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 21148e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 21158897c1b1SKirill A. Shutemov struct file *fpin; 21168e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 211710d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 21188e205f77SHugh Dickins 21198e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 21208897c1b1SKirill A. Shutemov fpin = maybe_unlock_mmap_for_io(vmf, NULL); 21218897c1b1SKirill A. Shutemov if (fpin) 21228e205f77SHugh Dickins ret = VM_FAULT_RETRY; 21238e205f77SHugh Dickins 21248e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 21258e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 21268e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 21278e205f77SHugh Dickins spin_unlock(&inode->i_lock); 21288e205f77SHugh Dickins schedule(); 21298e205f77SHugh Dickins 21308e205f77SHugh Dickins /* 21318e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 21328e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 21338e205f77SHugh Dickins * is usually invalid by the time we reach here, but 21348e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 21358e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 21368e205f77SHugh Dickins */ 21378e205f77SHugh Dickins spin_lock(&inode->i_lock); 21388e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 21398e205f77SHugh Dickins spin_unlock(&inode->i_lock); 21408897c1b1SKirill A. Shutemov 21418897c1b1SKirill A. Shutemov if (fpin) 21428897c1b1SKirill A. Shutemov fput(fpin); 21438e205f77SHugh Dickins return ret; 2144f00cdc6dSHugh Dickins } 21458e205f77SHugh Dickins spin_unlock(&inode->i_lock); 2146f00cdc6dSHugh Dickins } 2147f00cdc6dSHugh Dickins 214868a54100SMatthew Wilcox (Oracle) err = shmem_get_folio_gfp(inode, vmf->pgoff, &folio, SGP_CACHE, 2149cfda0526SMike Rapoport gfp, vma, vmf, &ret); 215020acce67SSouptick Joarder if (err) 215120acce67SSouptick Joarder return vmf_error(err); 215268a54100SMatthew Wilcox (Oracle) if (folio) 215368a54100SMatthew Wilcox (Oracle) vmf->page = folio_file_page(folio, vmf->pgoff); 215468da9f05SHugh Dickins return ret; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds 2157c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 2158c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 2159c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 2160c01d5b30SHugh Dickins { 2161c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2162c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2163c01d5b30SHugh Dickins unsigned long addr; 2164c01d5b30SHugh Dickins unsigned long offset; 2165c01d5b30SHugh Dickins unsigned long inflated_len; 2166c01d5b30SHugh Dickins unsigned long inflated_addr; 2167c01d5b30SHugh Dickins unsigned long inflated_offset; 2168c01d5b30SHugh Dickins 2169c01d5b30SHugh Dickins if (len > TASK_SIZE) 2170c01d5b30SHugh Dickins return -ENOMEM; 2171c01d5b30SHugh Dickins 2172c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2173c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2174c01d5b30SHugh Dickins 2175396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 2176c01d5b30SHugh Dickins return addr; 2177c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2178c01d5b30SHugh Dickins return addr; 2179c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2180c01d5b30SHugh Dickins return addr; 2181c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2182c01d5b30SHugh Dickins return addr; 2183c01d5b30SHugh Dickins 2184c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2185c01d5b30SHugh Dickins return addr; 2186c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2187c01d5b30SHugh Dickins return addr; 2188c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2189c01d5b30SHugh Dickins return addr; 2190c01d5b30SHugh Dickins /* 2191c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2192c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 219399158997SKirill A. Shutemov * But if caller specified an address hint and we allocated area there 219499158997SKirill A. Shutemov * successfully, respect that as before. 2195c01d5b30SHugh Dickins */ 219699158997SKirill A. Shutemov if (uaddr == addr) 2197c01d5b30SHugh Dickins return addr; 2198c01d5b30SHugh Dickins 2199c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2200c01d5b30SHugh Dickins struct super_block *sb; 2201c01d5b30SHugh Dickins 2202c01d5b30SHugh Dickins if (file) { 2203c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2204c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2205c01d5b30SHugh Dickins } else { 2206c01d5b30SHugh Dickins /* 2207c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2208c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2209c01d5b30SHugh Dickins */ 2210c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2211c01d5b30SHugh Dickins return addr; 2212c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2213c01d5b30SHugh Dickins } 22143089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2215c01d5b30SHugh Dickins return addr; 2216c01d5b30SHugh Dickins } 2217c01d5b30SHugh Dickins 2218c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2219c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2220c01d5b30SHugh Dickins return addr; 2221c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2222c01d5b30SHugh Dickins return addr; 2223c01d5b30SHugh Dickins 2224c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2225c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2226c01d5b30SHugh Dickins return addr; 2227c01d5b30SHugh Dickins if (inflated_len < len) 2228c01d5b30SHugh Dickins return addr; 2229c01d5b30SHugh Dickins 223099158997SKirill A. Shutemov inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); 2231c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2232c01d5b30SHugh Dickins return addr; 2233c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2234c01d5b30SHugh Dickins return addr; 2235c01d5b30SHugh Dickins 2236c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2237c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2238c01d5b30SHugh Dickins if (inflated_offset > offset) 2239c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2240c01d5b30SHugh Dickins 2241c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2242c01d5b30SHugh Dickins return addr; 2243c01d5b30SHugh Dickins return inflated_addr; 2244c01d5b30SHugh Dickins } 2245c01d5b30SHugh Dickins 22461da177e4SLinus Torvalds #ifdef CONFIG_NUMA 224741ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 22481da177e4SLinus Torvalds { 2249496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 225041ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 2253d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2254d8dc74f2SAdrian Bunk unsigned long addr) 22551da177e4SLinus Torvalds { 2256496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 225741ffe5d5SHugh Dickins pgoff_t index; 22581da177e4SLinus Torvalds 225941ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 226041ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds #endif 22631da177e4SLinus Torvalds 2264d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 22651da177e4SLinus Torvalds { 2266496ad9aaSAl Viro struct inode *inode = file_inode(file); 22671da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 22681da177e4SLinus Torvalds int retval = -ENOMEM; 22691da177e4SLinus Torvalds 2270ea0dfeb4SHugh Dickins /* 2271ea0dfeb4SHugh Dickins * What serializes the accesses to info->flags? 2272ea0dfeb4SHugh Dickins * ipc_lock_object() when called from shmctl_do_lock(), 2273ea0dfeb4SHugh Dickins * no serialization needed when called from shm_destroy(). 2274ea0dfeb4SHugh Dickins */ 22751da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 2276d7c9e99aSAlexey Gladkov if (!user_shm_lock(inode->i_size, ucounts)) 22771da177e4SLinus Torvalds goto out_nomem; 22781da177e4SLinus Torvalds info->flags |= VM_LOCKED; 227989e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 22801da177e4SLinus Torvalds } 2281d7c9e99aSAlexey Gladkov if (!lock && (info->flags & VM_LOCKED) && ucounts) { 2282d7c9e99aSAlexey Gladkov user_shm_unlock(inode->i_size, ucounts); 22831da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 228489e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds retval = 0; 228789e004eaSLee Schermerhorn 22881da177e4SLinus Torvalds out_nomem: 22891da177e4SLinus Torvalds return retval; 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds 22929b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 22931da177e4SLinus Torvalds { 2294d09e8ca6SPasha Tatashin struct inode *inode = file_inode(file); 2295d09e8ca6SPasha Tatashin struct shmem_inode_info *info = SHMEM_I(inode); 229622247efdSPeter Xu int ret; 2297ab3948f5SJoel Fernandes (Google) 229822247efdSPeter Xu ret = seal_check_future_write(info->seals, vma); 229922247efdSPeter Xu if (ret) 230022247efdSPeter Xu return ret; 2301ab3948f5SJoel Fernandes (Google) 230251b0bff2SCatalin Marinas /* arm64 - allow memory tagging on RAM-based files */ 23031c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MTE_ALLOWED); 230451b0bff2SCatalin Marinas 23051da177e4SLinus Torvalds file_accessed(file); 2306d09e8ca6SPasha Tatashin /* This is anonymous shared memory if it is unlinked at the time of mmap */ 2307d09e8ca6SPasha Tatashin if (inode->i_nlink) 23081da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2309d09e8ca6SPasha Tatashin else 2310d09e8ca6SPasha Tatashin vma->vm_ops = &shmem_anon_vm_ops; 23111da177e4SLinus Torvalds return 0; 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 2314cb241339SHugh Dickins #ifdef CONFIG_TMPFS_XATTR 2315cb241339SHugh Dickins static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 2316cb241339SHugh Dickins 2317cb241339SHugh Dickins /* 2318cb241339SHugh Dickins * chattr's fsflags are unrelated to extended attributes, 2319cb241339SHugh Dickins * but tmpfs has chosen to enable them under the same config option. 2320cb241339SHugh Dickins */ 2321cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2322e408e695STheodore Ts'o { 2323cb241339SHugh Dickins unsigned int i_flags = 0; 2324cb241339SHugh Dickins 2325cb241339SHugh Dickins if (fsflags & FS_NOATIME_FL) 2326cb241339SHugh Dickins i_flags |= S_NOATIME; 2327cb241339SHugh Dickins if (fsflags & FS_APPEND_FL) 2328cb241339SHugh Dickins i_flags |= S_APPEND; 2329cb241339SHugh Dickins if (fsflags & FS_IMMUTABLE_FL) 2330cb241339SHugh Dickins i_flags |= S_IMMUTABLE; 2331cb241339SHugh Dickins /* 2332cb241339SHugh Dickins * But FS_NODUMP_FL does not require any action in i_flags. 2333cb241339SHugh Dickins */ 2334cb241339SHugh Dickins inode_set_flags(inode, i_flags, S_NOATIME | S_APPEND | S_IMMUTABLE); 2335e408e695STheodore Ts'o } 2336cb241339SHugh Dickins #else 2337cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2338cb241339SHugh Dickins { 2339cb241339SHugh Dickins } 2340cb241339SHugh Dickins #define shmem_initxattrs NULL 2341cb241339SHugh Dickins #endif 2342e408e695STheodore Ts'o 23437a80e5b8SGiuseppe Scrivano static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block *sb, 23447a80e5b8SGiuseppe Scrivano struct inode *dir, umode_t mode, dev_t dev, 23457a80e5b8SGiuseppe Scrivano unsigned long flags) 23461da177e4SLinus Torvalds { 23471da177e4SLinus Torvalds struct inode *inode; 23481da177e4SLinus Torvalds struct shmem_inode_info *info; 23491da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2350e809d5f0SChris Down ino_t ino; 23511da177e4SLinus Torvalds 2352e809d5f0SChris Down if (shmem_reserve_inode(sb, &ino)) 23531da177e4SLinus Torvalds return NULL; 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds inode = new_inode(sb); 23561da177e4SLinus Torvalds if (inode) { 2357e809d5f0SChris Down inode->i_ino = ino; 23587a80e5b8SGiuseppe Scrivano inode_init_owner(idmap, inode, dir, mode); 23591da177e4SLinus Torvalds inode->i_blocks = 0; 2360078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2361a251c17aSJason A. Donenfeld inode->i_generation = get_random_u32(); 23621da177e4SLinus Torvalds info = SHMEM_I(inode); 23631da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 23641da177e4SLinus Torvalds spin_lock_init(&info->lock); 2365af53d3e9SHugh Dickins atomic_set(&info->stop_eviction, 0); 236640e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 23670b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2368f7cd16a5SXavier Roche info->i_crtime = inode->i_mtime; 2369e408e695STheodore Ts'o info->fsflags = (dir == NULL) ? 0 : 2370e408e695STheodore Ts'o SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; 2371cb241339SHugh Dickins if (info->fsflags) 2372cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 2373779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 23741da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 237538f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 237672c04902SAl Viro cache_no_acl(inode); 2377ff36da69SMatthew Wilcox (Oracle) mapping_set_large_folios(inode->i_mapping); 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds switch (mode & S_IFMT) { 23801da177e4SLinus Torvalds default: 238139f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 23821da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 23831da177e4SLinus Torvalds break; 23841da177e4SLinus Torvalds case S_IFREG: 238514fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 23861da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 23871da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 238871fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 238971fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 23901da177e4SLinus Torvalds break; 23911da177e4SLinus Torvalds case S_IFDIR: 2392d8c76e6fSDave Hansen inc_nlink(inode); 23931da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 23941da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 23951da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 23961da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 23971da177e4SLinus Torvalds break; 23981da177e4SLinus Torvalds case S_IFLNK: 23991da177e4SLinus Torvalds /* 24001da177e4SLinus Torvalds * Must not load anything in the rbtree, 24011da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 24021da177e4SLinus Torvalds */ 240371fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 24041da177e4SLinus Torvalds break; 24051da177e4SLinus Torvalds } 2406b45d71fbSJoel Fernandes (Google) 2407b45d71fbSJoel Fernandes (Google) lockdep_annotate_inode_mutex_key(inode); 24085b04c689SPavel Emelyanov } else 24095b04c689SPavel Emelyanov shmem_free_inode(sb); 24101da177e4SLinus Torvalds return inode; 24111da177e4SLinus Torvalds } 24121da177e4SLinus Torvalds 24133460f6e5SAxel Rasmussen #ifdef CONFIG_USERFAULTFD 24143460f6e5SAxel Rasmussen int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, 24154c27fe4cSMike Rapoport pmd_t *dst_pmd, 24164c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 24174c27fe4cSMike Rapoport unsigned long dst_addr, 24184c27fe4cSMike Rapoport unsigned long src_addr, 24198ee79edfSPeter Xu bool zeropage, bool wp_copy, 24204c27fe4cSMike Rapoport struct page **pagep) 24214c27fe4cSMike Rapoport { 24224c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 24234c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 24244c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 24254c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 24264c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 24274c27fe4cSMike Rapoport void *page_kaddr; 2428b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 24294c27fe4cSMike Rapoport int ret; 24303460f6e5SAxel Rasmussen pgoff_t max_off; 24314c27fe4cSMike Rapoport 24327ed9d238SAxel Rasmussen if (!shmem_inode_acct_block(inode, 1)) { 24337ed9d238SAxel Rasmussen /* 24347ed9d238SAxel Rasmussen * We may have got a page, returned -ENOENT triggering a retry, 24357ed9d238SAxel Rasmussen * and now we find ourselves with -ENOMEM. Release the page, to 24367ed9d238SAxel Rasmussen * avoid a BUG_ON in our caller. 24377ed9d238SAxel Rasmussen */ 24387ed9d238SAxel Rasmussen if (unlikely(*pagep)) { 24397ed9d238SAxel Rasmussen put_page(*pagep); 24407ed9d238SAxel Rasmussen *pagep = NULL; 24417ed9d238SAxel Rasmussen } 24427d64ae3aSAxel Rasmussen return -ENOMEM; 24437ed9d238SAxel Rasmussen } 24444c27fe4cSMike Rapoport 2445cb658a45SAndrea Arcangeli if (!*pagep) { 24467d64ae3aSAxel Rasmussen ret = -ENOMEM; 24477a7256d5SMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, pgoff); 24487a7256d5SMatthew Wilcox (Oracle) if (!folio) 24490f079694SMike Rapoport goto out_unacct_blocks; 24504c27fe4cSMike Rapoport 24513460f6e5SAxel Rasmussen if (!zeropage) { /* COPY */ 24527a7256d5SMatthew Wilcox (Oracle) page_kaddr = kmap_local_folio(folio, 0); 24535dc21f0cSIra Weiny /* 24545dc21f0cSIra Weiny * The read mmap_lock is held here. Despite the 24555dc21f0cSIra Weiny * mmap_lock being read recursive a deadlock is still 24565dc21f0cSIra Weiny * possible if a writer has taken a lock. For example: 24575dc21f0cSIra Weiny * 24585dc21f0cSIra Weiny * process A thread 1 takes read lock on own mmap_lock 24595dc21f0cSIra Weiny * process A thread 2 calls mmap, blocks taking write lock 24605dc21f0cSIra Weiny * process B thread 1 takes page fault, read lock on own mmap lock 24615dc21f0cSIra Weiny * process B thread 2 calls mmap, blocks taking write lock 24625dc21f0cSIra Weiny * process A thread 1 blocks taking read lock on process B 24635dc21f0cSIra Weiny * process B thread 1 blocks taking read lock on process A 24645dc21f0cSIra Weiny * 24655dc21f0cSIra Weiny * Disable page faults to prevent potential deadlock 24665dc21f0cSIra Weiny * and retry the copy outside the mmap_lock. 24675dc21f0cSIra Weiny */ 24685dc21f0cSIra Weiny pagefault_disable(); 24698d103963SMike Rapoport ret = copy_from_user(page_kaddr, 24708d103963SMike Rapoport (const void __user *)src_addr, 24714c27fe4cSMike Rapoport PAGE_SIZE); 24725dc21f0cSIra Weiny pagefault_enable(); 24737a7256d5SMatthew Wilcox (Oracle) kunmap_local(page_kaddr); 24744c27fe4cSMike Rapoport 2475c1e8d7c6SMichel Lespinasse /* fallback to copy_from_user outside mmap_lock */ 24764c27fe4cSMike Rapoport if (unlikely(ret)) { 24777a7256d5SMatthew Wilcox (Oracle) *pagep = &folio->page; 24787d64ae3aSAxel Rasmussen ret = -ENOENT; 24794c27fe4cSMike Rapoport /* don't free the page */ 24807d64ae3aSAxel Rasmussen goto out_unacct_blocks; 24814c27fe4cSMike Rapoport } 248219b482c2SMuchun Song 24837a7256d5SMatthew Wilcox (Oracle) flush_dcache_folio(folio); 24843460f6e5SAxel Rasmussen } else { /* ZEROPAGE */ 24857a7256d5SMatthew Wilcox (Oracle) clear_user_highpage(&folio->page, dst_addr); 24868d103963SMike Rapoport } 24874c27fe4cSMike Rapoport } else { 24887a7256d5SMatthew Wilcox (Oracle) folio = page_folio(*pagep); 24897a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(folio), folio); 24904c27fe4cSMike Rapoport *pagep = NULL; 24914c27fe4cSMike Rapoport } 24924c27fe4cSMike Rapoport 24937a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_locked(folio)); 24947a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_swapbacked(folio)); 24957a7256d5SMatthew Wilcox (Oracle) __folio_set_locked(folio); 24967a7256d5SMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 24977a7256d5SMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 24989cc90c66SAndrea Arcangeli 2499e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2500e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 25013460f6e5SAxel Rasmussen if (unlikely(pgoff >= max_off)) 2502e2a50c1fSAndrea Arcangeli goto out_release; 2503e2a50c1fSAndrea Arcangeli 2504b7dd44a1SMatthew Wilcox (Oracle) ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, 25053fea5a49SJohannes Weiner gfp & GFP_RECLAIM_MASK, dst_mm); 25064c27fe4cSMike Rapoport if (ret) 25074c27fe4cSMike Rapoport goto out_release; 25084c27fe4cSMike Rapoport 25097d64ae3aSAxel Rasmussen ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 25107a7256d5SMatthew Wilcox (Oracle) &folio->page, true, wp_copy); 25117d64ae3aSAxel Rasmussen if (ret) 25127d64ae3aSAxel Rasmussen goto out_delete_from_cache; 25134c27fe4cSMike Rapoport 251494b7cc01SYang Shi spin_lock_irq(&info->lock); 25154c27fe4cSMike Rapoport info->alloced++; 25164c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 25174c27fe4cSMike Rapoport shmem_recalc_inode(inode); 251894b7cc01SYang Shi spin_unlock_irq(&info->lock); 25194c27fe4cSMike Rapoport 25207a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 25217d64ae3aSAxel Rasmussen return 0; 25227d64ae3aSAxel Rasmussen out_delete_from_cache: 25237a7256d5SMatthew Wilcox (Oracle) filemap_remove_folio(folio); 25244c27fe4cSMike Rapoport out_release: 25257a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 25267a7256d5SMatthew Wilcox (Oracle) folio_put(folio); 25274c27fe4cSMike Rapoport out_unacct_blocks: 25280f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 25297d64ae3aSAxel Rasmussen return ret; 25304c27fe4cSMike Rapoport } 25313460f6e5SAxel Rasmussen #endif /* CONFIG_USERFAULTFD */ 25328d103963SMike Rapoport 25331da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 253492e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 253569f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds static int 2538800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 25399d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 2540800d15a5SNick Piggin struct page **pagep, void **fsdata) 25411da177e4SLinus Torvalds { 2542800d15a5SNick Piggin struct inode *inode = mapping->host; 254340e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 254409cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 2545eff1f906SMatthew Wilcox (Oracle) struct folio *folio; 2546a7605426SYang Shi int ret = 0; 254740e041a2SDavid Herrmann 25489608703eSJan Kara /* i_rwsem is held by caller */ 2549ab3948f5SJoel Fernandes (Google) if (unlikely(info->seals & (F_SEAL_GROW | 2550ab3948f5SJoel Fernandes (Google) F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { 2551ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) 255240e041a2SDavid Herrmann return -EPERM; 255340e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 255440e041a2SDavid Herrmann return -EPERM; 255540e041a2SDavid Herrmann } 255640e041a2SDavid Herrmann 2557eff1f906SMatthew Wilcox (Oracle) ret = shmem_get_folio(inode, index, &folio, SGP_WRITE); 2558a7605426SYang Shi 2559a7605426SYang Shi if (ret) 2560a7605426SYang Shi return ret; 2561a7605426SYang Shi 2562eff1f906SMatthew Wilcox (Oracle) *pagep = folio_file_page(folio, index); 2563a7605426SYang Shi if (PageHWPoison(*pagep)) { 2564eff1f906SMatthew Wilcox (Oracle) folio_unlock(folio); 2565eff1f906SMatthew Wilcox (Oracle) folio_put(folio); 2566a7605426SYang Shi *pagep = NULL; 2567a7605426SYang Shi return -EIO; 2568a7605426SYang Shi } 2569a7605426SYang Shi 2570a7605426SYang Shi return 0; 2571800d15a5SNick Piggin } 2572800d15a5SNick Piggin 2573800d15a5SNick Piggin static int 2574800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2575800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2576800d15a5SNick Piggin struct page *page, void *fsdata) 2577800d15a5SNick Piggin { 257869bbb87bSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 2579800d15a5SNick Piggin struct inode *inode = mapping->host; 2580800d15a5SNick Piggin 2581800d15a5SNick Piggin if (pos + copied > inode->i_size) 2582800d15a5SNick Piggin i_size_write(inode, pos + copied); 2583800d15a5SNick Piggin 258469bbb87bSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 258569bbb87bSMatthew Wilcox (Oracle) if (copied < folio_size(folio)) { 258669bbb87bSMatthew Wilcox (Oracle) size_t from = offset_in_folio(folio, pos); 258769bbb87bSMatthew Wilcox (Oracle) folio_zero_segments(folio, 0, from, 258869bbb87bSMatthew Wilcox (Oracle) from + copied, folio_size(folio)); 2589800d8c63SKirill A. Shutemov } 259069bbb87bSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 2591800d8c63SKirill A. Shutemov } 259269bbb87bSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 259369bbb87bSMatthew Wilcox (Oracle) folio_unlock(folio); 259469bbb87bSMatthew Wilcox (Oracle) folio_put(folio); 2595d3602444SHugh Dickins 2596800d15a5SNick Piggin return copied; 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 25992ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 26001da177e4SLinus Torvalds { 26016e58e79dSAl Viro struct file *file = iocb->ki_filp; 26026e58e79dSAl Viro struct inode *inode = file_inode(file); 26031da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 260441ffe5d5SHugh Dickins pgoff_t index; 260541ffe5d5SHugh Dickins unsigned long offset; 2606f7c1d074SGeert Uytterhoeven int error = 0; 2607cb66a7a1SAl Viro ssize_t retval = 0; 26086e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2609a0ee5ec5SHugh Dickins 261009cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 261109cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds for (;;) { 26144601e2fcSMatthew Wilcox (Oracle) struct folio *folio = NULL; 26151da177e4SLinus Torvalds struct page *page = NULL; 261641ffe5d5SHugh Dickins pgoff_t end_index; 261741ffe5d5SHugh Dickins unsigned long nr, ret; 26181da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 26191da177e4SLinus Torvalds 262009cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 26211da177e4SLinus Torvalds if (index > end_index) 26221da177e4SLinus Torvalds break; 26231da177e4SLinus Torvalds if (index == end_index) { 262409cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 26251da177e4SLinus Torvalds if (nr <= offset) 26261da177e4SLinus Torvalds break; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds 26294601e2fcSMatthew Wilcox (Oracle) error = shmem_get_folio(inode, index, &folio, SGP_READ); 26306e58e79dSAl Viro if (error) { 26316e58e79dSAl Viro if (error == -EINVAL) 26326e58e79dSAl Viro error = 0; 26331da177e4SLinus Torvalds break; 26341da177e4SLinus Torvalds } 26354601e2fcSMatthew Wilcox (Oracle) if (folio) { 26364601e2fcSMatthew Wilcox (Oracle) folio_unlock(folio); 2637a7605426SYang Shi 26384601e2fcSMatthew Wilcox (Oracle) page = folio_file_page(folio, index); 2639a7605426SYang Shi if (PageHWPoison(page)) { 26404601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 2641a7605426SYang Shi error = -EIO; 2642a7605426SYang Shi break; 2643a7605426SYang Shi } 264475edd345SHugh Dickins } 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds /* 26471da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 26489608703eSJan Kara * are called without i_rwsem protection against truncate 26491da177e4SLinus Torvalds */ 265009cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 26511da177e4SLinus Torvalds i_size = i_size_read(inode); 265209cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 26531da177e4SLinus Torvalds if (index == end_index) { 265409cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 26551da177e4SLinus Torvalds if (nr <= offset) { 26564601e2fcSMatthew Wilcox (Oracle) if (folio) 26574601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 26581da177e4SLinus Torvalds break; 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds } 26611da177e4SLinus Torvalds nr -= offset; 26621da177e4SLinus Torvalds 26634601e2fcSMatthew Wilcox (Oracle) if (folio) { 26641da177e4SLinus Torvalds /* 26651da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 26661da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 26671da177e4SLinus Torvalds * before reading the page on the kernel side. 26681da177e4SLinus Torvalds */ 26691da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 26701da177e4SLinus Torvalds flush_dcache_page(page); 26711da177e4SLinus Torvalds /* 26721da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 26731da177e4SLinus Torvalds */ 26741da177e4SLinus Torvalds if (!offset) 26754601e2fcSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 26761da177e4SLinus Torvalds /* 26771da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 26781da177e4SLinus Torvalds * now we can copy it to user space... 26791da177e4SLinus Torvalds */ 26802ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 26814601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 26821bdec44bSHugh Dickins 2683fcb14cb1SAl Viro } else if (user_backed_iter(to)) { 26841bdec44bSHugh Dickins /* 26851bdec44bSHugh Dickins * Copy to user tends to be so well optimized, but 26861bdec44bSHugh Dickins * clear_user() not so much, that it is noticeably 26871bdec44bSHugh Dickins * faster to copy the zero page instead of clearing. 26881bdec44bSHugh Dickins */ 26891bdec44bSHugh Dickins ret = copy_page_to_iter(ZERO_PAGE(0), offset, nr, to); 26901bdec44bSHugh Dickins } else { 26911bdec44bSHugh Dickins /* 26921bdec44bSHugh Dickins * But submitting the same page twice in a row to 26931bdec44bSHugh Dickins * splice() - or others? - can result in confusion: 26941bdec44bSHugh Dickins * so don't attempt that optimization on pipes etc. 26951bdec44bSHugh Dickins */ 26961bdec44bSHugh Dickins ret = iov_iter_zero(nr, to); 26971bdec44bSHugh Dickins } 26981bdec44bSHugh Dickins 26996e58e79dSAl Viro retval += ret; 27001da177e4SLinus Torvalds offset += ret; 270109cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 270209cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 27031da177e4SLinus Torvalds 27042ba5bbedSAl Viro if (!iov_iter_count(to)) 27051da177e4SLinus Torvalds break; 27066e58e79dSAl Viro if (ret < nr) { 27076e58e79dSAl Viro error = -EFAULT; 27086e58e79dSAl Viro break; 27096e58e79dSAl Viro } 27101da177e4SLinus Torvalds cond_resched(); 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds 271309cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 27146e58e79dSAl Viro file_accessed(file); 27156e58e79dSAl Viro return retval ? retval : error; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 2718965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2719220f2ac9SHugh Dickins { 2720220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2721220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2722220f2ac9SHugh Dickins 2723965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2724965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2725220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 272641139aa4SMatthew Wilcox (Oracle) if (offset < 0) 272741139aa4SMatthew Wilcox (Oracle) return -ENXIO; 272841139aa4SMatthew Wilcox (Oracle) 27295955102cSAl Viro inode_lock(inode); 27309608703eSJan Kara /* We're holding i_rwsem so we can access i_size directly */ 273141139aa4SMatthew Wilcox (Oracle) offset = mapping_seek_hole_data(mapping, offset, inode->i_size, whence); 2732387aae6fSHugh Dickins if (offset >= 0) 273346a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 27345955102cSAl Viro inode_unlock(inode); 2735220f2ac9SHugh Dickins return offset; 2736220f2ac9SHugh Dickins } 2737220f2ac9SHugh Dickins 273883e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 273983e4fa9cSHugh Dickins loff_t len) 274083e4fa9cSHugh Dickins { 2741496ad9aaSAl Viro struct inode *inode = file_inode(file); 2742e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 274340e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 27441aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2745d144bf62SHugh Dickins pgoff_t start, index, end, undo_fallocend; 2746e2d12e22SHugh Dickins int error; 274783e4fa9cSHugh Dickins 274813ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 274913ace4d0SHugh Dickins return -EOPNOTSUPP; 275013ace4d0SHugh Dickins 27515955102cSAl Viro inode_lock(inode); 275283e4fa9cSHugh Dickins 275383e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 275483e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 275583e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 275683e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 27578e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 275883e4fa9cSHugh Dickins 27599608703eSJan Kara /* protected by i_rwsem */ 2760ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { 276140e041a2SDavid Herrmann error = -EPERM; 276240e041a2SDavid Herrmann goto out; 276340e041a2SDavid Herrmann } 276440e041a2SDavid Herrmann 27658e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2766aa71ecd8SChen Jun shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; 2767f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2768f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2769f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2770f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2771f00cdc6dSHugh Dickins 277283e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 277383e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 277483e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 277583e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 277683e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 27778e205f77SHugh Dickins 27788e205f77SHugh Dickins spin_lock(&inode->i_lock); 27798e205f77SHugh Dickins inode->i_private = NULL; 27808e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 27812055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 27828e205f77SHugh Dickins spin_unlock(&inode->i_lock); 278383e4fa9cSHugh Dickins error = 0; 27848e205f77SHugh Dickins goto out; 278583e4fa9cSHugh Dickins } 278683e4fa9cSHugh Dickins 2787e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2788e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2789e2d12e22SHugh Dickins if (error) 2790e2d12e22SHugh Dickins goto out; 2791e2d12e22SHugh Dickins 279240e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 279340e041a2SDavid Herrmann error = -EPERM; 279440e041a2SDavid Herrmann goto out; 279540e041a2SDavid Herrmann } 279640e041a2SDavid Herrmann 279709cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 279809cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2799e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2800e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2801e2d12e22SHugh Dickins error = -ENOSPC; 2802e2d12e22SHugh Dickins goto out; 2803e2d12e22SHugh Dickins } 2804e2d12e22SHugh Dickins 28058e205f77SHugh Dickins shmem_falloc.waitq = NULL; 28061aac1400SHugh Dickins shmem_falloc.start = start; 28071aac1400SHugh Dickins shmem_falloc.next = start; 28081aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 28091aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 28101aac1400SHugh Dickins spin_lock(&inode->i_lock); 28111aac1400SHugh Dickins inode->i_private = &shmem_falloc; 28121aac1400SHugh Dickins spin_unlock(&inode->i_lock); 28131aac1400SHugh Dickins 2814d144bf62SHugh Dickins /* 2815d144bf62SHugh Dickins * info->fallocend is only relevant when huge pages might be 2816d144bf62SHugh Dickins * involved: to prevent split_huge_page() freeing fallocated 2817d144bf62SHugh Dickins * pages when FALLOC_FL_KEEP_SIZE committed beyond i_size. 2818d144bf62SHugh Dickins */ 2819d144bf62SHugh Dickins undo_fallocend = info->fallocend; 2820d144bf62SHugh Dickins if (info->fallocend < end) 2821d144bf62SHugh Dickins info->fallocend = end; 2822d144bf62SHugh Dickins 2823050dcb5cSHugh Dickins for (index = start; index < end; ) { 2824b0802b22SMatthew Wilcox (Oracle) struct folio *folio; 2825e2d12e22SHugh Dickins 2826e2d12e22SHugh Dickins /* 2827e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2828e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2829e2d12e22SHugh Dickins */ 2830e2d12e22SHugh Dickins if (signal_pending(current)) 2831e2d12e22SHugh Dickins error = -EINTR; 28321aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 28331aac1400SHugh Dickins error = -ENOMEM; 2834e2d12e22SHugh Dickins else 2835b0802b22SMatthew Wilcox (Oracle) error = shmem_get_folio(inode, index, &folio, 2836b0802b22SMatthew Wilcox (Oracle) SGP_FALLOC); 2837e2d12e22SHugh Dickins if (error) { 2838d144bf62SHugh Dickins info->fallocend = undo_fallocend; 2839b0802b22SMatthew Wilcox (Oracle) /* Remove the !uptodate folios we added */ 28407f556567SHugh Dickins if (index > start) { 28411635f6a7SHugh Dickins shmem_undo_range(inode, 284209cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2843b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 28447f556567SHugh Dickins } 28451aac1400SHugh Dickins goto undone; 2846e2d12e22SHugh Dickins } 2847e2d12e22SHugh Dickins 2848050dcb5cSHugh Dickins /* 2849050dcb5cSHugh Dickins * Here is a more important optimization than it appears: 2850b0802b22SMatthew Wilcox (Oracle) * a second SGP_FALLOC on the same large folio will clear it, 2851b0802b22SMatthew Wilcox (Oracle) * making it uptodate and un-undoable if we fail later. 2852050dcb5cSHugh Dickins */ 2853b0802b22SMatthew Wilcox (Oracle) index = folio_next_index(folio); 2854050dcb5cSHugh Dickins /* Beware 32-bit wraparound */ 2855050dcb5cSHugh Dickins if (!index) 2856050dcb5cSHugh Dickins index--; 2857050dcb5cSHugh Dickins 2858e2d12e22SHugh Dickins /* 28591aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 28601aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 28611aac1400SHugh Dickins */ 2862b0802b22SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) 2863050dcb5cSHugh Dickins shmem_falloc.nr_falloced += index - shmem_falloc.next; 2864050dcb5cSHugh Dickins shmem_falloc.next = index; 28651aac1400SHugh Dickins 28661aac1400SHugh Dickins /* 2867b0802b22SMatthew Wilcox (Oracle) * If !uptodate, leave it that way so that freeable folios 28681635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 2869b0802b22SMatthew Wilcox (Oracle) * But mark it dirty so that memory pressure will swap rather 2870b0802b22SMatthew Wilcox (Oracle) * than free the folios we are allocating (and SGP_CACHE folios 2871e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2872e2d12e22SHugh Dickins */ 2873b0802b22SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 2874b0802b22SMatthew Wilcox (Oracle) folio_unlock(folio); 2875b0802b22SMatthew Wilcox (Oracle) folio_put(folio); 2876e2d12e22SHugh Dickins cond_resched(); 2877e2d12e22SHugh Dickins } 2878e2d12e22SHugh Dickins 2879e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2880e2d12e22SHugh Dickins i_size_write(inode, offset + len); 28811aac1400SHugh Dickins undone: 28821aac1400SHugh Dickins spin_lock(&inode->i_lock); 28831aac1400SHugh Dickins inode->i_private = NULL; 28841aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2885e2d12e22SHugh Dickins out: 288615f242bbSHugh Dickins if (!error) 288715f242bbSHugh Dickins file_modified(file); 28885955102cSAl Viro inode_unlock(inode); 288983e4fa9cSHugh Dickins return error; 289083e4fa9cSHugh Dickins } 289183e4fa9cSHugh Dickins 2892726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 28931da177e4SLinus Torvalds { 2894726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 289709cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 28981da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 28990edd73b3SHugh Dickins if (sbinfo->max_blocks) { 29001da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 290141ffe5d5SHugh Dickins buf->f_bavail = 290241ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 290341ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 29040edd73b3SHugh Dickins } 29050edd73b3SHugh Dickins if (sbinfo->max_inodes) { 29061da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 29071da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 29081da177e4SLinus Torvalds } 29091da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 291059cda49eSAmir Goldstein 291159cda49eSAmir Goldstein buf->f_fsid = uuid_to_fsid(dentry->d_sb->s_uuid.b); 291259cda49eSAmir Goldstein 29131da177e4SLinus Torvalds return 0; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds /* 29171da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 29181da177e4SLinus Torvalds */ 29191da177e4SLinus Torvalds static int 29205ebb29beSChristian Brauner shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, 2921549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t dev) 29221da177e4SLinus Torvalds { 29230b0a0806SHugh Dickins struct inode *inode; 29241da177e4SLinus Torvalds int error = -ENOSPC; 29251da177e4SLinus Torvalds 29267a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, dev, VM_NORESERVE); 29271da177e4SLinus Torvalds if (inode) { 2928feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2929feda821eSChristoph Hellwig if (error) 2930feda821eSChristoph Hellwig goto out_iput; 29312a7dba39SEric Paris error = security_inode_init_security(inode, dir, 29329d8f13baSMimi Zohar &dentry->d_name, 29336d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2934feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2935feda821eSChristoph Hellwig goto out_iput; 293637ec43cdSMimi Zohar 2937718deb6bSAl Viro error = 0; 29381da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2939078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 294036f05cabSJeff Layton inode_inc_iversion(dir); 29411da177e4SLinus Torvalds d_instantiate(dentry, inode); 29421da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 29431da177e4SLinus Torvalds } 29441da177e4SLinus Torvalds return error; 2945feda821eSChristoph Hellwig out_iput: 2946feda821eSChristoph Hellwig iput(inode); 2947feda821eSChristoph Hellwig return error; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 295060545d0dSAl Viro static int 2951011e2b71SChristian Brauner shmem_tmpfile(struct mnt_idmap *idmap, struct inode *dir, 2952863f144fSMiklos Szeredi struct file *file, umode_t mode) 295360545d0dSAl Viro { 295460545d0dSAl Viro struct inode *inode; 295560545d0dSAl Viro int error = -ENOSPC; 295660545d0dSAl Viro 29577a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, 0, VM_NORESERVE); 295860545d0dSAl Viro if (inode) { 295960545d0dSAl Viro error = security_inode_init_security(inode, dir, 296060545d0dSAl Viro NULL, 296160545d0dSAl Viro shmem_initxattrs, NULL); 2962feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2963feda821eSChristoph Hellwig goto out_iput; 2964feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2965feda821eSChristoph Hellwig if (error) 2966feda821eSChristoph Hellwig goto out_iput; 2967863f144fSMiklos Szeredi d_tmpfile(file, inode); 296860545d0dSAl Viro } 2969863f144fSMiklos Szeredi return finish_open_simple(file, error); 2970feda821eSChristoph Hellwig out_iput: 2971feda821eSChristoph Hellwig iput(inode); 2972feda821eSChristoph Hellwig return error; 297360545d0dSAl Viro } 297460545d0dSAl Viro 2975c54bd91eSChristian Brauner static int shmem_mkdir(struct mnt_idmap *idmap, struct inode *dir, 2976549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 29771da177e4SLinus Torvalds { 29781da177e4SLinus Torvalds int error; 29791da177e4SLinus Torvalds 29807a80e5b8SGiuseppe Scrivano error = shmem_mknod(idmap, dir, dentry, mode | S_IFDIR, 0); 29817a80e5b8SGiuseppe Scrivano if (error) 29821da177e4SLinus Torvalds return error; 2983d8c76e6fSDave Hansen inc_nlink(dir); 29841da177e4SLinus Torvalds return 0; 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds 29876c960e68SChristian Brauner static int shmem_create(struct mnt_idmap *idmap, struct inode *dir, 2988549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 29891da177e4SLinus Torvalds { 29907a80e5b8SGiuseppe Scrivano return shmem_mknod(idmap, dir, dentry, mode | S_IFREG, 0); 29911da177e4SLinus Torvalds } 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds /* 29941da177e4SLinus Torvalds * Link a file.. 29951da177e4SLinus Torvalds */ 29961da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 29971da177e4SLinus Torvalds { 299875c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 299929b00e60SDarrick J. Wong int ret = 0; 30001da177e4SLinus Torvalds 30011da177e4SLinus Torvalds /* 30021da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 30031da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 30041da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 30051062af92SDarrick J. Wong * But if an O_TMPFILE file is linked into the tmpfs, the 30061062af92SDarrick J. Wong * first link must skip that, to get the accounting right. 30071da177e4SLinus Torvalds */ 30081062af92SDarrick J. Wong if (inode->i_nlink) { 3009e809d5f0SChris Down ret = shmem_reserve_inode(inode->i_sb, NULL); 30105b04c689SPavel Emelyanov if (ret) 30115b04c689SPavel Emelyanov goto out; 30121062af92SDarrick J. Wong } 30131da177e4SLinus Torvalds 30141da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3015078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 301636f05cabSJeff Layton inode_inc_iversion(dir); 3017d8c76e6fSDave Hansen inc_nlink(inode); 30187de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 30191da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 30201da177e4SLinus Torvalds d_instantiate(dentry, inode); 30215b04c689SPavel Emelyanov out: 30225b04c689SPavel Emelyanov return ret; 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds 30251da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 30261da177e4SLinus Torvalds { 302775c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 30281da177e4SLinus Torvalds 30295b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 30305b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 30311da177e4SLinus Torvalds 30321da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 3033078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 303436f05cabSJeff Layton inode_inc_iversion(dir); 30359a53c3a7SDave Hansen drop_nlink(inode); 30361da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 30371da177e4SLinus Torvalds return 0; 30381da177e4SLinus Torvalds } 30391da177e4SLinus Torvalds 30401da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 30411da177e4SLinus Torvalds { 30421da177e4SLinus Torvalds if (!simple_empty(dentry)) 30431da177e4SLinus Torvalds return -ENOTEMPTY; 30441da177e4SLinus Torvalds 304575c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 30469a53c3a7SDave Hansen drop_nlink(dir); 30471da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 30481da177e4SLinus Torvalds } 30491da177e4SLinus Torvalds 3050e18275aeSChristian Brauner static int shmem_whiteout(struct mnt_idmap *idmap, 3051549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry) 305246fdb794SMiklos Szeredi { 305346fdb794SMiklos Szeredi struct dentry *whiteout; 305446fdb794SMiklos Szeredi int error; 305546fdb794SMiklos Szeredi 305646fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 305746fdb794SMiklos Szeredi if (!whiteout) 305846fdb794SMiklos Szeredi return -ENOMEM; 305946fdb794SMiklos Szeredi 30607a80e5b8SGiuseppe Scrivano error = shmem_mknod(idmap, old_dir, whiteout, 306146fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 306246fdb794SMiklos Szeredi dput(whiteout); 306346fdb794SMiklos Szeredi if (error) 306446fdb794SMiklos Szeredi return error; 306546fdb794SMiklos Szeredi 306646fdb794SMiklos Szeredi /* 306746fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 306846fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 306946fdb794SMiklos Szeredi * 307046fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 307146fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 307246fdb794SMiklos Szeredi */ 307346fdb794SMiklos Szeredi d_rehash(whiteout); 307446fdb794SMiklos Szeredi return 0; 307546fdb794SMiklos Szeredi } 307646fdb794SMiklos Szeredi 30771da177e4SLinus Torvalds /* 30781da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 30791da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 30801da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 30811da177e4SLinus Torvalds * gets overwritten. 30821da177e4SLinus Torvalds */ 3083e18275aeSChristian Brauner static int shmem_rename2(struct mnt_idmap *idmap, 3084549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry, 3085549c7297SChristian Brauner struct inode *new_dir, struct dentry *new_dentry, 3086549c7297SChristian Brauner unsigned int flags) 30871da177e4SLinus Torvalds { 308875c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 30891da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 30901da177e4SLinus Torvalds 309146fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 30923b69ff51SMiklos Szeredi return -EINVAL; 30933b69ff51SMiklos Szeredi 309437456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 30956429e463SLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); 309637456771SMiklos Szeredi 30971da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 30981da177e4SLinus Torvalds return -ENOTEMPTY; 30991da177e4SLinus Torvalds 310046fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 310146fdb794SMiklos Szeredi int error; 310246fdb794SMiklos Szeredi 31037a80e5b8SGiuseppe Scrivano error = shmem_whiteout(idmap, old_dir, old_dentry); 310446fdb794SMiklos Szeredi if (error) 310546fdb794SMiklos Szeredi return error; 310646fdb794SMiklos Szeredi } 310746fdb794SMiklos Szeredi 310875c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 31091da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 3110b928095bSMiklos Szeredi if (they_are_dirs) { 311175c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 31129a53c3a7SDave Hansen drop_nlink(old_dir); 3113b928095bSMiklos Szeredi } 31141da177e4SLinus Torvalds } else if (they_are_dirs) { 31159a53c3a7SDave Hansen drop_nlink(old_dir); 3116d8c76e6fSDave Hansen inc_nlink(new_dir); 31171da177e4SLinus Torvalds } 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 31201da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 31211da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 31221da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3123078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 312436f05cabSJeff Layton inode_inc_iversion(old_dir); 312536f05cabSJeff Layton inode_inc_iversion(new_dir); 31261da177e4SLinus Torvalds return 0; 31271da177e4SLinus Torvalds } 31281da177e4SLinus Torvalds 31297a77db95SChristian Brauner static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, 3130549c7297SChristian Brauner struct dentry *dentry, const char *symname) 31311da177e4SLinus Torvalds { 31321da177e4SLinus Torvalds int error; 31331da177e4SLinus Torvalds int len; 31341da177e4SLinus Torvalds struct inode *inode; 31357ad0414bSMatthew Wilcox (Oracle) struct folio *folio; 31361da177e4SLinus Torvalds 31371da177e4SLinus Torvalds len = strlen(symname) + 1; 313809cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 31391da177e4SLinus Torvalds return -ENAMETOOLONG; 31401da177e4SLinus Torvalds 31417a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(idmap, dir->i_sb, dir, S_IFLNK | 0777, 0, 31420825a6f9SJoe Perches VM_NORESERVE); 31431da177e4SLinus Torvalds if (!inode) 31441da177e4SLinus Torvalds return -ENOSPC; 31451da177e4SLinus Torvalds 31469d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 31476d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3148343c3d7fSMateusz Nosek if (error && error != -EOPNOTSUPP) { 3149570bc1c2SStephen Smalley iput(inode); 3150570bc1c2SStephen Smalley return error; 3151570bc1c2SStephen Smalley } 3152570bc1c2SStephen Smalley 31531da177e4SLinus Torvalds inode->i_size = len-1; 315469f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 31553ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 31563ed47db3SAl Viro if (!inode->i_link) { 315769f07ec9SHugh Dickins iput(inode); 315869f07ec9SHugh Dickins return -ENOMEM; 315969f07ec9SHugh Dickins } 316069f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 31611da177e4SLinus Torvalds } else { 3162e8ecde25SAl Viro inode_nohighmem(inode); 31637ad0414bSMatthew Wilcox (Oracle) error = shmem_get_folio(inode, 0, &folio, SGP_WRITE); 31641da177e4SLinus Torvalds if (error) { 31651da177e4SLinus Torvalds iput(inode); 31661da177e4SLinus Torvalds return error; 31671da177e4SLinus Torvalds } 316814fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 31691da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 31707ad0414bSMatthew Wilcox (Oracle) memcpy(folio_address(folio), symname, len); 31717ad0414bSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 31727ad0414bSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 31737ad0414bSMatthew Wilcox (Oracle) folio_unlock(folio); 31747ad0414bSMatthew Wilcox (Oracle) folio_put(folio); 31751da177e4SLinus Torvalds } 31761da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3177078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 317836f05cabSJeff Layton inode_inc_iversion(dir); 31791da177e4SLinus Torvalds d_instantiate(dentry, inode); 31801da177e4SLinus Torvalds dget(dentry); 31811da177e4SLinus Torvalds return 0; 31821da177e4SLinus Torvalds } 31831da177e4SLinus Torvalds 3184fceef393SAl Viro static void shmem_put_link(void *arg) 3185fceef393SAl Viro { 3186e4b57722SMatthew Wilcox (Oracle) folio_mark_accessed(arg); 3187e4b57722SMatthew Wilcox (Oracle) folio_put(arg); 3188fceef393SAl Viro } 3189fceef393SAl Viro 31906b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3191fceef393SAl Viro struct inode *inode, 3192fceef393SAl Viro struct delayed_call *done) 31931da177e4SLinus Torvalds { 3194e4b57722SMatthew Wilcox (Oracle) struct folio *folio = NULL; 31956b255391SAl Viro int error; 3196e4b57722SMatthew Wilcox (Oracle) 31976a6c9904SAl Viro if (!dentry) { 3198e4b57722SMatthew Wilcox (Oracle) folio = filemap_get_folio(inode->i_mapping, 0); 3199e4b57722SMatthew Wilcox (Oracle) if (!folio) 32006b255391SAl Viro return ERR_PTR(-ECHILD); 32017459c149SMatthew Wilcox (Oracle) if (PageHWPoison(folio_page(folio, 0)) || 3202e4b57722SMatthew Wilcox (Oracle) !folio_test_uptodate(folio)) { 3203e4b57722SMatthew Wilcox (Oracle) folio_put(folio); 32046a6c9904SAl Viro return ERR_PTR(-ECHILD); 32056a6c9904SAl Viro } 32066a6c9904SAl Viro } else { 3207e4b57722SMatthew Wilcox (Oracle) error = shmem_get_folio(inode, 0, &folio, SGP_READ); 3208680baacbSAl Viro if (error) 3209680baacbSAl Viro return ERR_PTR(error); 3210e4b57722SMatthew Wilcox (Oracle) if (!folio) 3211a7605426SYang Shi return ERR_PTR(-ECHILD); 32127459c149SMatthew Wilcox (Oracle) if (PageHWPoison(folio_page(folio, 0))) { 3213e4b57722SMatthew Wilcox (Oracle) folio_unlock(folio); 3214e4b57722SMatthew Wilcox (Oracle) folio_put(folio); 3215a7605426SYang Shi return ERR_PTR(-ECHILD); 3216a7605426SYang Shi } 3217e4b57722SMatthew Wilcox (Oracle) folio_unlock(folio); 32181da177e4SLinus Torvalds } 3219e4b57722SMatthew Wilcox (Oracle) set_delayed_call(done, shmem_put_link, folio); 3220e4b57722SMatthew Wilcox (Oracle) return folio_address(folio); 32211da177e4SLinus Torvalds } 32221da177e4SLinus Torvalds 3223b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3224e408e695STheodore Ts'o 3225e408e695STheodore Ts'o static int shmem_fileattr_get(struct dentry *dentry, struct fileattr *fa) 3226e408e695STheodore Ts'o { 3227e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3228e408e695STheodore Ts'o 3229e408e695STheodore Ts'o fileattr_fill_flags(fa, info->fsflags & SHMEM_FL_USER_VISIBLE); 3230e408e695STheodore Ts'o 3231e408e695STheodore Ts'o return 0; 3232e408e695STheodore Ts'o } 3233e408e695STheodore Ts'o 32348782a9aeSChristian Brauner static int shmem_fileattr_set(struct mnt_idmap *idmap, 3235e408e695STheodore Ts'o struct dentry *dentry, struct fileattr *fa) 3236e408e695STheodore Ts'o { 3237e408e695STheodore Ts'o struct inode *inode = d_inode(dentry); 3238e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(inode); 3239e408e695STheodore Ts'o 3240e408e695STheodore Ts'o if (fileattr_has_fsx(fa)) 3241e408e695STheodore Ts'o return -EOPNOTSUPP; 3242cb241339SHugh Dickins if (fa->flags & ~SHMEM_FL_USER_MODIFIABLE) 3243cb241339SHugh Dickins return -EOPNOTSUPP; 3244e408e695STheodore Ts'o 3245e408e695STheodore Ts'o info->fsflags = (info->fsflags & ~SHMEM_FL_USER_MODIFIABLE) | 3246e408e695STheodore Ts'o (fa->flags & SHMEM_FL_USER_MODIFIABLE); 3247e408e695STheodore Ts'o 3248cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 3249e408e695STheodore Ts'o inode->i_ctime = current_time(inode); 325036f05cabSJeff Layton inode_inc_iversion(inode); 3251e408e695STheodore Ts'o return 0; 3252e408e695STheodore Ts'o } 3253e408e695STheodore Ts'o 3254b09e0fa4SEric Paris /* 3255b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3256b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3257b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3258b09e0fa4SEric Paris * filesystem level, though. 3259b09e0fa4SEric Paris */ 3260b09e0fa4SEric Paris 32616d9d88d0SJarkko Sakkinen /* 32626d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 32636d9d88d0SJarkko Sakkinen */ 32646d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 32656d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 32666d9d88d0SJarkko Sakkinen void *fs_info) 32676d9d88d0SJarkko Sakkinen { 32686d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 32696d9d88d0SJarkko Sakkinen const struct xattr *xattr; 327038f38657SAristeu Rozanski struct simple_xattr *new_xattr; 32716d9d88d0SJarkko Sakkinen size_t len; 32726d9d88d0SJarkko Sakkinen 32736d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 327438f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 32756d9d88d0SJarkko Sakkinen if (!new_xattr) 32766d9d88d0SJarkko Sakkinen return -ENOMEM; 32776d9d88d0SJarkko Sakkinen 32786d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 32796d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 32806d9d88d0SJarkko Sakkinen GFP_KERNEL); 32816d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 32823bef735aSChengguang Xu kvfree(new_xattr); 32836d9d88d0SJarkko Sakkinen return -ENOMEM; 32846d9d88d0SJarkko Sakkinen } 32856d9d88d0SJarkko Sakkinen 32866d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 32876d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 32886d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 32896d9d88d0SJarkko Sakkinen xattr->name, len); 32906d9d88d0SJarkko Sakkinen 32913b4c7bc0SChristian Brauner simple_xattr_add(&info->xattrs, new_xattr); 32926d9d88d0SJarkko Sakkinen } 32936d9d88d0SJarkko Sakkinen 32946d9d88d0SJarkko Sakkinen return 0; 32956d9d88d0SJarkko Sakkinen } 32966d9d88d0SJarkko Sakkinen 3297aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3298b296821aSAl Viro struct dentry *unused, struct inode *inode, 3299b296821aSAl Viro const char *name, void *buffer, size_t size) 3300aa7c5241SAndreas Gruenbacher { 3301b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3302aa7c5241SAndreas Gruenbacher 3303aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3304aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3305aa7c5241SAndreas Gruenbacher } 3306aa7c5241SAndreas Gruenbacher 3307aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 330839f60c1cSChristian Brauner struct mnt_idmap *idmap, 330959301226SAl Viro struct dentry *unused, struct inode *inode, 331059301226SAl Viro const char *name, const void *value, 331159301226SAl Viro size_t size, int flags) 3312aa7c5241SAndreas Gruenbacher { 331359301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 331436f05cabSJeff Layton int err; 3315aa7c5241SAndreas Gruenbacher 3316aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 331736f05cabSJeff Layton err = simple_xattr_set(&info->xattrs, name, value, size, flags, NULL); 331836f05cabSJeff Layton if (!err) { 331936f05cabSJeff Layton inode->i_ctime = current_time(inode); 332036f05cabSJeff Layton inode_inc_iversion(inode); 332136f05cabSJeff Layton } 332236f05cabSJeff Layton return err; 3323aa7c5241SAndreas Gruenbacher } 3324aa7c5241SAndreas Gruenbacher 3325aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3326aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3327aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3328aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3329aa7c5241SAndreas Gruenbacher }; 3330aa7c5241SAndreas Gruenbacher 3331aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3332aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3333aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3334aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3335aa7c5241SAndreas Gruenbacher }; 3336aa7c5241SAndreas Gruenbacher 3337b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3338b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3339feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3340feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3341b09e0fa4SEric Paris #endif 3342aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3343aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3344b09e0fa4SEric Paris NULL 3345b09e0fa4SEric Paris }; 3346b09e0fa4SEric Paris 3347b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3348b09e0fa4SEric Paris { 334975c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3350786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3351b09e0fa4SEric Paris } 3352b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3353b09e0fa4SEric Paris 335469f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 3355f7cd16a5SXavier Roche .getattr = shmem_getattr, 33566b255391SAl Viro .get_link = simple_get_link, 3357b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3358b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3359b09e0fa4SEric Paris #endif 33601da177e4SLinus Torvalds }; 33611da177e4SLinus Torvalds 336292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 3363f7cd16a5SXavier Roche .getattr = shmem_getattr, 33646b255391SAl Viro .get_link = shmem_get_link, 3365b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3366b09e0fa4SEric Paris .listxattr = shmem_listxattr, 336739f0247dSAndreas Gruenbacher #endif 3368b09e0fa4SEric Paris }; 336939f0247dSAndreas Gruenbacher 337091828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 337191828a40SDavid M. Grimes { 337291828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 337391828a40SDavid M. Grimes } 337491828a40SDavid M. Grimes 337591828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 337691828a40SDavid M. Grimes { 337791828a40SDavid M. Grimes __u32 *fh = vfh; 337891828a40SDavid M. Grimes __u64 inum = fh[2]; 337991828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 338091828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 338191828a40SDavid M. Grimes } 338291828a40SDavid M. Grimes 338312ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 338412ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 338512ba780dSAmir Goldstein { 338612ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 338712ba780dSAmir Goldstein 338812ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 338912ba780dSAmir Goldstein } 339012ba780dSAmir Goldstein 339112ba780dSAmir Goldstein 3392480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3393480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 339491828a40SDavid M. Grimes { 339591828a40SDavid M. Grimes struct inode *inode; 3396480b116cSChristoph Hellwig struct dentry *dentry = NULL; 339735c2a7f4SHugh Dickins u64 inum; 339891828a40SDavid M. Grimes 3399480b116cSChristoph Hellwig if (fh_len < 3) 3400480b116cSChristoph Hellwig return NULL; 3401480b116cSChristoph Hellwig 340235c2a7f4SHugh Dickins inum = fid->raw[2]; 340335c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 340435c2a7f4SHugh Dickins 3405480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3406480b116cSChristoph Hellwig shmem_match, fid->raw); 340791828a40SDavid M. Grimes if (inode) { 340812ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 340991828a40SDavid M. Grimes iput(inode); 341091828a40SDavid M. Grimes } 341191828a40SDavid M. Grimes 3412480b116cSChristoph Hellwig return dentry; 341391828a40SDavid M. Grimes } 341491828a40SDavid M. Grimes 3415b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3416b0b0382bSAl Viro struct inode *parent) 341791828a40SDavid M. Grimes { 34185fe0c237SAneesh Kumar K.V if (*len < 3) { 34195fe0c237SAneesh Kumar K.V *len = 3; 342094e07a75SNamjae Jeon return FILEID_INVALID; 34215fe0c237SAneesh Kumar K.V } 342291828a40SDavid M. Grimes 34231d3382cbSAl Viro if (inode_unhashed(inode)) { 342491828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 342591828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 342691828a40SDavid M. Grimes * time, we need a lock to ensure we only try 342791828a40SDavid M. Grimes * to do it once 342891828a40SDavid M. Grimes */ 342991828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 343091828a40SDavid M. Grimes spin_lock(&lock); 34311d3382cbSAl Viro if (inode_unhashed(inode)) 343291828a40SDavid M. Grimes __insert_inode_hash(inode, 343391828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 343491828a40SDavid M. Grimes spin_unlock(&lock); 343591828a40SDavid M. Grimes } 343691828a40SDavid M. Grimes 343791828a40SDavid M. Grimes fh[0] = inode->i_generation; 343891828a40SDavid M. Grimes fh[1] = inode->i_ino; 343991828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 344091828a40SDavid M. Grimes 344191828a40SDavid M. Grimes *len = 3; 344291828a40SDavid M. Grimes return 1; 344391828a40SDavid M. Grimes } 344491828a40SDavid M. Grimes 344539655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 344691828a40SDavid M. Grimes .get_parent = shmem_get_parent, 344791828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3448480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 344991828a40SDavid M. Grimes }; 345091828a40SDavid M. Grimes 3451626c3920SAl Viro enum shmem_param { 3452626c3920SAl Viro Opt_gid, 3453626c3920SAl Viro Opt_huge, 3454626c3920SAl Viro Opt_mode, 3455626c3920SAl Viro Opt_mpol, 3456626c3920SAl Viro Opt_nr_blocks, 3457626c3920SAl Viro Opt_nr_inodes, 3458626c3920SAl Viro Opt_size, 3459626c3920SAl Viro Opt_uid, 3460ea3271f7SChris Down Opt_inode32, 3461ea3271f7SChris Down Opt_inode64, 3462626c3920SAl Viro }; 34631da177e4SLinus Torvalds 34645eede625SAl Viro static const struct constant_table shmem_param_enums_huge[] = { 34652710c957SAl Viro {"never", SHMEM_HUGE_NEVER }, 34662710c957SAl Viro {"always", SHMEM_HUGE_ALWAYS }, 34672710c957SAl Viro {"within_size", SHMEM_HUGE_WITHIN_SIZE }, 34682710c957SAl Viro {"advise", SHMEM_HUGE_ADVISE }, 34692710c957SAl Viro {} 34702710c957SAl Viro }; 34712710c957SAl Viro 3472d7167b14SAl Viro const struct fs_parameter_spec shmem_fs_parameters[] = { 3473626c3920SAl Viro fsparam_u32 ("gid", Opt_gid), 34742710c957SAl Viro fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge), 3475626c3920SAl Viro fsparam_u32oct("mode", Opt_mode), 3476626c3920SAl Viro fsparam_string("mpol", Opt_mpol), 3477626c3920SAl Viro fsparam_string("nr_blocks", Opt_nr_blocks), 3478626c3920SAl Viro fsparam_string("nr_inodes", Opt_nr_inodes), 3479626c3920SAl Viro fsparam_string("size", Opt_size), 3480626c3920SAl Viro fsparam_u32 ("uid", Opt_uid), 3481ea3271f7SChris Down fsparam_flag ("inode32", Opt_inode32), 3482ea3271f7SChris Down fsparam_flag ("inode64", Opt_inode64), 3483626c3920SAl Viro {} 3484626c3920SAl Viro }; 3485626c3920SAl Viro 3486f3235626SDavid Howells static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) 3487626c3920SAl Viro { 3488f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3489626c3920SAl Viro struct fs_parse_result result; 3490e04dc423SAl Viro unsigned long long size; 3491626c3920SAl Viro char *rest; 3492626c3920SAl Viro int opt; 3493626c3920SAl Viro 3494d7167b14SAl Viro opt = fs_parse(fc, shmem_fs_parameters, param, &result); 3495f3235626SDavid Howells if (opt < 0) 3496626c3920SAl Viro return opt; 3497626c3920SAl Viro 3498626c3920SAl Viro switch (opt) { 3499626c3920SAl Viro case Opt_size: 3500626c3920SAl Viro size = memparse(param->string, &rest); 3501e04dc423SAl Viro if (*rest == '%') { 3502e04dc423SAl Viro size <<= PAGE_SHIFT; 3503e04dc423SAl Viro size *= totalram_pages(); 3504e04dc423SAl Viro do_div(size, 100); 3505e04dc423SAl Viro rest++; 3506e04dc423SAl Viro } 3507e04dc423SAl Viro if (*rest) 3508626c3920SAl Viro goto bad_value; 3509e04dc423SAl Viro ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE); 3510e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3511626c3920SAl Viro break; 3512626c3920SAl Viro case Opt_nr_blocks: 3513626c3920SAl Viro ctx->blocks = memparse(param->string, &rest); 35140c98c8e1SZhaoLong Wang if (*rest || ctx->blocks > S64_MAX) 3515626c3920SAl Viro goto bad_value; 3516e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3517626c3920SAl Viro break; 3518626c3920SAl Viro case Opt_nr_inodes: 3519626c3920SAl Viro ctx->inodes = memparse(param->string, &rest); 3520e04dc423SAl Viro if (*rest) 3521626c3920SAl Viro goto bad_value; 3522e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_INODES; 3523626c3920SAl Viro break; 3524626c3920SAl Viro case Opt_mode: 3525626c3920SAl Viro ctx->mode = result.uint_32 & 07777; 3526626c3920SAl Viro break; 3527626c3920SAl Viro case Opt_uid: 3528626c3920SAl Viro ctx->uid = make_kuid(current_user_ns(), result.uint_32); 3529e04dc423SAl Viro if (!uid_valid(ctx->uid)) 3530626c3920SAl Viro goto bad_value; 3531626c3920SAl Viro break; 3532626c3920SAl Viro case Opt_gid: 3533626c3920SAl Viro ctx->gid = make_kgid(current_user_ns(), result.uint_32); 3534e04dc423SAl Viro if (!gid_valid(ctx->gid)) 3535626c3920SAl Viro goto bad_value; 3536626c3920SAl Viro break; 3537626c3920SAl Viro case Opt_huge: 3538626c3920SAl Viro ctx->huge = result.uint_32; 3539626c3920SAl Viro if (ctx->huge != SHMEM_HUGE_NEVER && 3540396bcc52SMatthew Wilcox (Oracle) !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 3541626c3920SAl Viro has_transparent_hugepage())) 3542626c3920SAl Viro goto unsupported_parameter; 3543e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_HUGE; 3544626c3920SAl Viro break; 3545626c3920SAl Viro case Opt_mpol: 3546626c3920SAl Viro if (IS_ENABLED(CONFIG_NUMA)) { 3547e04dc423SAl Viro mpol_put(ctx->mpol); 3548e04dc423SAl Viro ctx->mpol = NULL; 3549626c3920SAl Viro if (mpol_parse_str(param->string, &ctx->mpol)) 3550626c3920SAl Viro goto bad_value; 3551626c3920SAl Viro break; 3552626c3920SAl Viro } 3553626c3920SAl Viro goto unsupported_parameter; 3554ea3271f7SChris Down case Opt_inode32: 3555ea3271f7SChris Down ctx->full_inums = false; 3556ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3557ea3271f7SChris Down break; 3558ea3271f7SChris Down case Opt_inode64: 3559ea3271f7SChris Down if (sizeof(ino_t) < 8) { 3560ea3271f7SChris Down return invalfc(fc, 3561ea3271f7SChris Down "Cannot use inode64 with <64bit inums in kernel\n"); 3562ea3271f7SChris Down } 3563ea3271f7SChris Down ctx->full_inums = true; 3564ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3565ea3271f7SChris Down break; 3566e04dc423SAl Viro } 3567e04dc423SAl Viro return 0; 3568e04dc423SAl Viro 3569626c3920SAl Viro unsupported_parameter: 3570f35aa2bcSAl Viro return invalfc(fc, "Unsupported parameter '%s'", param->key); 3571626c3920SAl Viro bad_value: 3572f35aa2bcSAl Viro return invalfc(fc, "Bad value for '%s'", param->key); 3573e04dc423SAl Viro } 3574e04dc423SAl Viro 3575f3235626SDavid Howells static int shmem_parse_options(struct fs_context *fc, void *data) 3576e04dc423SAl Viro { 3577f3235626SDavid Howells char *options = data; 3578f3235626SDavid Howells 357933f37c64SAl Viro if (options) { 358033f37c64SAl Viro int err = security_sb_eat_lsm_opts(options, &fc->security); 358133f37c64SAl Viro if (err) 358233f37c64SAl Viro return err; 358333f37c64SAl Viro } 358433f37c64SAl Viro 3585b00dc3adSHugh Dickins while (options != NULL) { 3586626c3920SAl Viro char *this_char = options; 3587b00dc3adSHugh Dickins for (;;) { 3588b00dc3adSHugh Dickins /* 3589b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3590b00dc3adSHugh Dickins * mount options form a comma-separated list, 3591b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3592b00dc3adSHugh Dickins */ 3593b00dc3adSHugh Dickins options = strchr(options, ','); 3594b00dc3adSHugh Dickins if (options == NULL) 3595b00dc3adSHugh Dickins break; 3596b00dc3adSHugh Dickins options++; 3597b00dc3adSHugh Dickins if (!isdigit(*options)) { 3598b00dc3adSHugh Dickins options[-1] = '\0'; 3599b00dc3adSHugh Dickins break; 3600b00dc3adSHugh Dickins } 3601b00dc3adSHugh Dickins } 3602626c3920SAl Viro if (*this_char) { 3603626c3920SAl Viro char *value = strchr(this_char, '='); 3604f3235626SDavid Howells size_t len = 0; 3605626c3920SAl Viro int err; 3606626c3920SAl Viro 3607626c3920SAl Viro if (value) { 3608626c3920SAl Viro *value++ = '\0'; 3609f3235626SDavid Howells len = strlen(value); 36101da177e4SLinus Torvalds } 3611f3235626SDavid Howells err = vfs_parse_fs_string(fc, this_char, value, len); 3612f3235626SDavid Howells if (err < 0) 3613f3235626SDavid Howells return err; 36141da177e4SLinus Torvalds } 3615626c3920SAl Viro } 36161da177e4SLinus Torvalds return 0; 36171da177e4SLinus Torvalds } 36181da177e4SLinus Torvalds 3619f3235626SDavid Howells /* 3620f3235626SDavid Howells * Reconfigure a shmem filesystem. 3621f3235626SDavid Howells * 3622f3235626SDavid Howells * Note that we disallow change from limited->unlimited blocks/inodes while any 3623f3235626SDavid Howells * are in use; but we must separately disallow unlimited->limited, because in 3624f3235626SDavid Howells * that case we have no record of how much is already in use. 3625f3235626SDavid Howells */ 3626f3235626SDavid Howells static int shmem_reconfigure(struct fs_context *fc) 36271da177e4SLinus Torvalds { 3628f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3629f3235626SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); 36300edd73b3SHugh Dickins unsigned long inodes; 3631bf11b9a8SSebastian Andrzej Siewior struct mempolicy *mpol = NULL; 3632f3235626SDavid Howells const char *err; 36330edd73b3SHugh Dickins 3634bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 36350edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 36360c98c8e1SZhaoLong Wang 3637f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { 3638f3235626SDavid Howells if (!sbinfo->max_blocks) { 3639f3235626SDavid Howells err = "Cannot retroactively limit size"; 36400edd73b3SHugh Dickins goto out; 36410b5071ddSAl Viro } 3642f3235626SDavid Howells if (percpu_counter_compare(&sbinfo->used_blocks, 3643f3235626SDavid Howells ctx->blocks) > 0) { 3644f3235626SDavid Howells err = "Too small a size for current use"; 36450b5071ddSAl Viro goto out; 3646f3235626SDavid Howells } 3647f3235626SDavid Howells } 3648f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) { 3649f3235626SDavid Howells if (!sbinfo->max_inodes) { 3650f3235626SDavid Howells err = "Cannot retroactively limit inodes"; 36510b5071ddSAl Viro goto out; 36520b5071ddSAl Viro } 3653f3235626SDavid Howells if (ctx->inodes < inodes) { 3654f3235626SDavid Howells err = "Too few inodes for current use"; 3655f3235626SDavid Howells goto out; 3656f3235626SDavid Howells } 3657f3235626SDavid Howells } 36580edd73b3SHugh Dickins 3659ea3271f7SChris Down if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums && 3660ea3271f7SChris Down sbinfo->next_ino > UINT_MAX) { 3661ea3271f7SChris Down err = "Current inum too high to switch to 32-bit inums"; 3662ea3271f7SChris Down goto out; 3663ea3271f7SChris Down } 3664ea3271f7SChris Down 3665f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_HUGE) 3666f3235626SDavid Howells sbinfo->huge = ctx->huge; 3667ea3271f7SChris Down if (ctx->seen & SHMEM_SEEN_INUMS) 3668ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 3669f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_BLOCKS) 3670f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3671f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_INODES) { 3672f3235626SDavid Howells sbinfo->max_inodes = ctx->inodes; 3673f3235626SDavid Howells sbinfo->free_inodes = ctx->inodes - inodes; 36740b5071ddSAl Viro } 367571fe804bSLee Schermerhorn 36765f00110fSGreg Thelen /* 36775f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 36785f00110fSGreg Thelen */ 3679f3235626SDavid Howells if (ctx->mpol) { 3680bf11b9a8SSebastian Andrzej Siewior mpol = sbinfo->mpol; 3681f3235626SDavid Howells sbinfo->mpol = ctx->mpol; /* transfers initial ref */ 3682f3235626SDavid Howells ctx->mpol = NULL; 36835f00110fSGreg Thelen } 3684bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3685bf11b9a8SSebastian Andrzej Siewior mpol_put(mpol); 3686f3235626SDavid Howells return 0; 36870edd73b3SHugh Dickins out: 3688bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3689f35aa2bcSAl Viro return invalfc(fc, "%s", err); 36901da177e4SLinus Torvalds } 3691680d794bSakpm@linux-foundation.org 369234c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3693680d794bSakpm@linux-foundation.org { 369434c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3695680d794bSakpm@linux-foundation.org 3696680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3697680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 369809cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3699680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3700680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 37010825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 370209208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 37038751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 37048751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 37058751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 37068751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 37078751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 37088751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3709ea3271f7SChris Down 3710ea3271f7SChris Down /* 3711ea3271f7SChris Down * Showing inode{64,32} might be useful even if it's the system default, 3712ea3271f7SChris Down * since then people don't have to resort to checking both here and 3713ea3271f7SChris Down * /proc/config.gz to confirm 64-bit inums were successfully applied 3714ea3271f7SChris Down * (which may not even exist if IKCONFIG_PROC isn't enabled). 3715ea3271f7SChris Down * 3716ea3271f7SChris Down * We hide it when inode64 isn't the default and we are using 32-bit 3717ea3271f7SChris Down * inodes, since that probably just means the feature isn't even under 3718ea3271f7SChris Down * consideration. 3719ea3271f7SChris Down * 3720ea3271f7SChris Down * As such: 3721ea3271f7SChris Down * 3722ea3271f7SChris Down * +-----------------+-----------------+ 3723ea3271f7SChris Down * | TMPFS_INODE64=y | TMPFS_INODE64=n | 3724ea3271f7SChris Down * +------------------+-----------------+-----------------+ 3725ea3271f7SChris Down * | full_inums=true | show | show | 3726ea3271f7SChris Down * | full_inums=false | show | hide | 3727ea3271f7SChris Down * +------------------+-----------------+-----------------+ 3728ea3271f7SChris Down * 3729ea3271f7SChris Down */ 3730ea3271f7SChris Down if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums) 3731ea3271f7SChris Down seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32)); 3732396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 37335a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 37345a6e75f8SKirill A. Shutemov if (sbinfo->huge) 37355a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 37365a6e75f8SKirill A. Shutemov #endif 373771fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3738680d794bSakpm@linux-foundation.org return 0; 3739680d794bSakpm@linux-foundation.org } 37409183df25SDavid Herrmann 3741680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 37421da177e4SLinus Torvalds 37431da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 37441da177e4SLinus Torvalds { 3745602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3746602586a8SHugh Dickins 3747e809d5f0SChris Down free_percpu(sbinfo->ino_batch); 3748602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 374949cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3750602586a8SHugh Dickins kfree(sbinfo); 37511da177e4SLinus Torvalds sb->s_fs_info = NULL; 37521da177e4SLinus Torvalds } 37531da177e4SLinus Torvalds 3754f3235626SDavid Howells static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) 37551da177e4SLinus Torvalds { 3756f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 37571da177e4SLinus Torvalds struct inode *inode; 37580edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3759680d794bSakpm@linux-foundation.org 3760680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3761425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3762680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3763680d794bSakpm@linux-foundation.org if (!sbinfo) 3764680d794bSakpm@linux-foundation.org return -ENOMEM; 3765680d794bSakpm@linux-foundation.org 3766680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 37671da177e4SLinus Torvalds 37680edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 37691da177e4SLinus Torvalds /* 37701da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 37711da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 37721da177e4SLinus Torvalds * but the internal instance is left unlimited. 37731da177e4SLinus Torvalds */ 37741751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 3775f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) 3776f3235626SDavid Howells ctx->blocks = shmem_default_max_blocks(); 3777f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_INODES)) 3778f3235626SDavid Howells ctx->inodes = shmem_default_max_inodes(); 3779ea3271f7SChris Down if (!(ctx->seen & SHMEM_SEEN_INUMS)) 3780ea3271f7SChris Down ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64); 3781ca4e0519SAl Viro } else { 37821751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 37831da177e4SLinus Torvalds } 378491828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 378536f05cabSJeff Layton sb->s_flags |= SB_NOSEC | SB_I_VERSION; 37860edd73b3SHugh Dickins #else 37871751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 37880edd73b3SHugh Dickins #endif 3789f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3790f3235626SDavid Howells sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes; 3791e809d5f0SChris Down if (sb->s_flags & SB_KERNMOUNT) { 3792e809d5f0SChris Down sbinfo->ino_batch = alloc_percpu(ino_t); 3793e809d5f0SChris Down if (!sbinfo->ino_batch) 3794e809d5f0SChris Down goto failed; 3795e809d5f0SChris Down } 3796f3235626SDavid Howells sbinfo->uid = ctx->uid; 3797f3235626SDavid Howells sbinfo->gid = ctx->gid; 3798ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 3799f3235626SDavid Howells sbinfo->mode = ctx->mode; 3800f3235626SDavid Howells sbinfo->huge = ctx->huge; 3801f3235626SDavid Howells sbinfo->mpol = ctx->mpol; 3802f3235626SDavid Howells ctx->mpol = NULL; 38031da177e4SLinus Torvalds 3804bf11b9a8SSebastian Andrzej Siewior raw_spin_lock_init(&sbinfo->stat_lock); 3805908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3806602586a8SHugh Dickins goto failed; 3807779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3808779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 38091da177e4SLinus Torvalds 3810285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 381109cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 381209cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 38131da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 38141da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3815cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3816b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 381739f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3818b09e0fa4SEric Paris #endif 3819b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 38201751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 382139f0247dSAndreas Gruenbacher #endif 38222b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 38230edd73b3SHugh Dickins 38247a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(&nop_mnt_idmap, sb, NULL, S_IFDIR | sbinfo->mode, 0, 38257a80e5b8SGiuseppe Scrivano VM_NORESERVE); 38261da177e4SLinus Torvalds if (!inode) 38271da177e4SLinus Torvalds goto failed; 3828680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3829680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3830318ceed0SAl Viro sb->s_root = d_make_root(inode); 3831318ceed0SAl Viro if (!sb->s_root) 383248fde701SAl Viro goto failed; 38331da177e4SLinus Torvalds return 0; 38341da177e4SLinus Torvalds 38351da177e4SLinus Torvalds failed: 38361da177e4SLinus Torvalds shmem_put_super(sb); 3837f2b346e4SMiaohe Lin return -ENOMEM; 38381da177e4SLinus Torvalds } 38391da177e4SLinus Torvalds 3840f3235626SDavid Howells static int shmem_get_tree(struct fs_context *fc) 3841f3235626SDavid Howells { 3842f3235626SDavid Howells return get_tree_nodev(fc, shmem_fill_super); 3843f3235626SDavid Howells } 3844f3235626SDavid Howells 3845f3235626SDavid Howells static void shmem_free_fc(struct fs_context *fc) 3846f3235626SDavid Howells { 3847f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3848f3235626SDavid Howells 3849f3235626SDavid Howells if (ctx) { 3850f3235626SDavid Howells mpol_put(ctx->mpol); 3851f3235626SDavid Howells kfree(ctx); 3852f3235626SDavid Howells } 3853f3235626SDavid Howells } 3854f3235626SDavid Howells 3855f3235626SDavid Howells static const struct fs_context_operations shmem_fs_context_ops = { 3856f3235626SDavid Howells .free = shmem_free_fc, 3857f3235626SDavid Howells .get_tree = shmem_get_tree, 3858f3235626SDavid Howells #ifdef CONFIG_TMPFS 3859f3235626SDavid Howells .parse_monolithic = shmem_parse_options, 3860f3235626SDavid Howells .parse_param = shmem_parse_one, 3861f3235626SDavid Howells .reconfigure = shmem_reconfigure, 3862f3235626SDavid Howells #endif 3863f3235626SDavid Howells }; 3864f3235626SDavid Howells 3865fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 38661da177e4SLinus Torvalds 38671da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 38681da177e4SLinus Torvalds { 386941ffe5d5SHugh Dickins struct shmem_inode_info *info; 3870fd60b288SMuchun Song info = alloc_inode_sb(sb, shmem_inode_cachep, GFP_KERNEL); 387141ffe5d5SHugh Dickins if (!info) 38721da177e4SLinus Torvalds return NULL; 387341ffe5d5SHugh Dickins return &info->vfs_inode; 38741da177e4SLinus Torvalds } 38751da177e4SLinus Torvalds 387674b1da56SAl Viro static void shmem_free_in_core_inode(struct inode *inode) 3877fa0d7e3dSNick Piggin { 387884e710daSAl Viro if (S_ISLNK(inode->i_mode)) 38793ed47db3SAl Viro kfree(inode->i_link); 3880fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3881fa0d7e3dSNick Piggin } 3882fa0d7e3dSNick Piggin 38831da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 38841da177e4SLinus Torvalds { 388509208d15SAl Viro if (S_ISREG(inode->i_mode)) 38861da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 38871da177e4SLinus Torvalds } 38881da177e4SLinus Torvalds 388941ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 38901da177e4SLinus Torvalds { 389141ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 389241ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 38931da177e4SLinus Torvalds } 38941da177e4SLinus Torvalds 38959a8ec03eSweiping zhang static void shmem_init_inodecache(void) 38961da177e4SLinus Torvalds { 38971da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 38981da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 38995d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 39001da177e4SLinus Torvalds } 39011da177e4SLinus Torvalds 390241ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 39031da177e4SLinus Torvalds { 39041a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 39051da177e4SLinus Torvalds } 39061da177e4SLinus Torvalds 3907a7605426SYang Shi /* Keep the page in page cache instead of truncating it */ 3908a7605426SYang Shi static int shmem_error_remove_page(struct address_space *mapping, 3909a7605426SYang Shi struct page *page) 3910a7605426SYang Shi { 3911a7605426SYang Shi return 0; 3912a7605426SYang Shi } 3913a7605426SYang Shi 391430e6a51dSHui Su const struct address_space_operations shmem_aops = { 39151da177e4SLinus Torvalds .writepage = shmem_writepage, 391646de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 39171da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3918800d15a5SNick Piggin .write_begin = shmem_write_begin, 3919800d15a5SNick Piggin .write_end = shmem_write_end, 39201da177e4SLinus Torvalds #endif 39211c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 392254184650SMatthew Wilcox (Oracle) .migrate_folio = migrate_folio, 39231c93923cSAndrew Morton #endif 3924a7605426SYang Shi .error_remove_page = shmem_error_remove_page, 39251da177e4SLinus Torvalds }; 392630e6a51dSHui Su EXPORT_SYMBOL(shmem_aops); 39271da177e4SLinus Torvalds 392815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 39291da177e4SLinus Torvalds .mmap = shmem_mmap, 3930a5454f95SThomas Weißschuh .open = generic_file_open, 3931c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 39321da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3933220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 39342ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 39358174202bSAl Viro .write_iter = generic_file_write_iter, 39361b061d92SChristoph Hellwig .fsync = noop_fsync, 393782c156f8SAl Viro .splice_read = generic_file_splice_read, 3938f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 393983e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 39401da177e4SLinus Torvalds #endif 39411da177e4SLinus Torvalds }; 39421da177e4SLinus Torvalds 394392e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 394444a30220SYu Zhao .getattr = shmem_getattr, 394594c1e62dSHugh Dickins .setattr = shmem_setattr, 3946b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3947b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3948feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3949e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 3950e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 3951b09e0fa4SEric Paris #endif 39521da177e4SLinus Torvalds }; 39531da177e4SLinus Torvalds 395492e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 39551da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3956f7cd16a5SXavier Roche .getattr = shmem_getattr, 39571da177e4SLinus Torvalds .create = shmem_create, 39581da177e4SLinus Torvalds .lookup = simple_lookup, 39591da177e4SLinus Torvalds .link = shmem_link, 39601da177e4SLinus Torvalds .unlink = shmem_unlink, 39611da177e4SLinus Torvalds .symlink = shmem_symlink, 39621da177e4SLinus Torvalds .mkdir = shmem_mkdir, 39631da177e4SLinus Torvalds .rmdir = shmem_rmdir, 39641da177e4SLinus Torvalds .mknod = shmem_mknod, 39652773bf00SMiklos Szeredi .rename = shmem_rename2, 396660545d0dSAl Viro .tmpfile = shmem_tmpfile, 39671da177e4SLinus Torvalds #endif 3968b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3969b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3970e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 3971e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 3972b09e0fa4SEric Paris #endif 397339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 397494c1e62dSHugh Dickins .setattr = shmem_setattr, 3975feda821eSChristoph Hellwig .set_acl = simple_set_acl, 397639f0247dSAndreas Gruenbacher #endif 397739f0247dSAndreas Gruenbacher }; 397839f0247dSAndreas Gruenbacher 397992e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3980f7cd16a5SXavier Roche .getattr = shmem_getattr, 3981b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3982b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3983b09e0fa4SEric Paris #endif 398439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 398594c1e62dSHugh Dickins .setattr = shmem_setattr, 3986feda821eSChristoph Hellwig .set_acl = simple_set_acl, 398739f0247dSAndreas Gruenbacher #endif 39881da177e4SLinus Torvalds }; 39891da177e4SLinus Torvalds 3990759b9775SHugh Dickins static const struct super_operations shmem_ops = { 39911da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 399274b1da56SAl Viro .free_inode = shmem_free_in_core_inode, 39931da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 39941da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 39951da177e4SLinus Torvalds .statfs = shmem_statfs, 3996680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 39971da177e4SLinus Torvalds #endif 39981f895f75SAl Viro .evict_inode = shmem_evict_inode, 39991da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 40001da177e4SLinus Torvalds .put_super = shmem_put_super, 4001396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4002779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 4003779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 4004779750d2SKirill A. Shutemov #endif 40051da177e4SLinus Torvalds }; 40061da177e4SLinus Torvalds 4007f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 400854cb8821SNick Piggin .fault = shmem_fault, 4009d7c17551SNing Qu .map_pages = filemap_map_pages, 40101da177e4SLinus Torvalds #ifdef CONFIG_NUMA 40111da177e4SLinus Torvalds .set_policy = shmem_set_policy, 40121da177e4SLinus Torvalds .get_policy = shmem_get_policy, 40131da177e4SLinus Torvalds #endif 40141da177e4SLinus Torvalds }; 40151da177e4SLinus Torvalds 4016d09e8ca6SPasha Tatashin static const struct vm_operations_struct shmem_anon_vm_ops = { 4017d09e8ca6SPasha Tatashin .fault = shmem_fault, 4018d09e8ca6SPasha Tatashin .map_pages = filemap_map_pages, 4019d09e8ca6SPasha Tatashin #ifdef CONFIG_NUMA 4020d09e8ca6SPasha Tatashin .set_policy = shmem_set_policy, 4021d09e8ca6SPasha Tatashin .get_policy = shmem_get_policy, 4022d09e8ca6SPasha Tatashin #endif 4023d09e8ca6SPasha Tatashin }; 4024d09e8ca6SPasha Tatashin 4025f3235626SDavid Howells int shmem_init_fs_context(struct fs_context *fc) 40261da177e4SLinus Torvalds { 4027f3235626SDavid Howells struct shmem_options *ctx; 4028f3235626SDavid Howells 4029f3235626SDavid Howells ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL); 4030f3235626SDavid Howells if (!ctx) 4031f3235626SDavid Howells return -ENOMEM; 4032f3235626SDavid Howells 4033f3235626SDavid Howells ctx->mode = 0777 | S_ISVTX; 4034f3235626SDavid Howells ctx->uid = current_fsuid(); 4035f3235626SDavid Howells ctx->gid = current_fsgid(); 4036f3235626SDavid Howells 4037f3235626SDavid Howells fc->fs_private = ctx; 4038f3235626SDavid Howells fc->ops = &shmem_fs_context_ops; 4039f3235626SDavid Howells return 0; 40401da177e4SLinus Torvalds } 40411da177e4SLinus Torvalds 404241ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 40431da177e4SLinus Torvalds .owner = THIS_MODULE, 40441da177e4SLinus Torvalds .name = "tmpfs", 4045f3235626SDavid Howells .init_fs_context = shmem_init_fs_context, 4046f3235626SDavid Howells #ifdef CONFIG_TMPFS 4047d7167b14SAl Viro .parameters = shmem_fs_parameters, 4048f3235626SDavid Howells #endif 40491da177e4SLinus Torvalds .kill_sb = kill_litter_super, 40507a80e5b8SGiuseppe Scrivano #ifdef CONFIG_SHMEM 40517a80e5b8SGiuseppe Scrivano .fs_flags = FS_USERNS_MOUNT | FS_ALLOW_IDMAP, 40527a80e5b8SGiuseppe Scrivano #else 4053ff36da69SMatthew Wilcox (Oracle) .fs_flags = FS_USERNS_MOUNT, 40547a80e5b8SGiuseppe Scrivano #endif 40551da177e4SLinus Torvalds }; 40561da177e4SLinus Torvalds 40579096bbe9SMiaohe Lin void __init shmem_init(void) 40581da177e4SLinus Torvalds { 40591da177e4SLinus Torvalds int error; 40601da177e4SLinus Torvalds 40619a8ec03eSweiping zhang shmem_init_inodecache(); 40621da177e4SLinus Torvalds 406341ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 40641da177e4SLinus Torvalds if (error) { 40651170532bSJoe Perches pr_err("Could not register tmpfs\n"); 40661da177e4SLinus Torvalds goto out2; 40671da177e4SLinus Torvalds } 406895dc112aSGreg Kroah-Hartman 4069ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 40701da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 40711da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 40721170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 40731da177e4SLinus Torvalds goto out1; 40741da177e4SLinus Torvalds } 40755a6e75f8SKirill A. Shutemov 4076396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4077435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 40785a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 40795a6e75f8SKirill A. Shutemov else 40805e6e5a12SHugh Dickins shmem_huge = SHMEM_HUGE_NEVER; /* just in case it was patched */ 40815a6e75f8SKirill A. Shutemov #endif 40829096bbe9SMiaohe Lin return; 40831da177e4SLinus Torvalds 40841da177e4SLinus Torvalds out1: 408541ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 40861da177e4SLinus Torvalds out2: 408741ffe5d5SHugh Dickins shmem_destroy_inodecache(); 40881da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 40891da177e4SLinus Torvalds } 4090853ac43aSMatt Mackall 4091396bcc52SMatthew Wilcox (Oracle) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS) 40925a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 40935a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 40945a6e75f8SKirill A. Shutemov { 409526083eb6SColin Ian King static const int values[] = { 40965a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 40975a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 40985a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 40995a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 41005a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 41015a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 41025a6e75f8SKirill A. Shutemov }; 410379d4d38aSJoe Perches int len = 0; 410479d4d38aSJoe Perches int i; 41055a6e75f8SKirill A. Shutemov 410679d4d38aSJoe Perches for (i = 0; i < ARRAY_SIZE(values); i++) { 410779d4d38aSJoe Perches len += sysfs_emit_at(buf, len, 410879d4d38aSJoe Perches shmem_huge == values[i] ? "%s[%s]" : "%s%s", 410979d4d38aSJoe Perches i ? " " : "", 41105a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 41115a6e75f8SKirill A. Shutemov } 411279d4d38aSJoe Perches 411379d4d38aSJoe Perches len += sysfs_emit_at(buf, len, "\n"); 411479d4d38aSJoe Perches 411579d4d38aSJoe Perches return len; 41165a6e75f8SKirill A. Shutemov } 41175a6e75f8SKirill A. Shutemov 41185a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 41195a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 41205a6e75f8SKirill A. Shutemov { 41215a6e75f8SKirill A. Shutemov char tmp[16]; 41225a6e75f8SKirill A. Shutemov int huge; 41235a6e75f8SKirill A. Shutemov 41245a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 41255a6e75f8SKirill A. Shutemov return -EINVAL; 41265a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 41275a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 41285a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 41295a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 41305a6e75f8SKirill A. Shutemov 41315a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 41325a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 41335a6e75f8SKirill A. Shutemov return -EINVAL; 41345a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 41355a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 41365a6e75f8SKirill A. Shutemov return -EINVAL; 41375a6e75f8SKirill A. Shutemov 41385a6e75f8SKirill A. Shutemov shmem_huge = huge; 4139435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 41405a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 41415a6e75f8SKirill A. Shutemov return count; 41425a6e75f8SKirill A. Shutemov } 41435a6e75f8SKirill A. Shutemov 41444bfa8adaSMiaohe Lin struct kobj_attribute shmem_enabled_attr = __ATTR_RW(shmem_enabled); 4145396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ 4146f3f0e1d2SKirill A. Shutemov 4147853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 4148853ac43aSMatt Mackall 4149853ac43aSMatt Mackall /* 4150853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 4151853ac43aSMatt Mackall * 4152853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 4153853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 4154853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 4155853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 4156853ac43aSMatt Mackall */ 4157853ac43aSMatt Mackall 415841ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 4159853ac43aSMatt Mackall .name = "tmpfs", 4160f3235626SDavid Howells .init_fs_context = ramfs_init_fs_context, 4161d7167b14SAl Viro .parameters = ramfs_fs_parameters, 4162853ac43aSMatt Mackall .kill_sb = kill_litter_super, 41632b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 4164853ac43aSMatt Mackall }; 4165853ac43aSMatt Mackall 41669096bbe9SMiaohe Lin void __init shmem_init(void) 4167853ac43aSMatt Mackall { 416841ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 4169853ac43aSMatt Mackall 417041ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 4171853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 4172853ac43aSMatt Mackall } 4173853ac43aSMatt Mackall 417410a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 4175853ac43aSMatt Mackall { 4176853ac43aSMatt Mackall return 0; 4177853ac43aSMatt Mackall } 4178853ac43aSMatt Mackall 4179d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 41803f96b79aSHugh Dickins { 41813f96b79aSHugh Dickins return 0; 41823f96b79aSHugh Dickins } 41833f96b79aSHugh Dickins 418424513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 418524513264SHugh Dickins { 418624513264SHugh Dickins } 418724513264SHugh Dickins 4188c01d5b30SHugh Dickins #ifdef CONFIG_MMU 4189c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 4190c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 4191c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 4192c01d5b30SHugh Dickins { 4193c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 4194c01d5b30SHugh Dickins } 4195c01d5b30SHugh Dickins #endif 4196c01d5b30SHugh Dickins 419741ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 419894c1e62dSHugh Dickins { 419941ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 420094c1e62dSHugh Dickins } 420194c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 420294c1e62dSHugh Dickins 4203853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 4204d09e8ca6SPasha Tatashin #define shmem_anon_vm_ops generic_file_vm_ops 42050b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 42067a80e5b8SGiuseppe Scrivano #define shmem_get_inode(idmap, sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 42070b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 42080b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 4209853ac43aSMatt Mackall 4210853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 4211853ac43aSMatt Mackall 4212853ac43aSMatt Mackall /* common code */ 42131da177e4SLinus Torvalds 4214703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 4215c7277090SEric Paris unsigned long flags, unsigned int i_flags) 42161da177e4SLinus Torvalds { 42171da177e4SLinus Torvalds struct inode *inode; 421893dec2daSAl Viro struct file *res; 42191da177e4SLinus Torvalds 4220703321b6SMatthew Auld if (IS_ERR(mnt)) 4221703321b6SMatthew Auld return ERR_CAST(mnt); 42221da177e4SLinus Torvalds 4223285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 42241da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 42251da177e4SLinus Torvalds 42261da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 42271da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 42281da177e4SLinus Torvalds 42297a80e5b8SGiuseppe Scrivano if (is_idmapped_mnt(mnt)) 42307a80e5b8SGiuseppe Scrivano return ERR_PTR(-EINVAL); 42317a80e5b8SGiuseppe Scrivano 42327a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(&nop_mnt_idmap, mnt->mnt_sb, NULL, 42337a80e5b8SGiuseppe Scrivano S_IFREG | S_IRWXUGO, 0, flags); 4234dac2d1f6SAl Viro if (unlikely(!inode)) { 4235dac2d1f6SAl Viro shmem_unacct_size(flags, size); 4236dac2d1f6SAl Viro return ERR_PTR(-ENOSPC); 4237dac2d1f6SAl Viro } 4238c7277090SEric Paris inode->i_flags |= i_flags; 42391da177e4SLinus Torvalds inode->i_size = size; 42406d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 424126567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 424293dec2daSAl Viro if (!IS_ERR(res)) 424393dec2daSAl Viro res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 42444b42af81SAl Viro &shmem_file_operations); 42456b4d0b27SAl Viro if (IS_ERR(res)) 424693dec2daSAl Viro iput(inode); 42476b4d0b27SAl Viro return res; 42481da177e4SLinus Torvalds } 4249c7277090SEric Paris 4250c7277090SEric Paris /** 4251c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 4252c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 4253c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 4254e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 4255e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 4256c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4257c7277090SEric Paris * @size: size to be set for the file 4258c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4259c7277090SEric Paris */ 4260c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 4261c7277090SEric Paris { 4262703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 4263c7277090SEric Paris } 4264c7277090SEric Paris 4265c7277090SEric Paris /** 4266c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 4267c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4268c7277090SEric Paris * @size: size to be set for the file 4269c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4270c7277090SEric Paris */ 4271c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 4272c7277090SEric Paris { 4273703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 4274c7277090SEric Paris } 4275395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 42761da177e4SLinus Torvalds 427746711810SRandy Dunlap /** 4278703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 4279703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 4280703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 4281703321b6SMatthew Auld * @size: size to be set for the file 4282703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4283703321b6SMatthew Auld */ 4284703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 4285703321b6SMatthew Auld loff_t size, unsigned long flags) 4286703321b6SMatthew Auld { 4287703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 4288703321b6SMatthew Auld } 4289703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 4290703321b6SMatthew Auld 4291703321b6SMatthew Auld /** 42921da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 429345e55300SPeter Collingbourne * @vma: the vma to be mmapped is prepared by do_mmap 42941da177e4SLinus Torvalds */ 42951da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 42961da177e4SLinus Torvalds { 42971da177e4SLinus Torvalds struct file *file; 42981da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 42991da177e4SLinus Torvalds 430066fc1303SHugh Dickins /* 4301c1e8d7c6SMichel Lespinasse * Cloning a new file under mmap_lock leads to a lock ordering conflict 430266fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 430366fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 430466fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 430566fc1303SHugh Dickins */ 4306703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 43071da177e4SLinus Torvalds if (IS_ERR(file)) 43081da177e4SLinus Torvalds return PTR_ERR(file); 43091da177e4SLinus Torvalds 43101da177e4SLinus Torvalds if (vma->vm_file) 43111da177e4SLinus Torvalds fput(vma->vm_file); 43121da177e4SLinus Torvalds vma->vm_file = file; 4313d09e8ca6SPasha Tatashin vma->vm_ops = &shmem_anon_vm_ops; 4314f3f0e1d2SKirill A. Shutemov 43151da177e4SLinus Torvalds return 0; 43161da177e4SLinus Torvalds } 4317d9d90e5eSHugh Dickins 4318d9d90e5eSHugh Dickins /** 4319f01b2b3eSMatthew Wilcox (Oracle) * shmem_read_folio_gfp - read into page cache, using specified page allocation flags. 4320f01b2b3eSMatthew Wilcox (Oracle) * @mapping: the folio's address_space 4321f01b2b3eSMatthew Wilcox (Oracle) * @index: the folio index 4322d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4323d9d90e5eSHugh Dickins * 4324d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4325d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 43267e0a1265SMatthew Wilcox (Oracle) * But read_cache_page_gfp() uses the ->read_folio() method: which does not 4327d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4328d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4329d9d90e5eSHugh Dickins * 433068da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 433168da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4332d9d90e5eSHugh Dickins */ 4333f01b2b3eSMatthew Wilcox (Oracle) struct folio *shmem_read_folio_gfp(struct address_space *mapping, 4334d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4335d9d90e5eSHugh Dickins { 433668da9f05SHugh Dickins #ifdef CONFIG_SHMEM 433768da9f05SHugh Dickins struct inode *inode = mapping->host; 4338a3a9c397SMatthew Wilcox (Oracle) struct folio *folio; 433968da9f05SHugh Dickins int error; 434068da9f05SHugh Dickins 434130e6a51dSHui Su BUG_ON(!shmem_mapping(mapping)); 4342a3a9c397SMatthew Wilcox (Oracle) error = shmem_get_folio_gfp(inode, index, &folio, SGP_CACHE, 4343cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 434468da9f05SHugh Dickins if (error) 4345a7605426SYang Shi return ERR_PTR(error); 4346a7605426SYang Shi 4347a3a9c397SMatthew Wilcox (Oracle) folio_unlock(folio); 4348f01b2b3eSMatthew Wilcox (Oracle) return folio; 4349f01b2b3eSMatthew Wilcox (Oracle) #else 4350f01b2b3eSMatthew Wilcox (Oracle) /* 4351f01b2b3eSMatthew Wilcox (Oracle) * The tiny !SHMEM case uses ramfs without swap 4352f01b2b3eSMatthew Wilcox (Oracle) */ 4353f01b2b3eSMatthew Wilcox (Oracle) return mapping_read_folio_gfp(mapping, index, gfp); 4354f01b2b3eSMatthew Wilcox (Oracle) #endif 4355f01b2b3eSMatthew Wilcox (Oracle) } 4356f01b2b3eSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(shmem_read_folio_gfp); 4357f01b2b3eSMatthew Wilcox (Oracle) 4358f01b2b3eSMatthew Wilcox (Oracle) struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4359f01b2b3eSMatthew Wilcox (Oracle) pgoff_t index, gfp_t gfp) 4360f01b2b3eSMatthew Wilcox (Oracle) { 4361f01b2b3eSMatthew Wilcox (Oracle) struct folio *folio = shmem_read_folio_gfp(mapping, index, gfp); 4362f01b2b3eSMatthew Wilcox (Oracle) struct page *page; 4363f01b2b3eSMatthew Wilcox (Oracle) 4364f01b2b3eSMatthew Wilcox (Oracle) if (IS_ERR(folio)) 4365f01b2b3eSMatthew Wilcox (Oracle) return &folio->page; 4366f01b2b3eSMatthew Wilcox (Oracle) 4367a3a9c397SMatthew Wilcox (Oracle) page = folio_file_page(folio, index); 4368a7605426SYang Shi if (PageHWPoison(page)) { 4369a3a9c397SMatthew Wilcox (Oracle) folio_put(folio); 4370a7605426SYang Shi return ERR_PTR(-EIO); 4371a7605426SYang Shi } 4372a7605426SYang Shi 437368da9f05SHugh Dickins return page; 4374d9d90e5eSHugh Dickins } 4375d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4376