blob: 585a82d87a92fa2c8fce6e2a9f0e9e926ae43a77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dickins6922c0c2011-08-03 16:21:25 -07009 * Copyright (C) 2002-2011 Hugh Dickins.
10 * Copyright (C) 2011 Google Inc.
Hugh Dickins0edd73b2005-06-21 17:15:04 -070011 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 Mackall853ac432009-01-06 14:40:20 -080018 * tiny-shmem:
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * This file is released under the GPL.
22 */
23
Matt Mackall853ac432009-01-06 14:40:20 -080024#include <linux/fs.h>
25#include <linux/init.h>
26#include <linux/vfs.h>
27#include <linux/mount.h>
Andrew Morton250297e2013-04-29 15:06:12 -070028#include <linux/ramfs.h>
Hugh Dickinscaefba12009-04-13 14:40:12 -070029#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080030#include <linux/file.h>
31#include <linux/mm.h>
Arnd Bergmann46c9a942018-08-17 15:45:09 -070032#include <linux/random.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010033#include <linux/sched/signal.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040034#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080035#include <linux/swap.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080036#include <linux/uio.h>
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070037#include <linux/khugepaged.h>
Mike Kravetz749df872017-09-06 16:24:16 -070038#include <linux/hugetlb.h>
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -080039#include <linux/frontswap.h>
Al Viro626c3922019-09-08 20:28:06 -040040#include <linux/fs_parser.h>
Matt Mackall853ac432009-01-06 14:40:20 -080041
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080042#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
43
Matt Mackall853ac432009-01-06 14:40:20 -080044static struct vfsmount *shm_mnt;
45
46#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * This virtual memory filesystem is heavily based on the ramfs. It
49 * extends ramfs by the ability to use swap and honor resource limits
50 * which makes it a completely usable filesystem.
51 */
52
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070053#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070054#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010055#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080056#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/string.h>
59#include <linux/slab.h>
60#include <linux/backing-dev.h>
61#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070064#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070065#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070066#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070067#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/security.h>
69#include <linux/swapops.h>
70#include <linux/mempolicy.h>
71#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000072#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070073#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070074#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080075#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040076#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070077#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070078#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070079#include <uapi/linux/memfd.h>
Mike Rapoportcfda0522017-02-22 15:43:37 -080080#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080081#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030082#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070083
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080084#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Mel Gormandd56b042015-11-06 16:28:43 -080086#include "internal.h"
87
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030088#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
89#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Pretend that each entry is of this size in directory's i_size */
92#define BOGO_DIRENT_SIZE 20
93
Hugh Dickins69f07ec2011-08-03 16:21:26 -070094/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
95#define SHORT_SYMLINK_LEN 128
96
Hugh Dickins1aac1402012-05-29 15:06:42 -070097/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070098 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
99 * inode->i_private (with i_mutex making sure that it has only one user at
100 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700101 */
102struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700103 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700104 pgoff_t start; /* start of range currently being fallocated */
105 pgoff_t next; /* the next page offset to be fallocated */
106 pgoff_t nr_falloced; /* how many new pages have been fallocated */
107 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
108};
109
Al Viro0b5071d2019-09-08 18:49:18 -0400110struct shmem_options {
111 unsigned long long blocks;
112 unsigned long long inodes;
113 struct mempolicy *mpol;
114 kuid_t uid;
115 kgid_t gid;
116 umode_t mode;
117 int huge;
118 int seen;
119#define SHMEM_SEEN_BLOCKS 1
120#define SHMEM_SEEN_INODES 2
121#define SHMEM_SEEN_HUGE 4
122};
123
Andrew Mortonb76db732008-02-08 04:21:49 -0800124#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800125static unsigned long shmem_default_max_blocks(void)
126{
Arun KSca79b0c2018-12-28 00:34:29 -0800127 return totalram_pages() / 2;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800128}
129
130static unsigned long shmem_default_max_inodes(void)
131{
Arun KSca79b0c2018-12-28 00:34:29 -0800132 unsigned long nr_pages = totalram_pages();
133
134 return min(nr_pages - totalhigh_pages(), nr_pages / 2);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800135}
Andrew Mortonb76db732008-02-08 04:21:49 -0800136#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800137
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700138static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
139static int shmem_replace_page(struct page **pagep, gfp_t gfp,
140 struct shmem_inode_info *info, pgoff_t index);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -0800141static int shmem_swapin_page(struct inode *inode, pgoff_t index,
142 struct page **pagep, enum sgp_type sgp,
143 gfp_t gfp, struct vm_area_struct *vma,
144 vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700145static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700146 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800147 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700148 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700149
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700150int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700151 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700152{
153 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800154 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
158{
159 return sb->s_fs_info;
160}
161
162/*
163 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
164 * for shared memory and for shared anonymous (/dev/zero) mappings
165 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
166 * consistent with the pre-accounting of private mappings ...
167 */
168static inline int shmem_acct_size(unsigned long flags, loff_t size)
169{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000170 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000171 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static inline void shmem_unacct_size(unsigned long flags, loff_t size)
175{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000176 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 vm_unacct_memory(VM_ACCT(size));
178}
179
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700180static inline int shmem_reacct_size(unsigned long flags,
181 loff_t oldsize, loff_t newsize)
182{
183 if (!(flags & VM_NORESERVE)) {
184 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
185 return security_vm_enough_memory_mm(current->mm,
186 VM_ACCT(newsize) - VM_ACCT(oldsize));
187 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
188 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
189 }
190 return 0;
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
194 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700195 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
197 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
198 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700199static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700201 if (!(flags & VM_NORESERVE))
202 return 0;
203
204 return security_vm_enough_memory_mm(current->mm,
205 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static inline void shmem_unacct_blocks(unsigned long flags, long pages)
209{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000210 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300211 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Mike Rapoport0f079692017-09-06 16:22:59 -0700214static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
215{
216 struct shmem_inode_info *info = SHMEM_I(inode);
217 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
218
219 if (shmem_acct_block(info->flags, pages))
220 return false;
221
222 if (sbinfo->max_blocks) {
223 if (percpu_counter_compare(&sbinfo->used_blocks,
224 sbinfo->max_blocks - pages) > 0)
225 goto unacct;
226 percpu_counter_add(&sbinfo->used_blocks, pages);
227 }
228
229 return true;
230
231unacct:
232 shmem_unacct_blocks(info->flags, pages);
233 return false;
234}
235
236static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
237{
238 struct shmem_inode_info *info = SHMEM_I(inode);
239 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
240
241 if (sbinfo->max_blocks)
242 percpu_counter_sub(&sbinfo->used_blocks, pages);
243 shmem_unacct_blocks(info->flags, pages);
244}
245
Hugh Dickins759b9772007-03-05 00:30:28 -0800246static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700247static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800248static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800249static const struct inode_operations shmem_inode_operations;
250static const struct inode_operations shmem_dir_inode_operations;
251static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400252static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700253static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Mike Rapoportb0506e42017-02-22 15:43:28 -0800255bool vma_is_shmem(struct vm_area_struct *vma)
256{
257 return vma->vm_ops == &shmem_vm_ops;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800261static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Chris Downe809d5f2020-08-06 23:20:20 -0700263/*
264 * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and
265 * produces a novel ino for the newly allocated inode.
266 *
267 * It may also be called when making a hard link to permit the space needed by
268 * each dentry. However, in that case, no new inode number is needed since that
269 * internally draws from another pool of inode numbers (currently global
270 * get_next_ino()). This case is indicated by passing NULL as inop.
271 */
272#define SHMEM_INO_BATCH 1024
273static int shmem_reserve_inode(struct super_block *sb, ino_t *inop)
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800274{
275 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
Chris Downe809d5f2020-08-06 23:20:20 -0700276 ino_t ino;
277
278 if (!(sb->s_flags & SB_KERNMOUNT)) {
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800279 spin_lock(&sbinfo->stat_lock);
280 if (!sbinfo->free_inodes) {
281 spin_unlock(&sbinfo->stat_lock);
282 return -ENOSPC;
283 }
284 sbinfo->free_inodes--;
Chris Downe809d5f2020-08-06 23:20:20 -0700285 if (inop) {
286 ino = sbinfo->next_ino++;
287 if (unlikely(is_zero_ino(ino)))
288 ino = sbinfo->next_ino++;
289 if (unlikely(ino > UINT_MAX)) {
290 /*
291 * Emulate get_next_ino uint wraparound for
292 * compatibility
293 */
294 ino = 1;
295 }
296 *inop = ino;
297 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800298 spin_unlock(&sbinfo->stat_lock);
Chris Downe809d5f2020-08-06 23:20:20 -0700299 } else if (inop) {
300 /*
301 * __shmem_file_setup, one of our callers, is lock-free: it
302 * doesn't hold stat_lock in shmem_reserve_inode since
303 * max_inodes is always 0, and is called from potentially
304 * unknown contexts. As such, use a per-cpu batched allocator
305 * which doesn't require the per-sb stat_lock unless we are at
306 * the batch boundary.
307 */
308 ino_t *next_ino;
309 next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu());
310 ino = *next_ino;
311 if (unlikely(ino % SHMEM_INO_BATCH == 0)) {
312 spin_lock(&sbinfo->stat_lock);
313 ino = sbinfo->next_ino;
314 sbinfo->next_ino += SHMEM_INO_BATCH;
315 spin_unlock(&sbinfo->stat_lock);
316 if (unlikely(is_zero_ino(ino)))
317 ino++;
318 }
319 *inop = ino;
320 *next_ino = ++ino;
321 put_cpu();
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800322 }
Chris Downe809d5f2020-08-06 23:20:20 -0700323
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800324 return 0;
325}
326
327static void shmem_free_inode(struct super_block *sb)
328{
329 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
330 if (sbinfo->max_inodes) {
331 spin_lock(&sbinfo->stat_lock);
332 sbinfo->free_inodes++;
333 spin_unlock(&sbinfo->stat_lock);
334 }
335}
336
Randy Dunlap46711812008-03-19 17:00:41 -0700337/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700338 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @inode: inode to recalc
340 *
341 * We have to calculate the free blocks since the mm can drop
342 * undirtied hole pages behind our back.
343 *
344 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
345 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
346 *
347 * It has to be called with the spinlock held.
348 */
349static void shmem_recalc_inode(struct inode *inode)
350{
351 struct shmem_inode_info *info = SHMEM_I(inode);
352 long freed;
353
354 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
355 if (freed > 0) {
356 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700357 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700358 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360}
361
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700362bool shmem_charge(struct inode *inode, long pages)
363{
364 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700365 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700366
Mike Rapoport0f079692017-09-06 16:22:59 -0700367 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700368 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700369
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800370 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
371 inode->i_mapping->nrpages += pages;
372
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700373 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700374 info->alloced += pages;
375 inode->i_blocks += pages * BLOCKS_PER_PAGE;
376 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700377 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700378
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700379 return true;
380}
381
382void shmem_uncharge(struct inode *inode, long pages)
383{
384 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700385 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700386
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800387 /* nrpages adjustment done by __delete_from_page_cache() or caller */
388
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700389 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700390 info->alloced -= pages;
391 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
392 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700393 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700394
Mike Rapoport0f079692017-09-06 16:22:59 -0700395 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700396}
397
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700398/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500399 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700400 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500401static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700402 pgoff_t index, void *expected, void *replacement)
403{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500404 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700405 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700406
407 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700408 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500409 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700410 if (item != expected)
411 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500412 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700413 return 0;
414}
415
416/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700417 * Sometimes, before we decide whether to proceed or to fail, we must check
418 * that an entry was not already brought back from swap by a racing thread.
419 *
420 * Checking page is not enough: by the time a SwapCache page is locked, it
421 * might be reused, and again be SwapCache, using the same swap as before.
422 */
423static bool shmem_confirm_swap(struct address_space *mapping,
424 pgoff_t index, swp_entry_t swap)
425{
Matthew Wilcoxa12831b2017-11-22 08:34:58 -0500426 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700427}
428
429/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700430 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
431 *
432 * SHMEM_HUGE_NEVER:
433 * disables huge pages for the mount;
434 * SHMEM_HUGE_ALWAYS:
435 * enables huge pages for the mount;
436 * SHMEM_HUGE_WITHIN_SIZE:
437 * only allocate huge pages if the page will be fully within i_size,
438 * also respect fadvise()/madvise() hints;
439 * SHMEM_HUGE_ADVISE:
440 * only allocate huge pages if requested with fadvise()/madvise();
441 */
442
443#define SHMEM_HUGE_NEVER 0
444#define SHMEM_HUGE_ALWAYS 1
445#define SHMEM_HUGE_WITHIN_SIZE 2
446#define SHMEM_HUGE_ADVISE 3
447
448/*
449 * Special values.
450 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
451 *
452 * SHMEM_HUGE_DENY:
453 * disables huge on shm_mnt and all mounts, for emergency use;
454 * SHMEM_HUGE_FORCE:
455 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
456 *
457 */
458#define SHMEM_HUGE_DENY (-1)
459#define SHMEM_HUGE_FORCE (-2)
460
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700461#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700462/* ifdef here to avoid bloating shmem.o when not necessary */
463
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700464static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700465
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700466#if defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700467static int shmem_parse_huge(const char *str)
468{
469 if (!strcmp(str, "never"))
470 return SHMEM_HUGE_NEVER;
471 if (!strcmp(str, "always"))
472 return SHMEM_HUGE_ALWAYS;
473 if (!strcmp(str, "within_size"))
474 return SHMEM_HUGE_WITHIN_SIZE;
475 if (!strcmp(str, "advise"))
476 return SHMEM_HUGE_ADVISE;
477 if (!strcmp(str, "deny"))
478 return SHMEM_HUGE_DENY;
479 if (!strcmp(str, "force"))
480 return SHMEM_HUGE_FORCE;
481 return -EINVAL;
482}
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700483#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700484
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700485#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700486static const char *shmem_format_huge(int huge)
487{
488 switch (huge) {
489 case SHMEM_HUGE_NEVER:
490 return "never";
491 case SHMEM_HUGE_ALWAYS:
492 return "always";
493 case SHMEM_HUGE_WITHIN_SIZE:
494 return "within_size";
495 case SHMEM_HUGE_ADVISE:
496 return "advise";
497 case SHMEM_HUGE_DENY:
498 return "deny";
499 case SHMEM_HUGE_FORCE:
500 return "force";
501 default:
502 VM_BUG_ON(1);
503 return "bad_val";
504 }
505}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800506#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700507
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700508static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
509 struct shrink_control *sc, unsigned long nr_to_split)
510{
511 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800512 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700513 struct inode *inode;
514 struct shmem_inode_info *info;
515 struct page *page;
516 unsigned long batch = sc ? sc->nr_to_scan : 128;
517 int removed = 0, split = 0;
518
519 if (list_empty(&sbinfo->shrinklist))
520 return SHRINK_STOP;
521
522 spin_lock(&sbinfo->shrinklist_lock);
523 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
524 info = list_entry(pos, struct shmem_inode_info, shrinklist);
525
526 /* pin the inode */
527 inode = igrab(&info->vfs_inode);
528
529 /* inode is about to be evicted */
530 if (!inode) {
531 list_del_init(&info->shrinklist);
532 removed++;
533 goto next;
534 }
535
536 /* Check if there's anything to gain */
537 if (round_up(inode->i_size, PAGE_SIZE) ==
538 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800539 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700540 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700541 goto next;
542 }
543
544 list_move(&info->shrinklist, &list);
545next:
546 if (!--batch)
547 break;
548 }
549 spin_unlock(&sbinfo->shrinklist_lock);
550
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800551 list_for_each_safe(pos, next, &to_remove) {
552 info = list_entry(pos, struct shmem_inode_info, shrinklist);
553 inode = &info->vfs_inode;
554 list_del_init(&info->shrinklist);
555 iput(inode);
556 }
557
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700558 list_for_each_safe(pos, next, &list) {
559 int ret;
560
561 info = list_entry(pos, struct shmem_inode_info, shrinklist);
562 inode = &info->vfs_inode;
563
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700564 if (nr_to_split && split >= nr_to_split)
565 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700566
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700567 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700568 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
569 if (!page)
570 goto drop;
571
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700572 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700573 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700574 put_page(page);
575 goto drop;
576 }
577
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700578 /*
579 * Leave the inode on the list if we failed to lock
580 * the page at this time.
581 *
582 * Waiting for the lock may lead to deadlock in the
583 * reclaim path.
584 */
585 if (!trylock_page(page)) {
586 put_page(page);
587 goto leave;
588 }
589
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700590 ret = split_huge_page(page);
591 unlock_page(page);
592 put_page(page);
593
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700594 /* If split failed leave the inode on the list */
595 if (ret)
596 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700597
598 split++;
599drop:
600 list_del_init(&info->shrinklist);
601 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700602leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700603 iput(inode);
604 }
605
606 spin_lock(&sbinfo->shrinklist_lock);
607 list_splice_tail(&list, &sbinfo->shrinklist);
608 sbinfo->shrinklist_len -= removed;
609 spin_unlock(&sbinfo->shrinklist_lock);
610
611 return split;
612}
613
614static long shmem_unused_huge_scan(struct super_block *sb,
615 struct shrink_control *sc)
616{
617 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
618
619 if (!READ_ONCE(sbinfo->shrinklist_len))
620 return SHRINK_STOP;
621
622 return shmem_unused_huge_shrink(sbinfo, sc, 0);
623}
624
625static long shmem_unused_huge_count(struct super_block *sb,
626 struct shrink_control *sc)
627{
628 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
629 return READ_ONCE(sbinfo->shrinklist_len);
630}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700631#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700632
633#define shmem_huge SHMEM_HUGE_DENY
634
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700635static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
636 struct shrink_control *sc, unsigned long nr_to_split)
637{
638 return 0;
639}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700640#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700641
Yang Shi89fdcd22018-06-07 17:06:59 -0700642static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
643{
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700644 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Yang Shi89fdcd22018-06-07 17:06:59 -0700645 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
646 shmem_huge != SHMEM_HUGE_DENY)
647 return true;
648 return false;
649}
650
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700651/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700652 * Like add_to_page_cache_locked, but error if expected item has gone.
653 */
654static int shmem_add_to_page_cache(struct page *page,
655 struct address_space *mapping,
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700656 pgoff_t index, void *expected, gfp_t gfp,
657 struct mm_struct *charge_mm)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700658{
Matthew Wilcox552446a42017-12-01 13:25:14 -0500659 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
660 unsigned long i = 0;
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700661 unsigned long nr = compound_nr(page);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700662 int error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700663
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700664 VM_BUG_ON_PAGE(PageTail(page), page);
665 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800666 VM_BUG_ON_PAGE(!PageLocked(page), page);
667 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700668 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700669
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700670 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700671 page->mapping = mapping;
672 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700673
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700674 if (!PageSwapCache(page)) {
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -0700675 error = mem_cgroup_charge(page, charge_mm, gfp);
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700676 if (error) {
677 if (PageTransHuge(page)) {
678 count_vm_event(THP_FILE_FALLBACK);
679 count_vm_event(THP_FILE_FALLBACK_CHARGE);
680 }
681 goto error;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700682 }
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700683 }
684 cgroup_throttle_swaprate(page, gfp);
685
Matthew Wilcox552446a42017-12-01 13:25:14 -0500686 do {
687 void *entry;
688 xas_lock_irq(&xas);
689 entry = xas_find_conflict(&xas);
690 if (entry != expected)
691 xas_set_err(&xas, -EEXIST);
692 xas_create_range(&xas);
693 if (xas_error(&xas))
694 goto unlock;
695next:
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700696 xas_store(&xas, page);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500697 if (++i < nr) {
698 xas_next(&xas);
699 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700700 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500701 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700702 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700703 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500704 }
705 mapping->nrpages += nr;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700706 __mod_lruvec_page_state(page, NR_FILE_PAGES, nr);
707 __mod_lruvec_page_state(page, NR_SHMEM, nr);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500708unlock:
709 xas_unlock_irq(&xas);
710 } while (xas_nomem(&xas, gfp));
711
712 if (xas_error(&xas)) {
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700713 error = xas_error(&xas);
714 goto error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700715 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500716
717 return 0;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700718error:
719 page->mapping = NULL;
720 page_ref_sub(page, nr);
721 return error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700722}
723
724/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700725 * Like delete_from_page_cache, but substitutes swap for page.
726 */
727static void shmem_delete_from_page_cache(struct page *page, void *radswap)
728{
729 struct address_space *mapping = page->mapping;
730 int error;
731
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700732 VM_BUG_ON_PAGE(PageCompound(page), page);
733
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700734 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500735 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700736 page->mapping = NULL;
737 mapping->nrpages--;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700738 __dec_lruvec_page_state(page, NR_FILE_PAGES);
739 __dec_lruvec_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700740 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300741 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700742 BUG_ON(error);
743}
744
745/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500746 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700747 */
748static int shmem_free_swap(struct address_space *mapping,
749 pgoff_t index, void *radswap)
750{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700751 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700752
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500753 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700754 if (old != radswap)
755 return -ENOENT;
756 free_swap_and_cache(radix_to_swp_entry(radswap));
757 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700758}
759
760/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800761 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800762 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800763 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700764 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800765 * as long as the inode doesn't go away and racy results are not a problem.
766 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800767unsigned long shmem_partial_swap_usage(struct address_space *mapping,
768 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800769{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500770 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800771 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800772 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800773
774 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500775 xas_for_each(&xas, page, end - 1) {
776 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700777 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400778 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800779 swapped++;
780
781 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500782 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800783 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800784 }
785 }
786
787 rcu_read_unlock();
788
789 return swapped << PAGE_SHIFT;
790}
791
792/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800793 * Determine (in bytes) how many of the shmem object's pages mapped by the
794 * given vma is swapped out.
795 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700796 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800797 * as long as the inode doesn't go away and racy results are not a problem.
798 */
799unsigned long shmem_swap_usage(struct vm_area_struct *vma)
800{
801 struct inode *inode = file_inode(vma->vm_file);
802 struct shmem_inode_info *info = SHMEM_I(inode);
803 struct address_space *mapping = inode->i_mapping;
804 unsigned long swapped;
805
806 /* Be careful as we don't hold info->lock */
807 swapped = READ_ONCE(info->swapped);
808
809 /*
810 * The easier cases are when the shmem object has nothing in swap, or
811 * the vma maps it whole. Then we can simply use the stats that we
812 * already track.
813 */
814 if (!swapped)
815 return 0;
816
817 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
818 return swapped << PAGE_SHIFT;
819
820 /* Here comes the more involved part */
821 return shmem_partial_swap_usage(mapping,
822 linear_page_index(vma, vma->vm_start),
823 linear_page_index(vma, vma->vm_end));
824}
825
826/*
Hugh Dickins24513262012-01-20 14:34:21 -0800827 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
828 */
829void shmem_unlock_mapping(struct address_space *mapping)
830{
831 struct pagevec pvec;
832 pgoff_t indices[PAGEVEC_SIZE];
833 pgoff_t index = 0;
834
Mel Gorman86679822017-11-15 17:37:52 -0800835 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800836 /*
837 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
838 */
839 while (!mapping_unevictable(mapping)) {
840 /*
841 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
842 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
843 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700844 pvec.nr = find_get_entries(mapping, index,
845 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800846 if (!pvec.nr)
847 break;
848 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700849 pagevec_remove_exceptionals(&pvec);
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +0000850 check_move_unevictable_pages(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800851 pagevec_release(&pvec);
852 cond_resched();
853 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700854}
855
856/*
Hugh Dickins71725ed2020-04-06 20:07:57 -0700857 * Check whether a hole-punch or truncation needs to split a huge page,
858 * returning true if no split was required, or the split has been successful.
859 *
860 * Eviction (or truncation to 0 size) should never need to split a huge page;
861 * but in rare cases might do so, if shmem_undo_range() failed to trylock on
862 * head, and then succeeded to trylock on tail.
863 *
864 * A split can only succeed when there are no additional references on the
865 * huge page: so the split below relies upon find_get_entries() having stopped
866 * when it found a subpage of the huge page, without getting further references.
867 */
868static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff_t end)
869{
870 if (!PageTransCompound(page))
871 return true;
872
873 /* Just proceed to delete a huge page wholly within the range punched */
874 if (PageHead(page) &&
875 page->index >= start && page->index + HPAGE_PMD_NR <= end)
876 return true;
877
878 /* Try to split huge page, so we can truly punch the hole or truncate */
879 return split_huge_page(page) >= 0;
880}
881
882/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500883 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700884 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700885 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700886static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
887 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700889 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300891 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
892 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
893 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
894 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700895 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700896 pgoff_t indices[PAGEVEC_SIZE];
897 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700898 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700899 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700901 if (lend == -1)
902 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700903
Mel Gorman86679822017-11-15 17:37:52 -0800904 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700905 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700906 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700907 pvec.nr = find_get_entries(mapping, index,
908 min(end - index, (pgoff_t)PAGEVEC_SIZE),
909 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700910 if (!pvec.nr)
911 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700912 for (i = 0; i < pagevec_count(&pvec); i++) {
913 struct page *page = pvec.pages[i];
914
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700915 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700916 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700917 break;
918
Matthew Wilcox3159f942017-11-03 13:30:42 -0400919 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700920 if (unfalloc)
921 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700922 nr_swaps_freed += !shmem_free_swap(mapping,
923 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700924 continue;
925 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700926
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700927 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
928
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700929 if (!trylock_page(page))
930 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700931
Hugh Dickins71725ed2020-04-06 20:07:57 -0700932 if ((!unfalloc || !PageUptodate(page)) &&
933 page_mapping(page) == mapping) {
934 VM_BUG_ON_PAGE(PageWriteback(page), page);
935 if (shmem_punch_compound(page, start, end))
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700936 truncate_inode_page(mapping, page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700937 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700938 unlock_page(page);
939 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700940 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800941 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700942 cond_resched();
943 index++;
944 }
945
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700946 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700947 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700948 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700949 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300950 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700951 if (start > end) {
952 top = partial_end;
953 partial_end = 0;
954 }
955 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700956 set_page_dirty(page);
957 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300958 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700959 }
960 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700961 if (partial_end) {
962 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700963 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700964 if (page) {
965 zero_user_segment(page, 0, partial_end);
966 set_page_dirty(page);
967 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300968 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700969 }
970 }
971 if (start >= end)
972 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700973
974 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700975 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700976 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700977
978 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700979 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700980 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700981 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700982 /* If all gone or hole-punch or unfalloc, we're done */
983 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700984 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700985 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700986 index = start;
987 continue;
988 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700989 for (i = 0; i < pagevec_count(&pvec); i++) {
990 struct page *page = pvec.pages[i];
991
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700992 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700993 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700994 break;
995
Matthew Wilcox3159f942017-11-03 13:30:42 -0400996 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700997 if (unfalloc)
998 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700999 if (shmem_free_swap(mapping, index, page)) {
1000 /* Swap was replaced by page: retry */
1001 index--;
1002 break;
1003 }
1004 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001005 continue;
1006 }
1007
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001008 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001009
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001010 if (!unfalloc || !PageUptodate(page)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07001011 if (page_mapping(page) != mapping) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001012 /* Page was replaced by swap: retry */
1013 unlock_page(page);
1014 index--;
1015 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001016 }
Hugh Dickins71725ed2020-04-06 20:07:57 -07001017 VM_BUG_ON_PAGE(PageWriteback(page), page);
1018 if (shmem_punch_compound(page, start, end))
1019 truncate_inode_page(mapping, page);
Hugh Dickins0783ac95b2020-04-20 18:14:07 -07001020 else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07001021 /* Wipe the page and don't get stuck */
1022 clear_highpage(page);
1023 flush_dcache_page(page);
1024 set_page_dirty(page);
1025 if (index <
1026 round_up(start, HPAGE_PMD_NR))
1027 start = index + 1;
1028 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001029 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001030 unlock_page(page);
1031 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001032 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -08001033 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001034 index++;
1035 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001036
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001037 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001038 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001040 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001043void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1044{
1045 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001046 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
Hugh Dickins94c1e622011-06-27 16:18:03 -07001048EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
David Howellsa528d352017-01-31 16:46:22 +00001050static int shmem_getattr(const struct path *path, struct kstat *stat,
1051 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -07001052{
David Howellsa528d352017-01-31 16:46:22 +00001053 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -07001054 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -07001055 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -07001056
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001057 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001058 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001059 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001060 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001061 }
Yu Zhao44a30222015-09-08 15:03:33 -07001062 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -07001063
1064 if (is_huge_enabled(sb_info))
1065 stat->blksize = HPAGE_PMD_SIZE;
1066
Yu Zhao44a30222015-09-08 15:03:33 -07001067 return 0;
1068}
1069
Hugh Dickins94c1e622011-06-27 16:18:03 -07001070static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
David Howells75c3cfa2015-03-17 22:26:12 +00001072 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001073 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001074 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int error;
1076
Jan Kara31051c82016-05-26 16:55:18 +02001077 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001078 if (error)
1079 return error;
1080
Hugh Dickins94c1e622011-06-27 16:18:03 -07001081 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1082 loff_t oldsize = inode->i_size;
1083 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001084
David Herrmann40e041a2014-08-08 14:25:27 -07001085 /* protected by i_mutex */
1086 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1087 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1088 return -EPERM;
1089
Hugh Dickins94c1e622011-06-27 16:18:03 -07001090 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001091 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1092 oldsize, newsize);
1093 if (error)
1094 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001095 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001096 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001097 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001098 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001099 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001100 if (oldsize > holebegin)
1101 unmap_mapping_range(inode->i_mapping,
1102 holebegin, 0, 1);
1103 if (info->alloced)
1104 shmem_truncate_range(inode,
1105 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001106 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001107 if (oldsize > holebegin)
1108 unmap_mapping_range(inode->i_mapping,
1109 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001110
1111 /*
1112 * Part of the huge page can be beyond i_size: subject
1113 * to shrink under memory pressure.
1114 */
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001115 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001116 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001117 /*
1118 * _careful to defend against unlocked access to
1119 * ->shrink_list in shmem_unused_huge_shrink()
1120 */
1121 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001122 list_add_tail(&info->shrinklist,
1123 &sbinfo->shrinklist);
1124 sbinfo->shrinklist_len++;
1125 }
1126 spin_unlock(&sbinfo->shrinklist_lock);
1127 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001131 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001132 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001133 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return error;
1135}
1136
Al Viro1f895f72010-06-05 19:10:41 -04001137static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001140 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001142 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 shmem_unacct_size(info->flags, inode->i_size);
1144 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001145 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001146 if (!list_empty(&info->shrinklist)) {
1147 spin_lock(&sbinfo->shrinklist_lock);
1148 if (!list_empty(&info->shrinklist)) {
1149 list_del_init(&info->shrinklist);
1150 sbinfo->shrinklist_len--;
1151 }
1152 spin_unlock(&sbinfo->shrinklist_lock);
1153 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001154 while (!list_empty(&info->swaplist)) {
1155 /* Wait while shmem_unuse() is scanning this inode... */
1156 wait_var_event(&info->stop_eviction,
1157 !atomic_read(&info->stop_eviction));
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001158 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001159 /* ...but beware of the race if we peeked too early */
1160 if (!atomic_read(&info->stop_eviction))
1161 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001162 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Al Viro3ed47db2016-01-22 18:08:52 -05001164 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001165
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001166 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001167 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001168 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001169 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001172extern struct swap_info_struct *swap_info[];
1173
1174static int shmem_find_swap_entries(struct address_space *mapping,
1175 pgoff_t start, unsigned int nr_entries,
1176 struct page **entries, pgoff_t *indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001177 unsigned int type, bool frontswap)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001178{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001179 XA_STATE(xas, &mapping->i_pages, start);
1180 struct page *page;
Hugh Dickins87039542019-04-18 17:49:58 -07001181 swp_entry_t entry;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001182 unsigned int ret = 0;
1183
1184 if (!nr_entries)
1185 return 0;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001186
1187 rcu_read_lock();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001188 xas_for_each(&xas, page, ULONG_MAX) {
1189 if (xas_retry(&xas, page))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001190 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001191
1192 if (!xa_is_value(page))
1193 continue;
1194
Hugh Dickins87039542019-04-18 17:49:58 -07001195 entry = radix_to_swp_entry(page);
1196 if (swp_type(entry) != type)
1197 continue;
1198 if (frontswap &&
1199 !frontswap_test(swap_info[type], swp_offset(entry)))
1200 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001201
1202 indices[ret] = xas.xa_index;
1203 entries[ret] = page;
1204
1205 if (need_resched()) {
1206 xas_pause(&xas);
1207 cond_resched_rcu();
1208 }
1209 if (++ret == nr_entries)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001210 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001211 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001212 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001213
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001214 return ret;
1215}
1216
1217/*
1218 * Move the swapped pages for an inode to page cache. Returns the count
1219 * of pages swapped in, or the error in case of failure.
1220 */
1221static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec,
1222 pgoff_t *indices)
1223{
1224 int i = 0;
1225 int ret = 0;
1226 int error = 0;
1227 struct address_space *mapping = inode->i_mapping;
1228
1229 for (i = 0; i < pvec.nr; i++) {
1230 struct page *page = pvec.pages[i];
1231
1232 if (!xa_is_value(page))
1233 continue;
1234 error = shmem_swapin_page(inode, indices[i],
1235 &page, SGP_CACHE,
1236 mapping_gfp_mask(mapping),
1237 NULL, NULL);
1238 if (error == 0) {
1239 unlock_page(page);
1240 put_page(page);
1241 ret++;
1242 }
1243 if (error == -ENOMEM)
1244 break;
1245 error = 0;
1246 }
1247 return error ? error : ret;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001248}
1249
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001250/*
1251 * If swap found in inode, free it and move page from swapcache to filecache.
1252 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001253static int shmem_unuse_inode(struct inode *inode, unsigned int type,
1254 bool frontswap, unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001256 struct address_space *mapping = inode->i_mapping;
1257 pgoff_t start = 0;
1258 struct pagevec pvec;
1259 pgoff_t indices[PAGEVEC_SIZE];
1260 bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0);
1261 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001263 pagevec_init(&pvec);
1264 do {
1265 unsigned int nr_entries = PAGEVEC_SIZE;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001266
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001267 if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE)
1268 nr_entries = *fs_pages_to_unuse;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001269
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001270 pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries,
1271 pvec.pages, indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001272 type, frontswap);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001273 if (pvec.nr == 0) {
1274 ret = 0;
1275 break;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001276 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001277
1278 ret = shmem_unuse_swap_entries(inode, pvec, indices);
1279 if (ret < 0)
1280 break;
1281
1282 if (frontswap_partial) {
1283 *fs_pages_to_unuse -= ret;
1284 if (*fs_pages_to_unuse == 0) {
1285 ret = FRONTSWAP_PAGES_UNUSED;
1286 break;
1287 }
1288 }
1289
1290 start = indices[pvec.nr - 1];
1291 } while (true);
1292
1293 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296/*
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001297 * Read all the shared memory data that resides in the swap
1298 * device 'type' back into memory, so the swap device can be
1299 * unused.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001301int shmem_unuse(unsigned int type, bool frontswap,
1302 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001304 struct shmem_inode_info *info, *next;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001305 int error = 0;
1306
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001307 if (list_empty(&shmem_swaplist))
1308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001310 mutex_lock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001311 list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) {
1312 if (!info->swapped) {
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001313 list_del_init(&info->swaplist);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001314 continue;
1315 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001316 /*
1317 * Drop the swaplist mutex while searching the inode for swap;
1318 * but before doing so, make sure shmem_evict_inode() will not
1319 * remove placeholder inode from swaplist, nor let it be freed
1320 * (igrab() would protect from unlink, but not from unmount).
1321 */
1322 atomic_inc(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001323 mutex_unlock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001324
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001325 error = shmem_unuse_inode(&info->vfs_inode, type, frontswap,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001326 fs_pages_to_unuse);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001327 cond_resched();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001328
1329 mutex_lock(&shmem_swaplist_mutex);
1330 next = list_next_entry(info, swaplist);
1331 if (!info->swapped)
1332 list_del_init(&info->swaplist);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001333 if (atomic_dec_and_test(&info->stop_eviction))
1334 wake_up_var(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001335 if (error)
Hugh Dickins778dd892011-05-11 15:13:37 -07001336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001338 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001339
Hugh Dickins778dd892011-05-11 15:13:37 -07001340 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343/*
1344 * Move the page from the page cache to the swap cache.
1345 */
1346static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1347{
1348 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001351 swp_entry_t swap;
1352 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001354 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 mapping = page->mapping;
1357 index = page->index;
1358 inode = mapping->host;
1359 info = SHMEM_I(inode);
1360 if (info->flags & VM_LOCKED)
1361 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001362 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 goto redirty;
1364
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001365 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001366 * Our capabilities prevent regular writeback or sync from ever calling
1367 * shmem_writepage; but a stacking filesystem might use ->writepage of
1368 * its underlying filesystem, in which case tmpfs should write out to
1369 * swap only in response to memory pressure, and not for the writeback
1370 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001371 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001372 if (!wbc->for_reclaim) {
1373 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1374 goto redirty;
1375 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001376
1377 /*
1378 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1379 * value into swapfile.c, the only way we can correctly account for a
1380 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001381 *
1382 * That's okay for a page already fallocated earlier, but if we have
1383 * not yet completed the fallocation, then (a) we want to keep track
1384 * of this page in case we have to undo it, and (b) it may not be a
1385 * good idea to continue anyway, once we're pushing into swap. So
1386 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001387 */
1388 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001389 if (inode->i_private) {
1390 struct shmem_falloc *shmem_falloc;
1391 spin_lock(&inode->i_lock);
1392 shmem_falloc = inode->i_private;
1393 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001394 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001395 index >= shmem_falloc->start &&
1396 index < shmem_falloc->next)
1397 shmem_falloc->nr_unswapped++;
1398 else
1399 shmem_falloc = NULL;
1400 spin_unlock(&inode->i_lock);
1401 if (shmem_falloc)
1402 goto redirty;
1403 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001404 clear_highpage(page);
1405 flush_dcache_page(page);
1406 SetPageUptodate(page);
1407 }
1408
Huang Ying38d8b4e2017-07-06 15:37:18 -07001409 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001410 if (!swap.val)
1411 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001412
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001413 /*
1414 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001415 * if it's not already there. Do it now before the page is
1416 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001417 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001418 * we've incremented swapped, because shmem_unuse_inode() will
1419 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001420 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001421 mutex_lock(&shmem_swaplist_mutex);
1422 if (list_empty(&info->swaplist))
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001423 list_add(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001424
Yang Shi4afab1c2019-11-30 17:58:07 -08001425 if (add_to_swap_cache(page, swap,
1426 __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001427 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001428 shmem_recalc_inode(inode);
1429 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001430 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001431
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001432 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001433 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1434
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001435 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001436 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001437 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return 0;
1439 }
1440
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001441 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001442 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443redirty:
1444 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001445 if (wbc->for_reclaim)
1446 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1447 unlock_page(page);
1448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Hugh Dickins75edd342016-05-19 17:12:44 -07001451#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001452static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001453{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001454 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001455
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001456 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001457 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001458
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001459 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001460
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001461 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001462}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001463
1464static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1465{
1466 struct mempolicy *mpol = NULL;
1467 if (sbinfo->mpol) {
1468 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1469 mpol = sbinfo->mpol;
1470 mpol_get(mpol);
1471 spin_unlock(&sbinfo->stat_lock);
1472 }
1473 return mpol;
1474}
Hugh Dickins75edd342016-05-19 17:12:44 -07001475#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1476static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1477{
1478}
1479static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1480{
1481 return NULL;
1482}
1483#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1484#ifndef CONFIG_NUMA
1485#define vm_policy vm_private_data
1486#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001487
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001488static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1489 struct shmem_inode_info *info, pgoff_t index)
1490{
1491 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001492 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001493 /* Bias interleave by inode number to distribute better across nodes */
1494 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001495 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1496}
1497
1498static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1499{
1500 /* Drop reference taken by mpol_shared_policy_lookup() */
1501 mpol_cond_put(vma->vm_policy);
1502}
1503
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001504static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1505 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001508 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001509 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001511 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001512 vmf.vma = &pvma;
1513 vmf.address = 0;
1514 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001515 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001516
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001517 return page;
1518}
Mel Gorman18a2f372012-12-05 14:01:41 -08001519
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001520static struct page *shmem_alloc_hugepage(gfp_t gfp,
1521 struct shmem_inode_info *info, pgoff_t index)
1522{
1523 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001524 struct address_space *mapping = info->vfs_inode.i_mapping;
1525 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001526 struct page *page;
1527
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001528 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001529 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1530 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001531 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001532
1533 shmem_pseudo_vma_init(&pvma, info, hindex);
1534 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
David Rientjes19deb762019-09-04 12:54:20 -07001535 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001536 shmem_pseudo_vma_destroy(&pvma);
1537 if (page)
1538 prep_transhuge_page(page);
David Rientjesdcdf11e2020-04-06 20:04:25 -07001539 else
1540 count_vm_event(THP_FILE_FALLBACK);
Mel Gorman18a2f372012-12-05 14:01:41 -08001541 return page;
1542}
1543
1544static struct page *shmem_alloc_page(gfp_t gfp,
1545 struct shmem_inode_info *info, pgoff_t index)
1546{
1547 struct vm_area_struct pvma;
1548 struct page *page;
1549
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001550 shmem_pseudo_vma_init(&pvma, info, index);
1551 page = alloc_page_vma(gfp, &pvma, 0);
1552 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001553
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001554 return page;
1555}
1556
1557static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001558 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001559 pgoff_t index, bool huge)
1560{
Mike Rapoport0f079692017-09-06 16:22:59 -07001561 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001562 struct page *page;
1563 int nr;
1564 int err = -ENOSPC;
1565
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001566 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001567 huge = false;
1568 nr = huge ? HPAGE_PMD_NR : 1;
1569
Mike Rapoport0f079692017-09-06 16:22:59 -07001570 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001571 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001572
1573 if (huge)
1574 page = shmem_alloc_hugepage(gfp, info, index);
1575 else
1576 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001577 if (page) {
1578 __SetPageLocked(page);
1579 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001580 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001581 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001582
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001583 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001584 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001585failed:
1586 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001590 * When a page is moved from swapcache to shmem filecache (either by the
1591 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1592 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1593 * ignorance of the mapping it belongs to. If that mapping has special
1594 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1595 * we may need to copy to a suitable page before moving to filecache.
1596 *
1597 * In a future release, this may well be extended to respect cpuset and
1598 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1599 * but for now it is a simple matter of zone.
1600 */
1601static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1602{
1603 return page_zonenum(page) > gfp_zone(gfp);
1604}
1605
1606static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1607 struct shmem_inode_info *info, pgoff_t index)
1608{
1609 struct page *oldpage, *newpage;
1610 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001611 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001612 pgoff_t swap_index;
1613 int error;
1614
1615 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001616 entry.val = page_private(oldpage);
1617 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001618 swap_mapping = page_mapping(oldpage);
1619
1620 /*
1621 * We have arrived here because our zones are constrained, so don't
1622 * limit chance of success by further cpuset and node constraints.
1623 */
1624 gfp &= ~GFP_CONSTRAINT_MASK;
1625 newpage = shmem_alloc_page(gfp, info, index);
1626 if (!newpage)
1627 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001628
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001629 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001630 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001631 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001632
Hugh Dickins9956edf2016-11-10 10:46:11 -08001633 __SetPageLocked(newpage);
1634 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001635 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001636 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001637 SetPageSwapCache(newpage);
1638
1639 /*
1640 * Our caller will very soon move newpage out of swapcache, but it's
1641 * a nice clean interface for us to replace oldpage by newpage there.
1642 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001643 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001644 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001645 if (!error) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07001646 mem_cgroup_migrate(oldpage, newpage);
1647 __inc_lruvec_page_state(newpage, NR_FILE_PAGES);
1648 __dec_lruvec_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001649 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001650 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001651
Hugh Dickins0142ef62012-06-07 14:21:09 -07001652 if (unlikely(error)) {
1653 /*
1654 * Is this possible? I think not, now that our callers check
1655 * both PageSwapCache and page_private after getting page lock;
1656 * but be defensive. Reverse old to newpage for clear and free.
1657 */
1658 oldpage = newpage;
1659 } else {
Johannes Weiner6058eae2020-06-03 16:02:40 -07001660 lru_cache_add(newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001661 *pagep = newpage;
1662 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001663
1664 ClearPageSwapCache(oldpage);
1665 set_page_private(oldpage, 0);
1666
1667 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001668 put_page(oldpage);
1669 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001670 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001671}
1672
1673/*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001674 * Swap in the page pointed to by *pagep.
1675 * Caller has to make sure that *pagep contains a valid swapped page.
1676 * Returns 0 and the page in pagep if success. On failure, returns the
1677 * the error code and NULL in *pagep.
1678 */
1679static int shmem_swapin_page(struct inode *inode, pgoff_t index,
1680 struct page **pagep, enum sgp_type sgp,
1681 gfp_t gfp, struct vm_area_struct *vma,
1682 vm_fault_t *fault_type)
1683{
1684 struct address_space *mapping = inode->i_mapping;
1685 struct shmem_inode_info *info = SHMEM_I(inode);
1686 struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001687 struct page *page;
1688 swp_entry_t swap;
1689 int error;
1690
1691 VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
1692 swap = radix_to_swp_entry(*pagep);
1693 *pagep = NULL;
1694
1695 /* Look it up and read it in.. */
1696 page = lookup_swap_cache(swap, NULL, 0);
1697 if (!page) {
1698 /* Or update major stats only when swapin succeeds?? */
1699 if (fault_type) {
1700 *fault_type |= VM_FAULT_MAJOR;
1701 count_vm_event(PGMAJFAULT);
1702 count_memcg_event_mm(charge_mm, PGMAJFAULT);
1703 }
1704 /* Here we actually start the io */
1705 page = shmem_swapin(swap, gfp, info, index);
1706 if (!page) {
1707 error = -ENOMEM;
1708 goto failed;
1709 }
1710 }
1711
1712 /* We have to do this with page locked to prevent races */
1713 lock_page(page);
1714 if (!PageSwapCache(page) || page_private(page) != swap.val ||
1715 !shmem_confirm_swap(mapping, index, swap)) {
1716 error = -EEXIST;
1717 goto unlock;
1718 }
1719 if (!PageUptodate(page)) {
1720 error = -EIO;
1721 goto failed;
1722 }
1723 wait_on_page_writeback(page);
1724
1725 if (shmem_should_replace_page(page, gfp)) {
1726 error = shmem_replace_page(&page, gfp, info, index);
1727 if (error)
1728 goto failed;
1729 }
1730
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001731 error = shmem_add_to_page_cache(page, mapping, index,
1732 swp_to_radix_entry(swap), gfp,
1733 charge_mm);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001734 if (error)
1735 goto failed;
1736
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001737 spin_lock_irq(&info->lock);
1738 info->swapped--;
1739 shmem_recalc_inode(inode);
1740 spin_unlock_irq(&info->lock);
1741
1742 if (sgp == SGP_WRITE)
1743 mark_page_accessed(page);
1744
1745 delete_from_swap_cache(page);
1746 set_page_dirty(page);
1747 swap_free(swap);
1748
1749 *pagep = page;
1750 return 0;
1751failed:
1752 if (!shmem_confirm_swap(mapping, index, swap))
1753 error = -EEXIST;
1754unlock:
1755 if (page) {
1756 unlock_page(page);
1757 put_page(page);
1758 }
1759
1760 return error;
1761}
1762
1763/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001764 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 *
1766 * If we allocate a new one we do not mark it dirty. That's up to the
1767 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001768 * entry since a page cannot live in both the swap and page cache.
1769 *
Miles Chen28eb3c802019-09-23 15:39:34 -07001770 * vmf and fault_type are only supplied by shmem_fault:
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001771 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001773static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001774 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001775 struct vm_area_struct *vma, struct vm_fault *vmf,
1776 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001779 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001781 struct mm_struct *charge_mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001782 struct page *page;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001783 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001784 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001786 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001787 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001789 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001791 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1792 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793repeat:
Hugh Dickins75edd342016-05-19 17:12:44 -07001794 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001795 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001796 return -EINVAL;
1797 }
1798
1799 sbinfo = SHMEM_SB(inode->i_sb);
1800 charge_mm = vma ? vma->vm_mm : current->mm;
1801
1802 page = find_lock_entry(mapping, index);
1803 if (xa_is_value(page)) {
1804 error = shmem_swapin_page(inode, index, &page,
1805 sgp, gfp, vma, fault_type);
1806 if (error == -EEXIST)
1807 goto repeat;
1808
1809 *pagep = page;
1810 return error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001811 }
1812
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001813 if (page && sgp == SGP_WRITE)
1814 mark_page_accessed(page);
1815
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001816 /* fallocated page? */
1817 if (page && !PageUptodate(page)) {
1818 if (sgp != SGP_READ)
1819 goto clear;
1820 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001821 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001822 page = NULL;
1823 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001824 if (page || sgp == SGP_READ) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001825 *pagep = page;
1826 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001830 * Fast cache lookup did not find it:
1831 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001833
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001834 if (vma && userfaultfd_missing(vma)) {
1835 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1836 return 0;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001839 /* shmem_symlink() */
1840 if (mapping->a_ops != &shmem_aops)
1841 goto alloc_nohuge;
1842 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1843 goto alloc_nohuge;
1844 if (shmem_huge == SHMEM_HUGE_FORCE)
1845 goto alloc_huge;
1846 switch (sbinfo->huge) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001847 case SHMEM_HUGE_NEVER:
1848 goto alloc_nohuge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001849 case SHMEM_HUGE_WITHIN_SIZE: {
1850 loff_t i_size;
1851 pgoff_t off;
1852
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001853 off = round_up(index, HPAGE_PMD_NR);
1854 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1855 if (i_size >= HPAGE_PMD_SIZE &&
1856 i_size >> PAGE_SHIFT >= off)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001857 goto alloc_huge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001858
1859 fallthrough;
1860 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001861 case SHMEM_HUGE_ADVISE:
1862 if (sgp_huge == SGP_HUGE)
1863 goto alloc_huge;
1864 /* TODO: implement fadvise() hints */
1865 goto alloc_nohuge;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001868alloc_huge:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001869 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
1870 if (IS_ERR(page)) {
1871alloc_nohuge:
1872 page = shmem_alloc_and_acct_page(gfp, inode,
1873 index, false);
1874 }
1875 if (IS_ERR(page)) {
1876 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001877
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001878 error = PTR_ERR(page);
1879 page = NULL;
1880 if (error != -ENOSPC)
1881 goto unlock;
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001882 /*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001883 * Try to reclaim some space by splitting a huge page
1884 * beyond i_size on the filesystem.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001885 */
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001886 while (retry--) {
1887 int ret;
1888
1889 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1890 if (ret == SHRINK_STOP)
1891 break;
1892 if (ret)
1893 goto alloc_nohuge;
1894 }
1895 goto unlock;
1896 }
1897
1898 if (PageTransHuge(page))
1899 hindex = round_down(index, HPAGE_PMD_NR);
1900 else
1901 hindex = index;
1902
1903 if (sgp == SGP_WRITE)
1904 __SetPageReferenced(page);
1905
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001906 error = shmem_add_to_page_cache(page, mapping, hindex,
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001907 NULL, gfp & GFP_RECLAIM_MASK,
1908 charge_mm);
1909 if (error)
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001910 goto unacct;
Johannes Weiner6058eae2020-06-03 16:02:40 -07001911 lru_cache_add(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001912
1913 spin_lock_irq(&info->lock);
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001914 info->alloced += compound_nr(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001915 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
1916 shmem_recalc_inode(inode);
1917 spin_unlock_irq(&info->lock);
1918 alloced = true;
1919
1920 if (PageTransHuge(page) &&
1921 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1922 hindex + HPAGE_PMD_NR - 1) {
1923 /*
1924 * Part of the huge page is beyond i_size: subject
1925 * to shrink under memory pressure.
1926 */
1927 spin_lock(&sbinfo->shrinklist_lock);
1928 /*
1929 * _careful to defend against unlocked access to
1930 * ->shrink_list in shmem_unused_huge_shrink()
1931 */
1932 if (list_empty_careful(&info->shrinklist)) {
1933 list_add_tail(&info->shrinklist,
1934 &sbinfo->shrinklist);
1935 sbinfo->shrinklist_len++;
1936 }
1937 spin_unlock(&sbinfo->shrinklist_lock);
1938 }
1939
1940 /*
1941 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1942 */
1943 if (sgp == SGP_FALLOC)
1944 sgp = SGP_WRITE;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001945clear:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001946 /*
1947 * Let SGP_WRITE caller clear ends if write does not fill page;
1948 * but SGP_FALLOC on a page fallocated earlier must initialize
1949 * it now, lest undo on failure cancel our earlier guarantee.
1950 */
1951 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1952 struct page *head = compound_head(page);
1953 int i;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001954
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001955 for (i = 0; i < compound_nr(head); i++) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001956 clear_highpage(head + i);
1957 flush_dcache_page(head + i);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001958 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001959 SetPageUptodate(head);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001960 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001961
Hugh Dickins54af60422011-08-03 16:21:24 -07001962 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001963 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001964 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001965 if (alloced) {
1966 ClearPageDirty(page);
1967 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001968 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001969 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001970 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001971 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001972 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001973 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001974 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001975 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001976 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001977
Nick Piggind0217ac2007-07-19 01:47:03 -07001978 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001979 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001981unacct:
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001982 shmem_inode_unacct_blocks(inode, compound_nr(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001983
1984 if (PageTransHuge(page)) {
1985 unlock_page(page);
1986 put_page(page);
1987 goto alloc_nohuge;
1988 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001989unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001990 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001991 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001992 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001993 }
1994 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001995 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001996 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001997 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001998 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001999 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002000 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07002001 goto repeat;
2002 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002005/*
2006 * This is like autoremove_wake_function, but it removes the wait queue
2007 * entry unconditionally - even if something else had already woken the
2008 * target.
2009 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02002010static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002011{
2012 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02002013 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002014 return ret;
2015}
2016
Souptick Joarder20acce62018-06-07 17:09:17 -07002017static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Dave Jiang11bac802017-02-24 14:56:41 -08002019 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05002020 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002021 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002022 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07002023 int err;
2024 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002026 /*
2027 * Trinity finds that probing a hole which tmpfs is punching can
2028 * prevent the hole-punch from ever completing: which in turn
2029 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07002030 * faulting pages into the hole while it's being punched. Although
2031 * shmem_undo_range() does remove the additions, it may be unable to
2032 * keep up, as each new page needs its own unmap_mapping_range() call,
2033 * and the i_mmap tree grows ever slower to scan if new vmas are added.
2034 *
2035 * It does not matter if we sometimes reach this check just before the
2036 * hole-punch begins, so that one fault then races with the punch:
2037 * we just need to make racing faults a rare case.
2038 *
2039 * The implementation below would be much simpler if we just used a
2040 * standard mutex or completion: but we cannot take i_mutex in fault,
2041 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002042 */
2043 if (unlikely(inode->i_private)) {
2044 struct shmem_falloc *shmem_falloc;
2045
2046 spin_lock(&inode->i_lock);
2047 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002048 if (shmem_falloc &&
2049 shmem_falloc->waitq &&
2050 vmf->pgoff >= shmem_falloc->start &&
2051 vmf->pgoff < shmem_falloc->next) {
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002052 struct file *fpin;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002053 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002054 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002055
2056 ret = VM_FAULT_NOPAGE;
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002057 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
2058 if (fpin)
Hugh Dickins8e205f72014-07-23 14:00:10 -07002059 ret = VM_FAULT_RETRY;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002060
2061 shmem_falloc_waitq = shmem_falloc->waitq;
2062 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
2063 TASK_UNINTERRUPTIBLE);
2064 spin_unlock(&inode->i_lock);
2065 schedule();
2066
2067 /*
2068 * shmem_falloc_waitq points into the shmem_fallocate()
2069 * stack of the hole-punching task: shmem_falloc_waitq
2070 * is usually invalid by the time we reach here, but
2071 * finish_wait() does not dereference it in that case;
2072 * though i_lock needed lest racing with wake_up_all().
2073 */
2074 spin_lock(&inode->i_lock);
2075 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2076 spin_unlock(&inode->i_lock);
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002077
2078 if (fpin)
2079 fput(fpin);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002080 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002081 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002082 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002083 }
2084
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002085 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002086
2087 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2088 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002089 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002090 else if (vma->vm_flags & VM_HUGEPAGE)
2091 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002092
Souptick Joarder20acce62018-06-07 17:09:17 -07002093 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002094 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002095 if (err)
2096 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002097 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002100unsigned long shmem_get_unmapped_area(struct file *file,
2101 unsigned long uaddr, unsigned long len,
2102 unsigned long pgoff, unsigned long flags)
2103{
2104 unsigned long (*get_area)(struct file *,
2105 unsigned long, unsigned long, unsigned long, unsigned long);
2106 unsigned long addr;
2107 unsigned long offset;
2108 unsigned long inflated_len;
2109 unsigned long inflated_addr;
2110 unsigned long inflated_offset;
2111
2112 if (len > TASK_SIZE)
2113 return -ENOMEM;
2114
2115 get_area = current->mm->get_unmapped_area;
2116 addr = get_area(file, uaddr, len, pgoff, flags);
2117
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002118 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002119 return addr;
2120 if (IS_ERR_VALUE(addr))
2121 return addr;
2122 if (addr & ~PAGE_MASK)
2123 return addr;
2124 if (addr > TASK_SIZE - len)
2125 return addr;
2126
2127 if (shmem_huge == SHMEM_HUGE_DENY)
2128 return addr;
2129 if (len < HPAGE_PMD_SIZE)
2130 return addr;
2131 if (flags & MAP_FIXED)
2132 return addr;
2133 /*
2134 * Our priority is to support MAP_SHARED mapped hugely;
2135 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002136 * But if caller specified an address hint and we allocated area there
2137 * successfully, respect that as before.
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002138 */
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002139 if (uaddr == addr)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002140 return addr;
2141
2142 if (shmem_huge != SHMEM_HUGE_FORCE) {
2143 struct super_block *sb;
2144
2145 if (file) {
2146 VM_BUG_ON(file->f_op != &shmem_file_operations);
2147 sb = file_inode(file)->i_sb;
2148 } else {
2149 /*
2150 * Called directly from mm/mmap.c, or drivers/char/mem.c
2151 * for "/dev/zero", to create a shared anonymous object.
2152 */
2153 if (IS_ERR(shm_mnt))
2154 return addr;
2155 sb = shm_mnt->mnt_sb;
2156 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002157 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002158 return addr;
2159 }
2160
2161 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2162 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2163 return addr;
2164 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2165 return addr;
2166
2167 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2168 if (inflated_len > TASK_SIZE)
2169 return addr;
2170 if (inflated_len < len)
2171 return addr;
2172
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002173 inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002174 if (IS_ERR_VALUE(inflated_addr))
2175 return addr;
2176 if (inflated_addr & ~PAGE_MASK)
2177 return addr;
2178
2179 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2180 inflated_addr += offset - inflated_offset;
2181 if (inflated_offset > offset)
2182 inflated_addr += HPAGE_PMD_SIZE;
2183
2184 if (inflated_addr > TASK_SIZE - len)
2185 return addr;
2186 return inflated_addr;
2187}
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002190static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Al Viro496ad9a2013-01-23 17:07:38 -05002192 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002193 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002196static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2197 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Al Viro496ad9a2013-01-23 17:07:38 -05002199 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002200 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002202 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2203 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205#endif
2206
2207int shmem_lock(struct file *file, int lock, struct user_struct *user)
2208{
Al Viro496ad9a2013-01-23 17:07:38 -05002209 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct shmem_inode_info *info = SHMEM_I(inode);
2211 int retval = -ENOMEM;
2212
Hugh Dickinsea0dfeb2020-04-20 18:14:14 -07002213 /*
2214 * What serializes the accesses to info->flags?
2215 * ipc_lock_object() when called from shmctl_do_lock(),
2216 * no serialization needed when called from shm_destroy().
2217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (lock && !(info->flags & VM_LOCKED)) {
2219 if (!user_shm_lock(inode->i_size, user))
2220 goto out_nomem;
2221 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002222 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224 if (!lock && (info->flags & VM_LOCKED) && user) {
2225 user_shm_unlock(inode->i_size, user);
2226 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002227 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231out_nomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return retval;
2233}
2234
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002235static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002237 struct shmem_inode_info *info = SHMEM_I(file_inode(file));
2238
2239 if (info->seals & F_SEAL_FUTURE_WRITE) {
2240 /*
2241 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
2242 * "future write" seal active.
2243 */
2244 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
2245 return -EPERM;
2246
2247 /*
Nicolas Geoffray05d351102019-11-30 17:53:28 -08002248 * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as
2249 * MAP_SHARED and read-only, take care to not allow mprotect to
2250 * revert protections on such mappings. Do this only for shared
2251 * mappings. For private mappings, don't need to mask
2252 * VM_MAYWRITE as we still want them to be COW-writable.
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002253 */
Nicolas Geoffray05d351102019-11-30 17:53:28 -08002254 if (vma->vm_flags & VM_SHARED)
2255 vma->vm_flags &= ~(VM_MAYWRITE);
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002256 }
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 file_accessed(file);
2259 vma->vm_ops = &shmem_vm_ops;
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002260 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002261 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2262 (vma->vm_end & HPAGE_PMD_MASK)) {
2263 khugepaged_enter(vma, vma->vm_flags);
2264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 0;
2266}
2267
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002268static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002269 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 struct inode *inode;
2272 struct shmem_inode_info *info;
2273 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
Chris Downe809d5f2020-08-06 23:20:20 -07002274 ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Chris Downe809d5f2020-08-06 23:20:20 -07002276 if (shmem_reserve_inode(sb, &ino))
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002277 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 inode = new_inode(sb);
2280 if (inode) {
Chris Downe809d5f2020-08-06 23:20:20 -07002281 inode->i_ino = ino;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002282 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002284 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002285 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 info = SHMEM_I(inode);
2287 memset(info, 0, (char *)inode - (char *)info);
2288 spin_lock_init(&info->lock);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07002289 atomic_set(&info->stop_eviction, 0);
David Herrmann40e041a2014-08-08 14:25:27 -07002290 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002291 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002292 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002294 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002295 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 switch (mode & S_IFMT) {
2298 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002299 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 init_special_inode(inode, mode, dev);
2301 break;
2302 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002303 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 inode->i_op = &shmem_inode_operations;
2305 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002306 mpol_shared_policy_init(&info->policy,
2307 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 break;
2309 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002310 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /* Some things misbehave if size == 0 on a directory */
2312 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2313 inode->i_op = &shmem_dir_inode_operations;
2314 inode->i_fop = &simple_dir_operations;
2315 break;
2316 case S_IFLNK:
2317 /*
2318 * Must not load anything in the rbtree,
2319 * mpol_free_shared_policy will not be called.
2320 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002321 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 break;
2323 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002324
2325 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002326 } else
2327 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return inode;
2329}
2330
Johannes Weiner0cd61442014-04-03 14:47:46 -07002331bool shmem_mapping(struct address_space *mapping)
2332{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002333 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002334}
2335
Mike Rapoport8d103962017-09-06 16:23:02 -07002336static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2337 pmd_t *dst_pmd,
2338 struct vm_area_struct *dst_vma,
2339 unsigned long dst_addr,
2340 unsigned long src_addr,
2341 bool zeropage,
2342 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002343{
2344 struct inode *inode = file_inode(dst_vma->vm_file);
2345 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002346 struct address_space *mapping = inode->i_mapping;
2347 gfp_t gfp = mapping_gfp_mask(mapping);
2348 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002349 spinlock_t *ptl;
2350 void *page_kaddr;
2351 struct page *page;
2352 pte_t _dst_pte, *dst_pte;
2353 int ret;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002354 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002355
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002356 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002357 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002358 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002359
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002360 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002361 page = shmem_alloc_page(gfp, info, pgoff);
2362 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002363 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002364
Mike Rapoport8d103962017-09-06 16:23:02 -07002365 if (!zeropage) { /* mcopy_atomic */
2366 page_kaddr = kmap_atomic(page);
2367 ret = copy_from_user(page_kaddr,
2368 (const void __user *)src_addr,
2369 PAGE_SIZE);
2370 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002371
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002372 /* fallback to copy_from_user outside mmap_lock */
Mike Rapoport8d103962017-09-06 16:23:02 -07002373 if (unlikely(ret)) {
2374 *pagep = page;
2375 shmem_inode_unacct_blocks(inode, 1);
2376 /* don't free the page */
Andrea Arcangeli9e368252018-11-30 14:09:25 -08002377 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002378 }
2379 } else { /* mfill_zeropage_atomic */
2380 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002381 }
2382 } else {
2383 page = *pagep;
2384 *pagep = NULL;
2385 }
2386
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002387 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2388 __SetPageLocked(page);
2389 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002390 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002391
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002392 ret = -EFAULT;
2393 offset = linear_page_index(dst_vma, dst_addr);
2394 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2395 if (unlikely(offset >= max_off))
2396 goto out_release;
2397
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002398 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2399 gfp & GFP_RECLAIM_MASK, dst_mm);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002400 if (ret)
2401 goto out_release;
2402
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002403 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2404 if (dst_vma->vm_flags & VM_WRITE)
2405 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002406 else {
2407 /*
2408 * We don't set the pte dirty if the vma has no
2409 * VM_WRITE permission, so mark the page dirty or it
2410 * could be freed from under us. We could do it
2411 * unconditionally before unlock_page(), but doing it
2412 * only if VM_WRITE is not set is faster.
2413 */
2414 set_page_dirty(page);
2415 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002416
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002417 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002418
2419 ret = -EFAULT;
2420 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2421 if (unlikely(offset >= max_off))
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002422 goto out_release_unlock;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002423
2424 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002425 if (!pte_none(*dst_pte))
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002426 goto out_release_unlock;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002427
Johannes Weiner6058eae2020-06-03 16:02:40 -07002428 lru_cache_add(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002429
Yang Shi94b7cc02020-04-20 18:14:17 -07002430 spin_lock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002431 info->alloced++;
2432 inode->i_blocks += BLOCKS_PER_PAGE;
2433 shmem_recalc_inode(inode);
Yang Shi94b7cc02020-04-20 18:14:17 -07002434 spin_unlock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002435
2436 inc_mm_counter(dst_mm, mm_counter_file(page));
2437 page_add_file_rmap(page, false);
2438 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2439
2440 /* No need to invalidate - it was non-present before */
2441 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002442 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002443 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002444 ret = 0;
2445out:
2446 return ret;
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002447out_release_unlock:
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002448 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002449 ClearPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002450 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002451out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002452 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002453 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002454out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002455 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002456 goto out;
2457}
2458
Mike Rapoport8d103962017-09-06 16:23:02 -07002459int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2460 pmd_t *dst_pmd,
2461 struct vm_area_struct *dst_vma,
2462 unsigned long dst_addr,
2463 unsigned long src_addr,
2464 struct page **pagep)
2465{
2466 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2467 dst_addr, src_addr, false, pagep);
2468}
2469
2470int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2471 pmd_t *dst_pmd,
2472 struct vm_area_struct *dst_vma,
2473 unsigned long dst_addr)
2474{
2475 struct page *page = NULL;
2476
2477 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2478 dst_addr, 0, true, &page);
2479}
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002482static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002483static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002485#ifdef CONFIG_TMPFS_XATTR
2486static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2487#else
2488#define shmem_initxattrs NULL
2489#endif
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002492shmem_write_begin(struct file *file, struct address_space *mapping,
2493 loff_t pos, unsigned len, unsigned flags,
2494 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Nick Piggin800d15a2007-10-16 01:25:03 -07002496 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002497 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002498 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002499
2500 /* i_mutex is held by caller */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002501 if (unlikely(info->seals & (F_SEAL_GROW |
2502 F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
2503 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
David Herrmann40e041a2014-08-08 14:25:27 -07002504 return -EPERM;
2505 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2506 return -EPERM;
2507 }
2508
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002509 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002510}
2511
2512static int
2513shmem_write_end(struct file *file, struct address_space *mapping,
2514 loff_t pos, unsigned len, unsigned copied,
2515 struct page *page, void *fsdata)
2516{
2517 struct inode *inode = mapping->host;
2518
Hugh Dickinsd3602442008-02-04 22:28:44 -08002519 if (pos + copied > inode->i_size)
2520 i_size_write(inode, pos + copied);
2521
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002522 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002523 struct page *head = compound_head(page);
2524 if (PageTransCompound(page)) {
2525 int i;
2526
2527 for (i = 0; i < HPAGE_PMD_NR; i++) {
2528 if (head + i == page)
2529 continue;
2530 clear_highpage(head + i);
2531 flush_dcache_page(head + i);
2532 }
2533 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002534 if (copied < PAGE_SIZE) {
2535 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002536 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002537 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002538 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002539 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002540 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002541 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002542 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002543 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002544
Nick Piggin800d15a2007-10-16 01:25:03 -07002545 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
Al Viro2ba5bbe2014-04-02 20:00:02 -04002548static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Al Viro6e58e792014-02-03 17:07:03 -05002550 struct file *file = iocb->ki_filp;
2551 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002553 pgoff_t index;
2554 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002555 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002556 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002557 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002558 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002559
2560 /*
2561 * Might this read be for a stacking filesystem? Then when reading
2562 * holes of a sparse file, we actually need to allocate those pages,
2563 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2564 */
Al Viro777eda22014-12-17 04:46:46 -05002565 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002566 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002568 index = *ppos >> PAGE_SHIFT;
2569 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 for (;;) {
2572 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002573 pgoff_t end_index;
2574 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 loff_t i_size = i_size_read(inode);
2576
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002577 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (index > end_index)
2579 break;
2580 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002581 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (nr <= offset)
2583 break;
2584 }
2585
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002586 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002587 if (error) {
2588 if (error == -EINVAL)
2589 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 break;
2591 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002592 if (page) {
2593 if (sgp == SGP_CACHE)
2594 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002595 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /*
2599 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002600 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002602 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002604 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002606 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (nr <= offset) {
2608 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002609 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 break;
2611 }
2612 }
2613 nr -= offset;
2614
2615 if (page) {
2616 /*
2617 * If users can be writing to this page using arbitrary
2618 * virtual addresses, take care about potential aliasing
2619 * before reading the page on the kernel side.
2620 */
2621 if (mapping_writably_mapped(mapping))
2622 flush_dcache_page(page);
2623 /*
2624 * Mark the page accessed if we read the beginning.
2625 */
2626 if (!offset)
2627 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002628 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002630 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 /*
2634 * Ok, we have the page, and it's up-to-date, so
2635 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002637 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002638 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002640 index += offset >> PAGE_SHIFT;
2641 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002643 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002644 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 break;
Al Viro6e58e792014-02-03 17:07:03 -05002646 if (ret < nr) {
2647 error = -EFAULT;
2648 break;
2649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 cond_resched();
2651 }
2652
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002653 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002654 file_accessed(file);
2655 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002658/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002659 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002660 */
2661static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002662 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002663{
2664 struct page *page;
2665 struct pagevec pvec;
2666 pgoff_t indices[PAGEVEC_SIZE];
2667 bool done = false;
2668 int i;
2669
Mel Gorman86679822017-11-15 17:37:52 -08002670 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002671 pvec.nr = 1; /* start small: we may be there already */
2672 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002673 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002674 pvec.nr, pvec.pages, indices);
2675 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002676 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002677 index = end;
2678 break;
2679 }
2680 for (i = 0; i < pvec.nr; i++, index++) {
2681 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002682 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002683 done = true;
2684 break;
2685 }
2686 index = indices[i];
2687 }
2688 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002689 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002690 if (!PageUptodate(page))
2691 page = NULL;
2692 }
2693 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002694 (page && whence == SEEK_DATA) ||
2695 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002696 done = true;
2697 break;
2698 }
2699 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002700 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002701 pagevec_release(&pvec);
2702 pvec.nr = PAGEVEC_SIZE;
2703 cond_resched();
2704 }
2705 return index;
2706}
2707
Andrew Morton965c8e52012-12-17 15:59:39 -08002708static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002709{
2710 struct address_space *mapping = file->f_mapping;
2711 struct inode *inode = mapping->host;
2712 pgoff_t start, end;
2713 loff_t new_offset;
2714
Andrew Morton965c8e52012-12-17 15:59:39 -08002715 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2716 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002717 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002718 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002719 /* We're holding i_mutex so we can access i_size directly */
2720
Yufen Yu1a413642018-11-16 15:08:39 -08002721 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002722 offset = -ENXIO;
2723 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002724 start = offset >> PAGE_SHIFT;
2725 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002726 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002727 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002728 if (new_offset > offset) {
2729 if (new_offset < inode->i_size)
2730 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002731 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002732 offset = -ENXIO;
2733 else
2734 offset = inode->i_size;
2735 }
2736 }
2737
Hugh Dickins387aae62013-08-04 11:30:25 -07002738 if (offset >= 0)
2739 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002740 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002741 return offset;
2742}
2743
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002744static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2745 loff_t len)
2746{
Al Viro496ad9a2013-01-23 17:07:38 -05002747 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002748 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002749 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002750 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002751 pgoff_t start, index, end;
2752 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002753
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002754 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2755 return -EOPNOTSUPP;
2756
Al Viro59551022016-01-22 15:40:57 -05002757 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002758
2759 if (mode & FALLOC_FL_PUNCH_HOLE) {
2760 struct address_space *mapping = file->f_mapping;
2761 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2762 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002763 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002764
David Herrmann40e041a2014-08-08 14:25:27 -07002765 /* protected by i_mutex */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002766 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
David Herrmann40e041a2014-08-08 14:25:27 -07002767 error = -EPERM;
2768 goto out;
2769 }
2770
Hugh Dickins8e205f72014-07-23 14:00:10 -07002771 shmem_falloc.waitq = &shmem_falloc_waitq;
Chen Junaa71ecd2019-11-30 17:58:11 -08002772 shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002773 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2774 spin_lock(&inode->i_lock);
2775 inode->i_private = &shmem_falloc;
2776 spin_unlock(&inode->i_lock);
2777
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002778 if ((u64)unmap_end > (u64)unmap_start)
2779 unmap_mapping_range(mapping, unmap_start,
2780 1 + unmap_end - unmap_start, 0);
2781 shmem_truncate_range(inode, offset, offset + len - 1);
2782 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002783
2784 spin_lock(&inode->i_lock);
2785 inode->i_private = NULL;
2786 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002787 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002788 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002789 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002790 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002791 }
2792
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002793 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2794 error = inode_newsize_ok(inode, offset + len);
2795 if (error)
2796 goto out;
2797
David Herrmann40e041a2014-08-08 14:25:27 -07002798 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2799 error = -EPERM;
2800 goto out;
2801 }
2802
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002803 start = offset >> PAGE_SHIFT;
2804 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002805 /* Try to avoid a swapstorm if len is impossible to satisfy */
2806 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2807 error = -ENOSPC;
2808 goto out;
2809 }
2810
Hugh Dickins8e205f72014-07-23 14:00:10 -07002811 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002812 shmem_falloc.start = start;
2813 shmem_falloc.next = start;
2814 shmem_falloc.nr_falloced = 0;
2815 shmem_falloc.nr_unswapped = 0;
2816 spin_lock(&inode->i_lock);
2817 inode->i_private = &shmem_falloc;
2818 spin_unlock(&inode->i_lock);
2819
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002820 for (index = start; index < end; index++) {
2821 struct page *page;
2822
2823 /*
2824 * Good, the fallocate(2) manpage permits EINTR: we may have
2825 * been interrupted because we are using up too much memory.
2826 */
2827 if (signal_pending(current))
2828 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002829 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2830 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002831 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002832 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002833 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002834 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002835 if (index > start) {
2836 shmem_undo_range(inode,
2837 (loff_t)start << PAGE_SHIFT,
2838 ((loff_t)index << PAGE_SHIFT) - 1, true);
2839 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002840 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002841 }
2842
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002843 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002844 * Inform shmem_writepage() how far we have reached.
2845 * No need for lock or barrier: we have the page lock.
2846 */
2847 shmem_falloc.next++;
2848 if (!PageUptodate(page))
2849 shmem_falloc.nr_falloced++;
2850
2851 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002852 * If !PageUptodate, leave it that way so that freeable pages
2853 * can be recognized if we need to rollback on error later.
2854 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002855 * than free the pages we are allocating (and SGP_CACHE pages
2856 * might still be clean: we now need to mark those dirty too).
2857 */
2858 set_page_dirty(page);
2859 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002860 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002861 cond_resched();
2862 }
2863
2864 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2865 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002866 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002867undone:
2868 spin_lock(&inode->i_lock);
2869 inode->i_private = NULL;
2870 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002871out:
Al Viro59551022016-01-22 15:40:57 -05002872 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002873 return error;
2874}
2875
David Howells726c3342006-06-23 02:02:58 -07002876static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
David Howells726c3342006-06-23 02:02:58 -07002878 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002881 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002883 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002885 buf->f_bavail =
2886 buf->f_bfree = sbinfo->max_blocks -
2887 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002888 }
2889 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 buf->f_files = sbinfo->max_inodes;
2891 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893 /* else leave those fields 0 like simple_statfs */
2894 return 0;
2895}
2896
2897/*
2898 * File creation. Allocate an inode, and we're done..
2899 */
2900static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002901shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002903 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 int error = -ENOSPC;
2905
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002906 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002908 error = simple_acl_create(dir, inode);
2909 if (error)
2910 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002911 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002912 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002913 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002914 if (error && error != -EOPNOTSUPP)
2915 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002916
Al Viro718deb62009-12-16 19:35:36 -05002917 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002919 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 d_instantiate(dentry, inode);
2921 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
2923 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002924out_iput:
2925 iput(inode);
2926 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Al Viro60545d02013-06-07 01:20:27 -04002929static int
2930shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2931{
2932 struct inode *inode;
2933 int error = -ENOSPC;
2934
2935 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2936 if (inode) {
2937 error = security_inode_init_security(inode, dir,
2938 NULL,
2939 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002940 if (error && error != -EOPNOTSUPP)
2941 goto out_iput;
2942 error = simple_acl_create(dir, inode);
2943 if (error)
2944 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002945 d_tmpfile(dentry, inode);
2946 }
2947 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002948out_iput:
2949 iput(inode);
2950 return error;
Al Viro60545d02013-06-07 01:20:27 -04002951}
2952
Al Viro18bb1db2011-07-26 01:41:39 -04002953static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 int error;
2956
2957 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2958 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002959 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return 0;
2961}
2962
Al Viro4acdaf22011-07-26 01:42:34 -04002963static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002964 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2967}
2968
2969/*
2970 * Link a file..
2971 */
2972static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2973{
David Howells75c3cfa2015-03-17 22:26:12 +00002974 struct inode *inode = d_inode(old_dentry);
Darrick J. Wong29b00e602019-02-22 22:35:32 -08002975 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /*
2978 * No ordinary (disk based) filesystem counts links as inodes;
2979 * but each new link needs a new dentry, pinning lowmem, and
2980 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong1062af92019-02-21 08:48:09 -08002981 * But if an O_TMPFILE file is linked into the tmpfs, the
2982 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 */
Darrick J. Wong1062af92019-02-21 08:48:09 -08002984 if (inode->i_nlink) {
Chris Downe809d5f2020-08-06 23:20:20 -07002985 ret = shmem_reserve_inode(inode->i_sb, NULL);
Darrick J. Wong1062af92019-02-21 08:48:09 -08002986 if (ret)
2987 goto out;
2988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002991 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002992 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002993 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 dget(dentry); /* Extra pinning count for the created dentry */
2995 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002996out:
2997 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
3000static int shmem_unlink(struct inode *dir, struct dentry *dentry)
3001{
David Howells75c3cfa2015-03-17 22:26:12 +00003002 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08003004 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
3005 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003008 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003009 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 dput(dentry); /* Undo the count from "create" - this does all the work */
3011 return 0;
3012}
3013
3014static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
3015{
3016 if (!simple_empty(dentry))
3017 return -ENOTEMPTY;
3018
David Howells75c3cfa2015-03-17 22:26:12 +00003019 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003020 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return shmem_unlink(dir, dentry);
3022}
3023
Miklos Szeredi37456772014-07-23 15:15:34 +02003024static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
3025{
David Howellse36cb0b2015-01-29 12:02:35 +00003026 bool old_is_dir = d_is_dir(old_dentry);
3027 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02003028
3029 if (old_dir != new_dir && old_is_dir != new_is_dir) {
3030 if (old_is_dir) {
3031 drop_nlink(old_dir);
3032 inc_nlink(new_dir);
3033 } else {
3034 drop_nlink(new_dir);
3035 inc_nlink(old_dir);
3036 }
3037 }
3038 old_dir->i_ctime = old_dir->i_mtime =
3039 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00003040 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003041 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02003042
3043 return 0;
3044}
3045
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003046static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
3047{
3048 struct dentry *whiteout;
3049 int error;
3050
3051 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
3052 if (!whiteout)
3053 return -ENOMEM;
3054
3055 error = shmem_mknod(old_dir, whiteout,
3056 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
3057 dput(whiteout);
3058 if (error)
3059 return error;
3060
3061 /*
3062 * Cheat and hash the whiteout while the old dentry is still in
3063 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
3064 *
3065 * d_lookup() will consistently find one of them at this point,
3066 * not sure which one, but that isn't even important.
3067 */
3068 d_rehash(whiteout);
3069 return 0;
3070}
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072/*
3073 * The VFS layer already does all the dentry stuff for rename,
3074 * we just have to decrement the usage count for the target if
3075 * it exists so that the VFS layer correctly free's it when it
3076 * gets overwritten.
3077 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003078static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
David Howells75c3cfa2015-03-17 22:26:12 +00003080 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 int they_are_dirs = S_ISDIR(inode->i_mode);
3082
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003083 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003084 return -EINVAL;
3085
Miklos Szeredi37456772014-07-23 15:15:34 +02003086 if (flags & RENAME_EXCHANGE)
3087 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (!simple_empty(new_dentry))
3090 return -ENOTEMPTY;
3091
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003092 if (flags & RENAME_WHITEOUT) {
3093 int error;
3094
3095 error = shmem_whiteout(old_dir, old_dentry);
3096 if (error)
3097 return error;
3098 }
3099
David Howells75c3cfa2015-03-17 22:26:12 +00003100 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003102 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003103 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003104 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003107 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003108 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110
3111 old_dir->i_size -= BOGO_DIRENT_SIZE;
3112 new_dir->i_size += BOGO_DIRENT_SIZE;
3113 old_dir->i_ctime = old_dir->i_mtime =
3114 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003115 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return 0;
3117}
3118
3119static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3120{
3121 int error;
3122 int len;
3123 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003124 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003127 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return -ENAMETOOLONG;
3129
Joe Perches0825a6f2018-06-14 15:27:58 -07003130 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3131 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (!inode)
3133 return -ENOSPC;
3134
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003135 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003136 shmem_initxattrs, NULL);
Mateusz Nosek343c3d72020-04-06 20:07:54 -07003137 if (error && error != -EOPNOTSUPP) {
3138 iput(inode);
3139 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003140 }
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003143 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003144 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3145 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003146 iput(inode);
3147 return -ENOMEM;
3148 }
3149 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003151 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003152 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (error) {
3154 iput(inode);
3155 return error;
3156 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003157 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003159 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003160 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003162 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003163 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003166 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 d_instantiate(dentry, inode);
3168 dget(dentry);
3169 return 0;
3170}
3171
Al Virofceef392015-12-29 15:58:39 -05003172static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Al Virofceef392015-12-29 15:58:39 -05003174 mark_page_accessed(arg);
3175 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
Al Viro6b255392015-11-17 10:20:54 -05003178static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003179 struct inode *inode,
3180 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003183 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003184 if (!dentry) {
3185 page = find_get_page(inode->i_mapping, 0);
3186 if (!page)
3187 return ERR_PTR(-ECHILD);
3188 if (!PageUptodate(page)) {
3189 put_page(page);
3190 return ERR_PTR(-ECHILD);
3191 }
3192 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003193 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003194 if (error)
3195 return ERR_PTR(error);
3196 unlock_page(page);
3197 }
Al Virofceef392015-12-29 15:58:39 -05003198 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003199 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
Eric Parisb09e0fa2011-05-24 17:12:39 -07003202#ifdef CONFIG_TMPFS_XATTR
3203/*
3204 * Superblocks without xattr inode operations may get some security.* xattr
3205 * support from the LSM "for free". As soon as we have any other xattrs
3206 * like ACLs, we also need to implement the security.* handlers at
3207 * filesystem level, though.
3208 */
3209
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003210/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003211 * Callback for security_inode_init_security() for acquiring xattrs.
3212 */
3213static int shmem_initxattrs(struct inode *inode,
3214 const struct xattr *xattr_array,
3215 void *fs_info)
3216{
3217 struct shmem_inode_info *info = SHMEM_I(inode);
3218 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003219 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003220 size_t len;
3221
3222 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003223 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003224 if (!new_xattr)
3225 return -ENOMEM;
3226
3227 len = strlen(xattr->name) + 1;
3228 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3229 GFP_KERNEL);
3230 if (!new_xattr->name) {
Chengguang Xu3bef7352020-07-23 21:15:14 -07003231 kvfree(new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003232 return -ENOMEM;
3233 }
3234
3235 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3236 XATTR_SECURITY_PREFIX_LEN);
3237 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3238 xattr->name, len);
3239
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003240 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003241 }
3242
3243 return 0;
3244}
3245
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003246static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003247 struct dentry *unused, struct inode *inode,
3248 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003249{
Al Virob2968212016-04-10 20:48:24 -04003250 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003251
3252 name = xattr_full_name(handler, name);
3253 return simple_xattr_get(&info->xattrs, name, buffer, size);
3254}
3255
3256static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003257 struct dentry *unused, struct inode *inode,
3258 const char *name, const void *value,
3259 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003260{
Al Viro59301222016-05-27 10:19:30 -04003261 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003262
3263 name = xattr_full_name(handler, name);
Daniel Xua46a2292020-03-12 13:03:15 -07003264 return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003265}
3266
3267static const struct xattr_handler shmem_security_xattr_handler = {
3268 .prefix = XATTR_SECURITY_PREFIX,
3269 .get = shmem_xattr_handler_get,
3270 .set = shmem_xattr_handler_set,
3271};
3272
3273static const struct xattr_handler shmem_trusted_xattr_handler = {
3274 .prefix = XATTR_TRUSTED_PREFIX,
3275 .get = shmem_xattr_handler_get,
3276 .set = shmem_xattr_handler_set,
3277};
3278
Eric Parisb09e0fa2011-05-24 17:12:39 -07003279static const struct xattr_handler *shmem_xattr_handlers[] = {
3280#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003281 &posix_acl_access_xattr_handler,
3282 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003283#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003284 &shmem_security_xattr_handler,
3285 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003286 NULL
3287};
3288
Eric Parisb09e0fa2011-05-24 17:12:39 -07003289static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3290{
David Howells75c3cfa2015-03-17 22:26:12 +00003291 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003292 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003293}
3294#endif /* CONFIG_TMPFS_XATTR */
3295
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003296static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003297 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003298#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003299 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301};
3302
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003303static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003304 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003305#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003306 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003307#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003308};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003309
David M. Grimes91828a42006-10-17 00:09:45 -07003310static struct dentry *shmem_get_parent(struct dentry *child)
3311{
3312 return ERR_PTR(-ESTALE);
3313}
3314
3315static int shmem_match(struct inode *ino, void *vfh)
3316{
3317 __u32 *fh = vfh;
3318 __u64 inum = fh[2];
3319 inum = (inum << 32) | fh[1];
3320 return ino->i_ino == inum && fh[0] == ino->i_generation;
3321}
3322
Amir Goldstein12ba7802018-06-07 17:07:15 -07003323/* Find any alias of inode, but prefer a hashed alias */
3324static struct dentry *shmem_find_alias(struct inode *inode)
3325{
3326 struct dentry *alias = d_find_alias(inode);
3327
3328 return alias ?: d_find_any_alias(inode);
3329}
3330
3331
Christoph Hellwig480b1162007-10-21 16:42:13 -07003332static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3333 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003334{
David M. Grimes91828a42006-10-17 00:09:45 -07003335 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003336 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003337 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003338
Christoph Hellwig480b1162007-10-21 16:42:13 -07003339 if (fh_len < 3)
3340 return NULL;
3341
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003342 inum = fid->raw[2];
3343 inum = (inum << 32) | fid->raw[1];
3344
Christoph Hellwig480b1162007-10-21 16:42:13 -07003345 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3346 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003347 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003348 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003349 iput(inode);
3350 }
3351
Christoph Hellwig480b1162007-10-21 16:42:13 -07003352 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003353}
3354
Al Virob0b03822012-04-02 14:34:06 -04003355static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3356 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003357{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303358 if (*len < 3) {
3359 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003360 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303361 }
David M. Grimes91828a42006-10-17 00:09:45 -07003362
Al Viro1d3382cb2010-10-23 15:19:20 -04003363 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003364 /* Unfortunately insert_inode_hash is not idempotent,
3365 * so as we hash inodes here rather than at creation
3366 * time, we need a lock to ensure we only try
3367 * to do it once
3368 */
3369 static DEFINE_SPINLOCK(lock);
3370 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003371 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003372 __insert_inode_hash(inode,
3373 inode->i_ino + inode->i_generation);
3374 spin_unlock(&lock);
3375 }
3376
3377 fh[0] = inode->i_generation;
3378 fh[1] = inode->i_ino;
3379 fh[2] = ((__u64)inode->i_ino) >> 32;
3380
3381 *len = 3;
3382 return 1;
3383}
3384
Christoph Hellwig39655162007-10-21 16:42:17 -07003385static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003386 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003387 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003388 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003389};
3390
Al Viro626c3922019-09-08 20:28:06 -04003391enum shmem_param {
3392 Opt_gid,
3393 Opt_huge,
3394 Opt_mode,
3395 Opt_mpol,
3396 Opt_nr_blocks,
3397 Opt_nr_inodes,
3398 Opt_size,
3399 Opt_uid,
3400};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Al Viro5eede622019-12-16 13:33:32 -05003402static const struct constant_table shmem_param_enums_huge[] = {
Al Viro2710c957a2019-09-06 22:12:08 -04003403 {"never", SHMEM_HUGE_NEVER },
3404 {"always", SHMEM_HUGE_ALWAYS },
3405 {"within_size", SHMEM_HUGE_WITHIN_SIZE },
3406 {"advise", SHMEM_HUGE_ADVISE },
Al Viro2710c957a2019-09-06 22:12:08 -04003407 {}
3408};
3409
Al Virod7167b12019-09-07 07:23:15 -04003410const struct fs_parameter_spec shmem_fs_parameters[] = {
Al Viro626c3922019-09-08 20:28:06 -04003411 fsparam_u32 ("gid", Opt_gid),
Al Viro2710c957a2019-09-06 22:12:08 -04003412 fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge),
Al Viro626c3922019-09-08 20:28:06 -04003413 fsparam_u32oct("mode", Opt_mode),
3414 fsparam_string("mpol", Opt_mpol),
3415 fsparam_string("nr_blocks", Opt_nr_blocks),
3416 fsparam_string("nr_inodes", Opt_nr_inodes),
3417 fsparam_string("size", Opt_size),
3418 fsparam_u32 ("uid", Opt_uid),
3419 {}
3420};
3421
David Howellsf3235622019-03-25 16:38:31 +00003422static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param)
Al Viro626c3922019-09-08 20:28:06 -04003423{
David Howellsf3235622019-03-25 16:38:31 +00003424 struct shmem_options *ctx = fc->fs_private;
Al Viro626c3922019-09-08 20:28:06 -04003425 struct fs_parse_result result;
3426 unsigned long long size;
3427 char *rest;
3428 int opt;
3429
Al Virod7167b12019-09-07 07:23:15 -04003430 opt = fs_parse(fc, shmem_fs_parameters, param, &result);
David Howellsf3235622019-03-25 16:38:31 +00003431 if (opt < 0)
Al Viro626c3922019-09-08 20:28:06 -04003432 return opt;
Al Viro626c3922019-09-08 20:28:06 -04003433
3434 switch (opt) {
3435 case Opt_size:
3436 size = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003437 if (*rest == '%') {
3438 size <<= PAGE_SHIFT;
3439 size *= totalram_pages();
3440 do_div(size, 100);
3441 rest++;
3442 }
3443 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003444 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003445 ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE);
3446 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003447 break;
3448 case Opt_nr_blocks:
3449 ctx->blocks = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003450 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003451 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003452 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003453 break;
3454 case Opt_nr_inodes:
3455 ctx->inodes = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003456 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003457 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003458 ctx->seen |= SHMEM_SEEN_INODES;
Al Viro626c3922019-09-08 20:28:06 -04003459 break;
3460 case Opt_mode:
3461 ctx->mode = result.uint_32 & 07777;
3462 break;
3463 case Opt_uid:
3464 ctx->uid = make_kuid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003465 if (!uid_valid(ctx->uid))
Al Viro626c3922019-09-08 20:28:06 -04003466 goto bad_value;
3467 break;
3468 case Opt_gid:
3469 ctx->gid = make_kgid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003470 if (!gid_valid(ctx->gid))
Al Viro626c3922019-09-08 20:28:06 -04003471 goto bad_value;
3472 break;
3473 case Opt_huge:
3474 ctx->huge = result.uint_32;
3475 if (ctx->huge != SHMEM_HUGE_NEVER &&
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003476 !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Al Viro626c3922019-09-08 20:28:06 -04003477 has_transparent_hugepage()))
3478 goto unsupported_parameter;
Al Viroe04dc422019-09-08 19:20:12 -04003479 ctx->seen |= SHMEM_SEEN_HUGE;
Al Viro626c3922019-09-08 20:28:06 -04003480 break;
3481 case Opt_mpol:
3482 if (IS_ENABLED(CONFIG_NUMA)) {
3483 mpol_put(ctx->mpol);
3484 ctx->mpol = NULL;
3485 if (mpol_parse_str(param->string, &ctx->mpol))
3486 goto bad_value;
3487 break;
3488 }
3489 goto unsupported_parameter;
Al Viroe04dc422019-09-08 19:20:12 -04003490 }
3491 return 0;
3492
Al Viro626c3922019-09-08 20:28:06 -04003493unsupported_parameter:
Al Virof35aa2b2019-12-21 21:35:55 -05003494 return invalfc(fc, "Unsupported parameter '%s'", param->key);
Al Viro626c3922019-09-08 20:28:06 -04003495bad_value:
Al Virof35aa2b2019-12-21 21:35:55 -05003496 return invalfc(fc, "Bad value for '%s'", param->key);
Al Viroe04dc422019-09-08 19:20:12 -04003497}
3498
David Howellsf3235622019-03-25 16:38:31 +00003499static int shmem_parse_options(struct fs_context *fc, void *data)
Al Viroe04dc422019-09-08 19:20:12 -04003500{
David Howellsf3235622019-03-25 16:38:31 +00003501 char *options = data;
3502
Al Viro33f37c62019-10-09 22:48:01 -04003503 if (options) {
3504 int err = security_sb_eat_lsm_opts(options, &fc->security);
3505 if (err)
3506 return err;
3507 }
3508
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003509 while (options != NULL) {
Al Viro626c3922019-09-08 20:28:06 -04003510 char *this_char = options;
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003511 for (;;) {
3512 /*
3513 * NUL-terminate this option: unfortunately,
3514 * mount options form a comma-separated list,
3515 * but mpol's nodelist may also contain commas.
3516 */
3517 options = strchr(options, ',');
3518 if (options == NULL)
3519 break;
3520 options++;
3521 if (!isdigit(*options)) {
3522 options[-1] = '\0';
3523 break;
3524 }
3525 }
Al Viro626c3922019-09-08 20:28:06 -04003526 if (*this_char) {
3527 char *value = strchr(this_char,'=');
David Howellsf3235622019-03-25 16:38:31 +00003528 size_t len = 0;
Al Viro626c3922019-09-08 20:28:06 -04003529 int err;
3530
3531 if (value) {
3532 *value++ = '\0';
David Howellsf3235622019-03-25 16:38:31 +00003533 len = strlen(value);
Al Viro626c3922019-09-08 20:28:06 -04003534 }
David Howellsf3235622019-03-25 16:38:31 +00003535 err = vfs_parse_fs_string(fc, this_char, value, len);
3536 if (err < 0)
3537 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
3540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
David Howellsf3235622019-03-25 16:38:31 +00003543/*
3544 * Reconfigure a shmem filesystem.
3545 *
3546 * Note that we disallow change from limited->unlimited blocks/inodes while any
3547 * are in use; but we must separately disallow unlimited->limited, because in
3548 * that case we have no record of how much is already in use.
3549 */
3550static int shmem_reconfigure(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
David Howellsf3235622019-03-25 16:38:31 +00003552 struct shmem_options *ctx = fc->fs_private;
3553 struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003554 unsigned long inodes;
David Howellsf3235622019-03-25 16:38:31 +00003555 const char *err;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003556
3557 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003558 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
David Howellsf3235622019-03-25 16:38:31 +00003559 if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) {
3560 if (!sbinfo->max_blocks) {
3561 err = "Cannot retroactively limit size";
Al Viro0b5071d2019-09-08 18:49:18 -04003562 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003563 }
Al Viro0b5071d2019-09-08 18:49:18 -04003564 if (percpu_counter_compare(&sbinfo->used_blocks,
David Howellsf3235622019-03-25 16:38:31 +00003565 ctx->blocks) > 0) {
3566 err = "Too small a size for current use";
Al Viro0b5071d2019-09-08 18:49:18 -04003567 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003568 }
Al Viro0b5071d2019-09-08 18:49:18 -04003569 }
David Howellsf3235622019-03-25 16:38:31 +00003570 if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) {
3571 if (!sbinfo->max_inodes) {
3572 err = "Cannot retroactively limit inodes";
Al Viro0b5071d2019-09-08 18:49:18 -04003573 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003574 }
3575 if (ctx->inodes < inodes) {
3576 err = "Too few inodes for current use";
Al Viro0b5071d2019-09-08 18:49:18 -04003577 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003578 }
Al Viro0b5071d2019-09-08 18:49:18 -04003579 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003580
David Howellsf3235622019-03-25 16:38:31 +00003581 if (ctx->seen & SHMEM_SEEN_HUGE)
3582 sbinfo->huge = ctx->huge;
3583 if (ctx->seen & SHMEM_SEEN_BLOCKS)
3584 sbinfo->max_blocks = ctx->blocks;
3585 if (ctx->seen & SHMEM_SEEN_INODES) {
3586 sbinfo->max_inodes = ctx->inodes;
3587 sbinfo->free_inodes = ctx->inodes - inodes;
Al Viro0b5071d2019-09-08 18:49:18 -04003588 }
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003589
Greg Thelen5f001102013-02-22 16:36:01 -08003590 /*
3591 * Preserve previous mempolicy unless mpol remount option was specified.
3592 */
David Howellsf3235622019-03-25 16:38:31 +00003593 if (ctx->mpol) {
Greg Thelen5f001102013-02-22 16:36:01 -08003594 mpol_put(sbinfo->mpol);
David Howellsf3235622019-03-25 16:38:31 +00003595 sbinfo->mpol = ctx->mpol; /* transfers initial ref */
3596 ctx->mpol = NULL;
Greg Thelen5f001102013-02-22 16:36:01 -08003597 }
David Howellsf3235622019-03-25 16:38:31 +00003598 spin_unlock(&sbinfo->stat_lock);
3599 return 0;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003600out:
3601 spin_unlock(&sbinfo->stat_lock);
Al Virof35aa2b2019-12-21 21:35:55 -05003602 return invalfc(fc, "%s", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003604
Al Viro34c80b12011-12-08 21:32:45 -05003605static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003606{
Al Viro34c80b12011-12-08 21:32:45 -05003607 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003608
3609 if (sbinfo->max_blocks != shmem_default_max_blocks())
3610 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003611 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003612 if (sbinfo->max_inodes != shmem_default_max_inodes())
3613 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003614 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003615 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003616 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3617 seq_printf(seq, ",uid=%u",
3618 from_kuid_munged(&init_user_ns, sbinfo->uid));
3619 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3620 seq_printf(seq, ",gid=%u",
3621 from_kgid_munged(&init_user_ns, sbinfo->gid));
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003622#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003623 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3624 if (sbinfo->huge)
3625 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3626#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003627 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003628 return 0;
3629}
David Herrmann9183df22014-08-08 14:25:29 -07003630
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003631#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633static void shmem_put_super(struct super_block *sb)
3634{
Hugh Dickins602586a2010-08-17 15:23:56 -07003635 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3636
Chris Downe809d5f2020-08-06 23:20:20 -07003637 free_percpu(sbinfo->ino_batch);
Hugh Dickins602586a2010-08-17 15:23:56 -07003638 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003639 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003640 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 sb->s_fs_info = NULL;
3642}
3643
David Howellsf3235622019-03-25 16:38:31 +00003644static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
David Howellsf3235622019-03-25 16:38:31 +00003646 struct shmem_options *ctx = fc->fs_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003648 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003649 int err = -ENOMEM;
3650
3651 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003652 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003653 L1_CACHE_BYTES), GFP_KERNEL);
3654 if (!sbinfo)
3655 return -ENOMEM;
3656
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003657 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003659#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 /*
3661 * Per default we only allow half of the physical ram per
3662 * tmpfs instance, limiting inodes to one per page of lowmem;
3663 * but the internal instance is left unlimited.
3664 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003665 if (!(sb->s_flags & SB_KERNMOUNT)) {
David Howellsf3235622019-03-25 16:38:31 +00003666 if (!(ctx->seen & SHMEM_SEEN_BLOCKS))
3667 ctx->blocks = shmem_default_max_blocks();
3668 if (!(ctx->seen & SHMEM_SEEN_INODES))
3669 ctx->inodes = shmem_default_max_inodes();
Al Viroca4e0512013-08-31 12:57:10 -04003670 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003671 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
David M. Grimes91828a42006-10-17 00:09:45 -07003673 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003674 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003676 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677#endif
David Howellsf3235622019-03-25 16:38:31 +00003678 sbinfo->max_blocks = ctx->blocks;
3679 sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes;
Chris Downe809d5f2020-08-06 23:20:20 -07003680 if (sb->s_flags & SB_KERNMOUNT) {
3681 sbinfo->ino_batch = alloc_percpu(ino_t);
3682 if (!sbinfo->ino_batch)
3683 goto failed;
3684 }
David Howellsf3235622019-03-25 16:38:31 +00003685 sbinfo->uid = ctx->uid;
3686 sbinfo->gid = ctx->gid;
3687 sbinfo->mode = ctx->mode;
3688 sbinfo->huge = ctx->huge;
3689 sbinfo->mpol = ctx->mpol;
3690 ctx->mpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003692 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003693 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003694 goto failed;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003695 spin_lock_init(&sbinfo->shrinklist_lock);
3696 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003697
Hugh Dickins285b2c42011-08-03 16:21:20 -07003698 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003699 sb->s_blocksize = PAGE_SIZE;
3700 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 sb->s_magic = TMPFS_MAGIC;
3702 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003703 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003704#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003705 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003706#endif
3707#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003708 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003709#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003710 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003711
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003712 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 if (!inode)
3714 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003715 inode->i_uid = sbinfo->uid;
3716 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003717 sb->s_root = d_make_root(inode);
3718 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003719 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 return 0;
3721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722failed:
3723 shmem_put_super(sb);
3724 return err;
3725}
3726
David Howellsf3235622019-03-25 16:38:31 +00003727static int shmem_get_tree(struct fs_context *fc)
3728{
3729 return get_tree_nodev(fc, shmem_fill_super);
3730}
3731
3732static void shmem_free_fc(struct fs_context *fc)
3733{
3734 struct shmem_options *ctx = fc->fs_private;
3735
3736 if (ctx) {
3737 mpol_put(ctx->mpol);
3738 kfree(ctx);
3739 }
3740}
3741
3742static const struct fs_context_operations shmem_fs_context_ops = {
3743 .free = shmem_free_fc,
3744 .get_tree = shmem_get_tree,
3745#ifdef CONFIG_TMPFS
3746 .parse_monolithic = shmem_parse_options,
3747 .parse_param = shmem_parse_one,
3748 .reconfigure = shmem_reconfigure,
3749#endif
3750};
3751
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003752static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
3754static struct inode *shmem_alloc_inode(struct super_block *sb)
3755{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003756 struct shmem_inode_info *info;
3757 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3758 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003760 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}
3762
Al Viro74b1da52019-04-15 23:19:05 -04003763static void shmem_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003764{
Al Viro84e710d2016-04-15 00:58:55 -04003765 if (S_ISLNK(inode->i_mode))
3766 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003767 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3768}
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770static void shmem_destroy_inode(struct inode *inode)
3771{
Al Viro09208d12011-07-26 03:15:03 -04003772 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
3775
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003776static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003778 struct shmem_inode_info *info = foo;
3779 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
3781
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003782static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
3784 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3785 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003786 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003789static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003791 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003794static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003796 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003798 .write_begin = shmem_write_begin,
3799 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003801#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003802 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003803#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003804 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805};
3806
Helge Deller15ad7cd2006-12-06 20:40:36 -08003807static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003809 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003811 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003812 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003813 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003814 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003815 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003816 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003817 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818#endif
3819};
3820
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003821static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003822 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003823 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003824#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003825 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003826 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003827#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828};
3829
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003830static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831#ifdef CONFIG_TMPFS
3832 .create = shmem_create,
3833 .lookup = simple_lookup,
3834 .link = shmem_link,
3835 .unlink = shmem_unlink,
3836 .symlink = shmem_symlink,
3837 .mkdir = shmem_mkdir,
3838 .rmdir = shmem_rmdir,
3839 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003840 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003841 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003843#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003844 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003845#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003846#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003847 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003848 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003849#endif
3850};
3851
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003852static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003853#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003854 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003855#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003856#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003857 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003858 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003859#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860};
3861
Hugh Dickins759b9772007-03-05 00:30:28 -08003862static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 .alloc_inode = shmem_alloc_inode,
Al Viro74b1da52019-04-15 23:19:05 -04003864 .free_inode = shmem_free_in_core_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 .destroy_inode = shmem_destroy_inode,
3866#ifdef CONFIG_TMPFS
3867 .statfs = shmem_statfs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003868 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869#endif
Al Viro1f895f72010-06-05 19:10:41 -04003870 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 .drop_inode = generic_delete_inode,
3872 .put_super = shmem_put_super,
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003873#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003874 .nr_cached_objects = shmem_unused_huge_count,
3875 .free_cached_objects = shmem_unused_huge_scan,
3876#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877};
3878
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003879static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003880 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003881 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882#ifdef CONFIG_NUMA
3883 .set_policy = shmem_set_policy,
3884 .get_policy = shmem_get_policy,
3885#endif
3886};
3887
David Howellsf3235622019-03-25 16:38:31 +00003888int shmem_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
David Howellsf3235622019-03-25 16:38:31 +00003890 struct shmem_options *ctx;
3891
3892 ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL);
3893 if (!ctx)
3894 return -ENOMEM;
3895
3896 ctx->mode = 0777 | S_ISVTX;
3897 ctx->uid = current_fsuid();
3898 ctx->gid = current_fsgid();
3899
3900 fc->fs_private = ctx;
3901 fc->ops = &shmem_fs_context_ops;
3902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903}
3904
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003905static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 .owner = THIS_MODULE,
3907 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00003908 .init_fs_context = shmem_init_fs_context,
3909#ifdef CONFIG_TMPFS
Al Virod7167b12019-09-07 07:23:15 -04003910 .parameters = shmem_fs_parameters,
David Howellsf3235622019-03-25 16:38:31 +00003911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003913 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003916int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
3918 int error;
3919
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003920 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003922 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003924 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 goto out2;
3926 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003927
Al Viroca4e0512013-08-31 12:57:10 -04003928 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (IS_ERR(shm_mnt)) {
3930 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003931 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 goto out1;
3933 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003934
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003935#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003936 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003937 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3938 else
3939 shmem_huge = 0; /* just in case it was patched */
3940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 return 0;
3942
3943out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003944 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003946 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 shm_mnt = ERR_PTR(error);
3948 return error;
3949}
Matt Mackall853ac432009-01-06 14:40:20 -08003950
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003951#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003952static ssize_t shmem_enabled_show(struct kobject *kobj,
3953 struct kobj_attribute *attr, char *buf)
3954{
Colin Ian King26083eb62019-11-30 17:58:04 -08003955 static const int values[] = {
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003956 SHMEM_HUGE_ALWAYS,
3957 SHMEM_HUGE_WITHIN_SIZE,
3958 SHMEM_HUGE_ADVISE,
3959 SHMEM_HUGE_NEVER,
3960 SHMEM_HUGE_DENY,
3961 SHMEM_HUGE_FORCE,
3962 };
3963 int i, count;
3964
3965 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3966 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3967
3968 count += sprintf(buf + count, fmt,
3969 shmem_format_huge(values[i]));
3970 }
3971 buf[count - 1] = '\n';
3972 return count;
3973}
3974
3975static ssize_t shmem_enabled_store(struct kobject *kobj,
3976 struct kobj_attribute *attr, const char *buf, size_t count)
3977{
3978 char tmp[16];
3979 int huge;
3980
3981 if (count + 1 > sizeof(tmp))
3982 return -EINVAL;
3983 memcpy(tmp, buf, count);
3984 tmp[count] = '\0';
3985 if (count && tmp[count - 1] == '\n')
3986 tmp[count - 1] = '\0';
3987
3988 huge = shmem_parse_huge(tmp);
3989 if (huge == -EINVAL)
3990 return -EINVAL;
3991 if (!has_transparent_hugepage() &&
3992 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3993 return -EINVAL;
3994
3995 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003996 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003997 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3998 return count;
3999}
4000
4001struct kobj_attribute shmem_enabled_attr =
4002 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004003#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004004
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004005#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004006bool shmem_huge_enabled(struct vm_area_struct *vma)
4007{
4008 struct inode *inode = file_inode(vma->vm_file);
4009 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
4010 loff_t i_size;
4011 pgoff_t off;
4012
Yang Shic0630662019-07-18 15:57:27 -07004013 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
4014 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
4015 return false;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004016 if (shmem_huge == SHMEM_HUGE_FORCE)
4017 return true;
4018 if (shmem_huge == SHMEM_HUGE_DENY)
4019 return false;
4020 switch (sbinfo->huge) {
4021 case SHMEM_HUGE_NEVER:
4022 return false;
4023 case SHMEM_HUGE_ALWAYS:
4024 return true;
4025 case SHMEM_HUGE_WITHIN_SIZE:
4026 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
4027 i_size = round_up(i_size_read(inode), PAGE_SIZE);
4028 if (i_size >= HPAGE_PMD_SIZE &&
4029 i_size >> PAGE_SHIFT >= off)
4030 return true;
Joe Perchese4a9bc52020-04-06 20:08:39 -07004031 fallthrough;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004032 case SHMEM_HUGE_ADVISE:
4033 /* TODO: implement fadvise() hints */
4034 return (vma->vm_flags & VM_HUGEPAGE);
4035 default:
4036 VM_BUG_ON(1);
4037 return false;
4038 }
4039}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004040#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004041
Matt Mackall853ac432009-01-06 14:40:20 -08004042#else /* !CONFIG_SHMEM */
4043
4044/*
4045 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
4046 *
4047 * This is intended for small system where the benefits of the full
4048 * shmem code (swap-backed and resource-limited) are outweighed by
4049 * their complexity. On systems without swap this code should be
4050 * effectively equivalent, but much lighter weight.
4051 */
4052
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004053static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08004054 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00004055 .init_fs_context = ramfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04004056 .parameters = ramfs_fs_parameters,
Matt Mackall853ac432009-01-06 14:40:20 -08004057 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08004058 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08004059};
4060
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004061int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08004062{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004063 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08004064
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004065 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08004066 BUG_ON(IS_ERR(shm_mnt));
4067
4068 return 0;
4069}
4070
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08004071int shmem_unuse(unsigned int type, bool frontswap,
4072 unsigned long *fs_pages_to_unuse)
Matt Mackall853ac432009-01-06 14:40:20 -08004073{
4074 return 0;
4075}
4076
Hugh Dickins3f96b792009-09-21 17:03:37 -07004077int shmem_lock(struct file *file, int lock, struct user_struct *user)
4078{
4079 return 0;
4080}
4081
Hugh Dickins24513262012-01-20 14:34:21 -08004082void shmem_unlock_mapping(struct address_space *mapping)
4083{
4084}
4085
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07004086#ifdef CONFIG_MMU
4087unsigned long shmem_get_unmapped_area(struct file *file,
4088 unsigned long addr, unsigned long len,
4089 unsigned long pgoff, unsigned long flags)
4090{
4091 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
4092}
4093#endif
4094
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004095void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07004096{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004097 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07004098}
4099EXPORT_SYMBOL_GPL(shmem_truncate_range);
4100
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004101#define shmem_vm_ops generic_file_vm_ops
4102#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03004103#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004104#define shmem_acct_size(flags, size) 0
4105#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08004106
4107#endif /* CONFIG_SHMEM */
4108
4109/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Matthew Auld703321b2017-10-06 23:18:13 +01004111static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00004112 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04004115 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Matthew Auld703321b2017-10-06 23:18:13 +01004117 if (IS_ERR(mnt))
4118 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Hugh Dickins285b2c42011-08-03 16:21:20 -07004120 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return ERR_PTR(-EINVAL);
4122
4123 if (shmem_acct_size(flags, size))
4124 return ERR_PTR(-ENOMEM);
4125
Al Viro93dec2d2018-07-08 23:02:03 -04004126 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
4127 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04004128 if (unlikely(!inode)) {
4129 shmem_unacct_size(flags, size);
4130 return ERR_PTR(-ENOSPC);
4131 }
Eric Parisc7277092013-12-02 11:24:19 +00004132 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02004134 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05004135 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04004136 if (!IS_ERR(res))
4137 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
4138 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05004139 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04004140 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05004141 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
Eric Parisc7277092013-12-02 11:24:19 +00004143
4144/**
4145 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
4146 * kernel internal. There will be NO LSM permission checks against the
4147 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07004148 * higher layer. The users are the big_key and shm implementations. LSM
4149 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00004150 * @name: name for dentry (to be seen in /proc/<pid>/maps
4151 * @size: size to be set for the file
4152 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4153 */
4154struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
4155{
Matthew Auld703321b2017-10-06 23:18:13 +01004156 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00004157}
4158
4159/**
4160 * shmem_file_setup - get an unlinked file living in tmpfs
4161 * @name: name for dentry (to be seen in /proc/<pid>/maps
4162 * @size: size to be set for the file
4163 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4164 */
4165struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
4166{
Matthew Auld703321b2017-10-06 23:18:13 +01004167 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00004168}
Keith Packard395e0dd2008-06-20 00:08:06 -07004169EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Randy Dunlap46711812008-03-19 17:00:41 -07004171/**
Matthew Auld703321b2017-10-06 23:18:13 +01004172 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
4173 * @mnt: the tmpfs mount where the file will be created
4174 * @name: name for dentry (to be seen in /proc/<pid>/maps
4175 * @size: size to be set for the file
4176 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4177 */
4178struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
4179 loff_t size, unsigned long flags)
4180{
4181 return __shmem_file_setup(mnt, name, size, flags, 0);
4182}
4183EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4184
4185/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
4188 */
4189int shmem_zero_setup(struct vm_area_struct *vma)
4190{
4191 struct file *file;
4192 loff_t size = vma->vm_end - vma->vm_start;
4193
Hugh Dickins66fc1302015-06-14 09:48:09 -07004194 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004195 * Cloning a new file under mmap_lock leads to a lock ordering conflict
Hugh Dickins66fc1302015-06-14 09:48:09 -07004196 * between XFS directory reading and selinux: since this file is only
4197 * accessible to the user through its mapping, use S_PRIVATE flag to
4198 * bypass file security, in the same way as shmem_kernel_file_setup().
4199 */
Matthew Auld703321b2017-10-06 23:18:13 +01004200 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 if (IS_ERR(file))
4202 return PTR_ERR(file);
4203
4204 if (vma->vm_file)
4205 fput(vma->vm_file);
4206 vma->vm_file = file;
4207 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004208
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004209 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004210 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4211 (vma->vm_end & HPAGE_PMD_MASK)) {
4212 khugepaged_enter(vma, vma->vm_flags);
4213 }
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return 0;
4216}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004217
4218/**
4219 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4220 * @mapping: the page's address_space
4221 * @index: the page index
4222 * @gfp: the page allocator flags to use if allocating
4223 *
4224 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4225 * with any new page allocations done using the specified allocation flags.
4226 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4227 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4228 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4229 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004230 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4231 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004232 */
4233struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4234 pgoff_t index, gfp_t gfp)
4235{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004236#ifdef CONFIG_SHMEM
4237 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004238 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004239 int error;
4240
4241 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004242 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004243 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004244 if (error)
4245 page = ERR_PTR(error);
4246 else
4247 unlock_page(page);
4248 return page;
4249#else
4250 /*
4251 * The tiny !SHMEM case uses ramfs without swap
4252 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004253 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004254#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004255}
4256EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);