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> 794c27fe4cSMike Rapoport #include <linux/rmap.h> 802b4db796SAmir Goldstein #include <linux/uuid.h> 81e09764cfSCarlos Maiolino #include <linux/quotaops.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 93e07c469eSHugh Dickins /* Pretend that one inode + its dentry occupy this much memory */ 94e07c469eSHugh Dickins #define BOGO_INODE_SIZE 1024 95e07c469eSHugh Dickins 9669f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9769f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9869f07ec9SHugh Dickins 991aac1400SHugh Dickins /* 100f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 1019608703eSJan Kara * inode->i_private (with i_rwsem making sure that it has only one user at 102f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 1031aac1400SHugh Dickins */ 1041aac1400SHugh Dickins struct shmem_falloc { 1058e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 1061aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1071aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1081aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1091aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1101aac1400SHugh Dickins }; 1111aac1400SHugh Dickins 1120b5071ddSAl Viro struct shmem_options { 1130b5071ddSAl Viro unsigned long long blocks; 1140b5071ddSAl Viro unsigned long long inodes; 1150b5071ddSAl Viro struct mempolicy *mpol; 1160b5071ddSAl Viro kuid_t uid; 1170b5071ddSAl Viro kgid_t gid; 1180b5071ddSAl Viro umode_t mode; 119ea3271f7SChris Down bool full_inums; 1200b5071ddSAl Viro int huge; 1210b5071ddSAl Viro int seen; 1222c6efe9cSLuis Chamberlain bool noswap; 123e09764cfSCarlos Maiolino unsigned short quota_types; 124de4c0e7cSLukas Czerner struct shmem_quota_limits qlimits; 1250b5071ddSAl Viro #define SHMEM_SEEN_BLOCKS 1 1260b5071ddSAl Viro #define SHMEM_SEEN_INODES 2 1270b5071ddSAl Viro #define SHMEM_SEEN_HUGE 4 128ea3271f7SChris Down #define SHMEM_SEEN_INUMS 8 1292c6efe9cSLuis Chamberlain #define SHMEM_SEEN_NOSWAP 16 130e09764cfSCarlos Maiolino #define SHMEM_SEEN_QUOTA 32 1310b5071ddSAl Viro }; 1320b5071ddSAl Viro 133b76db735SAndrew Morton #ifdef CONFIG_TMPFS 134680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 135680d794bSakpm@linux-foundation.org { 136ca79b0c2SArun KS return totalram_pages() / 2; 137680d794bSakpm@linux-foundation.org } 138680d794bSakpm@linux-foundation.org 139680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 140680d794bSakpm@linux-foundation.org { 141ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 142ca79b0c2SArun KS 143e07c469eSHugh Dickins return min3(nr_pages - totalhigh_pages(), nr_pages / 2, 144e07c469eSHugh Dickins ULONG_MAX / BOGO_INODE_SIZE); 145680d794bSakpm@linux-foundation.org } 146b76db735SAndrew Morton #endif 147680d794bSakpm@linux-foundation.org 148da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 149da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 150c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 151c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type); 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds return sb->s_fs_info; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1601da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1611da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1621da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1651da177e4SLinus Torvalds { 1660b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 167191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1711da177e4SLinus Torvalds { 1720b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1731da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 17677142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 17777142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 17877142517SKonstantin Khlebnikov { 17977142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 18077142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 18177142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 18277142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 18377142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 18477142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 18577142517SKonstantin Khlebnikov } 18677142517SKonstantin Khlebnikov return 0; 18777142517SKonstantin Khlebnikov } 18877142517SKonstantin Khlebnikov 1891da177e4SLinus Torvalds /* 1901da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 19175edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 192923e2f0eSMatthew Wilcox (Oracle) * shmem_get_folio reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1931da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1941da177e4SLinus Torvalds */ 195800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1961da177e4SLinus Torvalds { 197800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 198800d8c63SKirill A. Shutemov return 0; 199800d8c63SKirill A. Shutemov 200800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 201800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 2051da177e4SLinus Torvalds { 2060b0a0806SHugh Dickins if (flags & VM_NORESERVE) 20709cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2103c1b7528SHugh Dickins static int shmem_inode_acct_block(struct inode *inode, long pages) 2110f079694SMike Rapoport { 2120f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2130f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 214c7e263abSLukas Czerner int err = -ENOSPC; 2150f079694SMike Rapoport 2160f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 217c7e263abSLukas Czerner return err; 2180f079694SMike Rapoport 2193c1b7528SHugh Dickins might_sleep(); /* when quotas */ 2200f079694SMike Rapoport if (sbinfo->max_blocks) { 2210f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2220f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2230f079694SMike Rapoport goto unacct; 224e09764cfSCarlos Maiolino 225e09764cfSCarlos Maiolino err = dquot_alloc_block_nodirty(inode, pages); 226e09764cfSCarlos Maiolino if (err) 227e09764cfSCarlos Maiolino goto unacct; 228e09764cfSCarlos Maiolino 2290f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 230e09764cfSCarlos Maiolino } else { 231e09764cfSCarlos Maiolino err = dquot_alloc_block_nodirty(inode, pages); 232e09764cfSCarlos Maiolino if (err) 233e09764cfSCarlos Maiolino goto unacct; 2340f079694SMike Rapoport } 2350f079694SMike Rapoport 236c7e263abSLukas Czerner return 0; 2370f079694SMike Rapoport 2380f079694SMike Rapoport unacct: 2390f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 240c7e263abSLukas Czerner return err; 2410f079694SMike Rapoport } 2420f079694SMike Rapoport 2433c1b7528SHugh Dickins static void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2440f079694SMike Rapoport { 2450f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2460f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2470f079694SMike Rapoport 2483c1b7528SHugh Dickins might_sleep(); /* when quotas */ 249e09764cfSCarlos Maiolino dquot_free_block_nodirty(inode, pages); 250e09764cfSCarlos Maiolino 2510f079694SMike Rapoport if (sbinfo->max_blocks) 2520f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2530f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2540f079694SMike Rapoport } 2550f079694SMike Rapoport 256759b9775SHugh Dickins static const struct super_operations shmem_ops; 25730e6a51dSHui Su const struct address_space_operations shmem_aops; 25815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 25992e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 26092e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 26192e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 262f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 263d09e8ca6SPasha Tatashin static const struct vm_operations_struct shmem_anon_vm_ops; 264779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2651da177e4SLinus Torvalds 266d09e8ca6SPasha Tatashin bool vma_is_anon_shmem(struct vm_area_struct *vma) 267d09e8ca6SPasha Tatashin { 268d09e8ca6SPasha Tatashin return vma->vm_ops == &shmem_anon_vm_ops; 269d09e8ca6SPasha Tatashin } 270d09e8ca6SPasha Tatashin 271b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 272b0506e48SMike Rapoport { 273d09e8ca6SPasha Tatashin return vma_is_anon_shmem(vma) || vma->vm_ops == &shmem_vm_ops; 274b0506e48SMike Rapoport } 275b0506e48SMike Rapoport 2761da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 277cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2781da177e4SLinus Torvalds 279e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 280e09764cfSCarlos Maiolino 281e09764cfSCarlos Maiolino static int shmem_enable_quotas(struct super_block *sb, 282e09764cfSCarlos Maiolino unsigned short quota_types) 283e09764cfSCarlos Maiolino { 284e09764cfSCarlos Maiolino int type, err = 0; 285e09764cfSCarlos Maiolino 286e09764cfSCarlos Maiolino sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; 287e09764cfSCarlos Maiolino for (type = 0; type < SHMEM_MAXQUOTAS; type++) { 288e09764cfSCarlos Maiolino if (!(quota_types & (1 << type))) 289e09764cfSCarlos Maiolino continue; 290e09764cfSCarlos Maiolino err = dquot_load_quota_sb(sb, type, QFMT_SHMEM, 291e09764cfSCarlos Maiolino DQUOT_USAGE_ENABLED | 292e09764cfSCarlos Maiolino DQUOT_LIMITS_ENABLED); 293e09764cfSCarlos Maiolino if (err) 294e09764cfSCarlos Maiolino goto out_err; 295e09764cfSCarlos Maiolino } 296e09764cfSCarlos Maiolino return 0; 297e09764cfSCarlos Maiolino 298e09764cfSCarlos Maiolino out_err: 299e09764cfSCarlos Maiolino pr_warn("tmpfs: failed to enable quota tracking (type=%d, err=%d)\n", 300e09764cfSCarlos Maiolino type, err); 301e09764cfSCarlos Maiolino for (type--; type >= 0; type--) 302e09764cfSCarlos Maiolino dquot_quota_off(sb, type); 303e09764cfSCarlos Maiolino return err; 304e09764cfSCarlos Maiolino } 305e09764cfSCarlos Maiolino 306e09764cfSCarlos Maiolino static void shmem_disable_quotas(struct super_block *sb) 307e09764cfSCarlos Maiolino { 308e09764cfSCarlos Maiolino int type; 309e09764cfSCarlos Maiolino 310e09764cfSCarlos Maiolino for (type = 0; type < SHMEM_MAXQUOTAS; type++) 311e09764cfSCarlos Maiolino dquot_quota_off(sb, type); 312e09764cfSCarlos Maiolino } 313e09764cfSCarlos Maiolino 314e09764cfSCarlos Maiolino static struct dquot **shmem_get_dquots(struct inode *inode) 315e09764cfSCarlos Maiolino { 316e09764cfSCarlos Maiolino return SHMEM_I(inode)->i_dquot; 317e09764cfSCarlos Maiolino } 318e09764cfSCarlos Maiolino #endif /* CONFIG_TMPFS_QUOTA */ 319e09764cfSCarlos Maiolino 320e809d5f0SChris Down /* 321e809d5f0SChris Down * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and 322e809d5f0SChris Down * produces a novel ino for the newly allocated inode. 323e809d5f0SChris Down * 324e809d5f0SChris Down * It may also be called when making a hard link to permit the space needed by 325e809d5f0SChris Down * each dentry. However, in that case, no new inode number is needed since that 326e809d5f0SChris Down * internally draws from another pool of inode numbers (currently global 327e809d5f0SChris Down * get_next_ino()). This case is indicated by passing NULL as inop. 328e809d5f0SChris Down */ 329e809d5f0SChris Down #define SHMEM_INO_BATCH 1024 330e809d5f0SChris Down static int shmem_reserve_inode(struct super_block *sb, ino_t *inop) 3315b04c689SPavel Emelyanov { 3325b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 333e809d5f0SChris Down ino_t ino; 334e809d5f0SChris Down 335e809d5f0SChris Down if (!(sb->s_flags & SB_KERNMOUNT)) { 336bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 337bb3e96d6SByron Stanoszek if (sbinfo->max_inodes) { 338e07c469eSHugh Dickins if (sbinfo->free_ispace < BOGO_INODE_SIZE) { 339bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3405b04c689SPavel Emelyanov return -ENOSPC; 3415b04c689SPavel Emelyanov } 342e07c469eSHugh Dickins sbinfo->free_ispace -= BOGO_INODE_SIZE; 343bb3e96d6SByron Stanoszek } 344e809d5f0SChris Down if (inop) { 345e809d5f0SChris Down ino = sbinfo->next_ino++; 346e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 347e809d5f0SChris Down ino = sbinfo->next_ino++; 348ea3271f7SChris Down if (unlikely(!sbinfo->full_inums && 349ea3271f7SChris Down ino > UINT_MAX)) { 350e809d5f0SChris Down /* 351e809d5f0SChris Down * Emulate get_next_ino uint wraparound for 352e809d5f0SChris Down * compatibility 353e809d5f0SChris Down */ 354ea3271f7SChris Down if (IS_ENABLED(CONFIG_64BIT)) 355ea3271f7SChris Down pr_warn("%s: inode number overflow on device %d, consider using inode64 mount option\n", 356ea3271f7SChris Down __func__, MINOR(sb->s_dev)); 357ea3271f7SChris Down sbinfo->next_ino = 1; 358ea3271f7SChris Down ino = sbinfo->next_ino++; 3595b04c689SPavel Emelyanov } 360e809d5f0SChris Down *inop = ino; 361e809d5f0SChris Down } 362bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 363e809d5f0SChris Down } else if (inop) { 364e809d5f0SChris Down /* 365e809d5f0SChris Down * __shmem_file_setup, one of our callers, is lock-free: it 366e809d5f0SChris Down * doesn't hold stat_lock in shmem_reserve_inode since 367e809d5f0SChris Down * max_inodes is always 0, and is called from potentially 368e809d5f0SChris Down * unknown contexts. As such, use a per-cpu batched allocator 369e809d5f0SChris Down * which doesn't require the per-sb stat_lock unless we are at 370e809d5f0SChris Down * the batch boundary. 371ea3271f7SChris Down * 372ea3271f7SChris Down * We don't need to worry about inode{32,64} since SB_KERNMOUNT 373ea3271f7SChris Down * shmem mounts are not exposed to userspace, so we don't need 374ea3271f7SChris Down * to worry about things like glibc compatibility. 375e809d5f0SChris Down */ 376e809d5f0SChris Down ino_t *next_ino; 377bf11b9a8SSebastian Andrzej Siewior 378e809d5f0SChris Down next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu()); 379e809d5f0SChris Down ino = *next_ino; 380e809d5f0SChris Down if (unlikely(ino % SHMEM_INO_BATCH == 0)) { 381bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 382e809d5f0SChris Down ino = sbinfo->next_ino; 383e809d5f0SChris Down sbinfo->next_ino += SHMEM_INO_BATCH; 384bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 385e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 386e809d5f0SChris Down ino++; 387e809d5f0SChris Down } 388e809d5f0SChris Down *inop = ino; 389e809d5f0SChris Down *next_ino = ++ino; 390e809d5f0SChris Down put_cpu(); 391e809d5f0SChris Down } 392e809d5f0SChris Down 3935b04c689SPavel Emelyanov return 0; 3945b04c689SPavel Emelyanov } 3955b04c689SPavel Emelyanov 3962daf18a7SHugh Dickins static void shmem_free_inode(struct super_block *sb, size_t freed_ispace) 3975b04c689SPavel Emelyanov { 3985b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3995b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 400bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 4012daf18a7SHugh Dickins sbinfo->free_ispace += BOGO_INODE_SIZE + freed_ispace; 402bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 4035b04c689SPavel Emelyanov } 4045b04c689SPavel Emelyanov } 4055b04c689SPavel Emelyanov 40646711810SRandy Dunlap /** 40741ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 4081da177e4SLinus Torvalds * @inode: inode to recalc 4093c1b7528SHugh Dickins * @alloced: the change in number of pages allocated to inode 4103c1b7528SHugh Dickins * @swapped: the change in number of pages swapped from inode 4111da177e4SLinus Torvalds * 4121da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 4131da177e4SLinus Torvalds * undirtied hole pages behind our back. 4141da177e4SLinus Torvalds * 4151da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 4161da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 4171da177e4SLinus Torvalds */ 4183c1b7528SHugh Dickins static void shmem_recalc_inode(struct inode *inode, long alloced, long swapped) 4191da177e4SLinus Torvalds { 4201da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 4211da177e4SLinus Torvalds long freed; 4221da177e4SLinus Torvalds 4233c1b7528SHugh Dickins spin_lock(&info->lock); 4243c1b7528SHugh Dickins info->alloced += alloced; 4253c1b7528SHugh Dickins info->swapped += swapped; 4263c1b7528SHugh Dickins freed = info->alloced - info->swapped - 4273c1b7528SHugh Dickins READ_ONCE(inode->i_mapping->nrpages); 4283c1b7528SHugh Dickins /* 4293c1b7528SHugh Dickins * Special case: whereas normally shmem_recalc_inode() is called 4303c1b7528SHugh Dickins * after i_mapping->nrpages has already been adjusted (up or down), 4313c1b7528SHugh Dickins * shmem_writepage() has to raise swapped before nrpages is lowered - 4323c1b7528SHugh Dickins * to stop a racing shmem_recalc_inode() from thinking that a page has 4333c1b7528SHugh Dickins * been freed. Compensate here, to avoid the need for a followup call. 4343c1b7528SHugh Dickins */ 4353c1b7528SHugh Dickins if (swapped > 0) 4363c1b7528SHugh Dickins freed += swapped; 4373c1b7528SHugh Dickins if (freed > 0) 4381da177e4SLinus Torvalds info->alloced -= freed; 4393c1b7528SHugh Dickins spin_unlock(&info->lock); 4403c1b7528SHugh Dickins 4413c1b7528SHugh Dickins /* The quota case may block */ 4423c1b7528SHugh Dickins if (freed > 0) 4430f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 446800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 447800d8c63SKirill A. Shutemov { 448509f0069SHugh Dickins struct address_space *mapping = inode->i_mapping; 449800d8c63SKirill A. Shutemov 450c7e263abSLukas Czerner if (shmem_inode_acct_block(inode, pages)) 451800d8c63SKirill A. Shutemov return false; 452b1cc94abSMike Rapoport 453aaa52e34SHugh Dickins /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ 454509f0069SHugh Dickins xa_lock_irq(&mapping->i_pages); 455509f0069SHugh Dickins mapping->nrpages += pages; 456509f0069SHugh Dickins xa_unlock_irq(&mapping->i_pages); 457aaa52e34SHugh Dickins 4583c1b7528SHugh Dickins shmem_recalc_inode(inode, pages, 0); 459800d8c63SKirill A. Shutemov return true; 460800d8c63SKirill A. Shutemov } 461800d8c63SKirill A. Shutemov 462800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 463800d8c63SKirill A. Shutemov { 4643c1b7528SHugh Dickins /* pages argument is currently unused: keep it to help debugging */ 4656ffcd825SMatthew Wilcox (Oracle) /* nrpages adjustment done by __filemap_remove_folio() or caller */ 466aaa52e34SHugh Dickins 4673c1b7528SHugh Dickins shmem_recalc_inode(inode, 0, 0); 468800d8c63SKirill A. Shutemov } 469800d8c63SKirill A. Shutemov 4707a5d0fbbSHugh Dickins /* 47162f945b6SMatthew Wilcox * Replace item expected in xarray by a new item, while holding xa_lock. 4727a5d0fbbSHugh Dickins */ 47362f945b6SMatthew Wilcox static int shmem_replace_entry(struct address_space *mapping, 4747a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 4757a5d0fbbSHugh Dickins { 47662f945b6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 4776dbaf22cSJohannes Weiner void *item; 4787a5d0fbbSHugh Dickins 4797a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 4806dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 48162f945b6SMatthew Wilcox item = xas_load(&xas); 4827a5d0fbbSHugh Dickins if (item != expected) 4837a5d0fbbSHugh Dickins return -ENOENT; 48462f945b6SMatthew Wilcox xas_store(&xas, replacement); 4857a5d0fbbSHugh Dickins return 0; 4867a5d0fbbSHugh Dickins } 4877a5d0fbbSHugh Dickins 4887a5d0fbbSHugh Dickins /* 489d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 490d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 491d1899228SHugh Dickins * 492d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 493d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 494d1899228SHugh Dickins */ 495d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 496d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 497d1899228SHugh Dickins { 498a12831bfSMatthew Wilcox return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); 499d1899228SHugh Dickins } 500d1899228SHugh Dickins 501d1899228SHugh Dickins /* 5025a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 5035a6e75f8SKirill A. Shutemov * 5045a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 5055a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 5065a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 5075a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 5085a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 5095a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 5105a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 5115a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 5125a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 5135a6e75f8SKirill A. Shutemov */ 5145a6e75f8SKirill A. Shutemov 5155a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 5165a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 5175a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 5185a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 5195a6e75f8SKirill A. Shutemov 5205a6e75f8SKirill A. Shutemov /* 5215a6e75f8SKirill A. Shutemov * Special values. 5225a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 5235a6e75f8SKirill A. Shutemov * 5245a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 5255a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 5265a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 5275a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 5285a6e75f8SKirill A. Shutemov * 5295a6e75f8SKirill A. Shutemov */ 5305a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 5315a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 5325a6e75f8SKirill A. Shutemov 533396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 5345a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 5355a6e75f8SKirill A. Shutemov 5365e6e5a12SHugh Dickins static int shmem_huge __read_mostly = SHMEM_HUGE_NEVER; 5375a6e75f8SKirill A. Shutemov 5382cf13384SDavid Stevens bool shmem_is_huge(struct inode *inode, pgoff_t index, bool shmem_huge_force, 5392cf13384SDavid Stevens struct mm_struct *mm, unsigned long vm_flags) 540c852023eSHugh Dickins { 541c852023eSHugh Dickins loff_t i_size; 542c852023eSHugh Dickins 543f7cd16a5SXavier Roche if (!S_ISREG(inode->i_mode)) 544f7cd16a5SXavier Roche return false; 5452cf13384SDavid Stevens if (mm && ((vm_flags & VM_NOHUGEPAGE) || test_bit(MMF_DISABLE_THP, &mm->flags))) 546c852023eSHugh Dickins return false; 5477c6c6cc4SZach O'Keefe if (shmem_huge == SHMEM_HUGE_DENY) 5487c6c6cc4SZach O'Keefe return false; 5493de0c269SZach O'Keefe if (shmem_huge_force || shmem_huge == SHMEM_HUGE_FORCE) 5503de0c269SZach O'Keefe return true; 5515e6e5a12SHugh Dickins 5525e6e5a12SHugh Dickins switch (SHMEM_SB(inode->i_sb)->huge) { 553c852023eSHugh Dickins case SHMEM_HUGE_ALWAYS: 554c852023eSHugh Dickins return true; 555c852023eSHugh Dickins case SHMEM_HUGE_WITHIN_SIZE: 556de6ee659SLiu Yuntao index = round_up(index + 1, HPAGE_PMD_NR); 557c852023eSHugh Dickins i_size = round_up(i_size_read(inode), PAGE_SIZE); 558de6ee659SLiu Yuntao if (i_size >> PAGE_SHIFT >= index) 559c852023eSHugh Dickins return true; 560c852023eSHugh Dickins fallthrough; 561c852023eSHugh Dickins case SHMEM_HUGE_ADVISE: 5622cf13384SDavid Stevens if (mm && (vm_flags & VM_HUGEPAGE)) 5635e6e5a12SHugh Dickins return true; 5645e6e5a12SHugh Dickins fallthrough; 565c852023eSHugh Dickins default: 566c852023eSHugh Dickins return false; 567c852023eSHugh Dickins } 568c852023eSHugh Dickins } 5695a6e75f8SKirill A. Shutemov 570e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) 5715a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 5725a6e75f8SKirill A. Shutemov { 5735a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 5745a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 5755a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 5765a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 5775a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 5785a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 5795a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 5805a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 5815a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 5825a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 5835a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 5845a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 5855a6e75f8SKirill A. Shutemov return -EINVAL; 5865a6e75f8SKirill A. Shutemov } 587e5f2249aSArnd Bergmann #endif 5885a6e75f8SKirill A. Shutemov 589e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 5905a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 5915a6e75f8SKirill A. Shutemov { 5925a6e75f8SKirill A. Shutemov switch (huge) { 5935a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 5945a6e75f8SKirill A. Shutemov return "never"; 5955a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 5965a6e75f8SKirill A. Shutemov return "always"; 5975a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 5985a6e75f8SKirill A. Shutemov return "within_size"; 5995a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 6005a6e75f8SKirill A. Shutemov return "advise"; 6015a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 6025a6e75f8SKirill A. Shutemov return "deny"; 6035a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 6045a6e75f8SKirill A. Shutemov return "force"; 6055a6e75f8SKirill A. Shutemov default: 6065a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 6075a6e75f8SKirill A. Shutemov return "bad_val"; 6085a6e75f8SKirill A. Shutemov } 6095a6e75f8SKirill A. Shutemov } 610f1f5929cSJérémy Lefaure #endif 6115a6e75f8SKirill A. Shutemov 612779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 613779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 614779750d2SKirill A. Shutemov { 615779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 616253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 617779750d2SKirill A. Shutemov struct inode *inode; 618779750d2SKirill A. Shutemov struct shmem_inode_info *info; 61905624571SMatthew Wilcox (Oracle) struct folio *folio; 620779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 62162c9827cSGang Li int split = 0; 622779750d2SKirill A. Shutemov 623779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 624779750d2SKirill A. Shutemov return SHRINK_STOP; 625779750d2SKirill A. Shutemov 626779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 627779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 628779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 629779750d2SKirill A. Shutemov 630779750d2SKirill A. Shutemov /* pin the inode */ 631779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 632779750d2SKirill A. Shutemov 633779750d2SKirill A. Shutemov /* inode is about to be evicted */ 634779750d2SKirill A. Shutemov if (!inode) { 635779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 636779750d2SKirill A. Shutemov goto next; 637779750d2SKirill A. Shutemov } 638779750d2SKirill A. Shutemov 639779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 640779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 641779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 642253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 643779750d2SKirill A. Shutemov goto next; 644779750d2SKirill A. Shutemov } 645779750d2SKirill A. Shutemov 646779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 647779750d2SKirill A. Shutemov next: 64862c9827cSGang Li sbinfo->shrinklist_len--; 649779750d2SKirill A. Shutemov if (!--batch) 650779750d2SKirill A. Shutemov break; 651779750d2SKirill A. Shutemov } 652779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 653779750d2SKirill A. Shutemov 654253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 655253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 656253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 657253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 658253fd0f0SKirill A. Shutemov iput(inode); 659253fd0f0SKirill A. Shutemov } 660253fd0f0SKirill A. Shutemov 661779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 662779750d2SKirill A. Shutemov int ret; 66305624571SMatthew Wilcox (Oracle) pgoff_t index; 664779750d2SKirill A. Shutemov 665779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 666779750d2SKirill A. Shutemov inode = &info->vfs_inode; 667779750d2SKirill A. Shutemov 668b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 66962c9827cSGang Li goto move_back; 670779750d2SKirill A. Shutemov 67105624571SMatthew Wilcox (Oracle) index = (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT; 67205624571SMatthew Wilcox (Oracle) folio = filemap_get_folio(inode->i_mapping, index); 67366dabbb6SChristoph Hellwig if (IS_ERR(folio)) 674779750d2SKirill A. Shutemov goto drop; 675779750d2SKirill A. Shutemov 676b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 67705624571SMatthew Wilcox (Oracle) if (!folio_test_large(folio)) { 67805624571SMatthew Wilcox (Oracle) folio_put(folio); 679779750d2SKirill A. Shutemov goto drop; 680779750d2SKirill A. Shutemov } 681779750d2SKirill A. Shutemov 682b3cd54b2SKirill A. Shutemov /* 68362c9827cSGang Li * Move the inode on the list back to shrinklist if we failed 68462c9827cSGang Li * to lock the page at this time. 685b3cd54b2SKirill A. Shutemov * 686b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 687b3cd54b2SKirill A. Shutemov * reclaim path. 688b3cd54b2SKirill A. Shutemov */ 68905624571SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) { 69005624571SMatthew Wilcox (Oracle) folio_put(folio); 69162c9827cSGang Li goto move_back; 692b3cd54b2SKirill A. Shutemov } 693b3cd54b2SKirill A. Shutemov 694d788f5b3SMatthew Wilcox (Oracle) ret = split_folio(folio); 69505624571SMatthew Wilcox (Oracle) folio_unlock(folio); 69605624571SMatthew Wilcox (Oracle) folio_put(folio); 697779750d2SKirill A. Shutemov 69862c9827cSGang Li /* If split failed move the inode on the list back to shrinklist */ 699b3cd54b2SKirill A. Shutemov if (ret) 70062c9827cSGang Li goto move_back; 701779750d2SKirill A. Shutemov 702779750d2SKirill A. Shutemov split++; 703779750d2SKirill A. Shutemov drop: 704779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 70562c9827cSGang Li goto put; 70662c9827cSGang Li move_back: 70762c9827cSGang Li /* 70862c9827cSGang Li * Make sure the inode is either on the global list or deleted 70962c9827cSGang Li * from any local list before iput() since it could be deleted 71062c9827cSGang Li * in another thread once we put the inode (then the local list 71162c9827cSGang Li * is corrupted). 71262c9827cSGang Li */ 71362c9827cSGang Li spin_lock(&sbinfo->shrinklist_lock); 71462c9827cSGang Li list_move(&info->shrinklist, &sbinfo->shrinklist); 71562c9827cSGang Li sbinfo->shrinklist_len++; 71662c9827cSGang Li spin_unlock(&sbinfo->shrinklist_lock); 71762c9827cSGang Li put: 718779750d2SKirill A. Shutemov iput(inode); 719779750d2SKirill A. Shutemov } 720779750d2SKirill A. Shutemov 721779750d2SKirill A. Shutemov return split; 722779750d2SKirill A. Shutemov } 723779750d2SKirill A. Shutemov 724779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 725779750d2SKirill A. Shutemov struct shrink_control *sc) 726779750d2SKirill A. Shutemov { 727779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 728779750d2SKirill A. Shutemov 729779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 730779750d2SKirill A. Shutemov return SHRINK_STOP; 731779750d2SKirill A. Shutemov 732779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 733779750d2SKirill A. Shutemov } 734779750d2SKirill A. Shutemov 735779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 736779750d2SKirill A. Shutemov struct shrink_control *sc) 737779750d2SKirill A. Shutemov { 738779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 739779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 740779750d2SKirill A. Shutemov } 741396bcc52SMatthew Wilcox (Oracle) #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 7425a6e75f8SKirill A. Shutemov 7435a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 7445a6e75f8SKirill A. Shutemov 7452cf13384SDavid Stevens bool shmem_is_huge(struct inode *inode, pgoff_t index, bool shmem_huge_force, 7462cf13384SDavid Stevens struct mm_struct *mm, unsigned long vm_flags) 7475e6e5a12SHugh Dickins { 7485e6e5a12SHugh Dickins return false; 7495e6e5a12SHugh Dickins } 7505e6e5a12SHugh Dickins 751779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 752779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 753779750d2SKirill A. Shutemov { 754779750d2SKirill A. Shutemov return 0; 755779750d2SKirill A. Shutemov } 756396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 7575a6e75f8SKirill A. Shutemov 7585a6e75f8SKirill A. Shutemov /* 7592bb876b5SMatthew Wilcox (Oracle) * Like filemap_add_folio, but error if expected item has gone. 76046f65ec1SHugh Dickins */ 761b7dd44a1SMatthew Wilcox (Oracle) static int shmem_add_to_page_cache(struct folio *folio, 76246f65ec1SHugh Dickins struct address_space *mapping, 7633fea5a49SJohannes Weiner pgoff_t index, void *expected, gfp_t gfp, 7643fea5a49SJohannes Weiner struct mm_struct *charge_mm) 76546f65ec1SHugh Dickins { 766b7dd44a1SMatthew Wilcox (Oracle) XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio)); 767b7dd44a1SMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 7683fea5a49SJohannes Weiner int error; 76946f65ec1SHugh Dickins 770b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(index != round_down(index, nr), folio); 771b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 772b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio); 773b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON(expected && folio_test_large(folio)); 77446f65ec1SHugh Dickins 775b7dd44a1SMatthew Wilcox (Oracle) folio_ref_add(folio, nr); 776b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = mapping; 777b7dd44a1SMatthew Wilcox (Oracle) folio->index = index; 77846f65ec1SHugh Dickins 779b7dd44a1SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) { 780b7dd44a1SMatthew Wilcox (Oracle) error = mem_cgroup_charge(folio, charge_mm, gfp); 7813fea5a49SJohannes Weiner if (error) { 782b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 7833fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK); 7843fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK_CHARGE); 7853fea5a49SJohannes Weiner } 7863fea5a49SJohannes Weiner goto error; 7873fea5a49SJohannes Weiner } 7884c6355b2SJohannes Weiner } 789b7dd44a1SMatthew Wilcox (Oracle) folio_throttle_swaprate(folio, gfp); 7903fea5a49SJohannes Weiner 791552446a4SMatthew Wilcox do { 792552446a4SMatthew Wilcox xas_lock_irq(&xas); 7936b24ca4aSMatthew Wilcox (Oracle) if (expected != xas_find_conflict(&xas)) { 794552446a4SMatthew Wilcox xas_set_err(&xas, -EEXIST); 7956b24ca4aSMatthew Wilcox (Oracle) goto unlock; 7966b24ca4aSMatthew Wilcox (Oracle) } 7976b24ca4aSMatthew Wilcox (Oracle) if (expected && xas_find_conflict(&xas)) { 7986b24ca4aSMatthew Wilcox (Oracle) xas_set_err(&xas, -EEXIST); 7996b24ca4aSMatthew Wilcox (Oracle) goto unlock; 8006b24ca4aSMatthew Wilcox (Oracle) } 801b7dd44a1SMatthew Wilcox (Oracle) xas_store(&xas, folio); 802552446a4SMatthew Wilcox if (xas_error(&xas)) 803552446a4SMatthew Wilcox goto unlock; 804b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 805800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 806b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr); 807552446a4SMatthew Wilcox } 808552446a4SMatthew Wilcox mapping->nrpages += nr; 809b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr); 810b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, nr); 811552446a4SMatthew Wilcox unlock: 812552446a4SMatthew Wilcox xas_unlock_irq(&xas); 813552446a4SMatthew Wilcox } while (xas_nomem(&xas, gfp)); 814552446a4SMatthew Wilcox 815552446a4SMatthew Wilcox if (xas_error(&xas)) { 8163fea5a49SJohannes Weiner error = xas_error(&xas); 8173fea5a49SJohannes Weiner goto error; 81846f65ec1SHugh Dickins } 819552446a4SMatthew Wilcox 820552446a4SMatthew Wilcox return 0; 8213fea5a49SJohannes Weiner error: 822b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = NULL; 823b7dd44a1SMatthew Wilcox (Oracle) folio_ref_sub(folio, nr); 8243fea5a49SJohannes Weiner return error; 82546f65ec1SHugh Dickins } 82646f65ec1SHugh Dickins 82746f65ec1SHugh Dickins /* 8284cd400fdSMatthew Wilcox (Oracle) * Like delete_from_page_cache, but substitutes swap for @folio. 8296922c0c7SHugh Dickins */ 8304cd400fdSMatthew Wilcox (Oracle) static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) 8316922c0c7SHugh Dickins { 8324cd400fdSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 8334cd400fdSMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 8346922c0c7SHugh Dickins int error; 8356922c0c7SHugh Dickins 836b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 8374cd400fdSMatthew Wilcox (Oracle) error = shmem_replace_entry(mapping, folio->index, folio, radswap); 8384cd400fdSMatthew Wilcox (Oracle) folio->mapping = NULL; 8394cd400fdSMatthew Wilcox (Oracle) mapping->nrpages -= nr; 8404cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr); 8414cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr); 842b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 8434cd400fdSMatthew Wilcox (Oracle) folio_put(folio); 8446922c0c7SHugh Dickins BUG_ON(error); 8456922c0c7SHugh Dickins } 8466922c0c7SHugh Dickins 8476922c0c7SHugh Dickins /* 848c121d3bbSMatthew Wilcox * Remove swap entry from page cache, free the swap and its page cache. 8497a5d0fbbSHugh Dickins */ 8507a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 8517a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 8527a5d0fbbSHugh Dickins { 8536dbaf22cSJohannes Weiner void *old; 8547a5d0fbbSHugh Dickins 85555f3f7eaSMatthew Wilcox old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); 8566dbaf22cSJohannes Weiner if (old != radswap) 8576dbaf22cSJohannes Weiner return -ENOENT; 8587a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 8596dbaf22cSJohannes Weiner return 0; 8607a5d0fbbSHugh Dickins } 8617a5d0fbbSHugh Dickins 8627a5d0fbbSHugh Dickins /* 8636a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 86448131e03SVlastimil Babka * given offsets are swapped out. 8656a15a370SVlastimil Babka * 8669608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 8676a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 8686a15a370SVlastimil Babka */ 86948131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 87048131e03SVlastimil Babka pgoff_t start, pgoff_t end) 8716a15a370SVlastimil Babka { 8727ae3424fSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start); 8736a15a370SVlastimil Babka struct page *page; 87448131e03SVlastimil Babka unsigned long swapped = 0; 8756a15a370SVlastimil Babka 8766a15a370SVlastimil Babka rcu_read_lock(); 8777ae3424fSMatthew Wilcox xas_for_each(&xas, page, end - 1) { 8787ae3424fSMatthew Wilcox if (xas_retry(&xas, page)) 8792cf938aaSMatthew Wilcox continue; 8803159f943SMatthew Wilcox if (xa_is_value(page)) 8816a15a370SVlastimil Babka swapped++; 8826a15a370SVlastimil Babka 8836a15a370SVlastimil Babka if (need_resched()) { 8847ae3424fSMatthew Wilcox xas_pause(&xas); 8856a15a370SVlastimil Babka cond_resched_rcu(); 8866a15a370SVlastimil Babka } 8876a15a370SVlastimil Babka } 8886a15a370SVlastimil Babka 8896a15a370SVlastimil Babka rcu_read_unlock(); 8906a15a370SVlastimil Babka 8916a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 8926a15a370SVlastimil Babka } 8936a15a370SVlastimil Babka 8946a15a370SVlastimil Babka /* 89548131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 89648131e03SVlastimil Babka * given vma is swapped out. 89748131e03SVlastimil Babka * 8989608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 89948131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 90048131e03SVlastimil Babka */ 90148131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 90248131e03SVlastimil Babka { 90348131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 90448131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 90548131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 90648131e03SVlastimil Babka unsigned long swapped; 90748131e03SVlastimil Babka 90848131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 90948131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 91048131e03SVlastimil Babka 91148131e03SVlastimil Babka /* 91248131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 91348131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 91448131e03SVlastimil Babka * already track. 91548131e03SVlastimil Babka */ 91648131e03SVlastimil Babka if (!swapped) 91748131e03SVlastimil Babka return 0; 91848131e03SVlastimil Babka 91948131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 92048131e03SVlastimil Babka return swapped << PAGE_SHIFT; 92148131e03SVlastimil Babka 92248131e03SVlastimil Babka /* Here comes the more involved part */ 92302399c88SPeter Xu return shmem_partial_swap_usage(mapping, vma->vm_pgoff, 92402399c88SPeter Xu vma->vm_pgoff + vma_pages(vma)); 92548131e03SVlastimil Babka } 92648131e03SVlastimil Babka 92748131e03SVlastimil Babka /* 92824513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 92924513264SHugh Dickins */ 93024513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 93124513264SHugh Dickins { 932105c988fSMatthew Wilcox (Oracle) struct folio_batch fbatch; 93324513264SHugh Dickins pgoff_t index = 0; 93424513264SHugh Dickins 935105c988fSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 93624513264SHugh Dickins /* 93724513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 93824513264SHugh Dickins */ 939105c988fSMatthew Wilcox (Oracle) while (!mapping_unevictable(mapping) && 940105c988fSMatthew Wilcox (Oracle) filemap_get_folios(mapping, &index, ~0UL, &fbatch)) { 941105c988fSMatthew Wilcox (Oracle) check_move_unevictable_folios(&fbatch); 942105c988fSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 94324513264SHugh Dickins cond_resched(); 94424513264SHugh Dickins } 9457a5d0fbbSHugh Dickins } 9467a5d0fbbSHugh Dickins 947b9a8a419SMatthew Wilcox (Oracle) static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) 94871725ed1SHugh Dickins { 949b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 95071725ed1SHugh Dickins 951b9a8a419SMatthew Wilcox (Oracle) /* 952a7f5862cSMatthew Wilcox (Oracle) * At first avoid shmem_get_folio(,,,SGP_READ): that fails 95381914affSHugh Dickins * beyond i_size, and reports fallocated folios as holes. 954b9a8a419SMatthew Wilcox (Oracle) */ 95581914affSHugh Dickins folio = filemap_get_entry(inode->i_mapping, index); 95681914affSHugh Dickins if (!folio) 957b9a8a419SMatthew Wilcox (Oracle) return folio; 95881914affSHugh Dickins if (!xa_is_value(folio)) { 95981914affSHugh Dickins folio_lock(folio); 96081914affSHugh Dickins if (folio->mapping == inode->i_mapping) 96181914affSHugh Dickins return folio; 96281914affSHugh Dickins /* The folio has been swapped out */ 96381914affSHugh Dickins folio_unlock(folio); 96481914affSHugh Dickins folio_put(folio); 96581914affSHugh Dickins } 966b9a8a419SMatthew Wilcox (Oracle) /* 96781914affSHugh Dickins * But read a folio back from swap if any of it is within i_size 968b9a8a419SMatthew Wilcox (Oracle) * (although in some cases this is just a waste of time). 969b9a8a419SMatthew Wilcox (Oracle) */ 970a7f5862cSMatthew Wilcox (Oracle) folio = NULL; 971a7f5862cSMatthew Wilcox (Oracle) shmem_get_folio(inode, index, &folio, SGP_READ); 972a7f5862cSMatthew Wilcox (Oracle) return folio; 97371725ed1SHugh Dickins } 97471725ed1SHugh Dickins 97571725ed1SHugh Dickins /* 9767f4446eeSMatthew Wilcox * Remove range of pages and swap entries from page cache, and free them. 9771635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 9787a5d0fbbSHugh Dickins */ 9791635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 9801635f6a7SHugh Dickins bool unfalloc) 9811da177e4SLinus Torvalds { 982285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 9831da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 98409cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 98509cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 9860e499ed3SMatthew Wilcox (Oracle) struct folio_batch fbatch; 9877a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 988b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 989b9a8a419SMatthew Wilcox (Oracle) bool same_folio; 9907a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 991285b2c4fSHugh Dickins pgoff_t index; 992bda97eabSHugh Dickins int i; 9931da177e4SLinus Torvalds 99483e4fa9cSHugh Dickins if (lend == -1) 99583e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 996bda97eabSHugh Dickins 997d144bf62SHugh Dickins if (info->fallocend > start && info->fallocend <= end && !unfalloc) 998d144bf62SHugh Dickins info->fallocend = start; 999d144bf62SHugh Dickins 100051dcbdacSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1001bda97eabSHugh Dickins index = start; 10023392ca12SVishal Moola (Oracle) while (index < end && find_lock_entries(mapping, &index, end - 1, 100351dcbdacSMatthew Wilcox (Oracle) &fbatch, indices)) { 100451dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 1005b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 1006bda97eabSHugh Dickins 10077b774aabSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 10081635f6a7SHugh Dickins if (unfalloc) 10091635f6a7SHugh Dickins continue; 10107a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 10113392ca12SVishal Moola (Oracle) indices[i], folio); 10127a5d0fbbSHugh Dickins continue; 10137a5d0fbbSHugh Dickins } 10147a5d0fbbSHugh Dickins 10157b774aabSMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) 10161e84a3d9SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 10177b774aabSMatthew Wilcox (Oracle) folio_unlock(folio); 1018bda97eabSHugh Dickins } 101951dcbdacSMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 102051dcbdacSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1021bda97eabSHugh Dickins cond_resched(); 1022bda97eabSHugh Dickins } 1023bda97eabSHugh Dickins 102444bcabd7SHugh Dickins /* 102544bcabd7SHugh Dickins * When undoing a failed fallocate, we want none of the partial folio 102644bcabd7SHugh Dickins * zeroing and splitting below, but shall want to truncate the whole 102744bcabd7SHugh Dickins * folio when !uptodate indicates that it was added by this fallocate, 102844bcabd7SHugh Dickins * even when [lstart, lend] covers only a part of the folio. 102944bcabd7SHugh Dickins */ 103044bcabd7SHugh Dickins if (unfalloc) 103144bcabd7SHugh Dickins goto whole_folios; 103244bcabd7SHugh Dickins 1033b9a8a419SMatthew Wilcox (Oracle) same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT); 1034b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lstart >> PAGE_SHIFT); 1035b9a8a419SMatthew Wilcox (Oracle) if (folio) { 1036b9a8a419SMatthew Wilcox (Oracle) same_folio = lend < folio_pos(folio) + folio_size(folio); 1037b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1038b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) { 1039b9a8a419SMatthew Wilcox (Oracle) start = folio->index + folio_nr_pages(folio); 1040b9a8a419SMatthew Wilcox (Oracle) if (same_folio) 1041b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 104283e4fa9cSHugh Dickins } 1043b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 1044b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 1045b9a8a419SMatthew Wilcox (Oracle) folio = NULL; 1046bda97eabSHugh Dickins } 1047b9a8a419SMatthew Wilcox (Oracle) 1048b9a8a419SMatthew Wilcox (Oracle) if (!same_folio) 1049b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT); 1050b9a8a419SMatthew Wilcox (Oracle) if (folio) { 1051b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1052b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) 1053b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 1054b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 1055b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 1056bda97eabSHugh Dickins } 1057bda97eabSHugh Dickins 105844bcabd7SHugh Dickins whole_folios: 105944bcabd7SHugh Dickins 1060bda97eabSHugh Dickins index = start; 1061b1a36650SHugh Dickins while (index < end) { 1062bda97eabSHugh Dickins cond_resched(); 10630cd6144aSJohannes Weiner 10649fb6beeaSVishal Moola (Oracle) if (!find_get_entries(mapping, &index, end - 1, &fbatch, 1065cf2039afSMatthew Wilcox (Oracle) indices)) { 1066b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 1067b1a36650SHugh Dickins if (index == start || end != -1) 1068bda97eabSHugh Dickins break; 1069b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 1070bda97eabSHugh Dickins index = start; 1071bda97eabSHugh Dickins continue; 1072bda97eabSHugh Dickins } 10730e499ed3SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 1074b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 1075bda97eabSHugh Dickins 10760e499ed3SMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 10771635f6a7SHugh Dickins if (unfalloc) 10781635f6a7SHugh Dickins continue; 10799fb6beeaSVishal Moola (Oracle) if (shmem_free_swap(mapping, indices[i], folio)) { 1080b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 10819fb6beeaSVishal Moola (Oracle) index = indices[i]; 1082b1a36650SHugh Dickins break; 1083b1a36650SHugh Dickins } 1084b1a36650SHugh Dickins nr_swaps_freed++; 10857a5d0fbbSHugh Dickins continue; 10867a5d0fbbSHugh Dickins } 10877a5d0fbbSHugh Dickins 10880e499ed3SMatthew Wilcox (Oracle) folio_lock(folio); 1089800d8c63SKirill A. Shutemov 10900e499ed3SMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) { 10910e499ed3SMatthew Wilcox (Oracle) if (folio_mapping(folio) != mapping) { 1092b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 10930e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 10949fb6beeaSVishal Moola (Oracle) index = indices[i]; 1095b1a36650SHugh Dickins break; 10967a5d0fbbSHugh Dickins } 10970e499ed3SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_writeback(folio), 10980e499ed3SMatthew Wilcox (Oracle) folio); 10990e499ed3SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 110071725ed1SHugh Dickins } 11010e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 1102bda97eabSHugh Dickins } 11030e499ed3SMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 11040e499ed3SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1105bda97eabSHugh Dickins } 110694c1e62dSHugh Dickins 11073c1b7528SHugh Dickins shmem_recalc_inode(inode, 0, -nr_swaps_freed); 11081635f6a7SHugh Dickins } 11091da177e4SLinus Torvalds 11101635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 11111635f6a7SHugh Dickins { 11121635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 1113078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 111436f05cabSJeff Layton inode_inc_iversion(inode); 11151da177e4SLinus Torvalds } 111694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 11171da177e4SLinus Torvalds 1118b74d24f7SChristian Brauner static int shmem_getattr(struct mnt_idmap *idmap, 1119549c7297SChristian Brauner const struct path *path, struct kstat *stat, 1120a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 112144a30220SYu Zhao { 1122a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 112344a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 112444a30220SYu Zhao 11253c1b7528SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) 11263c1b7528SHugh Dickins shmem_recalc_inode(inode, 0, 0); 11273c1b7528SHugh Dickins 1128e408e695STheodore Ts'o if (info->fsflags & FS_APPEND_FL) 1129e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_APPEND; 1130e408e695STheodore Ts'o if (info->fsflags & FS_IMMUTABLE_FL) 1131e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_IMMUTABLE; 1132e408e695STheodore Ts'o if (info->fsflags & FS_NODUMP_FL) 1133e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_NODUMP; 1134e408e695STheodore Ts'o stat->attributes_mask |= (STATX_ATTR_APPEND | 1135e408e695STheodore Ts'o STATX_ATTR_IMMUTABLE | 1136e408e695STheodore Ts'o STATX_ATTR_NODUMP); 11377a80e5b8SGiuseppe Scrivano generic_fillattr(idmap, inode, stat); 113889fdcd26SYang Shi 11392cf13384SDavid Stevens if (shmem_is_huge(inode, 0, false, NULL, 0)) 114089fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 114189fdcd26SYang Shi 1142f7cd16a5SXavier Roche if (request_mask & STATX_BTIME) { 1143f7cd16a5SXavier Roche stat->result_mask |= STATX_BTIME; 1144f7cd16a5SXavier Roche stat->btime.tv_sec = info->i_crtime.tv_sec; 1145f7cd16a5SXavier Roche stat->btime.tv_nsec = info->i_crtime.tv_nsec; 1146f7cd16a5SXavier Roche } 1147f7cd16a5SXavier Roche 114844a30220SYu Zhao return 0; 114944a30220SYu Zhao } 115044a30220SYu Zhao 1151c1632a0fSChristian Brauner static int shmem_setattr(struct mnt_idmap *idmap, 1152549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 11531da177e4SLinus Torvalds { 115475c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 115540e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 11561da177e4SLinus Torvalds int error; 115736f05cabSJeff Layton bool update_mtime = false; 115836f05cabSJeff Layton bool update_ctime = true; 11591da177e4SLinus Torvalds 11607a80e5b8SGiuseppe Scrivano error = setattr_prepare(idmap, dentry, attr); 1161db78b877SChristoph Hellwig if (error) 1162db78b877SChristoph Hellwig return error; 1163db78b877SChristoph Hellwig 11646fd73538SDaniel Verkamp if ((info->seals & F_SEAL_EXEC) && (attr->ia_valid & ATTR_MODE)) { 11656fd73538SDaniel Verkamp if ((inode->i_mode ^ attr->ia_mode) & 0111) { 11666fd73538SDaniel Verkamp return -EPERM; 11676fd73538SDaniel Verkamp } 11686fd73538SDaniel Verkamp } 11696fd73538SDaniel Verkamp 117094c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 117194c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 117294c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 11733889e6e7Snpiggin@suse.de 11749608703eSJan Kara /* protected by i_rwsem */ 117540e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 117640e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 117740e041a2SDavid Herrmann return -EPERM; 117840e041a2SDavid Herrmann 117994c1e62dSHugh Dickins if (newsize != oldsize) { 118077142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 118177142517SKonstantin Khlebnikov oldsize, newsize); 118277142517SKonstantin Khlebnikov if (error) 118377142517SKonstantin Khlebnikov return error; 118494c1e62dSHugh Dickins i_size_write(inode, newsize); 118536f05cabSJeff Layton update_mtime = true; 118636f05cabSJeff Layton } else { 118736f05cabSJeff Layton update_ctime = false; 118894c1e62dSHugh Dickins } 1189afa2db2fSJosef Bacik if (newsize <= oldsize) { 119094c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1191d0424c42SHugh Dickins if (oldsize > holebegin) 1192d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1193d0424c42SHugh Dickins holebegin, 0, 1); 1194d0424c42SHugh Dickins if (info->alloced) 1195d0424c42SHugh Dickins shmem_truncate_range(inode, 1196d0424c42SHugh Dickins newsize, (loff_t)-1); 119794c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1198d0424c42SHugh Dickins if (oldsize > holebegin) 1199d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1200d0424c42SHugh Dickins holebegin, 0, 1); 120194c1e62dSHugh Dickins } 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 1204e09764cfSCarlos Maiolino if (is_quota_modification(idmap, inode, attr)) { 1205e09764cfSCarlos Maiolino error = dquot_initialize(inode); 1206e09764cfSCarlos Maiolino if (error) 1207e09764cfSCarlos Maiolino return error; 1208e09764cfSCarlos Maiolino } 1209e09764cfSCarlos Maiolino 1210e09764cfSCarlos Maiolino /* Transfer quota accounting */ 1211e09764cfSCarlos Maiolino if (i_uid_needs_update(idmap, attr, inode) || 1212e09764cfSCarlos Maiolino i_gid_needs_update(idmap, attr, inode)) { 1213e09764cfSCarlos Maiolino error = dquot_transfer(idmap, inode, attr); 1214e09764cfSCarlos Maiolino 1215e09764cfSCarlos Maiolino if (error) 1216e09764cfSCarlos Maiolino return error; 1217e09764cfSCarlos Maiolino } 1218e09764cfSCarlos Maiolino 12197a80e5b8SGiuseppe Scrivano setattr_copy(idmap, inode, attr); 1220db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 12217a80e5b8SGiuseppe Scrivano error = posix_acl_chmod(idmap, dentry, inode->i_mode); 122236f05cabSJeff Layton if (!error && update_ctime) { 122336f05cabSJeff Layton inode->i_ctime = current_time(inode); 122436f05cabSJeff Layton if (update_mtime) 122536f05cabSJeff Layton inode->i_mtime = inode->i_ctime; 122636f05cabSJeff Layton inode_inc_iversion(inode); 122736f05cabSJeff Layton } 12281da177e4SLinus Torvalds return error; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12311f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1234779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 12352daf18a7SHugh Dickins size_t freed = 0; 12361da177e4SLinus Torvalds 123730e6a51dSHui Su if (shmem_mapping(inode->i_mapping)) { 12381da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 12391da177e4SLinus Torvalds inode->i_size = 0; 1240bc786390SHugh Dickins mapping_set_exiting(inode->i_mapping); 12413889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1242779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1243779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1244779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1245779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1246779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1247779750d2SKirill A. Shutemov } 1248779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1249779750d2SKirill A. Shutemov } 1250af53d3e9SHugh Dickins while (!list_empty(&info->swaplist)) { 1251af53d3e9SHugh Dickins /* Wait while shmem_unuse() is scanning this inode... */ 1252af53d3e9SHugh Dickins wait_var_event(&info->stop_eviction, 1253af53d3e9SHugh Dickins !atomic_read(&info->stop_eviction)); 1254cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1255af53d3e9SHugh Dickins /* ...but beware of the race if we peeked too early */ 1256af53d3e9SHugh Dickins if (!atomic_read(&info->stop_eviction)) 12571da177e4SLinus Torvalds list_del_init(&info->swaplist); 1258cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 12591da177e4SLinus Torvalds } 12603ed47db3SAl Viro } 1261b09e0fa4SEric Paris 12622daf18a7SHugh Dickins simple_xattrs_free(&info->xattrs, sbinfo->max_inodes ? &freed : NULL); 12632daf18a7SHugh Dickins shmem_free_inode(inode->i_sb, freed); 12640f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 1265dbd5768fSJan Kara clear_inode(inode); 1266e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 1267e09764cfSCarlos Maiolino dquot_free_inode(inode); 1268e09764cfSCarlos Maiolino dquot_drop(inode); 1269e09764cfSCarlos Maiolino #endif 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 1272b56a2d8aSVineeth Remanan Pillai static int shmem_find_swap_entries(struct address_space *mapping, 1273da08e9b7SMatthew Wilcox (Oracle) pgoff_t start, struct folio_batch *fbatch, 1274da08e9b7SMatthew Wilcox (Oracle) pgoff_t *indices, unsigned int type) 1275478922e2SMatthew Wilcox { 1276b56a2d8aSVineeth Remanan Pillai XA_STATE(xas, &mapping->i_pages, start); 1277da08e9b7SMatthew Wilcox (Oracle) struct folio *folio; 127887039546SHugh Dickins swp_entry_t entry; 1279478922e2SMatthew Wilcox 1280478922e2SMatthew Wilcox rcu_read_lock(); 1281da08e9b7SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { 1282da08e9b7SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) 12835b9c98f3SMike Kravetz continue; 1284b56a2d8aSVineeth Remanan Pillai 1285da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1286478922e2SMatthew Wilcox continue; 1287b56a2d8aSVineeth Remanan Pillai 1288da08e9b7SMatthew Wilcox (Oracle) entry = radix_to_swp_entry(folio); 12896cec2b95SMiaohe Lin /* 12906cec2b95SMiaohe Lin * swapin error entries can be found in the mapping. But they're 12916cec2b95SMiaohe Lin * deliberately ignored here as we've done everything we can do. 12926cec2b95SMiaohe Lin */ 129387039546SHugh Dickins if (swp_type(entry) != type) 1294b56a2d8aSVineeth Remanan Pillai continue; 1295b56a2d8aSVineeth Remanan Pillai 1296e384200eSHugh Dickins indices[folio_batch_count(fbatch)] = xas.xa_index; 1297da08e9b7SMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio)) 1298da08e9b7SMatthew Wilcox (Oracle) break; 1299b56a2d8aSVineeth Remanan Pillai 1300b56a2d8aSVineeth Remanan Pillai if (need_resched()) { 1301e21a2955SMatthew Wilcox xas_pause(&xas); 1302478922e2SMatthew Wilcox cond_resched_rcu(); 1303478922e2SMatthew Wilcox } 1304b56a2d8aSVineeth Remanan Pillai } 1305478922e2SMatthew Wilcox rcu_read_unlock(); 1306e21a2955SMatthew Wilcox 1307da08e9b7SMatthew Wilcox (Oracle) return xas.xa_index; 1308b56a2d8aSVineeth Remanan Pillai } 1309b56a2d8aSVineeth Remanan Pillai 1310b56a2d8aSVineeth Remanan Pillai /* 1311b56a2d8aSVineeth Remanan Pillai * Move the swapped pages for an inode to page cache. Returns the count 1312b56a2d8aSVineeth Remanan Pillai * of pages swapped in, or the error in case of failure. 1313b56a2d8aSVineeth Remanan Pillai */ 1314da08e9b7SMatthew Wilcox (Oracle) static int shmem_unuse_swap_entries(struct inode *inode, 1315da08e9b7SMatthew Wilcox (Oracle) struct folio_batch *fbatch, pgoff_t *indices) 1316b56a2d8aSVineeth Remanan Pillai { 1317b56a2d8aSVineeth Remanan Pillai int i = 0; 1318b56a2d8aSVineeth Remanan Pillai int ret = 0; 1319b56a2d8aSVineeth Remanan Pillai int error = 0; 1320b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1321b56a2d8aSVineeth Remanan Pillai 1322da08e9b7SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(fbatch); i++) { 1323da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = fbatch->folios[i]; 1324b56a2d8aSVineeth Remanan Pillai 1325da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1326b56a2d8aSVineeth Remanan Pillai continue; 1327da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, indices[i], 1328da08e9b7SMatthew Wilcox (Oracle) &folio, SGP_CACHE, 1329b56a2d8aSVineeth Remanan Pillai mapping_gfp_mask(mapping), 1330b56a2d8aSVineeth Remanan Pillai NULL, NULL); 1331b56a2d8aSVineeth Remanan Pillai if (error == 0) { 1332da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1333da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1334b56a2d8aSVineeth Remanan Pillai ret++; 1335b56a2d8aSVineeth Remanan Pillai } 1336b56a2d8aSVineeth Remanan Pillai if (error == -ENOMEM) 1337b56a2d8aSVineeth Remanan Pillai break; 1338b56a2d8aSVineeth Remanan Pillai error = 0; 1339b56a2d8aSVineeth Remanan Pillai } 1340b56a2d8aSVineeth Remanan Pillai return error ? error : ret; 1341478922e2SMatthew Wilcox } 1342478922e2SMatthew Wilcox 134346f65ec1SHugh Dickins /* 134446f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 134546f65ec1SHugh Dickins */ 134610a9c496SChristoph Hellwig static int shmem_unuse_inode(struct inode *inode, unsigned int type) 13471da177e4SLinus Torvalds { 1348b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1349b56a2d8aSVineeth Remanan Pillai pgoff_t start = 0; 1350da08e9b7SMatthew Wilcox (Oracle) struct folio_batch fbatch; 1351b56a2d8aSVineeth Remanan Pillai pgoff_t indices[PAGEVEC_SIZE]; 1352b56a2d8aSVineeth Remanan Pillai int ret = 0; 13531da177e4SLinus Torvalds 1354b56a2d8aSVineeth Remanan Pillai do { 1355da08e9b7SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1356da08e9b7SMatthew Wilcox (Oracle) shmem_find_swap_entries(mapping, start, &fbatch, indices, type); 1357da08e9b7SMatthew Wilcox (Oracle) if (folio_batch_count(&fbatch) == 0) { 1358b56a2d8aSVineeth Remanan Pillai ret = 0; 1359778dd893SHugh Dickins break; 1360b56a2d8aSVineeth Remanan Pillai } 1361b56a2d8aSVineeth Remanan Pillai 1362da08e9b7SMatthew Wilcox (Oracle) ret = shmem_unuse_swap_entries(inode, &fbatch, indices); 1363b56a2d8aSVineeth Remanan Pillai if (ret < 0) 1364b56a2d8aSVineeth Remanan Pillai break; 1365b56a2d8aSVineeth Remanan Pillai 1366da08e9b7SMatthew Wilcox (Oracle) start = indices[folio_batch_count(&fbatch) - 1]; 1367b56a2d8aSVineeth Remanan Pillai } while (true); 1368b56a2d8aSVineeth Remanan Pillai 1369b56a2d8aSVineeth Remanan Pillai return ret; 1370b56a2d8aSVineeth Remanan Pillai } 1371b56a2d8aSVineeth Remanan Pillai 1372b56a2d8aSVineeth Remanan Pillai /* 1373b56a2d8aSVineeth Remanan Pillai * Read all the shared memory data that resides in the swap 1374b56a2d8aSVineeth Remanan Pillai * device 'type' back into memory, so the swap device can be 1375b56a2d8aSVineeth Remanan Pillai * unused. 1376b56a2d8aSVineeth Remanan Pillai */ 137710a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 1378b56a2d8aSVineeth Remanan Pillai { 1379b56a2d8aSVineeth Remanan Pillai struct shmem_inode_info *info, *next; 1380b56a2d8aSVineeth Remanan Pillai int error = 0; 1381b56a2d8aSVineeth Remanan Pillai 1382b56a2d8aSVineeth Remanan Pillai if (list_empty(&shmem_swaplist)) 1383b56a2d8aSVineeth Remanan Pillai return 0; 1384b56a2d8aSVineeth Remanan Pillai 1385b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1386b56a2d8aSVineeth Remanan Pillai list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { 1387b56a2d8aSVineeth Remanan Pillai if (!info->swapped) { 1388b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1389b56a2d8aSVineeth Remanan Pillai continue; 1390b56a2d8aSVineeth Remanan Pillai } 1391af53d3e9SHugh Dickins /* 1392af53d3e9SHugh Dickins * Drop the swaplist mutex while searching the inode for swap; 1393af53d3e9SHugh Dickins * but before doing so, make sure shmem_evict_inode() will not 1394af53d3e9SHugh Dickins * remove placeholder inode from swaplist, nor let it be freed 1395af53d3e9SHugh Dickins * (igrab() would protect from unlink, but not from unmount). 1396af53d3e9SHugh Dickins */ 1397af53d3e9SHugh Dickins atomic_inc(&info->stop_eviction); 1398b56a2d8aSVineeth Remanan Pillai mutex_unlock(&shmem_swaplist_mutex); 1399b56a2d8aSVineeth Remanan Pillai 140010a9c496SChristoph Hellwig error = shmem_unuse_inode(&info->vfs_inode, type); 1401b56a2d8aSVineeth Remanan Pillai cond_resched(); 1402b56a2d8aSVineeth Remanan Pillai 1403b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1404b56a2d8aSVineeth Remanan Pillai next = list_next_entry(info, swaplist); 1405b56a2d8aSVineeth Remanan Pillai if (!info->swapped) 1406b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1407af53d3e9SHugh Dickins if (atomic_dec_and_test(&info->stop_eviction)) 1408af53d3e9SHugh Dickins wake_up_var(&info->stop_eviction); 1409b56a2d8aSVineeth Remanan Pillai if (error) 1410b56a2d8aSVineeth Remanan Pillai break; 14111da177e4SLinus Torvalds } 1412cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1413778dd893SHugh Dickins 1414778dd893SHugh Dickins return error; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds /* 14181da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 14191da177e4SLinus Torvalds */ 14201da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 14211da177e4SLinus Torvalds { 1422e2e3fdc7SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 14238ccee8c1SLuis Chamberlain struct address_space *mapping = folio->mapping; 14248ccee8c1SLuis Chamberlain struct inode *inode = mapping->host; 14258ccee8c1SLuis Chamberlain struct shmem_inode_info *info = SHMEM_I(inode); 14262c6efe9cSLuis Chamberlain struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 14276922c0c7SHugh Dickins swp_entry_t swap; 14286922c0c7SHugh Dickins pgoff_t index; 14291da177e4SLinus Torvalds 14301e6decf3SHugh Dickins /* 1431cf7992bfSLuis Chamberlain * Our capabilities prevent regular writeback or sync from ever calling 1432cf7992bfSLuis Chamberlain * shmem_writepage; but a stacking filesystem might use ->writepage of 1433cf7992bfSLuis Chamberlain * its underlying filesystem, in which case tmpfs should write out to 1434cf7992bfSLuis Chamberlain * swap only in response to memory pressure, and not for the writeback 1435cf7992bfSLuis Chamberlain * threads or sync. 1436cf7992bfSLuis Chamberlain */ 1437cf7992bfSLuis Chamberlain if (WARN_ON_ONCE(!wbc->for_reclaim)) 1438cf7992bfSLuis Chamberlain goto redirty; 1439cf7992bfSLuis Chamberlain 14402c6efe9cSLuis Chamberlain if (WARN_ON_ONCE((info->flags & VM_LOCKED) || sbinfo->noswap)) 14419a976f0cSLuis Chamberlain goto redirty; 14429a976f0cSLuis Chamberlain 14439a976f0cSLuis Chamberlain if (!total_swap_pages) 14449a976f0cSLuis Chamberlain goto redirty; 14459a976f0cSLuis Chamberlain 1446cf7992bfSLuis Chamberlain /* 14471e6decf3SHugh Dickins * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or 14481e6decf3SHugh Dickins * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages, 14491e6decf3SHugh Dickins * and its shmem_writeback() needs them to be split when swapping. 14501e6decf3SHugh Dickins */ 1451f530ed0eSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 14521e6decf3SHugh Dickins /* Ensure the subpages are still dirty */ 1453f530ed0eSMatthew Wilcox (Oracle) folio_test_set_dirty(folio); 14541e6decf3SHugh Dickins if (split_huge_page(page) < 0) 14551e6decf3SHugh Dickins goto redirty; 1456f530ed0eSMatthew Wilcox (Oracle) folio = page_folio(page); 1457f530ed0eSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 14581e6decf3SHugh Dickins } 14591e6decf3SHugh Dickins 1460f530ed0eSMatthew Wilcox (Oracle) index = folio->index; 14611635f6a7SHugh Dickins 14621635f6a7SHugh Dickins /* 14631635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 14641635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 1465f530ed0eSMatthew Wilcox (Oracle) * fallocated folio arriving here is now to initialize it and write it. 14661aac1400SHugh Dickins * 1467f530ed0eSMatthew Wilcox (Oracle) * That's okay for a folio already fallocated earlier, but if we have 14681aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 1469f530ed0eSMatthew Wilcox (Oracle) * of this folio in case we have to undo it, and (b) it may not be a 14701aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 1471f530ed0eSMatthew Wilcox (Oracle) * reactivate the folio, and let shmem_fallocate() quit when too many. 14721635f6a7SHugh Dickins */ 1473f530ed0eSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 14741aac1400SHugh Dickins if (inode->i_private) { 14751aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 14761aac1400SHugh Dickins spin_lock(&inode->i_lock); 14771aac1400SHugh Dickins shmem_falloc = inode->i_private; 14781aac1400SHugh Dickins if (shmem_falloc && 14798e205f77SHugh Dickins !shmem_falloc->waitq && 14801aac1400SHugh Dickins index >= shmem_falloc->start && 14811aac1400SHugh Dickins index < shmem_falloc->next) 14821aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 14831aac1400SHugh Dickins else 14841aac1400SHugh Dickins shmem_falloc = NULL; 14851aac1400SHugh Dickins spin_unlock(&inode->i_lock); 14861aac1400SHugh Dickins if (shmem_falloc) 14871aac1400SHugh Dickins goto redirty; 14881aac1400SHugh Dickins } 1489f530ed0eSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio)); 1490f530ed0eSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 1491f530ed0eSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 14921635f6a7SHugh Dickins } 14931635f6a7SHugh Dickins 1494e2e3fdc7SMatthew Wilcox (Oracle) swap = folio_alloc_swap(folio); 149548f170fbSHugh Dickins if (!swap.val) 149648f170fbSHugh Dickins goto redirty; 1497d9fe526aSHugh Dickins 1498b1dea800SHugh Dickins /* 1499b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 1500f530ed0eSMatthew Wilcox (Oracle) * if it's not already there. Do it now before the folio is 15016922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1502b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 15036922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 15046922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1505b1dea800SHugh Dickins */ 1506b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 150705bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 1508b56a2d8aSVineeth Remanan Pillai list_add(&info->swaplist, &shmem_swaplist); 1509b1dea800SHugh Dickins 1510a4c366f0SMatthew Wilcox (Oracle) if (add_to_swap_cache(folio, swap, 15113852f676SJoonsoo Kim __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, 15123852f676SJoonsoo Kim NULL) == 0) { 15133c1b7528SHugh Dickins shmem_recalc_inode(inode, 0, 1); 1514aaa46865SHugh Dickins swap_shmem_alloc(swap); 15154cd400fdSMatthew Wilcox (Oracle) shmem_delete_from_page_cache(folio, swp_to_radix_entry(swap)); 15166922c0c7SHugh Dickins 15176922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1518f530ed0eSMatthew Wilcox (Oracle) BUG_ON(folio_mapped(folio)); 1519f530ed0eSMatthew Wilcox (Oracle) swap_writepage(&folio->page, wbc); 15201da177e4SLinus Torvalds return 0; 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds 15236922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 15244081f744SMatthew Wilcox (Oracle) put_swap_folio(folio, swap); 15251da177e4SLinus Torvalds redirty: 1526f530ed0eSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1527d9fe526aSHugh Dickins if (wbc->for_reclaim) 1528f530ed0eSMatthew Wilcox (Oracle) return AOP_WRITEPAGE_ACTIVATE; /* Return with folio locked */ 1529f530ed0eSMatthew Wilcox (Oracle) folio_unlock(folio); 1530d9fe526aSHugh Dickins return 0; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 153375edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 153471fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1535680d794bSakpm@linux-foundation.org { 1536680d794bSakpm@linux-foundation.org char buffer[64]; 1537680d794bSakpm@linux-foundation.org 153871fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1539095f1fc4SLee Schermerhorn return; /* show nothing */ 1540095f1fc4SLee Schermerhorn 1541a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1542095f1fc4SLee Schermerhorn 1543095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1544680d794bSakpm@linux-foundation.org } 154571fe804bSLee Schermerhorn 154671fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 154771fe804bSLee Schermerhorn { 154871fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 154971fe804bSLee Schermerhorn if (sbinfo->mpol) { 1550bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 155171fe804bSLee Schermerhorn mpol = sbinfo->mpol; 155271fe804bSLee Schermerhorn mpol_get(mpol); 1553bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 155471fe804bSLee Schermerhorn } 155571fe804bSLee Schermerhorn return mpol; 155671fe804bSLee Schermerhorn } 155775edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 155875edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 155975edd345SHugh Dickins { 156075edd345SHugh Dickins } 156175edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 156275edd345SHugh Dickins { 156375edd345SHugh Dickins return NULL; 156475edd345SHugh Dickins } 156575edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 156675edd345SHugh Dickins #ifndef CONFIG_NUMA 156775edd345SHugh Dickins #define vm_policy vm_private_data 156875edd345SHugh Dickins #endif 1569680d794bSakpm@linux-foundation.org 1570800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1571800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1572800d8c63SKirill A. Shutemov { 1573800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 15742c4541e2SKirill A. Shutemov vma_init(vma, NULL); 1575800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1576800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1577800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1578800d8c63SKirill A. Shutemov } 1579800d8c63SKirill A. Shutemov 1580800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1581800d8c63SKirill A. Shutemov { 1582800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1583800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1584800d8c63SKirill A. Shutemov } 1585800d8c63SKirill A. Shutemov 15865739a81cSMatthew Wilcox (Oracle) static struct folio *shmem_swapin(swp_entry_t swap, gfp_t gfp, 158741ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 15881da177e4SLinus Torvalds { 15891da177e4SLinus Torvalds struct vm_area_struct pvma; 159018a2f371SMel Gorman struct page *page; 15918c63ca5bSWill Deacon struct vm_fault vmf = { 15928c63ca5bSWill Deacon .vma = &pvma, 15938c63ca5bSWill Deacon }; 15941da177e4SLinus Torvalds 1595800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1596e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1597800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 159818a2f371SMel Gorman 15995739a81cSMatthew Wilcox (Oracle) if (!page) 16005739a81cSMatthew Wilcox (Oracle) return NULL; 16015739a81cSMatthew Wilcox (Oracle) return page_folio(page); 1602800d8c63SKirill A. Shutemov } 160318a2f371SMel Gorman 160478cc8cdcSRik van Riel /* 160578cc8cdcSRik van Riel * Make sure huge_gfp is always more limited than limit_gfp. 160678cc8cdcSRik van Riel * Some of the flags set permissions, while others set limitations. 160778cc8cdcSRik van Riel */ 160878cc8cdcSRik van Riel static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp) 160978cc8cdcSRik van Riel { 161078cc8cdcSRik van Riel gfp_t allowflags = __GFP_IO | __GFP_FS | __GFP_RECLAIM; 161178cc8cdcSRik van Riel gfp_t denyflags = __GFP_NOWARN | __GFP_NORETRY; 1612187df5ddSRik van Riel gfp_t zoneflags = limit_gfp & GFP_ZONEMASK; 1613187df5ddSRik van Riel gfp_t result = huge_gfp & ~(allowflags | GFP_ZONEMASK); 1614187df5ddSRik van Riel 1615187df5ddSRik van Riel /* Allow allocations only from the originally specified zones. */ 1616187df5ddSRik van Riel result |= zoneflags; 161778cc8cdcSRik van Riel 161878cc8cdcSRik van Riel /* 161978cc8cdcSRik van Riel * Minimize the result gfp by taking the union with the deny flags, 162078cc8cdcSRik van Riel * and the intersection of the allow flags. 162178cc8cdcSRik van Riel */ 162278cc8cdcSRik van Riel result |= (limit_gfp & denyflags); 162378cc8cdcSRik van Riel result |= (huge_gfp & limit_gfp) & allowflags; 162478cc8cdcSRik van Riel 162578cc8cdcSRik van Riel return result; 162678cc8cdcSRik van Riel } 162778cc8cdcSRik van Riel 162872827e5cSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_hugefolio(gfp_t gfp, 1629800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1630800d8c63SKirill A. Shutemov { 1631800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 16327b8d046fSMatthew Wilcox struct address_space *mapping = info->vfs_inode.i_mapping; 16337b8d046fSMatthew Wilcox pgoff_t hindex; 1634dfe98499SMatthew Wilcox (Oracle) struct folio *folio; 1635800d8c63SKirill A. Shutemov 16364620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 16377b8d046fSMatthew Wilcox if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, 16387b8d046fSMatthew Wilcox XA_PRESENT)) 1639800d8c63SKirill A. Shutemov return NULL; 1640800d8c63SKirill A. Shutemov 1641800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1642dfe98499SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, &pvma, 0, true); 1643800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1644dfe98499SMatthew Wilcox (Oracle) if (!folio) 1645dcdf11eeSDavid Rientjes count_vm_event(THP_FILE_FALLBACK); 164672827e5cSMatthew Wilcox (Oracle) return folio; 164718a2f371SMel Gorman } 164818a2f371SMel Gorman 16490c023ef5SMatthew Wilcox (Oracle) static struct folio *shmem_alloc_folio(gfp_t gfp, 165018a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 165118a2f371SMel Gorman { 165218a2f371SMel Gorman struct vm_area_struct pvma; 16530c023ef5SMatthew Wilcox (Oracle) struct folio *folio; 165418a2f371SMel Gorman 1655800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 16560c023ef5SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, 0, &pvma, 0, false); 1657800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 165818a2f371SMel Gorman 16590c023ef5SMatthew Wilcox (Oracle) return folio; 166018a2f371SMel Gorman } 166118a2f371SMel Gorman 1662b1d0ec3aSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode, 1663800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1664800d8c63SKirill A. Shutemov { 16650f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 166672827e5cSMatthew Wilcox (Oracle) struct folio *folio; 1667800d8c63SKirill A. Shutemov int nr; 1668c7e263abSLukas Czerner int err; 1669800d8c63SKirill A. Shutemov 1670396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 1671800d8c63SKirill A. Shutemov huge = false; 1672800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1673800d8c63SKirill A. Shutemov 1674c7e263abSLukas Czerner err = shmem_inode_acct_block(inode, nr); 1675c7e263abSLukas Czerner if (err) 1676800d8c63SKirill A. Shutemov goto failed; 1677800d8c63SKirill A. Shutemov 1678800d8c63SKirill A. Shutemov if (huge) 167972827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_hugefolio(gfp, info, index); 1680800d8c63SKirill A. Shutemov else 168172827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, index); 168272827e5cSMatthew Wilcox (Oracle) if (folio) { 168372827e5cSMatthew Wilcox (Oracle) __folio_set_locked(folio); 168472827e5cSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 1685b1d0ec3aSMatthew Wilcox (Oracle) return folio; 168675edd345SHugh Dickins } 168718a2f371SMel Gorman 1688800d8c63SKirill A. Shutemov err = -ENOMEM; 16890f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1690800d8c63SKirill A. Shutemov failed: 1691800d8c63SKirill A. Shutemov return ERR_PTR(err); 16921da177e4SLinus Torvalds } 169371fe804bSLee Schermerhorn 16941da177e4SLinus Torvalds /* 1695bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1696fc26babbSMatthew Wilcox (Oracle) * usual swapin of shmem_get_folio_gfp(), or by the less common swapoff of 1697bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1698bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1699bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1700bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1701bde05d1cSHugh Dickins * 1702bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1703bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1704bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1705bde05d1cSHugh Dickins */ 1706069d849cSMatthew Wilcox (Oracle) static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp) 1707bde05d1cSHugh Dickins { 1708069d849cSMatthew Wilcox (Oracle) return folio_zonenum(folio) > gfp_zone(gfp); 1709bde05d1cSHugh Dickins } 1710bde05d1cSHugh Dickins 17110d698e25SMatthew Wilcox (Oracle) static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, 1712bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1713bde05d1cSHugh Dickins { 1714d21bba2bSMatthew Wilcox (Oracle) struct folio *old, *new; 1715bde05d1cSHugh Dickins struct address_space *swap_mapping; 1716c1cb20d4SYu Zhao swp_entry_t entry; 1717bde05d1cSHugh Dickins pgoff_t swap_index; 1718bde05d1cSHugh Dickins int error; 1719bde05d1cSHugh Dickins 17200d698e25SMatthew Wilcox (Oracle) old = *foliop; 1721907ea17eSMatthew Wilcox (Oracle) entry = folio_swap_entry(old); 1722c1cb20d4SYu Zhao swap_index = swp_offset(entry); 1723907ea17eSMatthew Wilcox (Oracle) swap_mapping = swap_address_space(entry); 1724bde05d1cSHugh Dickins 1725bde05d1cSHugh Dickins /* 1726bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1727bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1728bde05d1cSHugh Dickins */ 1729bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1730907ea17eSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(old), old); 1731907ea17eSMatthew Wilcox (Oracle) new = shmem_alloc_folio(gfp, info, index); 1732907ea17eSMatthew Wilcox (Oracle) if (!new) 1733bde05d1cSHugh Dickins return -ENOMEM; 1734bde05d1cSHugh Dickins 1735907ea17eSMatthew Wilcox (Oracle) folio_get(new); 1736907ea17eSMatthew Wilcox (Oracle) folio_copy(new, old); 1737907ea17eSMatthew Wilcox (Oracle) flush_dcache_folio(new); 1738bde05d1cSHugh Dickins 1739907ea17eSMatthew Wilcox (Oracle) __folio_set_locked(new); 1740907ea17eSMatthew Wilcox (Oracle) __folio_set_swapbacked(new); 1741907ea17eSMatthew Wilcox (Oracle) folio_mark_uptodate(new); 1742907ea17eSMatthew Wilcox (Oracle) folio_set_swap_entry(new, entry); 1743907ea17eSMatthew Wilcox (Oracle) folio_set_swapcache(new); 1744bde05d1cSHugh Dickins 1745bde05d1cSHugh Dickins /* 1746bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1747bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1748bde05d1cSHugh Dickins */ 1749b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 1750907ea17eSMatthew Wilcox (Oracle) error = shmem_replace_entry(swap_mapping, swap_index, old, new); 17510142ef6cSHugh Dickins if (!error) { 1752d21bba2bSMatthew Wilcox (Oracle) mem_cgroup_migrate(old, new); 1753907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_FILE_PAGES, 1); 1754907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_SHMEM, 1); 1755907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_FILE_PAGES, -1); 1756907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_SHMEM, -1); 17570142ef6cSHugh Dickins } 1758b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1759bde05d1cSHugh Dickins 17600142ef6cSHugh Dickins if (unlikely(error)) { 17610142ef6cSHugh Dickins /* 17620142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 17630142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 17640142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 17650142ef6cSHugh Dickins */ 1766907ea17eSMatthew Wilcox (Oracle) old = new; 17670142ef6cSHugh Dickins } else { 1768907ea17eSMatthew Wilcox (Oracle) folio_add_lru(new); 17690d698e25SMatthew Wilcox (Oracle) *foliop = new; 17700142ef6cSHugh Dickins } 1771bde05d1cSHugh Dickins 1772907ea17eSMatthew Wilcox (Oracle) folio_clear_swapcache(old); 1773907ea17eSMatthew Wilcox (Oracle) old->private = NULL; 1774bde05d1cSHugh Dickins 1775907ea17eSMatthew Wilcox (Oracle) folio_unlock(old); 1776907ea17eSMatthew Wilcox (Oracle) folio_put_refs(old, 2); 17770142ef6cSHugh Dickins return error; 1778bde05d1cSHugh Dickins } 1779bde05d1cSHugh Dickins 17806cec2b95SMiaohe Lin static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, 17816cec2b95SMiaohe Lin struct folio *folio, swp_entry_t swap) 17826cec2b95SMiaohe Lin { 17836cec2b95SMiaohe Lin struct address_space *mapping = inode->i_mapping; 17846cec2b95SMiaohe Lin swp_entry_t swapin_error; 17856cec2b95SMiaohe Lin void *old; 17866cec2b95SMiaohe Lin 178715520a3fSPeter Xu swapin_error = make_swapin_error_entry(); 17886cec2b95SMiaohe Lin old = xa_cmpxchg_irq(&mapping->i_pages, index, 17896cec2b95SMiaohe Lin swp_to_radix_entry(swap), 17906cec2b95SMiaohe Lin swp_to_radix_entry(swapin_error), 0); 17916cec2b95SMiaohe Lin if (old != swp_to_radix_entry(swap)) 17926cec2b95SMiaohe Lin return; 17936cec2b95SMiaohe Lin 17946cec2b95SMiaohe Lin folio_wait_writeback(folio); 179575fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 17966cec2b95SMiaohe Lin /* 17973c1b7528SHugh Dickins * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks 17983c1b7528SHugh Dickins * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks) 17993c1b7528SHugh Dickins * in shmem_evict_inode(). 18006cec2b95SMiaohe Lin */ 18013c1b7528SHugh Dickins shmem_recalc_inode(inode, -1, -1); 18026cec2b95SMiaohe Lin swap_free(swap); 18036cec2b95SMiaohe Lin } 18046cec2b95SMiaohe Lin 1805bde05d1cSHugh Dickins /* 1806833de10fSMiaohe Lin * Swap in the folio pointed to by *foliop. 1807833de10fSMiaohe Lin * Caller has to make sure that *foliop contains a valid swapped folio. 1808833de10fSMiaohe Lin * Returns 0 and the folio in foliop if success. On failure, returns the 1809833de10fSMiaohe Lin * error code and NULL in *foliop. 18101da177e4SLinus Torvalds */ 1811da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 1812da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 1813c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 18142b740303SSouptick Joarder vm_fault_t *fault_type) 18151da177e4SLinus Torvalds { 18161da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 181723f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 181804f94e3fSDan Schatzberg struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; 1819cbc2bd98SKairui Song struct swap_info_struct *si; 1820da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = NULL; 18211da177e4SLinus Torvalds swp_entry_t swap; 18221da177e4SLinus Torvalds int error; 18231da177e4SLinus Torvalds 1824da08e9b7SMatthew Wilcox (Oracle) VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); 1825da08e9b7SMatthew Wilcox (Oracle) swap = radix_to_swp_entry(*foliop); 1826da08e9b7SMatthew Wilcox (Oracle) *foliop = NULL; 182754af6042SHugh Dickins 18286cec2b95SMiaohe Lin if (is_swapin_error_entry(swap)) 18296cec2b95SMiaohe Lin return -EIO; 18306cec2b95SMiaohe Lin 1831cbc2bd98SKairui Song si = get_swap_device(swap); 1832cbc2bd98SKairui Song if (!si) { 1833cbc2bd98SKairui Song if (!shmem_confirm_swap(mapping, index, swap)) 1834cbc2bd98SKairui Song return -EEXIST; 1835cbc2bd98SKairui Song else 1836cbc2bd98SKairui Song return -EINVAL; 1837cbc2bd98SKairui Song } 1838cbc2bd98SKairui Song 18391da177e4SLinus Torvalds /* Look it up and read it in.. */ 18405739a81cSMatthew Wilcox (Oracle) folio = swap_cache_get_folio(swap, NULL, 0); 18415739a81cSMatthew Wilcox (Oracle) if (!folio) { 18429e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 18439e18eb29SAndres Lagar-Cavilla if (fault_type) { 184468da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 18459e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 18462262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 18479e18eb29SAndres Lagar-Cavilla } 18489e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 18495739a81cSMatthew Wilcox (Oracle) folio = shmem_swapin(swap, gfp, info, index); 18505739a81cSMatthew Wilcox (Oracle) if (!folio) { 18511da177e4SLinus Torvalds error = -ENOMEM; 185254af6042SHugh Dickins goto failed; 1853285b2c4fSHugh Dickins } 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds 1856833de10fSMiaohe Lin /* We have to do this with folio locked to prevent races */ 1857da08e9b7SMatthew Wilcox (Oracle) folio_lock(folio); 1858da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio) || 1859da08e9b7SMatthew Wilcox (Oracle) folio_swap_entry(folio).val != swap.val || 1860d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1861c5bf121eSVineeth Remanan Pillai error = -EEXIST; 1862d1899228SHugh Dickins goto unlock; 1863bde05d1cSHugh Dickins } 1864da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 18651da177e4SLinus Torvalds error = -EIO; 186654af6042SHugh Dickins goto failed; 186754af6042SHugh Dickins } 1868da08e9b7SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 186954af6042SHugh Dickins 18708a84802eSSteven Price /* 18718a84802eSSteven Price * Some architectures may have to restore extra metadata to the 1872da08e9b7SMatthew Wilcox (Oracle) * folio after reading from swap. 18738a84802eSSteven Price */ 1874da08e9b7SMatthew Wilcox (Oracle) arch_swap_restore(swap, folio); 18758a84802eSSteven Price 1876069d849cSMatthew Wilcox (Oracle) if (shmem_should_replace_folio(folio, gfp)) { 18770d698e25SMatthew Wilcox (Oracle) error = shmem_replace_folio(&folio, gfp, info, index); 1878bde05d1cSHugh Dickins if (error) 187954af6042SHugh Dickins goto failed; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 1882b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, index, 18833fea5a49SJohannes Weiner swp_to_radix_entry(swap), gfp, 18843fea5a49SJohannes Weiner charge_mm); 188554af6042SHugh Dickins if (error) 188654af6042SHugh Dickins goto failed; 188754af6042SHugh Dickins 18883c1b7528SHugh Dickins shmem_recalc_inode(inode, 0, -1); 188927ab7006SHugh Dickins 189066d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1891da08e9b7SMatthew Wilcox (Oracle) folio_mark_accessed(folio); 189266d2f4d2SHugh Dickins 189375fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 1894da08e9b7SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 189527ab7006SHugh Dickins swap_free(swap); 1896cbc2bd98SKairui Song put_swap_device(si); 189727ab7006SHugh Dickins 1898da08e9b7SMatthew Wilcox (Oracle) *foliop = folio; 1899c5bf121eSVineeth Remanan Pillai return 0; 1900c5bf121eSVineeth Remanan Pillai failed: 1901c5bf121eSVineeth Remanan Pillai if (!shmem_confirm_swap(mapping, index, swap)) 1902c5bf121eSVineeth Remanan Pillai error = -EEXIST; 19036cec2b95SMiaohe Lin if (error == -EIO) 19046cec2b95SMiaohe Lin shmem_set_folio_swapin_error(inode, index, folio, swap); 1905c5bf121eSVineeth Remanan Pillai unlock: 1906da08e9b7SMatthew Wilcox (Oracle) if (folio) { 1907da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1908da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1909c5bf121eSVineeth Remanan Pillai } 1910cbc2bd98SKairui Song put_swap_device(si); 1911c5bf121eSVineeth Remanan Pillai 1912c5bf121eSVineeth Remanan Pillai return error; 1913c5bf121eSVineeth Remanan Pillai } 1914c5bf121eSVineeth Remanan Pillai 1915c5bf121eSVineeth Remanan Pillai /* 1916fc26babbSMatthew Wilcox (Oracle) * shmem_get_folio_gfp - find page in cache, or get from swap, or allocate 1917c5bf121eSVineeth Remanan Pillai * 1918c5bf121eSVineeth Remanan Pillai * If we allocate a new one we do not mark it dirty. That's up to the 1919c5bf121eSVineeth Remanan Pillai * vm. If we swap it in we mark it dirty since we also free the swap 1920c5bf121eSVineeth Remanan Pillai * entry since a page cannot live in both the swap and page cache. 1921c5bf121eSVineeth Remanan Pillai * 1922c949b097SAxel Rasmussen * vma, vmf, and fault_type are only supplied by shmem_fault: 1923c5bf121eSVineeth Remanan Pillai * otherwise they are NULL. 1924c5bf121eSVineeth Remanan Pillai */ 1925fc26babbSMatthew Wilcox (Oracle) static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, 1926fc26babbSMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, gfp_t gfp, 1927c5bf121eSVineeth Remanan Pillai struct vm_area_struct *vma, struct vm_fault *vmf, 1928c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type) 1929c5bf121eSVineeth Remanan Pillai { 1930c5bf121eSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1931c5bf121eSVineeth Remanan Pillai struct shmem_inode_info *info = SHMEM_I(inode); 1932c5bf121eSVineeth Remanan Pillai struct shmem_sb_info *sbinfo; 1933c5bf121eSVineeth Remanan Pillai struct mm_struct *charge_mm; 1934b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 19356fe7d712SLukas Bulwahn pgoff_t hindex; 1936164cc4feSRik van Riel gfp_t huge_gfp; 1937c5bf121eSVineeth Remanan Pillai int error; 1938c5bf121eSVineeth Remanan Pillai int once = 0; 1939c5bf121eSVineeth Remanan Pillai int alloced = 0; 1940c5bf121eSVineeth Remanan Pillai 1941c5bf121eSVineeth Remanan Pillai if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 1942c5bf121eSVineeth Remanan Pillai return -EFBIG; 1943c5bf121eSVineeth Remanan Pillai repeat: 1944c5bf121eSVineeth Remanan Pillai if (sgp <= SGP_CACHE && 1945c5bf121eSVineeth Remanan Pillai ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1946c5bf121eSVineeth Remanan Pillai return -EINVAL; 1947c5bf121eSVineeth Remanan Pillai } 1948c5bf121eSVineeth Remanan Pillai 1949c5bf121eSVineeth Remanan Pillai sbinfo = SHMEM_SB(inode->i_sb); 195004f94e3fSDan Schatzberg charge_mm = vma ? vma->vm_mm : NULL; 1951c5bf121eSVineeth Remanan Pillai 1952aaeb94ebSChristoph Hellwig folio = filemap_get_entry(mapping, index); 1953b1d0ec3aSMatthew Wilcox (Oracle) if (folio && vma && userfaultfd_minor(vma)) { 1954aaeb94ebSChristoph Hellwig if (!xa_is_value(folio)) 1955b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1956c949b097SAxel Rasmussen *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); 1957c949b097SAxel Rasmussen return 0; 1958c949b097SAxel Rasmussen } 1959c949b097SAxel Rasmussen 1960b1d0ec3aSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 1961da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, index, &folio, 1962c5bf121eSVineeth Remanan Pillai sgp, gfp, vma, fault_type); 1963c5bf121eSVineeth Remanan Pillai if (error == -EEXIST) 1964c5bf121eSVineeth Remanan Pillai goto repeat; 1965c5bf121eSVineeth Remanan Pillai 1966fc26babbSMatthew Wilcox (Oracle) *foliop = folio; 1967c5bf121eSVineeth Remanan Pillai return error; 1968c5bf121eSVineeth Remanan Pillai } 1969c5bf121eSVineeth Remanan Pillai 1970b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 1971aaeb94ebSChristoph Hellwig folio_lock(folio); 1972aaeb94ebSChristoph Hellwig 1973aaeb94ebSChristoph Hellwig /* Has the folio been truncated or swapped out? */ 1974aaeb94ebSChristoph Hellwig if (unlikely(folio->mapping != mapping)) { 1975aaeb94ebSChristoph Hellwig folio_unlock(folio); 1976aaeb94ebSChristoph Hellwig folio_put(folio); 1977aaeb94ebSChristoph Hellwig goto repeat; 1978aaeb94ebSChristoph Hellwig } 1979acdd9f8eSHugh Dickins if (sgp == SGP_WRITE) 1980b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 1981b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) 1982acdd9f8eSHugh Dickins goto out; 1983fc26babbSMatthew Wilcox (Oracle) /* fallocated folio */ 1984c5bf121eSVineeth Remanan Pillai if (sgp != SGP_READ) 1985c5bf121eSVineeth Remanan Pillai goto clear; 1986b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 1987b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1988c5bf121eSVineeth Remanan Pillai } 1989c5bf121eSVineeth Remanan Pillai 1990c5bf121eSVineeth Remanan Pillai /* 1991fc26babbSMatthew Wilcox (Oracle) * SGP_READ: succeed on hole, with NULL folio, letting caller zero. 1992fc26babbSMatthew Wilcox (Oracle) * SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail. 1993acdd9f8eSHugh Dickins */ 1994fc26babbSMatthew Wilcox (Oracle) *foliop = NULL; 1995acdd9f8eSHugh Dickins if (sgp == SGP_READ) 1996acdd9f8eSHugh Dickins return 0; 1997acdd9f8eSHugh Dickins if (sgp == SGP_NOALLOC) 1998acdd9f8eSHugh Dickins return -ENOENT; 1999acdd9f8eSHugh Dickins 2000acdd9f8eSHugh Dickins /* 2001acdd9f8eSHugh Dickins * Fast cache lookup and swap lookup did not find it: allocate. 2002c5bf121eSVineeth Remanan Pillai */ 2003c5bf121eSVineeth Remanan Pillai 2004cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 2005cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 2006cfda0526SMike Rapoport return 0; 2007cfda0526SMike Rapoport } 2008cfda0526SMike Rapoport 20092cf13384SDavid Stevens if (!shmem_is_huge(inode, index, false, 20102cf13384SDavid Stevens vma ? vma->vm_mm : NULL, vma ? vma->vm_flags : 0)) 2011800d8c63SKirill A. Shutemov goto alloc_nohuge; 201227d80fa2SKees Cook 2013164cc4feSRik van Riel huge_gfp = vma_thp_gfp_mask(vma); 201478cc8cdcSRik van Riel huge_gfp = limit_gfp_mask(huge_gfp, gfp); 2015b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(huge_gfp, inode, index, true); 2016b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 2017c5bf121eSVineeth Remanan Pillai alloc_nohuge: 2018b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(gfp, inode, index, false); 201954af6042SHugh Dickins } 2020b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 2021779750d2SKirill A. Shutemov int retry = 5; 2022c5bf121eSVineeth Remanan Pillai 2023b1d0ec3aSMatthew Wilcox (Oracle) error = PTR_ERR(folio); 2024b1d0ec3aSMatthew Wilcox (Oracle) folio = NULL; 2025779750d2SKirill A. Shutemov if (error != -ENOSPC) 2026c5bf121eSVineeth Remanan Pillai goto unlock; 2027779750d2SKirill A. Shutemov /* 2028fc26babbSMatthew Wilcox (Oracle) * Try to reclaim some space by splitting a large folio 2029779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 2030779750d2SKirill A. Shutemov */ 2031779750d2SKirill A. Shutemov while (retry--) { 2032779750d2SKirill A. Shutemov int ret; 2033c5bf121eSVineeth Remanan Pillai 2034779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 2035779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 2036779750d2SKirill A. Shutemov break; 2037779750d2SKirill A. Shutemov if (ret) 2038779750d2SKirill A. Shutemov goto alloc_nohuge; 2039779750d2SKirill A. Shutemov } 2040c5bf121eSVineeth Remanan Pillai goto unlock; 2041800d8c63SKirill A. Shutemov } 2042800d8c63SKirill A. Shutemov 2043b1d0ec3aSMatthew Wilcox (Oracle) hindex = round_down(index, folio_nr_pages(folio)); 2044800d8c63SKirill A. Shutemov 204566d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 2046b1d0ec3aSMatthew Wilcox (Oracle) __folio_set_referenced(folio); 204766d2f4d2SHugh Dickins 2048b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, hindex, 20493fea5a49SJohannes Weiner NULL, gfp & GFP_RECLAIM_MASK, 20503fea5a49SJohannes Weiner charge_mm); 20513fea5a49SJohannes Weiner if (error) 2052800d8c63SKirill A. Shutemov goto unacct; 205354af6042SHugh Dickins 20543c1b7528SHugh Dickins folio_add_lru(folio); 20553c1b7528SHugh Dickins shmem_recalc_inode(inode, folio_nr_pages(folio), 0); 20561635f6a7SHugh Dickins alloced = true; 205754af6042SHugh Dickins 2058b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio) && 2059779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 2060fc26babbSMatthew Wilcox (Oracle) folio_next_index(folio) - 1) { 2061779750d2SKirill A. Shutemov /* 2062fc26babbSMatthew Wilcox (Oracle) * Part of the large folio is beyond i_size: subject 2063779750d2SKirill A. Shutemov * to shrink under memory pressure. 2064779750d2SKirill A. Shutemov */ 2065779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 2066d041353dSCong Wang /* 2067d041353dSCong Wang * _careful to defend against unlocked access to 2068d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 2069d041353dSCong Wang */ 2070d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 2071779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 2072779750d2SKirill A. Shutemov &sbinfo->shrinklist); 2073779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 2074779750d2SKirill A. Shutemov } 2075779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 2076779750d2SKirill A. Shutemov } 2077779750d2SKirill A. Shutemov 2078ec9516fbSHugh Dickins /* 2079fc26babbSMatthew Wilcox (Oracle) * Let SGP_FALLOC use the SGP_WRITE optimization on a new folio. 20801635f6a7SHugh Dickins */ 20811635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 20821635f6a7SHugh Dickins sgp = SGP_WRITE; 20831635f6a7SHugh Dickins clear: 20841635f6a7SHugh Dickins /* 2085fc26babbSMatthew Wilcox (Oracle) * Let SGP_WRITE caller clear ends if write does not fill folio; 2086fc26babbSMatthew Wilcox (Oracle) * but SGP_FALLOC on a folio fallocated earlier must initialize 20871635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 2088ec9516fbSHugh Dickins */ 2089b1d0ec3aSMatthew Wilcox (Oracle) if (sgp != SGP_WRITE && !folio_test_uptodate(folio)) { 2090b1d0ec3aSMatthew Wilcox (Oracle) long i, n = folio_nr_pages(folio); 2091800d8c63SKirill A. Shutemov 2092b1d0ec3aSMatthew Wilcox (Oracle) for (i = 0; i < n; i++) 2093b1d0ec3aSMatthew Wilcox (Oracle) clear_highpage(folio_page(folio, i)); 2094b1d0ec3aSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 2095b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 2096ec9516fbSHugh Dickins } 2097bde05d1cSHugh Dickins 209854af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 209975edd345SHugh Dickins if (sgp <= SGP_CACHE && 210009cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 2101267a4c76SHugh Dickins if (alloced) { 2102b1d0ec3aSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 2103b1d0ec3aSMatthew Wilcox (Oracle) filemap_remove_folio(folio); 21043c1b7528SHugh Dickins shmem_recalc_inode(inode, 0, 0); 2105267a4c76SHugh Dickins } 210654af6042SHugh Dickins error = -EINVAL; 2107267a4c76SHugh Dickins goto unlock; 2108ff36b801SShaohua Li } 210963ec1973SMatthew Wilcox (Oracle) out: 2110fc26babbSMatthew Wilcox (Oracle) *foliop = folio; 211154af6042SHugh Dickins return 0; 2112d00806b1SNick Piggin 2113d0217ac0SNick Piggin /* 211454af6042SHugh Dickins * Error recovery. 21151da177e4SLinus Torvalds */ 211654af6042SHugh Dickins unacct: 2117b1d0ec3aSMatthew Wilcox (Oracle) shmem_inode_unacct_blocks(inode, folio_nr_pages(folio)); 2118800d8c63SKirill A. Shutemov 2119b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 2120b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2121b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 2122800d8c63SKirill A. Shutemov goto alloc_nohuge; 2123800d8c63SKirill A. Shutemov } 2124d1899228SHugh Dickins unlock: 2125b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 2126b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2127b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 212854af6042SHugh Dickins } 212954af6042SHugh Dickins if (error == -ENOSPC && !once++) { 21303c1b7528SHugh Dickins shmem_recalc_inode(inode, 0, 0); 21311da177e4SLinus Torvalds goto repeat; 2132d8dc74f2SAdrian Bunk } 21337f4446eeSMatthew Wilcox if (error == -EEXIST) 213454af6042SHugh Dickins goto repeat; 213554af6042SHugh Dickins return error; 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds 21384e1fc793SMatthew Wilcox (Oracle) int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **foliop, 21394e1fc793SMatthew Wilcox (Oracle) enum sgp_type sgp) 21404e1fc793SMatthew Wilcox (Oracle) { 21414e1fc793SMatthew Wilcox (Oracle) return shmem_get_folio_gfp(inode, index, foliop, sgp, 21424e1fc793SMatthew Wilcox (Oracle) mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 21434e1fc793SMatthew Wilcox (Oracle) } 21444e1fc793SMatthew Wilcox (Oracle) 214510d20bd2SLinus Torvalds /* 214610d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 214710d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 214810d20bd2SLinus Torvalds * target. 214910d20bd2SLinus Torvalds */ 2150ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 215110d20bd2SLinus Torvalds { 215210d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 21532055da97SIngo Molnar list_del_init(&wait->entry); 215410d20bd2SLinus Torvalds return ret; 215510d20bd2SLinus Torvalds } 215610d20bd2SLinus Torvalds 215720acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 21581da177e4SLinus Torvalds { 215911bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 2160496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 21619e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 216268a54100SMatthew Wilcox (Oracle) struct folio *folio = NULL; 216320acce67SSouptick Joarder int err; 216420acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 21651da177e4SLinus Torvalds 2166f00cdc6dSHugh Dickins /* 2167f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 2168f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 21699608703eSJan Kara * locks writers out with its hold on i_rwsem. So refrain from 21708e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 21718e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 21728e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 21738e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 21748e205f77SHugh Dickins * 21758e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 21768e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 21778e205f77SHugh Dickins * we just need to make racing faults a rare case. 21788e205f77SHugh Dickins * 21798e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 21809608703eSJan Kara * standard mutex or completion: but we cannot take i_rwsem in fault, 21818e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 2182f00cdc6dSHugh Dickins */ 2183f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 2184f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 2185f00cdc6dSHugh Dickins 2186f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2187f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 21888e205f77SHugh Dickins if (shmem_falloc && 21898e205f77SHugh Dickins shmem_falloc->waitq && 21908e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 21918e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 21928897c1b1SKirill A. Shutemov struct file *fpin; 21938e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 219410d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 21958e205f77SHugh Dickins 21968e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 21978897c1b1SKirill A. Shutemov fpin = maybe_unlock_mmap_for_io(vmf, NULL); 21988897c1b1SKirill A. Shutemov if (fpin) 21998e205f77SHugh Dickins ret = VM_FAULT_RETRY; 22008e205f77SHugh Dickins 22018e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 22028e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 22038e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 22048e205f77SHugh Dickins spin_unlock(&inode->i_lock); 22058e205f77SHugh Dickins schedule(); 22068e205f77SHugh Dickins 22078e205f77SHugh Dickins /* 22088e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 22098e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 22108e205f77SHugh Dickins * is usually invalid by the time we reach here, but 22118e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 22128e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 22138e205f77SHugh Dickins */ 22148e205f77SHugh Dickins spin_lock(&inode->i_lock); 22158e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 22168e205f77SHugh Dickins spin_unlock(&inode->i_lock); 22178897c1b1SKirill A. Shutemov 22188897c1b1SKirill A. Shutemov if (fpin) 22198897c1b1SKirill A. Shutemov fput(fpin); 22208e205f77SHugh Dickins return ret; 2221f00cdc6dSHugh Dickins } 22228e205f77SHugh Dickins spin_unlock(&inode->i_lock); 2223f00cdc6dSHugh Dickins } 2224f00cdc6dSHugh Dickins 222568a54100SMatthew Wilcox (Oracle) err = shmem_get_folio_gfp(inode, vmf->pgoff, &folio, SGP_CACHE, 2226cfda0526SMike Rapoport gfp, vma, vmf, &ret); 222720acce67SSouptick Joarder if (err) 222820acce67SSouptick Joarder return vmf_error(err); 222968a54100SMatthew Wilcox (Oracle) if (folio) 223068a54100SMatthew Wilcox (Oracle) vmf->page = folio_file_page(folio, vmf->pgoff); 223168da9f05SHugh Dickins return ret; 22321da177e4SLinus Torvalds } 22331da177e4SLinus Torvalds 2234c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 2235c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 2236c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 2237c01d5b30SHugh Dickins { 2238c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2239c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2240c01d5b30SHugh Dickins unsigned long addr; 2241c01d5b30SHugh Dickins unsigned long offset; 2242c01d5b30SHugh Dickins unsigned long inflated_len; 2243c01d5b30SHugh Dickins unsigned long inflated_addr; 2244c01d5b30SHugh Dickins unsigned long inflated_offset; 2245c01d5b30SHugh Dickins 2246c01d5b30SHugh Dickins if (len > TASK_SIZE) 2247c01d5b30SHugh Dickins return -ENOMEM; 2248c01d5b30SHugh Dickins 2249c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2250c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2251c01d5b30SHugh Dickins 2252396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 2253c01d5b30SHugh Dickins return addr; 2254c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2255c01d5b30SHugh Dickins return addr; 2256c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2257c01d5b30SHugh Dickins return addr; 2258c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2259c01d5b30SHugh Dickins return addr; 2260c01d5b30SHugh Dickins 2261c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2262c01d5b30SHugh Dickins return addr; 2263c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2264c01d5b30SHugh Dickins return addr; 2265c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2266c01d5b30SHugh Dickins return addr; 2267c01d5b30SHugh Dickins /* 2268c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2269c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 227099158997SKirill A. Shutemov * But if caller specified an address hint and we allocated area there 227199158997SKirill A. Shutemov * successfully, respect that as before. 2272c01d5b30SHugh Dickins */ 227399158997SKirill A. Shutemov if (uaddr == addr) 2274c01d5b30SHugh Dickins return addr; 2275c01d5b30SHugh Dickins 2276c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2277c01d5b30SHugh Dickins struct super_block *sb; 2278c01d5b30SHugh Dickins 2279c01d5b30SHugh Dickins if (file) { 2280c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2281c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2282c01d5b30SHugh Dickins } else { 2283c01d5b30SHugh Dickins /* 2284c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2285c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2286c01d5b30SHugh Dickins */ 2287c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2288c01d5b30SHugh Dickins return addr; 2289c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2290c01d5b30SHugh Dickins } 22913089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2292c01d5b30SHugh Dickins return addr; 2293c01d5b30SHugh Dickins } 2294c01d5b30SHugh Dickins 2295c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2296c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2297c01d5b30SHugh Dickins return addr; 2298c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2299c01d5b30SHugh Dickins return addr; 2300c01d5b30SHugh Dickins 2301c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2302c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2303c01d5b30SHugh Dickins return addr; 2304c01d5b30SHugh Dickins if (inflated_len < len) 2305c01d5b30SHugh Dickins return addr; 2306c01d5b30SHugh Dickins 230799158997SKirill A. Shutemov inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); 2308c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2309c01d5b30SHugh Dickins return addr; 2310c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2311c01d5b30SHugh Dickins return addr; 2312c01d5b30SHugh Dickins 2313c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2314c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2315c01d5b30SHugh Dickins if (inflated_offset > offset) 2316c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2317c01d5b30SHugh Dickins 2318c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2319c01d5b30SHugh Dickins return addr; 2320c01d5b30SHugh Dickins return inflated_addr; 2321c01d5b30SHugh Dickins } 2322c01d5b30SHugh Dickins 23231da177e4SLinus Torvalds #ifdef CONFIG_NUMA 232441ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 23251da177e4SLinus Torvalds { 2326496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 232741ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 2330d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2331d8dc74f2SAdrian Bunk unsigned long addr) 23321da177e4SLinus Torvalds { 2333496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 233441ffe5d5SHugh Dickins pgoff_t index; 23351da177e4SLinus Torvalds 233641ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 233741ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds #endif 23401da177e4SLinus Torvalds 2341d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 23421da177e4SLinus Torvalds { 2343496ad9aaSAl Viro struct inode *inode = file_inode(file); 23441da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 23451da177e4SLinus Torvalds int retval = -ENOMEM; 23461da177e4SLinus Torvalds 2347ea0dfeb4SHugh Dickins /* 2348ea0dfeb4SHugh Dickins * What serializes the accesses to info->flags? 2349ea0dfeb4SHugh Dickins * ipc_lock_object() when called from shmctl_do_lock(), 2350ea0dfeb4SHugh Dickins * no serialization needed when called from shm_destroy(). 2351ea0dfeb4SHugh Dickins */ 23521da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 2353d7c9e99aSAlexey Gladkov if (!user_shm_lock(inode->i_size, ucounts)) 23541da177e4SLinus Torvalds goto out_nomem; 23551da177e4SLinus Torvalds info->flags |= VM_LOCKED; 235689e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 23571da177e4SLinus Torvalds } 2358d7c9e99aSAlexey Gladkov if (!lock && (info->flags & VM_LOCKED) && ucounts) { 2359d7c9e99aSAlexey Gladkov user_shm_unlock(inode->i_size, ucounts); 23601da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 236189e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds retval = 0; 236489e004eaSLee Schermerhorn 23651da177e4SLinus Torvalds out_nomem: 23661da177e4SLinus Torvalds return retval; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 23699b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 23701da177e4SLinus Torvalds { 2371d09e8ca6SPasha Tatashin struct inode *inode = file_inode(file); 2372d09e8ca6SPasha Tatashin struct shmem_inode_info *info = SHMEM_I(inode); 237322247efdSPeter Xu int ret; 2374ab3948f5SJoel Fernandes (Google) 237522247efdSPeter Xu ret = seal_check_future_write(info->seals, vma); 237622247efdSPeter Xu if (ret) 237722247efdSPeter Xu return ret; 2378ab3948f5SJoel Fernandes (Google) 237951b0bff2SCatalin Marinas /* arm64 - allow memory tagging on RAM-based files */ 23801c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MTE_ALLOWED); 238151b0bff2SCatalin Marinas 23821da177e4SLinus Torvalds file_accessed(file); 2383d09e8ca6SPasha Tatashin /* This is anonymous shared memory if it is unlinked at the time of mmap */ 2384d09e8ca6SPasha Tatashin if (inode->i_nlink) 23851da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2386d09e8ca6SPasha Tatashin else 2387d09e8ca6SPasha Tatashin vma->vm_ops = &shmem_anon_vm_ops; 23881da177e4SLinus Torvalds return 0; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds 2391*e88e0d36SHugh Dickins static int shmem_file_open(struct inode *inode, struct file *file) 2392*e88e0d36SHugh Dickins { 2393*e88e0d36SHugh Dickins file->f_mode |= FMODE_CAN_ODIRECT; 2394*e88e0d36SHugh Dickins return generic_file_open(inode, file); 2395*e88e0d36SHugh Dickins } 2396*e88e0d36SHugh Dickins 2397cb241339SHugh Dickins #ifdef CONFIG_TMPFS_XATTR 2398cb241339SHugh Dickins static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 2399cb241339SHugh Dickins 2400cb241339SHugh Dickins /* 2401cb241339SHugh Dickins * chattr's fsflags are unrelated to extended attributes, 2402cb241339SHugh Dickins * but tmpfs has chosen to enable them under the same config option. 2403cb241339SHugh Dickins */ 2404cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2405e408e695STheodore Ts'o { 2406cb241339SHugh Dickins unsigned int i_flags = 0; 2407cb241339SHugh Dickins 2408cb241339SHugh Dickins if (fsflags & FS_NOATIME_FL) 2409cb241339SHugh Dickins i_flags |= S_NOATIME; 2410cb241339SHugh Dickins if (fsflags & FS_APPEND_FL) 2411cb241339SHugh Dickins i_flags |= S_APPEND; 2412cb241339SHugh Dickins if (fsflags & FS_IMMUTABLE_FL) 2413cb241339SHugh Dickins i_flags |= S_IMMUTABLE; 2414cb241339SHugh Dickins /* 2415cb241339SHugh Dickins * But FS_NODUMP_FL does not require any action in i_flags. 2416cb241339SHugh Dickins */ 2417cb241339SHugh Dickins inode_set_flags(inode, i_flags, S_NOATIME | S_APPEND | S_IMMUTABLE); 2418e408e695STheodore Ts'o } 2419cb241339SHugh Dickins #else 2420cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2421cb241339SHugh Dickins { 2422cb241339SHugh Dickins } 2423cb241339SHugh Dickins #define shmem_initxattrs NULL 2424cb241339SHugh Dickins #endif 2425e408e695STheodore Ts'o 2426a2e45955SChuck Lever static struct offset_ctx *shmem_get_offset_ctx(struct inode *inode) 2427a2e45955SChuck Lever { 2428a2e45955SChuck Lever return &SHMEM_I(inode)->dir_offsets; 2429a2e45955SChuck Lever } 2430a2e45955SChuck Lever 2431e09764cfSCarlos Maiolino static struct inode *__shmem_get_inode(struct mnt_idmap *idmap, 2432e09764cfSCarlos Maiolino struct super_block *sb, 2433e09764cfSCarlos Maiolino struct inode *dir, umode_t mode, 2434e09764cfSCarlos Maiolino dev_t dev, unsigned long flags) 24351da177e4SLinus Torvalds { 24361da177e4SLinus Torvalds struct inode *inode; 24371da177e4SLinus Torvalds struct shmem_inode_info *info; 24381da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2439e809d5f0SChris Down ino_t ino; 244071480663SCarlos Maiolino int err; 24411da177e4SLinus Torvalds 244271480663SCarlos Maiolino err = shmem_reserve_inode(sb, &ino); 244371480663SCarlos Maiolino if (err) 244471480663SCarlos Maiolino return ERR_PTR(err); 244571480663SCarlos Maiolino 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds inode = new_inode(sb); 244871480663SCarlos Maiolino 244971480663SCarlos Maiolino if (!inode) { 24502daf18a7SHugh Dickins shmem_free_inode(sb, 0); 245171480663SCarlos Maiolino return ERR_PTR(-ENOSPC); 245271480663SCarlos Maiolino } 245371480663SCarlos Maiolino 2454e809d5f0SChris Down inode->i_ino = ino; 24557a80e5b8SGiuseppe Scrivano inode_init_owner(idmap, inode, dir, mode); 24561da177e4SLinus Torvalds inode->i_blocks = 0; 2457078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2458a251c17aSJason A. Donenfeld inode->i_generation = get_random_u32(); 24591da177e4SLinus Torvalds info = SHMEM_I(inode); 24601da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 24611da177e4SLinus Torvalds spin_lock_init(&info->lock); 2462af53d3e9SHugh Dickins atomic_set(&info->stop_eviction, 0); 246340e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 24640b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2465f7cd16a5SXavier Roche info->i_crtime = inode->i_mtime; 2466e408e695STheodore Ts'o info->fsflags = (dir == NULL) ? 0 : 2467e408e695STheodore Ts'o SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; 2468cb241339SHugh Dickins if (info->fsflags) 2469cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 2470779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 24711da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 247271480663SCarlos Maiolino INIT_LIST_HEAD(&info->swaplist); 24732c6efe9cSLuis Chamberlain if (sbinfo->noswap) 24742c6efe9cSLuis Chamberlain mapping_set_unevictable(inode->i_mapping); 247538f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 247672c04902SAl Viro cache_no_acl(inode); 2477ff36da69SMatthew Wilcox (Oracle) mapping_set_large_folios(inode->i_mapping); 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds switch (mode & S_IFMT) { 24801da177e4SLinus Torvalds default: 248139f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 24821da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 24831da177e4SLinus Torvalds break; 24841da177e4SLinus Torvalds case S_IFREG: 248514fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 24861da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 24871da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 248871fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 248971fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 24901da177e4SLinus Torvalds break; 24911da177e4SLinus Torvalds case S_IFDIR: 2492d8c76e6fSDave Hansen inc_nlink(inode); 24931da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 24941da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 24951da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 2496a2e45955SChuck Lever inode->i_fop = &simple_offset_dir_operations; 2497a2e45955SChuck Lever simple_offset_init(shmem_get_offset_ctx(inode)); 24981da177e4SLinus Torvalds break; 24991da177e4SLinus Torvalds case S_IFLNK: 25001da177e4SLinus Torvalds /* 25011da177e4SLinus Torvalds * Must not load anything in the rbtree, 25021da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 25031da177e4SLinus Torvalds */ 250471fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 25051da177e4SLinus Torvalds break; 25061da177e4SLinus Torvalds } 2507b45d71fbSJoel Fernandes (Google) 2508b45d71fbSJoel Fernandes (Google) lockdep_annotate_inode_mutex_key(inode); 25091da177e4SLinus Torvalds return inode; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 2512e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 2513e09764cfSCarlos Maiolino static struct inode *shmem_get_inode(struct mnt_idmap *idmap, 2514e09764cfSCarlos Maiolino struct super_block *sb, struct inode *dir, 2515e09764cfSCarlos Maiolino umode_t mode, dev_t dev, unsigned long flags) 2516e09764cfSCarlos Maiolino { 2517e09764cfSCarlos Maiolino int err; 2518e09764cfSCarlos Maiolino struct inode *inode; 2519e09764cfSCarlos Maiolino 2520e09764cfSCarlos Maiolino inode = __shmem_get_inode(idmap, sb, dir, mode, dev, flags); 2521e09764cfSCarlos Maiolino if (IS_ERR(inode)) 2522e09764cfSCarlos Maiolino return inode; 2523e09764cfSCarlos Maiolino 2524e09764cfSCarlos Maiolino err = dquot_initialize(inode); 2525e09764cfSCarlos Maiolino if (err) 2526e09764cfSCarlos Maiolino goto errout; 2527e09764cfSCarlos Maiolino 2528e09764cfSCarlos Maiolino err = dquot_alloc_inode(inode); 2529e09764cfSCarlos Maiolino if (err) { 2530e09764cfSCarlos Maiolino dquot_drop(inode); 2531e09764cfSCarlos Maiolino goto errout; 2532e09764cfSCarlos Maiolino } 2533e09764cfSCarlos Maiolino return inode; 2534e09764cfSCarlos Maiolino 2535e09764cfSCarlos Maiolino errout: 2536e09764cfSCarlos Maiolino inode->i_flags |= S_NOQUOTA; 2537e09764cfSCarlos Maiolino iput(inode); 2538e09764cfSCarlos Maiolino return ERR_PTR(err); 2539e09764cfSCarlos Maiolino } 2540e09764cfSCarlos Maiolino #else 2541e09764cfSCarlos Maiolino static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap, 2542e09764cfSCarlos Maiolino struct super_block *sb, struct inode *dir, 2543e09764cfSCarlos Maiolino umode_t mode, dev_t dev, unsigned long flags) 2544e09764cfSCarlos Maiolino { 2545e09764cfSCarlos Maiolino return __shmem_get_inode(idmap, sb, dir, mode, dev, flags); 2546e09764cfSCarlos Maiolino } 2547e09764cfSCarlos Maiolino #endif /* CONFIG_TMPFS_QUOTA */ 2548e09764cfSCarlos Maiolino 25493460f6e5SAxel Rasmussen #ifdef CONFIG_USERFAULTFD 255061c50040SAxel Rasmussen int shmem_mfill_atomic_pte(pmd_t *dst_pmd, 25514c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 25524c27fe4cSMike Rapoport unsigned long dst_addr, 25534c27fe4cSMike Rapoport unsigned long src_addr, 2554d9712937SAxel Rasmussen uffd_flags_t flags, 2555d7be6d7eSZhangPeng struct folio **foliop) 25564c27fe4cSMike Rapoport { 25574c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 25584c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 25594c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 25604c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 25614c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 25624c27fe4cSMike Rapoport void *page_kaddr; 2563b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 25644c27fe4cSMike Rapoport int ret; 25653460f6e5SAxel Rasmussen pgoff_t max_off; 25664c27fe4cSMike Rapoport 2567c7e263abSLukas Czerner if (shmem_inode_acct_block(inode, 1)) { 25687ed9d238SAxel Rasmussen /* 25697ed9d238SAxel Rasmussen * We may have got a page, returned -ENOENT triggering a retry, 25707ed9d238SAxel Rasmussen * and now we find ourselves with -ENOMEM. Release the page, to 25717ed9d238SAxel Rasmussen * avoid a BUG_ON in our caller. 25727ed9d238SAxel Rasmussen */ 2573d7be6d7eSZhangPeng if (unlikely(*foliop)) { 2574d7be6d7eSZhangPeng folio_put(*foliop); 2575d7be6d7eSZhangPeng *foliop = NULL; 25767ed9d238SAxel Rasmussen } 25777d64ae3aSAxel Rasmussen return -ENOMEM; 25787ed9d238SAxel Rasmussen } 25794c27fe4cSMike Rapoport 2580d7be6d7eSZhangPeng if (!*foliop) { 25817d64ae3aSAxel Rasmussen ret = -ENOMEM; 25827a7256d5SMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, pgoff); 25837a7256d5SMatthew Wilcox (Oracle) if (!folio) 25840f079694SMike Rapoport goto out_unacct_blocks; 25854c27fe4cSMike Rapoport 2586d9712937SAxel Rasmussen if (uffd_flags_mode_is(flags, MFILL_ATOMIC_COPY)) { 25877a7256d5SMatthew Wilcox (Oracle) page_kaddr = kmap_local_folio(folio, 0); 25885dc21f0cSIra Weiny /* 25895dc21f0cSIra Weiny * The read mmap_lock is held here. Despite the 25905dc21f0cSIra Weiny * mmap_lock being read recursive a deadlock is still 25915dc21f0cSIra Weiny * possible if a writer has taken a lock. For example: 25925dc21f0cSIra Weiny * 25935dc21f0cSIra Weiny * process A thread 1 takes read lock on own mmap_lock 25945dc21f0cSIra Weiny * process A thread 2 calls mmap, blocks taking write lock 25955dc21f0cSIra Weiny * process B thread 1 takes page fault, read lock on own mmap lock 25965dc21f0cSIra Weiny * process B thread 2 calls mmap, blocks taking write lock 25975dc21f0cSIra Weiny * process A thread 1 blocks taking read lock on process B 25985dc21f0cSIra Weiny * process B thread 1 blocks taking read lock on process A 25995dc21f0cSIra Weiny * 26005dc21f0cSIra Weiny * Disable page faults to prevent potential deadlock 26015dc21f0cSIra Weiny * and retry the copy outside the mmap_lock. 26025dc21f0cSIra Weiny */ 26035dc21f0cSIra Weiny pagefault_disable(); 26048d103963SMike Rapoport ret = copy_from_user(page_kaddr, 26058d103963SMike Rapoport (const void __user *)src_addr, 26064c27fe4cSMike Rapoport PAGE_SIZE); 26075dc21f0cSIra Weiny pagefault_enable(); 26087a7256d5SMatthew Wilcox (Oracle) kunmap_local(page_kaddr); 26094c27fe4cSMike Rapoport 2610c1e8d7c6SMichel Lespinasse /* fallback to copy_from_user outside mmap_lock */ 26114c27fe4cSMike Rapoport if (unlikely(ret)) { 2612d7be6d7eSZhangPeng *foliop = folio; 26137d64ae3aSAxel Rasmussen ret = -ENOENT; 26144c27fe4cSMike Rapoport /* don't free the page */ 26157d64ae3aSAxel Rasmussen goto out_unacct_blocks; 26164c27fe4cSMike Rapoport } 261719b482c2SMuchun Song 26187a7256d5SMatthew Wilcox (Oracle) flush_dcache_folio(folio); 26193460f6e5SAxel Rasmussen } else { /* ZEROPAGE */ 26207a7256d5SMatthew Wilcox (Oracle) clear_user_highpage(&folio->page, dst_addr); 26218d103963SMike Rapoport } 26224c27fe4cSMike Rapoport } else { 2623d7be6d7eSZhangPeng folio = *foliop; 26247a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(folio), folio); 2625d7be6d7eSZhangPeng *foliop = NULL; 26264c27fe4cSMike Rapoport } 26274c27fe4cSMike Rapoport 26287a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_locked(folio)); 26297a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_swapbacked(folio)); 26307a7256d5SMatthew Wilcox (Oracle) __folio_set_locked(folio); 26317a7256d5SMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 26327a7256d5SMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 26339cc90c66SAndrea Arcangeli 2634e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2635e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 26363460f6e5SAxel Rasmussen if (unlikely(pgoff >= max_off)) 2637e2a50c1fSAndrea Arcangeli goto out_release; 2638e2a50c1fSAndrea Arcangeli 2639b7dd44a1SMatthew Wilcox (Oracle) ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, 264061c50040SAxel Rasmussen gfp & GFP_RECLAIM_MASK, dst_vma->vm_mm); 26414c27fe4cSMike Rapoport if (ret) 26424c27fe4cSMike Rapoport goto out_release; 26434c27fe4cSMike Rapoport 264461c50040SAxel Rasmussen ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, 2645d9712937SAxel Rasmussen &folio->page, true, flags); 26467d64ae3aSAxel Rasmussen if (ret) 26477d64ae3aSAxel Rasmussen goto out_delete_from_cache; 26484c27fe4cSMike Rapoport 26493c1b7528SHugh Dickins shmem_recalc_inode(inode, 1, 0); 26507a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 26517d64ae3aSAxel Rasmussen return 0; 26527d64ae3aSAxel Rasmussen out_delete_from_cache: 26537a7256d5SMatthew Wilcox (Oracle) filemap_remove_folio(folio); 26544c27fe4cSMike Rapoport out_release: 26557a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 26567a7256d5SMatthew Wilcox (Oracle) folio_put(folio); 26574c27fe4cSMike Rapoport out_unacct_blocks: 26580f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 26597d64ae3aSAxel Rasmussen return ret; 26604c27fe4cSMike Rapoport } 26613460f6e5SAxel Rasmussen #endif /* CONFIG_USERFAULTFD */ 26628d103963SMike Rapoport 26631da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 266492e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 266569f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds static int 2668800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 26699d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 2670800d15a5SNick Piggin struct page **pagep, void **fsdata) 26711da177e4SLinus Torvalds { 2672800d15a5SNick Piggin struct inode *inode = mapping->host; 267340e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 267409cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 2675eff1f906SMatthew Wilcox (Oracle) struct folio *folio; 2676a7605426SYang Shi int ret = 0; 267740e041a2SDavid Herrmann 26789608703eSJan Kara /* i_rwsem is held by caller */ 2679ab3948f5SJoel Fernandes (Google) if (unlikely(info->seals & (F_SEAL_GROW | 2680ab3948f5SJoel Fernandes (Google) F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { 2681ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) 268240e041a2SDavid Herrmann return -EPERM; 268340e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 268440e041a2SDavid Herrmann return -EPERM; 268540e041a2SDavid Herrmann } 268640e041a2SDavid Herrmann 2687eff1f906SMatthew Wilcox (Oracle) ret = shmem_get_folio(inode, index, &folio, SGP_WRITE); 2688a7605426SYang Shi 2689a7605426SYang Shi if (ret) 2690a7605426SYang Shi return ret; 2691a7605426SYang Shi 2692eff1f906SMatthew Wilcox (Oracle) *pagep = folio_file_page(folio, index); 2693a7605426SYang Shi if (PageHWPoison(*pagep)) { 2694eff1f906SMatthew Wilcox (Oracle) folio_unlock(folio); 2695eff1f906SMatthew Wilcox (Oracle) folio_put(folio); 2696a7605426SYang Shi *pagep = NULL; 2697a7605426SYang Shi return -EIO; 2698a7605426SYang Shi } 2699a7605426SYang Shi 2700a7605426SYang Shi return 0; 2701800d15a5SNick Piggin } 2702800d15a5SNick Piggin 2703800d15a5SNick Piggin static int 2704800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2705800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2706800d15a5SNick Piggin struct page *page, void *fsdata) 2707800d15a5SNick Piggin { 270869bbb87bSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 2709800d15a5SNick Piggin struct inode *inode = mapping->host; 2710800d15a5SNick Piggin 2711800d15a5SNick Piggin if (pos + copied > inode->i_size) 2712800d15a5SNick Piggin i_size_write(inode, pos + copied); 2713800d15a5SNick Piggin 271469bbb87bSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 271569bbb87bSMatthew Wilcox (Oracle) if (copied < folio_size(folio)) { 271669bbb87bSMatthew Wilcox (Oracle) size_t from = offset_in_folio(folio, pos); 271769bbb87bSMatthew Wilcox (Oracle) folio_zero_segments(folio, 0, from, 271869bbb87bSMatthew Wilcox (Oracle) from + copied, folio_size(folio)); 2719800d8c63SKirill A. Shutemov } 272069bbb87bSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 2721800d8c63SKirill A. Shutemov } 272269bbb87bSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 272369bbb87bSMatthew Wilcox (Oracle) folio_unlock(folio); 272469bbb87bSMatthew Wilcox (Oracle) folio_put(folio); 2725d3602444SHugh Dickins 2726800d15a5SNick Piggin return copied; 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds 27292ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 27301da177e4SLinus Torvalds { 27316e58e79dSAl Viro struct file *file = iocb->ki_filp; 27326e58e79dSAl Viro struct inode *inode = file_inode(file); 27331da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 273441ffe5d5SHugh Dickins pgoff_t index; 273541ffe5d5SHugh Dickins unsigned long offset; 2736f7c1d074SGeert Uytterhoeven int error = 0; 2737cb66a7a1SAl Viro ssize_t retval = 0; 27386e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2739a0ee5ec5SHugh Dickins 274009cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 274109cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 27421da177e4SLinus Torvalds 27431da177e4SLinus Torvalds for (;;) { 27444601e2fcSMatthew Wilcox (Oracle) struct folio *folio = NULL; 27451da177e4SLinus Torvalds struct page *page = NULL; 274641ffe5d5SHugh Dickins pgoff_t end_index; 274741ffe5d5SHugh Dickins unsigned long nr, ret; 27481da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 27491da177e4SLinus Torvalds 275009cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 27511da177e4SLinus Torvalds if (index > end_index) 27521da177e4SLinus Torvalds break; 27531da177e4SLinus Torvalds if (index == end_index) { 275409cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 27551da177e4SLinus Torvalds if (nr <= offset) 27561da177e4SLinus Torvalds break; 27571da177e4SLinus Torvalds } 27581da177e4SLinus Torvalds 27594601e2fcSMatthew Wilcox (Oracle) error = shmem_get_folio(inode, index, &folio, SGP_READ); 27606e58e79dSAl Viro if (error) { 27616e58e79dSAl Viro if (error == -EINVAL) 27626e58e79dSAl Viro error = 0; 27631da177e4SLinus Torvalds break; 27641da177e4SLinus Torvalds } 27654601e2fcSMatthew Wilcox (Oracle) if (folio) { 27664601e2fcSMatthew Wilcox (Oracle) folio_unlock(folio); 2767a7605426SYang Shi 27684601e2fcSMatthew Wilcox (Oracle) page = folio_file_page(folio, index); 2769a7605426SYang Shi if (PageHWPoison(page)) { 27704601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 2771a7605426SYang Shi error = -EIO; 2772a7605426SYang Shi break; 2773a7605426SYang Shi } 277475edd345SHugh Dickins } 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds /* 27771da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 27789608703eSJan Kara * are called without i_rwsem protection against truncate 27791da177e4SLinus Torvalds */ 278009cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 27811da177e4SLinus Torvalds i_size = i_size_read(inode); 278209cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 27831da177e4SLinus Torvalds if (index == end_index) { 278409cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 27851da177e4SLinus Torvalds if (nr <= offset) { 27864601e2fcSMatthew Wilcox (Oracle) if (folio) 27874601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 27881da177e4SLinus Torvalds break; 27891da177e4SLinus Torvalds } 27901da177e4SLinus Torvalds } 27911da177e4SLinus Torvalds nr -= offset; 27921da177e4SLinus Torvalds 27934601e2fcSMatthew Wilcox (Oracle) if (folio) { 27941da177e4SLinus Torvalds /* 27951da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 27961da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 27971da177e4SLinus Torvalds * before reading the page on the kernel side. 27981da177e4SLinus Torvalds */ 27991da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 28001da177e4SLinus Torvalds flush_dcache_page(page); 28011da177e4SLinus Torvalds /* 28021da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 28031da177e4SLinus Torvalds */ 28041da177e4SLinus Torvalds if (!offset) 28054601e2fcSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 28061da177e4SLinus Torvalds /* 28071da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 28081da177e4SLinus Torvalds * now we can copy it to user space... 28091da177e4SLinus Torvalds */ 28102ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 28114601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 28121bdec44bSHugh Dickins 2813fcb14cb1SAl Viro } else if (user_backed_iter(to)) { 28141bdec44bSHugh Dickins /* 28151bdec44bSHugh Dickins * Copy to user tends to be so well optimized, but 28161bdec44bSHugh Dickins * clear_user() not so much, that it is noticeably 28171bdec44bSHugh Dickins * faster to copy the zero page instead of clearing. 28181bdec44bSHugh Dickins */ 28191bdec44bSHugh Dickins ret = copy_page_to_iter(ZERO_PAGE(0), offset, nr, to); 28201bdec44bSHugh Dickins } else { 28211bdec44bSHugh Dickins /* 28221bdec44bSHugh Dickins * But submitting the same page twice in a row to 28231bdec44bSHugh Dickins * splice() - or others? - can result in confusion: 28241bdec44bSHugh Dickins * so don't attempt that optimization on pipes etc. 28251bdec44bSHugh Dickins */ 28261bdec44bSHugh Dickins ret = iov_iter_zero(nr, to); 28271bdec44bSHugh Dickins } 28281bdec44bSHugh Dickins 28296e58e79dSAl Viro retval += ret; 28301da177e4SLinus Torvalds offset += ret; 283109cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 283209cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 28331da177e4SLinus Torvalds 28342ba5bbedSAl Viro if (!iov_iter_count(to)) 28351da177e4SLinus Torvalds break; 28366e58e79dSAl Viro if (ret < nr) { 28376e58e79dSAl Viro error = -EFAULT; 28386e58e79dSAl Viro break; 28396e58e79dSAl Viro } 28401da177e4SLinus Torvalds cond_resched(); 28411da177e4SLinus Torvalds } 28421da177e4SLinus Torvalds 284309cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 28446e58e79dSAl Viro file_accessed(file); 28456e58e79dSAl Viro return retval ? retval : error; 28461da177e4SLinus Torvalds } 28471da177e4SLinus Torvalds 2848*e88e0d36SHugh Dickins static ssize_t shmem_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 2849*e88e0d36SHugh Dickins { 2850*e88e0d36SHugh Dickins struct file *file = iocb->ki_filp; 2851*e88e0d36SHugh Dickins struct inode *inode = file->f_mapping->host; 2852*e88e0d36SHugh Dickins ssize_t ret; 2853*e88e0d36SHugh Dickins 2854*e88e0d36SHugh Dickins inode_lock(inode); 2855*e88e0d36SHugh Dickins ret = generic_write_checks(iocb, from); 2856*e88e0d36SHugh Dickins if (ret <= 0) 2857*e88e0d36SHugh Dickins goto unlock; 2858*e88e0d36SHugh Dickins ret = file_remove_privs(file); 2859*e88e0d36SHugh Dickins if (ret) 2860*e88e0d36SHugh Dickins goto unlock; 2861*e88e0d36SHugh Dickins ret = file_update_time(file); 2862*e88e0d36SHugh Dickins if (ret) 2863*e88e0d36SHugh Dickins goto unlock; 2864*e88e0d36SHugh Dickins ret = generic_perform_write(iocb, from); 2865*e88e0d36SHugh Dickins unlock: 2866*e88e0d36SHugh Dickins inode_unlock(inode); 2867*e88e0d36SHugh Dickins return ret; 2868*e88e0d36SHugh Dickins } 2869*e88e0d36SHugh Dickins 2870bd194b18SDavid Howells static bool zero_pipe_buf_get(struct pipe_inode_info *pipe, 2871bd194b18SDavid Howells struct pipe_buffer *buf) 2872bd194b18SDavid Howells { 2873bd194b18SDavid Howells return true; 2874bd194b18SDavid Howells } 2875bd194b18SDavid Howells 2876bd194b18SDavid Howells static void zero_pipe_buf_release(struct pipe_inode_info *pipe, 2877bd194b18SDavid Howells struct pipe_buffer *buf) 2878bd194b18SDavid Howells { 2879bd194b18SDavid Howells } 2880bd194b18SDavid Howells 2881bd194b18SDavid Howells static bool zero_pipe_buf_try_steal(struct pipe_inode_info *pipe, 2882bd194b18SDavid Howells struct pipe_buffer *buf) 2883bd194b18SDavid Howells { 2884bd194b18SDavid Howells return false; 2885bd194b18SDavid Howells } 2886bd194b18SDavid Howells 2887bd194b18SDavid Howells static const struct pipe_buf_operations zero_pipe_buf_ops = { 2888bd194b18SDavid Howells .release = zero_pipe_buf_release, 2889bd194b18SDavid Howells .try_steal = zero_pipe_buf_try_steal, 2890bd194b18SDavid Howells .get = zero_pipe_buf_get, 2891bd194b18SDavid Howells }; 2892bd194b18SDavid Howells 2893bd194b18SDavid Howells static size_t splice_zeropage_into_pipe(struct pipe_inode_info *pipe, 2894bd194b18SDavid Howells loff_t fpos, size_t size) 2895bd194b18SDavid Howells { 2896bd194b18SDavid Howells size_t offset = fpos & ~PAGE_MASK; 2897bd194b18SDavid Howells 2898bd194b18SDavid Howells size = min_t(size_t, size, PAGE_SIZE - offset); 2899bd194b18SDavid Howells 2900bd194b18SDavid Howells if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { 2901bd194b18SDavid Howells struct pipe_buffer *buf = pipe_head_buf(pipe); 2902bd194b18SDavid Howells 2903bd194b18SDavid Howells *buf = (struct pipe_buffer) { 2904bd194b18SDavid Howells .ops = &zero_pipe_buf_ops, 2905bd194b18SDavid Howells .page = ZERO_PAGE(0), 2906bd194b18SDavid Howells .offset = offset, 2907bd194b18SDavid Howells .len = size, 2908bd194b18SDavid Howells }; 2909bd194b18SDavid Howells pipe->head++; 2910bd194b18SDavid Howells } 2911bd194b18SDavid Howells 2912bd194b18SDavid Howells return size; 2913bd194b18SDavid Howells } 2914bd194b18SDavid Howells 2915bd194b18SDavid Howells static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 2916bd194b18SDavid Howells struct pipe_inode_info *pipe, 2917bd194b18SDavid Howells size_t len, unsigned int flags) 2918bd194b18SDavid Howells { 2919bd194b18SDavid Howells struct inode *inode = file_inode(in); 2920bd194b18SDavid Howells struct address_space *mapping = inode->i_mapping; 2921bd194b18SDavid Howells struct folio *folio = NULL; 2922bd194b18SDavid Howells size_t total_spliced = 0, used, npages, n, part; 2923bd194b18SDavid Howells loff_t isize; 2924bd194b18SDavid Howells int error = 0; 2925bd194b18SDavid Howells 2926bd194b18SDavid Howells /* Work out how much data we can actually add into the pipe */ 2927bd194b18SDavid Howells used = pipe_occupancy(pipe->head, pipe->tail); 2928bd194b18SDavid Howells npages = max_t(ssize_t, pipe->max_usage - used, 0); 2929bd194b18SDavid Howells len = min_t(size_t, len, npages * PAGE_SIZE); 2930bd194b18SDavid Howells 2931bd194b18SDavid Howells do { 2932bd194b18SDavid Howells if (*ppos >= i_size_read(inode)) 2933bd194b18SDavid Howells break; 2934bd194b18SDavid Howells 2935fa598952SHugh Dickins error = shmem_get_folio(inode, *ppos / PAGE_SIZE, &folio, 2936fa598952SHugh Dickins SGP_READ); 2937bd194b18SDavid Howells if (error) { 2938bd194b18SDavid Howells if (error == -EINVAL) 2939bd194b18SDavid Howells error = 0; 2940bd194b18SDavid Howells break; 2941bd194b18SDavid Howells } 2942bd194b18SDavid Howells if (folio) { 2943bd194b18SDavid Howells folio_unlock(folio); 2944bd194b18SDavid Howells 2945fa598952SHugh Dickins if (folio_test_hwpoison(folio) || 2946fa598952SHugh Dickins (folio_test_large(folio) && 2947fa598952SHugh Dickins folio_test_has_hwpoisoned(folio))) { 2948bd194b18SDavid Howells error = -EIO; 2949bd194b18SDavid Howells break; 2950bd194b18SDavid Howells } 2951bd194b18SDavid Howells } 2952bd194b18SDavid Howells 2953bd194b18SDavid Howells /* 2954bd194b18SDavid Howells * i_size must be checked after we know the pages are Uptodate. 2955bd194b18SDavid Howells * 2956bd194b18SDavid Howells * Checking i_size after the check allows us to calculate 2957bd194b18SDavid Howells * the correct value for "nr", which means the zero-filled 2958bd194b18SDavid Howells * part of the page is not copied back to userspace (unless 2959bd194b18SDavid Howells * another truncate extends the file - this is desired though). 2960bd194b18SDavid Howells */ 2961bd194b18SDavid Howells isize = i_size_read(inode); 2962bd194b18SDavid Howells if (unlikely(*ppos >= isize)) 2963bd194b18SDavid Howells break; 2964bd194b18SDavid Howells part = min_t(loff_t, isize - *ppos, len); 2965bd194b18SDavid Howells 2966bd194b18SDavid Howells if (folio) { 2967bd194b18SDavid Howells /* 2968bd194b18SDavid Howells * If users can be writing to this page using arbitrary 2969bd194b18SDavid Howells * virtual addresses, take care about potential aliasing 2970bd194b18SDavid Howells * before reading the page on the kernel side. 2971bd194b18SDavid Howells */ 2972bd194b18SDavid Howells if (mapping_writably_mapped(mapping)) 2973bd194b18SDavid Howells flush_dcache_folio(folio); 2974bd194b18SDavid Howells folio_mark_accessed(folio); 2975bd194b18SDavid Howells /* 2976bd194b18SDavid Howells * Ok, we have the page, and it's up-to-date, so we can 2977bd194b18SDavid Howells * now splice it into the pipe. 2978bd194b18SDavid Howells */ 2979bd194b18SDavid Howells n = splice_folio_into_pipe(pipe, folio, *ppos, part); 2980bd194b18SDavid Howells folio_put(folio); 2981bd194b18SDavid Howells folio = NULL; 2982bd194b18SDavid Howells } else { 2983fa598952SHugh Dickins n = splice_zeropage_into_pipe(pipe, *ppos, part); 2984bd194b18SDavid Howells } 2985bd194b18SDavid Howells 2986bd194b18SDavid Howells if (!n) 2987bd194b18SDavid Howells break; 2988bd194b18SDavid Howells len -= n; 2989bd194b18SDavid Howells total_spliced += n; 2990bd194b18SDavid Howells *ppos += n; 2991bd194b18SDavid Howells in->f_ra.prev_pos = *ppos; 2992bd194b18SDavid Howells if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 2993bd194b18SDavid Howells break; 2994bd194b18SDavid Howells 2995bd194b18SDavid Howells cond_resched(); 2996bd194b18SDavid Howells } while (len); 2997bd194b18SDavid Howells 2998bd194b18SDavid Howells if (folio) 2999bd194b18SDavid Howells folio_put(folio); 3000bd194b18SDavid Howells 3001bd194b18SDavid Howells file_accessed(in); 3002bd194b18SDavid Howells return total_spliced ? total_spliced : error; 3003bd194b18SDavid Howells } 3004bd194b18SDavid Howells 3005965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 3006220f2ac9SHugh Dickins { 3007220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 3008220f2ac9SHugh Dickins struct inode *inode = mapping->host; 3009220f2ac9SHugh Dickins 3010965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 3011965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 3012220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 301341139aa4SMatthew Wilcox (Oracle) if (offset < 0) 301441139aa4SMatthew Wilcox (Oracle) return -ENXIO; 301541139aa4SMatthew Wilcox (Oracle) 30165955102cSAl Viro inode_lock(inode); 30179608703eSJan Kara /* We're holding i_rwsem so we can access i_size directly */ 301841139aa4SMatthew Wilcox (Oracle) offset = mapping_seek_hole_data(mapping, offset, inode->i_size, whence); 3019387aae6fSHugh Dickins if (offset >= 0) 302046a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 30215955102cSAl Viro inode_unlock(inode); 3022220f2ac9SHugh Dickins return offset; 3023220f2ac9SHugh Dickins } 3024220f2ac9SHugh Dickins 302583e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 302683e4fa9cSHugh Dickins loff_t len) 302783e4fa9cSHugh Dickins { 3028496ad9aaSAl Viro struct inode *inode = file_inode(file); 3029e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 303040e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 30311aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 3032d144bf62SHugh Dickins pgoff_t start, index, end, undo_fallocend; 3033e2d12e22SHugh Dickins int error; 303483e4fa9cSHugh Dickins 303513ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 303613ace4d0SHugh Dickins return -EOPNOTSUPP; 303713ace4d0SHugh Dickins 30385955102cSAl Viro inode_lock(inode); 303983e4fa9cSHugh Dickins 304083e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 304183e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 304283e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 304383e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 30448e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 304583e4fa9cSHugh Dickins 30469608703eSJan Kara /* protected by i_rwsem */ 3047ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { 304840e041a2SDavid Herrmann error = -EPERM; 304940e041a2SDavid Herrmann goto out; 305040e041a2SDavid Herrmann } 305140e041a2SDavid Herrmann 30528e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 3053aa71ecd8SChen Jun shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; 3054f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 3055f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 3056f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 3057f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 3058f00cdc6dSHugh Dickins 305983e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 306083e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 306183e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 306283e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 306383e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 30648e205f77SHugh Dickins 30658e205f77SHugh Dickins spin_lock(&inode->i_lock); 30668e205f77SHugh Dickins inode->i_private = NULL; 30678e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 30682055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 30698e205f77SHugh Dickins spin_unlock(&inode->i_lock); 307083e4fa9cSHugh Dickins error = 0; 30718e205f77SHugh Dickins goto out; 307283e4fa9cSHugh Dickins } 307383e4fa9cSHugh Dickins 3074e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 3075e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 3076e2d12e22SHugh Dickins if (error) 3077e2d12e22SHugh Dickins goto out; 3078e2d12e22SHugh Dickins 307940e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 308040e041a2SDavid Herrmann error = -EPERM; 308140e041a2SDavid Herrmann goto out; 308240e041a2SDavid Herrmann } 308340e041a2SDavid Herrmann 308409cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 308509cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 3086e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 3087e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 3088e2d12e22SHugh Dickins error = -ENOSPC; 3089e2d12e22SHugh Dickins goto out; 3090e2d12e22SHugh Dickins } 3091e2d12e22SHugh Dickins 30928e205f77SHugh Dickins shmem_falloc.waitq = NULL; 30931aac1400SHugh Dickins shmem_falloc.start = start; 30941aac1400SHugh Dickins shmem_falloc.next = start; 30951aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 30961aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 30971aac1400SHugh Dickins spin_lock(&inode->i_lock); 30981aac1400SHugh Dickins inode->i_private = &shmem_falloc; 30991aac1400SHugh Dickins spin_unlock(&inode->i_lock); 31001aac1400SHugh Dickins 3101d144bf62SHugh Dickins /* 3102d144bf62SHugh Dickins * info->fallocend is only relevant when huge pages might be 3103d144bf62SHugh Dickins * involved: to prevent split_huge_page() freeing fallocated 3104d144bf62SHugh Dickins * pages when FALLOC_FL_KEEP_SIZE committed beyond i_size. 3105d144bf62SHugh Dickins */ 3106d144bf62SHugh Dickins undo_fallocend = info->fallocend; 3107d144bf62SHugh Dickins if (info->fallocend < end) 3108d144bf62SHugh Dickins info->fallocend = end; 3109d144bf62SHugh Dickins 3110050dcb5cSHugh Dickins for (index = start; index < end; ) { 3111b0802b22SMatthew Wilcox (Oracle) struct folio *folio; 3112e2d12e22SHugh Dickins 3113e2d12e22SHugh Dickins /* 3114e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 3115e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 3116e2d12e22SHugh Dickins */ 3117e2d12e22SHugh Dickins if (signal_pending(current)) 3118e2d12e22SHugh Dickins error = -EINTR; 31191aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 31201aac1400SHugh Dickins error = -ENOMEM; 3121e2d12e22SHugh Dickins else 3122b0802b22SMatthew Wilcox (Oracle) error = shmem_get_folio(inode, index, &folio, 3123b0802b22SMatthew Wilcox (Oracle) SGP_FALLOC); 3124e2d12e22SHugh Dickins if (error) { 3125d144bf62SHugh Dickins info->fallocend = undo_fallocend; 3126b0802b22SMatthew Wilcox (Oracle) /* Remove the !uptodate folios we added */ 31277f556567SHugh Dickins if (index > start) { 31281635f6a7SHugh Dickins shmem_undo_range(inode, 312909cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 3130b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 31317f556567SHugh Dickins } 31321aac1400SHugh Dickins goto undone; 3133e2d12e22SHugh Dickins } 3134e2d12e22SHugh Dickins 3135050dcb5cSHugh Dickins /* 3136050dcb5cSHugh Dickins * Here is a more important optimization than it appears: 3137b0802b22SMatthew Wilcox (Oracle) * a second SGP_FALLOC on the same large folio will clear it, 3138b0802b22SMatthew Wilcox (Oracle) * making it uptodate and un-undoable if we fail later. 3139050dcb5cSHugh Dickins */ 3140b0802b22SMatthew Wilcox (Oracle) index = folio_next_index(folio); 3141050dcb5cSHugh Dickins /* Beware 32-bit wraparound */ 3142050dcb5cSHugh Dickins if (!index) 3143050dcb5cSHugh Dickins index--; 3144050dcb5cSHugh Dickins 3145e2d12e22SHugh Dickins /* 31461aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 31471aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 31481aac1400SHugh Dickins */ 3149b0802b22SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) 3150050dcb5cSHugh Dickins shmem_falloc.nr_falloced += index - shmem_falloc.next; 3151050dcb5cSHugh Dickins shmem_falloc.next = index; 31521aac1400SHugh Dickins 31531aac1400SHugh Dickins /* 3154b0802b22SMatthew Wilcox (Oracle) * If !uptodate, leave it that way so that freeable folios 31551635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 3156b0802b22SMatthew Wilcox (Oracle) * But mark it dirty so that memory pressure will swap rather 3157b0802b22SMatthew Wilcox (Oracle) * than free the folios we are allocating (and SGP_CACHE folios 3158e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 3159e2d12e22SHugh Dickins */ 3160b0802b22SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 3161b0802b22SMatthew Wilcox (Oracle) folio_unlock(folio); 3162b0802b22SMatthew Wilcox (Oracle) folio_put(folio); 3163e2d12e22SHugh Dickins cond_resched(); 3164e2d12e22SHugh Dickins } 3165e2d12e22SHugh Dickins 3166e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 3167e2d12e22SHugh Dickins i_size_write(inode, offset + len); 31681aac1400SHugh Dickins undone: 31691aac1400SHugh Dickins spin_lock(&inode->i_lock); 31701aac1400SHugh Dickins inode->i_private = NULL; 31711aac1400SHugh Dickins spin_unlock(&inode->i_lock); 3172e2d12e22SHugh Dickins out: 317315f242bbSHugh Dickins if (!error) 317415f242bbSHugh Dickins file_modified(file); 31755955102cSAl Viro inode_unlock(inode); 317683e4fa9cSHugh Dickins return error; 317783e4fa9cSHugh Dickins } 317883e4fa9cSHugh Dickins 3179726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 31801da177e4SLinus Torvalds { 3181726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 318409cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 31851da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 31860edd73b3SHugh Dickins if (sbinfo->max_blocks) { 31871da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 318841ffe5d5SHugh Dickins buf->f_bavail = 318941ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 319041ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 31910edd73b3SHugh Dickins } 31920edd73b3SHugh Dickins if (sbinfo->max_inodes) { 31931da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 3194e07c469eSHugh Dickins buf->f_ffree = sbinfo->free_ispace / BOGO_INODE_SIZE; 31951da177e4SLinus Torvalds } 31961da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 319759cda49eSAmir Goldstein 319859cda49eSAmir Goldstein buf->f_fsid = uuid_to_fsid(dentry->d_sb->s_uuid.b); 319959cda49eSAmir Goldstein 32001da177e4SLinus Torvalds return 0; 32011da177e4SLinus Torvalds } 32021da177e4SLinus Torvalds 32031da177e4SLinus Torvalds /* 32041da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 32051da177e4SLinus Torvalds */ 32061da177e4SLinus Torvalds static int 32075ebb29beSChristian Brauner shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, 3208549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t dev) 32091da177e4SLinus Torvalds { 32100b0a0806SHugh Dickins struct inode *inode; 321171480663SCarlos Maiolino int error; 32121da177e4SLinus Torvalds 32137a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, dev, VM_NORESERVE); 321471480663SCarlos Maiolino 321571480663SCarlos Maiolino if (IS_ERR(inode)) 321671480663SCarlos Maiolino return PTR_ERR(inode); 321771480663SCarlos Maiolino 3218feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 3219feda821eSChristoph Hellwig if (error) 3220feda821eSChristoph Hellwig goto out_iput; 32212a7dba39SEric Paris error = security_inode_init_security(inode, dir, 32229d8f13baSMimi Zohar &dentry->d_name, 32236d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3224feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 3225feda821eSChristoph Hellwig goto out_iput; 322637ec43cdSMimi Zohar 3227a2e45955SChuck Lever error = simple_offset_add(shmem_get_offset_ctx(dir), dentry); 3228a2e45955SChuck Lever if (error) 3229a2e45955SChuck Lever goto out_iput; 3230a2e45955SChuck Lever 32311da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3232078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 323336f05cabSJeff Layton inode_inc_iversion(dir); 32341da177e4SLinus Torvalds d_instantiate(dentry, inode); 32351da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 32361da177e4SLinus Torvalds return error; 323771480663SCarlos Maiolino 3238feda821eSChristoph Hellwig out_iput: 3239feda821eSChristoph Hellwig iput(inode); 3240feda821eSChristoph Hellwig return error; 32411da177e4SLinus Torvalds } 32421da177e4SLinus Torvalds 324360545d0dSAl Viro static int 3244011e2b71SChristian Brauner shmem_tmpfile(struct mnt_idmap *idmap, struct inode *dir, 3245863f144fSMiklos Szeredi struct file *file, umode_t mode) 324660545d0dSAl Viro { 324760545d0dSAl Viro struct inode *inode; 324871480663SCarlos Maiolino int error; 324960545d0dSAl Viro 32507a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, 0, VM_NORESERVE); 325171480663SCarlos Maiolino 325271480663SCarlos Maiolino if (IS_ERR(inode)) { 325371480663SCarlos Maiolino error = PTR_ERR(inode); 325471480663SCarlos Maiolino goto err_out; 325571480663SCarlos Maiolino } 325671480663SCarlos Maiolino 325760545d0dSAl Viro error = security_inode_init_security(inode, dir, 325860545d0dSAl Viro NULL, 325960545d0dSAl Viro shmem_initxattrs, NULL); 3260feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 3261feda821eSChristoph Hellwig goto out_iput; 3262feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 3263feda821eSChristoph Hellwig if (error) 3264feda821eSChristoph Hellwig goto out_iput; 3265863f144fSMiklos Szeredi d_tmpfile(file, inode); 326671480663SCarlos Maiolino 326771480663SCarlos Maiolino err_out: 3268863f144fSMiklos Szeredi return finish_open_simple(file, error); 3269feda821eSChristoph Hellwig out_iput: 3270feda821eSChristoph Hellwig iput(inode); 3271feda821eSChristoph Hellwig return error; 327260545d0dSAl Viro } 327360545d0dSAl Viro 3274c54bd91eSChristian Brauner static int shmem_mkdir(struct mnt_idmap *idmap, struct inode *dir, 3275549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 32761da177e4SLinus Torvalds { 32771da177e4SLinus Torvalds int error; 32781da177e4SLinus Torvalds 32797a80e5b8SGiuseppe Scrivano error = shmem_mknod(idmap, dir, dentry, mode | S_IFDIR, 0); 32807a80e5b8SGiuseppe Scrivano if (error) 32811da177e4SLinus Torvalds return error; 3282d8c76e6fSDave Hansen inc_nlink(dir); 32831da177e4SLinus Torvalds return 0; 32841da177e4SLinus Torvalds } 32851da177e4SLinus Torvalds 32866c960e68SChristian Brauner static int shmem_create(struct mnt_idmap *idmap, struct inode *dir, 3287549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 32881da177e4SLinus Torvalds { 32897a80e5b8SGiuseppe Scrivano return shmem_mknod(idmap, dir, dentry, mode | S_IFREG, 0); 32901da177e4SLinus Torvalds } 32911da177e4SLinus Torvalds 32921da177e4SLinus Torvalds /* 32931da177e4SLinus Torvalds * Link a file.. 32941da177e4SLinus Torvalds */ 32951da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 32961da177e4SLinus Torvalds { 329775c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 329829b00e60SDarrick J. Wong int ret = 0; 32991da177e4SLinus Torvalds 33001da177e4SLinus Torvalds /* 33011da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 33021da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 33031da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 33041062af92SDarrick J. Wong * But if an O_TMPFILE file is linked into the tmpfs, the 33051062af92SDarrick J. Wong * first link must skip that, to get the accounting right. 33061da177e4SLinus Torvalds */ 33071062af92SDarrick J. Wong if (inode->i_nlink) { 3308e809d5f0SChris Down ret = shmem_reserve_inode(inode->i_sb, NULL); 33095b04c689SPavel Emelyanov if (ret) 33105b04c689SPavel Emelyanov goto out; 33111062af92SDarrick J. Wong } 33121da177e4SLinus Torvalds 3313a2e45955SChuck Lever ret = simple_offset_add(shmem_get_offset_ctx(dir), dentry); 3314a2e45955SChuck Lever if (ret) { 3315a2e45955SChuck Lever if (inode->i_nlink) 33162daf18a7SHugh Dickins shmem_free_inode(inode->i_sb, 0); 3317a2e45955SChuck Lever goto out; 3318a2e45955SChuck Lever } 3319a2e45955SChuck Lever 33201da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3321078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 332236f05cabSJeff Layton inode_inc_iversion(dir); 3323d8c76e6fSDave Hansen inc_nlink(inode); 33247de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 33251da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 33261da177e4SLinus Torvalds d_instantiate(dentry, inode); 33275b04c689SPavel Emelyanov out: 33285b04c689SPavel Emelyanov return ret; 33291da177e4SLinus Torvalds } 33301da177e4SLinus Torvalds 33311da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 33321da177e4SLinus Torvalds { 333375c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 33341da177e4SLinus Torvalds 33355b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 33362daf18a7SHugh Dickins shmem_free_inode(inode->i_sb, 0); 33371da177e4SLinus Torvalds 3338a2e45955SChuck Lever simple_offset_remove(shmem_get_offset_ctx(dir), dentry); 3339a2e45955SChuck Lever 33401da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 3341078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 334236f05cabSJeff Layton inode_inc_iversion(dir); 33439a53c3a7SDave Hansen drop_nlink(inode); 33441da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 33451da177e4SLinus Torvalds return 0; 33461da177e4SLinus Torvalds } 33471da177e4SLinus Torvalds 33481da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 33491da177e4SLinus Torvalds { 33501da177e4SLinus Torvalds if (!simple_empty(dentry)) 33511da177e4SLinus Torvalds return -ENOTEMPTY; 33521da177e4SLinus Torvalds 335375c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 33549a53c3a7SDave Hansen drop_nlink(dir); 33551da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 33561da177e4SLinus Torvalds } 33571da177e4SLinus Torvalds 3358e18275aeSChristian Brauner static int shmem_whiteout(struct mnt_idmap *idmap, 3359549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry) 336046fdb794SMiklos Szeredi { 336146fdb794SMiklos Szeredi struct dentry *whiteout; 336246fdb794SMiklos Szeredi int error; 336346fdb794SMiklos Szeredi 336446fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 336546fdb794SMiklos Szeredi if (!whiteout) 336646fdb794SMiklos Szeredi return -ENOMEM; 336746fdb794SMiklos Szeredi 33687a80e5b8SGiuseppe Scrivano error = shmem_mknod(idmap, old_dir, whiteout, 336946fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 337046fdb794SMiklos Szeredi dput(whiteout); 337146fdb794SMiklos Szeredi if (error) 337246fdb794SMiklos Szeredi return error; 337346fdb794SMiklos Szeredi 337446fdb794SMiklos Szeredi /* 337546fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 337646fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 337746fdb794SMiklos Szeredi * 337846fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 337946fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 338046fdb794SMiklos Szeredi */ 338146fdb794SMiklos Szeredi d_rehash(whiteout); 338246fdb794SMiklos Szeredi return 0; 338346fdb794SMiklos Szeredi } 338446fdb794SMiklos Szeredi 33851da177e4SLinus Torvalds /* 33861da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 33871da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 33881da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 33891da177e4SLinus Torvalds * gets overwritten. 33901da177e4SLinus Torvalds */ 3391e18275aeSChristian Brauner static int shmem_rename2(struct mnt_idmap *idmap, 3392549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry, 3393549c7297SChristian Brauner struct inode *new_dir, struct dentry *new_dentry, 3394549c7297SChristian Brauner unsigned int flags) 33951da177e4SLinus Torvalds { 339675c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 33971da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 3398a2e45955SChuck Lever int error; 33991da177e4SLinus Torvalds 340046fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 34013b69ff51SMiklos Szeredi return -EINVAL; 34023b69ff51SMiklos Szeredi 340337456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 3404a2e45955SChuck Lever return simple_offset_rename_exchange(old_dir, old_dentry, 3405a2e45955SChuck Lever new_dir, new_dentry); 340637456771SMiklos Szeredi 34071da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 34081da177e4SLinus Torvalds return -ENOTEMPTY; 34091da177e4SLinus Torvalds 341046fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 34117a80e5b8SGiuseppe Scrivano error = shmem_whiteout(idmap, old_dir, old_dentry); 341246fdb794SMiklos Szeredi if (error) 341346fdb794SMiklos Szeredi return error; 341446fdb794SMiklos Szeredi } 341546fdb794SMiklos Szeredi 3416a2e45955SChuck Lever simple_offset_remove(shmem_get_offset_ctx(old_dir), old_dentry); 3417a2e45955SChuck Lever error = simple_offset_add(shmem_get_offset_ctx(new_dir), old_dentry); 3418a2e45955SChuck Lever if (error) 3419a2e45955SChuck Lever return error; 3420a2e45955SChuck Lever 342175c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 34221da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 3423b928095bSMiklos Szeredi if (they_are_dirs) { 342475c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 34259a53c3a7SDave Hansen drop_nlink(old_dir); 3426b928095bSMiklos Szeredi } 34271da177e4SLinus Torvalds } else if (they_are_dirs) { 34289a53c3a7SDave Hansen drop_nlink(old_dir); 3429d8c76e6fSDave Hansen inc_nlink(new_dir); 34301da177e4SLinus Torvalds } 34311da177e4SLinus Torvalds 34321da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 34331da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 34341da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 34351da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3436078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 343736f05cabSJeff Layton inode_inc_iversion(old_dir); 343836f05cabSJeff Layton inode_inc_iversion(new_dir); 34391da177e4SLinus Torvalds return 0; 34401da177e4SLinus Torvalds } 34411da177e4SLinus Torvalds 34427a77db95SChristian Brauner static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, 3443549c7297SChristian Brauner struct dentry *dentry, const char *symname) 34441da177e4SLinus Torvalds { 34451da177e4SLinus Torvalds int error; 34461da177e4SLinus Torvalds int len; 34471da177e4SLinus Torvalds struct inode *inode; 34487ad0414bSMatthew Wilcox (Oracle) struct folio *folio; 34491da177e4SLinus Torvalds 34501da177e4SLinus Torvalds len = strlen(symname) + 1; 345109cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 34521da177e4SLinus Torvalds return -ENAMETOOLONG; 34531da177e4SLinus Torvalds 34547a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(idmap, dir->i_sb, dir, S_IFLNK | 0777, 0, 34550825a6f9SJoe Perches VM_NORESERVE); 345671480663SCarlos Maiolino 345771480663SCarlos Maiolino if (IS_ERR(inode)) 345871480663SCarlos Maiolino return PTR_ERR(inode); 34591da177e4SLinus Torvalds 34609d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 34616d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 346223a31d87SChuck Lever if (error && error != -EOPNOTSUPP) 346323a31d87SChuck Lever goto out_iput; 3464570bc1c2SStephen Smalley 3465a2e45955SChuck Lever error = simple_offset_add(shmem_get_offset_ctx(dir), dentry); 3466a2e45955SChuck Lever if (error) 3467a2e45955SChuck Lever goto out_iput; 3468a2e45955SChuck Lever 34691da177e4SLinus Torvalds inode->i_size = len-1; 347069f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 34713ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 34723ed47db3SAl Viro if (!inode->i_link) { 347323a31d87SChuck Lever error = -ENOMEM; 3474a2e45955SChuck Lever goto out_remove_offset; 347569f07ec9SHugh Dickins } 347669f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 34771da177e4SLinus Torvalds } else { 3478e8ecde25SAl Viro inode_nohighmem(inode); 34797ad0414bSMatthew Wilcox (Oracle) error = shmem_get_folio(inode, 0, &folio, SGP_WRITE); 348023a31d87SChuck Lever if (error) 3481a2e45955SChuck Lever goto out_remove_offset; 348214fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 34831da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 34847ad0414bSMatthew Wilcox (Oracle) memcpy(folio_address(folio), symname, len); 34857ad0414bSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 34867ad0414bSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 34877ad0414bSMatthew Wilcox (Oracle) folio_unlock(folio); 34887ad0414bSMatthew Wilcox (Oracle) folio_put(folio); 34891da177e4SLinus Torvalds } 34901da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3491078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 349236f05cabSJeff Layton inode_inc_iversion(dir); 34931da177e4SLinus Torvalds d_instantiate(dentry, inode); 34941da177e4SLinus Torvalds dget(dentry); 34951da177e4SLinus Torvalds return 0; 3496a2e45955SChuck Lever 3497a2e45955SChuck Lever out_remove_offset: 3498a2e45955SChuck Lever simple_offset_remove(shmem_get_offset_ctx(dir), dentry); 349923a31d87SChuck Lever out_iput: 350023a31d87SChuck Lever iput(inode); 350123a31d87SChuck Lever return error; 35021da177e4SLinus Torvalds } 35031da177e4SLinus Torvalds 3504fceef393SAl Viro static void shmem_put_link(void *arg) 3505fceef393SAl Viro { 3506e4b57722SMatthew Wilcox (Oracle) folio_mark_accessed(arg); 3507e4b57722SMatthew Wilcox (Oracle) folio_put(arg); 3508fceef393SAl Viro } 3509fceef393SAl Viro 35106b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3511fceef393SAl Viro struct inode *inode, 3512fceef393SAl Viro struct delayed_call *done) 35131da177e4SLinus Torvalds { 3514e4b57722SMatthew Wilcox (Oracle) struct folio *folio = NULL; 35156b255391SAl Viro int error; 3516e4b57722SMatthew Wilcox (Oracle) 35176a6c9904SAl Viro if (!dentry) { 3518e4b57722SMatthew Wilcox (Oracle) folio = filemap_get_folio(inode->i_mapping, 0); 351966dabbb6SChristoph Hellwig if (IS_ERR(folio)) 35206b255391SAl Viro return ERR_PTR(-ECHILD); 35217459c149SMatthew Wilcox (Oracle) if (PageHWPoison(folio_page(folio, 0)) || 3522e4b57722SMatthew Wilcox (Oracle) !folio_test_uptodate(folio)) { 3523e4b57722SMatthew Wilcox (Oracle) folio_put(folio); 35246a6c9904SAl Viro return ERR_PTR(-ECHILD); 35256a6c9904SAl Viro } 35266a6c9904SAl Viro } else { 3527e4b57722SMatthew Wilcox (Oracle) error = shmem_get_folio(inode, 0, &folio, SGP_READ); 3528680baacbSAl Viro if (error) 3529680baacbSAl Viro return ERR_PTR(error); 3530e4b57722SMatthew Wilcox (Oracle) if (!folio) 3531a7605426SYang Shi return ERR_PTR(-ECHILD); 35327459c149SMatthew Wilcox (Oracle) if (PageHWPoison(folio_page(folio, 0))) { 3533e4b57722SMatthew Wilcox (Oracle) folio_unlock(folio); 3534e4b57722SMatthew Wilcox (Oracle) folio_put(folio); 3535a7605426SYang Shi return ERR_PTR(-ECHILD); 3536a7605426SYang Shi } 3537e4b57722SMatthew Wilcox (Oracle) folio_unlock(folio); 35381da177e4SLinus Torvalds } 3539e4b57722SMatthew Wilcox (Oracle) set_delayed_call(done, shmem_put_link, folio); 3540e4b57722SMatthew Wilcox (Oracle) return folio_address(folio); 35411da177e4SLinus Torvalds } 35421da177e4SLinus Torvalds 3543b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3544e408e695STheodore Ts'o 3545e408e695STheodore Ts'o static int shmem_fileattr_get(struct dentry *dentry, struct fileattr *fa) 3546e408e695STheodore Ts'o { 3547e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3548e408e695STheodore Ts'o 3549e408e695STheodore Ts'o fileattr_fill_flags(fa, info->fsflags & SHMEM_FL_USER_VISIBLE); 3550e408e695STheodore Ts'o 3551e408e695STheodore Ts'o return 0; 3552e408e695STheodore Ts'o } 3553e408e695STheodore Ts'o 35548782a9aeSChristian Brauner static int shmem_fileattr_set(struct mnt_idmap *idmap, 3555e408e695STheodore Ts'o struct dentry *dentry, struct fileattr *fa) 3556e408e695STheodore Ts'o { 3557e408e695STheodore Ts'o struct inode *inode = d_inode(dentry); 3558e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(inode); 3559e408e695STheodore Ts'o 3560e408e695STheodore Ts'o if (fileattr_has_fsx(fa)) 3561e408e695STheodore Ts'o return -EOPNOTSUPP; 3562cb241339SHugh Dickins if (fa->flags & ~SHMEM_FL_USER_MODIFIABLE) 3563cb241339SHugh Dickins return -EOPNOTSUPP; 3564e408e695STheodore Ts'o 3565e408e695STheodore Ts'o info->fsflags = (info->fsflags & ~SHMEM_FL_USER_MODIFIABLE) | 3566e408e695STheodore Ts'o (fa->flags & SHMEM_FL_USER_MODIFIABLE); 3567e408e695STheodore Ts'o 3568cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 3569e408e695STheodore Ts'o inode->i_ctime = current_time(inode); 357036f05cabSJeff Layton inode_inc_iversion(inode); 3571e408e695STheodore Ts'o return 0; 3572e408e695STheodore Ts'o } 3573e408e695STheodore Ts'o 3574b09e0fa4SEric Paris /* 3575b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3576b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3577b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3578b09e0fa4SEric Paris * filesystem level, though. 3579b09e0fa4SEric Paris */ 3580b09e0fa4SEric Paris 35816d9d88d0SJarkko Sakkinen /* 35826d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 35836d9d88d0SJarkko Sakkinen */ 35846d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 35856d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 35866d9d88d0SJarkko Sakkinen void *fs_info) 35876d9d88d0SJarkko Sakkinen { 35886d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 35892daf18a7SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 35906d9d88d0SJarkko Sakkinen const struct xattr *xattr; 359138f38657SAristeu Rozanski struct simple_xattr *new_xattr; 35922daf18a7SHugh Dickins size_t ispace = 0; 35936d9d88d0SJarkko Sakkinen size_t len; 35946d9d88d0SJarkko Sakkinen 35952daf18a7SHugh Dickins if (sbinfo->max_inodes) { 35962daf18a7SHugh Dickins for (xattr = xattr_array; xattr->name != NULL; xattr++) { 35972daf18a7SHugh Dickins ispace += simple_xattr_space(xattr->name, 35982daf18a7SHugh Dickins xattr->value_len + XATTR_SECURITY_PREFIX_LEN); 35992daf18a7SHugh Dickins } 36002daf18a7SHugh Dickins if (ispace) { 36012daf18a7SHugh Dickins raw_spin_lock(&sbinfo->stat_lock); 36022daf18a7SHugh Dickins if (sbinfo->free_ispace < ispace) 36032daf18a7SHugh Dickins ispace = 0; 36042daf18a7SHugh Dickins else 36052daf18a7SHugh Dickins sbinfo->free_ispace -= ispace; 36062daf18a7SHugh Dickins raw_spin_unlock(&sbinfo->stat_lock); 36072daf18a7SHugh Dickins if (!ispace) 36082daf18a7SHugh Dickins return -ENOSPC; 36092daf18a7SHugh Dickins } 36102daf18a7SHugh Dickins } 36112daf18a7SHugh Dickins 36126d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 361338f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 36146d9d88d0SJarkko Sakkinen if (!new_xattr) 36152daf18a7SHugh Dickins break; 36166d9d88d0SJarkko Sakkinen 36176d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 36186d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 36196d9d88d0SJarkko Sakkinen GFP_KERNEL); 36206d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 36213bef735aSChengguang Xu kvfree(new_xattr); 36222daf18a7SHugh Dickins break; 36236d9d88d0SJarkko Sakkinen } 36246d9d88d0SJarkko Sakkinen 36256d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 36266d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 36276d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 36286d9d88d0SJarkko Sakkinen xattr->name, len); 36296d9d88d0SJarkko Sakkinen 36303b4c7bc0SChristian Brauner simple_xattr_add(&info->xattrs, new_xattr); 36316d9d88d0SJarkko Sakkinen } 36326d9d88d0SJarkko Sakkinen 36332daf18a7SHugh Dickins if (xattr->name != NULL) { 36342daf18a7SHugh Dickins if (ispace) { 36352daf18a7SHugh Dickins raw_spin_lock(&sbinfo->stat_lock); 36362daf18a7SHugh Dickins sbinfo->free_ispace += ispace; 36372daf18a7SHugh Dickins raw_spin_unlock(&sbinfo->stat_lock); 36382daf18a7SHugh Dickins } 36392daf18a7SHugh Dickins simple_xattrs_free(&info->xattrs, NULL); 36402daf18a7SHugh Dickins return -ENOMEM; 36412daf18a7SHugh Dickins } 36422daf18a7SHugh Dickins 36436d9d88d0SJarkko Sakkinen return 0; 36446d9d88d0SJarkko Sakkinen } 36456d9d88d0SJarkko Sakkinen 3646aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3647b296821aSAl Viro struct dentry *unused, struct inode *inode, 3648b296821aSAl Viro const char *name, void *buffer, size_t size) 3649aa7c5241SAndreas Gruenbacher { 3650b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3651aa7c5241SAndreas Gruenbacher 3652aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3653aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3654aa7c5241SAndreas Gruenbacher } 3655aa7c5241SAndreas Gruenbacher 3656aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 365739f60c1cSChristian Brauner struct mnt_idmap *idmap, 365859301226SAl Viro struct dentry *unused, struct inode *inode, 365959301226SAl Viro const char *name, const void *value, 366059301226SAl Viro size_t size, int flags) 3661aa7c5241SAndreas Gruenbacher { 366259301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 36632daf18a7SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 36645de75970SHugh Dickins struct simple_xattr *old_xattr; 36652daf18a7SHugh Dickins size_t ispace = 0; 3666aa7c5241SAndreas Gruenbacher 3667aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 36682daf18a7SHugh Dickins if (value && sbinfo->max_inodes) { 36692daf18a7SHugh Dickins ispace = simple_xattr_space(name, size); 36702daf18a7SHugh Dickins raw_spin_lock(&sbinfo->stat_lock); 36712daf18a7SHugh Dickins if (sbinfo->free_ispace < ispace) 36722daf18a7SHugh Dickins ispace = 0; 36732daf18a7SHugh Dickins else 36742daf18a7SHugh Dickins sbinfo->free_ispace -= ispace; 36752daf18a7SHugh Dickins raw_spin_unlock(&sbinfo->stat_lock); 36762daf18a7SHugh Dickins if (!ispace) 36772daf18a7SHugh Dickins return -ENOSPC; 36782daf18a7SHugh Dickins } 36792daf18a7SHugh Dickins 36805de75970SHugh Dickins old_xattr = simple_xattr_set(&info->xattrs, name, value, size, flags); 36815de75970SHugh Dickins if (!IS_ERR(old_xattr)) { 36822daf18a7SHugh Dickins ispace = 0; 36832daf18a7SHugh Dickins if (old_xattr && sbinfo->max_inodes) 36842daf18a7SHugh Dickins ispace = simple_xattr_space(old_xattr->name, 36852daf18a7SHugh Dickins old_xattr->size); 36865de75970SHugh Dickins simple_xattr_free(old_xattr); 36875de75970SHugh Dickins old_xattr = NULL; 368836f05cabSJeff Layton inode->i_ctime = current_time(inode); 368936f05cabSJeff Layton inode_inc_iversion(inode); 369036f05cabSJeff Layton } 36912daf18a7SHugh Dickins if (ispace) { 36922daf18a7SHugh Dickins raw_spin_lock(&sbinfo->stat_lock); 36932daf18a7SHugh Dickins sbinfo->free_ispace += ispace; 36942daf18a7SHugh Dickins raw_spin_unlock(&sbinfo->stat_lock); 36952daf18a7SHugh Dickins } 36965de75970SHugh Dickins return PTR_ERR(old_xattr); 3697aa7c5241SAndreas Gruenbacher } 3698aa7c5241SAndreas Gruenbacher 3699aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3700aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3701aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3702aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3703aa7c5241SAndreas Gruenbacher }; 3704aa7c5241SAndreas Gruenbacher 3705aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3706aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3707aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3708aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3709aa7c5241SAndreas Gruenbacher }; 3710aa7c5241SAndreas Gruenbacher 37112daf18a7SHugh Dickins static const struct xattr_handler shmem_user_xattr_handler = { 37122daf18a7SHugh Dickins .prefix = XATTR_USER_PREFIX, 37132daf18a7SHugh Dickins .get = shmem_xattr_handler_get, 37142daf18a7SHugh Dickins .set = shmem_xattr_handler_set, 37152daf18a7SHugh Dickins }; 37162daf18a7SHugh Dickins 3717b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3718aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3719aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 37202daf18a7SHugh Dickins &shmem_user_xattr_handler, 3721b09e0fa4SEric Paris NULL 3722b09e0fa4SEric Paris }; 3723b09e0fa4SEric Paris 3724b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3725b09e0fa4SEric Paris { 372675c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3727786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3728b09e0fa4SEric Paris } 3729b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3730b09e0fa4SEric Paris 373169f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 3732f7cd16a5SXavier Roche .getattr = shmem_getattr, 3733e09764cfSCarlos Maiolino .setattr = shmem_setattr, 37346b255391SAl Viro .get_link = simple_get_link, 3735b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3736b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3737b09e0fa4SEric Paris #endif 37381da177e4SLinus Torvalds }; 37391da177e4SLinus Torvalds 374092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 3741f7cd16a5SXavier Roche .getattr = shmem_getattr, 3742e09764cfSCarlos Maiolino .setattr = shmem_setattr, 37436b255391SAl Viro .get_link = shmem_get_link, 3744b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3745b09e0fa4SEric Paris .listxattr = shmem_listxattr, 374639f0247dSAndreas Gruenbacher #endif 3747b09e0fa4SEric Paris }; 374839f0247dSAndreas Gruenbacher 374991828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 375091828a40SDavid M. Grimes { 375191828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 375291828a40SDavid M. Grimes } 375391828a40SDavid M. Grimes 375491828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 375591828a40SDavid M. Grimes { 375691828a40SDavid M. Grimes __u32 *fh = vfh; 375791828a40SDavid M. Grimes __u64 inum = fh[2]; 375891828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 375991828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 376091828a40SDavid M. Grimes } 376191828a40SDavid M. Grimes 376212ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 376312ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 376412ba780dSAmir Goldstein { 376512ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 376612ba780dSAmir Goldstein 376712ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 376812ba780dSAmir Goldstein } 376912ba780dSAmir Goldstein 377012ba780dSAmir Goldstein 3771480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3772480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 377391828a40SDavid M. Grimes { 377491828a40SDavid M. Grimes struct inode *inode; 3775480b116cSChristoph Hellwig struct dentry *dentry = NULL; 377635c2a7f4SHugh Dickins u64 inum; 377791828a40SDavid M. Grimes 3778480b116cSChristoph Hellwig if (fh_len < 3) 3779480b116cSChristoph Hellwig return NULL; 3780480b116cSChristoph Hellwig 378135c2a7f4SHugh Dickins inum = fid->raw[2]; 378235c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 378335c2a7f4SHugh Dickins 3784480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3785480b116cSChristoph Hellwig shmem_match, fid->raw); 378691828a40SDavid M. Grimes if (inode) { 378712ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 378891828a40SDavid M. Grimes iput(inode); 378991828a40SDavid M. Grimes } 379091828a40SDavid M. Grimes 3791480b116cSChristoph Hellwig return dentry; 379291828a40SDavid M. Grimes } 379391828a40SDavid M. Grimes 3794b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3795b0b0382bSAl Viro struct inode *parent) 379691828a40SDavid M. Grimes { 37975fe0c237SAneesh Kumar K.V if (*len < 3) { 37985fe0c237SAneesh Kumar K.V *len = 3; 379994e07a75SNamjae Jeon return FILEID_INVALID; 38005fe0c237SAneesh Kumar K.V } 380191828a40SDavid M. Grimes 38021d3382cbSAl Viro if (inode_unhashed(inode)) { 380391828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 380491828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 380591828a40SDavid M. Grimes * time, we need a lock to ensure we only try 380691828a40SDavid M. Grimes * to do it once 380791828a40SDavid M. Grimes */ 380891828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 380991828a40SDavid M. Grimes spin_lock(&lock); 38101d3382cbSAl Viro if (inode_unhashed(inode)) 381191828a40SDavid M. Grimes __insert_inode_hash(inode, 381291828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 381391828a40SDavid M. Grimes spin_unlock(&lock); 381491828a40SDavid M. Grimes } 381591828a40SDavid M. Grimes 381691828a40SDavid M. Grimes fh[0] = inode->i_generation; 381791828a40SDavid M. Grimes fh[1] = inode->i_ino; 381891828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 381991828a40SDavid M. Grimes 382091828a40SDavid M. Grimes *len = 3; 382191828a40SDavid M. Grimes return 1; 382291828a40SDavid M. Grimes } 382391828a40SDavid M. Grimes 382439655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 382591828a40SDavid M. Grimes .get_parent = shmem_get_parent, 382691828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3827480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 382891828a40SDavid M. Grimes }; 382991828a40SDavid M. Grimes 3830626c3920SAl Viro enum shmem_param { 3831626c3920SAl Viro Opt_gid, 3832626c3920SAl Viro Opt_huge, 3833626c3920SAl Viro Opt_mode, 3834626c3920SAl Viro Opt_mpol, 3835626c3920SAl Viro Opt_nr_blocks, 3836626c3920SAl Viro Opt_nr_inodes, 3837626c3920SAl Viro Opt_size, 3838626c3920SAl Viro Opt_uid, 3839ea3271f7SChris Down Opt_inode32, 3840ea3271f7SChris Down Opt_inode64, 38412c6efe9cSLuis Chamberlain Opt_noswap, 3842e09764cfSCarlos Maiolino Opt_quota, 3843e09764cfSCarlos Maiolino Opt_usrquota, 3844e09764cfSCarlos Maiolino Opt_grpquota, 3845de4c0e7cSLukas Czerner Opt_usrquota_block_hardlimit, 3846de4c0e7cSLukas Czerner Opt_usrquota_inode_hardlimit, 3847de4c0e7cSLukas Czerner Opt_grpquota_block_hardlimit, 3848de4c0e7cSLukas Czerner Opt_grpquota_inode_hardlimit, 3849626c3920SAl Viro }; 38501da177e4SLinus Torvalds 38515eede625SAl Viro static const struct constant_table shmem_param_enums_huge[] = { 38522710c957SAl Viro {"never", SHMEM_HUGE_NEVER }, 38532710c957SAl Viro {"always", SHMEM_HUGE_ALWAYS }, 38542710c957SAl Viro {"within_size", SHMEM_HUGE_WITHIN_SIZE }, 38552710c957SAl Viro {"advise", SHMEM_HUGE_ADVISE }, 38562710c957SAl Viro {} 38572710c957SAl Viro }; 38582710c957SAl Viro 3859d7167b14SAl Viro const struct fs_parameter_spec shmem_fs_parameters[] = { 3860626c3920SAl Viro fsparam_u32 ("gid", Opt_gid), 38612710c957SAl Viro fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge), 3862626c3920SAl Viro fsparam_u32oct("mode", Opt_mode), 3863626c3920SAl Viro fsparam_string("mpol", Opt_mpol), 3864626c3920SAl Viro fsparam_string("nr_blocks", Opt_nr_blocks), 3865626c3920SAl Viro fsparam_string("nr_inodes", Opt_nr_inodes), 3866626c3920SAl Viro fsparam_string("size", Opt_size), 3867626c3920SAl Viro fsparam_u32 ("uid", Opt_uid), 3868ea3271f7SChris Down fsparam_flag ("inode32", Opt_inode32), 3869ea3271f7SChris Down fsparam_flag ("inode64", Opt_inode64), 38702c6efe9cSLuis Chamberlain fsparam_flag ("noswap", Opt_noswap), 3871e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 3872e09764cfSCarlos Maiolino fsparam_flag ("quota", Opt_quota), 3873e09764cfSCarlos Maiolino fsparam_flag ("usrquota", Opt_usrquota), 3874e09764cfSCarlos Maiolino fsparam_flag ("grpquota", Opt_grpquota), 3875de4c0e7cSLukas Czerner fsparam_string("usrquota_block_hardlimit", Opt_usrquota_block_hardlimit), 3876de4c0e7cSLukas Czerner fsparam_string("usrquota_inode_hardlimit", Opt_usrquota_inode_hardlimit), 3877de4c0e7cSLukas Czerner fsparam_string("grpquota_block_hardlimit", Opt_grpquota_block_hardlimit), 3878de4c0e7cSLukas Czerner fsparam_string("grpquota_inode_hardlimit", Opt_grpquota_inode_hardlimit), 3879e09764cfSCarlos Maiolino #endif 3880626c3920SAl Viro {} 3881626c3920SAl Viro }; 3882626c3920SAl Viro 3883f3235626SDavid Howells static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) 3884626c3920SAl Viro { 3885f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3886626c3920SAl Viro struct fs_parse_result result; 3887e04dc423SAl Viro unsigned long long size; 3888626c3920SAl Viro char *rest; 3889626c3920SAl Viro int opt; 38900200679fSChristian Brauner kuid_t kuid; 38910200679fSChristian Brauner kgid_t kgid; 3892626c3920SAl Viro 3893d7167b14SAl Viro opt = fs_parse(fc, shmem_fs_parameters, param, &result); 3894f3235626SDavid Howells if (opt < 0) 3895626c3920SAl Viro return opt; 3896626c3920SAl Viro 3897626c3920SAl Viro switch (opt) { 3898626c3920SAl Viro case Opt_size: 3899626c3920SAl Viro size = memparse(param->string, &rest); 3900e04dc423SAl Viro if (*rest == '%') { 3901e04dc423SAl Viro size <<= PAGE_SHIFT; 3902e04dc423SAl Viro size *= totalram_pages(); 3903e04dc423SAl Viro do_div(size, 100); 3904e04dc423SAl Viro rest++; 3905e04dc423SAl Viro } 3906e04dc423SAl Viro if (*rest) 3907626c3920SAl Viro goto bad_value; 3908e04dc423SAl Viro ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE); 3909e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3910626c3920SAl Viro break; 3911626c3920SAl Viro case Opt_nr_blocks: 3912626c3920SAl Viro ctx->blocks = memparse(param->string, &rest); 3913e07c469eSHugh Dickins if (*rest || ctx->blocks > LONG_MAX) 3914626c3920SAl Viro goto bad_value; 3915e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3916626c3920SAl Viro break; 3917626c3920SAl Viro case Opt_nr_inodes: 3918626c3920SAl Viro ctx->inodes = memparse(param->string, &rest); 3919e07c469eSHugh Dickins if (*rest || ctx->inodes > ULONG_MAX / BOGO_INODE_SIZE) 3920626c3920SAl Viro goto bad_value; 3921e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_INODES; 3922626c3920SAl Viro break; 3923626c3920SAl Viro case Opt_mode: 3924626c3920SAl Viro ctx->mode = result.uint_32 & 07777; 3925626c3920SAl Viro break; 3926626c3920SAl Viro case Opt_uid: 39270200679fSChristian Brauner kuid = make_kuid(current_user_ns(), result.uint_32); 39280200679fSChristian Brauner if (!uid_valid(kuid)) 3929626c3920SAl Viro goto bad_value; 39300200679fSChristian Brauner 39310200679fSChristian Brauner /* 39320200679fSChristian Brauner * The requested uid must be representable in the 39330200679fSChristian Brauner * filesystem's idmapping. 39340200679fSChristian Brauner */ 39350200679fSChristian Brauner if (!kuid_has_mapping(fc->user_ns, kuid)) 39360200679fSChristian Brauner goto bad_value; 39370200679fSChristian Brauner 39380200679fSChristian Brauner ctx->uid = kuid; 3939626c3920SAl Viro break; 3940626c3920SAl Viro case Opt_gid: 39410200679fSChristian Brauner kgid = make_kgid(current_user_ns(), result.uint_32); 39420200679fSChristian Brauner if (!gid_valid(kgid)) 3943626c3920SAl Viro goto bad_value; 39440200679fSChristian Brauner 39450200679fSChristian Brauner /* 39460200679fSChristian Brauner * The requested gid must be representable in the 39470200679fSChristian Brauner * filesystem's idmapping. 39480200679fSChristian Brauner */ 39490200679fSChristian Brauner if (!kgid_has_mapping(fc->user_ns, kgid)) 39500200679fSChristian Brauner goto bad_value; 39510200679fSChristian Brauner 39520200679fSChristian Brauner ctx->gid = kgid; 3953626c3920SAl Viro break; 3954626c3920SAl Viro case Opt_huge: 3955626c3920SAl Viro ctx->huge = result.uint_32; 3956626c3920SAl Viro if (ctx->huge != SHMEM_HUGE_NEVER && 3957396bcc52SMatthew Wilcox (Oracle) !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 3958626c3920SAl Viro has_transparent_hugepage())) 3959626c3920SAl Viro goto unsupported_parameter; 3960e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_HUGE; 3961626c3920SAl Viro break; 3962626c3920SAl Viro case Opt_mpol: 3963626c3920SAl Viro if (IS_ENABLED(CONFIG_NUMA)) { 3964e04dc423SAl Viro mpol_put(ctx->mpol); 3965e04dc423SAl Viro ctx->mpol = NULL; 3966626c3920SAl Viro if (mpol_parse_str(param->string, &ctx->mpol)) 3967626c3920SAl Viro goto bad_value; 3968626c3920SAl Viro break; 3969626c3920SAl Viro } 3970626c3920SAl Viro goto unsupported_parameter; 3971ea3271f7SChris Down case Opt_inode32: 3972ea3271f7SChris Down ctx->full_inums = false; 3973ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3974ea3271f7SChris Down break; 3975ea3271f7SChris Down case Opt_inode64: 3976ea3271f7SChris Down if (sizeof(ino_t) < 8) { 3977ea3271f7SChris Down return invalfc(fc, 3978ea3271f7SChris Down "Cannot use inode64 with <64bit inums in kernel\n"); 3979ea3271f7SChris Down } 3980ea3271f7SChris Down ctx->full_inums = true; 3981ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3982ea3271f7SChris Down break; 39832c6efe9cSLuis Chamberlain case Opt_noswap: 398401106e14SChristian Brauner if ((fc->user_ns != &init_user_ns) || !capable(CAP_SYS_ADMIN)) { 398501106e14SChristian Brauner return invalfc(fc, 398601106e14SChristian Brauner "Turning off swap in unprivileged tmpfs mounts unsupported"); 398701106e14SChristian Brauner } 39882c6efe9cSLuis Chamberlain ctx->noswap = true; 39892c6efe9cSLuis Chamberlain ctx->seen |= SHMEM_SEEN_NOSWAP; 39902c6efe9cSLuis Chamberlain break; 3991e09764cfSCarlos Maiolino case Opt_quota: 3992e09764cfSCarlos Maiolino if (fc->user_ns != &init_user_ns) 3993e09764cfSCarlos Maiolino return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); 3994e09764cfSCarlos Maiolino ctx->seen |= SHMEM_SEEN_QUOTA; 3995e09764cfSCarlos Maiolino ctx->quota_types |= (QTYPE_MASK_USR | QTYPE_MASK_GRP); 3996e09764cfSCarlos Maiolino break; 3997e09764cfSCarlos Maiolino case Opt_usrquota: 3998e09764cfSCarlos Maiolino if (fc->user_ns != &init_user_ns) 3999e09764cfSCarlos Maiolino return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); 4000e09764cfSCarlos Maiolino ctx->seen |= SHMEM_SEEN_QUOTA; 4001e09764cfSCarlos Maiolino ctx->quota_types |= QTYPE_MASK_USR; 4002e09764cfSCarlos Maiolino break; 4003e09764cfSCarlos Maiolino case Opt_grpquota: 4004e09764cfSCarlos Maiolino if (fc->user_ns != &init_user_ns) 4005e09764cfSCarlos Maiolino return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); 4006e09764cfSCarlos Maiolino ctx->seen |= SHMEM_SEEN_QUOTA; 4007e09764cfSCarlos Maiolino ctx->quota_types |= QTYPE_MASK_GRP; 4008e09764cfSCarlos Maiolino break; 4009de4c0e7cSLukas Czerner case Opt_usrquota_block_hardlimit: 4010de4c0e7cSLukas Czerner size = memparse(param->string, &rest); 4011de4c0e7cSLukas Czerner if (*rest || !size) 4012de4c0e7cSLukas Czerner goto bad_value; 4013de4c0e7cSLukas Czerner if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) 4014de4c0e7cSLukas Czerner return invalfc(fc, 4015de4c0e7cSLukas Czerner "User quota block hardlimit too large."); 4016de4c0e7cSLukas Czerner ctx->qlimits.usrquota_bhardlimit = size; 4017de4c0e7cSLukas Czerner break; 4018de4c0e7cSLukas Czerner case Opt_grpquota_block_hardlimit: 4019de4c0e7cSLukas Czerner size = memparse(param->string, &rest); 4020de4c0e7cSLukas Czerner if (*rest || !size) 4021de4c0e7cSLukas Czerner goto bad_value; 4022de4c0e7cSLukas Czerner if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) 4023de4c0e7cSLukas Czerner return invalfc(fc, 4024de4c0e7cSLukas Czerner "Group quota block hardlimit too large."); 4025de4c0e7cSLukas Czerner ctx->qlimits.grpquota_bhardlimit = size; 4026de4c0e7cSLukas Czerner break; 4027de4c0e7cSLukas Czerner case Opt_usrquota_inode_hardlimit: 4028de4c0e7cSLukas Czerner size = memparse(param->string, &rest); 4029de4c0e7cSLukas Czerner if (*rest || !size) 4030de4c0e7cSLukas Czerner goto bad_value; 4031de4c0e7cSLukas Czerner if (size > SHMEM_QUOTA_MAX_INO_LIMIT) 4032de4c0e7cSLukas Czerner return invalfc(fc, 4033de4c0e7cSLukas Czerner "User quota inode hardlimit too large."); 4034de4c0e7cSLukas Czerner ctx->qlimits.usrquota_ihardlimit = size; 4035de4c0e7cSLukas Czerner break; 4036de4c0e7cSLukas Czerner case Opt_grpquota_inode_hardlimit: 4037de4c0e7cSLukas Czerner size = memparse(param->string, &rest); 4038de4c0e7cSLukas Czerner if (*rest || !size) 4039de4c0e7cSLukas Czerner goto bad_value; 4040de4c0e7cSLukas Czerner if (size > SHMEM_QUOTA_MAX_INO_LIMIT) 4041de4c0e7cSLukas Czerner return invalfc(fc, 4042de4c0e7cSLukas Czerner "Group quota inode hardlimit too large."); 4043de4c0e7cSLukas Czerner ctx->qlimits.grpquota_ihardlimit = size; 4044de4c0e7cSLukas Czerner break; 4045e04dc423SAl Viro } 4046e04dc423SAl Viro return 0; 4047e04dc423SAl Viro 4048626c3920SAl Viro unsupported_parameter: 4049f35aa2bcSAl Viro return invalfc(fc, "Unsupported parameter '%s'", param->key); 4050626c3920SAl Viro bad_value: 4051f35aa2bcSAl Viro return invalfc(fc, "Bad value for '%s'", param->key); 4052e04dc423SAl Viro } 4053e04dc423SAl Viro 4054f3235626SDavid Howells static int shmem_parse_options(struct fs_context *fc, void *data) 4055e04dc423SAl Viro { 4056f3235626SDavid Howells char *options = data; 4057f3235626SDavid Howells 405833f37c64SAl Viro if (options) { 405933f37c64SAl Viro int err = security_sb_eat_lsm_opts(options, &fc->security); 406033f37c64SAl Viro if (err) 406133f37c64SAl Viro return err; 406233f37c64SAl Viro } 406333f37c64SAl Viro 4064b00dc3adSHugh Dickins while (options != NULL) { 4065626c3920SAl Viro char *this_char = options; 4066b00dc3adSHugh Dickins for (;;) { 4067b00dc3adSHugh Dickins /* 4068b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 4069b00dc3adSHugh Dickins * mount options form a comma-separated list, 4070b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 4071b00dc3adSHugh Dickins */ 4072b00dc3adSHugh Dickins options = strchr(options, ','); 4073b00dc3adSHugh Dickins if (options == NULL) 4074b00dc3adSHugh Dickins break; 4075b00dc3adSHugh Dickins options++; 4076b00dc3adSHugh Dickins if (!isdigit(*options)) { 4077b00dc3adSHugh Dickins options[-1] = '\0'; 4078b00dc3adSHugh Dickins break; 4079b00dc3adSHugh Dickins } 4080b00dc3adSHugh Dickins } 4081626c3920SAl Viro if (*this_char) { 4082626c3920SAl Viro char *value = strchr(this_char, '='); 4083f3235626SDavid Howells size_t len = 0; 4084626c3920SAl Viro int err; 4085626c3920SAl Viro 4086626c3920SAl Viro if (value) { 4087626c3920SAl Viro *value++ = '\0'; 4088f3235626SDavid Howells len = strlen(value); 40891da177e4SLinus Torvalds } 4090f3235626SDavid Howells err = vfs_parse_fs_string(fc, this_char, value, len); 4091f3235626SDavid Howells if (err < 0) 4092f3235626SDavid Howells return err; 40931da177e4SLinus Torvalds } 4094626c3920SAl Viro } 40951da177e4SLinus Torvalds return 0; 40961da177e4SLinus Torvalds } 40971da177e4SLinus Torvalds 4098f3235626SDavid Howells /* 4099f3235626SDavid Howells * Reconfigure a shmem filesystem. 4100f3235626SDavid Howells */ 4101f3235626SDavid Howells static int shmem_reconfigure(struct fs_context *fc) 41021da177e4SLinus Torvalds { 4103f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 4104f3235626SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); 4105e07c469eSHugh Dickins unsigned long used_isp; 4106bf11b9a8SSebastian Andrzej Siewior struct mempolicy *mpol = NULL; 4107f3235626SDavid Howells const char *err; 41080edd73b3SHugh Dickins 4109bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 4110e07c469eSHugh Dickins used_isp = sbinfo->max_inodes * BOGO_INODE_SIZE - sbinfo->free_ispace; 41110c98c8e1SZhaoLong Wang 4112f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { 4113f3235626SDavid Howells if (!sbinfo->max_blocks) { 4114f3235626SDavid Howells err = "Cannot retroactively limit size"; 41150edd73b3SHugh Dickins goto out; 41160b5071ddSAl Viro } 4117f3235626SDavid Howells if (percpu_counter_compare(&sbinfo->used_blocks, 4118f3235626SDavid Howells ctx->blocks) > 0) { 4119f3235626SDavid Howells err = "Too small a size for current use"; 41200b5071ddSAl Viro goto out; 4121f3235626SDavid Howells } 4122f3235626SDavid Howells } 4123f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) { 4124f3235626SDavid Howells if (!sbinfo->max_inodes) { 4125f3235626SDavid Howells err = "Cannot retroactively limit inodes"; 41260b5071ddSAl Viro goto out; 41270b5071ddSAl Viro } 4128e07c469eSHugh Dickins if (ctx->inodes * BOGO_INODE_SIZE < used_isp) { 4129f3235626SDavid Howells err = "Too few inodes for current use"; 4130f3235626SDavid Howells goto out; 4131f3235626SDavid Howells } 4132f3235626SDavid Howells } 41330edd73b3SHugh Dickins 4134ea3271f7SChris Down if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums && 4135ea3271f7SChris Down sbinfo->next_ino > UINT_MAX) { 4136ea3271f7SChris Down err = "Current inum too high to switch to 32-bit inums"; 4137ea3271f7SChris Down goto out; 4138ea3271f7SChris Down } 41392c6efe9cSLuis Chamberlain if ((ctx->seen & SHMEM_SEEN_NOSWAP) && ctx->noswap && !sbinfo->noswap) { 41402c6efe9cSLuis Chamberlain err = "Cannot disable swap on remount"; 41412c6efe9cSLuis Chamberlain goto out; 41422c6efe9cSLuis Chamberlain } 41432c6efe9cSLuis Chamberlain if (!(ctx->seen & SHMEM_SEEN_NOSWAP) && !ctx->noswap && sbinfo->noswap) { 41442c6efe9cSLuis Chamberlain err = "Cannot enable swap on remount if it was disabled on first mount"; 41452c6efe9cSLuis Chamberlain goto out; 41462c6efe9cSLuis Chamberlain } 4147ea3271f7SChris Down 4148e09764cfSCarlos Maiolino if (ctx->seen & SHMEM_SEEN_QUOTA && 4149e09764cfSCarlos Maiolino !sb_any_quota_loaded(fc->root->d_sb)) { 4150e09764cfSCarlos Maiolino err = "Cannot enable quota on remount"; 4151e09764cfSCarlos Maiolino goto out; 4152e09764cfSCarlos Maiolino } 4153e09764cfSCarlos Maiolino 4154de4c0e7cSLukas Czerner #ifdef CONFIG_TMPFS_QUOTA 4155de4c0e7cSLukas Czerner #define CHANGED_LIMIT(name) \ 4156de4c0e7cSLukas Czerner (ctx->qlimits.name## hardlimit && \ 4157de4c0e7cSLukas Czerner (ctx->qlimits.name## hardlimit != sbinfo->qlimits.name## hardlimit)) 4158de4c0e7cSLukas Czerner 4159de4c0e7cSLukas Czerner if (CHANGED_LIMIT(usrquota_b) || CHANGED_LIMIT(usrquota_i) || 4160de4c0e7cSLukas Czerner CHANGED_LIMIT(grpquota_b) || CHANGED_LIMIT(grpquota_i)) { 4161de4c0e7cSLukas Czerner err = "Cannot change global quota limit on remount"; 4162de4c0e7cSLukas Czerner goto out; 4163de4c0e7cSLukas Czerner } 4164de4c0e7cSLukas Czerner #endif /* CONFIG_TMPFS_QUOTA */ 4165de4c0e7cSLukas Czerner 4166f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_HUGE) 4167f3235626SDavid Howells sbinfo->huge = ctx->huge; 4168ea3271f7SChris Down if (ctx->seen & SHMEM_SEEN_INUMS) 4169ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 4170f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_BLOCKS) 4171f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 4172f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_INODES) { 4173f3235626SDavid Howells sbinfo->max_inodes = ctx->inodes; 4174e07c469eSHugh Dickins sbinfo->free_ispace = ctx->inodes * BOGO_INODE_SIZE - used_isp; 41750b5071ddSAl Viro } 417671fe804bSLee Schermerhorn 41775f00110fSGreg Thelen /* 41785f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 41795f00110fSGreg Thelen */ 4180f3235626SDavid Howells if (ctx->mpol) { 4181bf11b9a8SSebastian Andrzej Siewior mpol = sbinfo->mpol; 4182f3235626SDavid Howells sbinfo->mpol = ctx->mpol; /* transfers initial ref */ 4183f3235626SDavid Howells ctx->mpol = NULL; 41845f00110fSGreg Thelen } 41852c6efe9cSLuis Chamberlain 41862c6efe9cSLuis Chamberlain if (ctx->noswap) 41872c6efe9cSLuis Chamberlain sbinfo->noswap = true; 41882c6efe9cSLuis Chamberlain 4189bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 4190bf11b9a8SSebastian Andrzej Siewior mpol_put(mpol); 4191f3235626SDavid Howells return 0; 41920edd73b3SHugh Dickins out: 4193bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 4194f35aa2bcSAl Viro return invalfc(fc, "%s", err); 41951da177e4SLinus Torvalds } 4196680d794bSakpm@linux-foundation.org 419734c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 4198680d794bSakpm@linux-foundation.org { 419934c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 4200283ebdeeSTu Jinjiang struct mempolicy *mpol; 4201680d794bSakpm@linux-foundation.org 4202680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 4203680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 420409cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 4205680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 4206680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 42070825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 420809208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 42098751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 42108751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 42118751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 42128751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 42138751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 42148751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 4215ea3271f7SChris Down 4216ea3271f7SChris Down /* 4217ea3271f7SChris Down * Showing inode{64,32} might be useful even if it's the system default, 4218ea3271f7SChris Down * since then people don't have to resort to checking both here and 4219ea3271f7SChris Down * /proc/config.gz to confirm 64-bit inums were successfully applied 4220ea3271f7SChris Down * (which may not even exist if IKCONFIG_PROC isn't enabled). 4221ea3271f7SChris Down * 4222ea3271f7SChris Down * We hide it when inode64 isn't the default and we are using 32-bit 4223ea3271f7SChris Down * inodes, since that probably just means the feature isn't even under 4224ea3271f7SChris Down * consideration. 4225ea3271f7SChris Down * 4226ea3271f7SChris Down * As such: 4227ea3271f7SChris Down * 4228ea3271f7SChris Down * +-----------------+-----------------+ 4229ea3271f7SChris Down * | TMPFS_INODE64=y | TMPFS_INODE64=n | 4230ea3271f7SChris Down * +------------------+-----------------+-----------------+ 4231ea3271f7SChris Down * | full_inums=true | show | show | 4232ea3271f7SChris Down * | full_inums=false | show | hide | 4233ea3271f7SChris Down * +------------------+-----------------+-----------------+ 4234ea3271f7SChris Down * 4235ea3271f7SChris Down */ 4236ea3271f7SChris Down if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums) 4237ea3271f7SChris Down seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32)); 4238396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 42395a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 42405a6e75f8SKirill A. Shutemov if (sbinfo->huge) 42415a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 42425a6e75f8SKirill A. Shutemov #endif 4243283ebdeeSTu Jinjiang mpol = shmem_get_sbmpol(sbinfo); 4244283ebdeeSTu Jinjiang shmem_show_mpol(seq, mpol); 4245283ebdeeSTu Jinjiang mpol_put(mpol); 42462c6efe9cSLuis Chamberlain if (sbinfo->noswap) 42472c6efe9cSLuis Chamberlain seq_printf(seq, ",noswap"); 4248680d794bSakpm@linux-foundation.org return 0; 4249680d794bSakpm@linux-foundation.org } 42509183df25SDavid Herrmann 4251680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 42521da177e4SLinus Torvalds 42531da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 42541da177e4SLinus Torvalds { 4255602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 4256602586a8SHugh Dickins 4257e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 4258e09764cfSCarlos Maiolino shmem_disable_quotas(sb); 4259e09764cfSCarlos Maiolino #endif 4260e809d5f0SChris Down free_percpu(sbinfo->ino_batch); 4261602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 426249cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 4263602586a8SHugh Dickins kfree(sbinfo); 42641da177e4SLinus Torvalds sb->s_fs_info = NULL; 42651da177e4SLinus Torvalds } 42661da177e4SLinus Torvalds 4267f3235626SDavid Howells static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) 42681da177e4SLinus Torvalds { 4269f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 42701da177e4SLinus Torvalds struct inode *inode; 42710edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 427271480663SCarlos Maiolino int error = -ENOMEM; 4273680d794bSakpm@linux-foundation.org 4274680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 4275425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 4276680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 4277680d794bSakpm@linux-foundation.org if (!sbinfo) 427871480663SCarlos Maiolino return error; 4279680d794bSakpm@linux-foundation.org 4280680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 42811da177e4SLinus Torvalds 42820edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 42831da177e4SLinus Torvalds /* 42841da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 42851da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 42861da177e4SLinus Torvalds * but the internal instance is left unlimited. 42871da177e4SLinus Torvalds */ 42881751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 4289f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) 4290f3235626SDavid Howells ctx->blocks = shmem_default_max_blocks(); 4291f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_INODES)) 4292f3235626SDavid Howells ctx->inodes = shmem_default_max_inodes(); 4293ea3271f7SChris Down if (!(ctx->seen & SHMEM_SEEN_INUMS)) 4294ea3271f7SChris Down ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64); 42952c6efe9cSLuis Chamberlain sbinfo->noswap = ctx->noswap; 4296ca4e0519SAl Viro } else { 42971751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 42981da177e4SLinus Torvalds } 429991828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 430036f05cabSJeff Layton sb->s_flags |= SB_NOSEC | SB_I_VERSION; 43010edd73b3SHugh Dickins #else 43021751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 43030edd73b3SHugh Dickins #endif 4304f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 4305e07c469eSHugh Dickins sbinfo->max_inodes = ctx->inodes; 4306e07c469eSHugh Dickins sbinfo->free_ispace = sbinfo->max_inodes * BOGO_INODE_SIZE; 4307e809d5f0SChris Down if (sb->s_flags & SB_KERNMOUNT) { 4308e809d5f0SChris Down sbinfo->ino_batch = alloc_percpu(ino_t); 4309e809d5f0SChris Down if (!sbinfo->ino_batch) 4310e809d5f0SChris Down goto failed; 4311e809d5f0SChris Down } 4312f3235626SDavid Howells sbinfo->uid = ctx->uid; 4313f3235626SDavid Howells sbinfo->gid = ctx->gid; 4314ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 4315f3235626SDavid Howells sbinfo->mode = ctx->mode; 4316f3235626SDavid Howells sbinfo->huge = ctx->huge; 4317f3235626SDavid Howells sbinfo->mpol = ctx->mpol; 4318f3235626SDavid Howells ctx->mpol = NULL; 43191da177e4SLinus Torvalds 4320bf11b9a8SSebastian Andrzej Siewior raw_spin_lock_init(&sbinfo->stat_lock); 4321908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 4322602586a8SHugh Dickins goto failed; 4323779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 4324779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 43251da177e4SLinus Torvalds 4326285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 432709cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 432809cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 43291da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 43301da177e4SLinus Torvalds sb->s_op = &shmem_ops; 4331cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 4332b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 433339f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 4334b09e0fa4SEric Paris #endif 4335b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 43361751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 433739f0247dSAndreas Gruenbacher #endif 43382b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 43390edd73b3SHugh Dickins 4340e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 4341e09764cfSCarlos Maiolino if (ctx->seen & SHMEM_SEEN_QUOTA) { 4342e09764cfSCarlos Maiolino sb->dq_op = &shmem_quota_operations; 4343e09764cfSCarlos Maiolino sb->s_qcop = &dquot_quotactl_sysfile_ops; 4344e09764cfSCarlos Maiolino sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; 4345e09764cfSCarlos Maiolino 4346de4c0e7cSLukas Czerner /* Copy the default limits from ctx into sbinfo */ 4347de4c0e7cSLukas Czerner memcpy(&sbinfo->qlimits, &ctx->qlimits, 4348de4c0e7cSLukas Czerner sizeof(struct shmem_quota_limits)); 4349de4c0e7cSLukas Czerner 4350e09764cfSCarlos Maiolino if (shmem_enable_quotas(sb, ctx->quota_types)) 4351e09764cfSCarlos Maiolino goto failed; 4352e09764cfSCarlos Maiolino } 4353e09764cfSCarlos Maiolino #endif /* CONFIG_TMPFS_QUOTA */ 4354e09764cfSCarlos Maiolino 43557a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(&nop_mnt_idmap, sb, NULL, S_IFDIR | sbinfo->mode, 0, 43567a80e5b8SGiuseppe Scrivano VM_NORESERVE); 435771480663SCarlos Maiolino if (IS_ERR(inode)) { 435871480663SCarlos Maiolino error = PTR_ERR(inode); 43591da177e4SLinus Torvalds goto failed; 436071480663SCarlos Maiolino } 4361680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 4362680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 4363318ceed0SAl Viro sb->s_root = d_make_root(inode); 4364318ceed0SAl Viro if (!sb->s_root) 436548fde701SAl Viro goto failed; 43661da177e4SLinus Torvalds return 0; 43671da177e4SLinus Torvalds 43681da177e4SLinus Torvalds failed: 43691da177e4SLinus Torvalds shmem_put_super(sb); 437071480663SCarlos Maiolino return error; 43711da177e4SLinus Torvalds } 43721da177e4SLinus Torvalds 4373f3235626SDavid Howells static int shmem_get_tree(struct fs_context *fc) 4374f3235626SDavid Howells { 4375f3235626SDavid Howells return get_tree_nodev(fc, shmem_fill_super); 4376f3235626SDavid Howells } 4377f3235626SDavid Howells 4378f3235626SDavid Howells static void shmem_free_fc(struct fs_context *fc) 4379f3235626SDavid Howells { 4380f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 4381f3235626SDavid Howells 4382f3235626SDavid Howells if (ctx) { 4383f3235626SDavid Howells mpol_put(ctx->mpol); 4384f3235626SDavid Howells kfree(ctx); 4385f3235626SDavid Howells } 4386f3235626SDavid Howells } 4387f3235626SDavid Howells 4388f3235626SDavid Howells static const struct fs_context_operations shmem_fs_context_ops = { 4389f3235626SDavid Howells .free = shmem_free_fc, 4390f3235626SDavid Howells .get_tree = shmem_get_tree, 4391f3235626SDavid Howells #ifdef CONFIG_TMPFS 4392f3235626SDavid Howells .parse_monolithic = shmem_parse_options, 4393f3235626SDavid Howells .parse_param = shmem_parse_one, 4394f3235626SDavid Howells .reconfigure = shmem_reconfigure, 4395f3235626SDavid Howells #endif 4396f3235626SDavid Howells }; 4397f3235626SDavid Howells 4398fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 43991da177e4SLinus Torvalds 44001da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 44011da177e4SLinus Torvalds { 440241ffe5d5SHugh Dickins struct shmem_inode_info *info; 4403fd60b288SMuchun Song info = alloc_inode_sb(sb, shmem_inode_cachep, GFP_KERNEL); 440441ffe5d5SHugh Dickins if (!info) 44051da177e4SLinus Torvalds return NULL; 440641ffe5d5SHugh Dickins return &info->vfs_inode; 44071da177e4SLinus Torvalds } 44081da177e4SLinus Torvalds 440974b1da56SAl Viro static void shmem_free_in_core_inode(struct inode *inode) 4410fa0d7e3dSNick Piggin { 441184e710daSAl Viro if (S_ISLNK(inode->i_mode)) 44123ed47db3SAl Viro kfree(inode->i_link); 4413fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 4414fa0d7e3dSNick Piggin } 4415fa0d7e3dSNick Piggin 44161da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 44171da177e4SLinus Torvalds { 441809208d15SAl Viro if (S_ISREG(inode->i_mode)) 44191da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 4420a2e45955SChuck Lever if (S_ISDIR(inode->i_mode)) 4421a2e45955SChuck Lever simple_offset_destroy(shmem_get_offset_ctx(inode)); 44221da177e4SLinus Torvalds } 44231da177e4SLinus Torvalds 442441ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 44251da177e4SLinus Torvalds { 442641ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 442741ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 44281da177e4SLinus Torvalds } 44291da177e4SLinus Torvalds 44309a8ec03eSweiping zhang static void shmem_init_inodecache(void) 44311da177e4SLinus Torvalds { 44321da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 44331da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 44345d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 44351da177e4SLinus Torvalds } 44361da177e4SLinus Torvalds 443741ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 44381da177e4SLinus Torvalds { 44391a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 44401da177e4SLinus Torvalds } 44411da177e4SLinus Torvalds 4442a7605426SYang Shi /* Keep the page in page cache instead of truncating it */ 4443a7605426SYang Shi static int shmem_error_remove_page(struct address_space *mapping, 4444a7605426SYang Shi struct page *page) 4445a7605426SYang Shi { 4446a7605426SYang Shi return 0; 4447a7605426SYang Shi } 4448a7605426SYang Shi 444930e6a51dSHui Su const struct address_space_operations shmem_aops = { 44501da177e4SLinus Torvalds .writepage = shmem_writepage, 445146de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 44521da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 4453800d15a5SNick Piggin .write_begin = shmem_write_begin, 4454800d15a5SNick Piggin .write_end = shmem_write_end, 44551da177e4SLinus Torvalds #endif 44561c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 445754184650SMatthew Wilcox (Oracle) .migrate_folio = migrate_folio, 44581c93923cSAndrew Morton #endif 4459a7605426SYang Shi .error_remove_page = shmem_error_remove_page, 44601da177e4SLinus Torvalds }; 446130e6a51dSHui Su EXPORT_SYMBOL(shmem_aops); 44621da177e4SLinus Torvalds 446315ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 44641da177e4SLinus Torvalds .mmap = shmem_mmap, 4465*e88e0d36SHugh Dickins .open = shmem_file_open, 4466c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 44671da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 4468220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 44692ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 4470*e88e0d36SHugh Dickins .write_iter = shmem_file_write_iter, 44711b061d92SChristoph Hellwig .fsync = noop_fsync, 4472bd194b18SDavid Howells .splice_read = shmem_file_splice_read, 4473f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 447483e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 44751da177e4SLinus Torvalds #endif 44761da177e4SLinus Torvalds }; 44771da177e4SLinus Torvalds 447892e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 447944a30220SYu Zhao .getattr = shmem_getattr, 448094c1e62dSHugh Dickins .setattr = shmem_setattr, 4481b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 4482b09e0fa4SEric Paris .listxattr = shmem_listxattr, 4483feda821eSChristoph Hellwig .set_acl = simple_set_acl, 4484e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 4485e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 4486b09e0fa4SEric Paris #endif 44871da177e4SLinus Torvalds }; 44881da177e4SLinus Torvalds 448992e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 44901da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 4491f7cd16a5SXavier Roche .getattr = shmem_getattr, 44921da177e4SLinus Torvalds .create = shmem_create, 44931da177e4SLinus Torvalds .lookup = simple_lookup, 44941da177e4SLinus Torvalds .link = shmem_link, 44951da177e4SLinus Torvalds .unlink = shmem_unlink, 44961da177e4SLinus Torvalds .symlink = shmem_symlink, 44971da177e4SLinus Torvalds .mkdir = shmem_mkdir, 44981da177e4SLinus Torvalds .rmdir = shmem_rmdir, 44991da177e4SLinus Torvalds .mknod = shmem_mknod, 45002773bf00SMiklos Szeredi .rename = shmem_rename2, 450160545d0dSAl Viro .tmpfile = shmem_tmpfile, 4502a2e45955SChuck Lever .get_offset_ctx = shmem_get_offset_ctx, 45031da177e4SLinus Torvalds #endif 4504b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 4505b09e0fa4SEric Paris .listxattr = shmem_listxattr, 4506e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 4507e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 4508b09e0fa4SEric Paris #endif 450939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 451094c1e62dSHugh Dickins .setattr = shmem_setattr, 4511feda821eSChristoph Hellwig .set_acl = simple_set_acl, 451239f0247dSAndreas Gruenbacher #endif 451339f0247dSAndreas Gruenbacher }; 451439f0247dSAndreas Gruenbacher 451592e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 4516f7cd16a5SXavier Roche .getattr = shmem_getattr, 4517b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 4518b09e0fa4SEric Paris .listxattr = shmem_listxattr, 4519b09e0fa4SEric Paris #endif 452039f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 452194c1e62dSHugh Dickins .setattr = shmem_setattr, 4522feda821eSChristoph Hellwig .set_acl = simple_set_acl, 452339f0247dSAndreas Gruenbacher #endif 45241da177e4SLinus Torvalds }; 45251da177e4SLinus Torvalds 4526759b9775SHugh Dickins static const struct super_operations shmem_ops = { 45271da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 452874b1da56SAl Viro .free_inode = shmem_free_in_core_inode, 45291da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 45301da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 45311da177e4SLinus Torvalds .statfs = shmem_statfs, 4532680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 45331da177e4SLinus Torvalds #endif 4534e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 4535e09764cfSCarlos Maiolino .get_dquots = shmem_get_dquots, 4536e09764cfSCarlos Maiolino #endif 45371f895f75SAl Viro .evict_inode = shmem_evict_inode, 45381da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 45391da177e4SLinus Torvalds .put_super = shmem_put_super, 4540396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4541779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 4542779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 4543779750d2SKirill A. Shutemov #endif 45441da177e4SLinus Torvalds }; 45451da177e4SLinus Torvalds 4546f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 454754cb8821SNick Piggin .fault = shmem_fault, 4548d7c17551SNing Qu .map_pages = filemap_map_pages, 45491da177e4SLinus Torvalds #ifdef CONFIG_NUMA 45501da177e4SLinus Torvalds .set_policy = shmem_set_policy, 45511da177e4SLinus Torvalds .get_policy = shmem_get_policy, 45521da177e4SLinus Torvalds #endif 45531da177e4SLinus Torvalds }; 45541da177e4SLinus Torvalds 4555d09e8ca6SPasha Tatashin static const struct vm_operations_struct shmem_anon_vm_ops = { 4556d09e8ca6SPasha Tatashin .fault = shmem_fault, 4557d09e8ca6SPasha Tatashin .map_pages = filemap_map_pages, 4558d09e8ca6SPasha Tatashin #ifdef CONFIG_NUMA 4559d09e8ca6SPasha Tatashin .set_policy = shmem_set_policy, 4560d09e8ca6SPasha Tatashin .get_policy = shmem_get_policy, 4561d09e8ca6SPasha Tatashin #endif 4562d09e8ca6SPasha Tatashin }; 4563d09e8ca6SPasha Tatashin 4564f3235626SDavid Howells int shmem_init_fs_context(struct fs_context *fc) 45651da177e4SLinus Torvalds { 4566f3235626SDavid Howells struct shmem_options *ctx; 4567f3235626SDavid Howells 4568f3235626SDavid Howells ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL); 4569f3235626SDavid Howells if (!ctx) 4570f3235626SDavid Howells return -ENOMEM; 4571f3235626SDavid Howells 4572f3235626SDavid Howells ctx->mode = 0777 | S_ISVTX; 4573f3235626SDavid Howells ctx->uid = current_fsuid(); 4574f3235626SDavid Howells ctx->gid = current_fsgid(); 4575f3235626SDavid Howells 4576f3235626SDavid Howells fc->fs_private = ctx; 4577f3235626SDavid Howells fc->ops = &shmem_fs_context_ops; 4578f3235626SDavid Howells return 0; 45791da177e4SLinus Torvalds } 45801da177e4SLinus Torvalds 458141ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 45821da177e4SLinus Torvalds .owner = THIS_MODULE, 45831da177e4SLinus Torvalds .name = "tmpfs", 4584f3235626SDavid Howells .init_fs_context = shmem_init_fs_context, 4585f3235626SDavid Howells #ifdef CONFIG_TMPFS 4586d7167b14SAl Viro .parameters = shmem_fs_parameters, 4587f3235626SDavid Howells #endif 45881da177e4SLinus Torvalds .kill_sb = kill_litter_super, 45897a80e5b8SGiuseppe Scrivano #ifdef CONFIG_SHMEM 45907a80e5b8SGiuseppe Scrivano .fs_flags = FS_USERNS_MOUNT | FS_ALLOW_IDMAP, 45917a80e5b8SGiuseppe Scrivano #else 4592ff36da69SMatthew Wilcox (Oracle) .fs_flags = FS_USERNS_MOUNT, 45937a80e5b8SGiuseppe Scrivano #endif 45941da177e4SLinus Torvalds }; 45951da177e4SLinus Torvalds 45969096bbe9SMiaohe Lin void __init shmem_init(void) 45971da177e4SLinus Torvalds { 45981da177e4SLinus Torvalds int error; 45991da177e4SLinus Torvalds 46009a8ec03eSweiping zhang shmem_init_inodecache(); 46011da177e4SLinus Torvalds 4602e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 4603e09764cfSCarlos Maiolino error = register_quota_format(&shmem_quota_format); 4604e09764cfSCarlos Maiolino if (error < 0) { 4605e09764cfSCarlos Maiolino pr_err("Could not register quota format\n"); 4606e09764cfSCarlos Maiolino goto out3; 4607e09764cfSCarlos Maiolino } 4608e09764cfSCarlos Maiolino #endif 4609e09764cfSCarlos Maiolino 461041ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 46111da177e4SLinus Torvalds if (error) { 46121170532bSJoe Perches pr_err("Could not register tmpfs\n"); 46131da177e4SLinus Torvalds goto out2; 46141da177e4SLinus Torvalds } 461595dc112aSGreg Kroah-Hartman 4616ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 46171da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 46181da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 46191170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 46201da177e4SLinus Torvalds goto out1; 46211da177e4SLinus Torvalds } 46225a6e75f8SKirill A. Shutemov 4623396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4624435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 46255a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 46265a6e75f8SKirill A. Shutemov else 46275e6e5a12SHugh Dickins shmem_huge = SHMEM_HUGE_NEVER; /* just in case it was patched */ 46285a6e75f8SKirill A. Shutemov #endif 46299096bbe9SMiaohe Lin return; 46301da177e4SLinus Torvalds 46311da177e4SLinus Torvalds out1: 463241ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 46331da177e4SLinus Torvalds out2: 4634e09764cfSCarlos Maiolino #ifdef CONFIG_TMPFS_QUOTA 4635e09764cfSCarlos Maiolino unregister_quota_format(&shmem_quota_format); 4636e09764cfSCarlos Maiolino out3: 4637e09764cfSCarlos Maiolino #endif 463841ffe5d5SHugh Dickins shmem_destroy_inodecache(); 46391da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 46401da177e4SLinus Torvalds } 4641853ac43aSMatt Mackall 4642396bcc52SMatthew Wilcox (Oracle) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS) 46435a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 46445a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 46455a6e75f8SKirill A. Shutemov { 464626083eb6SColin Ian King static const int values[] = { 46475a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 46485a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 46495a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 46505a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 46515a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 46525a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 46535a6e75f8SKirill A. Shutemov }; 465479d4d38aSJoe Perches int len = 0; 465579d4d38aSJoe Perches int i; 46565a6e75f8SKirill A. Shutemov 465779d4d38aSJoe Perches for (i = 0; i < ARRAY_SIZE(values); i++) { 465879d4d38aSJoe Perches len += sysfs_emit_at(buf, len, 465979d4d38aSJoe Perches shmem_huge == values[i] ? "%s[%s]" : "%s%s", 466079d4d38aSJoe Perches i ? " " : "", 46615a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 46625a6e75f8SKirill A. Shutemov } 466379d4d38aSJoe Perches 466479d4d38aSJoe Perches len += sysfs_emit_at(buf, len, "\n"); 466579d4d38aSJoe Perches 466679d4d38aSJoe Perches return len; 46675a6e75f8SKirill A. Shutemov } 46685a6e75f8SKirill A. Shutemov 46695a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 46705a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 46715a6e75f8SKirill A. Shutemov { 46725a6e75f8SKirill A. Shutemov char tmp[16]; 46735a6e75f8SKirill A. Shutemov int huge; 46745a6e75f8SKirill A. Shutemov 46755a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 46765a6e75f8SKirill A. Shutemov return -EINVAL; 46775a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 46785a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 46795a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 46805a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 46815a6e75f8SKirill A. Shutemov 46825a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 46835a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 46845a6e75f8SKirill A. Shutemov return -EINVAL; 46855a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 46865a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 46875a6e75f8SKirill A. Shutemov return -EINVAL; 46885a6e75f8SKirill A. Shutemov 46895a6e75f8SKirill A. Shutemov shmem_huge = huge; 4690435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 46915a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 46925a6e75f8SKirill A. Shutemov return count; 46935a6e75f8SKirill A. Shutemov } 46945a6e75f8SKirill A. Shutemov 46954bfa8adaSMiaohe Lin struct kobj_attribute shmem_enabled_attr = __ATTR_RW(shmem_enabled); 4696396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ 4697f3f0e1d2SKirill A. Shutemov 4698853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 4699853ac43aSMatt Mackall 4700853ac43aSMatt Mackall /* 4701853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 4702853ac43aSMatt Mackall * 4703853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 4704853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 4705853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 4706853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 4707853ac43aSMatt Mackall */ 4708853ac43aSMatt Mackall 470941ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 4710853ac43aSMatt Mackall .name = "tmpfs", 4711f3235626SDavid Howells .init_fs_context = ramfs_init_fs_context, 4712d7167b14SAl Viro .parameters = ramfs_fs_parameters, 471336ce9d76SRoberto Sassu .kill_sb = ramfs_kill_sb, 47142b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 4715853ac43aSMatt Mackall }; 4716853ac43aSMatt Mackall 47179096bbe9SMiaohe Lin void __init shmem_init(void) 4718853ac43aSMatt Mackall { 471941ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 4720853ac43aSMatt Mackall 472141ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 4722853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 4723853ac43aSMatt Mackall } 4724853ac43aSMatt Mackall 472510a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 4726853ac43aSMatt Mackall { 4727853ac43aSMatt Mackall return 0; 4728853ac43aSMatt Mackall } 4729853ac43aSMatt Mackall 4730d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 47313f96b79aSHugh Dickins { 47323f96b79aSHugh Dickins return 0; 47333f96b79aSHugh Dickins } 47343f96b79aSHugh Dickins 473524513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 473624513264SHugh Dickins { 473724513264SHugh Dickins } 473824513264SHugh Dickins 4739c01d5b30SHugh Dickins #ifdef CONFIG_MMU 4740c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 4741c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 4742c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 4743c01d5b30SHugh Dickins { 4744c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 4745c01d5b30SHugh Dickins } 4746c01d5b30SHugh Dickins #endif 4747c01d5b30SHugh Dickins 474841ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 474994c1e62dSHugh Dickins { 475041ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 475194c1e62dSHugh Dickins } 475294c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 475394c1e62dSHugh Dickins 4754853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 4755d09e8ca6SPasha Tatashin #define shmem_anon_vm_ops generic_file_vm_ops 47560b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 47570b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 47580b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 4759853ac43aSMatt Mackall 476071480663SCarlos Maiolino static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block *sb, struct inode *dir, 476171480663SCarlos Maiolino umode_t mode, dev_t dev, unsigned long flags) 476271480663SCarlos Maiolino { 476371480663SCarlos Maiolino struct inode *inode = ramfs_get_inode(sb, dir, mode, dev); 476471480663SCarlos Maiolino return inode ? inode : ERR_PTR(-ENOSPC); 476571480663SCarlos Maiolino } 476671480663SCarlos Maiolino 4767853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 4768853ac43aSMatt Mackall 4769853ac43aSMatt Mackall /* common code */ 47701da177e4SLinus Torvalds 4771703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 4772c7277090SEric Paris unsigned long flags, unsigned int i_flags) 47731da177e4SLinus Torvalds { 47741da177e4SLinus Torvalds struct inode *inode; 477593dec2daSAl Viro struct file *res; 47761da177e4SLinus Torvalds 4777703321b6SMatthew Auld if (IS_ERR(mnt)) 4778703321b6SMatthew Auld return ERR_CAST(mnt); 47791da177e4SLinus Torvalds 4780285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 47811da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 47821da177e4SLinus Torvalds 47831da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 47841da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 47851da177e4SLinus Torvalds 47867a80e5b8SGiuseppe Scrivano if (is_idmapped_mnt(mnt)) 47877a80e5b8SGiuseppe Scrivano return ERR_PTR(-EINVAL); 47887a80e5b8SGiuseppe Scrivano 47897a80e5b8SGiuseppe Scrivano inode = shmem_get_inode(&nop_mnt_idmap, mnt->mnt_sb, NULL, 47907a80e5b8SGiuseppe Scrivano S_IFREG | S_IRWXUGO, 0, flags); 479171480663SCarlos Maiolino 479271480663SCarlos Maiolino if (IS_ERR(inode)) { 4793dac2d1f6SAl Viro shmem_unacct_size(flags, size); 479471480663SCarlos Maiolino return ERR_CAST(inode); 4795dac2d1f6SAl Viro } 4796c7277090SEric Paris inode->i_flags |= i_flags; 47971da177e4SLinus Torvalds inode->i_size = size; 47986d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 479926567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 480093dec2daSAl Viro if (!IS_ERR(res)) 480193dec2daSAl Viro res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 48024b42af81SAl Viro &shmem_file_operations); 48036b4d0b27SAl Viro if (IS_ERR(res)) 480493dec2daSAl Viro iput(inode); 48056b4d0b27SAl Viro return res; 48061da177e4SLinus Torvalds } 4807c7277090SEric Paris 4808c7277090SEric Paris /** 4809c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 4810c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 4811c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 4812e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 4813e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 4814c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4815c7277090SEric Paris * @size: size to be set for the file 4816c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4817c7277090SEric Paris */ 4818c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 4819c7277090SEric Paris { 4820703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 4821c7277090SEric Paris } 4822c7277090SEric Paris 4823c7277090SEric Paris /** 4824c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 4825c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4826c7277090SEric Paris * @size: size to be set for the file 4827c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4828c7277090SEric Paris */ 4829c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 4830c7277090SEric Paris { 4831703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 4832c7277090SEric Paris } 4833395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 48341da177e4SLinus Torvalds 483546711810SRandy Dunlap /** 4836703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 4837703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 4838703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 4839703321b6SMatthew Auld * @size: size to be set for the file 4840703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4841703321b6SMatthew Auld */ 4842703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 4843703321b6SMatthew Auld loff_t size, unsigned long flags) 4844703321b6SMatthew Auld { 4845703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 4846703321b6SMatthew Auld } 4847703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 4848703321b6SMatthew Auld 4849703321b6SMatthew Auld /** 48501da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 485145e55300SPeter Collingbourne * @vma: the vma to be mmapped is prepared by do_mmap 48521da177e4SLinus Torvalds */ 48531da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 48541da177e4SLinus Torvalds { 48551da177e4SLinus Torvalds struct file *file; 48561da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 48571da177e4SLinus Torvalds 485866fc1303SHugh Dickins /* 4859c1e8d7c6SMichel Lespinasse * Cloning a new file under mmap_lock leads to a lock ordering conflict 486066fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 486166fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 486266fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 486366fc1303SHugh Dickins */ 4864703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 48651da177e4SLinus Torvalds if (IS_ERR(file)) 48661da177e4SLinus Torvalds return PTR_ERR(file); 48671da177e4SLinus Torvalds 48681da177e4SLinus Torvalds if (vma->vm_file) 48691da177e4SLinus Torvalds fput(vma->vm_file); 48701da177e4SLinus Torvalds vma->vm_file = file; 4871d09e8ca6SPasha Tatashin vma->vm_ops = &shmem_anon_vm_ops; 4872f3f0e1d2SKirill A. Shutemov 48731da177e4SLinus Torvalds return 0; 48741da177e4SLinus Torvalds } 4875d9d90e5eSHugh Dickins 4876d9d90e5eSHugh Dickins /** 4877f01b2b3eSMatthew Wilcox (Oracle) * shmem_read_folio_gfp - read into page cache, using specified page allocation flags. 4878f01b2b3eSMatthew Wilcox (Oracle) * @mapping: the folio's address_space 4879f01b2b3eSMatthew Wilcox (Oracle) * @index: the folio index 4880d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4881d9d90e5eSHugh Dickins * 4882d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4883d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 48847e0a1265SMatthew Wilcox (Oracle) * But read_cache_page_gfp() uses the ->read_folio() method: which does not 4885d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4886d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4887d9d90e5eSHugh Dickins * 488868da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 488968da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4890d9d90e5eSHugh Dickins */ 4891f01b2b3eSMatthew Wilcox (Oracle) struct folio *shmem_read_folio_gfp(struct address_space *mapping, 4892d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4893d9d90e5eSHugh Dickins { 489468da9f05SHugh Dickins #ifdef CONFIG_SHMEM 489568da9f05SHugh Dickins struct inode *inode = mapping->host; 4896a3a9c397SMatthew Wilcox (Oracle) struct folio *folio; 489768da9f05SHugh Dickins int error; 489868da9f05SHugh Dickins 489930e6a51dSHui Su BUG_ON(!shmem_mapping(mapping)); 4900a3a9c397SMatthew Wilcox (Oracle) error = shmem_get_folio_gfp(inode, index, &folio, SGP_CACHE, 4901cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 490268da9f05SHugh Dickins if (error) 4903a7605426SYang Shi return ERR_PTR(error); 4904a7605426SYang Shi 4905a3a9c397SMatthew Wilcox (Oracle) folio_unlock(folio); 4906f01b2b3eSMatthew Wilcox (Oracle) return folio; 4907f01b2b3eSMatthew Wilcox (Oracle) #else 4908f01b2b3eSMatthew Wilcox (Oracle) /* 4909f01b2b3eSMatthew Wilcox (Oracle) * The tiny !SHMEM case uses ramfs without swap 4910f01b2b3eSMatthew Wilcox (Oracle) */ 4911f01b2b3eSMatthew Wilcox (Oracle) return mapping_read_folio_gfp(mapping, index, gfp); 4912f01b2b3eSMatthew Wilcox (Oracle) #endif 4913f01b2b3eSMatthew Wilcox (Oracle) } 4914f01b2b3eSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(shmem_read_folio_gfp); 4915f01b2b3eSMatthew Wilcox (Oracle) 4916f01b2b3eSMatthew Wilcox (Oracle) struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4917f01b2b3eSMatthew Wilcox (Oracle) pgoff_t index, gfp_t gfp) 4918f01b2b3eSMatthew Wilcox (Oracle) { 4919f01b2b3eSMatthew Wilcox (Oracle) struct folio *folio = shmem_read_folio_gfp(mapping, index, gfp); 4920f01b2b3eSMatthew Wilcox (Oracle) struct page *page; 4921f01b2b3eSMatthew Wilcox (Oracle) 4922f01b2b3eSMatthew Wilcox (Oracle) if (IS_ERR(folio)) 4923f01b2b3eSMatthew Wilcox (Oracle) return &folio->page; 4924f01b2b3eSMatthew Wilcox (Oracle) 4925a3a9c397SMatthew Wilcox (Oracle) page = folio_file_page(folio, index); 4926a7605426SYang Shi if (PageHWPoison(page)) { 4927a3a9c397SMatthew Wilcox (Oracle) folio_put(folio); 4928a7605426SYang Shi return ERR_PTR(-EIO); 4929a7605426SYang Shi } 4930a7605426SYang Shi 493168da9f05SHugh Dickins return page; 4932d9d90e5eSHugh Dickins } 4933d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4934