Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Resizable virtual memory filesystem for Linux. |
| 3 | * |
| 4 | * Copyright (C) 2000 Linus Torvalds. |
| 5 | * 2000 Transmeta Corp. |
| 6 | * 2000-2001 Christoph Rohland |
| 7 | * 2000-2001 SAP AG |
| 8 | * 2002 Red Hat Inc. |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 9 | * Copyright (C) 2002-2011 Hugh Dickins. |
| 10 | * Copyright (C) 2011 Google Inc. |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 11 | * Copyright (C) 2002-2005 VERITAS Software Corporation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Copyright (C) 2004 Andi Kleen, SuSE Labs |
| 13 | * |
| 14 | * Extended attribute support for tmpfs: |
| 15 | * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> |
| 16 | * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 17 | * |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 18 | * tiny-shmem: |
| 19 | * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> |
| 20 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * This file is released under the GPL. |
| 22 | */ |
| 23 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 24 | #include <linux/fs.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/vfs.h> |
| 27 | #include <linux/mount.h> |
Andrew Morton | 250297e | 2013-04-29 15:06:12 -0700 | [diff] [blame] | 28 | #include <linux/ramfs.h> |
Hugh Dickins | caefba1 | 2009-04-13 14:40:12 -0700 | [diff] [blame] | 29 | #include <linux/pagemap.h> |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 30 | #include <linux/file.h> |
| 31 | #include <linux/mm.h> |
Arnd Bergmann | 46c9a94 | 2018-08-17 15:45:09 -0700 | [diff] [blame] | 32 | #include <linux/random.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 33 | #include <linux/sched/signal.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 34 | #include <linux/export.h> |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 35 | #include <linux/swap.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 36 | #include <linux/uio.h> |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 37 | #include <linux/khugepaged.h> |
Mike Kravetz | 749df87 | 2017-09-06 16:24:16 -0700 | [diff] [blame] | 38 | #include <linux/hugetlb.h> |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 39 | |
Andrea Arcangeli | 95cc09d | 2017-02-22 15:43:31 -0800 | [diff] [blame] | 40 | #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ |
| 41 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 42 | static struct vfsmount *shm_mnt; |
| 43 | |
| 44 | #ifdef CONFIG_SHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * This virtual memory filesystem is heavily based on the ramfs. It |
| 47 | * extends ramfs by the ability to use swap and honor resource limits |
| 48 | * which makes it a completely usable filesystem. |
| 49 | */ |
| 50 | |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 51 | #include <linux/xattr.h> |
Christoph Hellwig | a569425 | 2007-07-17 04:04:28 -0700 | [diff] [blame] | 52 | #include <linux/exportfs.h> |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 53 | #include <linux/posix_acl.h> |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 54 | #include <linux/posix_acl_xattr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/string.h> |
| 57 | #include <linux/slab.h> |
| 58 | #include <linux/backing-dev.h> |
| 59 | #include <linux/shmem_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 62 | #include <linux/pagevec.h> |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 63 | #include <linux/percpu_counter.h> |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 64 | #include <linux/falloc.h> |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 65 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include <linux/security.h> |
| 67 | #include <linux/swapops.h> |
| 68 | #include <linux/mempolicy.h> |
| 69 | #include <linux/namei.h> |
Hugh Dickins | b00dc3a | 2006-02-21 23:49:47 +0000 | [diff] [blame] | 70 | #include <linux/ctype.h> |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 71 | #include <linux/migrate.h> |
Christoph Lameter | c1f60a5 | 2006-09-25 23:31:11 -0700 | [diff] [blame] | 72 | #include <linux/highmem.h> |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 73 | #include <linux/seq_file.h> |
Mimi Zohar | 9256292 | 2008-10-07 14:00:12 -0400 | [diff] [blame] | 74 | #include <linux/magic.h> |
David Herrmann | 9183df2 | 2014-08-08 14:25:29 -0700 | [diff] [blame] | 75 | #include <linux/syscalls.h> |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 76 | #include <linux/fcntl.h> |
David Herrmann | 9183df2 | 2014-08-08 14:25:29 -0700 | [diff] [blame] | 77 | #include <uapi/linux/memfd.h> |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 78 | #include <linux/userfaultfd_k.h> |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 79 | #include <linux/rmap.h> |
Amir Goldstein | 2b4db79 | 2017-05-18 15:29:33 +0300 | [diff] [blame] | 80 | #include <linux/uuid.h> |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 81 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 82 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <asm/pgtable.h> |
| 84 | |
Mel Gorman | dd56b04 | 2015-11-06 16:28:43 -0800 | [diff] [blame] | 85 | #include "internal.h" |
| 86 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 87 | #define BLOCKS_PER_PAGE (PAGE_SIZE/512) |
| 88 | #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* Pretend that each entry is of this size in directory's i_size */ |
| 91 | #define BOGO_DIRENT_SIZE 20 |
| 92 | |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 93 | /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ |
| 94 | #define SHORT_SYMLINK_LEN 128 |
| 95 | |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 96 | /* |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 97 | * shmem_fallocate communicates with shmem_fault or shmem_writepage via |
| 98 | * inode->i_private (with i_mutex making sure that it has only one user at |
| 99 | * a time): we would prefer not to enlarge the shmem inode just for that. |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 100 | */ |
| 101 | struct shmem_falloc { |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 102 | wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 103 | pgoff_t start; /* start of range currently being fallocated */ |
| 104 | pgoff_t next; /* the next page offset to be fallocated */ |
| 105 | pgoff_t nr_falloced; /* how many new pages have been fallocated */ |
| 106 | pgoff_t nr_unswapped; /* how often writepage refused to swap out */ |
| 107 | }; |
| 108 | |
Andrew Morton | b76db735 | 2008-02-08 04:21:49 -0800 | [diff] [blame] | 109 | #ifdef CONFIG_TMPFS |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 110 | static unsigned long shmem_default_max_blocks(void) |
| 111 | { |
| 112 | return totalram_pages / 2; |
| 113 | } |
| 114 | |
| 115 | static unsigned long shmem_default_max_inodes(void) |
| 116 | { |
| 117 | return min(totalram_pages - totalhigh_pages, totalram_pages / 2); |
| 118 | } |
Andrew Morton | b76db735 | 2008-02-08 04:21:49 -0800 | [diff] [blame] | 119 | #endif |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 120 | |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 121 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp); |
| 122 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, |
| 123 | struct shmem_inode_info *info, pgoff_t index); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 124 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 125 | struct page **pagep, enum sgp_type sgp, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 126 | gfp_t gfp, struct vm_area_struct *vma, |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 127 | struct vm_fault *vmf, vm_fault_t *fault_type); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 128 | |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 129 | int shmem_getpage(struct inode *inode, pgoff_t index, |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 130 | struct page **pagep, enum sgp_type sgp) |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 131 | { |
| 132 | return shmem_getpage_gfp(inode, index, pagep, sgp, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 133 | mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) |
| 137 | { |
| 138 | return sb->s_fs_info; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * shmem_file_setup pre-accounts the whole fixed size of a VM object, |
| 143 | * for shared memory and for shared anonymous (/dev/zero) mappings |
| 144 | * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), |
| 145 | * consistent with the pre-accounting of private mappings ... |
| 146 | */ |
| 147 | static inline int shmem_acct_size(unsigned long flags, loff_t size) |
| 148 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 149 | return (flags & VM_NORESERVE) ? |
Al Viro | 191c542 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 150 | 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static inline void shmem_unacct_size(unsigned long flags, loff_t size) |
| 154 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 155 | if (!(flags & VM_NORESERVE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | vm_unacct_memory(VM_ACCT(size)); |
| 157 | } |
| 158 | |
Konstantin Khlebnikov | 7714251 | 2014-08-06 16:06:34 -0700 | [diff] [blame] | 159 | static inline int shmem_reacct_size(unsigned long flags, |
| 160 | loff_t oldsize, loff_t newsize) |
| 161 | { |
| 162 | if (!(flags & VM_NORESERVE)) { |
| 163 | if (VM_ACCT(newsize) > VM_ACCT(oldsize)) |
| 164 | return security_vm_enough_memory_mm(current->mm, |
| 165 | VM_ACCT(newsize) - VM_ACCT(oldsize)); |
| 166 | else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) |
| 167 | vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | /* |
| 173 | * ... whereas tmpfs objects are accounted incrementally as |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 174 | * pages are allocated, in order to allow large sparse files. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, |
| 176 | * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. |
| 177 | */ |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 178 | static inline int shmem_acct_block(unsigned long flags, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 180 | if (!(flags & VM_NORESERVE)) |
| 181 | return 0; |
| 182 | |
| 183 | return security_vm_enough_memory_mm(current->mm, |
| 184 | pages * VM_ACCT(PAGE_SIZE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static inline void shmem_unacct_blocks(unsigned long flags, long pages) |
| 188 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 189 | if (flags & VM_NORESERVE) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 190 | vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 193 | static inline bool shmem_inode_acct_block(struct inode *inode, long pages) |
| 194 | { |
| 195 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 196 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
| 197 | |
| 198 | if (shmem_acct_block(info->flags, pages)) |
| 199 | return false; |
| 200 | |
| 201 | if (sbinfo->max_blocks) { |
| 202 | if (percpu_counter_compare(&sbinfo->used_blocks, |
| 203 | sbinfo->max_blocks - pages) > 0) |
| 204 | goto unacct; |
| 205 | percpu_counter_add(&sbinfo->used_blocks, pages); |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | |
| 210 | unacct: |
| 211 | shmem_unacct_blocks(info->flags, pages); |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) |
| 216 | { |
| 217 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 218 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
| 219 | |
| 220 | if (sbinfo->max_blocks) |
| 221 | percpu_counter_sub(&sbinfo->used_blocks, pages); |
| 222 | shmem_unacct_blocks(info->flags, pages); |
| 223 | } |
| 224 | |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 225 | static const struct super_operations shmem_ops; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 226 | static const struct address_space_operations shmem_aops; |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 227 | static const struct file_operations shmem_file_operations; |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 228 | static const struct inode_operations shmem_inode_operations; |
| 229 | static const struct inode_operations shmem_dir_inode_operations; |
| 230 | static const struct inode_operations shmem_special_inode_operations; |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 231 | static const struct vm_operations_struct shmem_vm_ops; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 232 | static struct file_system_type shmem_fs_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Mike Rapoport | b0506e4 | 2017-02-22 15:43:28 -0800 | [diff] [blame] | 234 | bool vma_is_shmem(struct vm_area_struct *vma) |
| 235 | { |
| 236 | return vma->vm_ops == &shmem_vm_ops; |
| 237 | } |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | static LIST_HEAD(shmem_swaplist); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 240 | static DEFINE_MUTEX(shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 242 | static int shmem_reserve_inode(struct super_block *sb) |
| 243 | { |
| 244 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 245 | if (sbinfo->max_inodes) { |
| 246 | spin_lock(&sbinfo->stat_lock); |
| 247 | if (!sbinfo->free_inodes) { |
| 248 | spin_unlock(&sbinfo->stat_lock); |
| 249 | return -ENOSPC; |
| 250 | } |
| 251 | sbinfo->free_inodes--; |
| 252 | spin_unlock(&sbinfo->stat_lock); |
| 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static void shmem_free_inode(struct super_block *sb) |
| 258 | { |
| 259 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 260 | if (sbinfo->max_inodes) { |
| 261 | spin_lock(&sbinfo->stat_lock); |
| 262 | sbinfo->free_inodes++; |
| 263 | spin_unlock(&sbinfo->stat_lock); |
| 264 | } |
| 265 | } |
| 266 | |
Randy Dunlap | 4671181 | 2008-03-19 17:00:41 -0700 | [diff] [blame] | 267 | /** |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 268 | * shmem_recalc_inode - recalculate the block usage of an inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | * @inode: inode to recalc |
| 270 | * |
| 271 | * We have to calculate the free blocks since the mm can drop |
| 272 | * undirtied hole pages behind our back. |
| 273 | * |
| 274 | * But normally info->alloced == inode->i_mapping->nrpages + info->swapped |
| 275 | * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) |
| 276 | * |
| 277 | * It has to be called with the spinlock held. |
| 278 | */ |
| 279 | static void shmem_recalc_inode(struct inode *inode) |
| 280 | { |
| 281 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 282 | long freed; |
| 283 | |
| 284 | freed = info->alloced - info->swapped - inode->i_mapping->nrpages; |
| 285 | if (freed > 0) { |
| 286 | info->alloced -= freed; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 287 | inode->i_blocks -= freed * BLOCKS_PER_PAGE; |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 288 | shmem_inode_unacct_blocks(inode, freed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 292 | bool shmem_charge(struct inode *inode, long pages) |
| 293 | { |
| 294 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 295 | unsigned long flags; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 296 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 297 | if (!shmem_inode_acct_block(inode, pages)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 298 | return false; |
Mike Rapoport | b1cc94a | 2017-09-06 16:22:56 -0700 | [diff] [blame] | 299 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 300 | spin_lock_irqsave(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 301 | info->alloced += pages; |
| 302 | inode->i_blocks += pages * BLOCKS_PER_PAGE; |
| 303 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 304 | spin_unlock_irqrestore(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 305 | inode->i_mapping->nrpages += pages; |
| 306 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
| 310 | void shmem_uncharge(struct inode *inode, long pages) |
| 311 | { |
| 312 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 313 | unsigned long flags; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 314 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 315 | spin_lock_irqsave(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 316 | info->alloced -= pages; |
| 317 | inode->i_blocks -= pages * BLOCKS_PER_PAGE; |
| 318 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 319 | spin_unlock_irqrestore(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 320 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 321 | shmem_inode_unacct_blocks(inode, pages); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 324 | /* |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 325 | * Replace item expected in xarray by a new item, while holding xa_lock. |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 326 | */ |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 327 | static int shmem_replace_entry(struct address_space *mapping, |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 328 | pgoff_t index, void *expected, void *replacement) |
| 329 | { |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 330 | XA_STATE(xas, &mapping->i_pages, index); |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 331 | void *item; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 332 | |
| 333 | VM_BUG_ON(!expected); |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 334 | VM_BUG_ON(!replacement); |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 335 | item = xas_load(&xas); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 336 | if (item != expected) |
| 337 | return -ENOENT; |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 338 | xas_store(&xas, replacement); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 343 | * Sometimes, before we decide whether to proceed or to fail, we must check |
| 344 | * that an entry was not already brought back from swap by a racing thread. |
| 345 | * |
| 346 | * Checking page is not enough: by the time a SwapCache page is locked, it |
| 347 | * might be reused, and again be SwapCache, using the same swap as before. |
| 348 | */ |
| 349 | static bool shmem_confirm_swap(struct address_space *mapping, |
| 350 | pgoff_t index, swp_entry_t swap) |
| 351 | { |
Matthew Wilcox | a12831b | 2017-11-22 08:34:58 -0500 | [diff] [blame] | 352 | return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 356 | * Definitions for "huge tmpfs": tmpfs mounted with the huge= option |
| 357 | * |
| 358 | * SHMEM_HUGE_NEVER: |
| 359 | * disables huge pages for the mount; |
| 360 | * SHMEM_HUGE_ALWAYS: |
| 361 | * enables huge pages for the mount; |
| 362 | * SHMEM_HUGE_WITHIN_SIZE: |
| 363 | * only allocate huge pages if the page will be fully within i_size, |
| 364 | * also respect fadvise()/madvise() hints; |
| 365 | * SHMEM_HUGE_ADVISE: |
| 366 | * only allocate huge pages if requested with fadvise()/madvise(); |
| 367 | */ |
| 368 | |
| 369 | #define SHMEM_HUGE_NEVER 0 |
| 370 | #define SHMEM_HUGE_ALWAYS 1 |
| 371 | #define SHMEM_HUGE_WITHIN_SIZE 2 |
| 372 | #define SHMEM_HUGE_ADVISE 3 |
| 373 | |
| 374 | /* |
| 375 | * Special values. |
| 376 | * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: |
| 377 | * |
| 378 | * SHMEM_HUGE_DENY: |
| 379 | * disables huge on shm_mnt and all mounts, for emergency use; |
| 380 | * SHMEM_HUGE_FORCE: |
| 381 | * enables huge on shm_mnt and all mounts, w/o needing option, for testing; |
| 382 | * |
| 383 | */ |
| 384 | #define SHMEM_HUGE_DENY (-1) |
| 385 | #define SHMEM_HUGE_FORCE (-2) |
| 386 | |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 387 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 388 | /* ifdef here to avoid bloating shmem.o when not necessary */ |
| 389 | |
Mike Kravetz | 5b9c98f | 2018-06-07 17:05:53 -0700 | [diff] [blame] | 390 | static int shmem_huge __read_mostly; |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 391 | |
Jérémy Lefaure | f1f5929 | 2016-12-12 16:43:23 -0800 | [diff] [blame] | 392 | #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 393 | static int shmem_parse_huge(const char *str) |
| 394 | { |
| 395 | if (!strcmp(str, "never")) |
| 396 | return SHMEM_HUGE_NEVER; |
| 397 | if (!strcmp(str, "always")) |
| 398 | return SHMEM_HUGE_ALWAYS; |
| 399 | if (!strcmp(str, "within_size")) |
| 400 | return SHMEM_HUGE_WITHIN_SIZE; |
| 401 | if (!strcmp(str, "advise")) |
| 402 | return SHMEM_HUGE_ADVISE; |
| 403 | if (!strcmp(str, "deny")) |
| 404 | return SHMEM_HUGE_DENY; |
| 405 | if (!strcmp(str, "force")) |
| 406 | return SHMEM_HUGE_FORCE; |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | |
| 410 | static const char *shmem_format_huge(int huge) |
| 411 | { |
| 412 | switch (huge) { |
| 413 | case SHMEM_HUGE_NEVER: |
| 414 | return "never"; |
| 415 | case SHMEM_HUGE_ALWAYS: |
| 416 | return "always"; |
| 417 | case SHMEM_HUGE_WITHIN_SIZE: |
| 418 | return "within_size"; |
| 419 | case SHMEM_HUGE_ADVISE: |
| 420 | return "advise"; |
| 421 | case SHMEM_HUGE_DENY: |
| 422 | return "deny"; |
| 423 | case SHMEM_HUGE_FORCE: |
| 424 | return "force"; |
| 425 | default: |
| 426 | VM_BUG_ON(1); |
| 427 | return "bad_val"; |
| 428 | } |
| 429 | } |
Jérémy Lefaure | f1f5929 | 2016-12-12 16:43:23 -0800 | [diff] [blame] | 430 | #endif |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 431 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 432 | static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, |
| 433 | struct shrink_control *sc, unsigned long nr_to_split) |
| 434 | { |
| 435 | LIST_HEAD(list), *pos, *next; |
Kirill A. Shutemov | 253fd0f | 2017-02-03 13:13:15 -0800 | [diff] [blame] | 436 | LIST_HEAD(to_remove); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 437 | struct inode *inode; |
| 438 | struct shmem_inode_info *info; |
| 439 | struct page *page; |
| 440 | unsigned long batch = sc ? sc->nr_to_scan : 128; |
| 441 | int removed = 0, split = 0; |
| 442 | |
| 443 | if (list_empty(&sbinfo->shrinklist)) |
| 444 | return SHRINK_STOP; |
| 445 | |
| 446 | spin_lock(&sbinfo->shrinklist_lock); |
| 447 | list_for_each_safe(pos, next, &sbinfo->shrinklist) { |
| 448 | info = list_entry(pos, struct shmem_inode_info, shrinklist); |
| 449 | |
| 450 | /* pin the inode */ |
| 451 | inode = igrab(&info->vfs_inode); |
| 452 | |
| 453 | /* inode is about to be evicted */ |
| 454 | if (!inode) { |
| 455 | list_del_init(&info->shrinklist); |
| 456 | removed++; |
| 457 | goto next; |
| 458 | } |
| 459 | |
| 460 | /* Check if there's anything to gain */ |
| 461 | if (round_up(inode->i_size, PAGE_SIZE) == |
| 462 | round_up(inode->i_size, HPAGE_PMD_SIZE)) { |
Kirill A. Shutemov | 253fd0f | 2017-02-03 13:13:15 -0800 | [diff] [blame] | 463 | list_move(&info->shrinklist, &to_remove); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 464 | removed++; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 465 | goto next; |
| 466 | } |
| 467 | |
| 468 | list_move(&info->shrinklist, &list); |
| 469 | next: |
| 470 | if (!--batch) |
| 471 | break; |
| 472 | } |
| 473 | spin_unlock(&sbinfo->shrinklist_lock); |
| 474 | |
Kirill A. Shutemov | 253fd0f | 2017-02-03 13:13:15 -0800 | [diff] [blame] | 475 | list_for_each_safe(pos, next, &to_remove) { |
| 476 | info = list_entry(pos, struct shmem_inode_info, shrinklist); |
| 477 | inode = &info->vfs_inode; |
| 478 | list_del_init(&info->shrinklist); |
| 479 | iput(inode); |
| 480 | } |
| 481 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 482 | list_for_each_safe(pos, next, &list) { |
| 483 | int ret; |
| 484 | |
| 485 | info = list_entry(pos, struct shmem_inode_info, shrinklist); |
| 486 | inode = &info->vfs_inode; |
| 487 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 488 | if (nr_to_split && split >= nr_to_split) |
| 489 | goto leave; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 490 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 491 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 492 | (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); |
| 493 | if (!page) |
| 494 | goto drop; |
| 495 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 496 | /* No huge page at the end of the file: nothing to split */ |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 497 | if (!PageTransHuge(page)) { |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 498 | put_page(page); |
| 499 | goto drop; |
| 500 | } |
| 501 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 502 | /* |
| 503 | * Leave the inode on the list if we failed to lock |
| 504 | * the page at this time. |
| 505 | * |
| 506 | * Waiting for the lock may lead to deadlock in the |
| 507 | * reclaim path. |
| 508 | */ |
| 509 | if (!trylock_page(page)) { |
| 510 | put_page(page); |
| 511 | goto leave; |
| 512 | } |
| 513 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 514 | ret = split_huge_page(page); |
| 515 | unlock_page(page); |
| 516 | put_page(page); |
| 517 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 518 | /* If split failed leave the inode on the list */ |
| 519 | if (ret) |
| 520 | goto leave; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 521 | |
| 522 | split++; |
| 523 | drop: |
| 524 | list_del_init(&info->shrinklist); |
| 525 | removed++; |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 526 | leave: |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 527 | iput(inode); |
| 528 | } |
| 529 | |
| 530 | spin_lock(&sbinfo->shrinklist_lock); |
| 531 | list_splice_tail(&list, &sbinfo->shrinklist); |
| 532 | sbinfo->shrinklist_len -= removed; |
| 533 | spin_unlock(&sbinfo->shrinklist_lock); |
| 534 | |
| 535 | return split; |
| 536 | } |
| 537 | |
| 538 | static long shmem_unused_huge_scan(struct super_block *sb, |
| 539 | struct shrink_control *sc) |
| 540 | { |
| 541 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 542 | |
| 543 | if (!READ_ONCE(sbinfo->shrinklist_len)) |
| 544 | return SHRINK_STOP; |
| 545 | |
| 546 | return shmem_unused_huge_shrink(sbinfo, sc, 0); |
| 547 | } |
| 548 | |
| 549 | static long shmem_unused_huge_count(struct super_block *sb, |
| 550 | struct shrink_control *sc) |
| 551 | { |
| 552 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 553 | return READ_ONCE(sbinfo->shrinklist_len); |
| 554 | } |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 555 | #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */ |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 556 | |
| 557 | #define shmem_huge SHMEM_HUGE_DENY |
| 558 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 559 | static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, |
| 560 | struct shrink_control *sc, unsigned long nr_to_split) |
| 561 | { |
| 562 | return 0; |
| 563 | } |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 564 | #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 565 | |
Yang Shi | 89fdcd2 | 2018-06-07 17:06:59 -0700 | [diff] [blame] | 566 | static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) |
| 567 | { |
| 568 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && |
| 569 | (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) && |
| 570 | shmem_huge != SHMEM_HUGE_DENY) |
| 571 | return true; |
| 572 | return false; |
| 573 | } |
| 574 | |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 575 | /* |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 576 | * Like add_to_page_cache_locked, but error if expected item has gone. |
| 577 | */ |
| 578 | static int shmem_add_to_page_cache(struct page *page, |
| 579 | struct address_space *mapping, |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 580 | pgoff_t index, void *expected, gfp_t gfp) |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 581 | { |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 582 | XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page)); |
| 583 | unsigned long i = 0; |
| 584 | unsigned long nr = 1UL << compound_order(page); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 585 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 586 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 587 | VM_BUG_ON_PAGE(index != round_down(index, nr), page); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 588 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 589 | VM_BUG_ON_PAGE(!PageSwapBacked(page), page); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 590 | VM_BUG_ON(expected && PageTransHuge(page)); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 591 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 592 | page_ref_add(page, nr); |
Hugh Dickins | b065b43 | 2012-07-11 14:02:48 -0700 | [diff] [blame] | 593 | page->mapping = mapping; |
| 594 | page->index = index; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 595 | |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 596 | do { |
| 597 | void *entry; |
| 598 | xas_lock_irq(&xas); |
| 599 | entry = xas_find_conflict(&xas); |
| 600 | if (entry != expected) |
| 601 | xas_set_err(&xas, -EEXIST); |
| 602 | xas_create_range(&xas); |
| 603 | if (xas_error(&xas)) |
| 604 | goto unlock; |
| 605 | next: |
| 606 | xas_store(&xas, page + i); |
| 607 | if (++i < nr) { |
| 608 | xas_next(&xas); |
| 609 | goto next; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 610 | } |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 611 | if (PageTransHuge(page)) { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 612 | count_vm_event(THP_FILE_ALLOC); |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 613 | __inc_node_page_state(page, NR_SHMEM_THPS); |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 614 | } |
| 615 | mapping->nrpages += nr; |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 616 | __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); |
| 617 | __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 618 | unlock: |
| 619 | xas_unlock_irq(&xas); |
| 620 | } while (xas_nomem(&xas, gfp)); |
| 621 | |
| 622 | if (xas_error(&xas)) { |
Hugh Dickins | b065b43 | 2012-07-11 14:02:48 -0700 | [diff] [blame] | 623 | page->mapping = NULL; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 624 | page_ref_sub(page, nr); |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 625 | return xas_error(&xas); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 626 | } |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 627 | |
| 628 | return 0; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 632 | * Like delete_from_page_cache, but substitutes swap for page. |
| 633 | */ |
| 634 | static void shmem_delete_from_page_cache(struct page *page, void *radswap) |
| 635 | { |
| 636 | struct address_space *mapping = page->mapping; |
| 637 | int error; |
| 638 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 639 | VM_BUG_ON_PAGE(PageCompound(page), page); |
| 640 | |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 641 | xa_lock_irq(&mapping->i_pages); |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 642 | error = shmem_replace_entry(mapping, page->index, page, radswap); |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 643 | page->mapping = NULL; |
| 644 | mapping->nrpages--; |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 645 | __dec_node_page_state(page, NR_FILE_PAGES); |
| 646 | __dec_node_page_state(page, NR_SHMEM); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 647 | xa_unlock_irq(&mapping->i_pages); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 648 | put_page(page); |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 649 | BUG_ON(error); |
| 650 | } |
| 651 | |
| 652 | /* |
Matthew Wilcox | c121d3b | 2017-12-04 03:13:54 -0500 | [diff] [blame] | 653 | * Remove swap entry from page cache, free the swap and its page cache. |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 654 | */ |
| 655 | static int shmem_free_swap(struct address_space *mapping, |
| 656 | pgoff_t index, void *radswap) |
| 657 | { |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 658 | void *old; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 659 | |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 660 | xa_lock_irq(&mapping->i_pages); |
Matthew Wilcox | c121d3b | 2017-12-04 03:13:54 -0500 | [diff] [blame] | 661 | old = __xa_cmpxchg(&mapping->i_pages, index, radswap, NULL, 0); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 662 | xa_unlock_irq(&mapping->i_pages); |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 663 | if (old != radswap) |
| 664 | return -ENOENT; |
| 665 | free_swap_and_cache(radix_to_swp_entry(radswap)); |
| 666 | return 0; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 670 | * Determine (in bytes) how many of the shmem object's pages mapped by the |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 671 | * given offsets are swapped out. |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 672 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 673 | * This is safe to call without i_mutex or the i_pages lock thanks to RCU, |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 674 | * as long as the inode doesn't go away and racy results are not a problem. |
| 675 | */ |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 676 | unsigned long shmem_partial_swap_usage(struct address_space *mapping, |
| 677 | pgoff_t start, pgoff_t end) |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 678 | { |
Matthew Wilcox | 7ae3424 | 2017-12-04 03:28:00 -0500 | [diff] [blame] | 679 | XA_STATE(xas, &mapping->i_pages, start); |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 680 | struct page *page; |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 681 | unsigned long swapped = 0; |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 682 | |
| 683 | rcu_read_lock(); |
Matthew Wilcox | 7ae3424 | 2017-12-04 03:28:00 -0500 | [diff] [blame] | 684 | xas_for_each(&xas, page, end - 1) { |
| 685 | if (xas_retry(&xas, page)) |
Matthew Wilcox | 2cf938a | 2016-03-17 14:22:03 -0700 | [diff] [blame] | 686 | continue; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 687 | if (xa_is_value(page)) |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 688 | swapped++; |
| 689 | |
| 690 | if (need_resched()) { |
Matthew Wilcox | 7ae3424 | 2017-12-04 03:28:00 -0500 | [diff] [blame] | 691 | xas_pause(&xas); |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 692 | cond_resched_rcu(); |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | rcu_read_unlock(); |
| 697 | |
| 698 | return swapped << PAGE_SHIFT; |
| 699 | } |
| 700 | |
| 701 | /* |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 702 | * Determine (in bytes) how many of the shmem object's pages mapped by the |
| 703 | * given vma is swapped out. |
| 704 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 705 | * This is safe to call without i_mutex or the i_pages lock thanks to RCU, |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 706 | * as long as the inode doesn't go away and racy results are not a problem. |
| 707 | */ |
| 708 | unsigned long shmem_swap_usage(struct vm_area_struct *vma) |
| 709 | { |
| 710 | struct inode *inode = file_inode(vma->vm_file); |
| 711 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 712 | struct address_space *mapping = inode->i_mapping; |
| 713 | unsigned long swapped; |
| 714 | |
| 715 | /* Be careful as we don't hold info->lock */ |
| 716 | swapped = READ_ONCE(info->swapped); |
| 717 | |
| 718 | /* |
| 719 | * The easier cases are when the shmem object has nothing in swap, or |
| 720 | * the vma maps it whole. Then we can simply use the stats that we |
| 721 | * already track. |
| 722 | */ |
| 723 | if (!swapped) |
| 724 | return 0; |
| 725 | |
| 726 | if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) |
| 727 | return swapped << PAGE_SHIFT; |
| 728 | |
| 729 | /* Here comes the more involved part */ |
| 730 | return shmem_partial_swap_usage(mapping, |
| 731 | linear_page_index(vma, vma->vm_start), |
| 732 | linear_page_index(vma, vma->vm_end)); |
| 733 | } |
| 734 | |
| 735 | /* |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 736 | * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. |
| 737 | */ |
| 738 | void shmem_unlock_mapping(struct address_space *mapping) |
| 739 | { |
| 740 | struct pagevec pvec; |
| 741 | pgoff_t indices[PAGEVEC_SIZE]; |
| 742 | pgoff_t index = 0; |
| 743 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 744 | pagevec_init(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 745 | /* |
| 746 | * Minor point, but we might as well stop if someone else SHM_LOCKs it. |
| 747 | */ |
| 748 | while (!mapping_unevictable(mapping)) { |
| 749 | /* |
| 750 | * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it |
| 751 | * has finished, if it hits a row of PAGEVEC_SIZE swap entries. |
| 752 | */ |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 753 | pvec.nr = find_get_entries(mapping, index, |
| 754 | PAGEVEC_SIZE, pvec.pages, indices); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 755 | if (!pvec.nr) |
| 756 | break; |
| 757 | index = indices[pvec.nr - 1] + 1; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 758 | pagevec_remove_exceptionals(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 759 | check_move_unevictable_pages(pvec.pages, pvec.nr); |
| 760 | pagevec_release(&pvec); |
| 761 | cond_resched(); |
| 762 | } |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 766 | * Remove range of pages and swap entries from page cache, and free them. |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 767 | * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 768 | */ |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 769 | static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, |
| 770 | bool unfalloc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 772 | struct address_space *mapping = inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 774 | pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 775 | pgoff_t end = (lend + 1) >> PAGE_SHIFT; |
| 776 | unsigned int partial_start = lstart & (PAGE_SIZE - 1); |
| 777 | unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 778 | struct pagevec pvec; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 779 | pgoff_t indices[PAGEVEC_SIZE]; |
| 780 | long nr_swaps_freed = 0; |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 781 | pgoff_t index; |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 782 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 784 | if (lend == -1) |
| 785 | end = -1; /* unsigned, so actually very big */ |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 786 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 787 | pagevec_init(&pvec); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 788 | index = start; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 789 | while (index < end) { |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 790 | pvec.nr = find_get_entries(mapping, index, |
| 791 | min(end - index, (pgoff_t)PAGEVEC_SIZE), |
| 792 | pvec.pages, indices); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 793 | if (!pvec.nr) |
| 794 | break; |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 795 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 796 | struct page *page = pvec.pages[i]; |
| 797 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 798 | index = indices[i]; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 799 | if (index >= end) |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 800 | break; |
| 801 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 802 | if (xa_is_value(page)) { |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 803 | if (unfalloc) |
| 804 | continue; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 805 | nr_swaps_freed += !shmem_free_swap(mapping, |
| 806 | index, page); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 807 | continue; |
| 808 | } |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 809 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 810 | VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); |
| 811 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 812 | if (!trylock_page(page)) |
| 813 | continue; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 814 | |
| 815 | if (PageTransTail(page)) { |
| 816 | /* Middle of THP: zero out the page */ |
| 817 | clear_highpage(page); |
| 818 | unlock_page(page); |
| 819 | continue; |
| 820 | } else if (PageTransHuge(page)) { |
| 821 | if (index == round_down(end, HPAGE_PMD_NR)) { |
| 822 | /* |
| 823 | * Range ends in the middle of THP: |
| 824 | * zero out the page |
| 825 | */ |
| 826 | clear_highpage(page); |
| 827 | unlock_page(page); |
| 828 | continue; |
| 829 | } |
| 830 | index += HPAGE_PMD_NR - 1; |
| 831 | i += HPAGE_PMD_NR - 1; |
| 832 | } |
| 833 | |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 834 | if (!unfalloc || !PageUptodate(page)) { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 835 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 836 | if (page_mapping(page) == mapping) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 837 | VM_BUG_ON_PAGE(PageWriteback(page), page); |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 838 | truncate_inode_page(mapping, page); |
| 839 | } |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 840 | } |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 841 | unlock_page(page); |
| 842 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 843 | pagevec_remove_exceptionals(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 844 | pagevec_release(&pvec); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 845 | cond_resched(); |
| 846 | index++; |
| 847 | } |
| 848 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 849 | if (partial_start) { |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 850 | struct page *page = NULL; |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 851 | shmem_getpage(inode, start - 1, &page, SGP_READ); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 852 | if (page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 853 | unsigned int top = PAGE_SIZE; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 854 | if (start > end) { |
| 855 | top = partial_end; |
| 856 | partial_end = 0; |
| 857 | } |
| 858 | zero_user_segment(page, partial_start, top); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 859 | set_page_dirty(page); |
| 860 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 861 | put_page(page); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 864 | if (partial_end) { |
| 865 | struct page *page = NULL; |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 866 | shmem_getpage(inode, end, &page, SGP_READ); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 867 | if (page) { |
| 868 | zero_user_segment(page, 0, partial_end); |
| 869 | set_page_dirty(page); |
| 870 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 871 | put_page(page); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | if (start >= end) |
| 875 | return; |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 876 | |
| 877 | index = start; |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 878 | while (index < end) { |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 879 | cond_resched(); |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 880 | |
| 881 | pvec.nr = find_get_entries(mapping, index, |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 882 | min(end - index, (pgoff_t)PAGEVEC_SIZE), |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 883 | pvec.pages, indices); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 884 | if (!pvec.nr) { |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 885 | /* If all gone or hole-punch or unfalloc, we're done */ |
| 886 | if (index == start || end != -1) |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 887 | break; |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 888 | /* But if truncating, restart to make sure all gone */ |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 889 | index = start; |
| 890 | continue; |
| 891 | } |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 892 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 893 | struct page *page = pvec.pages[i]; |
| 894 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 895 | index = indices[i]; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 896 | if (index >= end) |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 897 | break; |
| 898 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 899 | if (xa_is_value(page)) { |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 900 | if (unfalloc) |
| 901 | continue; |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 902 | if (shmem_free_swap(mapping, index, page)) { |
| 903 | /* Swap was replaced by page: retry */ |
| 904 | index--; |
| 905 | break; |
| 906 | } |
| 907 | nr_swaps_freed++; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 908 | continue; |
| 909 | } |
| 910 | |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 911 | lock_page(page); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 912 | |
| 913 | if (PageTransTail(page)) { |
| 914 | /* Middle of THP: zero out the page */ |
| 915 | clear_highpage(page); |
| 916 | unlock_page(page); |
| 917 | /* |
| 918 | * Partial thp truncate due 'start' in middle |
| 919 | * of THP: don't need to look on these pages |
| 920 | * again on !pvec.nr restart. |
| 921 | */ |
| 922 | if (index != round_down(end, HPAGE_PMD_NR)) |
| 923 | start++; |
| 924 | continue; |
| 925 | } else if (PageTransHuge(page)) { |
| 926 | if (index == round_down(end, HPAGE_PMD_NR)) { |
| 927 | /* |
| 928 | * Range ends in the middle of THP: |
| 929 | * zero out the page |
| 930 | */ |
| 931 | clear_highpage(page); |
| 932 | unlock_page(page); |
| 933 | continue; |
| 934 | } |
| 935 | index += HPAGE_PMD_NR - 1; |
| 936 | i += HPAGE_PMD_NR - 1; |
| 937 | } |
| 938 | |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 939 | if (!unfalloc || !PageUptodate(page)) { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 940 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 941 | if (page_mapping(page) == mapping) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 942 | VM_BUG_ON_PAGE(PageWriteback(page), page); |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 943 | truncate_inode_page(mapping, page); |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 944 | } else { |
| 945 | /* Page was replaced by swap: retry */ |
| 946 | unlock_page(page); |
| 947 | index--; |
| 948 | break; |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 949 | } |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 950 | } |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 951 | unlock_page(page); |
| 952 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 953 | pagevec_remove_exceptionals(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 954 | pagevec_release(&pvec); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 955 | index++; |
| 956 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 957 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 958 | spin_lock_irq(&info->lock); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 959 | info->swapped -= nr_swaps_freed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 961 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 964 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
| 965 | { |
| 966 | shmem_undo_range(inode, lstart, lend, false); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 967 | inode->i_ctime = inode->i_mtime = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 969 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 971 | static int shmem_getattr(const struct path *path, struct kstat *stat, |
| 972 | u32 request_mask, unsigned int query_flags) |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 973 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 974 | struct inode *inode = path->dentry->d_inode; |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 975 | struct shmem_inode_info *info = SHMEM_I(inode); |
Yang Shi | 89fdcd2 | 2018-06-07 17:06:59 -0700 | [diff] [blame] | 976 | struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb); |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 977 | |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 978 | if (info->alloced - info->swapped != inode->i_mapping->nrpages) { |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 979 | spin_lock_irq(&info->lock); |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 980 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 981 | spin_unlock_irq(&info->lock); |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 982 | } |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 983 | generic_fillattr(inode, stat); |
Yang Shi | 89fdcd2 | 2018-06-07 17:06:59 -0700 | [diff] [blame] | 984 | |
| 985 | if (is_huge_enabled(sb_info)) |
| 986 | stat->blksize = HPAGE_PMD_SIZE; |
| 987 | |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 988 | return 0; |
| 989 | } |
| 990 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 991 | static int shmem_setattr(struct dentry *dentry, struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 993 | struct inode *inode = d_inode(dentry); |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 994 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 995 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | int error; |
| 997 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 998 | error = setattr_prepare(dentry, attr); |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 999 | if (error) |
| 1000 | return error; |
| 1001 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1002 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
| 1003 | loff_t oldsize = inode->i_size; |
| 1004 | loff_t newsize = attr->ia_size; |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 1005 | |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 1006 | /* protected by i_mutex */ |
| 1007 | if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || |
| 1008 | (newsize > oldsize && (info->seals & F_SEAL_GROW))) |
| 1009 | return -EPERM; |
| 1010 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1011 | if (newsize != oldsize) { |
Konstantin Khlebnikov | 7714251 | 2014-08-06 16:06:34 -0700 | [diff] [blame] | 1012 | error = shmem_reacct_size(SHMEM_I(inode)->flags, |
| 1013 | oldsize, newsize); |
| 1014 | if (error) |
| 1015 | return error; |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1016 | i_size_write(inode, newsize); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 1017 | inode->i_ctime = inode->i_mtime = current_time(inode); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1018 | } |
Josef Bacik | afa2db2 | 2015-06-24 16:58:45 -0700 | [diff] [blame] | 1019 | if (newsize <= oldsize) { |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1020 | loff_t holebegin = round_up(newsize, PAGE_SIZE); |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 1021 | if (oldsize > holebegin) |
| 1022 | unmap_mapping_range(inode->i_mapping, |
| 1023 | holebegin, 0, 1); |
| 1024 | if (info->alloced) |
| 1025 | shmem_truncate_range(inode, |
| 1026 | newsize, (loff_t)-1); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1027 | /* unmap again to remove racily COWed private pages */ |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 1028 | if (oldsize > holebegin) |
| 1029 | unmap_mapping_range(inode->i_mapping, |
| 1030 | holebegin, 0, 1); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1031 | |
| 1032 | /* |
| 1033 | * Part of the huge page can be beyond i_size: subject |
| 1034 | * to shrink under memory pressure. |
| 1035 | */ |
| 1036 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { |
| 1037 | spin_lock(&sbinfo->shrinklist_lock); |
Cong Wang | d041353 | 2017-08-10 15:24:24 -0700 | [diff] [blame] | 1038 | /* |
| 1039 | * _careful to defend against unlocked access to |
| 1040 | * ->shrink_list in shmem_unused_huge_shrink() |
| 1041 | */ |
| 1042 | if (list_empty_careful(&info->shrinklist)) { |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1043 | list_add_tail(&info->shrinklist, |
| 1044 | &sbinfo->shrinklist); |
| 1045 | sbinfo->shrinklist_len++; |
| 1046 | } |
| 1047 | spin_unlock(&sbinfo->shrinklist_lock); |
| 1048 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 1052 | setattr_copy(inode, attr); |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 1053 | if (attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 1054 | error = posix_acl_chmod(inode, inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | return error; |
| 1056 | } |
| 1057 | |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 1058 | static void shmem_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1061 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 1063 | if (inode->i_mapping->a_ops == &shmem_aops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | shmem_unacct_size(info->flags, inode->i_size); |
| 1065 | inode->i_size = 0; |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 1066 | shmem_truncate_range(inode, 0, (loff_t)-1); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1067 | if (!list_empty(&info->shrinklist)) { |
| 1068 | spin_lock(&sbinfo->shrinklist_lock); |
| 1069 | if (!list_empty(&info->shrinklist)) { |
| 1070 | list_del_init(&info->shrinklist); |
| 1071 | sbinfo->shrinklist_len--; |
| 1072 | } |
| 1073 | spin_unlock(&sbinfo->shrinklist_lock); |
| 1074 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | if (!list_empty(&info->swaplist)) { |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1076 | mutex_lock(&shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | list_del_init(&info->swaplist); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1078 | mutex_unlock(&shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
Al Viro | 3ed47db | 2016-01-22 18:08:52 -0500 | [diff] [blame] | 1080 | } |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1081 | |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 1082 | simple_xattrs_free(&info->xattrs); |
Hugh Dickins | 0f3c42f | 2012-11-16 14:15:04 -0800 | [diff] [blame] | 1083 | WARN_ON(inode->i_blocks); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 1084 | shmem_free_inode(inode->i_sb); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 1085 | clear_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1088 | static unsigned long find_swap_entry(struct xarray *xa, void *item) |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1089 | { |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1090 | XA_STATE(xas, xa, 0); |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1091 | unsigned int checked = 0; |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1092 | void *entry; |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1093 | |
| 1094 | rcu_read_lock(); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1095 | xas_for_each(&xas, entry, ULONG_MAX) { |
| 1096 | if (xas_retry(&xas, entry)) |
Mike Kravetz | 5b9c98f | 2018-06-07 17:05:53 -0700 | [diff] [blame] | 1097 | continue; |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1098 | if (entry == item) |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1099 | break; |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1100 | checked++; |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1101 | if ((checked % XA_CHECK_SCHED) != 0) |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1102 | continue; |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1103 | xas_pause(&xas); |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1104 | cond_resched_rcu(); |
| 1105 | } |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1106 | rcu_read_unlock(); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1107 | |
| 1108 | return entry ? xas.xa_index : -1; |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1111 | /* |
| 1112 | * If swap found in inode, free it and move page from swapcache to filecache. |
| 1113 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1114 | static int shmem_unuse_inode(struct shmem_inode_info *info, |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1115 | swp_entry_t swap, struct page **pagep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 1117 | struct address_space *mapping = info->vfs_inode.i_mapping; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1118 | void *radswap; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1119 | pgoff_t index; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1120 | gfp_t gfp; |
| 1121 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1123 | radswap = swp_to_radix_entry(swap); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 1124 | index = find_swap_entry(&mapping->i_pages, radswap); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1125 | if (index == -1) |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1126 | return -EAGAIN; /* tell shmem_unuse we found nothing */ |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 1127 | |
Hugh Dickins | 1b1b32f | 2008-02-04 22:28:55 -0800 | [diff] [blame] | 1128 | /* |
| 1129 | * Move _head_ to start search for next from here. |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 1130 | * But be careful: shmem_evict_inode checks list_empty without taking |
Hugh Dickins | 1b1b32f | 2008-02-04 22:28:55 -0800 | [diff] [blame] | 1131 | * mutex, and there's an instant in list_move_tail when info->swaplist |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 1132 | * would appear empty, if it were the only one on shmem_swaplist. |
Hugh Dickins | 1b1b32f | 2008-02-04 22:28:55 -0800 | [diff] [blame] | 1133 | */ |
| 1134 | if (shmem_swaplist.next != &info->swaplist) |
| 1135 | list_move_tail(&shmem_swaplist, &info->swaplist); |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 1136 | |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1137 | gfp = mapping_gfp_mask(mapping); |
| 1138 | if (shmem_should_replace_page(*pagep, gfp)) { |
| 1139 | mutex_unlock(&shmem_swaplist_mutex); |
| 1140 | error = shmem_replace_page(pagep, gfp, info, index); |
| 1141 | mutex_lock(&shmem_swaplist_mutex); |
| 1142 | /* |
| 1143 | * We needed to drop mutex to make that restrictive page |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1144 | * allocation, but the inode might have been freed while we |
| 1145 | * dropped it: although a racing shmem_evict_inode() cannot |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 1146 | * complete without emptying the page cache, our page lock |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1147 | * on this swapcache page is not enough to prevent that - |
| 1148 | * free_swap_and_cache() of our swap entry will only |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 1149 | * trylock_page(), removing swap from page cache whatever. |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1150 | * |
| 1151 | * We must not proceed to shmem_add_to_page_cache() if the |
| 1152 | * inode has been freed, but of course we cannot rely on |
| 1153 | * inode or mapping or info to check that. However, we can |
| 1154 | * safely check if our swap entry is still in use (and here |
| 1155 | * it can't have got reused for another page): if it's still |
| 1156 | * in use, then the inode cannot have been freed yet, and we |
| 1157 | * can safely proceed (if it's no longer in use, that tells |
| 1158 | * nothing about the inode, but we don't need to unuse swap). |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1159 | */ |
| 1160 | if (!page_swapcount(*pagep)) |
| 1161 | error = -ENOENT; |
| 1162 | } |
| 1163 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1164 | /* |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1165 | * We rely on shmem_swaplist_mutex, not only to protect the swaplist, |
| 1166 | * but also to hold up shmem_evict_inode(): so inode cannot be freed |
| 1167 | * beneath us (pagelock doesn't help until the page is in pagecache). |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1168 | */ |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1169 | if (!error) |
| 1170 | error = shmem_add_to_page_cache(*pagep, mapping, index, |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 1171 | radswap, gfp); |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1172 | if (error != -ENOMEM) { |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * Truncation and eviction use free_swap_and_cache(), which |
| 1175 | * only does trylock page: if we raced, best clean up here. |
| 1176 | */ |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1177 | delete_from_swap_cache(*pagep); |
| 1178 | set_page_dirty(*pagep); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1179 | if (!error) { |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1180 | spin_lock_irq(&info->lock); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1181 | info->swapped--; |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1182 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1183 | swap_free(swap); |
| 1184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 1186 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1190 | * Search through swapped inodes to find and replace swap by page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1192 | int shmem_unuse(swp_entry_t swap, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1194 | struct list_head *this, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | struct shmem_inode_info *info; |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1196 | struct mem_cgroup *memcg; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1197 | int error = 0; |
| 1198 | |
| 1199 | /* |
| 1200 | * There's a faint possibility that swap page was replaced before |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1201 | * caller locked it: caller will come back later with the right page. |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1202 | */ |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1203 | if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1204 | goto out; |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* |
| 1207 | * Charge page using GFP_KERNEL while we can wait, before taking |
| 1208 | * the shmem_swaplist_mutex which might hold up shmem_writepage(). |
| 1209 | * Charged back to the user (not to caller) when swap account is used. |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1210 | */ |
Tejun Heo | 2cf8558 | 2018-07-03 11:14:56 -0400 | [diff] [blame] | 1211 | error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL, |
| 1212 | &memcg, false); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1213 | if (error) |
| 1214 | goto out; |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 1215 | /* No memory allocation: swap entry occupies the slot for the page */ |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1216 | error = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1218 | mutex_lock(&shmem_swaplist_mutex); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1219 | list_for_each_safe(this, next, &shmem_swaplist) { |
| 1220 | info = list_entry(this, struct shmem_inode_info, swaplist); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 1221 | if (info->swapped) |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1222 | error = shmem_unuse_inode(info, swap, &page); |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1223 | else |
| 1224 | list_del_init(&info->swaplist); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1225 | cond_resched(); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1226 | if (error != -EAGAIN) |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1227 | break; |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1228 | /* found nothing in this: move on to search the next */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | } |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1230 | mutex_unlock(&shmem_swaplist_mutex); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1231 | |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1232 | if (error) { |
| 1233 | if (error != -ENOMEM) |
| 1234 | error = 0; |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 1235 | mem_cgroup_cancel_charge(page, memcg, false); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1236 | } else |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 1237 | mem_cgroup_commit_charge(page, memcg, true, false); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1238 | out: |
Hugh Dickins | aaa4686 | 2009-12-14 17:58:47 -0800 | [diff] [blame] | 1239 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1240 | put_page(page); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1241 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * Move the page from the page cache to the swap cache. |
| 1246 | */ |
| 1247 | static int shmem_writepage(struct page *page, struct writeback_control *wbc) |
| 1248 | { |
| 1249 | struct shmem_inode_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | struct address_space *mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | struct inode *inode; |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1252 | swp_entry_t swap; |
| 1253 | pgoff_t index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1255 | VM_BUG_ON_PAGE(PageCompound(page), page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | mapping = page->mapping; |
| 1258 | index = page->index; |
| 1259 | inode = mapping->host; |
| 1260 | info = SHMEM_I(inode); |
| 1261 | if (info->flags & VM_LOCKED) |
| 1262 | goto redirty; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1263 | if (!total_swap_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | goto redirty; |
| 1265 | |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1266 | /* |
Christoph Hellwig | 97b713b | 2015-01-14 10:42:31 +0100 | [diff] [blame] | 1267 | * Our capabilities prevent regular writeback or sync from ever calling |
| 1268 | * shmem_writepage; but a stacking filesystem might use ->writepage of |
| 1269 | * its underlying filesystem, in which case tmpfs should write out to |
| 1270 | * swap only in response to memory pressure, and not for the writeback |
| 1271 | * threads or sync. |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1272 | */ |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1273 | if (!wbc->for_reclaim) { |
| 1274 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ |
| 1275 | goto redirty; |
| 1276 | } |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1277 | |
| 1278 | /* |
| 1279 | * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC |
| 1280 | * value into swapfile.c, the only way we can correctly account for a |
| 1281 | * fallocated page arriving here is now to initialize it and write it. |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1282 | * |
| 1283 | * That's okay for a page already fallocated earlier, but if we have |
| 1284 | * not yet completed the fallocation, then (a) we want to keep track |
| 1285 | * of this page in case we have to undo it, and (b) it may not be a |
| 1286 | * good idea to continue anyway, once we're pushing into swap. So |
| 1287 | * reactivate the page, and let shmem_fallocate() quit when too many. |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1288 | */ |
| 1289 | if (!PageUptodate(page)) { |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1290 | if (inode->i_private) { |
| 1291 | struct shmem_falloc *shmem_falloc; |
| 1292 | spin_lock(&inode->i_lock); |
| 1293 | shmem_falloc = inode->i_private; |
| 1294 | if (shmem_falloc && |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1295 | !shmem_falloc->waitq && |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1296 | index >= shmem_falloc->start && |
| 1297 | index < shmem_falloc->next) |
| 1298 | shmem_falloc->nr_unswapped++; |
| 1299 | else |
| 1300 | shmem_falloc = NULL; |
| 1301 | spin_unlock(&inode->i_lock); |
| 1302 | if (shmem_falloc) |
| 1303 | goto redirty; |
| 1304 | } |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1305 | clear_highpage(page); |
| 1306 | flush_dcache_page(page); |
| 1307 | SetPageUptodate(page); |
| 1308 | } |
| 1309 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 1310 | swap = get_swap_page(page); |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1311 | if (!swap.val) |
| 1312 | goto redirty; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1313 | |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1314 | /* |
| 1315 | * Add inode to shmem_unuse()'s list of swapped-out inodes, |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1316 | * if it's not already there. Do it now before the page is |
| 1317 | * moved to swap cache, when its pagelock no longer protects |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1318 | * the inode from eviction. But don't unlock the mutex until |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1319 | * we've incremented swapped, because shmem_unuse_inode() will |
| 1320 | * prune a !swapped inode from the swaplist under this mutex. |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1321 | */ |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1322 | mutex_lock(&shmem_swaplist_mutex); |
| 1323 | if (list_empty(&info->swaplist)) |
| 1324 | list_add_tail(&info->swaplist, &shmem_swaplist); |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1325 | |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1326 | if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1327 | spin_lock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1328 | shmem_recalc_inode(inode); |
| 1329 | info->swapped++; |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1330 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1331 | |
Hugh Dickins | aaa4686 | 2009-12-14 17:58:47 -0800 | [diff] [blame] | 1332 | swap_shmem_alloc(swap); |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1333 | shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); |
| 1334 | |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1335 | mutex_unlock(&shmem_swaplist_mutex); |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1336 | BUG_ON(page_mapped(page)); |
Hugh Dickins | 9fab561 | 2009-03-31 15:23:33 -0700 | [diff] [blame] | 1337 | swap_writepage(page, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | return 0; |
| 1339 | } |
| 1340 | |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1341 | mutex_unlock(&shmem_swaplist_mutex); |
Minchan Kim | 75f6d6d | 2017-07-06 15:37:21 -0700 | [diff] [blame] | 1342 | put_swap_page(page, swap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | redirty: |
| 1344 | set_page_dirty(page); |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1345 | if (wbc->for_reclaim) |
| 1346 | return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ |
| 1347 | unlock_page(page); |
| 1348 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1351 | #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1352 | static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1353 | { |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 1354 | char buffer[64]; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1355 | |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1356 | if (!mpol || mpol->mode == MPOL_DEFAULT) |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 1357 | return; /* show nothing */ |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1358 | |
Hugh Dickins | a7a88b2 | 2013-01-02 02:04:23 -0800 | [diff] [blame] | 1359 | mpol_to_str(buffer, sizeof(buffer), mpol); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1360 | |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 1361 | seq_printf(seq, ",mpol=%s", buffer); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1362 | } |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1363 | |
| 1364 | static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) |
| 1365 | { |
| 1366 | struct mempolicy *mpol = NULL; |
| 1367 | if (sbinfo->mpol) { |
| 1368 | spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ |
| 1369 | mpol = sbinfo->mpol; |
| 1370 | mpol_get(mpol); |
| 1371 | spin_unlock(&sbinfo->stat_lock); |
| 1372 | } |
| 1373 | return mpol; |
| 1374 | } |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1375 | #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ |
| 1376 | static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
| 1377 | { |
| 1378 | } |
| 1379 | static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) |
| 1380 | { |
| 1381 | return NULL; |
| 1382 | } |
| 1383 | #endif /* CONFIG_NUMA && CONFIG_TMPFS */ |
| 1384 | #ifndef CONFIG_NUMA |
| 1385 | #define vm_policy vm_private_data |
| 1386 | #endif |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1387 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1388 | static void shmem_pseudo_vma_init(struct vm_area_struct *vma, |
| 1389 | struct shmem_inode_info *info, pgoff_t index) |
| 1390 | { |
| 1391 | /* Create a pseudo vma that just contains the policy */ |
Kirill A. Shutemov | 2c4541e | 2018-07-26 16:37:30 -0700 | [diff] [blame] | 1392 | vma_init(vma, NULL); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1393 | /* Bias interleave by inode number to distribute better across nodes */ |
| 1394 | vma->vm_pgoff = index + info->vfs_inode.i_ino; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1395 | vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); |
| 1396 | } |
| 1397 | |
| 1398 | static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) |
| 1399 | { |
| 1400 | /* Drop reference taken by mpol_shared_policy_lookup() */ |
| 1401 | mpol_cond_put(vma->vm_policy); |
| 1402 | } |
| 1403 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1404 | static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
| 1405 | struct shmem_inode_info *info, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | struct vm_area_struct pvma; |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1408 | struct page *page; |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 1409 | struct vm_fault vmf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1411 | shmem_pseudo_vma_init(&pvma, info, index); |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 1412 | vmf.vma = &pvma; |
| 1413 | vmf.address = 0; |
| 1414 | page = swap_cluster_readahead(swap, gfp, &vmf); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1415 | shmem_pseudo_vma_destroy(&pvma); |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1416 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1417 | return page; |
| 1418 | } |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1419 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1420 | static struct page *shmem_alloc_hugepage(gfp_t gfp, |
| 1421 | struct shmem_inode_info *info, pgoff_t index) |
| 1422 | { |
| 1423 | struct vm_area_struct pvma; |
Matthew Wilcox | 7b8d046 | 2017-12-01 22:13:06 -0500 | [diff] [blame] | 1424 | struct address_space *mapping = info->vfs_inode.i_mapping; |
| 1425 | pgoff_t hindex; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1426 | struct page *page; |
| 1427 | |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 1428 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1429 | return NULL; |
| 1430 | |
Geert Uytterhoeven | 4620a06 | 2016-08-03 19:58:19 +0200 | [diff] [blame] | 1431 | hindex = round_down(index, HPAGE_PMD_NR); |
Matthew Wilcox | 7b8d046 | 2017-12-01 22:13:06 -0500 | [diff] [blame] | 1432 | if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, |
| 1433 | XA_PRESENT)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1434 | return NULL; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1435 | |
| 1436 | shmem_pseudo_vma_init(&pvma, info, hindex); |
| 1437 | page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, |
Michal Hocko | 89c83fb | 2018-11-02 15:48:31 -0700 | [diff] [blame] | 1438 | HPAGE_PMD_ORDER, &pvma, 0, numa_node_id()); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1439 | shmem_pseudo_vma_destroy(&pvma); |
| 1440 | if (page) |
| 1441 | prep_transhuge_page(page); |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1442 | return page; |
| 1443 | } |
| 1444 | |
| 1445 | static struct page *shmem_alloc_page(gfp_t gfp, |
| 1446 | struct shmem_inode_info *info, pgoff_t index) |
| 1447 | { |
| 1448 | struct vm_area_struct pvma; |
| 1449 | struct page *page; |
| 1450 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1451 | shmem_pseudo_vma_init(&pvma, info, index); |
| 1452 | page = alloc_page_vma(gfp, &pvma, 0); |
| 1453 | shmem_pseudo_vma_destroy(&pvma); |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1454 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1455 | return page; |
| 1456 | } |
| 1457 | |
| 1458 | static struct page *shmem_alloc_and_acct_page(gfp_t gfp, |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1459 | struct inode *inode, |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1460 | pgoff_t index, bool huge) |
| 1461 | { |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1462 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1463 | struct page *page; |
| 1464 | int nr; |
| 1465 | int err = -ENOSPC; |
| 1466 | |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 1467 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1468 | huge = false; |
| 1469 | nr = huge ? HPAGE_PMD_NR : 1; |
| 1470 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1471 | if (!shmem_inode_acct_block(inode, nr)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1472 | goto failed; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1473 | |
| 1474 | if (huge) |
| 1475 | page = shmem_alloc_hugepage(gfp, info, index); |
| 1476 | else |
| 1477 | page = shmem_alloc_page(gfp, info, index); |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1478 | if (page) { |
| 1479 | __SetPageLocked(page); |
| 1480 | __SetPageSwapBacked(page); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1481 | return page; |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1482 | } |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1483 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1484 | err = -ENOMEM; |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1485 | shmem_inode_unacct_blocks(inode, nr); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1486 | failed: |
| 1487 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | } |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | /* |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1491 | * When a page is moved from swapcache to shmem filecache (either by the |
| 1492 | * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of |
| 1493 | * shmem_unuse_inode()), it may have been read in earlier from swap, in |
| 1494 | * ignorance of the mapping it belongs to. If that mapping has special |
| 1495 | * constraints (like the gma500 GEM driver, which requires RAM below 4GB), |
| 1496 | * we may need to copy to a suitable page before moving to filecache. |
| 1497 | * |
| 1498 | * In a future release, this may well be extended to respect cpuset and |
| 1499 | * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); |
| 1500 | * but for now it is a simple matter of zone. |
| 1501 | */ |
| 1502 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp) |
| 1503 | { |
| 1504 | return page_zonenum(page) > gfp_zone(gfp); |
| 1505 | } |
| 1506 | |
| 1507 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, |
| 1508 | struct shmem_inode_info *info, pgoff_t index) |
| 1509 | { |
| 1510 | struct page *oldpage, *newpage; |
| 1511 | struct address_space *swap_mapping; |
Yu Zhao | c1cb20d | 2018-11-30 14:09:03 -0800 | [diff] [blame^] | 1512 | swp_entry_t entry; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1513 | pgoff_t swap_index; |
| 1514 | int error; |
| 1515 | |
| 1516 | oldpage = *pagep; |
Yu Zhao | c1cb20d | 2018-11-30 14:09:03 -0800 | [diff] [blame^] | 1517 | entry.val = page_private(oldpage); |
| 1518 | swap_index = swp_offset(entry); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1519 | swap_mapping = page_mapping(oldpage); |
| 1520 | |
| 1521 | /* |
| 1522 | * We have arrived here because our zones are constrained, so don't |
| 1523 | * limit chance of success by further cpuset and node constraints. |
| 1524 | */ |
| 1525 | gfp &= ~GFP_CONSTRAINT_MASK; |
| 1526 | newpage = shmem_alloc_page(gfp, info, index); |
| 1527 | if (!newpage) |
| 1528 | return -ENOMEM; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1529 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1530 | get_page(newpage); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1531 | copy_highpage(newpage, oldpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1532 | flush_dcache_page(newpage); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1533 | |
Hugh Dickins | 9956edf | 2016-11-10 10:46:11 -0800 | [diff] [blame] | 1534 | __SetPageLocked(newpage); |
| 1535 | __SetPageSwapBacked(newpage); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1536 | SetPageUptodate(newpage); |
Yu Zhao | c1cb20d | 2018-11-30 14:09:03 -0800 | [diff] [blame^] | 1537 | set_page_private(newpage, entry.val); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1538 | SetPageSwapCache(newpage); |
| 1539 | |
| 1540 | /* |
| 1541 | * Our caller will very soon move newpage out of swapcache, but it's |
| 1542 | * a nice clean interface for us to replace oldpage by newpage there. |
| 1543 | */ |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 1544 | xa_lock_irq(&swap_mapping->i_pages); |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 1545 | error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1546 | if (!error) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 1547 | __inc_node_page_state(newpage, NR_FILE_PAGES); |
| 1548 | __dec_node_page_state(oldpage, NR_FILE_PAGES); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1549 | } |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 1550 | xa_unlock_irq(&swap_mapping->i_pages); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1551 | |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1552 | if (unlikely(error)) { |
| 1553 | /* |
| 1554 | * Is this possible? I think not, now that our callers check |
| 1555 | * both PageSwapCache and page_private after getting page lock; |
| 1556 | * but be defensive. Reverse old to newpage for clear and free. |
| 1557 | */ |
| 1558 | oldpage = newpage; |
| 1559 | } else { |
Johannes Weiner | 6a93ca8 | 2016-03-15 14:57:19 -0700 | [diff] [blame] | 1560 | mem_cgroup_migrate(oldpage, newpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1561 | lru_cache_add_anon(newpage); |
| 1562 | *pagep = newpage; |
| 1563 | } |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1564 | |
| 1565 | ClearPageSwapCache(oldpage); |
| 1566 | set_page_private(oldpage, 0); |
| 1567 | |
| 1568 | unlock_page(oldpage); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1569 | put_page(oldpage); |
| 1570 | put_page(oldpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1571 | return error; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /* |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 1575 | * shmem_getpage_gfp - find page in cache, or get from swap, or allocate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | * |
| 1577 | * If we allocate a new one we do not mark it dirty. That's up to the |
| 1578 | * vm. If we swap it in we mark it dirty since we also free the swap |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1579 | * entry since a page cannot live in both the swap and page cache. |
| 1580 | * |
| 1581 | * fault_mm and fault_type are only supplied by shmem_fault: |
| 1582 | * otherwise they are NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1584 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1585 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 1586 | struct vm_area_struct *vma, struct vm_fault *vmf, |
| 1587 | vm_fault_t *fault_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | { |
| 1589 | struct address_space *mapping = inode->i_mapping; |
Arnd Bergmann | 23f919d | 2016-12-12 16:42:28 -0800 | [diff] [blame] | 1590 | struct shmem_inode_info *info = SHMEM_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | struct shmem_sb_info *sbinfo; |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1592 | struct mm_struct *charge_mm; |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1593 | struct mem_cgroup *memcg; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1594 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | swp_entry_t swap; |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1596 | enum sgp_type sgp_huge = sgp; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1597 | pgoff_t hindex = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | int error; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1599 | int once = 0; |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1600 | int alloced = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1602 | if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | return -EFBIG; |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1604 | if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) |
| 1605 | sgp = SGP_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | repeat: |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1607 | swap.val = 0; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1608 | page = find_lock_entry(mapping, index); |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 1609 | if (xa_is_value(page)) { |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1610 | swap = radix_to_swp_entry(page); |
| 1611 | page = NULL; |
| 1612 | } |
| 1613 | |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1614 | if (sgp <= SGP_CACHE && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1615 | ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1616 | error = -EINVAL; |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1617 | goto unlock; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Hugh Dickins | 66d2f4d | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1620 | if (page && sgp == SGP_WRITE) |
| 1621 | mark_page_accessed(page); |
| 1622 | |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1623 | /* fallocated page? */ |
| 1624 | if (page && !PageUptodate(page)) { |
| 1625 | if (sgp != SGP_READ) |
| 1626 | goto clear; |
| 1627 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1628 | put_page(page); |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1629 | page = NULL; |
| 1630 | } |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1631 | if (page || (sgp == SGP_READ && !swap.val)) { |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1632 | *pagep = page; |
| 1633 | return 0; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
| 1636 | /* |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1637 | * Fast cache lookup did not find it: |
| 1638 | * bring it back from swap or allocate. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | */ |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1640 | sbinfo = SHMEM_SB(inode->i_sb); |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 1641 | charge_mm = vma ? vma->vm_mm : current->mm; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | if (swap.val) { |
| 1644 | /* Look it up and read it in.. */ |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 1645 | page = lookup_swap_cache(swap, NULL, 0); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1646 | if (!page) { |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1647 | /* Or update major stats only when swapin succeeds?? */ |
| 1648 | if (fault_type) { |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 1649 | *fault_type |= VM_FAULT_MAJOR; |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1650 | count_vm_event(PGMAJFAULT); |
Roman Gushchin | 2262185 | 2017-07-06 15:40:25 -0700 | [diff] [blame] | 1651 | count_memcg_event_mm(charge_mm, PGMAJFAULT); |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1652 | } |
| 1653 | /* Here we actually start the io */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1654 | page = shmem_swapin(swap, gfp, info, index); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1655 | if (!page) { |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1656 | error = -ENOMEM; |
| 1657 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | /* We have to do this with page locked to prevent races */ |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1662 | lock_page(page); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1663 | if (!PageSwapCache(page) || page_private(page) != swap.val || |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 1664 | !shmem_confirm_swap(mapping, index, swap)) { |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1665 | error = -EEXIST; /* try again */ |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 1666 | goto unlock; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1667 | } |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1668 | if (!PageUptodate(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | error = -EIO; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1670 | goto failed; |
| 1671 | } |
| 1672 | wait_on_page_writeback(page); |
| 1673 | |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1674 | if (shmem_should_replace_page(page, gfp)) { |
| 1675 | error = shmem_replace_page(&page, gfp, info, index); |
| 1676 | if (error) |
| 1677 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Tejun Heo | 2cf8558 | 2018-07-03 11:14:56 -0400 | [diff] [blame] | 1680 | error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 1681 | false); |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 1682 | if (!error) { |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1683 | error = shmem_add_to_page_cache(page, mapping, index, |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 1684 | swp_to_radix_entry(swap), gfp); |
Hugh Dickins | 215c02b | 2012-11-16 14:15:03 -0800 | [diff] [blame] | 1685 | /* |
| 1686 | * We already confirmed swap under page lock, and make |
| 1687 | * no memory allocation here, so usually no possibility |
| 1688 | * of error; but free_swap_and_cache() only trylocks a |
| 1689 | * page, so it is just possible that the entry has been |
| 1690 | * truncated or holepunched since swap was confirmed. |
| 1691 | * shmem_undo_range() will have done some of the |
| 1692 | * unaccounting, now delete_from_swap_cache() will do |
Vladimir Davydov | 93aa7d9 | 2015-02-11 15:24:59 -0800 | [diff] [blame] | 1693 | * the rest. |
Hugh Dickins | 215c02b | 2012-11-16 14:15:03 -0800 | [diff] [blame] | 1694 | * Reset swap.val? No, leave it so "failed" goes back to |
| 1695 | * "repeat": reading a hole and writing should succeed. |
| 1696 | */ |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1697 | if (error) { |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 1698 | mem_cgroup_cancel_charge(page, memcg, false); |
Hugh Dickins | 215c02b | 2012-11-16 14:15:03 -0800 | [diff] [blame] | 1699 | delete_from_swap_cache(page); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1700 | } |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 1701 | } |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1702 | if (error) |
| 1703 | goto failed; |
| 1704 | |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 1705 | mem_cgroup_commit_charge(page, memcg, true, false); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 1706 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1707 | spin_lock_irq(&info->lock); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1708 | info->swapped--; |
| 1709 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1710 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1711 | |
Hugh Dickins | 66d2f4d | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1712 | if (sgp == SGP_WRITE) |
| 1713 | mark_page_accessed(page); |
| 1714 | |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1715 | delete_from_swap_cache(page); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1716 | set_page_dirty(page); |
| 1717 | swap_free(swap); |
| 1718 | |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1719 | } else { |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 1720 | if (vma && userfaultfd_missing(vma)) { |
| 1721 | *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1725 | /* shmem_symlink() */ |
| 1726 | if (mapping->a_ops != &shmem_aops) |
| 1727 | goto alloc_nohuge; |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1728 | if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1729 | goto alloc_nohuge; |
| 1730 | if (shmem_huge == SHMEM_HUGE_FORCE) |
| 1731 | goto alloc_huge; |
| 1732 | switch (sbinfo->huge) { |
| 1733 | loff_t i_size; |
| 1734 | pgoff_t off; |
| 1735 | case SHMEM_HUGE_NEVER: |
| 1736 | goto alloc_nohuge; |
| 1737 | case SHMEM_HUGE_WITHIN_SIZE: |
| 1738 | off = round_up(index, HPAGE_PMD_NR); |
| 1739 | i_size = round_up(i_size_read(inode), PAGE_SIZE); |
| 1740 | if (i_size >= HPAGE_PMD_SIZE && |
| 1741 | i_size >> PAGE_SHIFT >= off) |
| 1742 | goto alloc_huge; |
| 1743 | /* fallthrough */ |
| 1744 | case SHMEM_HUGE_ADVISE: |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1745 | if (sgp_huge == SGP_HUGE) |
| 1746 | goto alloc_huge; |
| 1747 | /* TODO: implement fadvise() hints */ |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1748 | goto alloc_nohuge; |
Hugh Dickins | 59a16ea | 2011-05-11 15:13:38 -0700 | [diff] [blame] | 1749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1751 | alloc_huge: |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1752 | page = shmem_alloc_and_acct_page(gfp, inode, index, true); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1753 | if (IS_ERR(page)) { |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1754 | alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1755 | index, false); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1756 | } |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1757 | if (IS_ERR(page)) { |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1758 | int retry = 5; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1759 | error = PTR_ERR(page); |
| 1760 | page = NULL; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1761 | if (error != -ENOSPC) |
| 1762 | goto failed; |
| 1763 | /* |
| 1764 | * Try to reclaim some spece by splitting a huge page |
| 1765 | * beyond i_size on the filesystem. |
| 1766 | */ |
| 1767 | while (retry--) { |
| 1768 | int ret; |
| 1769 | ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); |
| 1770 | if (ret == SHRINK_STOP) |
| 1771 | break; |
| 1772 | if (ret) |
| 1773 | goto alloc_nohuge; |
| 1774 | } |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1775 | goto failed; |
| 1776 | } |
| 1777 | |
| 1778 | if (PageTransHuge(page)) |
| 1779 | hindex = round_down(index, HPAGE_PMD_NR); |
| 1780 | else |
| 1781 | hindex = index; |
| 1782 | |
Hugh Dickins | 66d2f4d | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1783 | if (sgp == SGP_WRITE) |
Hugh Dickins | eb39d61 | 2014-08-06 16:06:43 -0700 | [diff] [blame] | 1784 | __SetPageReferenced(page); |
Hugh Dickins | 66d2f4d | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1785 | |
Tejun Heo | 2cf8558 | 2018-07-03 11:14:56 -0400 | [diff] [blame] | 1786 | error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1787 | PageTransHuge(page)); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1788 | if (error) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1789 | goto unacct; |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 1790 | error = shmem_add_to_page_cache(page, mapping, hindex, |
| 1791 | NULL, gfp & GFP_RECLAIM_MASK); |
Hugh Dickins | b065b43 | 2012-07-11 14:02:48 -0700 | [diff] [blame] | 1792 | if (error) { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1793 | mem_cgroup_cancel_charge(page, memcg, |
| 1794 | PageTransHuge(page)); |
| 1795 | goto unacct; |
Hugh Dickins | b065b43 | 2012-07-11 14:02:48 -0700 | [diff] [blame] | 1796 | } |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1797 | mem_cgroup_commit_charge(page, memcg, false, |
| 1798 | PageTransHuge(page)); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1799 | lru_cache_add_anon(page); |
| 1800 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1801 | spin_lock_irq(&info->lock); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1802 | info->alloced += 1 << compound_order(page); |
| 1803 | inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1804 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1805 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1806 | alloced = true; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1807 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1808 | if (PageTransHuge(page) && |
| 1809 | DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < |
| 1810 | hindex + HPAGE_PMD_NR - 1) { |
| 1811 | /* |
| 1812 | * Part of the huge page is beyond i_size: subject |
| 1813 | * to shrink under memory pressure. |
| 1814 | */ |
| 1815 | spin_lock(&sbinfo->shrinklist_lock); |
Cong Wang | d041353 | 2017-08-10 15:24:24 -0700 | [diff] [blame] | 1816 | /* |
| 1817 | * _careful to defend against unlocked access to |
| 1818 | * ->shrink_list in shmem_unused_huge_shrink() |
| 1819 | */ |
| 1820 | if (list_empty_careful(&info->shrinklist)) { |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1821 | list_add_tail(&info->shrinklist, |
| 1822 | &sbinfo->shrinklist); |
| 1823 | sbinfo->shrinklist_len++; |
| 1824 | } |
| 1825 | spin_unlock(&sbinfo->shrinklist_lock); |
| 1826 | } |
| 1827 | |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 1828 | /* |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1829 | * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. |
| 1830 | */ |
| 1831 | if (sgp == SGP_FALLOC) |
| 1832 | sgp = SGP_WRITE; |
| 1833 | clear: |
| 1834 | /* |
| 1835 | * Let SGP_WRITE caller clear ends if write does not fill page; |
| 1836 | * but SGP_FALLOC on a page fallocated earlier must initialize |
| 1837 | * it now, lest undo on failure cancel our earlier guarantee. |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 1838 | */ |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1839 | if (sgp != SGP_WRITE && !PageUptodate(page)) { |
| 1840 | struct page *head = compound_head(page); |
| 1841 | int i; |
| 1842 | |
| 1843 | for (i = 0; i < (1 << compound_order(head)); i++) { |
| 1844 | clear_highpage(head + i); |
| 1845 | flush_dcache_page(head + i); |
| 1846 | } |
| 1847 | SetPageUptodate(head); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 1848 | } |
Hugh Dickins | 59a16ea | 2011-05-11 15:13:38 -0700 | [diff] [blame] | 1849 | } |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1850 | |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1851 | /* Perhaps the file has been truncated since we checked */ |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1852 | if (sgp <= SGP_CACHE && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1853 | ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1854 | if (alloced) { |
| 1855 | ClearPageDirty(page); |
| 1856 | delete_from_page_cache(page); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1857 | spin_lock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1858 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1859 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1860 | } |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1861 | error = -EINVAL; |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1862 | goto unlock; |
Shaohua Li | ff36b801 | 2010-08-09 17:19:06 -0700 | [diff] [blame] | 1863 | } |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1864 | *pagep = page + index - hindex; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1865 | return 0; |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 1866 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1867 | /* |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1868 | * Error recovery. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | */ |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1870 | unacct: |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1871 | shmem_inode_unacct_blocks(inode, 1 << compound_order(page)); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1872 | |
| 1873 | if (PageTransHuge(page)) { |
| 1874 | unlock_page(page); |
| 1875 | put_page(page); |
| 1876 | goto alloc_nohuge; |
| 1877 | } |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1878 | failed: |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1879 | if (swap.val && !shmem_confirm_swap(mapping, index, swap)) |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 1880 | error = -EEXIST; |
| 1881 | unlock: |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1882 | if (page) { |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1883 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1884 | put_page(page); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1885 | } |
| 1886 | if (error == -ENOSPC && !once++) { |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1887 | spin_lock_irq(&info->lock); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1888 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1889 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1890 | goto repeat; |
Josef "Jeff" Sipek | d3ac7f8 | 2006-12-08 02:36:44 -0800 | [diff] [blame] | 1891 | } |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 1892 | if (error == -EEXIST) |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1893 | goto repeat; |
| 1894 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 1897 | /* |
| 1898 | * This is like autoremove_wake_function, but it removes the wait queue |
| 1899 | * entry unconditionally - even if something else had already woken the |
| 1900 | * target. |
| 1901 | */ |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1902 | static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 1903 | { |
| 1904 | int ret = default_wake_function(wait, mode, sync, key); |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1905 | list_del_init(&wait->entry); |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 1906 | return ret; |
| 1907 | } |
| 1908 | |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 1909 | static vm_fault_t shmem_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1911 | struct vm_area_struct *vma = vmf->vma; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1912 | struct inode *inode = file_inode(vma->vm_file); |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1913 | gfp_t gfp = mapping_gfp_mask(inode->i_mapping); |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1914 | enum sgp_type sgp; |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 1915 | int err; |
| 1916 | vm_fault_t ret = VM_FAULT_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 1918 | /* |
| 1919 | * Trinity finds that probing a hole which tmpfs is punching can |
| 1920 | * prevent the hole-punch from ever completing: which in turn |
| 1921 | * locks writers out with its hold on i_mutex. So refrain from |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1922 | * faulting pages into the hole while it's being punched. Although |
| 1923 | * shmem_undo_range() does remove the additions, it may be unable to |
| 1924 | * keep up, as each new page needs its own unmap_mapping_range() call, |
| 1925 | * and the i_mmap tree grows ever slower to scan if new vmas are added. |
| 1926 | * |
| 1927 | * It does not matter if we sometimes reach this check just before the |
| 1928 | * hole-punch begins, so that one fault then races with the punch: |
| 1929 | * we just need to make racing faults a rare case. |
| 1930 | * |
| 1931 | * The implementation below would be much simpler if we just used a |
| 1932 | * standard mutex or completion: but we cannot take i_mutex in fault, |
| 1933 | * and bloating every shmem inode for this unlikely case would be sad. |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 1934 | */ |
| 1935 | if (unlikely(inode->i_private)) { |
| 1936 | struct shmem_falloc *shmem_falloc; |
| 1937 | |
| 1938 | spin_lock(&inode->i_lock); |
| 1939 | shmem_falloc = inode->i_private; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1940 | if (shmem_falloc && |
| 1941 | shmem_falloc->waitq && |
| 1942 | vmf->pgoff >= shmem_falloc->start && |
| 1943 | vmf->pgoff < shmem_falloc->next) { |
| 1944 | wait_queue_head_t *shmem_falloc_waitq; |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 1945 | DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1946 | |
| 1947 | ret = VM_FAULT_NOPAGE; |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 1948 | if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && |
| 1949 | !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1950 | /* It's polite to up mmap_sem if we can */ |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 1951 | up_read(&vma->vm_mm->mmap_sem); |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1952 | ret = VM_FAULT_RETRY; |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 1953 | } |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1954 | |
| 1955 | shmem_falloc_waitq = shmem_falloc->waitq; |
| 1956 | prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, |
| 1957 | TASK_UNINTERRUPTIBLE); |
| 1958 | spin_unlock(&inode->i_lock); |
| 1959 | schedule(); |
| 1960 | |
| 1961 | /* |
| 1962 | * shmem_falloc_waitq points into the shmem_fallocate() |
| 1963 | * stack of the hole-punching task: shmem_falloc_waitq |
| 1964 | * is usually invalid by the time we reach here, but |
| 1965 | * finish_wait() does not dereference it in that case; |
| 1966 | * though i_lock needed lest racing with wake_up_all(). |
| 1967 | */ |
| 1968 | spin_lock(&inode->i_lock); |
| 1969 | finish_wait(shmem_falloc_waitq, &shmem_fault_wait); |
| 1970 | spin_unlock(&inode->i_lock); |
| 1971 | return ret; |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 1972 | } |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1973 | spin_unlock(&inode->i_lock); |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1976 | sgp = SGP_CACHE; |
Michal Hocko | 1860033 | 2017-07-10 15:48:02 -0700 | [diff] [blame] | 1977 | |
| 1978 | if ((vma->vm_flags & VM_NOHUGEPAGE) || |
| 1979 | test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1980 | sgp = SGP_NOHUGE; |
Michal Hocko | 1860033 | 2017-07-10 15:48:02 -0700 | [diff] [blame] | 1981 | else if (vma->vm_flags & VM_HUGEPAGE) |
| 1982 | sgp = SGP_HUGE; |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1983 | |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 1984 | err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 1985 | gfp, vma, vmf, &ret); |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 1986 | if (err) |
| 1987 | return vmf_error(err); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 1988 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 1991 | unsigned long shmem_get_unmapped_area(struct file *file, |
| 1992 | unsigned long uaddr, unsigned long len, |
| 1993 | unsigned long pgoff, unsigned long flags) |
| 1994 | { |
| 1995 | unsigned long (*get_area)(struct file *, |
| 1996 | unsigned long, unsigned long, unsigned long, unsigned long); |
| 1997 | unsigned long addr; |
| 1998 | unsigned long offset; |
| 1999 | unsigned long inflated_len; |
| 2000 | unsigned long inflated_addr; |
| 2001 | unsigned long inflated_offset; |
| 2002 | |
| 2003 | if (len > TASK_SIZE) |
| 2004 | return -ENOMEM; |
| 2005 | |
| 2006 | get_area = current->mm->get_unmapped_area; |
| 2007 | addr = get_area(file, uaddr, len, pgoff, flags); |
| 2008 | |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 2009 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2010 | return addr; |
| 2011 | if (IS_ERR_VALUE(addr)) |
| 2012 | return addr; |
| 2013 | if (addr & ~PAGE_MASK) |
| 2014 | return addr; |
| 2015 | if (addr > TASK_SIZE - len) |
| 2016 | return addr; |
| 2017 | |
| 2018 | if (shmem_huge == SHMEM_HUGE_DENY) |
| 2019 | return addr; |
| 2020 | if (len < HPAGE_PMD_SIZE) |
| 2021 | return addr; |
| 2022 | if (flags & MAP_FIXED) |
| 2023 | return addr; |
| 2024 | /* |
| 2025 | * Our priority is to support MAP_SHARED mapped hugely; |
| 2026 | * and support MAP_PRIVATE mapped hugely too, until it is COWed. |
| 2027 | * But if caller specified an address hint, respect that as before. |
| 2028 | */ |
| 2029 | if (uaddr) |
| 2030 | return addr; |
| 2031 | |
| 2032 | if (shmem_huge != SHMEM_HUGE_FORCE) { |
| 2033 | struct super_block *sb; |
| 2034 | |
| 2035 | if (file) { |
| 2036 | VM_BUG_ON(file->f_op != &shmem_file_operations); |
| 2037 | sb = file_inode(file)->i_sb; |
| 2038 | } else { |
| 2039 | /* |
| 2040 | * Called directly from mm/mmap.c, or drivers/char/mem.c |
| 2041 | * for "/dev/zero", to create a shared anonymous object. |
| 2042 | */ |
| 2043 | if (IS_ERR(shm_mnt)) |
| 2044 | return addr; |
| 2045 | sb = shm_mnt->mnt_sb; |
| 2046 | } |
Toshi Kani | 3089bf6 | 2016-09-23 20:21:56 -0700 | [diff] [blame] | 2047 | if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2048 | return addr; |
| 2049 | } |
| 2050 | |
| 2051 | offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); |
| 2052 | if (offset && offset + len < 2 * HPAGE_PMD_SIZE) |
| 2053 | return addr; |
| 2054 | if ((addr & (HPAGE_PMD_SIZE-1)) == offset) |
| 2055 | return addr; |
| 2056 | |
| 2057 | inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; |
| 2058 | if (inflated_len > TASK_SIZE) |
| 2059 | return addr; |
| 2060 | if (inflated_len < len) |
| 2061 | return addr; |
| 2062 | |
| 2063 | inflated_addr = get_area(NULL, 0, inflated_len, 0, flags); |
| 2064 | if (IS_ERR_VALUE(inflated_addr)) |
| 2065 | return addr; |
| 2066 | if (inflated_addr & ~PAGE_MASK) |
| 2067 | return addr; |
| 2068 | |
| 2069 | inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); |
| 2070 | inflated_addr += offset - inflated_offset; |
| 2071 | if (inflated_offset > offset) |
| 2072 | inflated_addr += HPAGE_PMD_SIZE; |
| 2073 | |
| 2074 | if (inflated_addr > TASK_SIZE - len) |
| 2075 | return addr; |
| 2076 | return inflated_addr; |
| 2077 | } |
| 2078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | #ifdef CONFIG_NUMA |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2080 | static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2082 | struct inode *inode = file_inode(vma->vm_file); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2083 | return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Adrian Bunk | d8dc74f | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 2086 | static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, |
| 2087 | unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2089 | struct inode *inode = file_inode(vma->vm_file); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2090 | pgoff_t index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2092 | index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 2093 | return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
| 2095 | #endif |
| 2096 | |
| 2097 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
| 2098 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2099 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 2101 | int retval = -ENOMEM; |
| 2102 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 2103 | spin_lock_irq(&info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | if (lock && !(info->flags & VM_LOCKED)) { |
| 2105 | if (!user_shm_lock(inode->i_size, user)) |
| 2106 | goto out_nomem; |
| 2107 | info->flags |= VM_LOCKED; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2108 | mapping_set_unevictable(file->f_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | } |
| 2110 | if (!lock && (info->flags & VM_LOCKED) && user) { |
| 2111 | user_shm_unlock(inode->i_size, user); |
| 2112 | info->flags &= ~VM_LOCKED; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2113 | mapping_clear_unevictable(file->f_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
| 2115 | retval = 0; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | out_nomem: |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 2118 | spin_unlock_irq(&info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | return retval; |
| 2120 | } |
| 2121 | |
Adrian Bunk | 9b83a6a | 2007-02-28 20:11:03 -0800 | [diff] [blame] | 2122 | static int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | { |
| 2124 | file_accessed(file); |
| 2125 | vma->vm_ops = &shmem_vm_ops; |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 2126 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2127 | ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < |
| 2128 | (vma->vm_end & HPAGE_PMD_MASK)) { |
| 2129 | khugepaged_enter(vma, vma->vm_flags); |
| 2130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | return 0; |
| 2132 | } |
| 2133 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2134 | static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, |
Al Viro | 09208d1 | 2011-07-26 03:15:03 -0400 | [diff] [blame] | 2135 | umode_t mode, dev_t dev, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | { |
| 2137 | struct inode *inode; |
| 2138 | struct shmem_inode_info *info; |
| 2139 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 2140 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2141 | if (shmem_reserve_inode(sb)) |
| 2142 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | |
| 2144 | inode = new_inode(sb); |
| 2145 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 2146 | inode->i_ino = get_next_ino(); |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2147 | inode_init_owner(inode, dir, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | inode->i_blocks = 0; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2149 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
Arnd Bergmann | 46c9a94 | 2018-08-17 15:45:09 -0700 | [diff] [blame] | 2150 | inode->i_generation = prandom_u32(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | info = SHMEM_I(inode); |
| 2152 | memset(info, 0, (char *)inode - (char *)info); |
| 2153 | spin_lock_init(&info->lock); |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2154 | info->seals = F_SEAL_SEAL; |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 2155 | info->flags = flags & VM_NORESERVE; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 2156 | INIT_LIST_HEAD(&info->shrinklist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | INIT_LIST_HEAD(&info->swaplist); |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 2158 | simple_xattrs_init(&info->xattrs); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2159 | cache_no_acl(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | |
| 2161 | switch (mode & S_IFMT) { |
| 2162 | default: |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2163 | inode->i_op = &shmem_special_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | init_special_inode(inode, mode, dev); |
| 2165 | break; |
| 2166 | case S_IFREG: |
Hugh Dickins | 14fcc23 | 2008-07-28 15:46:19 -0700 | [diff] [blame] | 2167 | inode->i_mapping->a_ops = &shmem_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | inode->i_op = &shmem_inode_operations; |
| 2169 | inode->i_fop = &shmem_file_operations; |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 2170 | mpol_shared_policy_init(&info->policy, |
| 2171 | shmem_get_sbmpol(sbinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | break; |
| 2173 | case S_IFDIR: |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2174 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | /* Some things misbehave if size == 0 on a directory */ |
| 2176 | inode->i_size = 2 * BOGO_DIRENT_SIZE; |
| 2177 | inode->i_op = &shmem_dir_inode_operations; |
| 2178 | inode->i_fop = &simple_dir_operations; |
| 2179 | break; |
| 2180 | case S_IFLNK: |
| 2181 | /* |
| 2182 | * Must not load anything in the rbtree, |
| 2183 | * mpol_free_shared_policy will not be called. |
| 2184 | */ |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 2185 | mpol_shared_policy_init(&info->policy, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | break; |
| 2187 | } |
Joel Fernandes (Google) | b45d71f | 2018-09-20 12:22:39 -0700 | [diff] [blame] | 2188 | |
| 2189 | lockdep_annotate_inode_mutex_key(inode); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2190 | } else |
| 2191 | shmem_free_inode(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | return inode; |
| 2193 | } |
| 2194 | |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2195 | bool shmem_mapping(struct address_space *mapping) |
| 2196 | { |
Hugh Dickins | f800545 | 2017-02-22 15:41:42 -0800 | [diff] [blame] | 2197 | return mapping->a_ops == &shmem_aops; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2200 | static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, |
| 2201 | pmd_t *dst_pmd, |
| 2202 | struct vm_area_struct *dst_vma, |
| 2203 | unsigned long dst_addr, |
| 2204 | unsigned long src_addr, |
| 2205 | bool zeropage, |
| 2206 | struct page **pagep) |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2207 | { |
| 2208 | struct inode *inode = file_inode(dst_vma->vm_file); |
| 2209 | struct shmem_inode_info *info = SHMEM_I(inode); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2210 | struct address_space *mapping = inode->i_mapping; |
| 2211 | gfp_t gfp = mapping_gfp_mask(mapping); |
| 2212 | pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); |
| 2213 | struct mem_cgroup *memcg; |
| 2214 | spinlock_t *ptl; |
| 2215 | void *page_kaddr; |
| 2216 | struct page *page; |
| 2217 | pte_t _dst_pte, *dst_pte; |
| 2218 | int ret; |
| 2219 | |
Andrea Arcangeli | cb658a4 | 2017-02-22 15:43:55 -0800 | [diff] [blame] | 2220 | ret = -ENOMEM; |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 2221 | if (!shmem_inode_acct_block(inode, 1)) |
Andrea Arcangeli | cb658a4 | 2017-02-22 15:43:55 -0800 | [diff] [blame] | 2222 | goto out; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2223 | |
Andrea Arcangeli | cb658a4 | 2017-02-22 15:43:55 -0800 | [diff] [blame] | 2224 | if (!*pagep) { |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2225 | page = shmem_alloc_page(gfp, info, pgoff); |
| 2226 | if (!page) |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 2227 | goto out_unacct_blocks; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2228 | |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2229 | if (!zeropage) { /* mcopy_atomic */ |
| 2230 | page_kaddr = kmap_atomic(page); |
| 2231 | ret = copy_from_user(page_kaddr, |
| 2232 | (const void __user *)src_addr, |
| 2233 | PAGE_SIZE); |
| 2234 | kunmap_atomic(page_kaddr); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2235 | |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2236 | /* fallback to copy_from_user outside mmap_sem */ |
| 2237 | if (unlikely(ret)) { |
| 2238 | *pagep = page; |
| 2239 | shmem_inode_unacct_blocks(inode, 1); |
| 2240 | /* don't free the page */ |
| 2241 | return -EFAULT; |
| 2242 | } |
| 2243 | } else { /* mfill_zeropage_atomic */ |
| 2244 | clear_highpage(page); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2245 | } |
| 2246 | } else { |
| 2247 | page = *pagep; |
| 2248 | *pagep = NULL; |
| 2249 | } |
| 2250 | |
Andrea Arcangeli | 9cc90c6 | 2017-02-22 15:43:49 -0800 | [diff] [blame] | 2251 | VM_BUG_ON(PageLocked(page) || PageSwapBacked(page)); |
| 2252 | __SetPageLocked(page); |
| 2253 | __SetPageSwapBacked(page); |
Andrea Arcangeli | a425d35 | 2017-02-22 15:43:52 -0800 | [diff] [blame] | 2254 | __SetPageUptodate(page); |
Andrea Arcangeli | 9cc90c6 | 2017-02-22 15:43:49 -0800 | [diff] [blame] | 2255 | |
Tejun Heo | 2cf8558 | 2018-07-03 11:14:56 -0400 | [diff] [blame] | 2256 | ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2257 | if (ret) |
| 2258 | goto out_release; |
| 2259 | |
Matthew Wilcox | 552446a | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 2260 | ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL, |
| 2261 | gfp & GFP_RECLAIM_MASK); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2262 | if (ret) |
| 2263 | goto out_release_uncharge; |
| 2264 | |
| 2265 | mem_cgroup_commit_charge(page, memcg, false, false); |
| 2266 | |
| 2267 | _dst_pte = mk_pte(page, dst_vma->vm_page_prot); |
| 2268 | if (dst_vma->vm_flags & VM_WRITE) |
| 2269 | _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); |
| 2270 | |
| 2271 | ret = -EEXIST; |
| 2272 | dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); |
| 2273 | if (!pte_none(*dst_pte)) |
| 2274 | goto out_release_uncharge_unlock; |
| 2275 | |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2276 | lru_cache_add_anon(page); |
| 2277 | |
| 2278 | spin_lock(&info->lock); |
| 2279 | info->alloced++; |
| 2280 | inode->i_blocks += BLOCKS_PER_PAGE; |
| 2281 | shmem_recalc_inode(inode); |
| 2282 | spin_unlock(&info->lock); |
| 2283 | |
| 2284 | inc_mm_counter(dst_mm, mm_counter_file(page)); |
| 2285 | page_add_file_rmap(page, false); |
| 2286 | set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); |
| 2287 | |
| 2288 | /* No need to invalidate - it was non-present before */ |
| 2289 | update_mmu_cache(dst_vma, dst_addr, dst_pte); |
| 2290 | unlock_page(page); |
| 2291 | pte_unmap_unlock(dst_pte, ptl); |
| 2292 | ret = 0; |
| 2293 | out: |
| 2294 | return ret; |
| 2295 | out_release_uncharge_unlock: |
| 2296 | pte_unmap_unlock(dst_pte, ptl); |
| 2297 | out_release_uncharge: |
| 2298 | mem_cgroup_cancel_charge(page, memcg, false); |
| 2299 | out_release: |
Andrea Arcangeli | 9cc90c6 | 2017-02-22 15:43:49 -0800 | [diff] [blame] | 2300 | unlock_page(page); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2301 | put_page(page); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2302 | out_unacct_blocks: |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 2303 | shmem_inode_unacct_blocks(inode, 1); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2304 | goto out; |
| 2305 | } |
| 2306 | |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2307 | int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, |
| 2308 | pmd_t *dst_pmd, |
| 2309 | struct vm_area_struct *dst_vma, |
| 2310 | unsigned long dst_addr, |
| 2311 | unsigned long src_addr, |
| 2312 | struct page **pagep) |
| 2313 | { |
| 2314 | return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, |
| 2315 | dst_addr, src_addr, false, pagep); |
| 2316 | } |
| 2317 | |
| 2318 | int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, |
| 2319 | pmd_t *dst_pmd, |
| 2320 | struct vm_area_struct *dst_vma, |
| 2321 | unsigned long dst_addr) |
| 2322 | { |
| 2323 | struct page *page = NULL; |
| 2324 | |
| 2325 | return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, |
| 2326 | dst_addr, 0, true, &page); |
| 2327 | } |
| 2328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | #ifdef CONFIG_TMPFS |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2330 | static const struct inode_operations shmem_symlink_inode_operations; |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 2331 | static const struct inode_operations shmem_short_symlink_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 2333 | #ifdef CONFIG_TMPFS_XATTR |
| 2334 | static int shmem_initxattrs(struct inode *, const struct xattr *, void *); |
| 2335 | #else |
| 2336 | #define shmem_initxattrs NULL |
| 2337 | #endif |
| 2338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | static int |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2340 | shmem_write_begin(struct file *file, struct address_space *mapping, |
| 2341 | loff_t pos, unsigned len, unsigned flags, |
| 2342 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | { |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2344 | struct inode *inode = mapping->host; |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2345 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2346 | pgoff_t index = pos >> PAGE_SHIFT; |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2347 | |
| 2348 | /* i_mutex is held by caller */ |
Steven Rostedt (VMware) | 3f472cc | 2017-02-24 14:59:10 -0800 | [diff] [blame] | 2349 | if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) { |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2350 | if (info->seals & F_SEAL_WRITE) |
| 2351 | return -EPERM; |
| 2352 | if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) |
| 2353 | return -EPERM; |
| 2354 | } |
| 2355 | |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2356 | return shmem_getpage(inode, index, pagep, SGP_WRITE); |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | static int |
| 2360 | shmem_write_end(struct file *file, struct address_space *mapping, |
| 2361 | loff_t pos, unsigned len, unsigned copied, |
| 2362 | struct page *page, void *fsdata) |
| 2363 | { |
| 2364 | struct inode *inode = mapping->host; |
| 2365 | |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 2366 | if (pos + copied > inode->i_size) |
| 2367 | i_size_write(inode, pos + copied); |
| 2368 | |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2369 | if (!PageUptodate(page)) { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 2370 | struct page *head = compound_head(page); |
| 2371 | if (PageTransCompound(page)) { |
| 2372 | int i; |
| 2373 | |
| 2374 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
| 2375 | if (head + i == page) |
| 2376 | continue; |
| 2377 | clear_highpage(head + i); |
| 2378 | flush_dcache_page(head + i); |
| 2379 | } |
| 2380 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2381 | if (copied < PAGE_SIZE) { |
| 2382 | unsigned from = pos & (PAGE_SIZE - 1); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2383 | zero_user_segments(page, 0, from, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2384 | from + copied, PAGE_SIZE); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2385 | } |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 2386 | SetPageUptodate(head); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2387 | } |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2388 | set_page_dirty(page); |
Wu Fengguang | 6746aff | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2389 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2390 | put_page(page); |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2391 | |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2392 | return copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 2395 | static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | { |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2397 | struct file *file = iocb->ki_filp; |
| 2398 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | struct address_space *mapping = inode->i_mapping; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2400 | pgoff_t index; |
| 2401 | unsigned long offset; |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 2402 | enum sgp_type sgp = SGP_READ; |
Geert Uytterhoeven | f7c1d07 | 2014-04-13 20:46:22 +0200 | [diff] [blame] | 2403 | int error = 0; |
Al Viro | cb66a7a | 2014-03-04 15:24:06 -0500 | [diff] [blame] | 2404 | ssize_t retval = 0; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2405 | loff_t *ppos = &iocb->ki_pos; |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 2406 | |
| 2407 | /* |
| 2408 | * Might this read be for a stacking filesystem? Then when reading |
| 2409 | * holes of a sparse file, we actually need to allocate those pages, |
| 2410 | * and even mark them dirty, so it cannot exceed the max_blocks limit. |
| 2411 | */ |
Al Viro | 777eda2 | 2014-12-17 04:46:46 -0500 | [diff] [blame] | 2412 | if (!iter_is_iovec(to)) |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 2413 | sgp = SGP_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2415 | index = *ppos >> PAGE_SHIFT; |
| 2416 | offset = *ppos & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
| 2418 | for (;;) { |
| 2419 | struct page *page = NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2420 | pgoff_t end_index; |
| 2421 | unsigned long nr, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | loff_t i_size = i_size_read(inode); |
| 2423 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2424 | end_index = i_size >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | if (index > end_index) |
| 2426 | break; |
| 2427 | if (index == end_index) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2428 | nr = i_size & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | if (nr <= offset) |
| 2430 | break; |
| 2431 | } |
| 2432 | |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2433 | error = shmem_getpage(inode, index, &page, sgp); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2434 | if (error) { |
| 2435 | if (error == -EINVAL) |
| 2436 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | break; |
| 2438 | } |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 2439 | if (page) { |
| 2440 | if (sgp == SGP_CACHE) |
| 2441 | set_page_dirty(page); |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 2442 | unlock_page(page); |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 2443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | |
| 2445 | /* |
| 2446 | * We must evaluate after, since reads (unlike writes) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2447 | * are called without i_mutex protection against truncate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2449 | nr = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2451 | end_index = i_size >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | if (index == end_index) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2453 | nr = i_size & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | if (nr <= offset) { |
| 2455 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2456 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | break; |
| 2458 | } |
| 2459 | } |
| 2460 | nr -= offset; |
| 2461 | |
| 2462 | if (page) { |
| 2463 | /* |
| 2464 | * If users can be writing to this page using arbitrary |
| 2465 | * virtual addresses, take care about potential aliasing |
| 2466 | * before reading the page on the kernel side. |
| 2467 | */ |
| 2468 | if (mapping_writably_mapped(mapping)) |
| 2469 | flush_dcache_page(page); |
| 2470 | /* |
| 2471 | * Mark the page accessed if we read the beginning. |
| 2472 | */ |
| 2473 | if (!offset) |
| 2474 | mark_page_accessed(page); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 2475 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | page = ZERO_PAGE(0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2477 | get_page(page); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 2478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | |
| 2480 | /* |
| 2481 | * Ok, we have the page, and it's up-to-date, so |
| 2482 | * now we can copy it to user space... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | */ |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 2484 | ret = copy_page_to_iter(page, offset, nr, to); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2485 | retval += ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | offset += ret; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2487 | index += offset >> PAGE_SHIFT; |
| 2488 | offset &= ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2490 | put_page(page); |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 2491 | if (!iov_iter_count(to)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | break; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2493 | if (ret < nr) { |
| 2494 | error = -EFAULT; |
| 2495 | break; |
| 2496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | cond_resched(); |
| 2498 | } |
| 2499 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2500 | *ppos = ((loff_t) index << PAGE_SHIFT) + offset; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2501 | file_accessed(file); |
| 2502 | return retval ? retval : error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2505 | /* |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 2506 | * llseek SEEK_DATA or SEEK_HOLE through the page cache. |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2507 | */ |
| 2508 | static pgoff_t shmem_seek_hole_data(struct address_space *mapping, |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2509 | pgoff_t index, pgoff_t end, int whence) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2510 | { |
| 2511 | struct page *page; |
| 2512 | struct pagevec pvec; |
| 2513 | pgoff_t indices[PAGEVEC_SIZE]; |
| 2514 | bool done = false; |
| 2515 | int i; |
| 2516 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 2517 | pagevec_init(&pvec); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2518 | pvec.nr = 1; /* start small: we may be there already */ |
| 2519 | while (!done) { |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2520 | pvec.nr = find_get_entries(mapping, index, |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2521 | pvec.nr, pvec.pages, indices); |
| 2522 | if (!pvec.nr) { |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2523 | if (whence == SEEK_DATA) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2524 | index = end; |
| 2525 | break; |
| 2526 | } |
| 2527 | for (i = 0; i < pvec.nr; i++, index++) { |
| 2528 | if (index < indices[i]) { |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2529 | if (whence == SEEK_HOLE) { |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2530 | done = true; |
| 2531 | break; |
| 2532 | } |
| 2533 | index = indices[i]; |
| 2534 | } |
| 2535 | page = pvec.pages[i]; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 2536 | if (page && !xa_is_value(page)) { |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2537 | if (!PageUptodate(page)) |
| 2538 | page = NULL; |
| 2539 | } |
| 2540 | if (index >= end || |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2541 | (page && whence == SEEK_DATA) || |
| 2542 | (!page && whence == SEEK_HOLE)) { |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2543 | done = true; |
| 2544 | break; |
| 2545 | } |
| 2546 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2547 | pagevec_remove_exceptionals(&pvec); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2548 | pagevec_release(&pvec); |
| 2549 | pvec.nr = PAGEVEC_SIZE; |
| 2550 | cond_resched(); |
| 2551 | } |
| 2552 | return index; |
| 2553 | } |
| 2554 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2555 | static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2556 | { |
| 2557 | struct address_space *mapping = file->f_mapping; |
| 2558 | struct inode *inode = mapping->host; |
| 2559 | pgoff_t start, end; |
| 2560 | loff_t new_offset; |
| 2561 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2562 | if (whence != SEEK_DATA && whence != SEEK_HOLE) |
| 2563 | return generic_file_llseek_size(file, offset, whence, |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2564 | MAX_LFS_FILESIZE, i_size_read(inode)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2565 | inode_lock(inode); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2566 | /* We're holding i_mutex so we can access i_size directly */ |
| 2567 | |
Yufen Yu | 1a41364 | 2018-11-16 15:08:39 -0800 | [diff] [blame] | 2568 | if (offset < 0 || offset >= inode->i_size) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2569 | offset = -ENXIO; |
| 2570 | else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2571 | start = offset >> PAGE_SHIFT; |
| 2572 | end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2573 | new_offset = shmem_seek_hole_data(mapping, start, end, whence); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2574 | new_offset <<= PAGE_SHIFT; |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2575 | if (new_offset > offset) { |
| 2576 | if (new_offset < inode->i_size) |
| 2577 | offset = new_offset; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2578 | else if (whence == SEEK_DATA) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2579 | offset = -ENXIO; |
| 2580 | else |
| 2581 | offset = inode->i_size; |
| 2582 | } |
| 2583 | } |
| 2584 | |
Hugh Dickins | 387aae6 | 2013-08-04 11:30:25 -0700 | [diff] [blame] | 2585 | if (offset >= 0) |
| 2586 | offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2587 | inode_unlock(inode); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2588 | return offset; |
| 2589 | } |
| 2590 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2591 | static long shmem_fallocate(struct file *file, int mode, loff_t offset, |
| 2592 | loff_t len) |
| 2593 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2594 | struct inode *inode = file_inode(file); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2595 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2596 | struct shmem_inode_info *info = SHMEM_I(inode); |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2597 | struct shmem_falloc shmem_falloc; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2598 | pgoff_t start, index, end; |
| 2599 | int error; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2600 | |
Hugh Dickins | 13ace4d | 2014-06-23 13:22:03 -0700 | [diff] [blame] | 2601 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 2602 | return -EOPNOTSUPP; |
| 2603 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2604 | inode_lock(inode); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2605 | |
| 2606 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
| 2607 | struct address_space *mapping = file->f_mapping; |
| 2608 | loff_t unmap_start = round_up(offset, PAGE_SIZE); |
| 2609 | loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2610 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2611 | |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2612 | /* protected by i_mutex */ |
| 2613 | if (info->seals & F_SEAL_WRITE) { |
| 2614 | error = -EPERM; |
| 2615 | goto out; |
| 2616 | } |
| 2617 | |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2618 | shmem_falloc.waitq = &shmem_falloc_waitq; |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2619 | shmem_falloc.start = unmap_start >> PAGE_SHIFT; |
| 2620 | shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; |
| 2621 | spin_lock(&inode->i_lock); |
| 2622 | inode->i_private = &shmem_falloc; |
| 2623 | spin_unlock(&inode->i_lock); |
| 2624 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2625 | if ((u64)unmap_end > (u64)unmap_start) |
| 2626 | unmap_mapping_range(mapping, unmap_start, |
| 2627 | 1 + unmap_end - unmap_start, 0); |
| 2628 | shmem_truncate_range(inode, offset, offset + len - 1); |
| 2629 | /* No need to unmap again: hole-punching leaves COWed pages */ |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2630 | |
| 2631 | spin_lock(&inode->i_lock); |
| 2632 | inode->i_private = NULL; |
| 2633 | wake_up_all(&shmem_falloc_waitq); |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 2634 | WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2635 | spin_unlock(&inode->i_lock); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2636 | error = 0; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2637 | goto out; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2640 | /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ |
| 2641 | error = inode_newsize_ok(inode, offset + len); |
| 2642 | if (error) |
| 2643 | goto out; |
| 2644 | |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2645 | if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { |
| 2646 | error = -EPERM; |
| 2647 | goto out; |
| 2648 | } |
| 2649 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2650 | start = offset >> PAGE_SHIFT; |
| 2651 | end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2652 | /* Try to avoid a swapstorm if len is impossible to satisfy */ |
| 2653 | if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { |
| 2654 | error = -ENOSPC; |
| 2655 | goto out; |
| 2656 | } |
| 2657 | |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2658 | shmem_falloc.waitq = NULL; |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2659 | shmem_falloc.start = start; |
| 2660 | shmem_falloc.next = start; |
| 2661 | shmem_falloc.nr_falloced = 0; |
| 2662 | shmem_falloc.nr_unswapped = 0; |
| 2663 | spin_lock(&inode->i_lock); |
| 2664 | inode->i_private = &shmem_falloc; |
| 2665 | spin_unlock(&inode->i_lock); |
| 2666 | |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2667 | for (index = start; index < end; index++) { |
| 2668 | struct page *page; |
| 2669 | |
| 2670 | /* |
| 2671 | * Good, the fallocate(2) manpage permits EINTR: we may have |
| 2672 | * been interrupted because we are using up too much memory. |
| 2673 | */ |
| 2674 | if (signal_pending(current)) |
| 2675 | error = -EINTR; |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2676 | else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) |
| 2677 | error = -ENOMEM; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2678 | else |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2679 | error = shmem_getpage(inode, index, &page, SGP_FALLOC); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2680 | if (error) { |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2681 | /* Remove the !PageUptodate pages we added */ |
Hugh Dickins | 7f55656 | 2016-07-10 16:46:32 -0700 | [diff] [blame] | 2682 | if (index > start) { |
| 2683 | shmem_undo_range(inode, |
| 2684 | (loff_t)start << PAGE_SHIFT, |
| 2685 | ((loff_t)index << PAGE_SHIFT) - 1, true); |
| 2686 | } |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2687 | goto undone; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2688 | } |
| 2689 | |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2690 | /* |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2691 | * Inform shmem_writepage() how far we have reached. |
| 2692 | * No need for lock or barrier: we have the page lock. |
| 2693 | */ |
| 2694 | shmem_falloc.next++; |
| 2695 | if (!PageUptodate(page)) |
| 2696 | shmem_falloc.nr_falloced++; |
| 2697 | |
| 2698 | /* |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2699 | * If !PageUptodate, leave it that way so that freeable pages |
| 2700 | * can be recognized if we need to rollback on error later. |
| 2701 | * But set_page_dirty so that memory pressure will swap rather |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2702 | * than free the pages we are allocating (and SGP_CACHE pages |
| 2703 | * might still be clean: we now need to mark those dirty too). |
| 2704 | */ |
| 2705 | set_page_dirty(page); |
| 2706 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2707 | put_page(page); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2708 | cond_resched(); |
| 2709 | } |
| 2710 | |
| 2711 | if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) |
| 2712 | i_size_write(inode, offset + len); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2713 | inode->i_ctime = current_time(inode); |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2714 | undone: |
| 2715 | spin_lock(&inode->i_lock); |
| 2716 | inode->i_private = NULL; |
| 2717 | spin_unlock(&inode->i_lock); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2718 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2719 | inode_unlock(inode); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2720 | return error; |
| 2721 | } |
| 2722 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2723 | static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2725 | struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | |
| 2727 | buf->f_type = TMPFS_MAGIC; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2728 | buf->f_bsize = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | buf->f_namelen = NAME_MAX; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 2730 | if (sbinfo->max_blocks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | buf->f_blocks = sbinfo->max_blocks; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2732 | buf->f_bavail = |
| 2733 | buf->f_bfree = sbinfo->max_blocks - |
| 2734 | percpu_counter_sum(&sbinfo->used_blocks); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 2735 | } |
| 2736 | if (sbinfo->max_inodes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | buf->f_files = sbinfo->max_inodes; |
| 2738 | buf->f_ffree = sbinfo->free_inodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | } |
| 2740 | /* else leave those fields 0 like simple_statfs */ |
| 2741 | return 0; |
| 2742 | } |
| 2743 | |
| 2744 | /* |
| 2745 | * File creation. Allocate an inode, and we're done.. |
| 2746 | */ |
| 2747 | static int |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2748 | shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 2750 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | int error = -ENOSPC; |
| 2752 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2753 | inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | if (inode) { |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2755 | error = simple_acl_create(dir, inode); |
| 2756 | if (error) |
| 2757 | goto out_iput; |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2758 | error = security_inode_init_security(inode, dir, |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 2759 | &dentry->d_name, |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 2760 | shmem_initxattrs, NULL); |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2761 | if (error && error != -EOPNOTSUPP) |
| 2762 | goto out_iput; |
Mimi Zohar | 37ec43c | 2013-04-14 09:21:47 -0400 | [diff] [blame] | 2763 | |
Al Viro | 718deb6 | 2009-12-16 19:35:36 -0500 | [diff] [blame] | 2764 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | dir->i_size += BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2766 | dir->i_ctime = dir->i_mtime = current_time(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | d_instantiate(dentry, inode); |
| 2768 | dget(dentry); /* Extra count - pin the dentry in core */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | } |
| 2770 | return error; |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2771 | out_iput: |
| 2772 | iput(inode); |
| 2773 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 2776 | static int |
| 2777 | shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 2778 | { |
| 2779 | struct inode *inode; |
| 2780 | int error = -ENOSPC; |
| 2781 | |
| 2782 | inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); |
| 2783 | if (inode) { |
| 2784 | error = security_inode_init_security(inode, dir, |
| 2785 | NULL, |
| 2786 | shmem_initxattrs, NULL); |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2787 | if (error && error != -EOPNOTSUPP) |
| 2788 | goto out_iput; |
| 2789 | error = simple_acl_create(dir, inode); |
| 2790 | if (error) |
| 2791 | goto out_iput; |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 2792 | d_tmpfile(dentry, inode); |
| 2793 | } |
| 2794 | return error; |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2795 | out_iput: |
| 2796 | iput(inode); |
| 2797 | return error; |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 2798 | } |
| 2799 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2800 | static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | { |
| 2802 | int error; |
| 2803 | |
| 2804 | if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) |
| 2805 | return error; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2806 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | return 0; |
| 2808 | } |
| 2809 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2810 | static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 2811 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | { |
| 2813 | return shmem_mknod(dir, dentry, mode | S_IFREG, 0); |
| 2814 | } |
| 2815 | |
| 2816 | /* |
| 2817 | * Link a file.. |
| 2818 | */ |
| 2819 | static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 2820 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2821 | struct inode *inode = d_inode(old_dentry); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2822 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | |
| 2824 | /* |
| 2825 | * No ordinary (disk based) filesystem counts links as inodes; |
| 2826 | * but each new link needs a new dentry, pinning lowmem, and |
| 2827 | * tmpfs dentries cannot be pruned until they are unlinked. |
| 2828 | */ |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2829 | ret = shmem_reserve_inode(inode->i_sb); |
| 2830 | if (ret) |
| 2831 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
| 2833 | dir->i_size += BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2834 | inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2835 | inc_nlink(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 2836 | ihold(inode); /* New dentry reference */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | dget(dentry); /* Extra pinning count for the created dentry */ |
| 2838 | d_instantiate(dentry, inode); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2839 | out: |
| 2840 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | static int shmem_unlink(struct inode *dir, struct dentry *dentry) |
| 2844 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2845 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2847 | if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) |
| 2848 | shmem_free_inode(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | |
| 2850 | dir->i_size -= BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2851 | inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2852 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | dput(dentry); /* Undo the count from "create" - this does all the work */ |
| 2854 | return 0; |
| 2855 | } |
| 2856 | |
| 2857 | static int shmem_rmdir(struct inode *dir, struct dentry *dentry) |
| 2858 | { |
| 2859 | if (!simple_empty(dentry)) |
| 2860 | return -ENOTEMPTY; |
| 2861 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2862 | drop_nlink(d_inode(dentry)); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2863 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | return shmem_unlink(dir, dentry); |
| 2865 | } |
| 2866 | |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 2867 | static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) |
| 2868 | { |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 2869 | bool old_is_dir = d_is_dir(old_dentry); |
| 2870 | bool new_is_dir = d_is_dir(new_dentry); |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 2871 | |
| 2872 | if (old_dir != new_dir && old_is_dir != new_is_dir) { |
| 2873 | if (old_is_dir) { |
| 2874 | drop_nlink(old_dir); |
| 2875 | inc_nlink(new_dir); |
| 2876 | } else { |
| 2877 | drop_nlink(new_dir); |
| 2878 | inc_nlink(old_dir); |
| 2879 | } |
| 2880 | } |
| 2881 | old_dir->i_ctime = old_dir->i_mtime = |
| 2882 | new_dir->i_ctime = new_dir->i_mtime = |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2883 | d_inode(old_dentry)->i_ctime = |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2884 | d_inode(new_dentry)->i_ctime = current_time(old_dir); |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 2885 | |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
Miklos Szeredi | 46fdb79 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 2889 | static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) |
| 2890 | { |
| 2891 | struct dentry *whiteout; |
| 2892 | int error; |
| 2893 | |
| 2894 | whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); |
| 2895 | if (!whiteout) |
| 2896 | return -ENOMEM; |
| 2897 | |
| 2898 | error = shmem_mknod(old_dir, whiteout, |
| 2899 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); |
| 2900 | dput(whiteout); |
| 2901 | if (error) |
| 2902 | return error; |
| 2903 | |
| 2904 | /* |
| 2905 | * Cheat and hash the whiteout while the old dentry is still in |
| 2906 | * place, instead of playing games with FS_RENAME_DOES_D_MOVE. |
| 2907 | * |
| 2908 | * d_lookup() will consistently find one of them at this point, |
| 2909 | * not sure which one, but that isn't even important. |
| 2910 | */ |
| 2911 | d_rehash(whiteout); |
| 2912 | return 0; |
| 2913 | } |
| 2914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | /* |
| 2916 | * The VFS layer already does all the dentry stuff for rename, |
| 2917 | * we just have to decrement the usage count for the target if |
| 2918 | * it exists so that the VFS layer correctly free's it when it |
| 2919 | * gets overwritten. |
| 2920 | */ |
Miklos Szeredi | 3b69ff5 | 2014-07-23 15:15:33 +0200 | [diff] [blame] | 2921 | static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2923 | struct inode *inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | int they_are_dirs = S_ISDIR(inode->i_mode); |
| 2925 | |
Miklos Szeredi | 46fdb79 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 2926 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 3b69ff5 | 2014-07-23 15:15:33 +0200 | [diff] [blame] | 2927 | return -EINVAL; |
| 2928 | |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 2929 | if (flags & RENAME_EXCHANGE) |
| 2930 | return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); |
| 2931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | if (!simple_empty(new_dentry)) |
| 2933 | return -ENOTEMPTY; |
| 2934 | |
Miklos Szeredi | 46fdb79 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 2935 | if (flags & RENAME_WHITEOUT) { |
| 2936 | int error; |
| 2937 | |
| 2938 | error = shmem_whiteout(old_dir, old_dentry); |
| 2939 | if (error) |
| 2940 | return error; |
| 2941 | } |
| 2942 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2943 | if (d_really_is_positive(new_dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | (void) shmem_unlink(new_dir, new_dentry); |
Miklos Szeredi | b928095 | 2014-09-24 17:56:17 +0200 | [diff] [blame] | 2945 | if (they_are_dirs) { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2946 | drop_nlink(d_inode(new_dentry)); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2947 | drop_nlink(old_dir); |
Miklos Szeredi | b928095 | 2014-09-24 17:56:17 +0200 | [diff] [blame] | 2948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | } else if (they_are_dirs) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2950 | drop_nlink(old_dir); |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2951 | inc_nlink(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | old_dir->i_size -= BOGO_DIRENT_SIZE; |
| 2955 | new_dir->i_size += BOGO_DIRENT_SIZE; |
| 2956 | old_dir->i_ctime = old_dir->i_mtime = |
| 2957 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2958 | inode->i_ctime = current_time(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | return 0; |
| 2960 | } |
| 2961 | |
| 2962 | static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
| 2963 | { |
| 2964 | int error; |
| 2965 | int len; |
| 2966 | struct inode *inode; |
Hugh Dickins | 9276aad | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 2967 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | |
| 2969 | len = strlen(symname) + 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2970 | if (len > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | return -ENAMETOOLONG; |
| 2972 | |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 2973 | inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, |
| 2974 | VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | if (!inode) |
| 2976 | return -ENOSPC; |
| 2977 | |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 2978 | error = security_inode_init_security(inode, dir, &dentry->d_name, |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 2979 | shmem_initxattrs, NULL); |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2980 | if (error) { |
| 2981 | if (error != -EOPNOTSUPP) { |
| 2982 | iput(inode); |
| 2983 | return error; |
| 2984 | } |
| 2985 | error = 0; |
| 2986 | } |
| 2987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | inode->i_size = len-1; |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 2989 | if (len <= SHORT_SYMLINK_LEN) { |
Al Viro | 3ed47db | 2016-01-22 18:08:52 -0500 | [diff] [blame] | 2990 | inode->i_link = kmemdup(symname, len, GFP_KERNEL); |
| 2991 | if (!inode->i_link) { |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 2992 | iput(inode); |
| 2993 | return -ENOMEM; |
| 2994 | } |
| 2995 | inode->i_op = &shmem_short_symlink_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | } else { |
Al Viro | e8ecde2 | 2016-01-14 17:52:59 -0500 | [diff] [blame] | 2997 | inode_nohighmem(inode); |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2998 | error = shmem_getpage(inode, 0, &page, SGP_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | if (error) { |
| 3000 | iput(inode); |
| 3001 | return error; |
| 3002 | } |
Hugh Dickins | 14fcc23 | 2008-07-28 15:46:19 -0700 | [diff] [blame] | 3003 | inode->i_mapping->a_ops = &shmem_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | inode->i_op = &shmem_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3005 | memcpy(page_address(page), symname, len); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 3006 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | set_page_dirty(page); |
Wu Fengguang | 6746aff | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 3008 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3009 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | dir->i_size += BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 3012 | dir->i_ctime = dir->i_mtime = current_time(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | d_instantiate(dentry, inode); |
| 3014 | dget(dentry); |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3018 | static void shmem_put_link(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3020 | mark_page_accessed(arg); |
| 3021 | put_page(arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | } |
| 3023 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3024 | static const char *shmem_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3025 | struct inode *inode, |
| 3026 | struct delayed_call *done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | struct page *page = NULL; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3029 | int error; |
Al Viro | 6a6c990 | 2015-11-17 10:54:32 -0500 | [diff] [blame] | 3030 | if (!dentry) { |
| 3031 | page = find_get_page(inode->i_mapping, 0); |
| 3032 | if (!page) |
| 3033 | return ERR_PTR(-ECHILD); |
| 3034 | if (!PageUptodate(page)) { |
| 3035 | put_page(page); |
| 3036 | return ERR_PTR(-ECHILD); |
| 3037 | } |
| 3038 | } else { |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 3039 | error = shmem_getpage(inode, 0, &page, SGP_READ); |
Al Viro | 6a6c990 | 2015-11-17 10:54:32 -0500 | [diff] [blame] | 3040 | if (error) |
| 3041 | return ERR_PTR(error); |
| 3042 | unlock_page(page); |
| 3043 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3044 | set_delayed_call(done, shmem_put_link, page); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3045 | return page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3048 | #ifdef CONFIG_TMPFS_XATTR |
| 3049 | /* |
| 3050 | * Superblocks without xattr inode operations may get some security.* xattr |
| 3051 | * support from the LSM "for free". As soon as we have any other xattrs |
| 3052 | * like ACLs, we also need to implement the security.* handlers at |
| 3053 | * filesystem level, though. |
| 3054 | */ |
| 3055 | |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3056 | /* |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3057 | * Callback for security_inode_init_security() for acquiring xattrs. |
| 3058 | */ |
| 3059 | static int shmem_initxattrs(struct inode *inode, |
| 3060 | const struct xattr *xattr_array, |
| 3061 | void *fs_info) |
| 3062 | { |
| 3063 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 3064 | const struct xattr *xattr; |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 3065 | struct simple_xattr *new_xattr; |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3066 | size_t len; |
| 3067 | |
| 3068 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 3069 | new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3070 | if (!new_xattr) |
| 3071 | return -ENOMEM; |
| 3072 | |
| 3073 | len = strlen(xattr->name) + 1; |
| 3074 | new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, |
| 3075 | GFP_KERNEL); |
| 3076 | if (!new_xattr->name) { |
| 3077 | kfree(new_xattr); |
| 3078 | return -ENOMEM; |
| 3079 | } |
| 3080 | |
| 3081 | memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, |
| 3082 | XATTR_SECURITY_PREFIX_LEN); |
| 3083 | memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, |
| 3084 | xattr->name, len); |
| 3085 | |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 3086 | simple_xattr_list_add(&info->xattrs, new_xattr); |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3092 | static int shmem_xattr_handler_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 3093 | struct dentry *unused, struct inode *inode, |
| 3094 | const char *name, void *buffer, size_t size) |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3095 | { |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 3096 | struct shmem_inode_info *info = SHMEM_I(inode); |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3097 | |
| 3098 | name = xattr_full_name(handler, name); |
| 3099 | return simple_xattr_get(&info->xattrs, name, buffer, size); |
| 3100 | } |
| 3101 | |
| 3102 | static int shmem_xattr_handler_set(const struct xattr_handler *handler, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 3103 | struct dentry *unused, struct inode *inode, |
| 3104 | const char *name, const void *value, |
| 3105 | size_t size, int flags) |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3106 | { |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 3107 | struct shmem_inode_info *info = SHMEM_I(inode); |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3108 | |
| 3109 | name = xattr_full_name(handler, name); |
| 3110 | return simple_xattr_set(&info->xattrs, name, value, size, flags); |
| 3111 | } |
| 3112 | |
| 3113 | static const struct xattr_handler shmem_security_xattr_handler = { |
| 3114 | .prefix = XATTR_SECURITY_PREFIX, |
| 3115 | .get = shmem_xattr_handler_get, |
| 3116 | .set = shmem_xattr_handler_set, |
| 3117 | }; |
| 3118 | |
| 3119 | static const struct xattr_handler shmem_trusted_xattr_handler = { |
| 3120 | .prefix = XATTR_TRUSTED_PREFIX, |
| 3121 | .get = shmem_xattr_handler_get, |
| 3122 | .set = shmem_xattr_handler_set, |
| 3123 | }; |
| 3124 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3125 | static const struct xattr_handler *shmem_xattr_handlers[] = { |
| 3126 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3127 | &posix_acl_access_xattr_handler, |
| 3128 | &posix_acl_default_xattr_handler, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3129 | #endif |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3130 | &shmem_security_xattr_handler, |
| 3131 | &shmem_trusted_xattr_handler, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3132 | NULL |
| 3133 | }; |
| 3134 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3135 | static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 3136 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 3137 | struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); |
Andreas Gruenbacher | 786534b | 2015-12-02 14:44:39 +0100 | [diff] [blame] | 3138 | return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3139 | } |
| 3140 | #endif /* CONFIG_TMPFS_XATTR */ |
| 3141 | |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 3142 | static const struct inode_operations shmem_short_symlink_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3143 | .get_link = simple_get_link, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3144 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3145 | .listxattr = shmem_listxattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3146 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | }; |
| 3148 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3149 | static const struct inode_operations shmem_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3150 | .get_link = shmem_get_link, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3151 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3152 | .listxattr = shmem_listxattr, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3153 | #endif |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3154 | }; |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3155 | |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3156 | static struct dentry *shmem_get_parent(struct dentry *child) |
| 3157 | { |
| 3158 | return ERR_PTR(-ESTALE); |
| 3159 | } |
| 3160 | |
| 3161 | static int shmem_match(struct inode *ino, void *vfh) |
| 3162 | { |
| 3163 | __u32 *fh = vfh; |
| 3164 | __u64 inum = fh[2]; |
| 3165 | inum = (inum << 32) | fh[1]; |
| 3166 | return ino->i_ino == inum && fh[0] == ino->i_generation; |
| 3167 | } |
| 3168 | |
Amir Goldstein | 12ba780 | 2018-06-07 17:07:15 -0700 | [diff] [blame] | 3169 | /* Find any alias of inode, but prefer a hashed alias */ |
| 3170 | static struct dentry *shmem_find_alias(struct inode *inode) |
| 3171 | { |
| 3172 | struct dentry *alias = d_find_alias(inode); |
| 3173 | |
| 3174 | return alias ?: d_find_any_alias(inode); |
| 3175 | } |
| 3176 | |
| 3177 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3178 | static struct dentry *shmem_fh_to_dentry(struct super_block *sb, |
| 3179 | struct fid *fid, int fh_len, int fh_type) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3180 | { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3181 | struct inode *inode; |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3182 | struct dentry *dentry = NULL; |
Hugh Dickins | 35c2a7f | 2012-10-07 20:32:51 -0700 | [diff] [blame] | 3183 | u64 inum; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3184 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3185 | if (fh_len < 3) |
| 3186 | return NULL; |
| 3187 | |
Hugh Dickins | 35c2a7f | 2012-10-07 20:32:51 -0700 | [diff] [blame] | 3188 | inum = fid->raw[2]; |
| 3189 | inum = (inum << 32) | fid->raw[1]; |
| 3190 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3191 | inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), |
| 3192 | shmem_match, fid->raw); |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3193 | if (inode) { |
Amir Goldstein | 12ba780 | 2018-06-07 17:07:15 -0700 | [diff] [blame] | 3194 | dentry = shmem_find_alias(inode); |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3195 | iput(inode); |
| 3196 | } |
| 3197 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3198 | return dentry; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3199 | } |
| 3200 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 3201 | static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, |
| 3202 | struct inode *parent) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3203 | { |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 3204 | if (*len < 3) { |
| 3205 | *len = 3; |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 3206 | return FILEID_INVALID; |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 3207 | } |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3208 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3209 | if (inode_unhashed(inode)) { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3210 | /* Unfortunately insert_inode_hash is not idempotent, |
| 3211 | * so as we hash inodes here rather than at creation |
| 3212 | * time, we need a lock to ensure we only try |
| 3213 | * to do it once |
| 3214 | */ |
| 3215 | static DEFINE_SPINLOCK(lock); |
| 3216 | spin_lock(&lock); |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3217 | if (inode_unhashed(inode)) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3218 | __insert_inode_hash(inode, |
| 3219 | inode->i_ino + inode->i_generation); |
| 3220 | spin_unlock(&lock); |
| 3221 | } |
| 3222 | |
| 3223 | fh[0] = inode->i_generation; |
| 3224 | fh[1] = inode->i_ino; |
| 3225 | fh[2] = ((__u64)inode->i_ino) >> 32; |
| 3226 | |
| 3227 | *len = 3; |
| 3228 | return 1; |
| 3229 | } |
| 3230 | |
Christoph Hellwig | 3965516 | 2007-10-21 16:42:17 -0700 | [diff] [blame] | 3231 | static const struct export_operations shmem_export_ops = { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3232 | .get_parent = shmem_get_parent, |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3233 | .encode_fh = shmem_encode_fh, |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3234 | .fh_to_dentry = shmem_fh_to_dentry, |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3235 | }; |
| 3236 | |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3237 | static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, |
| 3238 | bool remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | { |
| 3240 | char *this_char, *value, *rest; |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3241 | struct mempolicy *mpol = NULL; |
Eric W. Biederman | 8751e03 | 2012-02-07 16:46:12 -0800 | [diff] [blame] | 3242 | uid_t uid; |
| 3243 | gid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | |
Hugh Dickins | b00dc3a | 2006-02-21 23:49:47 +0000 | [diff] [blame] | 3245 | while (options != NULL) { |
| 3246 | this_char = options; |
| 3247 | for (;;) { |
| 3248 | /* |
| 3249 | * NUL-terminate this option: unfortunately, |
| 3250 | * mount options form a comma-separated list, |
| 3251 | * but mpol's nodelist may also contain commas. |
| 3252 | */ |
| 3253 | options = strchr(options, ','); |
| 3254 | if (options == NULL) |
| 3255 | break; |
| 3256 | options++; |
| 3257 | if (!isdigit(*options)) { |
| 3258 | options[-1] = '\0'; |
| 3259 | break; |
| 3260 | } |
| 3261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | if (!*this_char) |
| 3263 | continue; |
| 3264 | if ((value = strchr(this_char,'=')) != NULL) { |
| 3265 | *value++ = 0; |
| 3266 | } else { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 3267 | pr_err("tmpfs: No value for mount option '%s'\n", |
| 3268 | this_char); |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3269 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | if (!strcmp(this_char,"size")) { |
| 3273 | unsigned long long size; |
| 3274 | size = memparse(value,&rest); |
| 3275 | if (*rest == '%') { |
| 3276 | size <<= PAGE_SHIFT; |
| 3277 | size *= totalram_pages; |
| 3278 | do_div(size, 100); |
| 3279 | rest++; |
| 3280 | } |
| 3281 | if (*rest) |
| 3282 | goto bad_val; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3283 | sbinfo->max_blocks = |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3284 | DIV_ROUND_UP(size, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | } else if (!strcmp(this_char,"nr_blocks")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3286 | sbinfo->max_blocks = memparse(value, &rest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | if (*rest) |
| 3288 | goto bad_val; |
| 3289 | } else if (!strcmp(this_char,"nr_inodes")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3290 | sbinfo->max_inodes = memparse(value, &rest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | if (*rest) |
| 3292 | goto bad_val; |
| 3293 | } else if (!strcmp(this_char,"mode")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3294 | if (remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | continue; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3296 | sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | if (*rest) |
| 3298 | goto bad_val; |
| 3299 | } else if (!strcmp(this_char,"uid")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3300 | if (remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | continue; |
Eric W. Biederman | 8751e03 | 2012-02-07 16:46:12 -0800 | [diff] [blame] | 3302 | uid = simple_strtoul(value, &rest, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | if (*rest) |
| 3304 | goto bad_val; |
Eric W. Biederman | 8751e03 | 2012-02-07 16:46:12 -0800 | [diff] [blame] | 3305 | sbinfo->uid = make_kuid(current_user_ns(), uid); |
| 3306 | if (!uid_valid(sbinfo->uid)) |
| 3307 | goto bad_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | } else if (!strcmp(this_char,"gid")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3309 | if (remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | continue; |
Eric W. Biederman | 8751e03 | 2012-02-07 16:46:12 -0800 | [diff] [blame] | 3311 | gid = simple_strtoul(value, &rest, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | if (*rest) |
| 3313 | goto bad_val; |
Eric W. Biederman | 8751e03 | 2012-02-07 16:46:12 -0800 | [diff] [blame] | 3314 | sbinfo->gid = make_kgid(current_user_ns(), gid); |
| 3315 | if (!gid_valid(sbinfo->gid)) |
| 3316 | goto bad_val; |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 3317 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3318 | } else if (!strcmp(this_char, "huge")) { |
| 3319 | int huge; |
| 3320 | huge = shmem_parse_huge(value); |
| 3321 | if (huge < 0) |
| 3322 | goto bad_val; |
| 3323 | if (!has_transparent_hugepage() && |
| 3324 | huge != SHMEM_HUGE_NEVER) |
| 3325 | goto bad_val; |
| 3326 | sbinfo->huge = huge; |
| 3327 | #endif |
| 3328 | #ifdef CONFIG_NUMA |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 3329 | } else if (!strcmp(this_char,"mpol")) { |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3330 | mpol_put(mpol); |
| 3331 | mpol = NULL; |
| 3332 | if (mpol_parse_str(value, &mpol)) |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 3333 | goto bad_val; |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3334 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | } else { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 3336 | pr_err("tmpfs: Bad mount option %s\n", this_char); |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3337 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | } |
| 3339 | } |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3340 | sbinfo->mpol = mpol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | return 0; |
| 3342 | |
| 3343 | bad_val: |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 3344 | pr_err("tmpfs: Bad value '%s' for mount option '%s'\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | value, this_char); |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3346 | error: |
| 3347 | mpol_put(mpol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | return 1; |
| 3349 | |
| 3350 | } |
| 3351 | |
| 3352 | static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) |
| 3353 | { |
| 3354 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3355 | struct shmem_sb_info config = *sbinfo; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3356 | unsigned long inodes; |
| 3357 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | |
Greg Thelen | 5f00110 | 2013-02-22 16:36:01 -0800 | [diff] [blame] | 3359 | config.mpol = NULL; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3360 | if (shmem_parse_options(data, &config, true)) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3361 | return error; |
| 3362 | |
| 3363 | spin_lock(&sbinfo->stat_lock); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3364 | inodes = sbinfo->max_inodes - sbinfo->free_inodes; |
Tim Chen | 7e49629 | 2010-08-09 17:19:05 -0700 | [diff] [blame] | 3365 | if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3366 | goto out; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3367 | if (config.max_inodes < inodes) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3368 | goto out; |
| 3369 | /* |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 3370 | * Those tests disallow limited->unlimited while any are in use; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3371 | * but we must separately disallow unlimited->limited, because |
| 3372 | * in that case we have no record of how much is already in use. |
| 3373 | */ |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3374 | if (config.max_blocks && !sbinfo->max_blocks) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3375 | goto out; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3376 | if (config.max_inodes && !sbinfo->max_inodes) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3377 | goto out; |
| 3378 | |
| 3379 | error = 0; |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3380 | sbinfo->huge = config.huge; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3381 | sbinfo->max_blocks = config.max_blocks; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3382 | sbinfo->max_inodes = config.max_inodes; |
| 3383 | sbinfo->free_inodes = config.max_inodes - inodes; |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 3384 | |
Greg Thelen | 5f00110 | 2013-02-22 16:36:01 -0800 | [diff] [blame] | 3385 | /* |
| 3386 | * Preserve previous mempolicy unless mpol remount option was specified. |
| 3387 | */ |
| 3388 | if (config.mpol) { |
| 3389 | mpol_put(sbinfo->mpol); |
| 3390 | sbinfo->mpol = config.mpol; /* transfers initial ref */ |
| 3391 | } |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3392 | out: |
| 3393 | spin_unlock(&sbinfo->stat_lock); |
| 3394 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | } |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3396 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 3397 | static int shmem_show_options(struct seq_file *seq, struct dentry *root) |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3398 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 3399 | struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3400 | |
| 3401 | if (sbinfo->max_blocks != shmem_default_max_blocks()) |
| 3402 | seq_printf(seq, ",size=%luk", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3403 | sbinfo->max_blocks << (PAGE_SHIFT - 10)); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3404 | if (sbinfo->max_inodes != shmem_default_max_inodes()) |
| 3405 | seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 3406 | if (sbinfo->mode != (0777 | S_ISVTX)) |
Al Viro | 09208d1 | 2011-07-26 03:15:03 -0400 | [diff] [blame] | 3407 | seq_printf(seq, ",mode=%03ho", sbinfo->mode); |
Eric W. Biederman | 8751e03 | 2012-02-07 16:46:12 -0800 | [diff] [blame] | 3408 | if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) |
| 3409 | seq_printf(seq, ",uid=%u", |
| 3410 | from_kuid_munged(&init_user_ns, sbinfo->uid)); |
| 3411 | if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) |
| 3412 | seq_printf(seq, ",gid=%u", |
| 3413 | from_kgid_munged(&init_user_ns, sbinfo->gid)); |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 3414 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3415 | /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ |
| 3416 | if (sbinfo->huge) |
| 3417 | seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); |
| 3418 | #endif |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 3419 | shmem_show_mpol(seq, sbinfo->mpol); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3420 | return 0; |
| 3421 | } |
David Herrmann | 9183df2 | 2014-08-08 14:25:29 -0700 | [diff] [blame] | 3422 | |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3423 | #endif /* CONFIG_TMPFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | |
| 3425 | static void shmem_put_super(struct super_block *sb) |
| 3426 | { |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 3427 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 3428 | |
| 3429 | percpu_counter_destroy(&sbinfo->used_blocks); |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3430 | mpol_put(sbinfo->mpol); |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 3431 | kfree(sbinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | sb->s_fs_info = NULL; |
| 3433 | } |
| 3434 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3435 | int shmem_fill_super(struct super_block *sb, void *data, int silent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | { |
| 3437 | struct inode *inode; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3438 | struct shmem_sb_info *sbinfo; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3439 | int err = -ENOMEM; |
| 3440 | |
| 3441 | /* Round up to L1_CACHE_BYTES to resist false sharing */ |
Pekka Enberg | 425fbf0 | 2009-09-21 17:03:50 -0700 | [diff] [blame] | 3442 | sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3443 | L1_CACHE_BYTES), GFP_KERNEL); |
| 3444 | if (!sbinfo) |
| 3445 | return -ENOMEM; |
| 3446 | |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 3447 | sbinfo->mode = 0777 | S_ISVTX; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 3448 | sbinfo->uid = current_fsuid(); |
| 3449 | sbinfo->gid = current_fsgid(); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3450 | sb->s_fs_info = sbinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3452 | #ifdef CONFIG_TMPFS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | /* |
| 3454 | * Per default we only allow half of the physical ram per |
| 3455 | * tmpfs instance, limiting inodes to one per page of lowmem; |
| 3456 | * but the internal instance is left unlimited. |
| 3457 | */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3458 | if (!(sb->s_flags & SB_KERNMOUNT)) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3459 | sbinfo->max_blocks = shmem_default_max_blocks(); |
| 3460 | sbinfo->max_inodes = shmem_default_max_inodes(); |
| 3461 | if (shmem_parse_options(data, sbinfo, false)) { |
| 3462 | err = -EINVAL; |
| 3463 | goto failed; |
| 3464 | } |
Al Viro | ca4e051 | 2013-08-31 12:57:10 -0400 | [diff] [blame] | 3465 | } else { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3466 | sb->s_flags |= SB_NOUSER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | } |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3468 | sb->s_export_op = &shmem_export_ops; |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3469 | sb->s_flags |= SB_NOSEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | #else |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3471 | sb->s_flags |= SB_NOUSER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | #endif |
| 3473 | |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3474 | spin_lock_init(&sbinfo->stat_lock); |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 3475 | if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 3476 | goto failed; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3477 | sbinfo->free_inodes = sbinfo->max_inodes; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 3478 | spin_lock_init(&sbinfo->shrinklist_lock); |
| 3479 | INIT_LIST_HEAD(&sbinfo->shrinklist); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3480 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 3481 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3482 | sb->s_blocksize = PAGE_SIZE; |
| 3483 | sb->s_blocksize_bits = PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | sb->s_magic = TMPFS_MAGIC; |
| 3485 | sb->s_op = &shmem_ops; |
Robin H. Johnson | cfd95a9 | 2006-06-12 21:50:25 +0100 | [diff] [blame] | 3486 | sb->s_time_gran = 1; |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3487 | #ifdef CONFIG_TMPFS_XATTR |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3488 | sb->s_xattr = shmem_xattr_handlers; |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3489 | #endif |
| 3490 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3491 | sb->s_flags |= SB_POSIXACL; |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3492 | #endif |
Amir Goldstein | 2b4db79 | 2017-05-18 15:29:33 +0300 | [diff] [blame] | 3493 | uuid_gen(&sb->s_uuid); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3494 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 3495 | inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | if (!inode) |
| 3497 | goto failed; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3498 | inode->i_uid = sbinfo->uid; |
| 3499 | inode->i_gid = sbinfo->gid; |
Al Viro | 318ceed | 2012-02-12 22:08:01 -0500 | [diff] [blame] | 3500 | sb->s_root = d_make_root(inode); |
| 3501 | if (!sb->s_root) |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 3502 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | return 0; |
| 3504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | failed: |
| 3506 | shmem_put_super(sb); |
| 3507 | return err; |
| 3508 | } |
| 3509 | |
Pekka Enberg | fcc234f | 2006-03-22 00:08:13 -0800 | [diff] [blame] | 3510 | static struct kmem_cache *shmem_inode_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | |
| 3512 | static struct inode *shmem_alloc_inode(struct super_block *sb) |
| 3513 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3514 | struct shmem_inode_info *info; |
| 3515 | info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); |
| 3516 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3517 | return NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3518 | return &info->vfs_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3521 | static void shmem_destroy_callback(struct rcu_head *head) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 3522 | { |
| 3523 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Al Viro | 84e710d | 2016-04-15 00:58:55 -0400 | [diff] [blame] | 3524 | if (S_ISLNK(inode->i_mode)) |
| 3525 | kfree(inode->i_link); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 3526 | kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); |
| 3527 | } |
| 3528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | static void shmem_destroy_inode(struct inode *inode) |
| 3530 | { |
Al Viro | 09208d1 | 2011-07-26 03:15:03 -0400 | [diff] [blame] | 3531 | if (S_ISREG(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | mpol_free_shared_policy(&SHMEM_I(inode)->policy); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3533 | call_rcu(&inode->i_rcu, shmem_destroy_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | } |
| 3535 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3536 | static void shmem_init_inode(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3538 | struct shmem_inode_info *info = foo; |
| 3539 | inode_init_once(&info->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
weiping zhang | 9a8ec03e | 2017-11-15 17:38:18 -0800 | [diff] [blame] | 3542 | static void shmem_init_inodecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | { |
| 3544 | shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", |
| 3545 | sizeof(struct shmem_inode_info), |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 3546 | 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | } |
| 3548 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3549 | static void shmem_destroy_inodecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | { |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 3551 | kmem_cache_destroy(shmem_inode_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | } |
| 3553 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 3554 | static const struct address_space_operations shmem_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | .writepage = shmem_writepage, |
Ken Chen | 7671932 | 2007-02-10 01:43:15 -0800 | [diff] [blame] | 3556 | .set_page_dirty = __set_page_dirty_no_writeback, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | #ifdef CONFIG_TMPFS |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 3558 | .write_begin = shmem_write_begin, |
| 3559 | .write_end = shmem_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | #endif |
Andrew Morton | 1c93923 | 2014-10-09 15:27:59 -0700 | [diff] [blame] | 3561 | #ifdef CONFIG_MIGRATION |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 3562 | .migratepage = migrate_page, |
Andrew Morton | 1c93923 | 2014-10-09 15:27:59 -0700 | [diff] [blame] | 3563 | #endif |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 3564 | .error_remove_page = generic_error_remove_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | }; |
| 3566 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 3567 | static const struct file_operations shmem_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | .mmap = shmem_mmap, |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 3569 | .get_unmapped_area = shmem_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | #ifdef CONFIG_TMPFS |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 3571 | .llseek = shmem_file_llseek, |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 3572 | .read_iter = shmem_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3573 | .write_iter = generic_file_write_iter, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 3574 | .fsync = noop_fsync, |
Al Viro | 82c156f | 2016-09-22 23:35:42 -0400 | [diff] [blame] | 3575 | .splice_read = generic_file_splice_read, |
Al Viro | f6cb85d | 2014-04-05 04:38:56 -0400 | [diff] [blame] | 3576 | .splice_write = iter_file_splice_write, |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 3577 | .fallocate = shmem_fallocate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | #endif |
| 3579 | }; |
| 3580 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3581 | static const struct inode_operations shmem_inode_operations = { |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 3582 | .getattr = shmem_getattr, |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3583 | .setattr = shmem_setattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3584 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3585 | .listxattr = shmem_listxattr, |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3586 | .set_acl = simple_set_acl, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3587 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | }; |
| 3589 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3590 | static const struct inode_operations shmem_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | #ifdef CONFIG_TMPFS |
| 3592 | .create = shmem_create, |
| 3593 | .lookup = simple_lookup, |
| 3594 | .link = shmem_link, |
| 3595 | .unlink = shmem_unlink, |
| 3596 | .symlink = shmem_symlink, |
| 3597 | .mkdir = shmem_mkdir, |
| 3598 | .rmdir = shmem_rmdir, |
| 3599 | .mknod = shmem_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 3600 | .rename = shmem_rename2, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3601 | .tmpfile = shmem_tmpfile, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | #endif |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3603 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3604 | .listxattr = shmem_listxattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3605 | #endif |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3606 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3607 | .setattr = shmem_setattr, |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3608 | .set_acl = simple_set_acl, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3609 | #endif |
| 3610 | }; |
| 3611 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3612 | static const struct inode_operations shmem_special_inode_operations = { |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3613 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3614 | .listxattr = shmem_listxattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3615 | #endif |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3616 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3617 | .setattr = shmem_setattr, |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3618 | .set_acl = simple_set_acl, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3619 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | }; |
| 3621 | |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 3622 | static const struct super_operations shmem_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | .alloc_inode = shmem_alloc_inode, |
| 3624 | .destroy_inode = shmem_destroy_inode, |
| 3625 | #ifdef CONFIG_TMPFS |
| 3626 | .statfs = shmem_statfs, |
| 3627 | .remount_fs = shmem_remount_fs, |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3628 | .show_options = shmem_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | #endif |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 3630 | .evict_inode = shmem_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | .drop_inode = generic_delete_inode, |
| 3632 | .put_super = shmem_put_super, |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 3633 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
| 3634 | .nr_cached_objects = shmem_unused_huge_count, |
| 3635 | .free_cached_objects = shmem_unused_huge_scan, |
| 3636 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | }; |
| 3638 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3639 | static const struct vm_operations_struct shmem_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 3640 | .fault = shmem_fault, |
Ning Qu | d7c1755 | 2014-04-07 15:37:24 -0700 | [diff] [blame] | 3641 | .map_pages = filemap_map_pages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | #ifdef CONFIG_NUMA |
| 3643 | .set_policy = shmem_set_policy, |
| 3644 | .get_policy = shmem_get_policy, |
| 3645 | #endif |
| 3646 | }; |
| 3647 | |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 3648 | static struct dentry *shmem_mount(struct file_system_type *fs_type, |
| 3649 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | { |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 3651 | return mount_nodev(fs_type, flags, data, shmem_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | } |
| 3653 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3654 | static struct file_system_type shmem_fs_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | .owner = THIS_MODULE, |
| 3656 | .name = "tmpfs", |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 3657 | .mount = shmem_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | .kill_sb = kill_litter_super, |
Eric W. Biederman | 2b8576c | 2013-01-25 16:32:10 -0800 | [diff] [blame] | 3659 | .fs_flags = FS_USERNS_MOUNT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3662 | int __init shmem_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | { |
| 3664 | int error; |
| 3665 | |
Rob Landley | 16203a7 | 2013-09-11 14:26:12 -0700 | [diff] [blame] | 3666 | /* If rootfs called this, don't re-init */ |
| 3667 | if (shmem_inode_cachep) |
| 3668 | return 0; |
| 3669 | |
weiping zhang | 9a8ec03e | 2017-11-15 17:38:18 -0800 | [diff] [blame] | 3670 | shmem_init_inodecache(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3672 | error = register_filesystem(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | if (error) { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 3674 | pr_err("Could not register tmpfs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | goto out2; |
| 3676 | } |
Greg Kroah-Hartman | 95dc112 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3677 | |
Al Viro | ca4e051 | 2013-08-31 12:57:10 -0400 | [diff] [blame] | 3678 | shm_mnt = kern_mount(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | if (IS_ERR(shm_mnt)) { |
| 3680 | error = PTR_ERR(shm_mnt); |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 3681 | pr_err("Could not kern_mount tmpfs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | goto out1; |
| 3683 | } |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3684 | |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 3685 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
Kirill A. Shutemov | 435c0b87 | 2017-08-25 15:55:33 -0700 | [diff] [blame] | 3686 | if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3687 | SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; |
| 3688 | else |
| 3689 | shmem_huge = 0; /* just in case it was patched */ |
| 3690 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | return 0; |
| 3692 | |
| 3693 | out1: |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3694 | unregister_filesystem(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3695 | out2: |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3696 | shmem_destroy_inodecache(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | shm_mnt = ERR_PTR(error); |
| 3698 | return error; |
| 3699 | } |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3700 | |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 3701 | #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3702 | static ssize_t shmem_enabled_show(struct kobject *kobj, |
| 3703 | struct kobj_attribute *attr, char *buf) |
| 3704 | { |
| 3705 | int values[] = { |
| 3706 | SHMEM_HUGE_ALWAYS, |
| 3707 | SHMEM_HUGE_WITHIN_SIZE, |
| 3708 | SHMEM_HUGE_ADVISE, |
| 3709 | SHMEM_HUGE_NEVER, |
| 3710 | SHMEM_HUGE_DENY, |
| 3711 | SHMEM_HUGE_FORCE, |
| 3712 | }; |
| 3713 | int i, count; |
| 3714 | |
| 3715 | for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { |
| 3716 | const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; |
| 3717 | |
| 3718 | count += sprintf(buf + count, fmt, |
| 3719 | shmem_format_huge(values[i])); |
| 3720 | } |
| 3721 | buf[count - 1] = '\n'; |
| 3722 | return count; |
| 3723 | } |
| 3724 | |
| 3725 | static ssize_t shmem_enabled_store(struct kobject *kobj, |
| 3726 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 3727 | { |
| 3728 | char tmp[16]; |
| 3729 | int huge; |
| 3730 | |
| 3731 | if (count + 1 > sizeof(tmp)) |
| 3732 | return -EINVAL; |
| 3733 | memcpy(tmp, buf, count); |
| 3734 | tmp[count] = '\0'; |
| 3735 | if (count && tmp[count - 1] == '\n') |
| 3736 | tmp[count - 1] = '\0'; |
| 3737 | |
| 3738 | huge = shmem_parse_huge(tmp); |
| 3739 | if (huge == -EINVAL) |
| 3740 | return -EINVAL; |
| 3741 | if (!has_transparent_hugepage() && |
| 3742 | huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) |
| 3743 | return -EINVAL; |
| 3744 | |
| 3745 | shmem_huge = huge; |
Kirill A. Shutemov | 435c0b87 | 2017-08-25 15:55:33 -0700 | [diff] [blame] | 3746 | if (shmem_huge > SHMEM_HUGE_DENY) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3747 | SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; |
| 3748 | return count; |
| 3749 | } |
| 3750 | |
| 3751 | struct kobj_attribute shmem_enabled_attr = |
| 3752 | __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); |
Arnd Bergmann | 3b33719 | 2016-08-10 16:27:44 -0700 | [diff] [blame] | 3753 | #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */ |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 3754 | |
Arnd Bergmann | 3b33719 | 2016-08-10 16:27:44 -0700 | [diff] [blame] | 3755 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 3756 | bool shmem_huge_enabled(struct vm_area_struct *vma) |
| 3757 | { |
| 3758 | struct inode *inode = file_inode(vma->vm_file); |
| 3759 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
| 3760 | loff_t i_size; |
| 3761 | pgoff_t off; |
| 3762 | |
| 3763 | if (shmem_huge == SHMEM_HUGE_FORCE) |
| 3764 | return true; |
| 3765 | if (shmem_huge == SHMEM_HUGE_DENY) |
| 3766 | return false; |
| 3767 | switch (sbinfo->huge) { |
| 3768 | case SHMEM_HUGE_NEVER: |
| 3769 | return false; |
| 3770 | case SHMEM_HUGE_ALWAYS: |
| 3771 | return true; |
| 3772 | case SHMEM_HUGE_WITHIN_SIZE: |
| 3773 | off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); |
| 3774 | i_size = round_up(i_size_read(inode), PAGE_SIZE); |
| 3775 | if (i_size >= HPAGE_PMD_SIZE && |
| 3776 | i_size >> PAGE_SHIFT >= off) |
| 3777 | return true; |
Gustavo A. R. Silva | c840287 | 2017-11-15 17:38:45 -0800 | [diff] [blame] | 3778 | /* fall through */ |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 3779 | case SHMEM_HUGE_ADVISE: |
| 3780 | /* TODO: implement fadvise() hints */ |
| 3781 | return (vma->vm_flags & VM_HUGEPAGE); |
| 3782 | default: |
| 3783 | VM_BUG_ON(1); |
| 3784 | return false; |
| 3785 | } |
| 3786 | } |
Arnd Bergmann | 3b33719 | 2016-08-10 16:27:44 -0700 | [diff] [blame] | 3787 | #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3788 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3789 | #else /* !CONFIG_SHMEM */ |
| 3790 | |
| 3791 | /* |
| 3792 | * tiny-shmem: simple shmemfs and tmpfs using ramfs code |
| 3793 | * |
| 3794 | * This is intended for small system where the benefits of the full |
| 3795 | * shmem code (swap-backed and resource-limited) are outweighed by |
| 3796 | * their complexity. On systems without swap this code should be |
| 3797 | * effectively equivalent, but much lighter weight. |
| 3798 | */ |
| 3799 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3800 | static struct file_system_type shmem_fs_type = { |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3801 | .name = "tmpfs", |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 3802 | .mount = ramfs_mount, |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3803 | .kill_sb = kill_litter_super, |
Eric W. Biederman | 2b8576c | 2013-01-25 16:32:10 -0800 | [diff] [blame] | 3804 | .fs_flags = FS_USERNS_MOUNT, |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3805 | }; |
| 3806 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3807 | int __init shmem_init(void) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3808 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3809 | BUG_ON(register_filesystem(&shmem_fs_type) != 0); |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3810 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3811 | shm_mnt = kern_mount(&shmem_fs_type); |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3812 | BUG_ON(IS_ERR(shm_mnt)); |
| 3813 | |
| 3814 | return 0; |
| 3815 | } |
| 3816 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3817 | int shmem_unuse(swp_entry_t swap, struct page *page) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3818 | { |
| 3819 | return 0; |
| 3820 | } |
| 3821 | |
Hugh Dickins | 3f96b79 | 2009-09-21 17:03:37 -0700 | [diff] [blame] | 3822 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
| 3823 | { |
| 3824 | return 0; |
| 3825 | } |
| 3826 | |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 3827 | void shmem_unlock_mapping(struct address_space *mapping) |
| 3828 | { |
| 3829 | } |
| 3830 | |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 3831 | #ifdef CONFIG_MMU |
| 3832 | unsigned long shmem_get_unmapped_area(struct file *file, |
| 3833 | unsigned long addr, unsigned long len, |
| 3834 | unsigned long pgoff, unsigned long flags) |
| 3835 | { |
| 3836 | return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); |
| 3837 | } |
| 3838 | #endif |
| 3839 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3840 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3841 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3842 | truncate_inode_pages_range(inode->i_mapping, lstart, lend); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3843 | } |
| 3844 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
| 3845 | |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 3846 | #define shmem_vm_ops generic_file_vm_ops |
| 3847 | #define shmem_file_operations ramfs_file_operations |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 3848 | #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 3849 | #define shmem_acct_size(flags, size) 0 |
| 3850 | #define shmem_unacct_size(flags, size) do {} while (0) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3851 | |
| 3852 | #endif /* CONFIG_SHMEM */ |
| 3853 | |
| 3854 | /* common code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 3856 | static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 3857 | unsigned long flags, unsigned int i_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | struct inode *inode; |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 3860 | struct file *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 3862 | if (IS_ERR(mnt)) |
| 3863 | return ERR_CAST(mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 3865 | if (size < 0 || size > MAX_LFS_FILESIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | return ERR_PTR(-EINVAL); |
| 3867 | |
| 3868 | if (shmem_acct_size(flags, size)) |
| 3869 | return ERR_PTR(-ENOMEM); |
| 3870 | |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 3871 | inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, |
| 3872 | flags); |
Al Viro | dac2d1f | 2018-06-09 09:27:41 -0400 | [diff] [blame] | 3873 | if (unlikely(!inode)) { |
| 3874 | shmem_unacct_size(flags, size); |
| 3875 | return ERR_PTR(-ENOSPC); |
| 3876 | } |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 3877 | inode->i_flags |= i_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | inode->i_size = size; |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 3879 | clear_nlink(inode); /* It is unlinked */ |
Al Viro | 26567cd | 2013-03-01 20:22:53 -0500 | [diff] [blame] | 3880 | res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 3881 | if (!IS_ERR(res)) |
| 3882 | res = alloc_file_pseudo(inode, mnt, name, O_RDWR, |
| 3883 | &shmem_file_operations); |
Al Viro | 26567cd | 2013-03-01 20:22:53 -0500 | [diff] [blame] | 3884 | if (IS_ERR(res)) |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 3885 | iput(inode); |
Al Viro | 6b4d0b2 | 2013-02-14 21:37:26 -0500 | [diff] [blame] | 3886 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3887 | } |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 3888 | |
| 3889 | /** |
| 3890 | * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be |
| 3891 | * kernel internal. There will be NO LSM permission checks against the |
| 3892 | * underlying inode. So users of this interface must do LSM checks at a |
Stephen Smalley | e1832f2 | 2015-08-06 15:46:55 -0700 | [diff] [blame] | 3893 | * higher layer. The users are the big_key and shm implementations. LSM |
| 3894 | * checks are provided at the key or shm level rather than the inode. |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 3895 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 3896 | * @size: size to be set for the file |
| 3897 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
| 3898 | */ |
| 3899 | struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) |
| 3900 | { |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 3901 | return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /** |
| 3905 | * shmem_file_setup - get an unlinked file living in tmpfs |
| 3906 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 3907 | * @size: size to be set for the file |
| 3908 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
| 3909 | */ |
| 3910 | struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) |
| 3911 | { |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 3912 | return __shmem_file_setup(shm_mnt, name, size, flags, 0); |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 3913 | } |
Keith Packard | 395e0dd | 2008-06-20 00:08:06 -0700 | [diff] [blame] | 3914 | EXPORT_SYMBOL_GPL(shmem_file_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | |
Randy Dunlap | 4671181 | 2008-03-19 17:00:41 -0700 | [diff] [blame] | 3916 | /** |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 3917 | * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs |
| 3918 | * @mnt: the tmpfs mount where the file will be created |
| 3919 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 3920 | * @size: size to be set for the file |
| 3921 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
| 3922 | */ |
| 3923 | struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, |
| 3924 | loff_t size, unsigned long flags) |
| 3925 | { |
| 3926 | return __shmem_file_setup(mnt, name, size, flags, 0); |
| 3927 | } |
| 3928 | EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); |
| 3929 | |
| 3930 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | * shmem_zero_setup - setup a shared anonymous mapping |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | * @vma: the vma to be mmapped is prepared by do_mmap_pgoff |
| 3933 | */ |
| 3934 | int shmem_zero_setup(struct vm_area_struct *vma) |
| 3935 | { |
| 3936 | struct file *file; |
| 3937 | loff_t size = vma->vm_end - vma->vm_start; |
| 3938 | |
Hugh Dickins | 66fc130 | 2015-06-14 09:48:09 -0700 | [diff] [blame] | 3939 | /* |
| 3940 | * Cloning a new file under mmap_sem leads to a lock ordering conflict |
| 3941 | * between XFS directory reading and selinux: since this file is only |
| 3942 | * accessible to the user through its mapping, use S_PRIVATE flag to |
| 3943 | * bypass file security, in the same way as shmem_kernel_file_setup(). |
| 3944 | */ |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 3945 | file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | if (IS_ERR(file)) |
| 3947 | return PTR_ERR(file); |
| 3948 | |
| 3949 | if (vma->vm_file) |
| 3950 | fput(vma->vm_file); |
| 3951 | vma->vm_file = file; |
| 3952 | vma->vm_ops = &shmem_vm_ops; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 3953 | |
Kirill A. Shutemov | e496cf3 | 2016-07-26 15:26:35 -0700 | [diff] [blame] | 3954 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 3955 | ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < |
| 3956 | (vma->vm_end & HPAGE_PMD_MASK)) { |
| 3957 | khugepaged_enter(vma, vma->vm_flags); |
| 3958 | } |
| 3959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | return 0; |
| 3961 | } |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 3962 | |
| 3963 | /** |
| 3964 | * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. |
| 3965 | * @mapping: the page's address_space |
| 3966 | * @index: the page index |
| 3967 | * @gfp: the page allocator flags to use if allocating |
| 3968 | * |
| 3969 | * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", |
| 3970 | * with any new page allocations done using the specified allocation flags. |
| 3971 | * But read_cache_page_gfp() uses the ->readpage() method: which does not |
| 3972 | * suit tmpfs, since it may have pages in swapcache, and needs to find those |
| 3973 | * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. |
| 3974 | * |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 3975 | * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in |
| 3976 | * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 3977 | */ |
| 3978 | struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, |
| 3979 | pgoff_t index, gfp_t gfp) |
| 3980 | { |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 3981 | #ifdef CONFIG_SHMEM |
| 3982 | struct inode *inode = mapping->host; |
Hugh Dickins | 9276aad | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 3983 | struct page *page; |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 3984 | int error; |
| 3985 | |
| 3986 | BUG_ON(mapping->a_ops != &shmem_aops); |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 3987 | error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 3988 | gfp, NULL, NULL, NULL); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 3989 | if (error) |
| 3990 | page = ERR_PTR(error); |
| 3991 | else |
| 3992 | unlock_page(page); |
| 3993 | return page; |
| 3994 | #else |
| 3995 | /* |
| 3996 | * The tiny !SHMEM case uses ramfs without swap |
| 3997 | */ |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 3998 | return read_cache_page_gfp(mapping, index, gfp); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 3999 | #endif |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 4000 | } |
| 4001 | EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); |