blob: 6adbdd3498754a8743f2ebc7c6d1ffca33a79b57 [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{
117 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
118}
Andrew Mortonb76db732008-02-08 04:21:49 -0800119#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800120
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700121static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
122static int shmem_replace_page(struct page **pagep, gfp_t gfp,
123 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700124static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700125 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800126 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700127 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700128
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700129int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700130 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700131{
132 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800133 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
137{
138 return sb->s_fs_info;
139}
140
141/*
142 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
143 * for shared memory and for shared anonymous (/dev/zero) mappings
144 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
145 * consistent with the pre-accounting of private mappings ...
146 */
147static inline int shmem_acct_size(unsigned long flags, loff_t size)
148{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000149 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000150 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static inline void shmem_unacct_size(unsigned long flags, loff_t size)
154{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000155 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 vm_unacct_memory(VM_ACCT(size));
157}
158
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700159static inline int shmem_reacct_size(unsigned long flags,
160 loff_t oldsize, loff_t newsize)
161{
162 if (!(flags & VM_NORESERVE)) {
163 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
164 return security_vm_enough_memory_mm(current->mm,
165 VM_ACCT(newsize) - VM_ACCT(oldsize));
166 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
167 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
168 }
169 return 0;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700174 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
176 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
177 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700178static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700180 if (!(flags & VM_NORESERVE))
181 return 0;
182
183 return security_vm_enough_memory_mm(current->mm,
184 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static inline void shmem_unacct_blocks(unsigned long flags, long pages)
188{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000189 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300190 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Mike Rapoport0f079692017-09-06 16:22:59 -0700193static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
194{
195 struct shmem_inode_info *info = SHMEM_I(inode);
196 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
197
198 if (shmem_acct_block(info->flags, pages))
199 return false;
200
201 if (sbinfo->max_blocks) {
202 if (percpu_counter_compare(&sbinfo->used_blocks,
203 sbinfo->max_blocks - pages) > 0)
204 goto unacct;
205 percpu_counter_add(&sbinfo->used_blocks, pages);
206 }
207
208 return true;
209
210unacct:
211 shmem_unacct_blocks(info->flags, pages);
212 return false;
213}
214
215static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
216{
217 struct shmem_inode_info *info = SHMEM_I(inode);
218 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
219
220 if (sbinfo->max_blocks)
221 percpu_counter_sub(&sbinfo->used_blocks, pages);
222 shmem_unacct_blocks(info->flags, pages);
223}
224
Hugh Dickins759b9772007-03-05 00:30:28 -0800225static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700226static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800227static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800228static const struct inode_operations shmem_inode_operations;
229static const struct inode_operations shmem_dir_inode_operations;
230static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400231static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700232static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Mike Rapoportb0506e42017-02-22 15:43:28 -0800234bool vma_is_shmem(struct vm_area_struct *vma)
235{
236 return vma->vm_ops == &shmem_vm_ops;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800240static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800242static int shmem_reserve_inode(struct super_block *sb)
243{
244 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
245 if (sbinfo->max_inodes) {
246 spin_lock(&sbinfo->stat_lock);
247 if (!sbinfo->free_inodes) {
248 spin_unlock(&sbinfo->stat_lock);
249 return -ENOSPC;
250 }
251 sbinfo->free_inodes--;
252 spin_unlock(&sbinfo->stat_lock);
253 }
254 return 0;
255}
256
257static void shmem_free_inode(struct super_block *sb)
258{
259 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
260 if (sbinfo->max_inodes) {
261 spin_lock(&sbinfo->stat_lock);
262 sbinfo->free_inodes++;
263 spin_unlock(&sbinfo->stat_lock);
264 }
265}
266
Randy Dunlap46711812008-03-19 17:00:41 -0700267/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700268 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * @inode: inode to recalc
270 *
271 * We have to calculate the free blocks since the mm can drop
272 * undirtied hole pages behind our back.
273 *
274 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
275 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
276 *
277 * It has to be called with the spinlock held.
278 */
279static void shmem_recalc_inode(struct inode *inode)
280{
281 struct shmem_inode_info *info = SHMEM_I(inode);
282 long freed;
283
284 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
285 if (freed > 0) {
286 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700287 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700288 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290}
291
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700292bool shmem_charge(struct inode *inode, long pages)
293{
294 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700295 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700296
Mike Rapoport0f079692017-09-06 16:22:59 -0700297 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700298 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700299
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800300 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
301 inode->i_mapping->nrpages += pages;
302
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700303 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700304 info->alloced += pages;
305 inode->i_blocks += pages * BLOCKS_PER_PAGE;
306 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700307 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700308
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700309 return true;
310}
311
312void shmem_uncharge(struct inode *inode, long pages)
313{
314 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700315 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700316
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800317 /* nrpages adjustment done by __delete_from_page_cache() or caller */
318
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700319 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700320 info->alloced -= pages;
321 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
322 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700323 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700324
Mike Rapoport0f079692017-09-06 16:22:59 -0700325 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700326}
327
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700328/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500329 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700330 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500331static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700332 pgoff_t index, void *expected, void *replacement)
333{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500334 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700335 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700336
337 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700338 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500339 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700340 if (item != expected)
341 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500342 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700343 return 0;
344}
345
346/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700347 * Sometimes, before we decide whether to proceed or to fail, we must check
348 * that an entry was not already brought back from swap by a racing thread.
349 *
350 * Checking page is not enough: by the time a SwapCache page is locked, it
351 * might be reused, and again be SwapCache, using the same swap as before.
352 */
353static bool shmem_confirm_swap(struct address_space *mapping,
354 pgoff_t index, swp_entry_t swap)
355{
Matthew Wilcoxa12831b2017-11-22 08:34:58 -0500356 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700357}
358
359/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700360 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
361 *
362 * SHMEM_HUGE_NEVER:
363 * disables huge pages for the mount;
364 * SHMEM_HUGE_ALWAYS:
365 * enables huge pages for the mount;
366 * SHMEM_HUGE_WITHIN_SIZE:
367 * only allocate huge pages if the page will be fully within i_size,
368 * also respect fadvise()/madvise() hints;
369 * SHMEM_HUGE_ADVISE:
370 * only allocate huge pages if requested with fadvise()/madvise();
371 */
372
373#define SHMEM_HUGE_NEVER 0
374#define SHMEM_HUGE_ALWAYS 1
375#define SHMEM_HUGE_WITHIN_SIZE 2
376#define SHMEM_HUGE_ADVISE 3
377
378/*
379 * Special values.
380 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
381 *
382 * SHMEM_HUGE_DENY:
383 * disables huge on shm_mnt and all mounts, for emergency use;
384 * SHMEM_HUGE_FORCE:
385 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
386 *
387 */
388#define SHMEM_HUGE_DENY (-1)
389#define SHMEM_HUGE_FORCE (-2)
390
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700391#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700392/* ifdef here to avoid bloating shmem.o when not necessary */
393
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700394static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700395
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800396#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700397static int shmem_parse_huge(const char *str)
398{
399 if (!strcmp(str, "never"))
400 return SHMEM_HUGE_NEVER;
401 if (!strcmp(str, "always"))
402 return SHMEM_HUGE_ALWAYS;
403 if (!strcmp(str, "within_size"))
404 return SHMEM_HUGE_WITHIN_SIZE;
405 if (!strcmp(str, "advise"))
406 return SHMEM_HUGE_ADVISE;
407 if (!strcmp(str, "deny"))
408 return SHMEM_HUGE_DENY;
409 if (!strcmp(str, "force"))
410 return SHMEM_HUGE_FORCE;
411 return -EINVAL;
412}
413
414static const char *shmem_format_huge(int huge)
415{
416 switch (huge) {
417 case SHMEM_HUGE_NEVER:
418 return "never";
419 case SHMEM_HUGE_ALWAYS:
420 return "always";
421 case SHMEM_HUGE_WITHIN_SIZE:
422 return "within_size";
423 case SHMEM_HUGE_ADVISE:
424 return "advise";
425 case SHMEM_HUGE_DENY:
426 return "deny";
427 case SHMEM_HUGE_FORCE:
428 return "force";
429 default:
430 VM_BUG_ON(1);
431 return "bad_val";
432 }
433}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800434#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700435
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700436static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
437 struct shrink_control *sc, unsigned long nr_to_split)
438{
439 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800440 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700441 struct inode *inode;
442 struct shmem_inode_info *info;
443 struct page *page;
444 unsigned long batch = sc ? sc->nr_to_scan : 128;
445 int removed = 0, split = 0;
446
447 if (list_empty(&sbinfo->shrinklist))
448 return SHRINK_STOP;
449
450 spin_lock(&sbinfo->shrinklist_lock);
451 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
452 info = list_entry(pos, struct shmem_inode_info, shrinklist);
453
454 /* pin the inode */
455 inode = igrab(&info->vfs_inode);
456
457 /* inode is about to be evicted */
458 if (!inode) {
459 list_del_init(&info->shrinklist);
460 removed++;
461 goto next;
462 }
463
464 /* Check if there's anything to gain */
465 if (round_up(inode->i_size, PAGE_SIZE) ==
466 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800467 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700468 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700469 goto next;
470 }
471
472 list_move(&info->shrinklist, &list);
473next:
474 if (!--batch)
475 break;
476 }
477 spin_unlock(&sbinfo->shrinklist_lock);
478
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800479 list_for_each_safe(pos, next, &to_remove) {
480 info = list_entry(pos, struct shmem_inode_info, shrinklist);
481 inode = &info->vfs_inode;
482 list_del_init(&info->shrinklist);
483 iput(inode);
484 }
485
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700486 list_for_each_safe(pos, next, &list) {
487 int ret;
488
489 info = list_entry(pos, struct shmem_inode_info, shrinklist);
490 inode = &info->vfs_inode;
491
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700492 if (nr_to_split && split >= nr_to_split)
493 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700494
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700495 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700496 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
497 if (!page)
498 goto drop;
499
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700500 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700501 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700502 put_page(page);
503 goto drop;
504 }
505
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700506 /*
507 * Leave the inode on the list if we failed to lock
508 * the page at this time.
509 *
510 * Waiting for the lock may lead to deadlock in the
511 * reclaim path.
512 */
513 if (!trylock_page(page)) {
514 put_page(page);
515 goto leave;
516 }
517
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700518 ret = split_huge_page(page);
519 unlock_page(page);
520 put_page(page);
521
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700522 /* If split failed leave the inode on the list */
523 if (ret)
524 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700525
526 split++;
527drop:
528 list_del_init(&info->shrinklist);
529 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700530leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700531 iput(inode);
532 }
533
534 spin_lock(&sbinfo->shrinklist_lock);
535 list_splice_tail(&list, &sbinfo->shrinklist);
536 sbinfo->shrinklist_len -= removed;
537 spin_unlock(&sbinfo->shrinklist_lock);
538
539 return split;
540}
541
542static long shmem_unused_huge_scan(struct super_block *sb,
543 struct shrink_control *sc)
544{
545 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
546
547 if (!READ_ONCE(sbinfo->shrinklist_len))
548 return SHRINK_STOP;
549
550 return shmem_unused_huge_shrink(sbinfo, sc, 0);
551}
552
553static long shmem_unused_huge_count(struct super_block *sb,
554 struct shrink_control *sc)
555{
556 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
557 return READ_ONCE(sbinfo->shrinklist_len);
558}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700559#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700560
561#define shmem_huge SHMEM_HUGE_DENY
562
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700563static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
564 struct shrink_control *sc, unsigned long nr_to_split)
565{
566 return 0;
567}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700568#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700569
Yang Shi89fdcd22018-06-07 17:06:59 -0700570static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
571{
572 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
573 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
574 shmem_huge != SHMEM_HUGE_DENY)
575 return true;
576 return false;
577}
578
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700579/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700580 * Like add_to_page_cache_locked, but error if expected item has gone.
581 */
582static int shmem_add_to_page_cache(struct page *page,
583 struct address_space *mapping,
Matthew Wilcox552446a42017-12-01 13:25:14 -0500584 pgoff_t index, void *expected, gfp_t gfp)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700585{
Matthew Wilcox552446a42017-12-01 13:25:14 -0500586 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
587 unsigned long i = 0;
588 unsigned long nr = 1UL << compound_order(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700589
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700590 VM_BUG_ON_PAGE(PageTail(page), page);
591 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800592 VM_BUG_ON_PAGE(!PageLocked(page), page);
593 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700594 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700595
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700596 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700597 page->mapping = mapping;
598 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700599
Matthew Wilcox552446a42017-12-01 13:25:14 -0500600 do {
601 void *entry;
602 xas_lock_irq(&xas);
603 entry = xas_find_conflict(&xas);
604 if (entry != expected)
605 xas_set_err(&xas, -EEXIST);
606 xas_create_range(&xas);
607 if (xas_error(&xas))
608 goto unlock;
609next:
610 xas_store(&xas, page + i);
611 if (++i < nr) {
612 xas_next(&xas);
613 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700614 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500615 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700616 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700617 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500618 }
619 mapping->nrpages += nr;
Mel Gorman11fb9982016-07-28 15:46:20 -0700620 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
621 __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500622unlock:
623 xas_unlock_irq(&xas);
624 } while (xas_nomem(&xas, gfp));
625
626 if (xas_error(&xas)) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700627 page->mapping = NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700628 page_ref_sub(page, nr);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500629 return xas_error(&xas);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700630 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500631
632 return 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700633}
634
635/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700636 * Like delete_from_page_cache, but substitutes swap for page.
637 */
638static void shmem_delete_from_page_cache(struct page *page, void *radswap)
639{
640 struct address_space *mapping = page->mapping;
641 int error;
642
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700643 VM_BUG_ON_PAGE(PageCompound(page), page);
644
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700645 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500646 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700647 page->mapping = NULL;
648 mapping->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700649 __dec_node_page_state(page, NR_FILE_PAGES);
650 __dec_node_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700651 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300652 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700653 BUG_ON(error);
654}
655
656/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500657 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700658 */
659static int shmem_free_swap(struct address_space *mapping,
660 pgoff_t index, void *radswap)
661{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700662 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700663
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500664 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700665 if (old != radswap)
666 return -ENOENT;
667 free_swap_and_cache(radix_to_swp_entry(radswap));
668 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700669}
670
671/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800672 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800673 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800674 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700675 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800676 * as long as the inode doesn't go away and racy results are not a problem.
677 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800678unsigned long shmem_partial_swap_usage(struct address_space *mapping,
679 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800680{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500681 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800682 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800683 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800684
685 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500686 xas_for_each(&xas, page, end - 1) {
687 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700688 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400689 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800690 swapped++;
691
692 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500693 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800694 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800695 }
696 }
697
698 rcu_read_unlock();
699
700 return swapped << PAGE_SHIFT;
701}
702
703/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800704 * Determine (in bytes) how many of the shmem object's pages mapped by the
705 * given vma is swapped out.
706 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700707 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800708 * as long as the inode doesn't go away and racy results are not a problem.
709 */
710unsigned long shmem_swap_usage(struct vm_area_struct *vma)
711{
712 struct inode *inode = file_inode(vma->vm_file);
713 struct shmem_inode_info *info = SHMEM_I(inode);
714 struct address_space *mapping = inode->i_mapping;
715 unsigned long swapped;
716
717 /* Be careful as we don't hold info->lock */
718 swapped = READ_ONCE(info->swapped);
719
720 /*
721 * The easier cases are when the shmem object has nothing in swap, or
722 * the vma maps it whole. Then we can simply use the stats that we
723 * already track.
724 */
725 if (!swapped)
726 return 0;
727
728 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
729 return swapped << PAGE_SHIFT;
730
731 /* Here comes the more involved part */
732 return shmem_partial_swap_usage(mapping,
733 linear_page_index(vma, vma->vm_start),
734 linear_page_index(vma, vma->vm_end));
735}
736
737/*
Hugh Dickins24513262012-01-20 14:34:21 -0800738 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
739 */
740void shmem_unlock_mapping(struct address_space *mapping)
741{
742 struct pagevec pvec;
743 pgoff_t indices[PAGEVEC_SIZE];
744 pgoff_t index = 0;
745
Mel Gorman86679822017-11-15 17:37:52 -0800746 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800747 /*
748 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
749 */
750 while (!mapping_unevictable(mapping)) {
751 /*
752 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
753 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
754 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700755 pvec.nr = find_get_entries(mapping, index,
756 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800757 if (!pvec.nr)
758 break;
759 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700760 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800761 check_move_unevictable_pages(pvec.pages, pvec.nr);
762 pagevec_release(&pvec);
763 cond_resched();
764 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700765}
766
767/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500768 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700769 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700770 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700771static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
772 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700774 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300776 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
777 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
778 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
779 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700780 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700781 pgoff_t indices[PAGEVEC_SIZE];
782 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700783 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700784 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700786 if (lend == -1)
787 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700788
Mel Gorman86679822017-11-15 17:37:52 -0800789 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700790 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700791 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700792 pvec.nr = find_get_entries(mapping, index,
793 min(end - index, (pgoff_t)PAGEVEC_SIZE),
794 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700795 if (!pvec.nr)
796 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700797 for (i = 0; i < pagevec_count(&pvec); i++) {
798 struct page *page = pvec.pages[i];
799
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700800 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700801 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700802 break;
803
Matthew Wilcox3159f942017-11-03 13:30:42 -0400804 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700805 if (unfalloc)
806 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700807 nr_swaps_freed += !shmem_free_swap(mapping,
808 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700809 continue;
810 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700811
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700812 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
813
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700814 if (!trylock_page(page))
815 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700816
817 if (PageTransTail(page)) {
818 /* Middle of THP: zero out the page */
819 clear_highpage(page);
820 unlock_page(page);
821 continue;
822 } else if (PageTransHuge(page)) {
823 if (index == round_down(end, HPAGE_PMD_NR)) {
824 /*
825 * Range ends in the middle of THP:
826 * zero out the page
827 */
828 clear_highpage(page);
829 unlock_page(page);
830 continue;
831 }
832 index += HPAGE_PMD_NR - 1;
833 i += HPAGE_PMD_NR - 1;
834 }
835
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700836 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700837 VM_BUG_ON_PAGE(PageTail(page), page);
838 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800839 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700840 truncate_inode_page(mapping, page);
841 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700842 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700843 unlock_page(page);
844 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700845 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800846 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700847 cond_resched();
848 index++;
849 }
850
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700851 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700852 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700853 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700854 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300855 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700856 if (start > end) {
857 top = partial_end;
858 partial_end = 0;
859 }
860 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700861 set_page_dirty(page);
862 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300863 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700864 }
865 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700866 if (partial_end) {
867 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700868 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700869 if (page) {
870 zero_user_segment(page, 0, partial_end);
871 set_page_dirty(page);
872 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300873 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700874 }
875 }
876 if (start >= end)
877 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700878
879 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700880 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700881 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700882
883 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700884 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700885 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700886 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700887 /* If all gone or hole-punch or unfalloc, we're done */
888 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700889 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700890 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700891 index = start;
892 continue;
893 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700894 for (i = 0; i < pagevec_count(&pvec); i++) {
895 struct page *page = pvec.pages[i];
896
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700897 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700898 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700899 break;
900
Matthew Wilcox3159f942017-11-03 13:30:42 -0400901 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700902 if (unfalloc)
903 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700904 if (shmem_free_swap(mapping, index, page)) {
905 /* Swap was replaced by page: retry */
906 index--;
907 break;
908 }
909 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700910 continue;
911 }
912
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700913 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700914
915 if (PageTransTail(page)) {
916 /* Middle of THP: zero out the page */
917 clear_highpage(page);
918 unlock_page(page);
919 /*
920 * Partial thp truncate due 'start' in middle
921 * of THP: don't need to look on these pages
922 * again on !pvec.nr restart.
923 */
924 if (index != round_down(end, HPAGE_PMD_NR))
925 start++;
926 continue;
927 } else if (PageTransHuge(page)) {
928 if (index == round_down(end, HPAGE_PMD_NR)) {
929 /*
930 * Range ends in the middle of THP:
931 * zero out the page
932 */
933 clear_highpage(page);
934 unlock_page(page);
935 continue;
936 }
937 index += HPAGE_PMD_NR - 1;
938 i += HPAGE_PMD_NR - 1;
939 }
940
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700941 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700942 VM_BUG_ON_PAGE(PageTail(page), page);
943 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800944 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700945 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700946 } else {
947 /* Page was replaced by swap: retry */
948 unlock_page(page);
949 index--;
950 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700951 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700952 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700953 unlock_page(page);
954 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700955 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800956 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700957 index++;
958 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700959
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700960 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700961 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700963 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700966void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
967{
968 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700969 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700971EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
David Howellsa528d352017-01-31 16:46:22 +0000973static int shmem_getattr(const struct path *path, struct kstat *stat,
974 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -0700975{
David Howellsa528d352017-01-31 16:46:22 +0000976 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -0700977 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -0700978 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -0700979
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800980 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700981 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800982 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700983 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800984 }
Yu Zhao44a30222015-09-08 15:03:33 -0700985 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -0700986
987 if (is_huge_enabled(sb_info))
988 stat->blksize = HPAGE_PMD_SIZE;
989
Yu Zhao44a30222015-09-08 15:03:33 -0700990 return 0;
991}
992
Hugh Dickins94c1e622011-06-27 16:18:03 -0700993static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
David Howells75c3cfa2015-03-17 22:26:12 +0000995 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -0700996 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700997 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 int error;
999
Jan Kara31051c82016-05-26 16:55:18 +02001000 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001001 if (error)
1002 return error;
1003
Hugh Dickins94c1e622011-06-27 16:18:03 -07001004 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1005 loff_t oldsize = inode->i_size;
1006 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001007
David Herrmann40e041a2014-08-08 14:25:27 -07001008 /* protected by i_mutex */
1009 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1010 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1011 return -EPERM;
1012
Hugh Dickins94c1e622011-06-27 16:18:03 -07001013 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001014 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1015 oldsize, newsize);
1016 if (error)
1017 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001018 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001019 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001020 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001021 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001022 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001023 if (oldsize > holebegin)
1024 unmap_mapping_range(inode->i_mapping,
1025 holebegin, 0, 1);
1026 if (info->alloced)
1027 shmem_truncate_range(inode,
1028 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001029 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001030 if (oldsize > holebegin)
1031 unmap_mapping_range(inode->i_mapping,
1032 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001033
1034 /*
1035 * Part of the huge page can be beyond i_size: subject
1036 * to shrink under memory pressure.
1037 */
1038 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1039 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001040 /*
1041 * _careful to defend against unlocked access to
1042 * ->shrink_list in shmem_unused_huge_shrink()
1043 */
1044 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001045 list_add_tail(&info->shrinklist,
1046 &sbinfo->shrinklist);
1047 sbinfo->shrinklist_len++;
1048 }
1049 spin_unlock(&sbinfo->shrinklist_lock);
1050 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001054 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001055 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001056 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return error;
1058}
1059
Al Viro1f895f72010-06-05 19:10:41 -04001060static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001063 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001065 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 shmem_unacct_size(info->flags, inode->i_size);
1067 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001068 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001069 if (!list_empty(&info->shrinklist)) {
1070 spin_lock(&sbinfo->shrinklist_lock);
1071 if (!list_empty(&info->shrinklist)) {
1072 list_del_init(&info->shrinklist);
1073 sbinfo->shrinklist_len--;
1074 }
1075 spin_unlock(&sbinfo->shrinklist_lock);
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001078 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001080 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Al Viro3ed47db2016-01-22 18:08:52 -05001082 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001083
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001084 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001085 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001086 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001087 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001090static unsigned long find_swap_entry(struct xarray *xa, void *item)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001091{
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001092 XA_STATE(xas, xa, 0);
Matthew Wilcox478922e2016-12-14 15:08:52 -08001093 unsigned int checked = 0;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001094 void *entry;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001095
1096 rcu_read_lock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001097 xas_for_each(&xas, entry, ULONG_MAX) {
1098 if (xas_retry(&xas, entry))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001099 continue;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001100 if (entry == item)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001101 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001102 checked++;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001103 if ((checked % XA_CHECK_SCHED) != 0)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001104 continue;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001105 xas_pause(&xas);
Matthew Wilcox478922e2016-12-14 15:08:52 -08001106 cond_resched_rcu();
1107 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001108 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001109
1110 return entry ? xas.xa_index : -1;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001111}
1112
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001113/*
1114 * If swap found in inode, free it and move page from swapcache to filecache.
1115 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001116static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001117 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Hugh Dickins285b2c42011-08-03 16:21:20 -07001119 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001120 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001121 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001122 gfp_t gfp;
1123 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001125 radswap = swp_to_radix_entry(swap);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001126 index = find_swap_entry(&mapping->i_pages, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001127 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -07001128 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001129
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001130 /*
1131 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -04001132 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001133 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -07001134 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001135 */
1136 if (shmem_swaplist.next != &info->swaplist)
1137 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001138
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001139 gfp = mapping_gfp_mask(mapping);
1140 if (shmem_should_replace_page(*pagep, gfp)) {
1141 mutex_unlock(&shmem_swaplist_mutex);
1142 error = shmem_replace_page(pagep, gfp, info, index);
1143 mutex_lock(&shmem_swaplist_mutex);
1144 /*
1145 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -07001146 * allocation, but the inode might have been freed while we
1147 * dropped it: although a racing shmem_evict_inode() cannot
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001148 * complete without emptying the page cache, our page lock
Hugh Dickins0142ef62012-06-07 14:21:09 -07001149 * on this swapcache page is not enough to prevent that -
1150 * free_swap_and_cache() of our swap entry will only
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001151 * trylock_page(), removing swap from page cache whatever.
Hugh Dickins0142ef62012-06-07 14:21:09 -07001152 *
1153 * We must not proceed to shmem_add_to_page_cache() if the
1154 * inode has been freed, but of course we cannot rely on
1155 * inode or mapping or info to check that. However, we can
1156 * safely check if our swap entry is still in use (and here
1157 * it can't have got reused for another page): if it's still
1158 * in use, then the inode cannot have been freed yet, and we
1159 * can safely proceed (if it's no longer in use, that tells
1160 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001161 */
1162 if (!page_swapcount(*pagep))
1163 error = -ENOENT;
1164 }
1165
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001166 /*
Hugh Dickins778dd892011-05-11 15:13:37 -07001167 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1168 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1169 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001170 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001171 if (!error)
1172 error = shmem_add_to_page_cache(*pagep, mapping, index,
Matthew Wilcox552446a42017-12-01 13:25:14 -05001173 radswap, gfp);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001174 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001175 /*
1176 * Truncation and eviction use free_swap_and_cache(), which
1177 * only does trylock page: if we raced, best clean up here.
1178 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001179 delete_from_swap_cache(*pagep);
1180 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001181 if (!error) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001182 spin_lock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001183 info->swapped--;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001184 spin_unlock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001185 swap_free(swap);
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001188 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001192 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001194int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001196 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001198 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001199 int error = 0;
1200
1201 /*
1202 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001203 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001204 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001205 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001206 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001207
1208 /*
1209 * Charge page using GFP_KERNEL while we can wait, before taking
1210 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1211 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001212 */
Tejun Heo2cf85582018-07-03 11:14:56 -04001213 error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL,
1214 &memcg, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001215 if (error)
1216 goto out;
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001217 /* No memory allocation: swap entry occupies the slot for the page */
Johannes Weiner00501b52014-08-08 14:19:20 -07001218 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001220 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001221 list_for_each_safe(this, next, &shmem_swaplist) {
1222 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001223 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001224 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001225 else
1226 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001227 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001228 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001229 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001230 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001232 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001233
Johannes Weiner00501b52014-08-08 14:19:20 -07001234 if (error) {
1235 if (error != -ENOMEM)
1236 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001237 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001238 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001239 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001240out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001241 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001242 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001243 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246/*
1247 * Move the page from the page cache to the swap cache.
1248 */
1249static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1250{
1251 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001254 swp_entry_t swap;
1255 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001257 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 mapping = page->mapping;
1260 index = page->index;
1261 inode = mapping->host;
1262 info = SHMEM_I(inode);
1263 if (info->flags & VM_LOCKED)
1264 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001265 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 goto redirty;
1267
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001268 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001269 * Our capabilities prevent regular writeback or sync from ever calling
1270 * shmem_writepage; but a stacking filesystem might use ->writepage of
1271 * its underlying filesystem, in which case tmpfs should write out to
1272 * swap only in response to memory pressure, and not for the writeback
1273 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001274 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001275 if (!wbc->for_reclaim) {
1276 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1277 goto redirty;
1278 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001279
1280 /*
1281 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1282 * value into swapfile.c, the only way we can correctly account for a
1283 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001284 *
1285 * That's okay for a page already fallocated earlier, but if we have
1286 * not yet completed the fallocation, then (a) we want to keep track
1287 * of this page in case we have to undo it, and (b) it may not be a
1288 * good idea to continue anyway, once we're pushing into swap. So
1289 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001290 */
1291 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001292 if (inode->i_private) {
1293 struct shmem_falloc *shmem_falloc;
1294 spin_lock(&inode->i_lock);
1295 shmem_falloc = inode->i_private;
1296 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001297 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001298 index >= shmem_falloc->start &&
1299 index < shmem_falloc->next)
1300 shmem_falloc->nr_unswapped++;
1301 else
1302 shmem_falloc = NULL;
1303 spin_unlock(&inode->i_lock);
1304 if (shmem_falloc)
1305 goto redirty;
1306 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001307 clear_highpage(page);
1308 flush_dcache_page(page);
1309 SetPageUptodate(page);
1310 }
1311
Huang Ying38d8b4e2017-07-06 15:37:18 -07001312 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001313 if (!swap.val)
1314 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001315
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001316 /*
1317 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001318 * if it's not already there. Do it now before the page is
1319 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001320 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001321 * we've incremented swapped, because shmem_unuse_inode() will
1322 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001323 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001324 mutex_lock(&shmem_swaplist_mutex);
1325 if (list_empty(&info->swaplist))
1326 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001327
Hugh Dickins48f170f2011-07-25 17:12:37 -07001328 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001329 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001330 shmem_recalc_inode(inode);
1331 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001332 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001333
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001334 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001335 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1336
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001337 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001338 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001339 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
1341 }
1342
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001343 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001344 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345redirty:
1346 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001347 if (wbc->for_reclaim)
1348 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1349 unlock_page(page);
1350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Hugh Dickins75edd342016-05-19 17:12:44 -07001353#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001354static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001355{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001356 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001357
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001358 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001359 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001360
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001361 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001362
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001363 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001364}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001365
1366static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1367{
1368 struct mempolicy *mpol = NULL;
1369 if (sbinfo->mpol) {
1370 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1371 mpol = sbinfo->mpol;
1372 mpol_get(mpol);
1373 spin_unlock(&sbinfo->stat_lock);
1374 }
1375 return mpol;
1376}
Hugh Dickins75edd342016-05-19 17:12:44 -07001377#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1378static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1379{
1380}
1381static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1382{
1383 return NULL;
1384}
1385#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1386#ifndef CONFIG_NUMA
1387#define vm_policy vm_private_data
1388#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001389
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001390static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1391 struct shmem_inode_info *info, pgoff_t index)
1392{
1393 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001394 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001395 /* Bias interleave by inode number to distribute better across nodes */
1396 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001397 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1398}
1399
1400static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1401{
1402 /* Drop reference taken by mpol_shared_policy_lookup() */
1403 mpol_cond_put(vma->vm_policy);
1404}
1405
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001406static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1407 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001410 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001411 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001413 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001414 vmf.vma = &pvma;
1415 vmf.address = 0;
1416 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001417 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001418
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001419 return page;
1420}
Mel Gorman18a2f372012-12-05 14:01:41 -08001421
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001422static struct page *shmem_alloc_hugepage(gfp_t gfp,
1423 struct shmem_inode_info *info, pgoff_t index)
1424{
1425 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001426 struct address_space *mapping = info->vfs_inode.i_mapping;
1427 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001428 struct page *page;
1429
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001430 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001431 return NULL;
1432
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001433 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001434 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1435 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001436 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001437
1438 shmem_pseudo_vma_init(&pvma, info, hindex);
1439 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
Michal Hocko89c83fb2018-11-02 15:48:31 -07001440 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id());
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001441 shmem_pseudo_vma_destroy(&pvma);
1442 if (page)
1443 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001444 return page;
1445}
1446
1447static struct page *shmem_alloc_page(gfp_t gfp,
1448 struct shmem_inode_info *info, pgoff_t index)
1449{
1450 struct vm_area_struct pvma;
1451 struct page *page;
1452
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001453 shmem_pseudo_vma_init(&pvma, info, index);
1454 page = alloc_page_vma(gfp, &pvma, 0);
1455 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001456
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001457 return page;
1458}
1459
1460static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001461 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001462 pgoff_t index, bool huge)
1463{
Mike Rapoport0f079692017-09-06 16:22:59 -07001464 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001465 struct page *page;
1466 int nr;
1467 int err = -ENOSPC;
1468
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001469 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001470 huge = false;
1471 nr = huge ? HPAGE_PMD_NR : 1;
1472
Mike Rapoport0f079692017-09-06 16:22:59 -07001473 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001474 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001475
1476 if (huge)
1477 page = shmem_alloc_hugepage(gfp, info, index);
1478 else
1479 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001480 if (page) {
1481 __SetPageLocked(page);
1482 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001483 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001484 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001485
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001486 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001487 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001488failed:
1489 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001493 * When a page is moved from swapcache to shmem filecache (either by the
1494 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1495 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1496 * ignorance of the mapping it belongs to. If that mapping has special
1497 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1498 * we may need to copy to a suitable page before moving to filecache.
1499 *
1500 * In a future release, this may well be extended to respect cpuset and
1501 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1502 * but for now it is a simple matter of zone.
1503 */
1504static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1505{
1506 return page_zonenum(page) > gfp_zone(gfp);
1507}
1508
1509static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1510 struct shmem_inode_info *info, pgoff_t index)
1511{
1512 struct page *oldpage, *newpage;
1513 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001514 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001515 pgoff_t swap_index;
1516 int error;
1517
1518 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001519 entry.val = page_private(oldpage);
1520 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001521 swap_mapping = page_mapping(oldpage);
1522
1523 /*
1524 * We have arrived here because our zones are constrained, so don't
1525 * limit chance of success by further cpuset and node constraints.
1526 */
1527 gfp &= ~GFP_CONSTRAINT_MASK;
1528 newpage = shmem_alloc_page(gfp, info, index);
1529 if (!newpage)
1530 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001531
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001532 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001533 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001534 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001535
Hugh Dickins9956edf2016-11-10 10:46:11 -08001536 __SetPageLocked(newpage);
1537 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001538 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001539 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001540 SetPageSwapCache(newpage);
1541
1542 /*
1543 * Our caller will very soon move newpage out of swapcache, but it's
1544 * a nice clean interface for us to replace oldpage by newpage there.
1545 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001546 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001547 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001548 if (!error) {
Mel Gorman11fb9982016-07-28 15:46:20 -07001549 __inc_node_page_state(newpage, NR_FILE_PAGES);
1550 __dec_node_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001551 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001552 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001553
Hugh Dickins0142ef62012-06-07 14:21:09 -07001554 if (unlikely(error)) {
1555 /*
1556 * Is this possible? I think not, now that our callers check
1557 * both PageSwapCache and page_private after getting page lock;
1558 * but be defensive. Reverse old to newpage for clear and free.
1559 */
1560 oldpage = newpage;
1561 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001562 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001563 lru_cache_add_anon(newpage);
1564 *pagep = newpage;
1565 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001566
1567 ClearPageSwapCache(oldpage);
1568 set_page_private(oldpage, 0);
1569
1570 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001571 put_page(oldpage);
1572 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001573 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001574}
1575
1576/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001577 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 *
1579 * If we allocate a new one we do not mark it dirty. That's up to the
1580 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001581 * entry since a page cannot live in both the swap and page cache.
1582 *
1583 * fault_mm and fault_type are only supplied by shmem_fault:
1584 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001586static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001587 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001588 struct vm_area_struct *vma, struct vm_fault *vmf,
1589 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001592 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001594 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001595 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001596 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 swp_entry_t swap;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001598 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001599 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001601 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001602 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001604 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001606 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1607 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001609 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001610 page = find_lock_entry(mapping, index);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001611 if (xa_is_value(page)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001612 swap = radix_to_swp_entry(page);
1613 page = NULL;
1614 }
1615
Hugh Dickins75edd342016-05-19 17:12:44 -07001616 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001617 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001618 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001619 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001620 }
1621
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001622 if (page && sgp == SGP_WRITE)
1623 mark_page_accessed(page);
1624
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001625 /* fallocated page? */
1626 if (page && !PageUptodate(page)) {
1627 if (sgp != SGP_READ)
1628 goto clear;
1629 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001630 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001631 page = NULL;
1632 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001633 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001634 *pagep = page;
1635 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001639 * Fast cache lookup did not find it:
1640 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001642 sbinfo = SHMEM_SB(inode->i_sb);
Mike Rapoportcfda0522017-02-22 15:43:37 -08001643 charge_mm = vma ? vma->vm_mm : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (swap.val) {
1646 /* Look it up and read it in.. */
Huang Yingec560172017-09-06 16:24:36 -07001647 page = lookup_swap_cache(swap, NULL, 0);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001648 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001649 /* Or update major stats only when swapin succeeds?? */
1650 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001651 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001652 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07001653 count_memcg_event_mm(charge_mm, PGMAJFAULT);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001654 }
1655 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001656 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001657 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001658 error = -ENOMEM;
1659 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
1663 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001664 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001665 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001666 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001667 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001668 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001669 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001670 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001672 goto failed;
1673 }
1674 wait_on_page_writeback(page);
1675
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001676 if (shmem_should_replace_page(page, gfp)) {
1677 error = shmem_replace_page(&page, gfp, info, index);
1678 if (error)
1679 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
Tejun Heo2cf85582018-07-03 11:14:56 -04001682 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001683 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001684 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001685 error = shmem_add_to_page_cache(page, mapping, index,
Matthew Wilcox552446a42017-12-01 13:25:14 -05001686 swp_to_radix_entry(swap), gfp);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001687 /*
1688 * We already confirmed swap under page lock, and make
1689 * no memory allocation here, so usually no possibility
1690 * of error; but free_swap_and_cache() only trylocks a
1691 * page, so it is just possible that the entry has been
1692 * truncated or holepunched since swap was confirmed.
1693 * shmem_undo_range() will have done some of the
1694 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001695 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001696 * Reset swap.val? No, leave it so "failed" goes back to
1697 * "repeat": reading a hole and writing should succeed.
1698 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001699 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001700 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001701 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001702 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001703 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001704 if (error)
1705 goto failed;
1706
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001707 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001708
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001709 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001710 info->swapped--;
1711 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001712 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001713
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001714 if (sgp == SGP_WRITE)
1715 mark_page_accessed(page);
1716
Hugh Dickins27ab7002011-07-25 17:12:36 -07001717 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001718 set_page_dirty(page);
1719 swap_free(swap);
1720
Hugh Dickins54af60422011-08-03 16:21:24 -07001721 } else {
Mike Rapoportcfda0522017-02-22 15:43:37 -08001722 if (vma && userfaultfd_missing(vma)) {
1723 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1724 return 0;
1725 }
1726
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001727 /* shmem_symlink() */
1728 if (mapping->a_ops != &shmem_aops)
1729 goto alloc_nohuge;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001730 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001731 goto alloc_nohuge;
1732 if (shmem_huge == SHMEM_HUGE_FORCE)
1733 goto alloc_huge;
1734 switch (sbinfo->huge) {
1735 loff_t i_size;
1736 pgoff_t off;
1737 case SHMEM_HUGE_NEVER:
1738 goto alloc_nohuge;
1739 case SHMEM_HUGE_WITHIN_SIZE:
1740 off = round_up(index, HPAGE_PMD_NR);
1741 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1742 if (i_size >= HPAGE_PMD_SIZE &&
1743 i_size >> PAGE_SHIFT >= off)
1744 goto alloc_huge;
1745 /* fallthrough */
1746 case SHMEM_HUGE_ADVISE:
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001747 if (sgp_huge == SGP_HUGE)
1748 goto alloc_huge;
1749 /* TODO: implement fadvise() hints */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001750 goto alloc_nohuge;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001753alloc_huge:
Mike Rapoport0f079692017-09-06 16:22:59 -07001754 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001755 if (IS_ERR(page)) {
Mike Rapoport0f079692017-09-06 16:22:59 -07001756alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001757 index, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001758 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001759 if (IS_ERR(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001760 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001761 error = PTR_ERR(page);
1762 page = NULL;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001763 if (error != -ENOSPC)
1764 goto failed;
1765 /*
1766 * Try to reclaim some spece by splitting a huge page
1767 * beyond i_size on the filesystem.
1768 */
1769 while (retry--) {
1770 int ret;
1771 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1772 if (ret == SHRINK_STOP)
1773 break;
1774 if (ret)
1775 goto alloc_nohuge;
1776 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001777 goto failed;
1778 }
1779
1780 if (PageTransHuge(page))
1781 hindex = round_down(index, HPAGE_PMD_NR);
1782 else
1783 hindex = index;
1784
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001785 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001786 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001787
Tejun Heo2cf85582018-07-03 11:14:56 -04001788 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001789 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001790 if (error)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001791 goto unacct;
Matthew Wilcox552446a42017-12-01 13:25:14 -05001792 error = shmem_add_to_page_cache(page, mapping, hindex,
1793 NULL, gfp & GFP_RECLAIM_MASK);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001794 if (error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001795 mem_cgroup_cancel_charge(page, memcg,
1796 PageTransHuge(page));
1797 goto unacct;
Hugh Dickinsb065b432012-07-11 14:02:48 -07001798 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001799 mem_cgroup_commit_charge(page, memcg, false,
1800 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001801 lru_cache_add_anon(page);
1802
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001803 spin_lock_irq(&info->lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001804 info->alloced += 1 << compound_order(page);
1805 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001806 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001807 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001808 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001809
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001810 if (PageTransHuge(page) &&
1811 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1812 hindex + HPAGE_PMD_NR - 1) {
1813 /*
1814 * Part of the huge page is beyond i_size: subject
1815 * to shrink under memory pressure.
1816 */
1817 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001818 /*
1819 * _careful to defend against unlocked access to
1820 * ->shrink_list in shmem_unused_huge_shrink()
1821 */
1822 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001823 list_add_tail(&info->shrinklist,
1824 &sbinfo->shrinklist);
1825 sbinfo->shrinklist_len++;
1826 }
1827 spin_unlock(&sbinfo->shrinklist_lock);
1828 }
1829
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001830 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001831 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1832 */
1833 if (sgp == SGP_FALLOC)
1834 sgp = SGP_WRITE;
1835clear:
1836 /*
1837 * Let SGP_WRITE caller clear ends if write does not fill page;
1838 * but SGP_FALLOC on a page fallocated earlier must initialize
1839 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001840 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001841 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1842 struct page *head = compound_head(page);
1843 int i;
1844
1845 for (i = 0; i < (1 << compound_order(head)); i++) {
1846 clear_highpage(head + i);
1847 flush_dcache_page(head + i);
1848 }
1849 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001850 }
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001851 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001852
Hugh Dickins54af60422011-08-03 16:21:24 -07001853 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001854 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001855 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001856 if (alloced) {
1857 ClearPageDirty(page);
1858 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001859 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001860 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001861 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001862 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001863 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001864 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001865 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001866 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001867 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001868
Nick Piggind0217ac2007-07-19 01:47:03 -07001869 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001870 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001872unacct:
Mike Rapoport0f079692017-09-06 16:22:59 -07001873 shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001874
1875 if (PageTransHuge(page)) {
1876 unlock_page(page);
1877 put_page(page);
1878 goto alloc_nohuge;
1879 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001880failed:
Hugh Dickins267a4c72015-12-11 13:40:55 -08001881 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
Hugh Dickinsd1899222012-07-11 14:02:47 -07001882 error = -EEXIST;
1883unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001884 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001885 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001886 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001887 }
1888 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001889 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001890 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001891 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001892 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001893 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001894 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07001895 goto repeat;
1896 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001899/*
1900 * This is like autoremove_wake_function, but it removes the wait queue
1901 * entry unconditionally - even if something else had already woken the
1902 * target.
1903 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001904static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001905{
1906 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001907 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001908 return ret;
1909}
1910
Souptick Joarder20acce62018-06-07 17:09:17 -07001911static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Dave Jiang11bac802017-02-24 14:56:41 -08001913 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001914 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001915 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001916 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07001917 int err;
1918 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001920 /*
1921 * Trinity finds that probing a hole which tmpfs is punching can
1922 * prevent the hole-punch from ever completing: which in turn
1923 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001924 * faulting pages into the hole while it's being punched. Although
1925 * shmem_undo_range() does remove the additions, it may be unable to
1926 * keep up, as each new page needs its own unmap_mapping_range() call,
1927 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1928 *
1929 * It does not matter if we sometimes reach this check just before the
1930 * hole-punch begins, so that one fault then races with the punch:
1931 * we just need to make racing faults a rare case.
1932 *
1933 * The implementation below would be much simpler if we just used a
1934 * standard mutex or completion: but we cannot take i_mutex in fault,
1935 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001936 */
1937 if (unlikely(inode->i_private)) {
1938 struct shmem_falloc *shmem_falloc;
1939
1940 spin_lock(&inode->i_lock);
1941 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001942 if (shmem_falloc &&
1943 shmem_falloc->waitq &&
1944 vmf->pgoff >= shmem_falloc->start &&
1945 vmf->pgoff < shmem_falloc->next) {
1946 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001947 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001948
1949 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001950 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1951 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001952 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001953 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001954 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001955 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001956
1957 shmem_falloc_waitq = shmem_falloc->waitq;
1958 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1959 TASK_UNINTERRUPTIBLE);
1960 spin_unlock(&inode->i_lock);
1961 schedule();
1962
1963 /*
1964 * shmem_falloc_waitq points into the shmem_fallocate()
1965 * stack of the hole-punching task: shmem_falloc_waitq
1966 * is usually invalid by the time we reach here, but
1967 * finish_wait() does not dereference it in that case;
1968 * though i_lock needed lest racing with wake_up_all().
1969 */
1970 spin_lock(&inode->i_lock);
1971 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1972 spin_unlock(&inode->i_lock);
1973 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001974 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001975 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001976 }
1977
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001978 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07001979
1980 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
1981 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001982 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07001983 else if (vma->vm_flags & VM_HUGEPAGE)
1984 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001985
Souptick Joarder20acce62018-06-07 17:09:17 -07001986 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08001987 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07001988 if (err)
1989 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001990 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07001993unsigned long shmem_get_unmapped_area(struct file *file,
1994 unsigned long uaddr, unsigned long len,
1995 unsigned long pgoff, unsigned long flags)
1996{
1997 unsigned long (*get_area)(struct file *,
1998 unsigned long, unsigned long, unsigned long, unsigned long);
1999 unsigned long addr;
2000 unsigned long offset;
2001 unsigned long inflated_len;
2002 unsigned long inflated_addr;
2003 unsigned long inflated_offset;
2004
2005 if (len > TASK_SIZE)
2006 return -ENOMEM;
2007
2008 get_area = current->mm->get_unmapped_area;
2009 addr = get_area(file, uaddr, len, pgoff, flags);
2010
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002011 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002012 return addr;
2013 if (IS_ERR_VALUE(addr))
2014 return addr;
2015 if (addr & ~PAGE_MASK)
2016 return addr;
2017 if (addr > TASK_SIZE - len)
2018 return addr;
2019
2020 if (shmem_huge == SHMEM_HUGE_DENY)
2021 return addr;
2022 if (len < HPAGE_PMD_SIZE)
2023 return addr;
2024 if (flags & MAP_FIXED)
2025 return addr;
2026 /*
2027 * Our priority is to support MAP_SHARED mapped hugely;
2028 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
2029 * But if caller specified an address hint, respect that as before.
2030 */
2031 if (uaddr)
2032 return addr;
2033
2034 if (shmem_huge != SHMEM_HUGE_FORCE) {
2035 struct super_block *sb;
2036
2037 if (file) {
2038 VM_BUG_ON(file->f_op != &shmem_file_operations);
2039 sb = file_inode(file)->i_sb;
2040 } else {
2041 /*
2042 * Called directly from mm/mmap.c, or drivers/char/mem.c
2043 * for "/dev/zero", to create a shared anonymous object.
2044 */
2045 if (IS_ERR(shm_mnt))
2046 return addr;
2047 sb = shm_mnt->mnt_sb;
2048 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002049 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002050 return addr;
2051 }
2052
2053 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2054 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2055 return addr;
2056 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2057 return addr;
2058
2059 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2060 if (inflated_len > TASK_SIZE)
2061 return addr;
2062 if (inflated_len < len)
2063 return addr;
2064
2065 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
2066 if (IS_ERR_VALUE(inflated_addr))
2067 return addr;
2068 if (inflated_addr & ~PAGE_MASK)
2069 return addr;
2070
2071 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2072 inflated_addr += offset - inflated_offset;
2073 if (inflated_offset > offset)
2074 inflated_addr += HPAGE_PMD_SIZE;
2075
2076 if (inflated_addr > TASK_SIZE - len)
2077 return addr;
2078 return inflated_addr;
2079}
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002082static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Al Viro496ad9a2013-01-23 17:07:38 -05002084 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002085 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002088static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2089 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Al Viro496ad9a2013-01-23 17:07:38 -05002091 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002092 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002094 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2095 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097#endif
2098
2099int shmem_lock(struct file *file, int lock, struct user_struct *user)
2100{
Al Viro496ad9a2013-01-23 17:07:38 -05002101 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct shmem_inode_info *info = SHMEM_I(inode);
2103 int retval = -ENOMEM;
2104
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002105 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (lock && !(info->flags & VM_LOCKED)) {
2107 if (!user_shm_lock(inode->i_size, user))
2108 goto out_nomem;
2109 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002110 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112 if (!lock && (info->flags & VM_LOCKED) && user) {
2113 user_shm_unlock(inode->i_size, user);
2114 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002115 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119out_nomem:
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002120 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return retval;
2122}
2123
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002124static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 file_accessed(file);
2127 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002128 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002129 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2130 (vma->vm_end & HPAGE_PMD_MASK)) {
2131 khugepaged_enter(vma, vma->vm_flags);
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return 0;
2134}
2135
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002136static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002137 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct inode *inode;
2140 struct shmem_inode_info *info;
2141 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2142
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002143 if (shmem_reserve_inode(sb))
2144 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 inode = new_inode(sb);
2147 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002148 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002149 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002151 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002152 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 info = SHMEM_I(inode);
2154 memset(info, 0, (char *)inode - (char *)info);
2155 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07002156 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002157 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002158 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002160 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002161 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 switch (mode & S_IFMT) {
2164 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002165 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 init_special_inode(inode, mode, dev);
2167 break;
2168 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002169 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 inode->i_op = &shmem_inode_operations;
2171 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002172 mpol_shared_policy_init(&info->policy,
2173 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002176 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /* Some things misbehave if size == 0 on a directory */
2178 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2179 inode->i_op = &shmem_dir_inode_operations;
2180 inode->i_fop = &simple_dir_operations;
2181 break;
2182 case S_IFLNK:
2183 /*
2184 * Must not load anything in the rbtree,
2185 * mpol_free_shared_policy will not be called.
2186 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002187 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 break;
2189 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002190
2191 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002192 } else
2193 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return inode;
2195}
2196
Johannes Weiner0cd61442014-04-03 14:47:46 -07002197bool shmem_mapping(struct address_space *mapping)
2198{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002199 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002200}
2201
Mike Rapoport8d103962017-09-06 16:23:02 -07002202static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2203 pmd_t *dst_pmd,
2204 struct vm_area_struct *dst_vma,
2205 unsigned long dst_addr,
2206 unsigned long src_addr,
2207 bool zeropage,
2208 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002209{
2210 struct inode *inode = file_inode(dst_vma->vm_file);
2211 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002212 struct address_space *mapping = inode->i_mapping;
2213 gfp_t gfp = mapping_gfp_mask(mapping);
2214 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2215 struct mem_cgroup *memcg;
2216 spinlock_t *ptl;
2217 void *page_kaddr;
2218 struct page *page;
2219 pte_t _dst_pte, *dst_pte;
2220 int ret;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002221 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002222
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002223 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002224 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002225 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002226
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002227 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002228 page = shmem_alloc_page(gfp, info, pgoff);
2229 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002230 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002231
Mike Rapoport8d103962017-09-06 16:23:02 -07002232 if (!zeropage) { /* mcopy_atomic */
2233 page_kaddr = kmap_atomic(page);
2234 ret = copy_from_user(page_kaddr,
2235 (const void __user *)src_addr,
2236 PAGE_SIZE);
2237 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002238
Mike Rapoport8d103962017-09-06 16:23:02 -07002239 /* fallback to copy_from_user outside mmap_sem */
2240 if (unlikely(ret)) {
2241 *pagep = page;
2242 shmem_inode_unacct_blocks(inode, 1);
2243 /* don't free the page */
Andrea Arcangeli9e368252018-11-30 14:09:25 -08002244 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002245 }
2246 } else { /* mfill_zeropage_atomic */
2247 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002248 }
2249 } else {
2250 page = *pagep;
2251 *pagep = NULL;
2252 }
2253
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002254 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2255 __SetPageLocked(page);
2256 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002257 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002258
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002259 ret = -EFAULT;
2260 offset = linear_page_index(dst_vma, dst_addr);
2261 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2262 if (unlikely(offset >= max_off))
2263 goto out_release;
2264
Tejun Heo2cf85582018-07-03 11:14:56 -04002265 ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002266 if (ret)
2267 goto out_release;
2268
Matthew Wilcox552446a42017-12-01 13:25:14 -05002269 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2270 gfp & GFP_RECLAIM_MASK);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002271 if (ret)
2272 goto out_release_uncharge;
2273
2274 mem_cgroup_commit_charge(page, memcg, false, false);
2275
2276 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2277 if (dst_vma->vm_flags & VM_WRITE)
2278 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002279 else {
2280 /*
2281 * We don't set the pte dirty if the vma has no
2282 * VM_WRITE permission, so mark the page dirty or it
2283 * could be freed from under us. We could do it
2284 * unconditionally before unlock_page(), but doing it
2285 * only if VM_WRITE is not set is faster.
2286 */
2287 set_page_dirty(page);
2288 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002289
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002290 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002291
2292 ret = -EFAULT;
2293 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2294 if (unlikely(offset >= max_off))
2295 goto out_release_uncharge_unlock;
2296
2297 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002298 if (!pte_none(*dst_pte))
2299 goto out_release_uncharge_unlock;
2300
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002301 lru_cache_add_anon(page);
2302
2303 spin_lock(&info->lock);
2304 info->alloced++;
2305 inode->i_blocks += BLOCKS_PER_PAGE;
2306 shmem_recalc_inode(inode);
2307 spin_unlock(&info->lock);
2308
2309 inc_mm_counter(dst_mm, mm_counter_file(page));
2310 page_add_file_rmap(page, false);
2311 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2312
2313 /* No need to invalidate - it was non-present before */
2314 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002315 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002316 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002317 ret = 0;
2318out:
2319 return ret;
2320out_release_uncharge_unlock:
2321 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002322 ClearPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002323 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002324out_release_uncharge:
2325 mem_cgroup_cancel_charge(page, memcg, false);
2326out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002327 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002328 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002329out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002330 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002331 goto out;
2332}
2333
Mike Rapoport8d103962017-09-06 16:23:02 -07002334int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2335 pmd_t *dst_pmd,
2336 struct vm_area_struct *dst_vma,
2337 unsigned long dst_addr,
2338 unsigned long src_addr,
2339 struct page **pagep)
2340{
2341 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2342 dst_addr, src_addr, false, pagep);
2343}
2344
2345int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2346 pmd_t *dst_pmd,
2347 struct vm_area_struct *dst_vma,
2348 unsigned long dst_addr)
2349{
2350 struct page *page = NULL;
2351
2352 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2353 dst_addr, 0, true, &page);
2354}
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002357static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002358static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002360#ifdef CONFIG_TMPFS_XATTR
2361static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2362#else
2363#define shmem_initxattrs NULL
2364#endif
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002367shmem_write_begin(struct file *file, struct address_space *mapping,
2368 loff_t pos, unsigned len, unsigned flags,
2369 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Nick Piggin800d15a2007-10-16 01:25:03 -07002371 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002372 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002373 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002374
2375 /* i_mutex is held by caller */
Steven Rostedt (VMware)3f472cc2017-02-24 14:59:10 -08002376 if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
David Herrmann40e041a2014-08-08 14:25:27 -07002377 if (info->seals & F_SEAL_WRITE)
2378 return -EPERM;
2379 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2380 return -EPERM;
2381 }
2382
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002383 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002384}
2385
2386static int
2387shmem_write_end(struct file *file, struct address_space *mapping,
2388 loff_t pos, unsigned len, unsigned copied,
2389 struct page *page, void *fsdata)
2390{
2391 struct inode *inode = mapping->host;
2392
Hugh Dickinsd3602442008-02-04 22:28:44 -08002393 if (pos + copied > inode->i_size)
2394 i_size_write(inode, pos + copied);
2395
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002396 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002397 struct page *head = compound_head(page);
2398 if (PageTransCompound(page)) {
2399 int i;
2400
2401 for (i = 0; i < HPAGE_PMD_NR; i++) {
2402 if (head + i == page)
2403 continue;
2404 clear_highpage(head + i);
2405 flush_dcache_page(head + i);
2406 }
2407 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002408 if (copied < PAGE_SIZE) {
2409 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002410 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002411 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002412 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002413 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002414 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002415 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002416 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002417 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002418
Nick Piggin800d15a2007-10-16 01:25:03 -07002419 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Al Viro2ba5bbe2014-04-02 20:00:02 -04002422static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Al Viro6e58e792014-02-03 17:07:03 -05002424 struct file *file = iocb->ki_filp;
2425 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002427 pgoff_t index;
2428 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002429 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002430 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002431 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002432 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002433
2434 /*
2435 * Might this read be for a stacking filesystem? Then when reading
2436 * holes of a sparse file, we actually need to allocate those pages,
2437 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2438 */
Al Viro777eda22014-12-17 04:46:46 -05002439 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002440 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002442 index = *ppos >> PAGE_SHIFT;
2443 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 for (;;) {
2446 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002447 pgoff_t end_index;
2448 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 loff_t i_size = i_size_read(inode);
2450
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002451 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (index > end_index)
2453 break;
2454 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002455 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (nr <= offset)
2457 break;
2458 }
2459
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002460 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002461 if (error) {
2462 if (error == -EINVAL)
2463 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 break;
2465 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002466 if (page) {
2467 if (sgp == SGP_CACHE)
2468 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002469 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 /*
2473 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002474 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002476 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002478 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002480 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (nr <= offset) {
2482 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002483 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 break;
2485 }
2486 }
2487 nr -= offset;
2488
2489 if (page) {
2490 /*
2491 * If users can be writing to this page using arbitrary
2492 * virtual addresses, take care about potential aliasing
2493 * before reading the page on the kernel side.
2494 */
2495 if (mapping_writably_mapped(mapping))
2496 flush_dcache_page(page);
2497 /*
2498 * Mark the page accessed if we read the beginning.
2499 */
2500 if (!offset)
2501 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002502 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002504 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 /*
2508 * Ok, we have the page, and it's up-to-date, so
2509 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002511 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002512 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002514 index += offset >> PAGE_SHIFT;
2515 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002517 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002518 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 break;
Al Viro6e58e792014-02-03 17:07:03 -05002520 if (ret < nr) {
2521 error = -EFAULT;
2522 break;
2523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 cond_resched();
2525 }
2526
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002527 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002528 file_accessed(file);
2529 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002532/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002533 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002534 */
2535static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002536 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002537{
2538 struct page *page;
2539 struct pagevec pvec;
2540 pgoff_t indices[PAGEVEC_SIZE];
2541 bool done = false;
2542 int i;
2543
Mel Gorman86679822017-11-15 17:37:52 -08002544 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002545 pvec.nr = 1; /* start small: we may be there already */
2546 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002547 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002548 pvec.nr, pvec.pages, indices);
2549 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002550 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002551 index = end;
2552 break;
2553 }
2554 for (i = 0; i < pvec.nr; i++, index++) {
2555 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002556 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002557 done = true;
2558 break;
2559 }
2560 index = indices[i];
2561 }
2562 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002563 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002564 if (!PageUptodate(page))
2565 page = NULL;
2566 }
2567 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002568 (page && whence == SEEK_DATA) ||
2569 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002570 done = true;
2571 break;
2572 }
2573 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002574 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002575 pagevec_release(&pvec);
2576 pvec.nr = PAGEVEC_SIZE;
2577 cond_resched();
2578 }
2579 return index;
2580}
2581
Andrew Morton965c8e52012-12-17 15:59:39 -08002582static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002583{
2584 struct address_space *mapping = file->f_mapping;
2585 struct inode *inode = mapping->host;
2586 pgoff_t start, end;
2587 loff_t new_offset;
2588
Andrew Morton965c8e52012-12-17 15:59:39 -08002589 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2590 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002591 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002592 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002593 /* We're holding i_mutex so we can access i_size directly */
2594
Yufen Yu1a413642018-11-16 15:08:39 -08002595 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002596 offset = -ENXIO;
2597 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002598 start = offset >> PAGE_SHIFT;
2599 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002600 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002601 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002602 if (new_offset > offset) {
2603 if (new_offset < inode->i_size)
2604 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002605 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002606 offset = -ENXIO;
2607 else
2608 offset = inode->i_size;
2609 }
2610 }
2611
Hugh Dickins387aae62013-08-04 11:30:25 -07002612 if (offset >= 0)
2613 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002614 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002615 return offset;
2616}
2617
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002618static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2619 loff_t len)
2620{
Al Viro496ad9a2013-01-23 17:07:38 -05002621 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002622 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002623 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002624 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002625 pgoff_t start, index, end;
2626 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002627
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002628 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2629 return -EOPNOTSUPP;
2630
Al Viro59551022016-01-22 15:40:57 -05002631 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002632
2633 if (mode & FALLOC_FL_PUNCH_HOLE) {
2634 struct address_space *mapping = file->f_mapping;
2635 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2636 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002637 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002638
David Herrmann40e041a2014-08-08 14:25:27 -07002639 /* protected by i_mutex */
2640 if (info->seals & F_SEAL_WRITE) {
2641 error = -EPERM;
2642 goto out;
2643 }
2644
Hugh Dickins8e205f72014-07-23 14:00:10 -07002645 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002646 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2647 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2648 spin_lock(&inode->i_lock);
2649 inode->i_private = &shmem_falloc;
2650 spin_unlock(&inode->i_lock);
2651
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002652 if ((u64)unmap_end > (u64)unmap_start)
2653 unmap_mapping_range(mapping, unmap_start,
2654 1 + unmap_end - unmap_start, 0);
2655 shmem_truncate_range(inode, offset, offset + len - 1);
2656 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002657
2658 spin_lock(&inode->i_lock);
2659 inode->i_private = NULL;
2660 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002661 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002662 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002663 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002664 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002665 }
2666
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002667 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2668 error = inode_newsize_ok(inode, offset + len);
2669 if (error)
2670 goto out;
2671
David Herrmann40e041a2014-08-08 14:25:27 -07002672 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2673 error = -EPERM;
2674 goto out;
2675 }
2676
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002677 start = offset >> PAGE_SHIFT;
2678 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002679 /* Try to avoid a swapstorm if len is impossible to satisfy */
2680 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2681 error = -ENOSPC;
2682 goto out;
2683 }
2684
Hugh Dickins8e205f72014-07-23 14:00:10 -07002685 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002686 shmem_falloc.start = start;
2687 shmem_falloc.next = start;
2688 shmem_falloc.nr_falloced = 0;
2689 shmem_falloc.nr_unswapped = 0;
2690 spin_lock(&inode->i_lock);
2691 inode->i_private = &shmem_falloc;
2692 spin_unlock(&inode->i_lock);
2693
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002694 for (index = start; index < end; index++) {
2695 struct page *page;
2696
2697 /*
2698 * Good, the fallocate(2) manpage permits EINTR: we may have
2699 * been interrupted because we are using up too much memory.
2700 */
2701 if (signal_pending(current))
2702 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002703 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2704 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002705 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002706 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002707 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002708 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002709 if (index > start) {
2710 shmem_undo_range(inode,
2711 (loff_t)start << PAGE_SHIFT,
2712 ((loff_t)index << PAGE_SHIFT) - 1, true);
2713 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002714 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002715 }
2716
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002717 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002718 * Inform shmem_writepage() how far we have reached.
2719 * No need for lock or barrier: we have the page lock.
2720 */
2721 shmem_falloc.next++;
2722 if (!PageUptodate(page))
2723 shmem_falloc.nr_falloced++;
2724
2725 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002726 * If !PageUptodate, leave it that way so that freeable pages
2727 * can be recognized if we need to rollback on error later.
2728 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002729 * than free the pages we are allocating (and SGP_CACHE pages
2730 * might still be clean: we now need to mark those dirty too).
2731 */
2732 set_page_dirty(page);
2733 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002734 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002735 cond_resched();
2736 }
2737
2738 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2739 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002740 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002741undone:
2742 spin_lock(&inode->i_lock);
2743 inode->i_private = NULL;
2744 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002745out:
Al Viro59551022016-01-22 15:40:57 -05002746 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002747 return error;
2748}
2749
David Howells726c3342006-06-23 02:02:58 -07002750static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
David Howells726c3342006-06-23 02:02:58 -07002752 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002755 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002757 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002759 buf->f_bavail =
2760 buf->f_bfree = sbinfo->max_blocks -
2761 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002762 }
2763 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 buf->f_files = sbinfo->max_inodes;
2765 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
2767 /* else leave those fields 0 like simple_statfs */
2768 return 0;
2769}
2770
2771/*
2772 * File creation. Allocate an inode, and we're done..
2773 */
2774static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002775shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002777 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 int error = -ENOSPC;
2779
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002780 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002782 error = simple_acl_create(dir, inode);
2783 if (error)
2784 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002785 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002786 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002787 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002788 if (error && error != -EOPNOTSUPP)
2789 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002790
Al Viro718deb62009-12-16 19:35:36 -05002791 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002793 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 d_instantiate(dentry, inode);
2795 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002798out_iput:
2799 iput(inode);
2800 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Al Viro60545d02013-06-07 01:20:27 -04002803static int
2804shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2805{
2806 struct inode *inode;
2807 int error = -ENOSPC;
2808
2809 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2810 if (inode) {
2811 error = security_inode_init_security(inode, dir,
2812 NULL,
2813 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002814 if (error && error != -EOPNOTSUPP)
2815 goto out_iput;
2816 error = simple_acl_create(dir, inode);
2817 if (error)
2818 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002819 d_tmpfile(dentry, inode);
2820 }
2821 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002822out_iput:
2823 iput(inode);
2824 return error;
Al Viro60545d02013-06-07 01:20:27 -04002825}
2826
Al Viro18bb1db2011-07-26 01:41:39 -04002827static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 int error;
2830
2831 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2832 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002833 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return 0;
2835}
2836
Al Viro4acdaf22011-07-26 01:42:34 -04002837static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002838 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2841}
2842
2843/*
2844 * Link a file..
2845 */
2846static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2847{
David Howells75c3cfa2015-03-17 22:26:12 +00002848 struct inode *inode = d_inode(old_dentry);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002849 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 /*
2852 * No ordinary (disk based) filesystem counts links as inodes;
2853 * but each new link needs a new dentry, pinning lowmem, and
2854 * tmpfs dentries cannot be pruned until they are unlinked.
2855 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002856 ret = shmem_reserve_inode(inode->i_sb);
2857 if (ret)
2858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002861 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002862 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002863 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 dget(dentry); /* Extra pinning count for the created dentry */
2865 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002866out:
2867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
2870static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2871{
David Howells75c3cfa2015-03-17 22:26:12 +00002872 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002874 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2875 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002878 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002879 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 dput(dentry); /* Undo the count from "create" - this does all the work */
2881 return 0;
2882}
2883
2884static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2885{
2886 if (!simple_empty(dentry))
2887 return -ENOTEMPTY;
2888
David Howells75c3cfa2015-03-17 22:26:12 +00002889 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002890 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return shmem_unlink(dir, dentry);
2892}
2893
Miklos Szeredi37456772014-07-23 15:15:34 +02002894static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2895{
David Howellse36cb0b2015-01-29 12:02:35 +00002896 bool old_is_dir = d_is_dir(old_dentry);
2897 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002898
2899 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2900 if (old_is_dir) {
2901 drop_nlink(old_dir);
2902 inc_nlink(new_dir);
2903 } else {
2904 drop_nlink(new_dir);
2905 inc_nlink(old_dir);
2906 }
2907 }
2908 old_dir->i_ctime = old_dir->i_mtime =
2909 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002910 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002911 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02002912
2913 return 0;
2914}
2915
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002916static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2917{
2918 struct dentry *whiteout;
2919 int error;
2920
2921 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2922 if (!whiteout)
2923 return -ENOMEM;
2924
2925 error = shmem_mknod(old_dir, whiteout,
2926 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2927 dput(whiteout);
2928 if (error)
2929 return error;
2930
2931 /*
2932 * Cheat and hash the whiteout while the old dentry is still in
2933 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2934 *
2935 * d_lookup() will consistently find one of them at this point,
2936 * not sure which one, but that isn't even important.
2937 */
2938 d_rehash(whiteout);
2939 return 0;
2940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942/*
2943 * The VFS layer already does all the dentry stuff for rename,
2944 * we just have to decrement the usage count for the target if
2945 * it exists so that the VFS layer correctly free's it when it
2946 * gets overwritten.
2947 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002948static 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 -07002949{
David Howells75c3cfa2015-03-17 22:26:12 +00002950 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 int they_are_dirs = S_ISDIR(inode->i_mode);
2952
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002953 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002954 return -EINVAL;
2955
Miklos Szeredi37456772014-07-23 15:15:34 +02002956 if (flags & RENAME_EXCHANGE)
2957 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (!simple_empty(new_dentry))
2960 return -ENOTEMPTY;
2961
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002962 if (flags & RENAME_WHITEOUT) {
2963 int error;
2964
2965 error = shmem_whiteout(old_dir, old_dentry);
2966 if (error)
2967 return error;
2968 }
2969
David Howells75c3cfa2015-03-17 22:26:12 +00002970 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02002972 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00002973 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002974 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02002975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002977 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002978 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980
2981 old_dir->i_size -= BOGO_DIRENT_SIZE;
2982 new_dir->i_size += BOGO_DIRENT_SIZE;
2983 old_dir->i_ctime = old_dir->i_mtime =
2984 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002985 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return 0;
2987}
2988
2989static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2990{
2991 int error;
2992 int len;
2993 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002994 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002997 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return -ENAMETOOLONG;
2999
Joe Perches0825a6f2018-06-14 15:27:58 -07003000 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3001 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (!inode)
3003 return -ENOSPC;
3004
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003005 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003006 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003007 if (error) {
3008 if (error != -EOPNOTSUPP) {
3009 iput(inode);
3010 return error;
3011 }
3012 error = 0;
3013 }
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003016 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003017 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3018 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003019 iput(inode);
3020 return -ENOMEM;
3021 }
3022 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003024 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003025 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (error) {
3027 iput(inode);
3028 return error;
3029 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003030 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003032 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003033 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003035 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003036 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003039 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 d_instantiate(dentry, inode);
3041 dget(dentry);
3042 return 0;
3043}
3044
Al Virofceef392015-12-29 15:58:39 -05003045static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Al Virofceef392015-12-29 15:58:39 -05003047 mark_page_accessed(arg);
3048 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Al Viro6b255392015-11-17 10:20:54 -05003051static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003052 struct inode *inode,
3053 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003056 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003057 if (!dentry) {
3058 page = find_get_page(inode->i_mapping, 0);
3059 if (!page)
3060 return ERR_PTR(-ECHILD);
3061 if (!PageUptodate(page)) {
3062 put_page(page);
3063 return ERR_PTR(-ECHILD);
3064 }
3065 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003066 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003067 if (error)
3068 return ERR_PTR(error);
3069 unlock_page(page);
3070 }
Al Virofceef392015-12-29 15:58:39 -05003071 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003072 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
Eric Parisb09e0fa2011-05-24 17:12:39 -07003075#ifdef CONFIG_TMPFS_XATTR
3076/*
3077 * Superblocks without xattr inode operations may get some security.* xattr
3078 * support from the LSM "for free". As soon as we have any other xattrs
3079 * like ACLs, we also need to implement the security.* handlers at
3080 * filesystem level, though.
3081 */
3082
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003083/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003084 * Callback for security_inode_init_security() for acquiring xattrs.
3085 */
3086static int shmem_initxattrs(struct inode *inode,
3087 const struct xattr *xattr_array,
3088 void *fs_info)
3089{
3090 struct shmem_inode_info *info = SHMEM_I(inode);
3091 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003092 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003093 size_t len;
3094
3095 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003096 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003097 if (!new_xattr)
3098 return -ENOMEM;
3099
3100 len = strlen(xattr->name) + 1;
3101 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3102 GFP_KERNEL);
3103 if (!new_xattr->name) {
3104 kfree(new_xattr);
3105 return -ENOMEM;
3106 }
3107
3108 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3109 XATTR_SECURITY_PREFIX_LEN);
3110 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3111 xattr->name, len);
3112
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003113 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003114 }
3115
3116 return 0;
3117}
3118
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003119static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003120 struct dentry *unused, struct inode *inode,
3121 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003122{
Al Virob2968212016-04-10 20:48:24 -04003123 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003124
3125 name = xattr_full_name(handler, name);
3126 return simple_xattr_get(&info->xattrs, name, buffer, size);
3127}
3128
3129static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003130 struct dentry *unused, struct inode *inode,
3131 const char *name, const void *value,
3132 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003133{
Al Viro59301222016-05-27 10:19:30 -04003134 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003135
3136 name = xattr_full_name(handler, name);
3137 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3138}
3139
3140static const struct xattr_handler shmem_security_xattr_handler = {
3141 .prefix = XATTR_SECURITY_PREFIX,
3142 .get = shmem_xattr_handler_get,
3143 .set = shmem_xattr_handler_set,
3144};
3145
3146static const struct xattr_handler shmem_trusted_xattr_handler = {
3147 .prefix = XATTR_TRUSTED_PREFIX,
3148 .get = shmem_xattr_handler_get,
3149 .set = shmem_xattr_handler_set,
3150};
3151
Eric Parisb09e0fa2011-05-24 17:12:39 -07003152static const struct xattr_handler *shmem_xattr_handlers[] = {
3153#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003154 &posix_acl_access_xattr_handler,
3155 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003156#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003157 &shmem_security_xattr_handler,
3158 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003159 NULL
3160};
3161
Eric Parisb09e0fa2011-05-24 17:12:39 -07003162static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3163{
David Howells75c3cfa2015-03-17 22:26:12 +00003164 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003165 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003166}
3167#endif /* CONFIG_TMPFS_XATTR */
3168
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003169static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003170 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003171#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003172 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174};
3175
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003176static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003177 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003178#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003179 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003180#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003181};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003182
David M. Grimes91828a42006-10-17 00:09:45 -07003183static struct dentry *shmem_get_parent(struct dentry *child)
3184{
3185 return ERR_PTR(-ESTALE);
3186}
3187
3188static int shmem_match(struct inode *ino, void *vfh)
3189{
3190 __u32 *fh = vfh;
3191 __u64 inum = fh[2];
3192 inum = (inum << 32) | fh[1];
3193 return ino->i_ino == inum && fh[0] == ino->i_generation;
3194}
3195
Amir Goldstein12ba7802018-06-07 17:07:15 -07003196/* Find any alias of inode, but prefer a hashed alias */
3197static struct dentry *shmem_find_alias(struct inode *inode)
3198{
3199 struct dentry *alias = d_find_alias(inode);
3200
3201 return alias ?: d_find_any_alias(inode);
3202}
3203
3204
Christoph Hellwig480b1162007-10-21 16:42:13 -07003205static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3206 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003207{
David M. Grimes91828a42006-10-17 00:09:45 -07003208 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003209 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003210 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003211
Christoph Hellwig480b1162007-10-21 16:42:13 -07003212 if (fh_len < 3)
3213 return NULL;
3214
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003215 inum = fid->raw[2];
3216 inum = (inum << 32) | fid->raw[1];
3217
Christoph Hellwig480b1162007-10-21 16:42:13 -07003218 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3219 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003220 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003221 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003222 iput(inode);
3223 }
3224
Christoph Hellwig480b1162007-10-21 16:42:13 -07003225 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003226}
3227
Al Virob0b03822012-04-02 14:34:06 -04003228static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3229 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003230{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303231 if (*len < 3) {
3232 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003233 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303234 }
David M. Grimes91828a42006-10-17 00:09:45 -07003235
Al Viro1d3382cb2010-10-23 15:19:20 -04003236 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003237 /* Unfortunately insert_inode_hash is not idempotent,
3238 * so as we hash inodes here rather than at creation
3239 * time, we need a lock to ensure we only try
3240 * to do it once
3241 */
3242 static DEFINE_SPINLOCK(lock);
3243 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003244 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003245 __insert_inode_hash(inode,
3246 inode->i_ino + inode->i_generation);
3247 spin_unlock(&lock);
3248 }
3249
3250 fh[0] = inode->i_generation;
3251 fh[1] = inode->i_ino;
3252 fh[2] = ((__u64)inode->i_ino) >> 32;
3253
3254 *len = 3;
3255 return 1;
3256}
3257
Christoph Hellwig39655162007-10-21 16:42:17 -07003258static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003259 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003260 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003261 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003262};
3263
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003264static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3265 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003268 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003269 uid_t uid;
3270 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003272 while (options != NULL) {
3273 this_char = options;
3274 for (;;) {
3275 /*
3276 * NUL-terminate this option: unfortunately,
3277 * mount options form a comma-separated list,
3278 * but mpol's nodelist may also contain commas.
3279 */
3280 options = strchr(options, ',');
3281 if (options == NULL)
3282 break;
3283 options++;
3284 if (!isdigit(*options)) {
3285 options[-1] = '\0';
3286 break;
3287 }
3288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (!*this_char)
3290 continue;
3291 if ((value = strchr(this_char,'=')) != NULL) {
3292 *value++ = 0;
3293 } else {
Joe Perches11705322016-03-17 14:19:50 -07003294 pr_err("tmpfs: No value for mount option '%s'\n",
3295 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003296 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
3298
3299 if (!strcmp(this_char,"size")) {
3300 unsigned long long size;
3301 size = memparse(value,&rest);
3302 if (*rest == '%') {
3303 size <<= PAGE_SHIFT;
3304 size *= totalram_pages;
3305 do_div(size, 100);
3306 rest++;
3307 }
3308 if (*rest)
3309 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003310 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003311 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003313 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (*rest)
3315 goto bad_val;
3316 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003317 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (*rest)
3319 goto bad_val;
3320 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003321 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003323 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 if (*rest)
3325 goto bad_val;
3326 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003327 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003329 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (*rest)
3331 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003332 sbinfo->uid = make_kuid(current_user_ns(), uid);
3333 if (!uid_valid(sbinfo->uid))
3334 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003336 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003338 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (*rest)
3340 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003341 sbinfo->gid = make_kgid(current_user_ns(), gid);
3342 if (!gid_valid(sbinfo->gid))
3343 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003344#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003345 } else if (!strcmp(this_char, "huge")) {
3346 int huge;
3347 huge = shmem_parse_huge(value);
3348 if (huge < 0)
3349 goto bad_val;
3350 if (!has_transparent_hugepage() &&
3351 huge != SHMEM_HUGE_NEVER)
3352 goto bad_val;
3353 sbinfo->huge = huge;
3354#endif
3355#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003356 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003357 mpol_put(mpol);
3358 mpol = NULL;
3359 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003360 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003361#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 } else {
Joe Perches11705322016-03-17 14:19:50 -07003363 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003364 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
3366 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003367 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return 0;
3369
3370bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003371 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003373error:
3374 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return 1;
3376
3377}
3378
3379static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3380{
3381 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003382 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003383 unsigned long inodes;
3384 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Greg Thelen5f001102013-02-22 16:36:01 -08003386 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003387 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003388 return error;
3389
3390 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003391 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003392 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003393 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003394 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003395 goto out;
3396 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003397 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003398 * but we must separately disallow unlimited->limited, because
3399 * in that case we have no record of how much is already in use.
3400 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003401 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003402 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003403 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003404 goto out;
3405
3406 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003407 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003408 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003409 sbinfo->max_inodes = config.max_inodes;
3410 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003411
Greg Thelen5f001102013-02-22 16:36:01 -08003412 /*
3413 * Preserve previous mempolicy unless mpol remount option was specified.
3414 */
3415 if (config.mpol) {
3416 mpol_put(sbinfo->mpol);
3417 sbinfo->mpol = config.mpol; /* transfers initial ref */
3418 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003419out:
3420 spin_unlock(&sbinfo->stat_lock);
3421 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003423
Al Viro34c80b12011-12-08 21:32:45 -05003424static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003425{
Al Viro34c80b12011-12-08 21:32:45 -05003426 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003427
3428 if (sbinfo->max_blocks != shmem_default_max_blocks())
3429 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003430 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003431 if (sbinfo->max_inodes != shmem_default_max_inodes())
3432 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003433 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003434 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003435 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3436 seq_printf(seq, ",uid=%u",
3437 from_kuid_munged(&init_user_ns, sbinfo->uid));
3438 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3439 seq_printf(seq, ",gid=%u",
3440 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003441#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003442 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3443 if (sbinfo->huge)
3444 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3445#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003446 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003447 return 0;
3448}
David Herrmann9183df22014-08-08 14:25:29 -07003449
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003450#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452static void shmem_put_super(struct super_block *sb)
3453{
Hugh Dickins602586a2010-08-17 15:23:56 -07003454 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3455
3456 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003457 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003458 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 sb->s_fs_info = NULL;
3460}
3461
Kay Sievers2b2af542009-04-30 15:23:42 +02003462int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003465 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003466 int err = -ENOMEM;
3467
3468 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003469 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003470 L1_CACHE_BYTES), GFP_KERNEL);
3471 if (!sbinfo)
3472 return -ENOMEM;
3473
Joe Perches0825a6f2018-06-14 15:27:58 -07003474 sbinfo->mode = 0777 | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003475 sbinfo->uid = current_fsuid();
3476 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003477 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003479#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 /*
3481 * Per default we only allow half of the physical ram per
3482 * tmpfs instance, limiting inodes to one per page of lowmem;
3483 * but the internal instance is left unlimited.
3484 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003485 if (!(sb->s_flags & SB_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003486 sbinfo->max_blocks = shmem_default_max_blocks();
3487 sbinfo->max_inodes = shmem_default_max_inodes();
3488 if (shmem_parse_options(data, sbinfo, false)) {
3489 err = -EINVAL;
3490 goto failed;
3491 }
Al Viroca4e0512013-08-31 12:57:10 -04003492 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003493 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
David M. Grimes91828a42006-10-17 00:09:45 -07003495 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003496 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003498 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499#endif
3500
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003501 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003502 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003503 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003504 sbinfo->free_inodes = sbinfo->max_inodes;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003505 spin_lock_init(&sbinfo->shrinklist_lock);
3506 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003507
Hugh Dickins285b2c42011-08-03 16:21:20 -07003508 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003509 sb->s_blocksize = PAGE_SIZE;
3510 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 sb->s_magic = TMPFS_MAGIC;
3512 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003513 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003514#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003515 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003516#endif
3517#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003518 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003519#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003520 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003521
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003522 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (!inode)
3524 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003525 inode->i_uid = sbinfo->uid;
3526 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003527 sb->s_root = d_make_root(inode);
3528 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003529 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return 0;
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532failed:
3533 shmem_put_super(sb);
3534 return err;
3535}
3536
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003537static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539static struct inode *shmem_alloc_inode(struct super_block *sb)
3540{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003541 struct shmem_inode_info *info;
3542 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3543 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003545 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003548static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003549{
3550 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003551 if (S_ISLNK(inode->i_mode))
3552 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003553 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3554}
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556static void shmem_destroy_inode(struct inode *inode)
3557{
Al Viro09208d12011-07-26 03:15:03 -04003558 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003560 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003563static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003565 struct shmem_inode_info *info = foo;
3566 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003569static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
3571 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3572 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003573 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003576static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003578 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003581static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003583 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003585 .write_begin = shmem_write_begin,
3586 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003588#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003589 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003590#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003591 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592};
3593
Helge Deller15ad7cd2006-12-06 20:40:36 -08003594static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003596 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003598 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003599 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003600 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003601 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003602 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003603 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003604 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605#endif
3606};
3607
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003608static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003609 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003610 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003611#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003612 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003613 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615};
3616
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003617static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618#ifdef CONFIG_TMPFS
3619 .create = shmem_create,
3620 .lookup = simple_lookup,
3621 .link = shmem_link,
3622 .unlink = shmem_unlink,
3623 .symlink = shmem_symlink,
3624 .mkdir = shmem_mkdir,
3625 .rmdir = shmem_rmdir,
3626 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003627 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003628 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003630#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003631 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003632#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003633#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003634 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003635 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003636#endif
3637};
3638
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003639static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003640#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003641 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003642#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003643#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003644 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003645 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003646#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647};
3648
Hugh Dickins759b9772007-03-05 00:30:28 -08003649static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 .alloc_inode = shmem_alloc_inode,
3651 .destroy_inode = shmem_destroy_inode,
3652#ifdef CONFIG_TMPFS
3653 .statfs = shmem_statfs,
3654 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003655 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656#endif
Al Viro1f895f72010-06-05 19:10:41 -04003657 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 .drop_inode = generic_delete_inode,
3659 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003660#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3661 .nr_cached_objects = shmem_unused_huge_count,
3662 .free_cached_objects = shmem_unused_huge_scan,
3663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664};
3665
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003666static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003667 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003668 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669#ifdef CONFIG_NUMA
3670 .set_policy = shmem_set_policy,
3671 .get_policy = shmem_get_policy,
3672#endif
3673};
3674
Al Viro3c26ff62010-07-25 11:46:36 +04003675static struct dentry *shmem_mount(struct file_system_type *fs_type,
3676 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Al Viro3c26ff62010-07-25 11:46:36 +04003678 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003681static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 .owner = THIS_MODULE,
3683 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003684 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003686 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003689int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690{
3691 int error;
3692
Rob Landley16203a72013-09-11 14:26:12 -07003693 /* If rootfs called this, don't re-init */
3694 if (shmem_inode_cachep)
3695 return 0;
3696
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003697 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003699 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003701 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 goto out2;
3703 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003704
Al Viroca4e0512013-08-31 12:57:10 -04003705 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 if (IS_ERR(shm_mnt)) {
3707 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003708 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 goto out1;
3710 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003711
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003712#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003713 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003714 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3715 else
3716 shmem_huge = 0; /* just in case it was patched */
3717#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 return 0;
3719
3720out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003721 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003723 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 shm_mnt = ERR_PTR(error);
3725 return error;
3726}
Matt Mackall853ac432009-01-06 14:40:20 -08003727
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003728#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003729static ssize_t shmem_enabled_show(struct kobject *kobj,
3730 struct kobj_attribute *attr, char *buf)
3731{
3732 int values[] = {
3733 SHMEM_HUGE_ALWAYS,
3734 SHMEM_HUGE_WITHIN_SIZE,
3735 SHMEM_HUGE_ADVISE,
3736 SHMEM_HUGE_NEVER,
3737 SHMEM_HUGE_DENY,
3738 SHMEM_HUGE_FORCE,
3739 };
3740 int i, count;
3741
3742 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3743 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3744
3745 count += sprintf(buf + count, fmt,
3746 shmem_format_huge(values[i]));
3747 }
3748 buf[count - 1] = '\n';
3749 return count;
3750}
3751
3752static ssize_t shmem_enabled_store(struct kobject *kobj,
3753 struct kobj_attribute *attr, const char *buf, size_t count)
3754{
3755 char tmp[16];
3756 int huge;
3757
3758 if (count + 1 > sizeof(tmp))
3759 return -EINVAL;
3760 memcpy(tmp, buf, count);
3761 tmp[count] = '\0';
3762 if (count && tmp[count - 1] == '\n')
3763 tmp[count - 1] = '\0';
3764
3765 huge = shmem_parse_huge(tmp);
3766 if (huge == -EINVAL)
3767 return -EINVAL;
3768 if (!has_transparent_hugepage() &&
3769 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3770 return -EINVAL;
3771
3772 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003773 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003774 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3775 return count;
3776}
3777
3778struct kobj_attribute shmem_enabled_attr =
3779 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07003780#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003781
Arnd Bergmann3b337192016-08-10 16:27:44 -07003782#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003783bool shmem_huge_enabled(struct vm_area_struct *vma)
3784{
3785 struct inode *inode = file_inode(vma->vm_file);
3786 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3787 loff_t i_size;
3788 pgoff_t off;
3789
3790 if (shmem_huge == SHMEM_HUGE_FORCE)
3791 return true;
3792 if (shmem_huge == SHMEM_HUGE_DENY)
3793 return false;
3794 switch (sbinfo->huge) {
3795 case SHMEM_HUGE_NEVER:
3796 return false;
3797 case SHMEM_HUGE_ALWAYS:
3798 return true;
3799 case SHMEM_HUGE_WITHIN_SIZE:
3800 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3801 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3802 if (i_size >= HPAGE_PMD_SIZE &&
3803 i_size >> PAGE_SHIFT >= off)
3804 return true;
Gustavo A. R. Silvac8402872017-11-15 17:38:45 -08003805 /* fall through */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003806 case SHMEM_HUGE_ADVISE:
3807 /* TODO: implement fadvise() hints */
3808 return (vma->vm_flags & VM_HUGEPAGE);
3809 default:
3810 VM_BUG_ON(1);
3811 return false;
3812 }
3813}
Arnd Bergmann3b337192016-08-10 16:27:44 -07003814#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003815
Matt Mackall853ac432009-01-06 14:40:20 -08003816#else /* !CONFIG_SHMEM */
3817
3818/*
3819 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3820 *
3821 * This is intended for small system where the benefits of the full
3822 * shmem code (swap-backed and resource-limited) are outweighed by
3823 * their complexity. On systems without swap this code should be
3824 * effectively equivalent, but much lighter weight.
3825 */
3826
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003827static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003828 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003829 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003830 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003831 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003832};
3833
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003834int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003835{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003836 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003837
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003838 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003839 BUG_ON(IS_ERR(shm_mnt));
3840
3841 return 0;
3842}
3843
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003844int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003845{
3846 return 0;
3847}
3848
Hugh Dickins3f96b792009-09-21 17:03:37 -07003849int shmem_lock(struct file *file, int lock, struct user_struct *user)
3850{
3851 return 0;
3852}
3853
Hugh Dickins24513262012-01-20 14:34:21 -08003854void shmem_unlock_mapping(struct address_space *mapping)
3855{
3856}
3857
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003858#ifdef CONFIG_MMU
3859unsigned long shmem_get_unmapped_area(struct file *file,
3860 unsigned long addr, unsigned long len,
3861 unsigned long pgoff, unsigned long flags)
3862{
3863 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3864}
3865#endif
3866
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003867void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003868{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003869 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003870}
3871EXPORT_SYMBOL_GPL(shmem_truncate_range);
3872
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003873#define shmem_vm_ops generic_file_vm_ops
3874#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003875#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003876#define shmem_acct_size(flags, size) 0
3877#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003878
3879#endif /* CONFIG_SHMEM */
3880
3881/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Matthew Auld703321b2017-10-06 23:18:13 +01003883static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00003884 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04003887 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Matthew Auld703321b2017-10-06 23:18:13 +01003889 if (IS_ERR(mnt))
3890 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Hugh Dickins285b2c42011-08-03 16:21:20 -07003892 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 return ERR_PTR(-EINVAL);
3894
3895 if (shmem_acct_size(flags, size))
3896 return ERR_PTR(-ENOMEM);
3897
Al Viro93dec2d2018-07-08 23:02:03 -04003898 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
3899 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04003900 if (unlikely(!inode)) {
3901 shmem_unacct_size(flags, size);
3902 return ERR_PTR(-ENOSPC);
3903 }
Eric Parisc7277092013-12-02 11:24:19 +00003904 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003906 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003907 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04003908 if (!IS_ERR(res))
3909 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
3910 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05003911 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04003912 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05003913 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914}
Eric Parisc7277092013-12-02 11:24:19 +00003915
3916/**
3917 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3918 * kernel internal. There will be NO LSM permission checks against the
3919 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003920 * higher layer. The users are the big_key and shm implementations. LSM
3921 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003922 * @name: name for dentry (to be seen in /proc/<pid>/maps
3923 * @size: size to be set for the file
3924 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3925 */
3926struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3927{
Matthew Auld703321b2017-10-06 23:18:13 +01003928 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00003929}
3930
3931/**
3932 * shmem_file_setup - get an unlinked file living in tmpfs
3933 * @name: name for dentry (to be seen in /proc/<pid>/maps
3934 * @size: size to be set for the file
3935 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3936 */
3937struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3938{
Matthew Auld703321b2017-10-06 23:18:13 +01003939 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00003940}
Keith Packard395e0dd2008-06-20 00:08:06 -07003941EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Randy Dunlap46711812008-03-19 17:00:41 -07003943/**
Matthew Auld703321b2017-10-06 23:18:13 +01003944 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
3945 * @mnt: the tmpfs mount where the file will be created
3946 * @name: name for dentry (to be seen in /proc/<pid>/maps
3947 * @size: size to be set for the file
3948 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3949 */
3950struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
3951 loff_t size, unsigned long flags)
3952{
3953 return __shmem_file_setup(mnt, name, size, flags, 0);
3954}
3955EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
3956
3957/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3960 */
3961int shmem_zero_setup(struct vm_area_struct *vma)
3962{
3963 struct file *file;
3964 loff_t size = vma->vm_end - vma->vm_start;
3965
Hugh Dickins66fc1302015-06-14 09:48:09 -07003966 /*
3967 * Cloning a new file under mmap_sem leads to a lock ordering conflict
3968 * between XFS directory reading and selinux: since this file is only
3969 * accessible to the user through its mapping, use S_PRIVATE flag to
3970 * bypass file security, in the same way as shmem_kernel_file_setup().
3971 */
Matthew Auld703321b2017-10-06 23:18:13 +01003972 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 if (IS_ERR(file))
3974 return PTR_ERR(file);
3975
3976 if (vma->vm_file)
3977 fput(vma->vm_file);
3978 vma->vm_file = file;
3979 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003980
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003981 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003982 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
3983 (vma->vm_end & HPAGE_PMD_MASK)) {
3984 khugepaged_enter(vma, vma->vm_flags);
3985 }
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 return 0;
3988}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003989
3990/**
3991 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3992 * @mapping: the page's address_space
3993 * @index: the page index
3994 * @gfp: the page allocator flags to use if allocating
3995 *
3996 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3997 * with any new page allocations done using the specified allocation flags.
3998 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3999 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4000 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4001 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004002 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4003 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004004 */
4005struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4006 pgoff_t index, gfp_t gfp)
4007{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004008#ifdef CONFIG_SHMEM
4009 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004010 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004011 int error;
4012
4013 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004014 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004015 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004016 if (error)
4017 page = ERR_PTR(error);
4018 else
4019 unlock_page(page);
4020 return page;
4021#else
4022 /*
4023 * The tiny !SHMEM case uses ramfs without swap
4024 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004025 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004026#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004027}
4028EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);