blob: b1f0f54470fb4d98002f854e4e24f0284cadcca8 [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>
Matt Mackall853ac432009-01-06 14:40:20 -080039
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080040#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
41
Matt Mackall853ac432009-01-06 14:40:20 -080042static struct vfsmount *shm_mnt;
43
44#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * This virtual memory filesystem is heavily based on the ramfs. It
47 * extends ramfs by the ability to use swap and honor resource limits
48 * which makes it a completely usable filesystem.
49 */
50
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070051#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070052#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010053#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080054#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/string.h>
57#include <linux/slab.h>
58#include <linux/backing-dev.h>
59#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070062#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070063#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070064#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070065#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/security.h>
67#include <linux/swapops.h>
68#include <linux/mempolicy.h>
69#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000070#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070071#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070072#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080073#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040074#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070075#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070076#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070077#include <uapi/linux/memfd.h>
Mike Rapoportcfda0522017-02-22 15:43:37 -080078#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080079#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030080#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070081
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080082#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <asm/pgtable.h>
84
Mel Gormandd56b042015-11-06 16:28:43 -080085#include "internal.h"
86
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030087#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
88#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Pretend that each entry is of this size in directory's i_size */
91#define BOGO_DIRENT_SIZE 20
92
Hugh Dickins69f07ec2011-08-03 16:21:26 -070093/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
94#define SHORT_SYMLINK_LEN 128
95
Hugh Dickins1aac1402012-05-29 15:06:42 -070096/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070097 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
98 * inode->i_private (with i_mutex making sure that it has only one user at
99 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700100 */
101struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700102 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700103 pgoff_t start; /* start of range currently being fallocated */
104 pgoff_t next; /* the next page offset to be fallocated */
105 pgoff_t nr_falloced; /* how many new pages have been fallocated */
106 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
107};
108
Andrew Mortonb76db732008-02-08 04:21:49 -0800109#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800110static unsigned long shmem_default_max_blocks(void)
111{
112 return totalram_pages / 2;
113}
114
115static unsigned long shmem_default_max_inodes(void)
116{
Arun KS3d6357d2018-12-28 00:34:20 -0800117 unsigned long nr_pages = totalram_pages;
118 return min(nr_pages - totalhigh_pages, nr_pages / 2);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800119}
Andrew Mortonb76db732008-02-08 04:21:49 -0800120#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800121
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700122static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
123static int shmem_replace_page(struct page **pagep, gfp_t gfp,
124 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700125static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700126 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800127 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700128 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700129
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700130int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700131 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700132{
133 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800134 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
138{
139 return sb->s_fs_info;
140}
141
142/*
143 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
144 * for shared memory and for shared anonymous (/dev/zero) mappings
145 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
146 * consistent with the pre-accounting of private mappings ...
147 */
148static inline int shmem_acct_size(unsigned long flags, loff_t size)
149{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000150 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000151 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154static inline void shmem_unacct_size(unsigned long flags, loff_t size)
155{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000156 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 vm_unacct_memory(VM_ACCT(size));
158}
159
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700160static inline int shmem_reacct_size(unsigned long flags,
161 loff_t oldsize, loff_t newsize)
162{
163 if (!(flags & VM_NORESERVE)) {
164 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
165 return security_vm_enough_memory_mm(current->mm,
166 VM_ACCT(newsize) - VM_ACCT(oldsize));
167 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
168 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
169 }
170 return 0;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700175 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
177 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
178 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700179static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700181 if (!(flags & VM_NORESERVE))
182 return 0;
183
184 return security_vm_enough_memory_mm(current->mm,
185 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static inline void shmem_unacct_blocks(unsigned long flags, long pages)
189{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000190 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300191 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Mike Rapoport0f079692017-09-06 16:22:59 -0700194static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
195{
196 struct shmem_inode_info *info = SHMEM_I(inode);
197 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
198
199 if (shmem_acct_block(info->flags, pages))
200 return false;
201
202 if (sbinfo->max_blocks) {
203 if (percpu_counter_compare(&sbinfo->used_blocks,
204 sbinfo->max_blocks - pages) > 0)
205 goto unacct;
206 percpu_counter_add(&sbinfo->used_blocks, pages);
207 }
208
209 return true;
210
211unacct:
212 shmem_unacct_blocks(info->flags, pages);
213 return false;
214}
215
216static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
217{
218 struct shmem_inode_info *info = SHMEM_I(inode);
219 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
220
221 if (sbinfo->max_blocks)
222 percpu_counter_sub(&sbinfo->used_blocks, pages);
223 shmem_unacct_blocks(info->flags, pages);
224}
225
Hugh Dickins759b9772007-03-05 00:30:28 -0800226static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700227static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800228static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800229static const struct inode_operations shmem_inode_operations;
230static const struct inode_operations shmem_dir_inode_operations;
231static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400232static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700233static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Mike Rapoportb0506e42017-02-22 15:43:28 -0800235bool vma_is_shmem(struct vm_area_struct *vma)
236{
237 return vma->vm_ops == &shmem_vm_ops;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800241static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800243static int shmem_reserve_inode(struct super_block *sb)
244{
245 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
246 if (sbinfo->max_inodes) {
247 spin_lock(&sbinfo->stat_lock);
248 if (!sbinfo->free_inodes) {
249 spin_unlock(&sbinfo->stat_lock);
250 return -ENOSPC;
251 }
252 sbinfo->free_inodes--;
253 spin_unlock(&sbinfo->stat_lock);
254 }
255 return 0;
256}
257
258static void shmem_free_inode(struct super_block *sb)
259{
260 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
261 if (sbinfo->max_inodes) {
262 spin_lock(&sbinfo->stat_lock);
263 sbinfo->free_inodes++;
264 spin_unlock(&sbinfo->stat_lock);
265 }
266}
267
Randy Dunlap46711812008-03-19 17:00:41 -0700268/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700269 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * @inode: inode to recalc
271 *
272 * We have to calculate the free blocks since the mm can drop
273 * undirtied hole pages behind our back.
274 *
275 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
276 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
277 *
278 * It has to be called with the spinlock held.
279 */
280static void shmem_recalc_inode(struct inode *inode)
281{
282 struct shmem_inode_info *info = SHMEM_I(inode);
283 long freed;
284
285 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
286 if (freed > 0) {
287 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700288 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700289 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
292
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700293bool shmem_charge(struct inode *inode, long pages)
294{
295 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700296 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700297
Mike Rapoport0f079692017-09-06 16:22:59 -0700298 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700299 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700300
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800301 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
302 inode->i_mapping->nrpages += pages;
303
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700304 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700305 info->alloced += pages;
306 inode->i_blocks += pages * BLOCKS_PER_PAGE;
307 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700308 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700309
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700310 return true;
311}
312
313void shmem_uncharge(struct inode *inode, long pages)
314{
315 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700316 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700317
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800318 /* nrpages adjustment done by __delete_from_page_cache() or caller */
319
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700320 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700321 info->alloced -= pages;
322 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
323 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700324 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700325
Mike Rapoport0f079692017-09-06 16:22:59 -0700326 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700327}
328
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700329/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500330 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700331 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500332static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700333 pgoff_t index, void *expected, void *replacement)
334{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500335 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700336 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700337
338 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700339 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500340 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700341 if (item != expected)
342 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500343 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700344 return 0;
345}
346
347/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700348 * Sometimes, before we decide whether to proceed or to fail, we must check
349 * that an entry was not already brought back from swap by a racing thread.
350 *
351 * Checking page is not enough: by the time a SwapCache page is locked, it
352 * might be reused, and again be SwapCache, using the same swap as before.
353 */
354static bool shmem_confirm_swap(struct address_space *mapping,
355 pgoff_t index, swp_entry_t swap)
356{
Matthew Wilcoxa12831b2017-11-22 08:34:58 -0500357 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700358}
359
360/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700361 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
362 *
363 * SHMEM_HUGE_NEVER:
364 * disables huge pages for the mount;
365 * SHMEM_HUGE_ALWAYS:
366 * enables huge pages for the mount;
367 * SHMEM_HUGE_WITHIN_SIZE:
368 * only allocate huge pages if the page will be fully within i_size,
369 * also respect fadvise()/madvise() hints;
370 * SHMEM_HUGE_ADVISE:
371 * only allocate huge pages if requested with fadvise()/madvise();
372 */
373
374#define SHMEM_HUGE_NEVER 0
375#define SHMEM_HUGE_ALWAYS 1
376#define SHMEM_HUGE_WITHIN_SIZE 2
377#define SHMEM_HUGE_ADVISE 3
378
379/*
380 * Special values.
381 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
382 *
383 * SHMEM_HUGE_DENY:
384 * disables huge on shm_mnt and all mounts, for emergency use;
385 * SHMEM_HUGE_FORCE:
386 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
387 *
388 */
389#define SHMEM_HUGE_DENY (-1)
390#define SHMEM_HUGE_FORCE (-2)
391
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700392#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700393/* ifdef here to avoid bloating shmem.o when not necessary */
394
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700395static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700396
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800397#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700398static int shmem_parse_huge(const char *str)
399{
400 if (!strcmp(str, "never"))
401 return SHMEM_HUGE_NEVER;
402 if (!strcmp(str, "always"))
403 return SHMEM_HUGE_ALWAYS;
404 if (!strcmp(str, "within_size"))
405 return SHMEM_HUGE_WITHIN_SIZE;
406 if (!strcmp(str, "advise"))
407 return SHMEM_HUGE_ADVISE;
408 if (!strcmp(str, "deny"))
409 return SHMEM_HUGE_DENY;
410 if (!strcmp(str, "force"))
411 return SHMEM_HUGE_FORCE;
412 return -EINVAL;
413}
414
415static const char *shmem_format_huge(int huge)
416{
417 switch (huge) {
418 case SHMEM_HUGE_NEVER:
419 return "never";
420 case SHMEM_HUGE_ALWAYS:
421 return "always";
422 case SHMEM_HUGE_WITHIN_SIZE:
423 return "within_size";
424 case SHMEM_HUGE_ADVISE:
425 return "advise";
426 case SHMEM_HUGE_DENY:
427 return "deny";
428 case SHMEM_HUGE_FORCE:
429 return "force";
430 default:
431 VM_BUG_ON(1);
432 return "bad_val";
433 }
434}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800435#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700436
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700437static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
438 struct shrink_control *sc, unsigned long nr_to_split)
439{
440 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800441 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700442 struct inode *inode;
443 struct shmem_inode_info *info;
444 struct page *page;
445 unsigned long batch = sc ? sc->nr_to_scan : 128;
446 int removed = 0, split = 0;
447
448 if (list_empty(&sbinfo->shrinklist))
449 return SHRINK_STOP;
450
451 spin_lock(&sbinfo->shrinklist_lock);
452 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
453 info = list_entry(pos, struct shmem_inode_info, shrinklist);
454
455 /* pin the inode */
456 inode = igrab(&info->vfs_inode);
457
458 /* inode is about to be evicted */
459 if (!inode) {
460 list_del_init(&info->shrinklist);
461 removed++;
462 goto next;
463 }
464
465 /* Check if there's anything to gain */
466 if (round_up(inode->i_size, PAGE_SIZE) ==
467 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800468 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700469 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700470 goto next;
471 }
472
473 list_move(&info->shrinklist, &list);
474next:
475 if (!--batch)
476 break;
477 }
478 spin_unlock(&sbinfo->shrinklist_lock);
479
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800480 list_for_each_safe(pos, next, &to_remove) {
481 info = list_entry(pos, struct shmem_inode_info, shrinklist);
482 inode = &info->vfs_inode;
483 list_del_init(&info->shrinklist);
484 iput(inode);
485 }
486
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700487 list_for_each_safe(pos, next, &list) {
488 int ret;
489
490 info = list_entry(pos, struct shmem_inode_info, shrinklist);
491 inode = &info->vfs_inode;
492
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700493 if (nr_to_split && split >= nr_to_split)
494 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700495
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700496 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700497 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
498 if (!page)
499 goto drop;
500
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700501 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700502 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700503 put_page(page);
504 goto drop;
505 }
506
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700507 /*
508 * Leave the inode on the list if we failed to lock
509 * the page at this time.
510 *
511 * Waiting for the lock may lead to deadlock in the
512 * reclaim path.
513 */
514 if (!trylock_page(page)) {
515 put_page(page);
516 goto leave;
517 }
518
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700519 ret = split_huge_page(page);
520 unlock_page(page);
521 put_page(page);
522
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700523 /* If split failed leave the inode on the list */
524 if (ret)
525 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700526
527 split++;
528drop:
529 list_del_init(&info->shrinklist);
530 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700531leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700532 iput(inode);
533 }
534
535 spin_lock(&sbinfo->shrinklist_lock);
536 list_splice_tail(&list, &sbinfo->shrinklist);
537 sbinfo->shrinklist_len -= removed;
538 spin_unlock(&sbinfo->shrinklist_lock);
539
540 return split;
541}
542
543static long shmem_unused_huge_scan(struct super_block *sb,
544 struct shrink_control *sc)
545{
546 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
547
548 if (!READ_ONCE(sbinfo->shrinklist_len))
549 return SHRINK_STOP;
550
551 return shmem_unused_huge_shrink(sbinfo, sc, 0);
552}
553
554static long shmem_unused_huge_count(struct super_block *sb,
555 struct shrink_control *sc)
556{
557 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
558 return READ_ONCE(sbinfo->shrinklist_len);
559}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700560#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700561
562#define shmem_huge SHMEM_HUGE_DENY
563
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700564static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
565 struct shrink_control *sc, unsigned long nr_to_split)
566{
567 return 0;
568}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700569#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700570
Yang Shi89fdcd22018-06-07 17:06:59 -0700571static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
572{
573 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
574 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
575 shmem_huge != SHMEM_HUGE_DENY)
576 return true;
577 return false;
578}
579
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700580/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700581 * Like add_to_page_cache_locked, but error if expected item has gone.
582 */
583static int shmem_add_to_page_cache(struct page *page,
584 struct address_space *mapping,
Matthew Wilcox552446a2017-12-01 13:25:14 -0500585 pgoff_t index, void *expected, gfp_t gfp)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700586{
Matthew Wilcox552446a2017-12-01 13:25:14 -0500587 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
588 unsigned long i = 0;
589 unsigned long nr = 1UL << compound_order(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700590
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700591 VM_BUG_ON_PAGE(PageTail(page), page);
592 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800593 VM_BUG_ON_PAGE(!PageLocked(page), page);
594 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700595 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700596
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700597 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700598 page->mapping = mapping;
599 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700600
Matthew Wilcox552446a2017-12-01 13:25:14 -0500601 do {
602 void *entry;
603 xas_lock_irq(&xas);
604 entry = xas_find_conflict(&xas);
605 if (entry != expected)
606 xas_set_err(&xas, -EEXIST);
607 xas_create_range(&xas);
608 if (xas_error(&xas))
609 goto unlock;
610next:
611 xas_store(&xas, page + i);
612 if (++i < nr) {
613 xas_next(&xas);
614 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700615 }
Matthew Wilcox552446a2017-12-01 13:25:14 -0500616 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700617 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700618 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500619 }
620 mapping->nrpages += nr;
Mel Gorman11fb9982016-07-28 15:46:20 -0700621 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
622 __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500623unlock:
624 xas_unlock_irq(&xas);
625 } while (xas_nomem(&xas, gfp));
626
627 if (xas_error(&xas)) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700628 page->mapping = NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700629 page_ref_sub(page, nr);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500630 return xas_error(&xas);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700631 }
Matthew Wilcox552446a2017-12-01 13:25:14 -0500632
633 return 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700634}
635
636/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700637 * Like delete_from_page_cache, but substitutes swap for page.
638 */
639static void shmem_delete_from_page_cache(struct page *page, void *radswap)
640{
641 struct address_space *mapping = page->mapping;
642 int error;
643
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700644 VM_BUG_ON_PAGE(PageCompound(page), page);
645
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700646 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500647 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700648 page->mapping = NULL;
649 mapping->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700650 __dec_node_page_state(page, NR_FILE_PAGES);
651 __dec_node_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700652 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700654 BUG_ON(error);
655}
656
657/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500658 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700659 */
660static int shmem_free_swap(struct address_space *mapping,
661 pgoff_t index, void *radswap)
662{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700663 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700664
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500665 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700666 if (old != radswap)
667 return -ENOENT;
668 free_swap_and_cache(radix_to_swp_entry(radswap));
669 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700670}
671
672/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800673 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800674 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800675 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700676 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800677 * as long as the inode doesn't go away and racy results are not a problem.
678 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800679unsigned long shmem_partial_swap_usage(struct address_space *mapping,
680 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800681{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500682 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800683 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800684 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800685
686 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500687 xas_for_each(&xas, page, end - 1) {
688 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700689 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400690 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800691 swapped++;
692
693 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500694 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800695 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800696 }
697 }
698
699 rcu_read_unlock();
700
701 return swapped << PAGE_SHIFT;
702}
703
704/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800705 * Determine (in bytes) how many of the shmem object's pages mapped by the
706 * given vma is swapped out.
707 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700708 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800709 * as long as the inode doesn't go away and racy results are not a problem.
710 */
711unsigned long shmem_swap_usage(struct vm_area_struct *vma)
712{
713 struct inode *inode = file_inode(vma->vm_file);
714 struct shmem_inode_info *info = SHMEM_I(inode);
715 struct address_space *mapping = inode->i_mapping;
716 unsigned long swapped;
717
718 /* Be careful as we don't hold info->lock */
719 swapped = READ_ONCE(info->swapped);
720
721 /*
722 * The easier cases are when the shmem object has nothing in swap, or
723 * the vma maps it whole. Then we can simply use the stats that we
724 * already track.
725 */
726 if (!swapped)
727 return 0;
728
729 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
730 return swapped << PAGE_SHIFT;
731
732 /* Here comes the more involved part */
733 return shmem_partial_swap_usage(mapping,
734 linear_page_index(vma, vma->vm_start),
735 linear_page_index(vma, vma->vm_end));
736}
737
738/*
Hugh Dickins24513262012-01-20 14:34:21 -0800739 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
740 */
741void shmem_unlock_mapping(struct address_space *mapping)
742{
743 struct pagevec pvec;
744 pgoff_t indices[PAGEVEC_SIZE];
745 pgoff_t index = 0;
746
Mel Gorman86679822017-11-15 17:37:52 -0800747 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800748 /*
749 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
750 */
751 while (!mapping_unevictable(mapping)) {
752 /*
753 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
754 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
755 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700756 pvec.nr = find_get_entries(mapping, index,
757 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800758 if (!pvec.nr)
759 break;
760 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700761 pagevec_remove_exceptionals(&pvec);
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +0000762 check_move_unevictable_pages(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800763 pagevec_release(&pvec);
764 cond_resched();
765 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700766}
767
768/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500769 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700770 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700771 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700772static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
773 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700775 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300777 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
778 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
779 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
780 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700781 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700782 pgoff_t indices[PAGEVEC_SIZE];
783 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700784 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700785 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700787 if (lend == -1)
788 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700789
Mel Gorman86679822017-11-15 17:37:52 -0800790 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700791 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700792 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700793 pvec.nr = find_get_entries(mapping, index,
794 min(end - index, (pgoff_t)PAGEVEC_SIZE),
795 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700796 if (!pvec.nr)
797 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700798 for (i = 0; i < pagevec_count(&pvec); i++) {
799 struct page *page = pvec.pages[i];
800
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700801 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700802 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700803 break;
804
Matthew Wilcox3159f942017-11-03 13:30:42 -0400805 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700806 if (unfalloc)
807 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700808 nr_swaps_freed += !shmem_free_swap(mapping,
809 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700810 continue;
811 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700812
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700813 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
814
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700815 if (!trylock_page(page))
816 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700817
818 if (PageTransTail(page)) {
819 /* Middle of THP: zero out the page */
820 clear_highpage(page);
821 unlock_page(page);
822 continue;
823 } else if (PageTransHuge(page)) {
824 if (index == round_down(end, HPAGE_PMD_NR)) {
825 /*
826 * Range ends in the middle of THP:
827 * zero out the page
828 */
829 clear_highpage(page);
830 unlock_page(page);
831 continue;
832 }
833 index += HPAGE_PMD_NR - 1;
834 i += HPAGE_PMD_NR - 1;
835 }
836
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700837 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700838 VM_BUG_ON_PAGE(PageTail(page), page);
839 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800840 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700841 truncate_inode_page(mapping, page);
842 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700843 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700844 unlock_page(page);
845 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700846 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800847 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700848 cond_resched();
849 index++;
850 }
851
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700852 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700853 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700854 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700855 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300856 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700857 if (start > end) {
858 top = partial_end;
859 partial_end = 0;
860 }
861 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700862 set_page_dirty(page);
863 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300864 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700865 }
866 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700867 if (partial_end) {
868 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700869 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700870 if (page) {
871 zero_user_segment(page, 0, partial_end);
872 set_page_dirty(page);
873 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300874 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700875 }
876 }
877 if (start >= end)
878 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700879
880 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700881 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700882 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700883
884 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700885 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700886 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700887 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700888 /* If all gone or hole-punch or unfalloc, we're done */
889 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700890 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700891 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700892 index = start;
893 continue;
894 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700895 for (i = 0; i < pagevec_count(&pvec); i++) {
896 struct page *page = pvec.pages[i];
897
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700898 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700899 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700900 break;
901
Matthew Wilcox3159f942017-11-03 13:30:42 -0400902 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700903 if (unfalloc)
904 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700905 if (shmem_free_swap(mapping, index, page)) {
906 /* Swap was replaced by page: retry */
907 index--;
908 break;
909 }
910 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700911 continue;
912 }
913
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700914 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700915
916 if (PageTransTail(page)) {
917 /* Middle of THP: zero out the page */
918 clear_highpage(page);
919 unlock_page(page);
920 /*
921 * Partial thp truncate due 'start' in middle
922 * of THP: don't need to look on these pages
923 * again on !pvec.nr restart.
924 */
925 if (index != round_down(end, HPAGE_PMD_NR))
926 start++;
927 continue;
928 } else if (PageTransHuge(page)) {
929 if (index == round_down(end, HPAGE_PMD_NR)) {
930 /*
931 * Range ends in the middle of THP:
932 * zero out the page
933 */
934 clear_highpage(page);
935 unlock_page(page);
936 continue;
937 }
938 index += HPAGE_PMD_NR - 1;
939 i += HPAGE_PMD_NR - 1;
940 }
941
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700942 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700943 VM_BUG_ON_PAGE(PageTail(page), page);
944 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800945 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700946 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700947 } else {
948 /* Page was replaced by swap: retry */
949 unlock_page(page);
950 index--;
951 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700952 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700953 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700954 unlock_page(page);
955 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700956 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800957 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700958 index++;
959 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700960
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700961 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700962 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700964 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700965}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700967void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
968{
969 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700970 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700972EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
David Howellsa528d352017-01-31 16:46:22 +0000974static int shmem_getattr(const struct path *path, struct kstat *stat,
975 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -0700976{
David Howellsa528d352017-01-31 16:46:22 +0000977 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -0700978 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -0700979 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -0700980
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800981 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700982 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800983 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700984 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800985 }
Yu Zhao44a30222015-09-08 15:03:33 -0700986 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -0700987
988 if (is_huge_enabled(sb_info))
989 stat->blksize = HPAGE_PMD_SIZE;
990
Yu Zhao44a30222015-09-08 15:03:33 -0700991 return 0;
992}
993
Hugh Dickins94c1e622011-06-27 16:18:03 -0700994static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
David Howells75c3cfa2015-03-17 22:26:12 +0000996 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -0700997 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700998 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int error;
1000
Jan Kara31051c82016-05-26 16:55:18 +02001001 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001002 if (error)
1003 return error;
1004
Hugh Dickins94c1e622011-06-27 16:18:03 -07001005 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1006 loff_t oldsize = inode->i_size;
1007 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001008
David Herrmann40e041a2014-08-08 14:25:27 -07001009 /* protected by i_mutex */
1010 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1011 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1012 return -EPERM;
1013
Hugh Dickins94c1e622011-06-27 16:18:03 -07001014 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001015 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1016 oldsize, newsize);
1017 if (error)
1018 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001019 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001020 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001021 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001022 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001023 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001024 if (oldsize > holebegin)
1025 unmap_mapping_range(inode->i_mapping,
1026 holebegin, 0, 1);
1027 if (info->alloced)
1028 shmem_truncate_range(inode,
1029 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001030 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001031 if (oldsize > holebegin)
1032 unmap_mapping_range(inode->i_mapping,
1033 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001034
1035 /*
1036 * Part of the huge page can be beyond i_size: subject
1037 * to shrink under memory pressure.
1038 */
1039 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1040 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001041 /*
1042 * _careful to defend against unlocked access to
1043 * ->shrink_list in shmem_unused_huge_shrink()
1044 */
1045 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001046 list_add_tail(&info->shrinklist,
1047 &sbinfo->shrinklist);
1048 sbinfo->shrinklist_len++;
1049 }
1050 spin_unlock(&sbinfo->shrinklist_lock);
1051 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001055 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001056 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001057 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return error;
1059}
1060
Al Viro1f895f72010-06-05 19:10:41 -04001061static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001064 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001066 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 shmem_unacct_size(info->flags, inode->i_size);
1068 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001069 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001070 if (!list_empty(&info->shrinklist)) {
1071 spin_lock(&sbinfo->shrinklist_lock);
1072 if (!list_empty(&info->shrinklist)) {
1073 list_del_init(&info->shrinklist);
1074 sbinfo->shrinklist_len--;
1075 }
1076 spin_unlock(&sbinfo->shrinklist_lock);
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001079 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001081 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Al Viro3ed47db2016-01-22 18:08:52 -05001083 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001084
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001085 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001086 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001087 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001088 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001091static unsigned long find_swap_entry(struct xarray *xa, void *item)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001092{
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001093 XA_STATE(xas, xa, 0);
Matthew Wilcox478922e2016-12-14 15:08:52 -08001094 unsigned int checked = 0;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001095 void *entry;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001096
1097 rcu_read_lock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001098 xas_for_each(&xas, entry, ULONG_MAX) {
1099 if (xas_retry(&xas, entry))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001100 continue;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001101 if (entry == item)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001102 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001103 checked++;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001104 if ((checked % XA_CHECK_SCHED) != 0)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001105 continue;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001106 xas_pause(&xas);
Matthew Wilcox478922e2016-12-14 15:08:52 -08001107 cond_resched_rcu();
1108 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001109 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001110
1111 return entry ? xas.xa_index : -1;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001112}
1113
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001114/*
1115 * If swap found in inode, free it and move page from swapcache to filecache.
1116 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001117static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001118 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Hugh Dickins285b2c42011-08-03 16:21:20 -07001120 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001121 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001122 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001123 gfp_t gfp;
1124 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001126 radswap = swp_to_radix_entry(swap);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001127 index = find_swap_entry(&mapping->i_pages, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001128 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -07001129 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001130
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001131 /*
1132 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -04001133 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001134 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -07001135 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001136 */
1137 if (shmem_swaplist.next != &info->swaplist)
1138 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001139
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001140 gfp = mapping_gfp_mask(mapping);
1141 if (shmem_should_replace_page(*pagep, gfp)) {
1142 mutex_unlock(&shmem_swaplist_mutex);
1143 error = shmem_replace_page(pagep, gfp, info, index);
1144 mutex_lock(&shmem_swaplist_mutex);
1145 /*
1146 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -07001147 * allocation, but the inode might have been freed while we
1148 * dropped it: although a racing shmem_evict_inode() cannot
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001149 * complete without emptying the page cache, our page lock
Hugh Dickins0142ef62012-06-07 14:21:09 -07001150 * on this swapcache page is not enough to prevent that -
1151 * free_swap_and_cache() of our swap entry will only
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001152 * trylock_page(), removing swap from page cache whatever.
Hugh Dickins0142ef62012-06-07 14:21:09 -07001153 *
1154 * We must not proceed to shmem_add_to_page_cache() if the
1155 * inode has been freed, but of course we cannot rely on
1156 * inode or mapping or info to check that. However, we can
1157 * safely check if our swap entry is still in use (and here
1158 * it can't have got reused for another page): if it's still
1159 * in use, then the inode cannot have been freed yet, and we
1160 * can safely proceed (if it's no longer in use, that tells
1161 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001162 */
1163 if (!page_swapcount(*pagep))
1164 error = -ENOENT;
1165 }
1166
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001167 /*
Hugh Dickins778dd892011-05-11 15:13:37 -07001168 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1169 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1170 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001171 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001172 if (!error)
1173 error = shmem_add_to_page_cache(*pagep, mapping, index,
Matthew Wilcox552446a2017-12-01 13:25:14 -05001174 radswap, gfp);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001175 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001176 /*
1177 * Truncation and eviction use free_swap_and_cache(), which
1178 * only does trylock page: if we raced, best clean up here.
1179 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001180 delete_from_swap_cache(*pagep);
1181 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001182 if (!error) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001183 spin_lock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001184 info->swapped--;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001185 spin_unlock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001186 swap_free(swap);
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001189 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001193 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001195int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001197 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001199 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001200 int error = 0;
1201
1202 /*
1203 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001204 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001205 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001206 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001207 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001208
1209 /*
1210 * Charge page using GFP_KERNEL while we can wait, before taking
1211 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1212 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001213 */
Tejun Heo2cf85582018-07-03 11:14:56 -04001214 error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL,
1215 &memcg, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001216 if (error)
1217 goto out;
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001218 /* No memory allocation: swap entry occupies the slot for the page */
Johannes Weiner00501b52014-08-08 14:19:20 -07001219 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001221 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001222 list_for_each_safe(this, next, &shmem_swaplist) {
1223 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001224 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001225 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001226 else
1227 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001228 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001229 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001230 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001231 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001233 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001234
Johannes Weiner00501b52014-08-08 14:19:20 -07001235 if (error) {
1236 if (error != -ENOMEM)
1237 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001238 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001239 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001240 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001241out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001242 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001243 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
1247/*
1248 * Move the page from the page cache to the swap cache.
1249 */
1250static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1251{
1252 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001255 swp_entry_t swap;
1256 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001258 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 mapping = page->mapping;
1261 index = page->index;
1262 inode = mapping->host;
1263 info = SHMEM_I(inode);
1264 if (info->flags & VM_LOCKED)
1265 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001266 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto redirty;
1268
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001269 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001270 * Our capabilities prevent regular writeback or sync from ever calling
1271 * shmem_writepage; but a stacking filesystem might use ->writepage of
1272 * its underlying filesystem, in which case tmpfs should write out to
1273 * swap only in response to memory pressure, and not for the writeback
1274 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001275 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001276 if (!wbc->for_reclaim) {
1277 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1278 goto redirty;
1279 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001280
1281 /*
1282 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1283 * value into swapfile.c, the only way we can correctly account for a
1284 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001285 *
1286 * That's okay for a page already fallocated earlier, but if we have
1287 * not yet completed the fallocation, then (a) we want to keep track
1288 * of this page in case we have to undo it, and (b) it may not be a
1289 * good idea to continue anyway, once we're pushing into swap. So
1290 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001291 */
1292 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001293 if (inode->i_private) {
1294 struct shmem_falloc *shmem_falloc;
1295 spin_lock(&inode->i_lock);
1296 shmem_falloc = inode->i_private;
1297 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001298 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001299 index >= shmem_falloc->start &&
1300 index < shmem_falloc->next)
1301 shmem_falloc->nr_unswapped++;
1302 else
1303 shmem_falloc = NULL;
1304 spin_unlock(&inode->i_lock);
1305 if (shmem_falloc)
1306 goto redirty;
1307 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001308 clear_highpage(page);
1309 flush_dcache_page(page);
1310 SetPageUptodate(page);
1311 }
1312
Huang Ying38d8b4e2017-07-06 15:37:18 -07001313 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001314 if (!swap.val)
1315 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001316
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001317 /*
1318 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001319 * if it's not already there. Do it now before the page is
1320 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001321 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001322 * we've incremented swapped, because shmem_unuse_inode() will
1323 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001324 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001325 mutex_lock(&shmem_swaplist_mutex);
1326 if (list_empty(&info->swaplist))
1327 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001328
Hugh Dickins48f170f2011-07-25 17:12:37 -07001329 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001330 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001331 shmem_recalc_inode(inode);
1332 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001333 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001334
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001335 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001336 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1337
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001338 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001339 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001340 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
1342 }
1343
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001344 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001345 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346redirty:
1347 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001348 if (wbc->for_reclaim)
1349 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1350 unlock_page(page);
1351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Hugh Dickins75edd342016-05-19 17:12:44 -07001354#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001355static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001356{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001357 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001358
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001359 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001360 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001361
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001362 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001363
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001364 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001365}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001366
1367static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1368{
1369 struct mempolicy *mpol = NULL;
1370 if (sbinfo->mpol) {
1371 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1372 mpol = sbinfo->mpol;
1373 mpol_get(mpol);
1374 spin_unlock(&sbinfo->stat_lock);
1375 }
1376 return mpol;
1377}
Hugh Dickins75edd342016-05-19 17:12:44 -07001378#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1379static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1380{
1381}
1382static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1383{
1384 return NULL;
1385}
1386#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1387#ifndef CONFIG_NUMA
1388#define vm_policy vm_private_data
1389#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001390
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001391static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1392 struct shmem_inode_info *info, pgoff_t index)
1393{
1394 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001395 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001396 /* Bias interleave by inode number to distribute better across nodes */
1397 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001398 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1399}
1400
1401static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1402{
1403 /* Drop reference taken by mpol_shared_policy_lookup() */
1404 mpol_cond_put(vma->vm_policy);
1405}
1406
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001407static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1408 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001411 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001412 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001414 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001415 vmf.vma = &pvma;
1416 vmf.address = 0;
1417 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001418 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001419
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001420 return page;
1421}
Mel Gorman18a2f372012-12-05 14:01:41 -08001422
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001423static struct page *shmem_alloc_hugepage(gfp_t gfp,
1424 struct shmem_inode_info *info, pgoff_t index)
1425{
1426 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001427 struct address_space *mapping = info->vfs_inode.i_mapping;
1428 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001429 struct page *page;
1430
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001431 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001432 return NULL;
1433
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001434 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001435 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1436 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001437 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001438
1439 shmem_pseudo_vma_init(&pvma, info, hindex);
1440 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
David Rientjes356ff8a2018-12-07 14:50:16 -08001441 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001442 shmem_pseudo_vma_destroy(&pvma);
1443 if (page)
1444 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001445 return page;
1446}
1447
1448static struct page *shmem_alloc_page(gfp_t gfp,
1449 struct shmem_inode_info *info, pgoff_t index)
1450{
1451 struct vm_area_struct pvma;
1452 struct page *page;
1453
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001454 shmem_pseudo_vma_init(&pvma, info, index);
1455 page = alloc_page_vma(gfp, &pvma, 0);
1456 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001457
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001458 return page;
1459}
1460
1461static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001462 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001463 pgoff_t index, bool huge)
1464{
Mike Rapoport0f079692017-09-06 16:22:59 -07001465 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001466 struct page *page;
1467 int nr;
1468 int err = -ENOSPC;
1469
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001470 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001471 huge = false;
1472 nr = huge ? HPAGE_PMD_NR : 1;
1473
Mike Rapoport0f079692017-09-06 16:22:59 -07001474 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001475 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001476
1477 if (huge)
1478 page = shmem_alloc_hugepage(gfp, info, index);
1479 else
1480 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001481 if (page) {
1482 __SetPageLocked(page);
1483 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001484 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001485 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001486
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001487 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001488 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001489failed:
1490 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001494 * When a page is moved from swapcache to shmem filecache (either by the
1495 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1496 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1497 * ignorance of the mapping it belongs to. If that mapping has special
1498 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1499 * we may need to copy to a suitable page before moving to filecache.
1500 *
1501 * In a future release, this may well be extended to respect cpuset and
1502 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1503 * but for now it is a simple matter of zone.
1504 */
1505static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1506{
1507 return page_zonenum(page) > gfp_zone(gfp);
1508}
1509
1510static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1511 struct shmem_inode_info *info, pgoff_t index)
1512{
1513 struct page *oldpage, *newpage;
1514 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001515 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001516 pgoff_t swap_index;
1517 int error;
1518
1519 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001520 entry.val = page_private(oldpage);
1521 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001522 swap_mapping = page_mapping(oldpage);
1523
1524 /*
1525 * We have arrived here because our zones are constrained, so don't
1526 * limit chance of success by further cpuset and node constraints.
1527 */
1528 gfp &= ~GFP_CONSTRAINT_MASK;
1529 newpage = shmem_alloc_page(gfp, info, index);
1530 if (!newpage)
1531 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001532
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001533 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001534 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001535 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001536
Hugh Dickins9956edf2016-11-10 10:46:11 -08001537 __SetPageLocked(newpage);
1538 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001539 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001540 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001541 SetPageSwapCache(newpage);
1542
1543 /*
1544 * Our caller will very soon move newpage out of swapcache, but it's
1545 * a nice clean interface for us to replace oldpage by newpage there.
1546 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001547 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001548 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001549 if (!error) {
Mel Gorman11fb9982016-07-28 15:46:20 -07001550 __inc_node_page_state(newpage, NR_FILE_PAGES);
1551 __dec_node_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001552 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001553 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001554
Hugh Dickins0142ef62012-06-07 14:21:09 -07001555 if (unlikely(error)) {
1556 /*
1557 * Is this possible? I think not, now that our callers check
1558 * both PageSwapCache and page_private after getting page lock;
1559 * but be defensive. Reverse old to newpage for clear and free.
1560 */
1561 oldpage = newpage;
1562 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001563 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001564 lru_cache_add_anon(newpage);
1565 *pagep = newpage;
1566 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001567
1568 ClearPageSwapCache(oldpage);
1569 set_page_private(oldpage, 0);
1570
1571 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001572 put_page(oldpage);
1573 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001574 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001575}
1576
1577/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001578 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 *
1580 * If we allocate a new one we do not mark it dirty. That's up to the
1581 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001582 * entry since a page cannot live in both the swap and page cache.
1583 *
1584 * fault_mm and fault_type are only supplied by shmem_fault:
1585 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001587static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001588 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001589 struct vm_area_struct *vma, struct vm_fault *vmf,
1590 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001593 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001595 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001596 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001597 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 swp_entry_t swap;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001599 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001600 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001602 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001603 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001605 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001607 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1608 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001610 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001611 page = find_lock_entry(mapping, index);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001612 if (xa_is_value(page)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001613 swap = radix_to_swp_entry(page);
1614 page = NULL;
1615 }
1616
Hugh Dickins75edd342016-05-19 17:12:44 -07001617 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001618 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001619 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001620 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001621 }
1622
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001623 if (page && sgp == SGP_WRITE)
1624 mark_page_accessed(page);
1625
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001626 /* fallocated page? */
1627 if (page && !PageUptodate(page)) {
1628 if (sgp != SGP_READ)
1629 goto clear;
1630 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001631 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001632 page = NULL;
1633 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001634 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001635 *pagep = page;
1636 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001640 * Fast cache lookup did not find it:
1641 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001643 sbinfo = SHMEM_SB(inode->i_sb);
Mike Rapoportcfda0522017-02-22 15:43:37 -08001644 charge_mm = vma ? vma->vm_mm : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (swap.val) {
1647 /* Look it up and read it in.. */
Huang Yingec560172017-09-06 16:24:36 -07001648 page = lookup_swap_cache(swap, NULL, 0);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001649 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001650 /* Or update major stats only when swapin succeeds?? */
1651 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001652 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001653 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07001654 count_memcg_event_mm(charge_mm, PGMAJFAULT);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001655 }
1656 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001657 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001658 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001659 error = -ENOMEM;
1660 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663
1664 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001665 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001666 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001667 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001668 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001669 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001670 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001671 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001673 goto failed;
1674 }
1675 wait_on_page_writeback(page);
1676
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001677 if (shmem_should_replace_page(page, gfp)) {
1678 error = shmem_replace_page(&page, gfp, info, index);
1679 if (error)
1680 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
Tejun Heo2cf85582018-07-03 11:14:56 -04001683 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001684 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001685 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001686 error = shmem_add_to_page_cache(page, mapping, index,
Matthew Wilcox552446a2017-12-01 13:25:14 -05001687 swp_to_radix_entry(swap), gfp);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001688 /*
1689 * We already confirmed swap under page lock, and make
1690 * no memory allocation here, so usually no possibility
1691 * of error; but free_swap_and_cache() only trylocks a
1692 * page, so it is just possible that the entry has been
1693 * truncated or holepunched since swap was confirmed.
1694 * shmem_undo_range() will have done some of the
1695 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001696 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001697 * Reset swap.val? No, leave it so "failed" goes back to
1698 * "repeat": reading a hole and writing should succeed.
1699 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001700 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001701 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001702 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001703 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001704 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001705 if (error)
1706 goto failed;
1707
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001708 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001709
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001710 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001711 info->swapped--;
1712 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001713 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001714
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001715 if (sgp == SGP_WRITE)
1716 mark_page_accessed(page);
1717
Hugh Dickins27ab7002011-07-25 17:12:36 -07001718 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001719 set_page_dirty(page);
1720 swap_free(swap);
1721
Hugh Dickins54af60422011-08-03 16:21:24 -07001722 } else {
Mike Rapoportcfda0522017-02-22 15:43:37 -08001723 if (vma && userfaultfd_missing(vma)) {
1724 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1725 return 0;
1726 }
1727
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001728 /* shmem_symlink() */
1729 if (mapping->a_ops != &shmem_aops)
1730 goto alloc_nohuge;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001731 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001732 goto alloc_nohuge;
1733 if (shmem_huge == SHMEM_HUGE_FORCE)
1734 goto alloc_huge;
1735 switch (sbinfo->huge) {
1736 loff_t i_size;
1737 pgoff_t off;
1738 case SHMEM_HUGE_NEVER:
1739 goto alloc_nohuge;
1740 case SHMEM_HUGE_WITHIN_SIZE:
1741 off = round_up(index, HPAGE_PMD_NR);
1742 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1743 if (i_size >= HPAGE_PMD_SIZE &&
1744 i_size >> PAGE_SHIFT >= off)
1745 goto alloc_huge;
1746 /* fallthrough */
1747 case SHMEM_HUGE_ADVISE:
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001748 if (sgp_huge == SGP_HUGE)
1749 goto alloc_huge;
1750 /* TODO: implement fadvise() hints */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001751 goto alloc_nohuge;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001754alloc_huge:
Mike Rapoport0f079692017-09-06 16:22:59 -07001755 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001756 if (IS_ERR(page)) {
Mike Rapoport0f079692017-09-06 16:22:59 -07001757alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001758 index, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001759 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001760 if (IS_ERR(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001761 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001762 error = PTR_ERR(page);
1763 page = NULL;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001764 if (error != -ENOSPC)
1765 goto failed;
1766 /*
1767 * Try to reclaim some spece by splitting a huge page
1768 * beyond i_size on the filesystem.
1769 */
1770 while (retry--) {
1771 int ret;
1772 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1773 if (ret == SHRINK_STOP)
1774 break;
1775 if (ret)
1776 goto alloc_nohuge;
1777 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001778 goto failed;
1779 }
1780
1781 if (PageTransHuge(page))
1782 hindex = round_down(index, HPAGE_PMD_NR);
1783 else
1784 hindex = index;
1785
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001786 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001787 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001788
Tejun Heo2cf85582018-07-03 11:14:56 -04001789 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001790 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001791 if (error)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001792 goto unacct;
Matthew Wilcox552446a2017-12-01 13:25:14 -05001793 error = shmem_add_to_page_cache(page, mapping, hindex,
1794 NULL, gfp & GFP_RECLAIM_MASK);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001795 if (error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001796 mem_cgroup_cancel_charge(page, memcg,
1797 PageTransHuge(page));
1798 goto unacct;
Hugh Dickinsb065b432012-07-11 14:02:48 -07001799 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001800 mem_cgroup_commit_charge(page, memcg, false,
1801 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001802 lru_cache_add_anon(page);
1803
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001804 spin_lock_irq(&info->lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001805 info->alloced += 1 << compound_order(page);
1806 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001807 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001808 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001809 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001810
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001811 if (PageTransHuge(page) &&
1812 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1813 hindex + HPAGE_PMD_NR - 1) {
1814 /*
1815 * Part of the huge page is beyond i_size: subject
1816 * to shrink under memory pressure.
1817 */
1818 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001819 /*
1820 * _careful to defend against unlocked access to
1821 * ->shrink_list in shmem_unused_huge_shrink()
1822 */
1823 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001824 list_add_tail(&info->shrinklist,
1825 &sbinfo->shrinklist);
1826 sbinfo->shrinklist_len++;
1827 }
1828 spin_unlock(&sbinfo->shrinklist_lock);
1829 }
1830
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001831 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001832 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1833 */
1834 if (sgp == SGP_FALLOC)
1835 sgp = SGP_WRITE;
1836clear:
1837 /*
1838 * Let SGP_WRITE caller clear ends if write does not fill page;
1839 * but SGP_FALLOC on a page fallocated earlier must initialize
1840 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001841 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001842 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1843 struct page *head = compound_head(page);
1844 int i;
1845
1846 for (i = 0; i < (1 << compound_order(head)); i++) {
1847 clear_highpage(head + i);
1848 flush_dcache_page(head + i);
1849 }
1850 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001851 }
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001852 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001853
Hugh Dickins54af60422011-08-03 16:21:24 -07001854 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001855 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001856 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001857 if (alloced) {
1858 ClearPageDirty(page);
1859 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001860 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001861 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001862 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001863 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001864 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001865 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001866 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001867 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001868 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001869
Nick Piggind0217ac2007-07-19 01:47:03 -07001870 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001871 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001873unacct:
Mike Rapoport0f079692017-09-06 16:22:59 -07001874 shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001875
1876 if (PageTransHuge(page)) {
1877 unlock_page(page);
1878 put_page(page);
1879 goto alloc_nohuge;
1880 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001881failed:
Hugh Dickins267a4c72015-12-11 13:40:55 -08001882 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
Hugh Dickinsd1899222012-07-11 14:02:47 -07001883 error = -EEXIST;
1884unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001885 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001886 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001887 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001888 }
1889 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001890 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001891 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001892 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001893 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001894 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001895 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07001896 goto repeat;
1897 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001900/*
1901 * This is like autoremove_wake_function, but it removes the wait queue
1902 * entry unconditionally - even if something else had already woken the
1903 * target.
1904 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001905static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001906{
1907 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001908 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001909 return ret;
1910}
1911
Souptick Joarder20acce62018-06-07 17:09:17 -07001912static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Dave Jiang11bac802017-02-24 14:56:41 -08001914 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001915 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001916 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001917 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07001918 int err;
1919 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001921 /*
1922 * Trinity finds that probing a hole which tmpfs is punching can
1923 * prevent the hole-punch from ever completing: which in turn
1924 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001925 * faulting pages into the hole while it's being punched. Although
1926 * shmem_undo_range() does remove the additions, it may be unable to
1927 * keep up, as each new page needs its own unmap_mapping_range() call,
1928 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1929 *
1930 * It does not matter if we sometimes reach this check just before the
1931 * hole-punch begins, so that one fault then races with the punch:
1932 * we just need to make racing faults a rare case.
1933 *
1934 * The implementation below would be much simpler if we just used a
1935 * standard mutex or completion: but we cannot take i_mutex in fault,
1936 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001937 */
1938 if (unlikely(inode->i_private)) {
1939 struct shmem_falloc *shmem_falloc;
1940
1941 spin_lock(&inode->i_lock);
1942 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001943 if (shmem_falloc &&
1944 shmem_falloc->waitq &&
1945 vmf->pgoff >= shmem_falloc->start &&
1946 vmf->pgoff < shmem_falloc->next) {
1947 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001948 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001949
1950 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001951 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1952 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001953 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001954 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001955 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001956 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001957
1958 shmem_falloc_waitq = shmem_falloc->waitq;
1959 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1960 TASK_UNINTERRUPTIBLE);
1961 spin_unlock(&inode->i_lock);
1962 schedule();
1963
1964 /*
1965 * shmem_falloc_waitq points into the shmem_fallocate()
1966 * stack of the hole-punching task: shmem_falloc_waitq
1967 * is usually invalid by the time we reach here, but
1968 * finish_wait() does not dereference it in that case;
1969 * though i_lock needed lest racing with wake_up_all().
1970 */
1971 spin_lock(&inode->i_lock);
1972 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1973 spin_unlock(&inode->i_lock);
1974 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001975 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001976 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001977 }
1978
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001979 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07001980
1981 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
1982 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001983 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07001984 else if (vma->vm_flags & VM_HUGEPAGE)
1985 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001986
Souptick Joarder20acce62018-06-07 17:09:17 -07001987 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08001988 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07001989 if (err)
1990 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001991 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07001994unsigned long shmem_get_unmapped_area(struct file *file,
1995 unsigned long uaddr, unsigned long len,
1996 unsigned long pgoff, unsigned long flags)
1997{
1998 unsigned long (*get_area)(struct file *,
1999 unsigned long, unsigned long, unsigned long, unsigned long);
2000 unsigned long addr;
2001 unsigned long offset;
2002 unsigned long inflated_len;
2003 unsigned long inflated_addr;
2004 unsigned long inflated_offset;
2005
2006 if (len > TASK_SIZE)
2007 return -ENOMEM;
2008
2009 get_area = current->mm->get_unmapped_area;
2010 addr = get_area(file, uaddr, len, pgoff, flags);
2011
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002012 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002013 return addr;
2014 if (IS_ERR_VALUE(addr))
2015 return addr;
2016 if (addr & ~PAGE_MASK)
2017 return addr;
2018 if (addr > TASK_SIZE - len)
2019 return addr;
2020
2021 if (shmem_huge == SHMEM_HUGE_DENY)
2022 return addr;
2023 if (len < HPAGE_PMD_SIZE)
2024 return addr;
2025 if (flags & MAP_FIXED)
2026 return addr;
2027 /*
2028 * Our priority is to support MAP_SHARED mapped hugely;
2029 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
2030 * But if caller specified an address hint, respect that as before.
2031 */
2032 if (uaddr)
2033 return addr;
2034
2035 if (shmem_huge != SHMEM_HUGE_FORCE) {
2036 struct super_block *sb;
2037
2038 if (file) {
2039 VM_BUG_ON(file->f_op != &shmem_file_operations);
2040 sb = file_inode(file)->i_sb;
2041 } else {
2042 /*
2043 * Called directly from mm/mmap.c, or drivers/char/mem.c
2044 * for "/dev/zero", to create a shared anonymous object.
2045 */
2046 if (IS_ERR(shm_mnt))
2047 return addr;
2048 sb = shm_mnt->mnt_sb;
2049 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002050 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002051 return addr;
2052 }
2053
2054 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2055 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2056 return addr;
2057 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2058 return addr;
2059
2060 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2061 if (inflated_len > TASK_SIZE)
2062 return addr;
2063 if (inflated_len < len)
2064 return addr;
2065
2066 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
2067 if (IS_ERR_VALUE(inflated_addr))
2068 return addr;
2069 if (inflated_addr & ~PAGE_MASK)
2070 return addr;
2071
2072 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2073 inflated_addr += offset - inflated_offset;
2074 if (inflated_offset > offset)
2075 inflated_addr += HPAGE_PMD_SIZE;
2076
2077 if (inflated_addr > TASK_SIZE - len)
2078 return addr;
2079 return inflated_addr;
2080}
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002083static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Al Viro496ad9a2013-01-23 17:07:38 -05002085 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002086 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002089static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2090 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Al Viro496ad9a2013-01-23 17:07:38 -05002092 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002093 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002095 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2096 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098#endif
2099
2100int shmem_lock(struct file *file, int lock, struct user_struct *user)
2101{
Al Viro496ad9a2013-01-23 17:07:38 -05002102 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct shmem_inode_info *info = SHMEM_I(inode);
2104 int retval = -ENOMEM;
2105
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002106 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (lock && !(info->flags & VM_LOCKED)) {
2108 if (!user_shm_lock(inode->i_size, user))
2109 goto out_nomem;
2110 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002111 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113 if (!lock && (info->flags & VM_LOCKED) && user) {
2114 user_shm_unlock(inode->i_size, user);
2115 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002116 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120out_nomem:
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002121 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return retval;
2123}
2124
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002125static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 file_accessed(file);
2128 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002129 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002130 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2131 (vma->vm_end & HPAGE_PMD_MASK)) {
2132 khugepaged_enter(vma, vma->vm_flags);
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return 0;
2135}
2136
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002137static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002138 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 struct inode *inode;
2141 struct shmem_inode_info *info;
2142 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2143
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002144 if (shmem_reserve_inode(sb))
2145 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 inode = new_inode(sb);
2148 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002149 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002150 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002152 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002153 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 info = SHMEM_I(inode);
2155 memset(info, 0, (char *)inode - (char *)info);
2156 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07002157 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002158 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002159 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002161 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002162 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 switch (mode & S_IFMT) {
2165 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002166 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 init_special_inode(inode, mode, dev);
2168 break;
2169 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002170 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 inode->i_op = &shmem_inode_operations;
2172 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002173 mpol_shared_policy_init(&info->policy,
2174 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 break;
2176 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002177 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* Some things misbehave if size == 0 on a directory */
2179 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2180 inode->i_op = &shmem_dir_inode_operations;
2181 inode->i_fop = &simple_dir_operations;
2182 break;
2183 case S_IFLNK:
2184 /*
2185 * Must not load anything in the rbtree,
2186 * mpol_free_shared_policy will not be called.
2187 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002188 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 break;
2190 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002191
2192 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002193 } else
2194 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return inode;
2196}
2197
Johannes Weiner0cd61442014-04-03 14:47:46 -07002198bool shmem_mapping(struct address_space *mapping)
2199{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002200 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002201}
2202
Mike Rapoport8d103962017-09-06 16:23:02 -07002203static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2204 pmd_t *dst_pmd,
2205 struct vm_area_struct *dst_vma,
2206 unsigned long dst_addr,
2207 unsigned long src_addr,
2208 bool zeropage,
2209 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002210{
2211 struct inode *inode = file_inode(dst_vma->vm_file);
2212 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002213 struct address_space *mapping = inode->i_mapping;
2214 gfp_t gfp = mapping_gfp_mask(mapping);
2215 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2216 struct mem_cgroup *memcg;
2217 spinlock_t *ptl;
2218 void *page_kaddr;
2219 struct page *page;
2220 pte_t _dst_pte, *dst_pte;
2221 int ret;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002222 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002223
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002224 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002225 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002226 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002227
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002228 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002229 page = shmem_alloc_page(gfp, info, pgoff);
2230 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002231 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002232
Mike Rapoport8d103962017-09-06 16:23:02 -07002233 if (!zeropage) { /* mcopy_atomic */
2234 page_kaddr = kmap_atomic(page);
2235 ret = copy_from_user(page_kaddr,
2236 (const void __user *)src_addr,
2237 PAGE_SIZE);
2238 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002239
Mike Rapoport8d103962017-09-06 16:23:02 -07002240 /* fallback to copy_from_user outside mmap_sem */
2241 if (unlikely(ret)) {
2242 *pagep = page;
2243 shmem_inode_unacct_blocks(inode, 1);
2244 /* don't free the page */
Andrea Arcangeli9e368252018-11-30 14:09:25 -08002245 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002246 }
2247 } else { /* mfill_zeropage_atomic */
2248 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002249 }
2250 } else {
2251 page = *pagep;
2252 *pagep = NULL;
2253 }
2254
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002255 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2256 __SetPageLocked(page);
2257 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002258 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002259
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002260 ret = -EFAULT;
2261 offset = linear_page_index(dst_vma, dst_addr);
2262 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2263 if (unlikely(offset >= max_off))
2264 goto out_release;
2265
Tejun Heo2cf85582018-07-03 11:14:56 -04002266 ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002267 if (ret)
2268 goto out_release;
2269
Matthew Wilcox552446a2017-12-01 13:25:14 -05002270 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2271 gfp & GFP_RECLAIM_MASK);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002272 if (ret)
2273 goto out_release_uncharge;
2274
2275 mem_cgroup_commit_charge(page, memcg, false, false);
2276
2277 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2278 if (dst_vma->vm_flags & VM_WRITE)
2279 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002280 else {
2281 /*
2282 * We don't set the pte dirty if the vma has no
2283 * VM_WRITE permission, so mark the page dirty or it
2284 * could be freed from under us. We could do it
2285 * unconditionally before unlock_page(), but doing it
2286 * only if VM_WRITE is not set is faster.
2287 */
2288 set_page_dirty(page);
2289 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002290
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002291 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002292
2293 ret = -EFAULT;
2294 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2295 if (unlikely(offset >= max_off))
2296 goto out_release_uncharge_unlock;
2297
2298 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002299 if (!pte_none(*dst_pte))
2300 goto out_release_uncharge_unlock;
2301
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002302 lru_cache_add_anon(page);
2303
2304 spin_lock(&info->lock);
2305 info->alloced++;
2306 inode->i_blocks += BLOCKS_PER_PAGE;
2307 shmem_recalc_inode(inode);
2308 spin_unlock(&info->lock);
2309
2310 inc_mm_counter(dst_mm, mm_counter_file(page));
2311 page_add_file_rmap(page, false);
2312 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2313
2314 /* No need to invalidate - it was non-present before */
2315 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002316 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002317 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002318 ret = 0;
2319out:
2320 return ret;
2321out_release_uncharge_unlock:
2322 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002323 ClearPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002324 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002325out_release_uncharge:
2326 mem_cgroup_cancel_charge(page, memcg, false);
2327out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002328 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002329 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002330out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002331 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002332 goto out;
2333}
2334
Mike Rapoport8d103962017-09-06 16:23:02 -07002335int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2336 pmd_t *dst_pmd,
2337 struct vm_area_struct *dst_vma,
2338 unsigned long dst_addr,
2339 unsigned long src_addr,
2340 struct page **pagep)
2341{
2342 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2343 dst_addr, src_addr, false, pagep);
2344}
2345
2346int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2347 pmd_t *dst_pmd,
2348 struct vm_area_struct *dst_vma,
2349 unsigned long dst_addr)
2350{
2351 struct page *page = NULL;
2352
2353 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2354 dst_addr, 0, true, &page);
2355}
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002358static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002359static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002361#ifdef CONFIG_TMPFS_XATTR
2362static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2363#else
2364#define shmem_initxattrs NULL
2365#endif
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002368shmem_write_begin(struct file *file, struct address_space *mapping,
2369 loff_t pos, unsigned len, unsigned flags,
2370 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Nick Piggin800d15a2007-10-16 01:25:03 -07002372 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002373 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002374 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002375
2376 /* i_mutex is held by caller */
Steven Rostedt (VMware)3f472cc2017-02-24 14:59:10 -08002377 if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
David Herrmann40e041a2014-08-08 14:25:27 -07002378 if (info->seals & F_SEAL_WRITE)
2379 return -EPERM;
2380 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2381 return -EPERM;
2382 }
2383
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002384 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002385}
2386
2387static int
2388shmem_write_end(struct file *file, struct address_space *mapping,
2389 loff_t pos, unsigned len, unsigned copied,
2390 struct page *page, void *fsdata)
2391{
2392 struct inode *inode = mapping->host;
2393
Hugh Dickinsd3602442008-02-04 22:28:44 -08002394 if (pos + copied > inode->i_size)
2395 i_size_write(inode, pos + copied);
2396
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002397 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002398 struct page *head = compound_head(page);
2399 if (PageTransCompound(page)) {
2400 int i;
2401
2402 for (i = 0; i < HPAGE_PMD_NR; i++) {
2403 if (head + i == page)
2404 continue;
2405 clear_highpage(head + i);
2406 flush_dcache_page(head + i);
2407 }
2408 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002409 if (copied < PAGE_SIZE) {
2410 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002411 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002412 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002413 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002414 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002415 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002416 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002417 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002418 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002419
Nick Piggin800d15a2007-10-16 01:25:03 -07002420 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Al Viro2ba5bbe2014-04-02 20:00:02 -04002423static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Al Viro6e58e792014-02-03 17:07:03 -05002425 struct file *file = iocb->ki_filp;
2426 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002428 pgoff_t index;
2429 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002430 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002431 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002432 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002433 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002434
2435 /*
2436 * Might this read be for a stacking filesystem? Then when reading
2437 * holes of a sparse file, we actually need to allocate those pages,
2438 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2439 */
Al Viro777eda22014-12-17 04:46:46 -05002440 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002441 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002443 index = *ppos >> PAGE_SHIFT;
2444 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 for (;;) {
2447 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002448 pgoff_t end_index;
2449 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 loff_t i_size = i_size_read(inode);
2451
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002452 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (index > end_index)
2454 break;
2455 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002456 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (nr <= offset)
2458 break;
2459 }
2460
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002461 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002462 if (error) {
2463 if (error == -EINVAL)
2464 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 break;
2466 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002467 if (page) {
2468 if (sgp == SGP_CACHE)
2469 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002470 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 /*
2474 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002475 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002477 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002479 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002481 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (nr <= offset) {
2483 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002484 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 break;
2486 }
2487 }
2488 nr -= offset;
2489
2490 if (page) {
2491 /*
2492 * If users can be writing to this page using arbitrary
2493 * virtual addresses, take care about potential aliasing
2494 * before reading the page on the kernel side.
2495 */
2496 if (mapping_writably_mapped(mapping))
2497 flush_dcache_page(page);
2498 /*
2499 * Mark the page accessed if we read the beginning.
2500 */
2501 if (!offset)
2502 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002503 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002505 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 /*
2509 * Ok, we have the page, and it's up-to-date, so
2510 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002512 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002513 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002515 index += offset >> PAGE_SHIFT;
2516 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002518 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002519 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 break;
Al Viro6e58e792014-02-03 17:07:03 -05002521 if (ret < nr) {
2522 error = -EFAULT;
2523 break;
2524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 cond_resched();
2526 }
2527
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002528 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002529 file_accessed(file);
2530 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002533/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002534 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002535 */
2536static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002537 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002538{
2539 struct page *page;
2540 struct pagevec pvec;
2541 pgoff_t indices[PAGEVEC_SIZE];
2542 bool done = false;
2543 int i;
2544
Mel Gorman86679822017-11-15 17:37:52 -08002545 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002546 pvec.nr = 1; /* start small: we may be there already */
2547 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002548 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002549 pvec.nr, pvec.pages, indices);
2550 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002551 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002552 index = end;
2553 break;
2554 }
2555 for (i = 0; i < pvec.nr; i++, index++) {
2556 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002557 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002558 done = true;
2559 break;
2560 }
2561 index = indices[i];
2562 }
2563 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002564 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002565 if (!PageUptodate(page))
2566 page = NULL;
2567 }
2568 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002569 (page && whence == SEEK_DATA) ||
2570 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002571 done = true;
2572 break;
2573 }
2574 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002575 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002576 pagevec_release(&pvec);
2577 pvec.nr = PAGEVEC_SIZE;
2578 cond_resched();
2579 }
2580 return index;
2581}
2582
Andrew Morton965c8e52012-12-17 15:59:39 -08002583static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002584{
2585 struct address_space *mapping = file->f_mapping;
2586 struct inode *inode = mapping->host;
2587 pgoff_t start, end;
2588 loff_t new_offset;
2589
Andrew Morton965c8e52012-12-17 15:59:39 -08002590 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2591 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002592 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002593 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002594 /* We're holding i_mutex so we can access i_size directly */
2595
Yufen Yu1a413642018-11-16 15:08:39 -08002596 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002597 offset = -ENXIO;
2598 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002599 start = offset >> PAGE_SHIFT;
2600 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002601 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002602 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002603 if (new_offset > offset) {
2604 if (new_offset < inode->i_size)
2605 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002606 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002607 offset = -ENXIO;
2608 else
2609 offset = inode->i_size;
2610 }
2611 }
2612
Hugh Dickins387aae62013-08-04 11:30:25 -07002613 if (offset >= 0)
2614 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002615 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002616 return offset;
2617}
2618
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002619static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2620 loff_t len)
2621{
Al Viro496ad9a2013-01-23 17:07:38 -05002622 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002623 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002624 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002625 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002626 pgoff_t start, index, end;
2627 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002628
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002629 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2630 return -EOPNOTSUPP;
2631
Al Viro59551022016-01-22 15:40:57 -05002632 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002633
2634 if (mode & FALLOC_FL_PUNCH_HOLE) {
2635 struct address_space *mapping = file->f_mapping;
2636 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2637 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002638 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002639
David Herrmann40e041a2014-08-08 14:25:27 -07002640 /* protected by i_mutex */
2641 if (info->seals & F_SEAL_WRITE) {
2642 error = -EPERM;
2643 goto out;
2644 }
2645
Hugh Dickins8e205f72014-07-23 14:00:10 -07002646 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002647 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2648 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2649 spin_lock(&inode->i_lock);
2650 inode->i_private = &shmem_falloc;
2651 spin_unlock(&inode->i_lock);
2652
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002653 if ((u64)unmap_end > (u64)unmap_start)
2654 unmap_mapping_range(mapping, unmap_start,
2655 1 + unmap_end - unmap_start, 0);
2656 shmem_truncate_range(inode, offset, offset + len - 1);
2657 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002658
2659 spin_lock(&inode->i_lock);
2660 inode->i_private = NULL;
2661 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002662 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002663 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002664 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002665 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002666 }
2667
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002668 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2669 error = inode_newsize_ok(inode, offset + len);
2670 if (error)
2671 goto out;
2672
David Herrmann40e041a2014-08-08 14:25:27 -07002673 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2674 error = -EPERM;
2675 goto out;
2676 }
2677
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002678 start = offset >> PAGE_SHIFT;
2679 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002680 /* Try to avoid a swapstorm if len is impossible to satisfy */
2681 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2682 error = -ENOSPC;
2683 goto out;
2684 }
2685
Hugh Dickins8e205f72014-07-23 14:00:10 -07002686 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002687 shmem_falloc.start = start;
2688 shmem_falloc.next = start;
2689 shmem_falloc.nr_falloced = 0;
2690 shmem_falloc.nr_unswapped = 0;
2691 spin_lock(&inode->i_lock);
2692 inode->i_private = &shmem_falloc;
2693 spin_unlock(&inode->i_lock);
2694
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002695 for (index = start; index < end; index++) {
2696 struct page *page;
2697
2698 /*
2699 * Good, the fallocate(2) manpage permits EINTR: we may have
2700 * been interrupted because we are using up too much memory.
2701 */
2702 if (signal_pending(current))
2703 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002704 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2705 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002706 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002707 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002708 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002709 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002710 if (index > start) {
2711 shmem_undo_range(inode,
2712 (loff_t)start << PAGE_SHIFT,
2713 ((loff_t)index << PAGE_SHIFT) - 1, true);
2714 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002715 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002716 }
2717
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002718 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002719 * Inform shmem_writepage() how far we have reached.
2720 * No need for lock or barrier: we have the page lock.
2721 */
2722 shmem_falloc.next++;
2723 if (!PageUptodate(page))
2724 shmem_falloc.nr_falloced++;
2725
2726 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002727 * If !PageUptodate, leave it that way so that freeable pages
2728 * can be recognized if we need to rollback on error later.
2729 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002730 * than free the pages we are allocating (and SGP_CACHE pages
2731 * might still be clean: we now need to mark those dirty too).
2732 */
2733 set_page_dirty(page);
2734 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002735 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002736 cond_resched();
2737 }
2738
2739 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2740 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002741 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002742undone:
2743 spin_lock(&inode->i_lock);
2744 inode->i_private = NULL;
2745 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002746out:
Al Viro59551022016-01-22 15:40:57 -05002747 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002748 return error;
2749}
2750
David Howells726c3342006-06-23 02:02:58 -07002751static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
David Howells726c3342006-06-23 02:02:58 -07002753 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002756 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002758 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002760 buf->f_bavail =
2761 buf->f_bfree = sbinfo->max_blocks -
2762 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002763 }
2764 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 buf->f_files = sbinfo->max_inodes;
2766 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768 /* else leave those fields 0 like simple_statfs */
2769 return 0;
2770}
2771
2772/*
2773 * File creation. Allocate an inode, and we're done..
2774 */
2775static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002776shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002778 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 int error = -ENOSPC;
2780
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002781 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002783 error = simple_acl_create(dir, inode);
2784 if (error)
2785 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002786 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002787 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002788 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002789 if (error && error != -EOPNOTSUPP)
2790 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002791
Al Viro718deb62009-12-16 19:35:36 -05002792 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002794 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 d_instantiate(dentry, inode);
2796 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002799out_iput:
2800 iput(inode);
2801 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Al Viro60545d02013-06-07 01:20:27 -04002804static int
2805shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2806{
2807 struct inode *inode;
2808 int error = -ENOSPC;
2809
2810 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2811 if (inode) {
2812 error = security_inode_init_security(inode, dir,
2813 NULL,
2814 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002815 if (error && error != -EOPNOTSUPP)
2816 goto out_iput;
2817 error = simple_acl_create(dir, inode);
2818 if (error)
2819 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002820 d_tmpfile(dentry, inode);
2821 }
2822 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002823out_iput:
2824 iput(inode);
2825 return error;
Al Viro60545d02013-06-07 01:20:27 -04002826}
2827
Al Viro18bb1db2011-07-26 01:41:39 -04002828static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 int error;
2831
2832 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2833 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002834 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 0;
2836}
2837
Al Viro4acdaf22011-07-26 01:42:34 -04002838static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002839 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2842}
2843
2844/*
2845 * Link a file..
2846 */
2847static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2848{
David Howells75c3cfa2015-03-17 22:26:12 +00002849 struct inode *inode = d_inode(old_dentry);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002850 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 /*
2853 * No ordinary (disk based) filesystem counts links as inodes;
2854 * but each new link needs a new dentry, pinning lowmem, and
2855 * tmpfs dentries cannot be pruned until they are unlinked.
2856 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002857 ret = shmem_reserve_inode(inode->i_sb);
2858 if (ret)
2859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002862 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002863 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002864 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 dget(dentry); /* Extra pinning count for the created dentry */
2866 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002867out:
2868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2872{
David Howells75c3cfa2015-03-17 22:26:12 +00002873 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002875 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2876 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002879 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002880 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 dput(dentry); /* Undo the count from "create" - this does all the work */
2882 return 0;
2883}
2884
2885static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2886{
2887 if (!simple_empty(dentry))
2888 return -ENOTEMPTY;
2889
David Howells75c3cfa2015-03-17 22:26:12 +00002890 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002891 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return shmem_unlink(dir, dentry);
2893}
2894
Miklos Szeredi37456772014-07-23 15:15:34 +02002895static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2896{
David Howellse36cb0b2015-01-29 12:02:35 +00002897 bool old_is_dir = d_is_dir(old_dentry);
2898 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002899
2900 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2901 if (old_is_dir) {
2902 drop_nlink(old_dir);
2903 inc_nlink(new_dir);
2904 } else {
2905 drop_nlink(new_dir);
2906 inc_nlink(old_dir);
2907 }
2908 }
2909 old_dir->i_ctime = old_dir->i_mtime =
2910 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002911 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002912 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02002913
2914 return 0;
2915}
2916
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002917static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2918{
2919 struct dentry *whiteout;
2920 int error;
2921
2922 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2923 if (!whiteout)
2924 return -ENOMEM;
2925
2926 error = shmem_mknod(old_dir, whiteout,
2927 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2928 dput(whiteout);
2929 if (error)
2930 return error;
2931
2932 /*
2933 * Cheat and hash the whiteout while the old dentry is still in
2934 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2935 *
2936 * d_lookup() will consistently find one of them at this point,
2937 * not sure which one, but that isn't even important.
2938 */
2939 d_rehash(whiteout);
2940 return 0;
2941}
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943/*
2944 * The VFS layer already does all the dentry stuff for rename,
2945 * we just have to decrement the usage count for the target if
2946 * it exists so that the VFS layer correctly free's it when it
2947 * gets overwritten.
2948 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002949static 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 -07002950{
David Howells75c3cfa2015-03-17 22:26:12 +00002951 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 int they_are_dirs = S_ISDIR(inode->i_mode);
2953
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002954 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002955 return -EINVAL;
2956
Miklos Szeredi37456772014-07-23 15:15:34 +02002957 if (flags & RENAME_EXCHANGE)
2958 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (!simple_empty(new_dentry))
2961 return -ENOTEMPTY;
2962
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002963 if (flags & RENAME_WHITEOUT) {
2964 int error;
2965
2966 error = shmem_whiteout(old_dir, old_dentry);
2967 if (error)
2968 return error;
2969 }
2970
David Howells75c3cfa2015-03-17 22:26:12 +00002971 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02002973 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00002974 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002975 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02002976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002978 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002979 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981
2982 old_dir->i_size -= BOGO_DIRENT_SIZE;
2983 new_dir->i_size += BOGO_DIRENT_SIZE;
2984 old_dir->i_ctime = old_dir->i_mtime =
2985 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002986 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return 0;
2988}
2989
2990static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2991{
2992 int error;
2993 int len;
2994 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002995 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002998 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return -ENAMETOOLONG;
3000
Joe Perches0825a6f2018-06-14 15:27:58 -07003001 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3002 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 if (!inode)
3004 return -ENOSPC;
3005
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003006 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003007 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003008 if (error) {
3009 if (error != -EOPNOTSUPP) {
3010 iput(inode);
3011 return error;
3012 }
3013 error = 0;
3014 }
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003017 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003018 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3019 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003020 iput(inode);
3021 return -ENOMEM;
3022 }
3023 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003025 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003026 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (error) {
3028 iput(inode);
3029 return error;
3030 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003031 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003033 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003034 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003036 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003037 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003040 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 d_instantiate(dentry, inode);
3042 dget(dentry);
3043 return 0;
3044}
3045
Al Virofceef392015-12-29 15:58:39 -05003046static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Al Virofceef392015-12-29 15:58:39 -05003048 mark_page_accessed(arg);
3049 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Al Viro6b255392015-11-17 10:20:54 -05003052static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003053 struct inode *inode,
3054 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003057 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003058 if (!dentry) {
3059 page = find_get_page(inode->i_mapping, 0);
3060 if (!page)
3061 return ERR_PTR(-ECHILD);
3062 if (!PageUptodate(page)) {
3063 put_page(page);
3064 return ERR_PTR(-ECHILD);
3065 }
3066 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003067 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003068 if (error)
3069 return ERR_PTR(error);
3070 unlock_page(page);
3071 }
Al Virofceef392015-12-29 15:58:39 -05003072 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003073 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
3075
Eric Parisb09e0fa2011-05-24 17:12:39 -07003076#ifdef CONFIG_TMPFS_XATTR
3077/*
3078 * Superblocks without xattr inode operations may get some security.* xattr
3079 * support from the LSM "for free". As soon as we have any other xattrs
3080 * like ACLs, we also need to implement the security.* handlers at
3081 * filesystem level, though.
3082 */
3083
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003084/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003085 * Callback for security_inode_init_security() for acquiring xattrs.
3086 */
3087static int shmem_initxattrs(struct inode *inode,
3088 const struct xattr *xattr_array,
3089 void *fs_info)
3090{
3091 struct shmem_inode_info *info = SHMEM_I(inode);
3092 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003093 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003094 size_t len;
3095
3096 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003097 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003098 if (!new_xattr)
3099 return -ENOMEM;
3100
3101 len = strlen(xattr->name) + 1;
3102 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3103 GFP_KERNEL);
3104 if (!new_xattr->name) {
3105 kfree(new_xattr);
3106 return -ENOMEM;
3107 }
3108
3109 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3110 XATTR_SECURITY_PREFIX_LEN);
3111 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3112 xattr->name, len);
3113
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003114 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003115 }
3116
3117 return 0;
3118}
3119
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003120static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003121 struct dentry *unused, struct inode *inode,
3122 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003123{
Al Virob2968212016-04-10 20:48:24 -04003124 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003125
3126 name = xattr_full_name(handler, name);
3127 return simple_xattr_get(&info->xattrs, name, buffer, size);
3128}
3129
3130static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003131 struct dentry *unused, struct inode *inode,
3132 const char *name, const void *value,
3133 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003134{
Al Viro59301222016-05-27 10:19:30 -04003135 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003136
3137 name = xattr_full_name(handler, name);
3138 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3139}
3140
3141static const struct xattr_handler shmem_security_xattr_handler = {
3142 .prefix = XATTR_SECURITY_PREFIX,
3143 .get = shmem_xattr_handler_get,
3144 .set = shmem_xattr_handler_set,
3145};
3146
3147static const struct xattr_handler shmem_trusted_xattr_handler = {
3148 .prefix = XATTR_TRUSTED_PREFIX,
3149 .get = shmem_xattr_handler_get,
3150 .set = shmem_xattr_handler_set,
3151};
3152
Eric Parisb09e0fa2011-05-24 17:12:39 -07003153static const struct xattr_handler *shmem_xattr_handlers[] = {
3154#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003155 &posix_acl_access_xattr_handler,
3156 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003157#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003158 &shmem_security_xattr_handler,
3159 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003160 NULL
3161};
3162
Eric Parisb09e0fa2011-05-24 17:12:39 -07003163static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3164{
David Howells75c3cfa2015-03-17 22:26:12 +00003165 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003166 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003167}
3168#endif /* CONFIG_TMPFS_XATTR */
3169
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003170static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003171 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003172#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003173 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175};
3176
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003177static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003178 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003179#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003180 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003181#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003182};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003183
David M. Grimes91828a42006-10-17 00:09:45 -07003184static struct dentry *shmem_get_parent(struct dentry *child)
3185{
3186 return ERR_PTR(-ESTALE);
3187}
3188
3189static int shmem_match(struct inode *ino, void *vfh)
3190{
3191 __u32 *fh = vfh;
3192 __u64 inum = fh[2];
3193 inum = (inum << 32) | fh[1];
3194 return ino->i_ino == inum && fh[0] == ino->i_generation;
3195}
3196
Amir Goldstein12ba7802018-06-07 17:07:15 -07003197/* Find any alias of inode, but prefer a hashed alias */
3198static struct dentry *shmem_find_alias(struct inode *inode)
3199{
3200 struct dentry *alias = d_find_alias(inode);
3201
3202 return alias ?: d_find_any_alias(inode);
3203}
3204
3205
Christoph Hellwig480b1162007-10-21 16:42:13 -07003206static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3207 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003208{
David M. Grimes91828a42006-10-17 00:09:45 -07003209 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003210 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003211 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003212
Christoph Hellwig480b1162007-10-21 16:42:13 -07003213 if (fh_len < 3)
3214 return NULL;
3215
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003216 inum = fid->raw[2];
3217 inum = (inum << 32) | fid->raw[1];
3218
Christoph Hellwig480b1162007-10-21 16:42:13 -07003219 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3220 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003221 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003222 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003223 iput(inode);
3224 }
3225
Christoph Hellwig480b1162007-10-21 16:42:13 -07003226 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003227}
3228
Al Virob0b03822012-04-02 14:34:06 -04003229static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3230 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003231{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303232 if (*len < 3) {
3233 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003234 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303235 }
David M. Grimes91828a42006-10-17 00:09:45 -07003236
Al Viro1d3382cb2010-10-23 15:19:20 -04003237 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003238 /* Unfortunately insert_inode_hash is not idempotent,
3239 * so as we hash inodes here rather than at creation
3240 * time, we need a lock to ensure we only try
3241 * to do it once
3242 */
3243 static DEFINE_SPINLOCK(lock);
3244 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003245 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003246 __insert_inode_hash(inode,
3247 inode->i_ino + inode->i_generation);
3248 spin_unlock(&lock);
3249 }
3250
3251 fh[0] = inode->i_generation;
3252 fh[1] = inode->i_ino;
3253 fh[2] = ((__u64)inode->i_ino) >> 32;
3254
3255 *len = 3;
3256 return 1;
3257}
3258
Christoph Hellwig39655162007-10-21 16:42:17 -07003259static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003260 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003261 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003262 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003263};
3264
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003265static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3266 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
3268 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003269 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003270 uid_t uid;
3271 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003273 while (options != NULL) {
3274 this_char = options;
3275 for (;;) {
3276 /*
3277 * NUL-terminate this option: unfortunately,
3278 * mount options form a comma-separated list,
3279 * but mpol's nodelist may also contain commas.
3280 */
3281 options = strchr(options, ',');
3282 if (options == NULL)
3283 break;
3284 options++;
3285 if (!isdigit(*options)) {
3286 options[-1] = '\0';
3287 break;
3288 }
3289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (!*this_char)
3291 continue;
3292 if ((value = strchr(this_char,'=')) != NULL) {
3293 *value++ = 0;
3294 } else {
Joe Perches11705322016-03-17 14:19:50 -07003295 pr_err("tmpfs: No value for mount option '%s'\n",
3296 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003297 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
3299
3300 if (!strcmp(this_char,"size")) {
3301 unsigned long long size;
3302 size = memparse(value,&rest);
3303 if (*rest == '%') {
3304 size <<= PAGE_SHIFT;
3305 size *= totalram_pages;
3306 do_div(size, 100);
3307 rest++;
3308 }
3309 if (*rest)
3310 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003311 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003312 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003314 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (*rest)
3316 goto bad_val;
3317 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003318 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (*rest)
3320 goto bad_val;
3321 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003322 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003324 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 if (*rest)
3326 goto bad_val;
3327 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003328 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003330 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 if (*rest)
3332 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003333 sbinfo->uid = make_kuid(current_user_ns(), uid);
3334 if (!uid_valid(sbinfo->uid))
3335 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003337 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003339 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (*rest)
3341 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003342 sbinfo->gid = make_kgid(current_user_ns(), gid);
3343 if (!gid_valid(sbinfo->gid))
3344 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003345#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003346 } else if (!strcmp(this_char, "huge")) {
3347 int huge;
3348 huge = shmem_parse_huge(value);
3349 if (huge < 0)
3350 goto bad_val;
3351 if (!has_transparent_hugepage() &&
3352 huge != SHMEM_HUGE_NEVER)
3353 goto bad_val;
3354 sbinfo->huge = huge;
3355#endif
3356#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003357 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003358 mpol_put(mpol);
3359 mpol = NULL;
3360 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003361 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 } else {
Joe Perches11705322016-03-17 14:19:50 -07003364 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003365 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003368 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return 0;
3370
3371bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003372 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003374error:
3375 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return 1;
3377
3378}
3379
3380static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3381{
3382 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003383 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003384 unsigned long inodes;
3385 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Greg Thelen5f001102013-02-22 16:36:01 -08003387 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003388 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003389 return error;
3390
3391 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003392 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003393 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003394 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003395 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003396 goto out;
3397 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003398 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003399 * but we must separately disallow unlimited->limited, because
3400 * in that case we have no record of how much is already in use.
3401 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003402 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003403 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003404 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003405 goto out;
3406
3407 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003408 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003409 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003410 sbinfo->max_inodes = config.max_inodes;
3411 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003412
Greg Thelen5f001102013-02-22 16:36:01 -08003413 /*
3414 * Preserve previous mempolicy unless mpol remount option was specified.
3415 */
3416 if (config.mpol) {
3417 mpol_put(sbinfo->mpol);
3418 sbinfo->mpol = config.mpol; /* transfers initial ref */
3419 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003420out:
3421 spin_unlock(&sbinfo->stat_lock);
3422 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003424
Al Viro34c80b12011-12-08 21:32:45 -05003425static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003426{
Al Viro34c80b12011-12-08 21:32:45 -05003427 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003428
3429 if (sbinfo->max_blocks != shmem_default_max_blocks())
3430 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003431 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003432 if (sbinfo->max_inodes != shmem_default_max_inodes())
3433 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003434 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003435 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003436 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3437 seq_printf(seq, ",uid=%u",
3438 from_kuid_munged(&init_user_ns, sbinfo->uid));
3439 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3440 seq_printf(seq, ",gid=%u",
3441 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003442#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003443 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3444 if (sbinfo->huge)
3445 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3446#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003447 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003448 return 0;
3449}
David Herrmann9183df22014-08-08 14:25:29 -07003450
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003451#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453static void shmem_put_super(struct super_block *sb)
3454{
Hugh Dickins602586a2010-08-17 15:23:56 -07003455 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3456
3457 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003458 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003459 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 sb->s_fs_info = NULL;
3461}
3462
Kay Sievers2b2af542009-04-30 15:23:42 +02003463int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
3465 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003466 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003467 int err = -ENOMEM;
3468
3469 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003470 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003471 L1_CACHE_BYTES), GFP_KERNEL);
3472 if (!sbinfo)
3473 return -ENOMEM;
3474
Joe Perches0825a6f2018-06-14 15:27:58 -07003475 sbinfo->mode = 0777 | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003476 sbinfo->uid = current_fsuid();
3477 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003478 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003480#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 /*
3482 * Per default we only allow half of the physical ram per
3483 * tmpfs instance, limiting inodes to one per page of lowmem;
3484 * but the internal instance is left unlimited.
3485 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003486 if (!(sb->s_flags & SB_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003487 sbinfo->max_blocks = shmem_default_max_blocks();
3488 sbinfo->max_inodes = shmem_default_max_inodes();
3489 if (shmem_parse_options(data, sbinfo, false)) {
3490 err = -EINVAL;
3491 goto failed;
3492 }
Al Viroca4e0512013-08-31 12:57:10 -04003493 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003494 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
David M. Grimes91828a42006-10-17 00:09:45 -07003496 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003497 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003499 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500#endif
3501
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003502 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003503 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003504 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003505 sbinfo->free_inodes = sbinfo->max_inodes;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003506 spin_lock_init(&sbinfo->shrinklist_lock);
3507 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003508
Hugh Dickins285b2c42011-08-03 16:21:20 -07003509 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003510 sb->s_blocksize = PAGE_SIZE;
3511 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 sb->s_magic = TMPFS_MAGIC;
3513 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003514 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003515#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003516 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003517#endif
3518#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003519 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003520#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003521 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003522
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003523 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 if (!inode)
3525 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003526 inode->i_uid = sbinfo->uid;
3527 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003528 sb->s_root = d_make_root(inode);
3529 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003530 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return 0;
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533failed:
3534 shmem_put_super(sb);
3535 return err;
3536}
3537
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003538static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540static struct inode *shmem_alloc_inode(struct super_block *sb)
3541{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003542 struct shmem_inode_info *info;
3543 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3544 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003546 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003549static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003550{
3551 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003552 if (S_ISLNK(inode->i_mode))
3553 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003554 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3555}
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557static void shmem_destroy_inode(struct inode *inode)
3558{
Al Viro09208d12011-07-26 03:15:03 -04003559 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003561 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003564static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003566 struct shmem_inode_info *info = foo;
3567 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003570static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3573 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003574 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003577static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003579 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003582static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003584 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003586 .write_begin = shmem_write_begin,
3587 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003589#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003590 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003591#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003592 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593};
3594
Helge Deller15ad7cd2006-12-06 20:40:36 -08003595static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003597 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003599 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003600 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003601 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003602 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003603 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003604 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003605 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606#endif
3607};
3608
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003609static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003610 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003611 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003612#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003613 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003614 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616};
3617
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003618static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619#ifdef CONFIG_TMPFS
3620 .create = shmem_create,
3621 .lookup = simple_lookup,
3622 .link = shmem_link,
3623 .unlink = shmem_unlink,
3624 .symlink = shmem_symlink,
3625 .mkdir = shmem_mkdir,
3626 .rmdir = shmem_rmdir,
3627 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003628 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003629 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003631#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003632 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003633#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003634#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003635 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003636 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003637#endif
3638};
3639
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003640static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003641#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003642 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003643#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003644#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003645 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003646 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003647#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648};
3649
Hugh Dickins759b9772007-03-05 00:30:28 -08003650static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 .alloc_inode = shmem_alloc_inode,
3652 .destroy_inode = shmem_destroy_inode,
3653#ifdef CONFIG_TMPFS
3654 .statfs = shmem_statfs,
3655 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003656 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657#endif
Al Viro1f895f72010-06-05 19:10:41 -04003658 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 .drop_inode = generic_delete_inode,
3660 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003661#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3662 .nr_cached_objects = shmem_unused_huge_count,
3663 .free_cached_objects = shmem_unused_huge_scan,
3664#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665};
3666
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003667static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003668 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003669 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670#ifdef CONFIG_NUMA
3671 .set_policy = shmem_set_policy,
3672 .get_policy = shmem_get_policy,
3673#endif
3674};
3675
Al Viro3c26ff62010-07-25 11:46:36 +04003676static struct dentry *shmem_mount(struct file_system_type *fs_type,
3677 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Al Viro3c26ff62010-07-25 11:46:36 +04003679 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003682static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 .owner = THIS_MODULE,
3684 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003685 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003687 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003690int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
3692 int error;
3693
Rob Landley16203a72013-09-11 14:26:12 -07003694 /* If rootfs called this, don't re-init */
3695 if (shmem_inode_cachep)
3696 return 0;
3697
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003698 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003700 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003702 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 goto out2;
3704 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003705
Al Viroca4e0512013-08-31 12:57:10 -04003706 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 if (IS_ERR(shm_mnt)) {
3708 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003709 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 goto out1;
3711 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003712
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003713#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003714 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003715 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3716 else
3717 shmem_huge = 0; /* just in case it was patched */
3718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 return 0;
3720
3721out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003722 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003724 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 shm_mnt = ERR_PTR(error);
3726 return error;
3727}
Matt Mackall853ac432009-01-06 14:40:20 -08003728
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003729#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003730static ssize_t shmem_enabled_show(struct kobject *kobj,
3731 struct kobj_attribute *attr, char *buf)
3732{
3733 int values[] = {
3734 SHMEM_HUGE_ALWAYS,
3735 SHMEM_HUGE_WITHIN_SIZE,
3736 SHMEM_HUGE_ADVISE,
3737 SHMEM_HUGE_NEVER,
3738 SHMEM_HUGE_DENY,
3739 SHMEM_HUGE_FORCE,
3740 };
3741 int i, count;
3742
3743 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3744 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3745
3746 count += sprintf(buf + count, fmt,
3747 shmem_format_huge(values[i]));
3748 }
3749 buf[count - 1] = '\n';
3750 return count;
3751}
3752
3753static ssize_t shmem_enabled_store(struct kobject *kobj,
3754 struct kobj_attribute *attr, const char *buf, size_t count)
3755{
3756 char tmp[16];
3757 int huge;
3758
3759 if (count + 1 > sizeof(tmp))
3760 return -EINVAL;
3761 memcpy(tmp, buf, count);
3762 tmp[count] = '\0';
3763 if (count && tmp[count - 1] == '\n')
3764 tmp[count - 1] = '\0';
3765
3766 huge = shmem_parse_huge(tmp);
3767 if (huge == -EINVAL)
3768 return -EINVAL;
3769 if (!has_transparent_hugepage() &&
3770 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3771 return -EINVAL;
3772
3773 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003774 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003775 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3776 return count;
3777}
3778
3779struct kobj_attribute shmem_enabled_attr =
3780 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07003781#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003782
Arnd Bergmann3b337192016-08-10 16:27:44 -07003783#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003784bool shmem_huge_enabled(struct vm_area_struct *vma)
3785{
3786 struct inode *inode = file_inode(vma->vm_file);
3787 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3788 loff_t i_size;
3789 pgoff_t off;
3790
3791 if (shmem_huge == SHMEM_HUGE_FORCE)
3792 return true;
3793 if (shmem_huge == SHMEM_HUGE_DENY)
3794 return false;
3795 switch (sbinfo->huge) {
3796 case SHMEM_HUGE_NEVER:
3797 return false;
3798 case SHMEM_HUGE_ALWAYS:
3799 return true;
3800 case SHMEM_HUGE_WITHIN_SIZE:
3801 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3802 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3803 if (i_size >= HPAGE_PMD_SIZE &&
3804 i_size >> PAGE_SHIFT >= off)
3805 return true;
Gustavo A. R. Silvac8402872017-11-15 17:38:45 -08003806 /* fall through */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003807 case SHMEM_HUGE_ADVISE:
3808 /* TODO: implement fadvise() hints */
3809 return (vma->vm_flags & VM_HUGEPAGE);
3810 default:
3811 VM_BUG_ON(1);
3812 return false;
3813 }
3814}
Arnd Bergmann3b337192016-08-10 16:27:44 -07003815#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003816
Matt Mackall853ac432009-01-06 14:40:20 -08003817#else /* !CONFIG_SHMEM */
3818
3819/*
3820 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3821 *
3822 * This is intended for small system where the benefits of the full
3823 * shmem code (swap-backed and resource-limited) are outweighed by
3824 * their complexity. On systems without swap this code should be
3825 * effectively equivalent, but much lighter weight.
3826 */
3827
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003828static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003829 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003830 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003831 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003832 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003833};
3834
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003835int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003836{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003837 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003838
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003839 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003840 BUG_ON(IS_ERR(shm_mnt));
3841
3842 return 0;
3843}
3844
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003845int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003846{
3847 return 0;
3848}
3849
Hugh Dickins3f96b792009-09-21 17:03:37 -07003850int shmem_lock(struct file *file, int lock, struct user_struct *user)
3851{
3852 return 0;
3853}
3854
Hugh Dickins24513262012-01-20 14:34:21 -08003855void shmem_unlock_mapping(struct address_space *mapping)
3856{
3857}
3858
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003859#ifdef CONFIG_MMU
3860unsigned long shmem_get_unmapped_area(struct file *file,
3861 unsigned long addr, unsigned long len,
3862 unsigned long pgoff, unsigned long flags)
3863{
3864 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3865}
3866#endif
3867
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003868void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003869{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003870 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003871}
3872EXPORT_SYMBOL_GPL(shmem_truncate_range);
3873
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003874#define shmem_vm_ops generic_file_vm_ops
3875#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003876#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003877#define shmem_acct_size(flags, size) 0
3878#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003879
3880#endif /* CONFIG_SHMEM */
3881
3882/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
Matthew Auld703321b2017-10-06 23:18:13 +01003884static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00003885 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04003888 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Matthew Auld703321b2017-10-06 23:18:13 +01003890 if (IS_ERR(mnt))
3891 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Hugh Dickins285b2c42011-08-03 16:21:20 -07003893 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return ERR_PTR(-EINVAL);
3895
3896 if (shmem_acct_size(flags, size))
3897 return ERR_PTR(-ENOMEM);
3898
Al Viro93dec2d2018-07-08 23:02:03 -04003899 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
3900 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04003901 if (unlikely(!inode)) {
3902 shmem_unacct_size(flags, size);
3903 return ERR_PTR(-ENOSPC);
3904 }
Eric Parisc7277092013-12-02 11:24:19 +00003905 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003907 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003908 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04003909 if (!IS_ERR(res))
3910 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
3911 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05003912 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04003913 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05003914 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915}
Eric Parisc7277092013-12-02 11:24:19 +00003916
3917/**
3918 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3919 * kernel internal. There will be NO LSM permission checks against the
3920 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003921 * higher layer. The users are the big_key and shm implementations. LSM
3922 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003923 * @name: name for dentry (to be seen in /proc/<pid>/maps
3924 * @size: size to be set for the file
3925 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3926 */
3927struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3928{
Matthew Auld703321b2017-10-06 23:18:13 +01003929 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00003930}
3931
3932/**
3933 * shmem_file_setup - get an unlinked file living in tmpfs
3934 * @name: name for dentry (to be seen in /proc/<pid>/maps
3935 * @size: size to be set for the file
3936 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3937 */
3938struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3939{
Matthew Auld703321b2017-10-06 23:18:13 +01003940 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00003941}
Keith Packard395e0dd2008-06-20 00:08:06 -07003942EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Randy Dunlap46711812008-03-19 17:00:41 -07003944/**
Matthew Auld703321b2017-10-06 23:18:13 +01003945 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
3946 * @mnt: the tmpfs mount where the file will be created
3947 * @name: name for dentry (to be seen in /proc/<pid>/maps
3948 * @size: size to be set for the file
3949 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3950 */
3951struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
3952 loff_t size, unsigned long flags)
3953{
3954 return __shmem_file_setup(mnt, name, size, flags, 0);
3955}
3956EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
3957
3958/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3961 */
3962int shmem_zero_setup(struct vm_area_struct *vma)
3963{
3964 struct file *file;
3965 loff_t size = vma->vm_end - vma->vm_start;
3966
Hugh Dickins66fc1302015-06-14 09:48:09 -07003967 /*
3968 * Cloning a new file under mmap_sem leads to a lock ordering conflict
3969 * between XFS directory reading and selinux: since this file is only
3970 * accessible to the user through its mapping, use S_PRIVATE flag to
3971 * bypass file security, in the same way as shmem_kernel_file_setup().
3972 */
Matthew Auld703321b2017-10-06 23:18:13 +01003973 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 if (IS_ERR(file))
3975 return PTR_ERR(file);
3976
3977 if (vma->vm_file)
3978 fput(vma->vm_file);
3979 vma->vm_file = file;
3980 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003981
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003982 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003983 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
3984 (vma->vm_end & HPAGE_PMD_MASK)) {
3985 khugepaged_enter(vma, vma->vm_flags);
3986 }
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return 0;
3989}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003990
3991/**
3992 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3993 * @mapping: the page's address_space
3994 * @index: the page index
3995 * @gfp: the page allocator flags to use if allocating
3996 *
3997 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3998 * with any new page allocations done using the specified allocation flags.
3999 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4000 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4001 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4002 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004003 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4004 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004005 */
4006struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4007 pgoff_t index, gfp_t gfp)
4008{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004009#ifdef CONFIG_SHMEM
4010 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004011 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004012 int error;
4013
4014 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004015 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004016 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004017 if (error)
4018 page = ERR_PTR(error);
4019 else
4020 unlock_page(page);
4021 return page;
4022#else
4023 /*
4024 * The tiny !SHMEM case uses ramfs without swap
4025 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004026 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004027#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004028}
4029EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);