blob: b4d27ef87496069d67137520d8816c792709bd16 [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{
Arun KSca79b0c2018-12-28 00:34:29 -0800112 return totalram_pages() / 2;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800113}
114
115static unsigned long shmem_default_max_inodes(void)
116{
Arun KSca79b0c2018-12-28 00:34:29 -0800117 unsigned long nr_pages = totalram_pages();
118
119 return min(nr_pages - totalhigh_pages(), nr_pages / 2);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800120}
Andrew Mortonb76db732008-02-08 04:21:49 -0800121#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800122
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700123static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
124static int shmem_replace_page(struct page **pagep, gfp_t gfp,
125 struct shmem_inode_info *info, pgoff_t index);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -0800126static int shmem_swapin_page(struct inode *inode, pgoff_t index,
127 struct page **pagep, enum sgp_type sgp,
128 gfp_t gfp, struct vm_area_struct *vma,
129 vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700130static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700131 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800132 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700133 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700134
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700135int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700136 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700137{
138 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800139 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
143{
144 return sb->s_fs_info;
145}
146
147/*
148 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
149 * for shared memory and for shared anonymous (/dev/zero) mappings
150 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
151 * consistent with the pre-accounting of private mappings ...
152 */
153static inline int shmem_acct_size(unsigned long flags, loff_t size)
154{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000155 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000156 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static inline void shmem_unacct_size(unsigned long flags, loff_t size)
160{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000161 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 vm_unacct_memory(VM_ACCT(size));
163}
164
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700165static inline int shmem_reacct_size(unsigned long flags,
166 loff_t oldsize, loff_t newsize)
167{
168 if (!(flags & VM_NORESERVE)) {
169 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
170 return security_vm_enough_memory_mm(current->mm,
171 VM_ACCT(newsize) - VM_ACCT(oldsize));
172 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
173 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
174 }
175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700180 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
182 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
183 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700184static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700186 if (!(flags & VM_NORESERVE))
187 return 0;
188
189 return security_vm_enough_memory_mm(current->mm,
190 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static inline void shmem_unacct_blocks(unsigned long flags, long pages)
194{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000195 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300196 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Mike Rapoport0f079692017-09-06 16:22:59 -0700199static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
200{
201 struct shmem_inode_info *info = SHMEM_I(inode);
202 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
203
204 if (shmem_acct_block(info->flags, pages))
205 return false;
206
207 if (sbinfo->max_blocks) {
208 if (percpu_counter_compare(&sbinfo->used_blocks,
209 sbinfo->max_blocks - pages) > 0)
210 goto unacct;
211 percpu_counter_add(&sbinfo->used_blocks, pages);
212 }
213
214 return true;
215
216unacct:
217 shmem_unacct_blocks(info->flags, pages);
218 return false;
219}
220
221static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
222{
223 struct shmem_inode_info *info = SHMEM_I(inode);
224 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
225
226 if (sbinfo->max_blocks)
227 percpu_counter_sub(&sbinfo->used_blocks, pages);
228 shmem_unacct_blocks(info->flags, pages);
229}
230
Hugh Dickins759b9772007-03-05 00:30:28 -0800231static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700232static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800233static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800234static const struct inode_operations shmem_inode_operations;
235static const struct inode_operations shmem_dir_inode_operations;
236static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400237static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700238static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Mike Rapoportb0506e42017-02-22 15:43:28 -0800240bool vma_is_shmem(struct vm_area_struct *vma)
241{
242 return vma->vm_ops == &shmem_vm_ops;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800246static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800248static int shmem_reserve_inode(struct super_block *sb)
249{
250 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
251 if (sbinfo->max_inodes) {
252 spin_lock(&sbinfo->stat_lock);
253 if (!sbinfo->free_inodes) {
254 spin_unlock(&sbinfo->stat_lock);
255 return -ENOSPC;
256 }
257 sbinfo->free_inodes--;
258 spin_unlock(&sbinfo->stat_lock);
259 }
260 return 0;
261}
262
263static void shmem_free_inode(struct super_block *sb)
264{
265 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
266 if (sbinfo->max_inodes) {
267 spin_lock(&sbinfo->stat_lock);
268 sbinfo->free_inodes++;
269 spin_unlock(&sbinfo->stat_lock);
270 }
271}
272
Randy Dunlap46711812008-03-19 17:00:41 -0700273/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700274 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * @inode: inode to recalc
276 *
277 * We have to calculate the free blocks since the mm can drop
278 * undirtied hole pages behind our back.
279 *
280 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
281 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
282 *
283 * It has to be called with the spinlock held.
284 */
285static void shmem_recalc_inode(struct inode *inode)
286{
287 struct shmem_inode_info *info = SHMEM_I(inode);
288 long freed;
289
290 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
291 if (freed > 0) {
292 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700293 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700294 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296}
297
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700298bool shmem_charge(struct inode *inode, long pages)
299{
300 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700301 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700302
Mike Rapoport0f079692017-09-06 16:22:59 -0700303 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700304 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700305
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800306 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
307 inode->i_mapping->nrpages += pages;
308
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700309 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700310 info->alloced += pages;
311 inode->i_blocks += pages * BLOCKS_PER_PAGE;
312 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700313 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700314
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700315 return true;
316}
317
318void shmem_uncharge(struct inode *inode, long pages)
319{
320 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700321 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700322
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800323 /* nrpages adjustment done by __delete_from_page_cache() or caller */
324
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700325 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700326 info->alloced -= pages;
327 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
328 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700329 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700330
Mike Rapoport0f079692017-09-06 16:22:59 -0700331 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700332}
333
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700334/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500335 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700336 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500337static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700338 pgoff_t index, void *expected, void *replacement)
339{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500340 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700341 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700342
343 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700344 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500345 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700346 if (item != expected)
347 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500348 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700349 return 0;
350}
351
352/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700353 * Sometimes, before we decide whether to proceed or to fail, we must check
354 * that an entry was not already brought back from swap by a racing thread.
355 *
356 * Checking page is not enough: by the time a SwapCache page is locked, it
357 * might be reused, and again be SwapCache, using the same swap as before.
358 */
359static bool shmem_confirm_swap(struct address_space *mapping,
360 pgoff_t index, swp_entry_t swap)
361{
Matthew Wilcoxa12831b2017-11-22 08:34:58 -0500362 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700363}
364
365/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700366 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
367 *
368 * SHMEM_HUGE_NEVER:
369 * disables huge pages for the mount;
370 * SHMEM_HUGE_ALWAYS:
371 * enables huge pages for the mount;
372 * SHMEM_HUGE_WITHIN_SIZE:
373 * only allocate huge pages if the page will be fully within i_size,
374 * also respect fadvise()/madvise() hints;
375 * SHMEM_HUGE_ADVISE:
376 * only allocate huge pages if requested with fadvise()/madvise();
377 */
378
379#define SHMEM_HUGE_NEVER 0
380#define SHMEM_HUGE_ALWAYS 1
381#define SHMEM_HUGE_WITHIN_SIZE 2
382#define SHMEM_HUGE_ADVISE 3
383
384/*
385 * Special values.
386 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
387 *
388 * SHMEM_HUGE_DENY:
389 * disables huge on shm_mnt and all mounts, for emergency use;
390 * SHMEM_HUGE_FORCE:
391 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
392 *
393 */
394#define SHMEM_HUGE_DENY (-1)
395#define SHMEM_HUGE_FORCE (-2)
396
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700397#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700398/* ifdef here to avoid bloating shmem.o when not necessary */
399
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700400static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700401
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800402#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700403static int shmem_parse_huge(const char *str)
404{
405 if (!strcmp(str, "never"))
406 return SHMEM_HUGE_NEVER;
407 if (!strcmp(str, "always"))
408 return SHMEM_HUGE_ALWAYS;
409 if (!strcmp(str, "within_size"))
410 return SHMEM_HUGE_WITHIN_SIZE;
411 if (!strcmp(str, "advise"))
412 return SHMEM_HUGE_ADVISE;
413 if (!strcmp(str, "deny"))
414 return SHMEM_HUGE_DENY;
415 if (!strcmp(str, "force"))
416 return SHMEM_HUGE_FORCE;
417 return -EINVAL;
418}
419
420static const char *shmem_format_huge(int huge)
421{
422 switch (huge) {
423 case SHMEM_HUGE_NEVER:
424 return "never";
425 case SHMEM_HUGE_ALWAYS:
426 return "always";
427 case SHMEM_HUGE_WITHIN_SIZE:
428 return "within_size";
429 case SHMEM_HUGE_ADVISE:
430 return "advise";
431 case SHMEM_HUGE_DENY:
432 return "deny";
433 case SHMEM_HUGE_FORCE:
434 return "force";
435 default:
436 VM_BUG_ON(1);
437 return "bad_val";
438 }
439}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800440#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700441
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700442static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
443 struct shrink_control *sc, unsigned long nr_to_split)
444{
445 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800446 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700447 struct inode *inode;
448 struct shmem_inode_info *info;
449 struct page *page;
450 unsigned long batch = sc ? sc->nr_to_scan : 128;
451 int removed = 0, split = 0;
452
453 if (list_empty(&sbinfo->shrinklist))
454 return SHRINK_STOP;
455
456 spin_lock(&sbinfo->shrinklist_lock);
457 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
458 info = list_entry(pos, struct shmem_inode_info, shrinklist);
459
460 /* pin the inode */
461 inode = igrab(&info->vfs_inode);
462
463 /* inode is about to be evicted */
464 if (!inode) {
465 list_del_init(&info->shrinklist);
466 removed++;
467 goto next;
468 }
469
470 /* Check if there's anything to gain */
471 if (round_up(inode->i_size, PAGE_SIZE) ==
472 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800473 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700474 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700475 goto next;
476 }
477
478 list_move(&info->shrinklist, &list);
479next:
480 if (!--batch)
481 break;
482 }
483 spin_unlock(&sbinfo->shrinklist_lock);
484
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800485 list_for_each_safe(pos, next, &to_remove) {
486 info = list_entry(pos, struct shmem_inode_info, shrinklist);
487 inode = &info->vfs_inode;
488 list_del_init(&info->shrinklist);
489 iput(inode);
490 }
491
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700492 list_for_each_safe(pos, next, &list) {
493 int ret;
494
495 info = list_entry(pos, struct shmem_inode_info, shrinklist);
496 inode = &info->vfs_inode;
497
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700498 if (nr_to_split && split >= nr_to_split)
499 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700500
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700501 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700502 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
503 if (!page)
504 goto drop;
505
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700506 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700507 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700508 put_page(page);
509 goto drop;
510 }
511
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700512 /*
513 * Leave the inode on the list if we failed to lock
514 * the page at this time.
515 *
516 * Waiting for the lock may lead to deadlock in the
517 * reclaim path.
518 */
519 if (!trylock_page(page)) {
520 put_page(page);
521 goto leave;
522 }
523
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700524 ret = split_huge_page(page);
525 unlock_page(page);
526 put_page(page);
527
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700528 /* If split failed leave the inode on the list */
529 if (ret)
530 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700531
532 split++;
533drop:
534 list_del_init(&info->shrinklist);
535 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700536leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700537 iput(inode);
538 }
539
540 spin_lock(&sbinfo->shrinklist_lock);
541 list_splice_tail(&list, &sbinfo->shrinklist);
542 sbinfo->shrinklist_len -= removed;
543 spin_unlock(&sbinfo->shrinklist_lock);
544
545 return split;
546}
547
548static long shmem_unused_huge_scan(struct super_block *sb,
549 struct shrink_control *sc)
550{
551 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
552
553 if (!READ_ONCE(sbinfo->shrinklist_len))
554 return SHRINK_STOP;
555
556 return shmem_unused_huge_shrink(sbinfo, sc, 0);
557}
558
559static long shmem_unused_huge_count(struct super_block *sb,
560 struct shrink_control *sc)
561{
562 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
563 return READ_ONCE(sbinfo->shrinklist_len);
564}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700565#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700566
567#define shmem_huge SHMEM_HUGE_DENY
568
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700569static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
570 struct shrink_control *sc, unsigned long nr_to_split)
571{
572 return 0;
573}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700574#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700575
Yang Shi89fdcd22018-06-07 17:06:59 -0700576static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
577{
578 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
579 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
580 shmem_huge != SHMEM_HUGE_DENY)
581 return true;
582 return false;
583}
584
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700585/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700586 * Like add_to_page_cache_locked, but error if expected item has gone.
587 */
588static int shmem_add_to_page_cache(struct page *page,
589 struct address_space *mapping,
Matthew Wilcox552446a2017-12-01 13:25:14 -0500590 pgoff_t index, void *expected, gfp_t gfp)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700591{
Matthew Wilcox552446a2017-12-01 13:25:14 -0500592 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
593 unsigned long i = 0;
594 unsigned long nr = 1UL << compound_order(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700595
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700596 VM_BUG_ON_PAGE(PageTail(page), page);
597 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800598 VM_BUG_ON_PAGE(!PageLocked(page), page);
599 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700600 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700601
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700602 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700603 page->mapping = mapping;
604 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700605
Matthew Wilcox552446a2017-12-01 13:25:14 -0500606 do {
607 void *entry;
608 xas_lock_irq(&xas);
609 entry = xas_find_conflict(&xas);
610 if (entry != expected)
611 xas_set_err(&xas, -EEXIST);
612 xas_create_range(&xas);
613 if (xas_error(&xas))
614 goto unlock;
615next:
616 xas_store(&xas, page + i);
617 if (++i < nr) {
618 xas_next(&xas);
619 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700620 }
Matthew Wilcox552446a2017-12-01 13:25:14 -0500621 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700622 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700623 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500624 }
625 mapping->nrpages += nr;
Mel Gorman11fb9982016-07-28 15:46:20 -0700626 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
627 __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500628unlock:
629 xas_unlock_irq(&xas);
630 } while (xas_nomem(&xas, gfp));
631
632 if (xas_error(&xas)) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700633 page->mapping = NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700634 page_ref_sub(page, nr);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500635 return xas_error(&xas);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700636 }
Matthew Wilcox552446a2017-12-01 13:25:14 -0500637
638 return 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700639}
640
641/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700642 * Like delete_from_page_cache, but substitutes swap for page.
643 */
644static void shmem_delete_from_page_cache(struct page *page, void *radswap)
645{
646 struct address_space *mapping = page->mapping;
647 int error;
648
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700649 VM_BUG_ON_PAGE(PageCompound(page), page);
650
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700651 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500652 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700653 page->mapping = NULL;
654 mapping->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700655 __dec_node_page_state(page, NR_FILE_PAGES);
656 __dec_node_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700657 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300658 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700659 BUG_ON(error);
660}
661
662/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500663 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700664 */
665static int shmem_free_swap(struct address_space *mapping,
666 pgoff_t index, void *radswap)
667{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700668 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700669
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500670 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700671 if (old != radswap)
672 return -ENOENT;
673 free_swap_and_cache(radix_to_swp_entry(radswap));
674 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700675}
676
677/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800678 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800679 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800680 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700681 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800682 * as long as the inode doesn't go away and racy results are not a problem.
683 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800684unsigned long shmem_partial_swap_usage(struct address_space *mapping,
685 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800686{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500687 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800688 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800689 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800690
691 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500692 xas_for_each(&xas, page, end - 1) {
693 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700694 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400695 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800696 swapped++;
697
698 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500699 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800700 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800701 }
702 }
703
704 rcu_read_unlock();
705
706 return swapped << PAGE_SHIFT;
707}
708
709/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800710 * Determine (in bytes) how many of the shmem object's pages mapped by the
711 * given vma is swapped out.
712 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700713 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800714 * as long as the inode doesn't go away and racy results are not a problem.
715 */
716unsigned long shmem_swap_usage(struct vm_area_struct *vma)
717{
718 struct inode *inode = file_inode(vma->vm_file);
719 struct shmem_inode_info *info = SHMEM_I(inode);
720 struct address_space *mapping = inode->i_mapping;
721 unsigned long swapped;
722
723 /* Be careful as we don't hold info->lock */
724 swapped = READ_ONCE(info->swapped);
725
726 /*
727 * The easier cases are when the shmem object has nothing in swap, or
728 * the vma maps it whole. Then we can simply use the stats that we
729 * already track.
730 */
731 if (!swapped)
732 return 0;
733
734 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
735 return swapped << PAGE_SHIFT;
736
737 /* Here comes the more involved part */
738 return shmem_partial_swap_usage(mapping,
739 linear_page_index(vma, vma->vm_start),
740 linear_page_index(vma, vma->vm_end));
741}
742
743/*
Hugh Dickins24513262012-01-20 14:34:21 -0800744 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
745 */
746void shmem_unlock_mapping(struct address_space *mapping)
747{
748 struct pagevec pvec;
749 pgoff_t indices[PAGEVEC_SIZE];
750 pgoff_t index = 0;
751
Mel Gorman86679822017-11-15 17:37:52 -0800752 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800753 /*
754 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
755 */
756 while (!mapping_unevictable(mapping)) {
757 /*
758 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
759 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
760 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700761 pvec.nr = find_get_entries(mapping, index,
762 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800763 if (!pvec.nr)
764 break;
765 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700766 pagevec_remove_exceptionals(&pvec);
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +0000767 check_move_unevictable_pages(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800768 pagevec_release(&pvec);
769 cond_resched();
770 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700771}
772
773/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500774 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700775 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700776 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700777static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
778 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700780 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300782 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
783 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
784 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
785 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700786 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700787 pgoff_t indices[PAGEVEC_SIZE];
788 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700789 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700790 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700792 if (lend == -1)
793 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700794
Mel Gorman86679822017-11-15 17:37:52 -0800795 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700796 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700797 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700798 pvec.nr = find_get_entries(mapping, index,
799 min(end - index, (pgoff_t)PAGEVEC_SIZE),
800 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700801 if (!pvec.nr)
802 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700803 for (i = 0; i < pagevec_count(&pvec); i++) {
804 struct page *page = pvec.pages[i];
805
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700806 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700807 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700808 break;
809
Matthew Wilcox3159f942017-11-03 13:30:42 -0400810 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700811 if (unfalloc)
812 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700813 nr_swaps_freed += !shmem_free_swap(mapping,
814 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700815 continue;
816 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700817
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700818 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
819
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700820 if (!trylock_page(page))
821 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700822
823 if (PageTransTail(page)) {
824 /* Middle of THP: zero out the page */
825 clear_highpage(page);
826 unlock_page(page);
827 continue;
828 } else if (PageTransHuge(page)) {
829 if (index == round_down(end, HPAGE_PMD_NR)) {
830 /*
831 * Range ends in the middle of THP:
832 * zero out the page
833 */
834 clear_highpage(page);
835 unlock_page(page);
836 continue;
837 }
838 index += HPAGE_PMD_NR - 1;
839 i += HPAGE_PMD_NR - 1;
840 }
841
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700842 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700843 VM_BUG_ON_PAGE(PageTail(page), page);
844 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800845 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700846 truncate_inode_page(mapping, page);
847 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700848 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700849 unlock_page(page);
850 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700851 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800852 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700853 cond_resched();
854 index++;
855 }
856
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700857 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700858 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700859 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700860 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300861 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700862 if (start > end) {
863 top = partial_end;
864 partial_end = 0;
865 }
866 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700867 set_page_dirty(page);
868 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300869 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700870 }
871 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700872 if (partial_end) {
873 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700874 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700875 if (page) {
876 zero_user_segment(page, 0, partial_end);
877 set_page_dirty(page);
878 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300879 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700880 }
881 }
882 if (start >= end)
883 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700884
885 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700886 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700887 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700888
889 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700890 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700891 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700892 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700893 /* If all gone or hole-punch or unfalloc, we're done */
894 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700895 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700896 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700897 index = start;
898 continue;
899 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700900 for (i = 0; i < pagevec_count(&pvec); i++) {
901 struct page *page = pvec.pages[i];
902
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700903 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700904 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700905 break;
906
Matthew Wilcox3159f942017-11-03 13:30:42 -0400907 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700908 if (unfalloc)
909 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700910 if (shmem_free_swap(mapping, index, page)) {
911 /* Swap was replaced by page: retry */
912 index--;
913 break;
914 }
915 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700916 continue;
917 }
918
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700919 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700920
921 if (PageTransTail(page)) {
922 /* Middle of THP: zero out the page */
923 clear_highpage(page);
924 unlock_page(page);
925 /*
926 * Partial thp truncate due 'start' in middle
927 * of THP: don't need to look on these pages
928 * again on !pvec.nr restart.
929 */
930 if (index != round_down(end, HPAGE_PMD_NR))
931 start++;
932 continue;
933 } else if (PageTransHuge(page)) {
934 if (index == round_down(end, HPAGE_PMD_NR)) {
935 /*
936 * Range ends in the middle of THP:
937 * zero out the page
938 */
939 clear_highpage(page);
940 unlock_page(page);
941 continue;
942 }
943 index += HPAGE_PMD_NR - 1;
944 i += HPAGE_PMD_NR - 1;
945 }
946
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700947 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700948 VM_BUG_ON_PAGE(PageTail(page), page);
949 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800950 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700951 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700952 } else {
953 /* Page was replaced by swap: retry */
954 unlock_page(page);
955 index--;
956 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700957 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700958 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700959 unlock_page(page);
960 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700961 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800962 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700963 index++;
964 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700965
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700966 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700967 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700969 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700970}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700972void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
973{
974 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700975 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700977EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
David Howellsa528d352017-01-31 16:46:22 +0000979static int shmem_getattr(const struct path *path, struct kstat *stat,
980 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -0700981{
David Howellsa528d352017-01-31 16:46:22 +0000982 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -0700983 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -0700984 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -0700985
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800986 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700987 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800988 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700989 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800990 }
Yu Zhao44a30222015-09-08 15:03:33 -0700991 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -0700992
993 if (is_huge_enabled(sb_info))
994 stat->blksize = HPAGE_PMD_SIZE;
995
Yu Zhao44a30222015-09-08 15:03:33 -0700996 return 0;
997}
998
Hugh Dickins94c1e622011-06-27 16:18:03 -0700999static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
David Howells75c3cfa2015-03-17 22:26:12 +00001001 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001002 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001003 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 int error;
1005
Jan Kara31051c82016-05-26 16:55:18 +02001006 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001007 if (error)
1008 return error;
1009
Hugh Dickins94c1e622011-06-27 16:18:03 -07001010 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1011 loff_t oldsize = inode->i_size;
1012 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001013
David Herrmann40e041a2014-08-08 14:25:27 -07001014 /* protected by i_mutex */
1015 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1016 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1017 return -EPERM;
1018
Hugh Dickins94c1e622011-06-27 16:18:03 -07001019 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001020 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1021 oldsize, newsize);
1022 if (error)
1023 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001024 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001025 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001026 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001027 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001028 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001029 if (oldsize > holebegin)
1030 unmap_mapping_range(inode->i_mapping,
1031 holebegin, 0, 1);
1032 if (info->alloced)
1033 shmem_truncate_range(inode,
1034 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001035 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001036 if (oldsize > holebegin)
1037 unmap_mapping_range(inode->i_mapping,
1038 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001039
1040 /*
1041 * Part of the huge page can be beyond i_size: subject
1042 * to shrink under memory pressure.
1043 */
1044 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1045 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001046 /*
1047 * _careful to defend against unlocked access to
1048 * ->shrink_list in shmem_unused_huge_shrink()
1049 */
1050 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001051 list_add_tail(&info->shrinklist,
1052 &sbinfo->shrinklist);
1053 sbinfo->shrinklist_len++;
1054 }
1055 spin_unlock(&sbinfo->shrinklist_lock);
1056 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001060 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001061 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001062 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return error;
1064}
1065
Al Viro1f895f72010-06-05 19:10:41 -04001066static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001069 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001071 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 shmem_unacct_size(info->flags, inode->i_size);
1073 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001074 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001075 if (!list_empty(&info->shrinklist)) {
1076 spin_lock(&sbinfo->shrinklist_lock);
1077 if (!list_empty(&info->shrinklist)) {
1078 list_del_init(&info->shrinklist);
1079 sbinfo->shrinklist_len--;
1080 }
1081 spin_unlock(&sbinfo->shrinklist_lock);
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001084 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001086 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Al Viro3ed47db2016-01-22 18:08:52 -05001088 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001089
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001090 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001091 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001092 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001093 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001096static unsigned long find_swap_entry(struct xarray *xa, void *item)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001097{
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001098 XA_STATE(xas, xa, 0);
Matthew Wilcox478922e2016-12-14 15:08:52 -08001099 unsigned int checked = 0;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001100 void *entry;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001101
1102 rcu_read_lock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001103 xas_for_each(&xas, entry, ULONG_MAX) {
1104 if (xas_retry(&xas, entry))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001105 continue;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001106 if (entry == item)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001107 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001108 checked++;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001109 if ((checked % XA_CHECK_SCHED) != 0)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001110 continue;
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001111 xas_pause(&xas);
Matthew Wilcox478922e2016-12-14 15:08:52 -08001112 cond_resched_rcu();
1113 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001114 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001115
1116 return entry ? xas.xa_index : -1;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001117}
1118
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001119/*
1120 * If swap found in inode, free it and move page from swapcache to filecache.
1121 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001122static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001123 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Hugh Dickins285b2c42011-08-03 16:21:20 -07001125 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001126 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001127 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001128 gfp_t gfp;
1129 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001131 radswap = swp_to_radix_entry(swap);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001132 index = find_swap_entry(&mapping->i_pages, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001133 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -07001134 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001135
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001136 /*
1137 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -04001138 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001139 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -07001140 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001141 */
1142 if (shmem_swaplist.next != &info->swaplist)
1143 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001144
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001145 gfp = mapping_gfp_mask(mapping);
1146 if (shmem_should_replace_page(*pagep, gfp)) {
1147 mutex_unlock(&shmem_swaplist_mutex);
1148 error = shmem_replace_page(pagep, gfp, info, index);
1149 mutex_lock(&shmem_swaplist_mutex);
1150 /*
1151 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -07001152 * allocation, but the inode might have been freed while we
1153 * dropped it: although a racing shmem_evict_inode() cannot
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001154 * complete without emptying the page cache, our page lock
Hugh Dickins0142ef62012-06-07 14:21:09 -07001155 * on this swapcache page is not enough to prevent that -
1156 * free_swap_and_cache() of our swap entry will only
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001157 * trylock_page(), removing swap from page cache whatever.
Hugh Dickins0142ef62012-06-07 14:21:09 -07001158 *
1159 * We must not proceed to shmem_add_to_page_cache() if the
1160 * inode has been freed, but of course we cannot rely on
1161 * inode or mapping or info to check that. However, we can
1162 * safely check if our swap entry is still in use (and here
1163 * it can't have got reused for another page): if it's still
1164 * in use, then the inode cannot have been freed yet, and we
1165 * can safely proceed (if it's no longer in use, that tells
1166 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001167 */
1168 if (!page_swapcount(*pagep))
1169 error = -ENOENT;
1170 }
1171
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001172 /*
Hugh Dickins778dd892011-05-11 15:13:37 -07001173 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1174 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1175 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001176 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001177 if (!error)
1178 error = shmem_add_to_page_cache(*pagep, mapping, index,
Matthew Wilcox552446a2017-12-01 13:25:14 -05001179 radswap, gfp);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001180 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001181 /*
1182 * Truncation and eviction use free_swap_and_cache(), which
1183 * only does trylock page: if we raced, best clean up here.
1184 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001185 delete_from_swap_cache(*pagep);
1186 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001187 if (!error) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001188 spin_lock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001189 info->swapped--;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001190 spin_unlock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001191 swap_free(swap);
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001194 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001198 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001200int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001202 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001204 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001205 int error = 0;
1206
1207 /*
1208 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001209 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001210 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001211 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001212 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001213
1214 /*
1215 * Charge page using GFP_KERNEL while we can wait, before taking
1216 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1217 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001218 */
Tejun Heo2cf85582018-07-03 11:14:56 -04001219 error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL,
1220 &memcg, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001221 if (error)
1222 goto out;
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001223 /* No memory allocation: swap entry occupies the slot for the page */
Johannes Weiner00501b52014-08-08 14:19:20 -07001224 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001226 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001227 list_for_each_safe(this, next, &shmem_swaplist) {
1228 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001229 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001230 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001231 else
1232 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001233 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001234 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001235 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001236 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001238 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001239
Johannes Weiner00501b52014-08-08 14:19:20 -07001240 if (error) {
1241 if (error != -ENOMEM)
1242 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001243 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001244 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001245 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001246out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001247 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001248 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001249 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252/*
1253 * Move the page from the page cache to the swap cache.
1254 */
1255static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1256{
1257 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001260 swp_entry_t swap;
1261 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001263 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 mapping = page->mapping;
1266 index = page->index;
1267 inode = mapping->host;
1268 info = SHMEM_I(inode);
1269 if (info->flags & VM_LOCKED)
1270 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001271 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 goto redirty;
1273
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001274 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001275 * Our capabilities prevent regular writeback or sync from ever calling
1276 * shmem_writepage; but a stacking filesystem might use ->writepage of
1277 * its underlying filesystem, in which case tmpfs should write out to
1278 * swap only in response to memory pressure, and not for the writeback
1279 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001280 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001281 if (!wbc->for_reclaim) {
1282 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1283 goto redirty;
1284 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001285
1286 /*
1287 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1288 * value into swapfile.c, the only way we can correctly account for a
1289 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001290 *
1291 * That's okay for a page already fallocated earlier, but if we have
1292 * not yet completed the fallocation, then (a) we want to keep track
1293 * of this page in case we have to undo it, and (b) it may not be a
1294 * good idea to continue anyway, once we're pushing into swap. So
1295 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001296 */
1297 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001298 if (inode->i_private) {
1299 struct shmem_falloc *shmem_falloc;
1300 spin_lock(&inode->i_lock);
1301 shmem_falloc = inode->i_private;
1302 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001303 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001304 index >= shmem_falloc->start &&
1305 index < shmem_falloc->next)
1306 shmem_falloc->nr_unswapped++;
1307 else
1308 shmem_falloc = NULL;
1309 spin_unlock(&inode->i_lock);
1310 if (shmem_falloc)
1311 goto redirty;
1312 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001313 clear_highpage(page);
1314 flush_dcache_page(page);
1315 SetPageUptodate(page);
1316 }
1317
Huang Ying38d8b4e2017-07-06 15:37:18 -07001318 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001319 if (!swap.val)
1320 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001321
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001322 /*
1323 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001324 * if it's not already there. Do it now before the page is
1325 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001326 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001327 * we've incremented swapped, because shmem_unuse_inode() will
1328 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001329 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001330 mutex_lock(&shmem_swaplist_mutex);
1331 if (list_empty(&info->swaplist))
1332 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001333
Hugh Dickins48f170f2011-07-25 17:12:37 -07001334 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001335 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001336 shmem_recalc_inode(inode);
1337 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001338 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001339
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001340 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001341 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1342
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001343 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001344 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001345 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return 0;
1347 }
1348
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001349 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001350 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351redirty:
1352 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001353 if (wbc->for_reclaim)
1354 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1355 unlock_page(page);
1356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Hugh Dickins75edd342016-05-19 17:12:44 -07001359#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001360static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001361{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001362 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001363
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001364 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001365 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001366
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001367 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001368
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001369 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001370}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001371
1372static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1373{
1374 struct mempolicy *mpol = NULL;
1375 if (sbinfo->mpol) {
1376 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1377 mpol = sbinfo->mpol;
1378 mpol_get(mpol);
1379 spin_unlock(&sbinfo->stat_lock);
1380 }
1381 return mpol;
1382}
Hugh Dickins75edd342016-05-19 17:12:44 -07001383#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1384static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1385{
1386}
1387static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1388{
1389 return NULL;
1390}
1391#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1392#ifndef CONFIG_NUMA
1393#define vm_policy vm_private_data
1394#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001395
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001396static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1397 struct shmem_inode_info *info, pgoff_t index)
1398{
1399 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001400 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001401 /* Bias interleave by inode number to distribute better across nodes */
1402 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001403 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1404}
1405
1406static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1407{
1408 /* Drop reference taken by mpol_shared_policy_lookup() */
1409 mpol_cond_put(vma->vm_policy);
1410}
1411
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001412static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1413 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001416 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001417 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001419 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001420 vmf.vma = &pvma;
1421 vmf.address = 0;
1422 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001423 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001424
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001425 return page;
1426}
Mel Gorman18a2f372012-12-05 14:01:41 -08001427
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001428static struct page *shmem_alloc_hugepage(gfp_t gfp,
1429 struct shmem_inode_info *info, pgoff_t index)
1430{
1431 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001432 struct address_space *mapping = info->vfs_inode.i_mapping;
1433 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001434 struct page *page;
1435
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001436 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001437 return NULL;
1438
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001439 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001440 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1441 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001442 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001443
1444 shmem_pseudo_vma_init(&pvma, info, hindex);
1445 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
David Rientjes356ff8a2018-12-07 14:50:16 -08001446 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001447 shmem_pseudo_vma_destroy(&pvma);
1448 if (page)
1449 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001450 return page;
1451}
1452
1453static struct page *shmem_alloc_page(gfp_t gfp,
1454 struct shmem_inode_info *info, pgoff_t index)
1455{
1456 struct vm_area_struct pvma;
1457 struct page *page;
1458
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001459 shmem_pseudo_vma_init(&pvma, info, index);
1460 page = alloc_page_vma(gfp, &pvma, 0);
1461 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001462
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001463 return page;
1464}
1465
1466static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001467 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001468 pgoff_t index, bool huge)
1469{
Mike Rapoport0f079692017-09-06 16:22:59 -07001470 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001471 struct page *page;
1472 int nr;
1473 int err = -ENOSPC;
1474
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001475 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001476 huge = false;
1477 nr = huge ? HPAGE_PMD_NR : 1;
1478
Mike Rapoport0f079692017-09-06 16:22:59 -07001479 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001480 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001481
1482 if (huge)
1483 page = shmem_alloc_hugepage(gfp, info, index);
1484 else
1485 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001486 if (page) {
1487 __SetPageLocked(page);
1488 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001489 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001490 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001491
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001492 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001493 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001494failed:
1495 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001499 * When a page is moved from swapcache to shmem filecache (either by the
1500 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1501 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1502 * ignorance of the mapping it belongs to. If that mapping has special
1503 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1504 * we may need to copy to a suitable page before moving to filecache.
1505 *
1506 * In a future release, this may well be extended to respect cpuset and
1507 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1508 * but for now it is a simple matter of zone.
1509 */
1510static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1511{
1512 return page_zonenum(page) > gfp_zone(gfp);
1513}
1514
1515static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1516 struct shmem_inode_info *info, pgoff_t index)
1517{
1518 struct page *oldpage, *newpage;
1519 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001520 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001521 pgoff_t swap_index;
1522 int error;
1523
1524 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001525 entry.val = page_private(oldpage);
1526 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001527 swap_mapping = page_mapping(oldpage);
1528
1529 /*
1530 * We have arrived here because our zones are constrained, so don't
1531 * limit chance of success by further cpuset and node constraints.
1532 */
1533 gfp &= ~GFP_CONSTRAINT_MASK;
1534 newpage = shmem_alloc_page(gfp, info, index);
1535 if (!newpage)
1536 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001537
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001538 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001539 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001540 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001541
Hugh Dickins9956edf2016-11-10 10:46:11 -08001542 __SetPageLocked(newpage);
1543 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001544 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001545 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001546 SetPageSwapCache(newpage);
1547
1548 /*
1549 * Our caller will very soon move newpage out of swapcache, but it's
1550 * a nice clean interface for us to replace oldpage by newpage there.
1551 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001552 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001553 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001554 if (!error) {
Mel Gorman11fb9982016-07-28 15:46:20 -07001555 __inc_node_page_state(newpage, NR_FILE_PAGES);
1556 __dec_node_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001557 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001558 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001559
Hugh Dickins0142ef62012-06-07 14:21:09 -07001560 if (unlikely(error)) {
1561 /*
1562 * Is this possible? I think not, now that our callers check
1563 * both PageSwapCache and page_private after getting page lock;
1564 * but be defensive. Reverse old to newpage for clear and free.
1565 */
1566 oldpage = newpage;
1567 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001568 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001569 lru_cache_add_anon(newpage);
1570 *pagep = newpage;
1571 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001572
1573 ClearPageSwapCache(oldpage);
1574 set_page_private(oldpage, 0);
1575
1576 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001577 put_page(oldpage);
1578 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001579 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001580}
1581
1582/*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001583 * Swap in the page pointed to by *pagep.
1584 * Caller has to make sure that *pagep contains a valid swapped page.
1585 * Returns 0 and the page in pagep if success. On failure, returns the
1586 * the error code and NULL in *pagep.
1587 */
1588static int shmem_swapin_page(struct inode *inode, pgoff_t index,
1589 struct page **pagep, enum sgp_type sgp,
1590 gfp_t gfp, struct vm_area_struct *vma,
1591 vm_fault_t *fault_type)
1592{
1593 struct address_space *mapping = inode->i_mapping;
1594 struct shmem_inode_info *info = SHMEM_I(inode);
1595 struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
1596 struct mem_cgroup *memcg;
1597 struct page *page;
1598 swp_entry_t swap;
1599 int error;
1600
1601 VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
1602 swap = radix_to_swp_entry(*pagep);
1603 *pagep = NULL;
1604
1605 /* Look it up and read it in.. */
1606 page = lookup_swap_cache(swap, NULL, 0);
1607 if (!page) {
1608 /* Or update major stats only when swapin succeeds?? */
1609 if (fault_type) {
1610 *fault_type |= VM_FAULT_MAJOR;
1611 count_vm_event(PGMAJFAULT);
1612 count_memcg_event_mm(charge_mm, PGMAJFAULT);
1613 }
1614 /* Here we actually start the io */
1615 page = shmem_swapin(swap, gfp, info, index);
1616 if (!page) {
1617 error = -ENOMEM;
1618 goto failed;
1619 }
1620 }
1621
1622 /* We have to do this with page locked to prevent races */
1623 lock_page(page);
1624 if (!PageSwapCache(page) || page_private(page) != swap.val ||
1625 !shmem_confirm_swap(mapping, index, swap)) {
1626 error = -EEXIST;
1627 goto unlock;
1628 }
1629 if (!PageUptodate(page)) {
1630 error = -EIO;
1631 goto failed;
1632 }
1633 wait_on_page_writeback(page);
1634
1635 if (shmem_should_replace_page(page, gfp)) {
1636 error = shmem_replace_page(&page, gfp, info, index);
1637 if (error)
1638 goto failed;
1639 }
1640
1641 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
1642 false);
1643 if (!error) {
1644 error = shmem_add_to_page_cache(page, mapping, index,
1645 swp_to_radix_entry(swap), gfp);
1646 /*
1647 * We already confirmed swap under page lock, and make
1648 * no memory allocation here, so usually no possibility
1649 * of error; but free_swap_and_cache() only trylocks a
1650 * page, so it is just possible that the entry has been
1651 * truncated or holepunched since swap was confirmed.
1652 * shmem_undo_range() will have done some of the
1653 * unaccounting, now delete_from_swap_cache() will do
1654 * the rest.
1655 */
1656 if (error) {
1657 mem_cgroup_cancel_charge(page, memcg, false);
1658 delete_from_swap_cache(page);
1659 }
1660 }
1661 if (error)
1662 goto failed;
1663
1664 mem_cgroup_commit_charge(page, memcg, true, false);
1665
1666 spin_lock_irq(&info->lock);
1667 info->swapped--;
1668 shmem_recalc_inode(inode);
1669 spin_unlock_irq(&info->lock);
1670
1671 if (sgp == SGP_WRITE)
1672 mark_page_accessed(page);
1673
1674 delete_from_swap_cache(page);
1675 set_page_dirty(page);
1676 swap_free(swap);
1677
1678 *pagep = page;
1679 return 0;
1680failed:
1681 if (!shmem_confirm_swap(mapping, index, swap))
1682 error = -EEXIST;
1683unlock:
1684 if (page) {
1685 unlock_page(page);
1686 put_page(page);
1687 }
1688
1689 return error;
1690}
1691
1692/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001693 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 *
1695 * If we allocate a new one we do not mark it dirty. That's up to the
1696 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001697 * entry since a page cannot live in both the swap and page cache.
1698 *
1699 * fault_mm and fault_type are only supplied by shmem_fault:
1700 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001702static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001703 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001704 struct vm_area_struct *vma, struct vm_fault *vmf,
1705 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001708 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001710 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001711 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001712 struct page *page;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001713 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001714 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001716 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001717 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001719 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001721 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1722 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723repeat:
Hugh Dickins75edd342016-05-19 17:12:44 -07001724 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001725 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001726 return -EINVAL;
1727 }
1728
1729 sbinfo = SHMEM_SB(inode->i_sb);
1730 charge_mm = vma ? vma->vm_mm : current->mm;
1731
1732 page = find_lock_entry(mapping, index);
1733 if (xa_is_value(page)) {
1734 error = shmem_swapin_page(inode, index, &page,
1735 sgp, gfp, vma, fault_type);
1736 if (error == -EEXIST)
1737 goto repeat;
1738
1739 *pagep = page;
1740 return error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001741 }
1742
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001743 if (page && sgp == SGP_WRITE)
1744 mark_page_accessed(page);
1745
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001746 /* fallocated page? */
1747 if (page && !PageUptodate(page)) {
1748 if (sgp != SGP_READ)
1749 goto clear;
1750 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001751 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001752 page = NULL;
1753 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001754 if (page || sgp == SGP_READ) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001755 *pagep = page;
1756 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001760 * Fast cache lookup did not find it:
1761 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001763
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001764 if (vma && userfaultfd_missing(vma)) {
1765 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1766 return 0;
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001769 /* shmem_symlink() */
1770 if (mapping->a_ops != &shmem_aops)
1771 goto alloc_nohuge;
1772 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1773 goto alloc_nohuge;
1774 if (shmem_huge == SHMEM_HUGE_FORCE)
1775 goto alloc_huge;
1776 switch (sbinfo->huge) {
1777 loff_t i_size;
1778 pgoff_t off;
1779 case SHMEM_HUGE_NEVER:
1780 goto alloc_nohuge;
1781 case SHMEM_HUGE_WITHIN_SIZE:
1782 off = round_up(index, HPAGE_PMD_NR);
1783 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1784 if (i_size >= HPAGE_PMD_SIZE &&
1785 i_size >> PAGE_SHIFT >= off)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001786 goto alloc_huge;
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001787 /* fallthrough */
1788 case SHMEM_HUGE_ADVISE:
1789 if (sgp_huge == SGP_HUGE)
1790 goto alloc_huge;
1791 /* TODO: implement fadvise() hints */
1792 goto alloc_nohuge;
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001795alloc_huge:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001796 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
1797 if (IS_ERR(page)) {
1798alloc_nohuge:
1799 page = shmem_alloc_and_acct_page(gfp, inode,
1800 index, false);
1801 }
1802 if (IS_ERR(page)) {
1803 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001804
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001805 error = PTR_ERR(page);
1806 page = NULL;
1807 if (error != -ENOSPC)
1808 goto unlock;
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001809 /*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001810 * Try to reclaim some space by splitting a huge page
1811 * beyond i_size on the filesystem.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001812 */
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001813 while (retry--) {
1814 int ret;
1815
1816 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1817 if (ret == SHRINK_STOP)
1818 break;
1819 if (ret)
1820 goto alloc_nohuge;
1821 }
1822 goto unlock;
1823 }
1824
1825 if (PageTransHuge(page))
1826 hindex = round_down(index, HPAGE_PMD_NR);
1827 else
1828 hindex = index;
1829
1830 if (sgp == SGP_WRITE)
1831 __SetPageReferenced(page);
1832
1833 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
1834 PageTransHuge(page));
1835 if (error)
1836 goto unacct;
1837 error = shmem_add_to_page_cache(page, mapping, hindex,
1838 NULL, gfp & GFP_RECLAIM_MASK);
1839 if (error) {
1840 mem_cgroup_cancel_charge(page, memcg,
1841 PageTransHuge(page));
1842 goto unacct;
1843 }
1844 mem_cgroup_commit_charge(page, memcg, false,
1845 PageTransHuge(page));
1846 lru_cache_add_anon(page);
1847
1848 spin_lock_irq(&info->lock);
1849 info->alloced += 1 << compound_order(page);
1850 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
1851 shmem_recalc_inode(inode);
1852 spin_unlock_irq(&info->lock);
1853 alloced = true;
1854
1855 if (PageTransHuge(page) &&
1856 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1857 hindex + HPAGE_PMD_NR - 1) {
1858 /*
1859 * Part of the huge page is beyond i_size: subject
1860 * to shrink under memory pressure.
1861 */
1862 spin_lock(&sbinfo->shrinklist_lock);
1863 /*
1864 * _careful to defend against unlocked access to
1865 * ->shrink_list in shmem_unused_huge_shrink()
1866 */
1867 if (list_empty_careful(&info->shrinklist)) {
1868 list_add_tail(&info->shrinklist,
1869 &sbinfo->shrinklist);
1870 sbinfo->shrinklist_len++;
1871 }
1872 spin_unlock(&sbinfo->shrinklist_lock);
1873 }
1874
1875 /*
1876 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1877 */
1878 if (sgp == SGP_FALLOC)
1879 sgp = SGP_WRITE;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001880clear:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001881 /*
1882 * Let SGP_WRITE caller clear ends if write does not fill page;
1883 * but SGP_FALLOC on a page fallocated earlier must initialize
1884 * it now, lest undo on failure cancel our earlier guarantee.
1885 */
1886 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1887 struct page *head = compound_head(page);
1888 int i;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001889
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001890 for (i = 0; i < (1 << compound_order(head)); i++) {
1891 clear_highpage(head + i);
1892 flush_dcache_page(head + i);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001893 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001894 SetPageUptodate(head);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001895 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001896
Hugh Dickins54af60422011-08-03 16:21:24 -07001897 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001898 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001899 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001900 if (alloced) {
1901 ClearPageDirty(page);
1902 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001903 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001904 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001905 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001906 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001907 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001908 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001909 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001910 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001911 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001912
Nick Piggind0217ac2007-07-19 01:47:03 -07001913 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001914 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001916unacct:
Mike Rapoport0f079692017-09-06 16:22:59 -07001917 shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001918
1919 if (PageTransHuge(page)) {
1920 unlock_page(page);
1921 put_page(page);
1922 goto alloc_nohuge;
1923 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001924unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001925 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001926 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001927 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001928 }
1929 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001930 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001931 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001932 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001933 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001934 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001935 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07001936 goto repeat;
1937 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001940/*
1941 * This is like autoremove_wake_function, but it removes the wait queue
1942 * entry unconditionally - even if something else had already woken the
1943 * target.
1944 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001945static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001946{
1947 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001948 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001949 return ret;
1950}
1951
Souptick Joarder20acce62018-06-07 17:09:17 -07001952static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Dave Jiang11bac802017-02-24 14:56:41 -08001954 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001955 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001956 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001957 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07001958 int err;
1959 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001961 /*
1962 * Trinity finds that probing a hole which tmpfs is punching can
1963 * prevent the hole-punch from ever completing: which in turn
1964 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001965 * faulting pages into the hole while it's being punched. Although
1966 * shmem_undo_range() does remove the additions, it may be unable to
1967 * keep up, as each new page needs its own unmap_mapping_range() call,
1968 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1969 *
1970 * It does not matter if we sometimes reach this check just before the
1971 * hole-punch begins, so that one fault then races with the punch:
1972 * we just need to make racing faults a rare case.
1973 *
1974 * The implementation below would be much simpler if we just used a
1975 * standard mutex or completion: but we cannot take i_mutex in fault,
1976 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001977 */
1978 if (unlikely(inode->i_private)) {
1979 struct shmem_falloc *shmem_falloc;
1980
1981 spin_lock(&inode->i_lock);
1982 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001983 if (shmem_falloc &&
1984 shmem_falloc->waitq &&
1985 vmf->pgoff >= shmem_falloc->start &&
1986 vmf->pgoff < shmem_falloc->next) {
1987 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001988 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001989
1990 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001991 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1992 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001993 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001994 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001995 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001996 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001997
1998 shmem_falloc_waitq = shmem_falloc->waitq;
1999 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
2000 TASK_UNINTERRUPTIBLE);
2001 spin_unlock(&inode->i_lock);
2002 schedule();
2003
2004 /*
2005 * shmem_falloc_waitq points into the shmem_fallocate()
2006 * stack of the hole-punching task: shmem_falloc_waitq
2007 * is usually invalid by the time we reach here, but
2008 * finish_wait() does not dereference it in that case;
2009 * though i_lock needed lest racing with wake_up_all().
2010 */
2011 spin_lock(&inode->i_lock);
2012 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2013 spin_unlock(&inode->i_lock);
2014 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002015 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002016 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002017 }
2018
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002019 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002020
2021 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2022 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002023 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002024 else if (vma->vm_flags & VM_HUGEPAGE)
2025 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002026
Souptick Joarder20acce62018-06-07 17:09:17 -07002027 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002028 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002029 if (err)
2030 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002031 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002034unsigned long shmem_get_unmapped_area(struct file *file,
2035 unsigned long uaddr, unsigned long len,
2036 unsigned long pgoff, unsigned long flags)
2037{
2038 unsigned long (*get_area)(struct file *,
2039 unsigned long, unsigned long, unsigned long, unsigned long);
2040 unsigned long addr;
2041 unsigned long offset;
2042 unsigned long inflated_len;
2043 unsigned long inflated_addr;
2044 unsigned long inflated_offset;
2045
2046 if (len > TASK_SIZE)
2047 return -ENOMEM;
2048
2049 get_area = current->mm->get_unmapped_area;
2050 addr = get_area(file, uaddr, len, pgoff, flags);
2051
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002052 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002053 return addr;
2054 if (IS_ERR_VALUE(addr))
2055 return addr;
2056 if (addr & ~PAGE_MASK)
2057 return addr;
2058 if (addr > TASK_SIZE - len)
2059 return addr;
2060
2061 if (shmem_huge == SHMEM_HUGE_DENY)
2062 return addr;
2063 if (len < HPAGE_PMD_SIZE)
2064 return addr;
2065 if (flags & MAP_FIXED)
2066 return addr;
2067 /*
2068 * Our priority is to support MAP_SHARED mapped hugely;
2069 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
2070 * But if caller specified an address hint, respect that as before.
2071 */
2072 if (uaddr)
2073 return addr;
2074
2075 if (shmem_huge != SHMEM_HUGE_FORCE) {
2076 struct super_block *sb;
2077
2078 if (file) {
2079 VM_BUG_ON(file->f_op != &shmem_file_operations);
2080 sb = file_inode(file)->i_sb;
2081 } else {
2082 /*
2083 * Called directly from mm/mmap.c, or drivers/char/mem.c
2084 * for "/dev/zero", to create a shared anonymous object.
2085 */
2086 if (IS_ERR(shm_mnt))
2087 return addr;
2088 sb = shm_mnt->mnt_sb;
2089 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002090 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002091 return addr;
2092 }
2093
2094 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2095 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2096 return addr;
2097 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2098 return addr;
2099
2100 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2101 if (inflated_len > TASK_SIZE)
2102 return addr;
2103 if (inflated_len < len)
2104 return addr;
2105
2106 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
2107 if (IS_ERR_VALUE(inflated_addr))
2108 return addr;
2109 if (inflated_addr & ~PAGE_MASK)
2110 return addr;
2111
2112 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2113 inflated_addr += offset - inflated_offset;
2114 if (inflated_offset > offset)
2115 inflated_addr += HPAGE_PMD_SIZE;
2116
2117 if (inflated_addr > TASK_SIZE - len)
2118 return addr;
2119 return inflated_addr;
2120}
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002123static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Al Viro496ad9a2013-01-23 17:07:38 -05002125 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002126 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002129static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2130 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Al Viro496ad9a2013-01-23 17:07:38 -05002132 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002133 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002135 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2136 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138#endif
2139
2140int shmem_lock(struct file *file, int lock, struct user_struct *user)
2141{
Al Viro496ad9a2013-01-23 17:07:38 -05002142 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct shmem_inode_info *info = SHMEM_I(inode);
2144 int retval = -ENOMEM;
2145
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002146 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (lock && !(info->flags & VM_LOCKED)) {
2148 if (!user_shm_lock(inode->i_size, user))
2149 goto out_nomem;
2150 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002151 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153 if (!lock && (info->flags & VM_LOCKED) && user) {
2154 user_shm_unlock(inode->i_size, user);
2155 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002156 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160out_nomem:
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002161 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return retval;
2163}
2164
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002165static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 file_accessed(file);
2168 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002169 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002170 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2171 (vma->vm_end & HPAGE_PMD_MASK)) {
2172 khugepaged_enter(vma, vma->vm_flags);
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return 0;
2175}
2176
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002177static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002178 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct inode *inode;
2181 struct shmem_inode_info *info;
2182 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2183
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002184 if (shmem_reserve_inode(sb))
2185 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 inode = new_inode(sb);
2188 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002189 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002190 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002192 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002193 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 info = SHMEM_I(inode);
2195 memset(info, 0, (char *)inode - (char *)info);
2196 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07002197 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002198 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002199 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002201 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002202 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 switch (mode & S_IFMT) {
2205 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002206 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 init_special_inode(inode, mode, dev);
2208 break;
2209 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002210 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 inode->i_op = &shmem_inode_operations;
2212 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002213 mpol_shared_policy_init(&info->policy,
2214 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 break;
2216 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002217 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /* Some things misbehave if size == 0 on a directory */
2219 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2220 inode->i_op = &shmem_dir_inode_operations;
2221 inode->i_fop = &simple_dir_operations;
2222 break;
2223 case S_IFLNK:
2224 /*
2225 * Must not load anything in the rbtree,
2226 * mpol_free_shared_policy will not be called.
2227 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002228 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
2230 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002231
2232 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002233 } else
2234 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return inode;
2236}
2237
Johannes Weiner0cd61442014-04-03 14:47:46 -07002238bool shmem_mapping(struct address_space *mapping)
2239{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002240 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002241}
2242
Mike Rapoport8d103962017-09-06 16:23:02 -07002243static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2244 pmd_t *dst_pmd,
2245 struct vm_area_struct *dst_vma,
2246 unsigned long dst_addr,
2247 unsigned long src_addr,
2248 bool zeropage,
2249 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002250{
2251 struct inode *inode = file_inode(dst_vma->vm_file);
2252 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002253 struct address_space *mapping = inode->i_mapping;
2254 gfp_t gfp = mapping_gfp_mask(mapping);
2255 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2256 struct mem_cgroup *memcg;
2257 spinlock_t *ptl;
2258 void *page_kaddr;
2259 struct page *page;
2260 pte_t _dst_pte, *dst_pte;
2261 int ret;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002262 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002263
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002264 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002265 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002266 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002267
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002268 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002269 page = shmem_alloc_page(gfp, info, pgoff);
2270 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002271 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002272
Mike Rapoport8d103962017-09-06 16:23:02 -07002273 if (!zeropage) { /* mcopy_atomic */
2274 page_kaddr = kmap_atomic(page);
2275 ret = copy_from_user(page_kaddr,
2276 (const void __user *)src_addr,
2277 PAGE_SIZE);
2278 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002279
Mike Rapoport8d103962017-09-06 16:23:02 -07002280 /* fallback to copy_from_user outside mmap_sem */
2281 if (unlikely(ret)) {
2282 *pagep = page;
2283 shmem_inode_unacct_blocks(inode, 1);
2284 /* don't free the page */
Andrea Arcangeli9e368252018-11-30 14:09:25 -08002285 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002286 }
2287 } else { /* mfill_zeropage_atomic */
2288 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002289 }
2290 } else {
2291 page = *pagep;
2292 *pagep = NULL;
2293 }
2294
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002295 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2296 __SetPageLocked(page);
2297 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002298 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002299
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002300 ret = -EFAULT;
2301 offset = linear_page_index(dst_vma, dst_addr);
2302 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2303 if (unlikely(offset >= max_off))
2304 goto out_release;
2305
Tejun Heo2cf85582018-07-03 11:14:56 -04002306 ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002307 if (ret)
2308 goto out_release;
2309
Matthew Wilcox552446a2017-12-01 13:25:14 -05002310 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2311 gfp & GFP_RECLAIM_MASK);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002312 if (ret)
2313 goto out_release_uncharge;
2314
2315 mem_cgroup_commit_charge(page, memcg, false, false);
2316
2317 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2318 if (dst_vma->vm_flags & VM_WRITE)
2319 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002320 else {
2321 /*
2322 * We don't set the pte dirty if the vma has no
2323 * VM_WRITE permission, so mark the page dirty or it
2324 * could be freed from under us. We could do it
2325 * unconditionally before unlock_page(), but doing it
2326 * only if VM_WRITE is not set is faster.
2327 */
2328 set_page_dirty(page);
2329 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002330
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002331 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002332
2333 ret = -EFAULT;
2334 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2335 if (unlikely(offset >= max_off))
2336 goto out_release_uncharge_unlock;
2337
2338 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002339 if (!pte_none(*dst_pte))
2340 goto out_release_uncharge_unlock;
2341
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002342 lru_cache_add_anon(page);
2343
2344 spin_lock(&info->lock);
2345 info->alloced++;
2346 inode->i_blocks += BLOCKS_PER_PAGE;
2347 shmem_recalc_inode(inode);
2348 spin_unlock(&info->lock);
2349
2350 inc_mm_counter(dst_mm, mm_counter_file(page));
2351 page_add_file_rmap(page, false);
2352 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2353
2354 /* No need to invalidate - it was non-present before */
2355 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002356 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002357 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002358 ret = 0;
2359out:
2360 return ret;
2361out_release_uncharge_unlock:
2362 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002363 ClearPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002364 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002365out_release_uncharge:
2366 mem_cgroup_cancel_charge(page, memcg, false);
2367out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002368 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002369 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002370out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002371 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002372 goto out;
2373}
2374
Mike Rapoport8d103962017-09-06 16:23:02 -07002375int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2376 pmd_t *dst_pmd,
2377 struct vm_area_struct *dst_vma,
2378 unsigned long dst_addr,
2379 unsigned long src_addr,
2380 struct page **pagep)
2381{
2382 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2383 dst_addr, src_addr, false, pagep);
2384}
2385
2386int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2387 pmd_t *dst_pmd,
2388 struct vm_area_struct *dst_vma,
2389 unsigned long dst_addr)
2390{
2391 struct page *page = NULL;
2392
2393 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2394 dst_addr, 0, true, &page);
2395}
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002398static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002399static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002401#ifdef CONFIG_TMPFS_XATTR
2402static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2403#else
2404#define shmem_initxattrs NULL
2405#endif
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002408shmem_write_begin(struct file *file, struct address_space *mapping,
2409 loff_t pos, unsigned len, unsigned flags,
2410 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Nick Piggin800d15a2007-10-16 01:25:03 -07002412 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002413 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002414 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002415
2416 /* i_mutex is held by caller */
Steven Rostedt (VMware)3f472cc2017-02-24 14:59:10 -08002417 if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
David Herrmann40e041a2014-08-08 14:25:27 -07002418 if (info->seals & F_SEAL_WRITE)
2419 return -EPERM;
2420 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2421 return -EPERM;
2422 }
2423
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002424 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002425}
2426
2427static int
2428shmem_write_end(struct file *file, struct address_space *mapping,
2429 loff_t pos, unsigned len, unsigned copied,
2430 struct page *page, void *fsdata)
2431{
2432 struct inode *inode = mapping->host;
2433
Hugh Dickinsd3602442008-02-04 22:28:44 -08002434 if (pos + copied > inode->i_size)
2435 i_size_write(inode, pos + copied);
2436
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002437 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002438 struct page *head = compound_head(page);
2439 if (PageTransCompound(page)) {
2440 int i;
2441
2442 for (i = 0; i < HPAGE_PMD_NR; i++) {
2443 if (head + i == page)
2444 continue;
2445 clear_highpage(head + i);
2446 flush_dcache_page(head + i);
2447 }
2448 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002449 if (copied < PAGE_SIZE) {
2450 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002451 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002452 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002453 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002454 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002455 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002456 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002457 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002458 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002459
Nick Piggin800d15a2007-10-16 01:25:03 -07002460 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Al Viro2ba5bbe2014-04-02 20:00:02 -04002463static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Al Viro6e58e792014-02-03 17:07:03 -05002465 struct file *file = iocb->ki_filp;
2466 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002468 pgoff_t index;
2469 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002470 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002471 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002472 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002473 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002474
2475 /*
2476 * Might this read be for a stacking filesystem? Then when reading
2477 * holes of a sparse file, we actually need to allocate those pages,
2478 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2479 */
Al Viro777eda22014-12-17 04:46:46 -05002480 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002481 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002483 index = *ppos >> PAGE_SHIFT;
2484 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 for (;;) {
2487 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002488 pgoff_t end_index;
2489 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 loff_t i_size = i_size_read(inode);
2491
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002492 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (index > end_index)
2494 break;
2495 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002496 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (nr <= offset)
2498 break;
2499 }
2500
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002501 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002502 if (error) {
2503 if (error == -EINVAL)
2504 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 break;
2506 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002507 if (page) {
2508 if (sgp == SGP_CACHE)
2509 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002510 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 /*
2514 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002515 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002517 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002519 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002521 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (nr <= offset) {
2523 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002524 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 break;
2526 }
2527 }
2528 nr -= offset;
2529
2530 if (page) {
2531 /*
2532 * If users can be writing to this page using arbitrary
2533 * virtual addresses, take care about potential aliasing
2534 * before reading the page on the kernel side.
2535 */
2536 if (mapping_writably_mapped(mapping))
2537 flush_dcache_page(page);
2538 /*
2539 * Mark the page accessed if we read the beginning.
2540 */
2541 if (!offset)
2542 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002543 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002545 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 /*
2549 * Ok, we have the page, and it's up-to-date, so
2550 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002552 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002553 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002555 index += offset >> PAGE_SHIFT;
2556 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002558 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002559 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 break;
Al Viro6e58e792014-02-03 17:07:03 -05002561 if (ret < nr) {
2562 error = -EFAULT;
2563 break;
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 cond_resched();
2566 }
2567
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002568 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002569 file_accessed(file);
2570 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002573/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002574 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002575 */
2576static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002577 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002578{
2579 struct page *page;
2580 struct pagevec pvec;
2581 pgoff_t indices[PAGEVEC_SIZE];
2582 bool done = false;
2583 int i;
2584
Mel Gorman86679822017-11-15 17:37:52 -08002585 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002586 pvec.nr = 1; /* start small: we may be there already */
2587 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002588 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002589 pvec.nr, pvec.pages, indices);
2590 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002591 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002592 index = end;
2593 break;
2594 }
2595 for (i = 0; i < pvec.nr; i++, index++) {
2596 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002597 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002598 done = true;
2599 break;
2600 }
2601 index = indices[i];
2602 }
2603 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002604 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002605 if (!PageUptodate(page))
2606 page = NULL;
2607 }
2608 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002609 (page && whence == SEEK_DATA) ||
2610 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002611 done = true;
2612 break;
2613 }
2614 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002615 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002616 pagevec_release(&pvec);
2617 pvec.nr = PAGEVEC_SIZE;
2618 cond_resched();
2619 }
2620 return index;
2621}
2622
Andrew Morton965c8e52012-12-17 15:59:39 -08002623static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002624{
2625 struct address_space *mapping = file->f_mapping;
2626 struct inode *inode = mapping->host;
2627 pgoff_t start, end;
2628 loff_t new_offset;
2629
Andrew Morton965c8e52012-12-17 15:59:39 -08002630 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2631 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002632 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002633 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002634 /* We're holding i_mutex so we can access i_size directly */
2635
Yufen Yu1a413642018-11-16 15:08:39 -08002636 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002637 offset = -ENXIO;
2638 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002639 start = offset >> PAGE_SHIFT;
2640 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002641 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002642 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002643 if (new_offset > offset) {
2644 if (new_offset < inode->i_size)
2645 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002646 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002647 offset = -ENXIO;
2648 else
2649 offset = inode->i_size;
2650 }
2651 }
2652
Hugh Dickins387aae62013-08-04 11:30:25 -07002653 if (offset >= 0)
2654 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002655 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002656 return offset;
2657}
2658
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002659static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2660 loff_t len)
2661{
Al Viro496ad9a2013-01-23 17:07:38 -05002662 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002663 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002664 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002665 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002666 pgoff_t start, index, end;
2667 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002668
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002669 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2670 return -EOPNOTSUPP;
2671
Al Viro59551022016-01-22 15:40:57 -05002672 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002673
2674 if (mode & FALLOC_FL_PUNCH_HOLE) {
2675 struct address_space *mapping = file->f_mapping;
2676 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2677 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002678 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002679
David Herrmann40e041a2014-08-08 14:25:27 -07002680 /* protected by i_mutex */
2681 if (info->seals & F_SEAL_WRITE) {
2682 error = -EPERM;
2683 goto out;
2684 }
2685
Hugh Dickins8e205f72014-07-23 14:00:10 -07002686 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002687 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2688 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2689 spin_lock(&inode->i_lock);
2690 inode->i_private = &shmem_falloc;
2691 spin_unlock(&inode->i_lock);
2692
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002693 if ((u64)unmap_end > (u64)unmap_start)
2694 unmap_mapping_range(mapping, unmap_start,
2695 1 + unmap_end - unmap_start, 0);
2696 shmem_truncate_range(inode, offset, offset + len - 1);
2697 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002698
2699 spin_lock(&inode->i_lock);
2700 inode->i_private = NULL;
2701 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002702 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002703 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002704 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002705 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002706 }
2707
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002708 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2709 error = inode_newsize_ok(inode, offset + len);
2710 if (error)
2711 goto out;
2712
David Herrmann40e041a2014-08-08 14:25:27 -07002713 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2714 error = -EPERM;
2715 goto out;
2716 }
2717
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002718 start = offset >> PAGE_SHIFT;
2719 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002720 /* Try to avoid a swapstorm if len is impossible to satisfy */
2721 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2722 error = -ENOSPC;
2723 goto out;
2724 }
2725
Hugh Dickins8e205f72014-07-23 14:00:10 -07002726 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002727 shmem_falloc.start = start;
2728 shmem_falloc.next = start;
2729 shmem_falloc.nr_falloced = 0;
2730 shmem_falloc.nr_unswapped = 0;
2731 spin_lock(&inode->i_lock);
2732 inode->i_private = &shmem_falloc;
2733 spin_unlock(&inode->i_lock);
2734
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002735 for (index = start; index < end; index++) {
2736 struct page *page;
2737
2738 /*
2739 * Good, the fallocate(2) manpage permits EINTR: we may have
2740 * been interrupted because we are using up too much memory.
2741 */
2742 if (signal_pending(current))
2743 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002744 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2745 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002746 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002747 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002748 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002749 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002750 if (index > start) {
2751 shmem_undo_range(inode,
2752 (loff_t)start << PAGE_SHIFT,
2753 ((loff_t)index << PAGE_SHIFT) - 1, true);
2754 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002755 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002756 }
2757
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002758 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002759 * Inform shmem_writepage() how far we have reached.
2760 * No need for lock or barrier: we have the page lock.
2761 */
2762 shmem_falloc.next++;
2763 if (!PageUptodate(page))
2764 shmem_falloc.nr_falloced++;
2765
2766 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002767 * If !PageUptodate, leave it that way so that freeable pages
2768 * can be recognized if we need to rollback on error later.
2769 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002770 * than free the pages we are allocating (and SGP_CACHE pages
2771 * might still be clean: we now need to mark those dirty too).
2772 */
2773 set_page_dirty(page);
2774 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002775 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002776 cond_resched();
2777 }
2778
2779 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2780 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002781 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002782undone:
2783 spin_lock(&inode->i_lock);
2784 inode->i_private = NULL;
2785 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002786out:
Al Viro59551022016-01-22 15:40:57 -05002787 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002788 return error;
2789}
2790
David Howells726c3342006-06-23 02:02:58 -07002791static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
David Howells726c3342006-06-23 02:02:58 -07002793 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002796 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002798 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002800 buf->f_bavail =
2801 buf->f_bfree = sbinfo->max_blocks -
2802 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002803 }
2804 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 buf->f_files = sbinfo->max_inodes;
2806 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
2808 /* else leave those fields 0 like simple_statfs */
2809 return 0;
2810}
2811
2812/*
2813 * File creation. Allocate an inode, and we're done..
2814 */
2815static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002816shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002818 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 int error = -ENOSPC;
2820
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002821 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002823 error = simple_acl_create(dir, inode);
2824 if (error)
2825 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002826 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002827 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002828 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002829 if (error && error != -EOPNOTSUPP)
2830 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002831
Al Viro718deb62009-12-16 19:35:36 -05002832 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002834 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 d_instantiate(dentry, inode);
2836 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002839out_iput:
2840 iput(inode);
2841 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Al Viro60545d02013-06-07 01:20:27 -04002844static int
2845shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2846{
2847 struct inode *inode;
2848 int error = -ENOSPC;
2849
2850 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2851 if (inode) {
2852 error = security_inode_init_security(inode, dir,
2853 NULL,
2854 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002855 if (error && error != -EOPNOTSUPP)
2856 goto out_iput;
2857 error = simple_acl_create(dir, inode);
2858 if (error)
2859 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002860 d_tmpfile(dentry, inode);
2861 }
2862 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002863out_iput:
2864 iput(inode);
2865 return error;
Al Viro60545d02013-06-07 01:20:27 -04002866}
2867
Al Viro18bb1db2011-07-26 01:41:39 -04002868static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 int error;
2871
2872 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2873 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002874 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return 0;
2876}
2877
Al Viro4acdaf22011-07-26 01:42:34 -04002878static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002879 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
2881 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2882}
2883
2884/*
2885 * Link a file..
2886 */
2887static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2888{
David Howells75c3cfa2015-03-17 22:26:12 +00002889 struct inode *inode = d_inode(old_dentry);
Darrick J. Wong29b00e62019-02-22 22:35:32 -08002890 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /*
2893 * No ordinary (disk based) filesystem counts links as inodes;
2894 * but each new link needs a new dentry, pinning lowmem, and
2895 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong1062af92019-02-21 08:48:09 -08002896 * But if an O_TMPFILE file is linked into the tmpfs, the
2897 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 */
Darrick J. Wong1062af92019-02-21 08:48:09 -08002899 if (inode->i_nlink) {
2900 ret = shmem_reserve_inode(inode->i_sb);
2901 if (ret)
2902 goto out;
2903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002906 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002907 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002908 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 dget(dentry); /* Extra pinning count for the created dentry */
2910 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002911out:
2912 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
2915static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2916{
David Howells75c3cfa2015-03-17 22:26:12 +00002917 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002919 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2920 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002923 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002924 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 dput(dentry); /* Undo the count from "create" - this does all the work */
2926 return 0;
2927}
2928
2929static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2930{
2931 if (!simple_empty(dentry))
2932 return -ENOTEMPTY;
2933
David Howells75c3cfa2015-03-17 22:26:12 +00002934 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002935 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return shmem_unlink(dir, dentry);
2937}
2938
Miklos Szeredi37456772014-07-23 15:15:34 +02002939static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2940{
David Howellse36cb0b2015-01-29 12:02:35 +00002941 bool old_is_dir = d_is_dir(old_dentry);
2942 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002943
2944 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2945 if (old_is_dir) {
2946 drop_nlink(old_dir);
2947 inc_nlink(new_dir);
2948 } else {
2949 drop_nlink(new_dir);
2950 inc_nlink(old_dir);
2951 }
2952 }
2953 old_dir->i_ctime = old_dir->i_mtime =
2954 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002955 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002956 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02002957
2958 return 0;
2959}
2960
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002961static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2962{
2963 struct dentry *whiteout;
2964 int error;
2965
2966 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2967 if (!whiteout)
2968 return -ENOMEM;
2969
2970 error = shmem_mknod(old_dir, whiteout,
2971 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2972 dput(whiteout);
2973 if (error)
2974 return error;
2975
2976 /*
2977 * Cheat and hash the whiteout while the old dentry is still in
2978 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2979 *
2980 * d_lookup() will consistently find one of them at this point,
2981 * not sure which one, but that isn't even important.
2982 */
2983 d_rehash(whiteout);
2984 return 0;
2985}
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987/*
2988 * The VFS layer already does all the dentry stuff for rename,
2989 * we just have to decrement the usage count for the target if
2990 * it exists so that the VFS layer correctly free's it when it
2991 * gets overwritten.
2992 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002993static 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 -07002994{
David Howells75c3cfa2015-03-17 22:26:12 +00002995 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 int they_are_dirs = S_ISDIR(inode->i_mode);
2997
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002998 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002999 return -EINVAL;
3000
Miklos Szeredi37456772014-07-23 15:15:34 +02003001 if (flags & RENAME_EXCHANGE)
3002 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (!simple_empty(new_dentry))
3005 return -ENOTEMPTY;
3006
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003007 if (flags & RENAME_WHITEOUT) {
3008 int error;
3009
3010 error = shmem_whiteout(old_dir, old_dentry);
3011 if (error)
3012 return error;
3013 }
3014
David Howells75c3cfa2015-03-17 22:26:12 +00003015 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003017 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003018 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003019 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003022 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003023 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
3025
3026 old_dir->i_size -= BOGO_DIRENT_SIZE;
3027 new_dir->i_size += BOGO_DIRENT_SIZE;
3028 old_dir->i_ctime = old_dir->i_mtime =
3029 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003030 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return 0;
3032}
3033
3034static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3035{
3036 int error;
3037 int len;
3038 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003039 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003042 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return -ENAMETOOLONG;
3044
Joe Perches0825a6f2018-06-14 15:27:58 -07003045 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3046 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (!inode)
3048 return -ENOSPC;
3049
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003050 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003051 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003052 if (error) {
3053 if (error != -EOPNOTSUPP) {
3054 iput(inode);
3055 return error;
3056 }
3057 error = 0;
3058 }
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003061 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003062 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3063 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003064 iput(inode);
3065 return -ENOMEM;
3066 }
3067 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003069 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003070 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (error) {
3072 iput(inode);
3073 return error;
3074 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003075 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003077 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003078 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003080 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003081 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003084 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 d_instantiate(dentry, inode);
3086 dget(dentry);
3087 return 0;
3088}
3089
Al Virofceef392015-12-29 15:58:39 -05003090static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Al Virofceef392015-12-29 15:58:39 -05003092 mark_page_accessed(arg);
3093 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Al Viro6b255392015-11-17 10:20:54 -05003096static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003097 struct inode *inode,
3098 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003101 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003102 if (!dentry) {
3103 page = find_get_page(inode->i_mapping, 0);
3104 if (!page)
3105 return ERR_PTR(-ECHILD);
3106 if (!PageUptodate(page)) {
3107 put_page(page);
3108 return ERR_PTR(-ECHILD);
3109 }
3110 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003111 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003112 if (error)
3113 return ERR_PTR(error);
3114 unlock_page(page);
3115 }
Al Virofceef392015-12-29 15:58:39 -05003116 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003117 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Eric Parisb09e0fa2011-05-24 17:12:39 -07003120#ifdef CONFIG_TMPFS_XATTR
3121/*
3122 * Superblocks without xattr inode operations may get some security.* xattr
3123 * support from the LSM "for free". As soon as we have any other xattrs
3124 * like ACLs, we also need to implement the security.* handlers at
3125 * filesystem level, though.
3126 */
3127
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003128/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003129 * Callback for security_inode_init_security() for acquiring xattrs.
3130 */
3131static int shmem_initxattrs(struct inode *inode,
3132 const struct xattr *xattr_array,
3133 void *fs_info)
3134{
3135 struct shmem_inode_info *info = SHMEM_I(inode);
3136 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003137 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003138 size_t len;
3139
3140 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003141 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003142 if (!new_xattr)
3143 return -ENOMEM;
3144
3145 len = strlen(xattr->name) + 1;
3146 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3147 GFP_KERNEL);
3148 if (!new_xattr->name) {
3149 kfree(new_xattr);
3150 return -ENOMEM;
3151 }
3152
3153 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3154 XATTR_SECURITY_PREFIX_LEN);
3155 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3156 xattr->name, len);
3157
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003158 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003159 }
3160
3161 return 0;
3162}
3163
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003164static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003165 struct dentry *unused, struct inode *inode,
3166 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003167{
Al Virob2968212016-04-10 20:48:24 -04003168 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003169
3170 name = xattr_full_name(handler, name);
3171 return simple_xattr_get(&info->xattrs, name, buffer, size);
3172}
3173
3174static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003175 struct dentry *unused, struct inode *inode,
3176 const char *name, const void *value,
3177 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003178{
Al Viro59301222016-05-27 10:19:30 -04003179 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003180
3181 name = xattr_full_name(handler, name);
3182 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3183}
3184
3185static const struct xattr_handler shmem_security_xattr_handler = {
3186 .prefix = XATTR_SECURITY_PREFIX,
3187 .get = shmem_xattr_handler_get,
3188 .set = shmem_xattr_handler_set,
3189};
3190
3191static const struct xattr_handler shmem_trusted_xattr_handler = {
3192 .prefix = XATTR_TRUSTED_PREFIX,
3193 .get = shmem_xattr_handler_get,
3194 .set = shmem_xattr_handler_set,
3195};
3196
Eric Parisb09e0fa2011-05-24 17:12:39 -07003197static const struct xattr_handler *shmem_xattr_handlers[] = {
3198#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003199 &posix_acl_access_xattr_handler,
3200 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003201#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003202 &shmem_security_xattr_handler,
3203 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003204 NULL
3205};
3206
Eric Parisb09e0fa2011-05-24 17:12:39 -07003207static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3208{
David Howells75c3cfa2015-03-17 22:26:12 +00003209 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003210 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003211}
3212#endif /* CONFIG_TMPFS_XATTR */
3213
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003214static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003215 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003216#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003217 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219};
3220
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003221static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003222 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003223#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003224 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003225#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003226};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003227
David M. Grimes91828a42006-10-17 00:09:45 -07003228static struct dentry *shmem_get_parent(struct dentry *child)
3229{
3230 return ERR_PTR(-ESTALE);
3231}
3232
3233static int shmem_match(struct inode *ino, void *vfh)
3234{
3235 __u32 *fh = vfh;
3236 __u64 inum = fh[2];
3237 inum = (inum << 32) | fh[1];
3238 return ino->i_ino == inum && fh[0] == ino->i_generation;
3239}
3240
Amir Goldstein12ba7802018-06-07 17:07:15 -07003241/* Find any alias of inode, but prefer a hashed alias */
3242static struct dentry *shmem_find_alias(struct inode *inode)
3243{
3244 struct dentry *alias = d_find_alias(inode);
3245
3246 return alias ?: d_find_any_alias(inode);
3247}
3248
3249
Christoph Hellwig480b1162007-10-21 16:42:13 -07003250static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3251 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003252{
David M. Grimes91828a42006-10-17 00:09:45 -07003253 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003254 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003255 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003256
Christoph Hellwig480b1162007-10-21 16:42:13 -07003257 if (fh_len < 3)
3258 return NULL;
3259
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003260 inum = fid->raw[2];
3261 inum = (inum << 32) | fid->raw[1];
3262
Christoph Hellwig480b1162007-10-21 16:42:13 -07003263 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3264 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003265 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003266 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003267 iput(inode);
3268 }
3269
Christoph Hellwig480b1162007-10-21 16:42:13 -07003270 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003271}
3272
Al Virob0b03822012-04-02 14:34:06 -04003273static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3274 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003275{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303276 if (*len < 3) {
3277 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003278 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303279 }
David M. Grimes91828a42006-10-17 00:09:45 -07003280
Al Viro1d3382cb2010-10-23 15:19:20 -04003281 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003282 /* Unfortunately insert_inode_hash is not idempotent,
3283 * so as we hash inodes here rather than at creation
3284 * time, we need a lock to ensure we only try
3285 * to do it once
3286 */
3287 static DEFINE_SPINLOCK(lock);
3288 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003289 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003290 __insert_inode_hash(inode,
3291 inode->i_ino + inode->i_generation);
3292 spin_unlock(&lock);
3293 }
3294
3295 fh[0] = inode->i_generation;
3296 fh[1] = inode->i_ino;
3297 fh[2] = ((__u64)inode->i_ino) >> 32;
3298
3299 *len = 3;
3300 return 1;
3301}
3302
Christoph Hellwig39655162007-10-21 16:42:17 -07003303static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003304 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003305 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003306 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003307};
3308
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003309static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3310 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003313 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003314 uid_t uid;
3315 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003317 while (options != NULL) {
3318 this_char = options;
3319 for (;;) {
3320 /*
3321 * NUL-terminate this option: unfortunately,
3322 * mount options form a comma-separated list,
3323 * but mpol's nodelist may also contain commas.
3324 */
3325 options = strchr(options, ',');
3326 if (options == NULL)
3327 break;
3328 options++;
3329 if (!isdigit(*options)) {
3330 options[-1] = '\0';
3331 break;
3332 }
3333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 if (!*this_char)
3335 continue;
3336 if ((value = strchr(this_char,'=')) != NULL) {
3337 *value++ = 0;
3338 } else {
Joe Perches11705322016-03-17 14:19:50 -07003339 pr_err("tmpfs: No value for mount option '%s'\n",
3340 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003341 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343
3344 if (!strcmp(this_char,"size")) {
3345 unsigned long long size;
3346 size = memparse(value,&rest);
3347 if (*rest == '%') {
3348 size <<= PAGE_SHIFT;
Arun KSca79b0c2018-12-28 00:34:29 -08003349 size *= totalram_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 do_div(size, 100);
3351 rest++;
3352 }
3353 if (*rest)
3354 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003355 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003356 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003358 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (*rest)
3360 goto bad_val;
3361 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003362 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (*rest)
3364 goto bad_val;
3365 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003366 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003368 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (*rest)
3370 goto bad_val;
3371 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003372 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003374 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (*rest)
3376 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003377 sbinfo->uid = make_kuid(current_user_ns(), uid);
3378 if (!uid_valid(sbinfo->uid))
3379 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003381 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003383 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 if (*rest)
3385 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003386 sbinfo->gid = make_kgid(current_user_ns(), gid);
3387 if (!gid_valid(sbinfo->gid))
3388 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003389#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003390 } else if (!strcmp(this_char, "huge")) {
3391 int huge;
3392 huge = shmem_parse_huge(value);
3393 if (huge < 0)
3394 goto bad_val;
3395 if (!has_transparent_hugepage() &&
3396 huge != SHMEM_HUGE_NEVER)
3397 goto bad_val;
3398 sbinfo->huge = huge;
3399#endif
3400#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003401 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003402 mpol_put(mpol);
3403 mpol = NULL;
3404 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003405 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003406#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 } else {
Joe Perches11705322016-03-17 14:19:50 -07003408 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003409 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 }
3411 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003412 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
3414
3415bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003416 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003418error:
3419 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return 1;
3421
3422}
3423
3424static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3425{
3426 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003427 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003428 unsigned long inodes;
3429 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Greg Thelen5f001102013-02-22 16:36:01 -08003431 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003432 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003433 return error;
3434
3435 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003436 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003437 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003438 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003439 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003440 goto out;
3441 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003442 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003443 * but we must separately disallow unlimited->limited, because
3444 * in that case we have no record of how much is already in use.
3445 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003446 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003447 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003448 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003449 goto out;
3450
3451 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003452 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003453 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003454 sbinfo->max_inodes = config.max_inodes;
3455 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003456
Greg Thelen5f001102013-02-22 16:36:01 -08003457 /*
3458 * Preserve previous mempolicy unless mpol remount option was specified.
3459 */
3460 if (config.mpol) {
3461 mpol_put(sbinfo->mpol);
3462 sbinfo->mpol = config.mpol; /* transfers initial ref */
3463 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003464out:
3465 spin_unlock(&sbinfo->stat_lock);
3466 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003468
Al Viro34c80b12011-12-08 21:32:45 -05003469static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003470{
Al Viro34c80b12011-12-08 21:32:45 -05003471 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003472
3473 if (sbinfo->max_blocks != shmem_default_max_blocks())
3474 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003475 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003476 if (sbinfo->max_inodes != shmem_default_max_inodes())
3477 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003478 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003479 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003480 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3481 seq_printf(seq, ",uid=%u",
3482 from_kuid_munged(&init_user_ns, sbinfo->uid));
3483 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3484 seq_printf(seq, ",gid=%u",
3485 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003486#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003487 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3488 if (sbinfo->huge)
3489 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3490#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003491 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003492 return 0;
3493}
David Herrmann9183df22014-08-08 14:25:29 -07003494
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003495#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497static void shmem_put_super(struct super_block *sb)
3498{
Hugh Dickins602586a2010-08-17 15:23:56 -07003499 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3500
3501 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003502 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003503 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 sb->s_fs_info = NULL;
3505}
3506
Kay Sievers2b2af542009-04-30 15:23:42 +02003507int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
3509 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003510 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003511 int err = -ENOMEM;
3512
3513 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003514 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003515 L1_CACHE_BYTES), GFP_KERNEL);
3516 if (!sbinfo)
3517 return -ENOMEM;
3518
Joe Perches0825a6f2018-06-14 15:27:58 -07003519 sbinfo->mode = 0777 | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003520 sbinfo->uid = current_fsuid();
3521 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003522 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003524#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 /*
3526 * Per default we only allow half of the physical ram per
3527 * tmpfs instance, limiting inodes to one per page of lowmem;
3528 * but the internal instance is left unlimited.
3529 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003530 if (!(sb->s_flags & SB_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003531 sbinfo->max_blocks = shmem_default_max_blocks();
3532 sbinfo->max_inodes = shmem_default_max_inodes();
3533 if (shmem_parse_options(data, sbinfo, false)) {
3534 err = -EINVAL;
3535 goto failed;
3536 }
Al Viroca4e0512013-08-31 12:57:10 -04003537 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003538 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
David M. Grimes91828a42006-10-17 00:09:45 -07003540 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003541 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003543 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544#endif
3545
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003546 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003547 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003548 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003549 sbinfo->free_inodes = sbinfo->max_inodes;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003550 spin_lock_init(&sbinfo->shrinklist_lock);
3551 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003552
Hugh Dickins285b2c42011-08-03 16:21:20 -07003553 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003554 sb->s_blocksize = PAGE_SIZE;
3555 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 sb->s_magic = TMPFS_MAGIC;
3557 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003558 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003559#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003560 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003561#endif
3562#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003563 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003564#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003565 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003566
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003567 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if (!inode)
3569 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003570 inode->i_uid = sbinfo->uid;
3571 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003572 sb->s_root = d_make_root(inode);
3573 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003574 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return 0;
3576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577failed:
3578 shmem_put_super(sb);
3579 return err;
3580}
3581
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003582static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584static struct inode *shmem_alloc_inode(struct super_block *sb)
3585{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003586 struct shmem_inode_info *info;
3587 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3588 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003590 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003593static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003594{
3595 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003596 if (S_ISLNK(inode->i_mode))
3597 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003598 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3599}
3600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601static void shmem_destroy_inode(struct inode *inode)
3602{
Al Viro09208d12011-07-26 03:15:03 -04003603 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003605 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003608static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003610 struct shmem_inode_info *info = foo;
3611 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003614static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3617 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003618 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003621static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003623 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003626static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003628 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003630 .write_begin = shmem_write_begin,
3631 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003633#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003634 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003635#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003636 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637};
3638
Helge Deller15ad7cd2006-12-06 20:40:36 -08003639static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003641 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003643 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003644 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003645 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003646 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003647 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003648 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003649 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650#endif
3651};
3652
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003653static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003654 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003655 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003656#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003657 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003658 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003659#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660};
3661
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003662static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663#ifdef CONFIG_TMPFS
3664 .create = shmem_create,
3665 .lookup = simple_lookup,
3666 .link = shmem_link,
3667 .unlink = shmem_unlink,
3668 .symlink = shmem_symlink,
3669 .mkdir = shmem_mkdir,
3670 .rmdir = shmem_rmdir,
3671 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003672 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003673 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003675#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003676 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003677#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003678#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003679 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003680 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003681#endif
3682};
3683
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003684static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003685#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003686 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003687#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003688#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003689 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003690 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692};
3693
Hugh Dickins759b9772007-03-05 00:30:28 -08003694static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 .alloc_inode = shmem_alloc_inode,
3696 .destroy_inode = shmem_destroy_inode,
3697#ifdef CONFIG_TMPFS
3698 .statfs = shmem_statfs,
3699 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003700 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701#endif
Al Viro1f895f72010-06-05 19:10:41 -04003702 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 .drop_inode = generic_delete_inode,
3704 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003705#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3706 .nr_cached_objects = shmem_unused_huge_count,
3707 .free_cached_objects = shmem_unused_huge_scan,
3708#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709};
3710
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003711static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003712 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003713 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714#ifdef CONFIG_NUMA
3715 .set_policy = shmem_set_policy,
3716 .get_policy = shmem_get_policy,
3717#endif
3718};
3719
Al Viro3c26ff62010-07-25 11:46:36 +04003720static struct dentry *shmem_mount(struct file_system_type *fs_type,
3721 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
Al Viro3c26ff62010-07-25 11:46:36 +04003723 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003726static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 .owner = THIS_MODULE,
3728 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003729 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003731 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003734int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
3736 int error;
3737
Rob Landley16203a72013-09-11 14:26:12 -07003738 /* If rootfs called this, don't re-init */
3739 if (shmem_inode_cachep)
3740 return 0;
3741
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003742 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003744 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003746 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 goto out2;
3748 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003749
Al Viroca4e0512013-08-31 12:57:10 -04003750 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 if (IS_ERR(shm_mnt)) {
3752 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003753 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 goto out1;
3755 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003756
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003757#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003758 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003759 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3760 else
3761 shmem_huge = 0; /* just in case it was patched */
3762#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return 0;
3764
3765out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003766 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003768 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 shm_mnt = ERR_PTR(error);
3770 return error;
3771}
Matt Mackall853ac432009-01-06 14:40:20 -08003772
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003773#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003774static ssize_t shmem_enabled_show(struct kobject *kobj,
3775 struct kobj_attribute *attr, char *buf)
3776{
3777 int values[] = {
3778 SHMEM_HUGE_ALWAYS,
3779 SHMEM_HUGE_WITHIN_SIZE,
3780 SHMEM_HUGE_ADVISE,
3781 SHMEM_HUGE_NEVER,
3782 SHMEM_HUGE_DENY,
3783 SHMEM_HUGE_FORCE,
3784 };
3785 int i, count;
3786
3787 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3788 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3789
3790 count += sprintf(buf + count, fmt,
3791 shmem_format_huge(values[i]));
3792 }
3793 buf[count - 1] = '\n';
3794 return count;
3795}
3796
3797static ssize_t shmem_enabled_store(struct kobject *kobj,
3798 struct kobj_attribute *attr, const char *buf, size_t count)
3799{
3800 char tmp[16];
3801 int huge;
3802
3803 if (count + 1 > sizeof(tmp))
3804 return -EINVAL;
3805 memcpy(tmp, buf, count);
3806 tmp[count] = '\0';
3807 if (count && tmp[count - 1] == '\n')
3808 tmp[count - 1] = '\0';
3809
3810 huge = shmem_parse_huge(tmp);
3811 if (huge == -EINVAL)
3812 return -EINVAL;
3813 if (!has_transparent_hugepage() &&
3814 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3815 return -EINVAL;
3816
3817 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003818 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003819 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3820 return count;
3821}
3822
3823struct kobj_attribute shmem_enabled_attr =
3824 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07003825#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003826
Arnd Bergmann3b337192016-08-10 16:27:44 -07003827#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003828bool shmem_huge_enabled(struct vm_area_struct *vma)
3829{
3830 struct inode *inode = file_inode(vma->vm_file);
3831 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3832 loff_t i_size;
3833 pgoff_t off;
3834
3835 if (shmem_huge == SHMEM_HUGE_FORCE)
3836 return true;
3837 if (shmem_huge == SHMEM_HUGE_DENY)
3838 return false;
3839 switch (sbinfo->huge) {
3840 case SHMEM_HUGE_NEVER:
3841 return false;
3842 case SHMEM_HUGE_ALWAYS:
3843 return true;
3844 case SHMEM_HUGE_WITHIN_SIZE:
3845 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3846 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3847 if (i_size >= HPAGE_PMD_SIZE &&
3848 i_size >> PAGE_SHIFT >= off)
3849 return true;
Gustavo A. R. Silvac8402872017-11-15 17:38:45 -08003850 /* fall through */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003851 case SHMEM_HUGE_ADVISE:
3852 /* TODO: implement fadvise() hints */
3853 return (vma->vm_flags & VM_HUGEPAGE);
3854 default:
3855 VM_BUG_ON(1);
3856 return false;
3857 }
3858}
Arnd Bergmann3b337192016-08-10 16:27:44 -07003859#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003860
Matt Mackall853ac432009-01-06 14:40:20 -08003861#else /* !CONFIG_SHMEM */
3862
3863/*
3864 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3865 *
3866 * This is intended for small system where the benefits of the full
3867 * shmem code (swap-backed and resource-limited) are outweighed by
3868 * their complexity. On systems without swap this code should be
3869 * effectively equivalent, but much lighter weight.
3870 */
3871
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003872static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003873 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003874 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003875 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003876 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003877};
3878
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003879int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003880{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003881 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003882
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003883 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003884 BUG_ON(IS_ERR(shm_mnt));
3885
3886 return 0;
3887}
3888
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003889int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003890{
3891 return 0;
3892}
3893
Hugh Dickins3f96b792009-09-21 17:03:37 -07003894int shmem_lock(struct file *file, int lock, struct user_struct *user)
3895{
3896 return 0;
3897}
3898
Hugh Dickins24513262012-01-20 14:34:21 -08003899void shmem_unlock_mapping(struct address_space *mapping)
3900{
3901}
3902
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003903#ifdef CONFIG_MMU
3904unsigned long shmem_get_unmapped_area(struct file *file,
3905 unsigned long addr, unsigned long len,
3906 unsigned long pgoff, unsigned long flags)
3907{
3908 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3909}
3910#endif
3911
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003912void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003913{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003914 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003915}
3916EXPORT_SYMBOL_GPL(shmem_truncate_range);
3917
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003918#define shmem_vm_ops generic_file_vm_ops
3919#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003920#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003921#define shmem_acct_size(flags, size) 0
3922#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003923
3924#endif /* CONFIG_SHMEM */
3925
3926/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Matthew Auld703321b2017-10-06 23:18:13 +01003928static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00003929 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04003932 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Matthew Auld703321b2017-10-06 23:18:13 +01003934 if (IS_ERR(mnt))
3935 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Hugh Dickins285b2c42011-08-03 16:21:20 -07003937 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 return ERR_PTR(-EINVAL);
3939
3940 if (shmem_acct_size(flags, size))
3941 return ERR_PTR(-ENOMEM);
3942
Al Viro93dec2d2018-07-08 23:02:03 -04003943 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
3944 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04003945 if (unlikely(!inode)) {
3946 shmem_unacct_size(flags, size);
3947 return ERR_PTR(-ENOSPC);
3948 }
Eric Parisc7277092013-12-02 11:24:19 +00003949 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003951 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003952 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04003953 if (!IS_ERR(res))
3954 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
3955 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05003956 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04003957 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05003958 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
Eric Parisc7277092013-12-02 11:24:19 +00003960
3961/**
3962 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3963 * kernel internal. There will be NO LSM permission checks against the
3964 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003965 * higher layer. The users are the big_key and shm implementations. LSM
3966 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003967 * @name: name for dentry (to be seen in /proc/<pid>/maps
3968 * @size: size to be set for the file
3969 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3970 */
3971struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3972{
Matthew Auld703321b2017-10-06 23:18:13 +01003973 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00003974}
3975
3976/**
3977 * shmem_file_setup - get an unlinked file living in tmpfs
3978 * @name: name for dentry (to be seen in /proc/<pid>/maps
3979 * @size: size to be set for the file
3980 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3981 */
3982struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3983{
Matthew Auld703321b2017-10-06 23:18:13 +01003984 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00003985}
Keith Packard395e0dd2008-06-20 00:08:06 -07003986EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Randy Dunlap46711812008-03-19 17:00:41 -07003988/**
Matthew Auld703321b2017-10-06 23:18:13 +01003989 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
3990 * @mnt: the tmpfs mount where the file will be created
3991 * @name: name for dentry (to be seen in /proc/<pid>/maps
3992 * @size: size to be set for the file
3993 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3994 */
3995struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
3996 loff_t size, unsigned long flags)
3997{
3998 return __shmem_file_setup(mnt, name, size, flags, 0);
3999}
4000EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4001
4002/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
4005 */
4006int shmem_zero_setup(struct vm_area_struct *vma)
4007{
4008 struct file *file;
4009 loff_t size = vma->vm_end - vma->vm_start;
4010
Hugh Dickins66fc1302015-06-14 09:48:09 -07004011 /*
4012 * Cloning a new file under mmap_sem leads to a lock ordering conflict
4013 * between XFS directory reading and selinux: since this file is only
4014 * accessible to the user through its mapping, use S_PRIVATE flag to
4015 * bypass file security, in the same way as shmem_kernel_file_setup().
4016 */
Matthew Auld703321b2017-10-06 23:18:13 +01004017 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 if (IS_ERR(file))
4019 return PTR_ERR(file);
4020
4021 if (vma->vm_file)
4022 fput(vma->vm_file);
4023 vma->vm_file = file;
4024 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004025
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07004026 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004027 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4028 (vma->vm_end & HPAGE_PMD_MASK)) {
4029 khugepaged_enter(vma, vma->vm_flags);
4030 }
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return 0;
4033}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004034
4035/**
4036 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4037 * @mapping: the page's address_space
4038 * @index: the page index
4039 * @gfp: the page allocator flags to use if allocating
4040 *
4041 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4042 * with any new page allocations done using the specified allocation flags.
4043 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4044 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4045 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4046 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004047 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4048 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004049 */
4050struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4051 pgoff_t index, gfp_t gfp)
4052{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004053#ifdef CONFIG_SHMEM
4054 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004055 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004056 int error;
4057
4058 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004059 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004060 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004061 if (error)
4062 page = ERR_PTR(error);
4063 else
4064 unlock_page(page);
4065 return page;
4066#else
4067 /*
4068 * The tiny !SHMEM case uses ramfs without swap
4069 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004070 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004071#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004072}
4073EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);